Skip to content

Instantly share code, notes, and snippets.

@emk
Created December 12, 2014 11:09
Show Gist options
  • Select an option

  • Save emk/88b6b2149e8d767bc537 to your computer and use it in GitHub Desktop.

Select an option

Save emk/88b6b2149e8d767bc537 to your computer and use it in GitHub Desktop.

Revisions

  1. emk created this gist Dec 12, 2014.
    25 changes: 25 additions & 0 deletions call.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    /// Call the global JavaScript function named `fn_name` with `args`, and
    /// return the result.
    pub fn call<'a>(&mut self, fn_name: &str,
    args: &[&Encodable<Encoder<'a>, DuktapeError>]) ->
    DuktapeResult<Value<'static>>
    {
    unsafe {
    assert_stack_height_unchanged!(self, {
    duk_push_global_object(self.ptr);
    fn_name.with_c_str(|c_str| {
    duk_get_prop_string(self.ptr, -1, c_str);
    });
    {
    let mut encoder = Encoder::new(self);
    for arg in args.iter() {
    arg.encode(&mut encoder).unwrap();
    }
    }
    let status = duk_pcall(self.ptr, args.len() as i32);
    let result = self.pop_result(status);
    duk_pop(self.ptr); // Remove global object.
    result
    })
    }
    }