-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from rustbridge/dherman-function-call
closes #60: call JS functions from Rust
- Loading branch information
Showing
7 changed files
with
77 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var addon = require('../native'); | ||
var assert = require('chai').assert; | ||
|
||
describe('JsFunction', function() { | ||
it('should return a JsFunction built in Rust', function () { | ||
assert.isFunction(addon.return_js_function()); | ||
}); | ||
|
||
it('should return a JsFunction built in Rust that implements x => x + 1', function () { | ||
assert.equal(addon.return_js_function()(41), 42); | ||
}); | ||
|
||
it('should call a JsFunction built in JS that implements x => x + 1', function () { | ||
assert.equal(addon.call_js_function(function(x) { return x + 1 }), 17); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use neon::vm::{Call, JsResult}; | ||
use neon::mem::Handle; | ||
use neon::js::{JsNumber, JsNull, JsFunction}; | ||
|
||
fn add1(call: Call) -> JsResult<JsNumber> { | ||
let scope = call.scope; | ||
let x = try!(try!(call.arguments.require(scope, 0)).check::<JsNumber>()).value(); | ||
Ok(JsNumber::new(scope, x + 1.0)) | ||
} | ||
|
||
pub fn return_js_function(call: Call) -> JsResult<JsFunction> { | ||
JsFunction::new(call.scope, add1) | ||
} | ||
|
||
pub fn call_js_function(call: Call) -> JsResult<JsNumber> { | ||
let scope = call.scope; | ||
let f = try!(try!(call.arguments.require(scope, 0)).check::<JsFunction>()); | ||
let args: Vec<Handle<JsNumber>> = vec![JsNumber::new(scope, 16.0)]; | ||
f.call(scope, JsNull::new(), args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters