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

source

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")?;
source

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")?;
source

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.
source

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.
source

pub fn eval_binary(&self, bytecode: &[u8]) -> Result<JSValueRef<'_>>

Evaluate QuickJS bytecode produced by Self::compile_module or Self::compile_global.

source

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.

source

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.

source

pub fn global_object(&self) -> Result<JSValueRef<'_>>

Retrieves the global object of the JavaScript context.

source

pub fn array_value(&self) -> Result<JSValueRef<'_>>

Creates a new JavaScript Array object.

source

pub fn array_buffer_value(&self, bytes: &[u8]) -> Result<JSValueRef<'_>>

Creates a new JavaScript ArrayBuffer object with the specified bytes.

source

pub fn object_value(&self) -> Result<JSValueRef<'_>>

Creates a new JavaScript Object.

source

pub fn value_from_f64(&self, val: f64) -> Result<JSValueRef<'_>>

Creates a new JavaScript Number object from a f64 value.

source

pub fn value_from_i32(&self, val: i32) -> Result<JSValueRef<'_>>

Creates a new JavaScript Number object from an i32 value.

source

pub fn value_from_i64(&self, val: i64) -> Result<JSValueRef<'_>>

Creates a new JavaScript Number or BigInt object from an i64 value.

source

pub fn value_from_u64(&self, val: u64) -> Result<JSValueRef<'_>>

Creates a new JavaScript Number or BigInt object from a u64 value.

source

pub fn value_from_u32(&self, val: u32) -> Result<JSValueRef<'_>>

Creates a new JavaScript Number object from a u32 value.

source

pub fn value_from_bool(&self, val: bool) -> Result<JSValueRef<'_>>

Creates a new JavaScript Boolean object from a bool value.

source

pub fn value_from_str(&self, val: &str) -> Result<JSValueRef<'_>>

Creates a new JavaScript String object from a &str value.

source

pub fn null_value(&self) -> Result<JSValueRef<'_>>

Creates a new JavaScript Null object.

source

pub fn undefined_value(&self) -> Result<JSValueRef<'_>>

Creates a new JavaScript Undefined object.

source

pub fn wrap_callback<F>(&self, f: F) -> Result<JSValueRef<'_>>
where F: FnMut(&Self, JSValueRef<'_>, &[JSValueRef<'_>]) -> Result<JSValue> + 'static,

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.

source

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.

Trait Implementations§

source§

impl Debug for JSContextRef

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for JSContextRef

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.