Rust bindings to QuickJS.
The JSValue of QuickJS is using reference counting to manage the memory. So we
create a Local handle to help you manage the reference count. The Local
implements Clone using JSDupValue to increment the reference count, and
implements Drop using JSFreeValue to decrement the reference count. You can
simply use to_local
to convert a JSValue into a Local handle, then enjoy the
conveniences of it.
```rust use rusty_qjs::{CallContext, JSContext, JSRuntime, JSValue}; use std::io::Write;
extern "C" fn jsprint( ctx: *mut JSContext, thisval: JSValue, argc: i32, argv: *mut JSValue, ) -> JSValue { let mut ctx = unsafe { ctx.asmut() }.unwrap(); let mut callctx = CallContext::new(&mut ctx, this_val, argc, argv);
let mut stdout = std::io::stdout(); for i in 0..callctx.argc { if i != 0 { stdout.writeall(b" ").unwrap(); } let val = callctx.get(i).unwrap(); stdout .writeall(val.tostring(callctx.jscontext).asbytes()) .unwrap(); } stdout.writeall(b"\n").unwrap(); JSValue::newundefined() }
fn setupconsole(ctx: &mut JSContext) { let global = ctx.getglobalobject().tolocal(ctx); let console = JSValue::newobject(ctx).tolocal(ctx); let log = JSValue::newfunction(ctx, jsprint, "log", 1).to_local(ctx);
console.setpropertystr("log", log).unwrap(); global.setpropertystr("console", console).unwrap(); }
fn main() { let rt = &mut JSRuntime::new(); let ctx = &mut JSContext::new(rt);
setupconsole(ctx);
ctx.evalscript("console.log(\"hello world\")", "