Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binding to napi_run_script #692

Merged
merged 7 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/neon-runtime/src/napi/bindings/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ mod napi1 {
finalize_hint: *mut c_void,
result: *mut Value,
) -> Status;

fn run_script(env: Env, script: Value, result: *mut Value) -> Status;
}
);
}
Expand Down
6 changes: 6 additions & 0 deletions crates/neon-runtime/src/napi/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ pub unsafe fn data(env: Env, out: *mut u8, len: isize, value: Local) -> isize {

read.assume_init() as isize
}

pub unsafe fn run_script(out: &mut Local, env: Env, value: Local) -> bool {
let status = napi::run_script(env, value, out as *mut _);

status == napi::Status::Ok
kjvalencik marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod handle;
pub mod meta;
pub mod object;
pub mod prelude;
pub mod reflect;
pub mod result;
#[cfg(feature = "legacy-runtime")]
pub mod task;
Expand Down
15 changes: 15 additions & 0 deletions src/reflect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use context::Context;
use handle::{Handle, Managed};
use result::JsResult;
use types::{build, JsString, JsValue};

#[cfg(feature = "napi-1")]
pub fn eval<'a, 'b, C: Context<'a>>(
cx: &mut C,
script: Handle<'b, JsString>,
) -> JsResult<'a, JsValue> {
let env = cx.env().to_raw();
build(cx.env(), |out| unsafe {
neon_runtime::string::run_script(out, env, script.to_raw())
})
}
13 changes: 12 additions & 1 deletion test/napi/lib/strings.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
var addon = require('..');
var assert = require('chai').assert;
var { assert, expect } = require('chai');

describe('JsString', function() {
it('should return a JsString built in Rust', function () {
assert.equal(addon.return_js_string(), "hello node");
});
describe('run_as_script', function () {
it('should return the evaluated value', function () {
assert.equal(addon.run_string_as_script('6 * 7'), 42);
});
it('should throw if the script throws', function () {
expect(() => addon.run_string_as_script('throw new Error("b1-66er")')).to.throw('b1-66er');
});
it('should throw SyntaxError if the string has invalid syntax', function () {
expect(() => addon.run_string_as_script('invalid js code')).to.throw(SyntaxError);
});
})
});
6 changes: 6 additions & 0 deletions test/napi/src/js/strings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use neon::prelude::*;
use neon::reflect::eval;

pub fn return_js_string(mut cx: FunctionContext) -> JsResult<JsString> {
Ok(cx.string("hello node"))
}

pub fn run_string_as_script(mut cx: FunctionContext) -> JsResult<JsValue> {
let string_script = cx.argument::<JsString>(0)?;
eval(&mut cx, string_script)
}
1 change: 1 addition & 0 deletions test/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
cx.export_function("add1", add1)?;

cx.export_function("return_js_string", return_js_string)?;
cx.export_function("run_string_as_script", run_string_as_script)?;

cx.export_function("return_js_number", return_js_number)?;
cx.export_function("return_large_js_number", return_large_js_number)?;
Expand Down