[][src]Struct ducc::Ducc

pub struct Ducc { /* fields omitted */ }

The entry point into the JavaScript execution environment.

The Duktape global variable is not available to scripts because it is possible to use its methods to violate security and safety guarantees made by this library.

Methods

impl Ducc[src]

pub fn new() -> Ducc[src]

Creates a new JavaScript execution environment.

pub fn globals(&self) -> Object[src]

Returns the global object.

pub fn compile(&self, source: &str, name: Option<&str>) -> Result<Function>[src]

Compiles a chunk of JavaScript code and returns it as a function with no arguments.

The source can be named by setting the name parameter. This is generally recommended as it results in better errors.

Equivalent to Duktape's duk_compile using DUK_COMPILE_EVAL.

pub fn exec<'ducc, R: FromValue<'ducc>>(
    &'ducc self,
    source: &str,
    name: Option<&str>,
    settings: ExecSettings
) -> Result<R>
[src]

Executes a chunk of JavaScript code and returns its result.

This is equivalent to calling Ducc::compile and Function::call immediately after. The only difference is that this function supports a settings parameter, can be used to specify one-time execution settings.

pub fn set_user_data<K, T>(
    &mut self,
    key: K,
    data: T
) -> Option<Box<dyn Any + 'static>> where
    K: ToString,
    T: Any + 'static, 
[src]

Inserts any sort of keyed value of type T into the Ducc, typically for later retrieval from within Rust functions called from within JavaScript. If a value already exists with the key, it is returned.

pub fn get_user_data<'ducc, T: Any + 'static>(
    &'ducc self,
    key: &str
) -> Option<&'ducc T>
[src]

Returns a user data value by its key, or None if no value exists with the key. If a value exists but it is not of the type T, None is returned. This is typically used by a Rust function called from within JavaScript.

pub fn remove_user_data(&mut self, key: &str) -> Option<Box<dyn Any + 'static>>[src]

Removes and returns a user data value by its key. Returns None if no value exists with the key.

pub fn create_function<'ducc, 'callback, R, F>(
    &'ducc self,
    func: F
) -> Function<'ducc> where
    R: ToValue<'callback>,
    F: 'static + Send + Fn(Invocation<'callback>) -> Result<R>, 
[src]

Wraps a Rust function or closure, creating a callable JavaScript function handle to it.

The function's return value is always a Result: If the function returns Err, the error is raised as a JavaScript error, which can be caught within JavaScript or bubbled up back into Rust. This allows using the ? operator to propagate errors through intermediate JavaScript code.

If the function returns Ok, the contained value will be converted to one or more JavaScript values. For details on Rust-to-JavaScript conversions, refer to the ToValue and ToValues traits.

pub fn create_function_mut<'ducc, 'callback, R, F>(
    &'ducc self,
    func: F
) -> Function<'ducc> where
    R: ToValue<'callback>,
    F: 'static + Send + FnMut(Invocation<'callback>) -> Result<R>, 
[src]

Wraps a mutable Rust closure, creating a callable JavaScript function handle to it.

This is a version of create_function that accepts a FnMut argument. Refer to create_function for more information about the implementation.

pub fn create_string(&self, value: &str) -> Result<String>[src]

Pass a &str to Duktape, creating and returning an interned string.

pub fn create_bytes(&self, value: &[u8]) -> Result<Bytes>[src]

Pass a &[u8] to Duktape, creating and returning an interned Bytes.

pub fn create_object(&self) -> Object[src]

Creates and returns an empty Object managed by Duktape.

pub fn create_array(&self) -> Array[src]

Creates and returns an empty Array managed by Duktape.

pub fn create_object_from<'ducc, K, V, I>(
    &'ducc self,
    iter: I
) -> Result<Object<'ducc>> where
    K: ToValue<'ducc>,
    V: ToValue<'ducc>,
    I: IntoIterator<Item = (K, V)>, 
[src]

Creates and returns an Object managed by Duktape filled with the keys and values from an iterator. Keys are coerced to object properties.

This is a thin wrapper around Ducc::create_object and Object::set. See Object::set for how this method might return an error.

pub fn coerce_string<'ducc>(
    &'ducc self,
    value: Value<'ducc>
) -> Result<String<'ducc>>
[src]

Coerces a Duktape value to a string. Nearly all JavaScript values are coercible to strings, but this may fail with a runtime error under extraordinary circumstances (e.g. if the Ecmascript ToString implementation throws an error).

pub fn coerce_number<'ducc>(&'ducc self, value: Value<'ducc>) -> Result<f64>[src]

Coerces a Duktape value to a number. Nearly all JavaScript values are coercible to numbers, but this may fail with a runtime error under extraordinary circumstances (e.g. if the Ecmascript ToNumber implementation throws an error).

This will return std::f64::NAN if the value has no numerical equivalent.

pub fn coerce_boolean(&self, value: Value) -> bool[src]

Coerces a Duktape value to a boolean (returns true if the value is "truthy", false otherwise).

Trait Implementations

impl Drop for Ducc[src]

Auto Trait Implementations

impl !Send for Ducc

impl Unpin for Ducc

impl !Sync for Ducc

impl UnwindSafe for Ducc

impl RefUnwindSafe for Ducc

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]