Skip to content

Commit

Permalink
Merge pull request #102 from rustbridge/expose-this-trait
Browse files Browse the repository at this point in the history
closes #101
  • Loading branch information
Dave Herman committed Aug 8, 2016
2 parents e27e565 + e476b7b commit 9444be9
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/internal/js/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ pub trait Class: Managed + Any {
}
}

impl<T: Class> This for T {
unsafe impl<T: Class> This for T {
fn as_this(h: raw::Local) -> Self {
Self::from_raw(h)
}
Expand Down
6 changes: 3 additions & 3 deletions src/internal/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl ValueInternal for JsValue {
}
}

impl This for JsValue {
unsafe impl This for JsValue {
fn as_this(h: raw::Local) -> Self {
JsValue(h)
}
Expand Down Expand Up @@ -154,7 +154,7 @@ impl Managed for JsUndefined {
fn from_raw(h: raw::Local) -> Self { JsUndefined(h) }
}

impl This for JsUndefined {
unsafe impl This for JsUndefined {
fn as_this(_: raw::Local) -> Self {
unsafe {
let mut local: raw::Local = mem::zeroed();
Expand Down Expand Up @@ -499,7 +499,7 @@ impl Managed for JsObject {
fn from_raw(h: raw::Local) -> Self { JsObject(h) }
}

impl This for JsObject {
unsafe impl This for JsObject {
fn as_this(h: raw::Local) -> Self { JsObject(h) }
}

Expand Down
3 changes: 2 additions & 1 deletion src/internal/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ extern "C" fn module_callback<'a>(kernel: fn(Module) -> VmResult<()>, exports: H
});
}

pub trait This: Managed {
/// A type that may be the type of a function's `this` binding.
pub unsafe trait This: Managed {
fn as_this(h: raw::Local) -> Self;
}

Expand Down
2 changes: 1 addition & 1 deletion src/vm.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//! Abstractions representing the JavaScript virtual machine and its control flow.

pub use internal::vm::{Call, FunctionCall, Arguments, Module, Throw, VmResult, JsResult, Lock};
pub use internal::vm::{Call, FunctionCall, This, Arguments, Module, Throw, VmResult, JsResult, Lock};
4 changes: 4 additions & 0 deletions tests/lib/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ describe('JsFunction', function() {
it('new a JsFunction', function () {
assert.equal(addon.construct_js_function(Date), 1970);
});

it('got two parameters, a string and a number', function() {
addon.check_string_and_number("string", 42);
});
});
20 changes: 18 additions & 2 deletions tests/native/src/js/functions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use neon::vm::{Call, JsResult};
use neon::vm::{Call, JsResult, This, FunctionCall};
use neon::mem::Handle;
use neon::js::{JsNumber, JsNull, JsFunction, Object, JsValue};
use neon::js::{JsNumber, JsNull, JsFunction, Object, JsValue, JsUndefined, JsString, Value};

fn add1(call: Call) -> JsResult<JsNumber> {
let scope = call.scope;
Expand Down Expand Up @@ -28,3 +28,19 @@ pub fn construct_js_function(call: Call) -> JsResult<JsNumber> {
let args: Vec<Handle<JsValue>> = vec![];
try!(get_utc_full_year_method.call(scope, o.upcast::<JsValue>(), args)).check::<JsNumber>()
}

trait CheckArgument<'a> {
fn check_argument<V: Value>(&mut self, i: i32) -> JsResult<'a, V>;
}

impl<'a, T: This> CheckArgument<'a> for FunctionCall<'a, T> {
fn check_argument<V: Value>(&mut self, i: i32) -> JsResult<'a, V> {
try!(self.arguments.require(self.scope, i)).check::<V>()
}
}

pub fn check_string_and_number(mut call: Call) -> JsResult<JsUndefined> {
let x = try!(call.check_argument::<JsString>(0));
let y = try!(call.check_argument::<JsNumber>(1));
Ok(JsUndefined::new())
}
2 changes: 2 additions & 0 deletions tests/native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ register_module!(m, {
try!(m.export("call_js_function", call_js_function));
try!(m.export("construct_js_function", construct_js_function));

try!(m.export("check_string_and_number", check_string_and_number));

let class: Handle<JsClass<JsUser>> = try!(JsUser::class(m.scope));
let constructor: Handle<JsFunction<JsUser>> = try!(class.constructor(m.scope));
try!(m.exports.set("User", constructor));
Expand Down

0 comments on commit 9444be9

Please sign in to comment.