#javascript #interpreter #error #encodable #convert #string #function

duktape

Embedded JavaScript interpreter with a small footprint

2 releases

Uses old Rust 2015

0.0.2 Jan 28, 2015
0.0.1 Dec 7, 2014

#3 in #encodable

Unlicense

43KB
895 lines

Build Status

Documentation.

WORK IN PROGRESS.

A Rust wrapper for Duktape. Things to do before this is minimally useful:

  • Handle non-UTF-8 strings.
  • Call JavaScript functions by name.
  • Define functions.
    • Call specified Rust functions from JavaScript.
    • Return errors from Rust to JavaScript.
  • Convert to use Encodable/Decodable everywhere.
    • Convert parameters to use Encodable.
    • Replace Value with serialize::Json.
    • Convert return values to use Decodable.
  • Add nice macros.
    • Provide macro for calling functions.
    • Provide macro for defining functions.

lib.rs:

Rust interface to Duktape JavaScript interpreter. This is still a work in progress!

Source code.

use duktape::{Context,Value,DuktapeResult};

fn add_example() -> DuktapeResult<Value<'static>> {
    // Create a new JavaScript interpreter.  This will be automatically
    // cleaned up when `ctx` goes out of scope.
    let mut ctx = try!(Context::new());

    // Load some code from a string.
    try!(ctx.eval("function add(x, y) { return x+y; }"));

    // Call the function we defined.
    ctx.call("add", &[&2.0f64, &1.0f64])
}

assert_eq!(Ok(Value::Number(3.0)), add_example());

We also have preliminary support for defining JavaScript functions using Rust, but it's still too ugly to show off.

Dependencies

~250KB