Struct quickjs_wasm_rs::JSContextRef
source · pub struct JSContextRef { /* private fields */ }
Expand description
JSContextRef
is a wrapper around a raw pointer to a QuickJS JSContext
.
This struct provides a safe interface for interacting with the underlying QuickJS context. It is primarily used for managing the JavaScript execution environment, such as creating and manipulating JavaScript objects, functions, and values.
§Safety
The raw pointer to JSContext
is not exposed publicly, ensuring that
the lifetime of the JSContextRef
does not outlive the lifetime of the
QuickJS context it refers to.
§Example
// Assuming you have a function to create a new QuickJS context
let context = JSContextRef::default();
context.eval_global("test.js", "1 + 1")?;
Implementations§
source§impl JSContextRef
impl JSContextRef
sourcepub fn eval_global(&self, name: &str, contents: &str) -> Result<JSValueRef<'_>>
pub fn eval_global(&self, name: &str, contents: &str) -> Result<JSValueRef<'_>>
Evaluates JavaScript code in the global scope.
This method takes JavaScript code as a string and evaluates it in the global scope of the JavaScript context.
§Arguments
name
: A string representing the name of the script.contents
: The JavaScript code to be evaluated as a string.
§Example
let context = JSContextRef::default();
context.eval_global("test.js", "1 + 1")?;
sourcepub fn eval_module(&self, name: &str, contents: &str) -> Result<JSValueRef<'_>>
pub fn eval_module(&self, name: &str, contents: &str) -> Result<JSValueRef<'_>>
Evaluates JavaScript code in an ECMAScript module scope.
This method takes JavaScript code as a string and evaluates it in a ECMAScript module scope.
§Arguments
name
: A string representing the name of the module.contents
: The JavaScript code to be evaluated as a string.
§Example
let contenxt = JSContextRef::default();
content.eval_module("test.js", "1 + 1")?;
sourcepub fn compile_module(&self, name: &str, contents: &str) -> Result<Vec<u8>>
pub fn compile_module(&self, name: &str, contents: &str) -> Result<Vec<u8>>
Compiles JavaScript to QuickJS bytecode with an ECMAScript module scope.
§Arguments
name
: A string representing the name of the script.contents
: The JavaScript code to be compiled as a string.
sourcepub fn compile_global(&self, name: &str, contents: &str) -> Result<Vec<u8>>
pub fn compile_global(&self, name: &str, contents: &str) -> Result<Vec<u8>>
Compiles JavaScript to QuickJS bytecode with a global scope.
§Arguments
name
: A string representing the name of the script.contents
: The JavaScript code to be compiled as a string.
sourcepub fn eval_binary(&self, bytecode: &[u8]) -> Result<JSValueRef<'_>>
pub fn eval_binary(&self, bytecode: &[u8]) -> Result<JSValueRef<'_>>
Evaluate QuickJS bytecode produced by Self::compile_module
or
Self::compile_global
.
sourcepub fn is_pending(&self) -> bool
pub fn is_pending(&self) -> bool
Checks if there are any pending jobs in the JavaScript context.
This method returns true
if there are pending jobs (for example, promises) in the
JavaScript context, and false
otherwise.
sourcepub fn execute_pending(&self) -> Result<()>
pub fn execute_pending(&self) -> Result<()>
Executes all pending jobs in the JavaScript context.
This method executes all pending jobs (e.g., promises) in the JavaScript context
until there are no more pending jobs or an exception occurs. It returns a Result
indicating
whether the execution was successful or an error if an exception was thrown.
sourcepub fn global_object(&self) -> Result<JSValueRef<'_>>
pub fn global_object(&self) -> Result<JSValueRef<'_>>
Retrieves the global object of the JavaScript context.
sourcepub fn array_value(&self) -> Result<JSValueRef<'_>>
pub fn array_value(&self) -> Result<JSValueRef<'_>>
Creates a new JavaScript Array object.
sourcepub fn array_buffer_value(&self, bytes: &[u8]) -> Result<JSValueRef<'_>>
pub fn array_buffer_value(&self, bytes: &[u8]) -> Result<JSValueRef<'_>>
Creates a new JavaScript ArrayBuffer object with the specified bytes.
sourcepub fn object_value(&self) -> Result<JSValueRef<'_>>
pub fn object_value(&self) -> Result<JSValueRef<'_>>
Creates a new JavaScript Object.
sourcepub fn value_from_f64(&self, val: f64) -> Result<JSValueRef<'_>>
pub fn value_from_f64(&self, val: f64) -> Result<JSValueRef<'_>>
Creates a new JavaScript Number object from a f64
value.
sourcepub fn value_from_i32(&self, val: i32) -> Result<JSValueRef<'_>>
pub fn value_from_i32(&self, val: i32) -> Result<JSValueRef<'_>>
Creates a new JavaScript Number object from an i32
value.
sourcepub fn value_from_i64(&self, val: i64) -> Result<JSValueRef<'_>>
pub fn value_from_i64(&self, val: i64) -> Result<JSValueRef<'_>>
Creates a new JavaScript Number or BigInt object from an i64
value.
sourcepub fn value_from_u64(&self, val: u64) -> Result<JSValueRef<'_>>
pub fn value_from_u64(&self, val: u64) -> Result<JSValueRef<'_>>
Creates a new JavaScript Number or BigInt object from a u64
value.
sourcepub fn value_from_u32(&self, val: u32) -> Result<JSValueRef<'_>>
pub fn value_from_u32(&self, val: u32) -> Result<JSValueRef<'_>>
Creates a new JavaScript Number object from a u32
value.
sourcepub fn value_from_bool(&self, val: bool) -> Result<JSValueRef<'_>>
pub fn value_from_bool(&self, val: bool) -> Result<JSValueRef<'_>>
Creates a new JavaScript Boolean object from a bool
value.
sourcepub fn value_from_str(&self, val: &str) -> Result<JSValueRef<'_>>
pub fn value_from_str(&self, val: &str) -> Result<JSValueRef<'_>>
Creates a new JavaScript String object from a &str
value.
sourcepub fn null_value(&self) -> Result<JSValueRef<'_>>
pub fn null_value(&self) -> Result<JSValueRef<'_>>
Creates a new JavaScript Null object.
sourcepub fn undefined_value(&self) -> Result<JSValueRef<'_>>
pub fn undefined_value(&self) -> Result<JSValueRef<'_>>
Creates a new JavaScript Undefined object.
sourcepub fn wrap_callback<F>(&self, f: F) -> Result<JSValueRef<'_>>
pub fn wrap_callback<F>(&self, f: F) -> Result<JSValueRef<'_>>
Wrap the specified function in a JS function.
Since the callback signature accepts parameters as high-level JSContextRef
and JSValueRef
objects, it can be
implemented without using unsafe
code, unlike JSContextRef::new_callback which provides a low-level API.
Returning a JSError from the callback will cause a JavaScript error with the appropriate
type to be thrown.
sourcepub fn new_callback<F>(&self, f: F) -> Result<JSValueRef<'_>>
pub fn new_callback<F>(&self, f: F) -> Result<JSValueRef<'_>>
Wrap the specified function in a JS function.
See also JSContextRef::wrap_callback for a high-level equivalent.