Crate quickjs_wasm_rs
source ·Expand description
High-level bindings and serializers for a Wasm build of QuickJS.
§Bindings
JSContextRef
corresponds to a QuickJS JSContext
and JSValueRef
corresponds to a QuickJS JSValue
.
use quickjs_wasm_rs::JSContextRef;
let mut context = JSContextRef::default();
will create a new context.
§Callbacks
To create a callback to be used in JavaScript, use wrap_callback
:
use quickjs_wasm_rs::js_value::JSValue;
let context = JSContextRef::default();
let callback = context.wrap_callback(|_ctx, _this, args| {
let s = args[0].to_string();
println!("{}", s);
Ok(JSValue::Undefined)
})?;
let global = context.global_object()?;
global.set_property("print", callback)?;
§Converting to and from Rust types
When working with callbacks, it is often useful to convert to Rust types.
_this
and args
in the callback function are of type JSValueRef
which can be converted into JSValue
.
JSValue
supports try_into
to convert to Rust types.
Rust types can then be converted back to JSValue
using try_into
.
use quickjs_wasm_rs::js_value::JSValue;
ctx.wrap_callback(|_ctx, this, args| {
let this: std::collections::HashMap<String, JSValue> = this.try_into()?;
let first_arg: Vec<JSValue> = args[0].try_into()?;
let ret_val = 0;
Ok(ret_val.try_into()?)
})?;
§Serializers
This crate provides optional transcoding features for converting between
serialization formats and JSValueRef
:
messagepack
providesquickjs_wasm_rs::messagepack
for msgpack, usingrmp_serde
.json
providesquickjs_wasm_rs::json
for JSON, usingserde_json
. msgpack example:
use quickjs_wasm_rs::{messagepack, JSContextRef, JSValueRef};
let context = JSContextRef::default();
let input_bytes: &[u8] = ...;
let input_value = messagepack::transcode_input(&context, input_bytes).unwrap();
let output_value: JSValueRef = ...;
let output = messagepack::transcode_output(output_value).unwrap();
Structs§
Deserializer
is a deserializer forJSValueRef
values, implementing theserde::Deserializer
trait.Exception
represents a JavaScript exception that occurs within the QuickJS context.JSContextRef
is a wrapper around a raw pointer to a QuickJSJSContext
.JSValueRef
is a wrapper around a QuickJSJSValue
with a reference to its associatedJSContextRef
.Serializer
is a serializer forJSValueRef
values, implementing theserde::Serializer
trait.
Enums§
JSError
represents various types of JavaScript errors that can occur during the execution of JavaScript code in QuickJS.- A safe and high level representation of a JavaScript value.
Functions§
- Converts a reference to QuickJS value represented by
quickjs_wasm_rs::JSValueRef
to aJSValue
. - Converts a reference to a
JSValue
to aquickjs_wasm_rs::JSValueRef
.