From d2531352aaf80771495251b8648505cd732d01da Mon Sep 17 00:00:00 2001 From: F001 Date: Fri, 16 Dec 2022 15:56:36 +0800 Subject: [PATCH] feat(napi): add "run_script" for "Env" (#1393) * add "run_script" for "Env" * Apply suggestions from code review use `AsRef` instead of `&str` Co-authored-by: LongYinan * use `AsRef` instead of `&str` Co-authored-by: LongYinan --- crates/napi/src/env.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/napi/src/env.rs b/crates/napi/src/env.rs index 20849d77..8b8568fc 100644 --- a/crates/napi/src/env.rs +++ b/crates/napi/src/env.rs @@ -1020,6 +1020,13 @@ impl Env { check_status!(unsafe { sys::napi_close_handle_scope(self.0, handle_scope) })?; result } + + pub fn run_script>(&self, script: S)->Result { + let s = self.create_string(script.as_ref())?; + let mut raw_value = ptr::null_mut(); + check_status!(unsafe { sys::napi_run_script(self.0, s.raw(), &mut raw_value) })?; + Ok(unsafe { JsObject::from_raw_unchecked(self.0, raw_value) }) + } pub fn get_napi_version(&self) -> Result { let global = self.get_global()?;