Expand description
JS APIs for Javy.
This crate provides JS APIs you can add to Javy.
Example usage:
use javy::{quickjs::JSValue, Runtime};
use javy_apis::RuntimeExt;
let runtime = Runtime::new_with_defaults()?;
let context = runtime.context();
context.global_object()?.set_property(
"print",
context.wrap_callback(move |_ctx, _this, args| {
let str = args
.first()
.ok_or(anyhow!("Need to pass an argument"))?
.to_string();
println!("{str}");
Ok(JSValue::Undefined)
})?,
)?;
context.eval_global("hello.js", "print('hello!');")?;
If you want to customize the runtime or the APIs, you can use the
Runtime::new_with_apis
method instead to provide a javy::Config
for the underlying Runtime
or an APIConfig
for the APIs.
§Features
console
- Registers an implementation of theconsole
API.text_encoding
- Registers implementations ofTextEncoder
andTextDecoder
.random
- Overrides the implementation ofMath.random
to one that seeds the RNG on first call toMath.random
. This is helpful to enable when using Wizer to snapshot ajavy::Runtime
so that the output ofMath.random
relies on the WASI context used at runtime and not the WASI context used when Wizening. Enabling this feature will increase the size of the Wasm module that includes the Javy Runtime and will introduce an additional hostcall invocation whenMath.random
is invoked for the first time.stream_io
- Registers implementations ofJavy.IO.readSync
andJavy.IO.writeSync
.
Structs§
- A configuration for APIs added in this crate.
Traits§
Functions§
- Adds enabled JS APIs to the provided
Runtime
.