Struct quickjs_runtime::esruntimebuilder::EsRuntimeBuilder[][src]

pub struct EsRuntimeBuilder { /* fields omitted */ }

the EsRuntimeBuilder is used to init an EsRuntime

Example

use quickjs_runtime::esruntimebuilder::EsRuntimeBuilder;
// init a rt which may use 16MB of memory
let rt = EsRuntimeBuilder::new()
.memory_limit(1024*1024*16)
.build();

Implementations

impl EsRuntimeBuilder[src]

pub fn build(self) -> Arc<EsRuntime>[src]

build an EsRuntime

pub fn new() -> Self[src]

init a new EsRuntimeBuilder

pub fn module_script_loader<M>(self, loader: M) -> Self where
    M: Fn(&QuickJsContext, &str, &str) -> Option<EsScript> + Send + Sync + 'static, 
[src]

add a script loaders which will be used to load modules when they are imported from script

Example

use quickjs_runtime::esscript::EsScript;
use quickjs_runtime::esruntimebuilder::EsRuntimeBuilder;
use quickjs_runtime::quickjscontext::QuickJsContext;
fn load_module(_q_ctx: &QuickJsContext, base: &str, name: &str) -> Option<EsScript> {
    // you should load your modules from files here
    // please note that you need to return the name as absolute_path in the returned script struct
    // return None if module is not found
    use quickjs_runtime::quickjscontext::QuickJsContext;
Some(EsScript::new(name, "export const foo = 12;"))
}
fn main(){
    let rt = EsRuntimeBuilder::new()
        .module_script_loader(load_module)
        .build();
}

pub fn native_module_loader<M: NativeModuleLoader + Send + 'static>(
    self,
    loader: M
) -> Self
[src]

add a module loader which can load native functions and proxy classes

Example

use quickjs_runtime::esruntimebuilder::EsRuntimeBuilder;
use quickjs_runtime::quickjsruntime::NativeModuleLoader;
use quickjs_runtime::valueref::JSValueRef;
use quickjs_runtime::quickjscontext::QuickJsContext;
use quickjs_runtime::quickjs_utils::functions;
use quickjs_runtime::quickjs_utils::primitives::{from_bool, from_i32};
use quickjs_runtime::reflection::Proxy;
use quickjs_runtime::esscript::EsScript;

struct MyModuleLoader{}
impl NativeModuleLoader for MyModuleLoader {
    fn has_module(&self, _q_ctx: &QuickJsContext,module_name: &str) -> bool {
        module_name.eq("my_module")
    }

    fn get_module_export_names(&self, _q_ctx: &QuickJsContext, _module_name: &str) -> Vec<&str> {
        vec!["someVal", "someFunc", "SomeClass"]
    }

    fn get_module_exports(&self, q_ctx: &QuickJsContext, _module_name: &str) -> Vec<(&str, JSValueRef)> {
         
        let js_val = from_i32(1470);
        let js_func = functions::new_function_q(
            q_ctx,
            "someFunc", |_q_ctx, _this, _args| {
                return Ok(from_i32(432));
            }, 0)
            .ok().unwrap();
        let js_class = Proxy::new()
            .name("SomeClass")
            .static_method("doIt", |_q_ctx, _args|{
                return Ok(from_i32(185));
            })
            .install(q_ctx, false)
            .ok().unwrap();

        vec![("someVal", js_val), ("someFunc", js_func), ("SomeClass", js_class)]
    }
}

let rt = EsRuntimeBuilder::new()
.native_module_loader(MyModuleLoader{})
.build();

rt.eval_module_sync(EsScript::new("test_native_mod.es", "import {someVal, someFunc, SomeClass} from 'my_module';\nlet i = (someVal + someFunc() + SomeClass.doIt());\nif (i !== 2087){throw Error('i was not 2087');}")).ok().expect("script failed");

pub fn fetch_response_provider<P>(self, provider: P) -> Self where
    P: Fn(&FetchRequest) -> Box<dyn FetchResponse + Send> + Send + Sync + 'static, 
[src]

Provide a fetch response provider in order to make the fetch api work in the EsRuntime

Example


use quickjs_runtime::esruntimebuilder::EsRuntimeBuilder;
use quickjs_runtime::features::fetch::response::FetchResponse;
use quickjs_runtime::features::fetch::request::FetchRequest;
use quickjs_runtime::esscript::EsScript;
use std::time::Duration;   

struct SimpleResponse{
    read_done: bool
}

impl SimpleResponse {
    fn new(_req: &FetchRequest) -> Self {
        Self{read_done:false}
    }
}

impl FetchResponse for SimpleResponse {
    fn get_http_status(&self) -> u16 {
        200
    }

    fn get_header(&self,name: &str) -> Option<&str> {
        unimplemented!()
    }

    fn read(&mut self) -> Option<Vec<u8>> {
        if self.read_done {
            None
        } else {
            self.read_done = true;      
            Some("Hello world".as_bytes().to_vec())
        }
    }
}

let rt = EsRuntimeBuilder::new()
.fetch_response_provider(|req| {Box::new(SimpleResponse::new(req))})
.build();

let res_prom = rt.eval_sync(EsScript::new("test_fetch.es", "(fetch('something')).then((fetchRes) => {return fetchRes.text();});")).ok().expect("script failed");
let res = res_prom.get_promise_result_sync(Duration::from_secs(1)).ok().expect("promise timed out");
let str_esvf = res.ok().expect("promise did not resolve ok");
assert_eq!(str_esvf.get_str(), "Hello world");

pub fn memory_limit(self, bytes: u64) -> Self[src]

set max memory the runtime may use

pub fn gc_threshold(self, size: u64) -> Self[src]

number of allocations before gc is run

pub fn max_stack_size(self, size: u64) -> Self[src]

set a max stack size

pub fn gc_interval(self, interval: Duration) -> Self[src]

Trait Implementations

impl Default for EsRuntimeBuilder[src]

Auto Trait Implementations

Blanket Implementations

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

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

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

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

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

impl<T> Pointable for T

type Init = T

The type for initializers.

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<V, T> VZip<V> for T where
    V: MultiLane<T>,