Skip to content

Latest commit

 

History

History
55 lines (39 loc) · 1.73 KB

README.md

File metadata and controls

55 lines (39 loc) · 1.73 KB

duktape-rs

Safe(er) rust wrapper for dukbind.

Work In Progress

This library is a work in progress and is currently limited in features.

  • Remove as_number/as_str/as_* in general and use From<T> trait instead.
  • Revisit error handling and maybe change to use anyhow.

What can it do?

At the moment, duktape-rs

  • Provides a safe* wrapper around dukbind (raw FFI bindings for duktape).

  • Provides manageable value returns that can be modified and passed back to the duktape context.

  • Supports heap pointers (for objects), including setting and getting properties of an object (as DukValue).

  • Can eval a &str and return the result (DukResult<DukValue, DukError>)

  • Supports handling (what I assume to be) most JS errors that crop up during eval minimally tested

    *Safety not guaranteed

Where are the docs?

For some reason docs.rs has a problem with compiling dukbind and won't generate them :/ Check back another time for documentation Coming Soon™

Basics

use duktape::Context;

fn main() {
    // Create a new context
    let ctx = Context::new().unwrap();
    // Eval 5+5
    let val = ctx.eval_string("5+5").unwrap();
    // Get resulting value as an i64
    println!("Result is: {}", val.as_i64().expect("Not an i64"))
}

Objects

Objects in duktape are returned as heap pointers that have to be stored and returned as a wrapper around that pointer.

let ctx = Context::new()?;

let obj: Object = ctx.eval_string("({ok: false})")?.try_into()?;

let val: bool = obj.get("ok")?.try_into()?;
println!("Value: {}", val); //-> Value: false

obj.set("ok", true)?;
println!("Object with new value: {}", obj.encode().unwrap()); //-> Object with new value: {"ok":true}