Struct quickjs_runtime::reflection::Proxy [−][src]
The Proxy struct can be used to create a class in JavaScript who’s methods can be implemented in rust
Example
use quickjs_runtime::esruntimebuilder::EsRuntimeBuilder; use quickjs_runtime::reflection::Proxy; use quickjs_runtime::quickjscontext::QuickJsContext; use quickjs_runtime::valueref::JSValueRef; use std::cell::RefCell; use std::collections::HashMap; use quickjs_runtime::quickjs_utils::primitives; use quickjs_runtime::esscript::EsScript; use quickjs_runtime::esvalue::EsValueFacade; use quickjs_runtime::eserror::EsError; struct MyFunkyStruct{ name: String } impl Drop for MyFunkyStruct {fn drop(&mut self) { println!("Funky drop: {}", self.name.as_str()); } } thread_local! { static INSTANCES: RefCell<HashMap<usize, MyFunkyStruct>> = RefCell::new(HashMap::new()); } //create a new EsRuntime let rt = EsRuntimeBuilder::new().build(); // install our proxy class as com.hirofa.FunkyClass rt.add_to_event_queue_sync(|q_js_rt| { let q_ctx = q_js_rt.get_main_context(); Proxy::new() .namespace(vec!["com", "hirofa"]) .name("FunkyClass") // the constructor is called when a script does new com.hirofa.FunkyClass, teh reflection utils // generate an instance_id which may be used to identify the instance .constructor(|q_ctx: &QuickJsContext, instance_id: usize, args: Vec<JSValueRef>| { // we'll asume our script allways constrcuts the Proxy with a single name argument let name = primitives::to_string_q(q_ctx, &args[0]).ok().expect("bad constructor! bad!"); // create a new instance of our struct and store it in a map let instance = MyFunkyStruct{name}; // store our struct in a thread_local map INSTANCES.with(move |rc| { let map = &mut *rc.borrow_mut(); map.insert(instance_id, instance); }); // return Ok, or Err if the constructor failed (e.g. wrong args were passed) Ok(()) }) // next we create a simple getName method, this will return a String .method("getName", |q_ctx, instance_id, args| { INSTANCES.with(move |rc| { let map = & *rc.borrow(); let instance = map.get(instance_id).unwrap(); primitives::from_string_q(q_ctx, instance.name.as_str()) }) }) // and lastly (but very important) implement a finalizer so our rust struct may be dropped .finalizer(|q_ctx, instance_id| { INSTANCES.with(move |rc| { let map = &mut *rc.borrow_mut(); map.remove(&instance_id); }); }) // install the Proxy in the context .install(q_ctx, true); }); match rt.eval_sync(EsScript::new("test_proxy.es", "{let inst = new com.hirofa.FunkyClass('FooBar'); let name = inst.getName(); inst = null; name;}" )) { Ok(name_esvf) => { // assert correct getName result assert_eq!(name_esvf.get_str(), "FooBar"); let i_ct = INSTANCES.with(|rc| rc.borrow().len()); // assert instance was finalized assert_eq!(i_ct, 0); }, Err(e) => { panic!("script failed: {}", e); } } rt.gc_sync();
Implementations
impl Proxy
[src]
pub fn new() -> Self
[src]
pub fn name(self, name: &str) -> Self
[src]
set the name of the proxy class this will indicate how to construct the class from script
pub fn namespace(self, namespace: Vec<&str>) -> Self
[src]
set the namespace of the proxy class
Example
use quickjs_runtime::reflection::Proxy; Proxy::new().namespace(vec!["com", "hirofa"]).name("SomeClass");
means from script you can access the class by
let instance = new com.hirofa.SomeClass();
pub fn get_class_name(&self) -> String
[src]
get the canonical classname of a Proxy
example
use quickjs_runtime::reflection::Proxy; Proxy::new().namespace(vec!["com", "hirofa"]).name("SomeClass");
will result in a class_name of “com.hirofa.SomeClass”
pub fn constructor<C>(self, constructor: C) -> Self where
C: Fn(&QuickJsContext, usize, Vec<JSValueRef>) -> Result<(), EsError> + 'static,
[src]
C: Fn(&QuickJsContext, usize, Vec<JSValueRef>) -> Result<(), EsError> + 'static,
add a constructor for the Proxy class this will enable a script to create a new instance of a Proxy class if omitted the Proxy class will not be constructable from script
pub fn finalizer<C>(self, finalizer: C) -> Self where
C: Fn(&QuickJsContext, usize) + 'static,
[src]
C: Fn(&QuickJsContext, usize) + 'static,
add a finalizer for the Proxy class this will be called when an instance of the Proxy class is dropped or garbage collected
pub fn method<M>(self, name: &str, method: M) -> Self where
M: Fn(&QuickJsContext, &usize, Vec<JSValueRef>) -> Result<JSValueRef, EsError> + 'static,
[src]
M: Fn(&QuickJsContext, &usize, Vec<JSValueRef>) -> Result<JSValueRef, EsError> + 'static,
add a method to the Proxy class, this method will be available as a member of instances of the Proxy class
pub fn native_method(self, name: &str, method: ProxyNativeMethod) -> Self
[src]
add a method to the Proxy class, this method will be available as a member of instances of the Proxy class
pub fn static_method<M>(self, name: &str, method: M) -> Self where
M: Fn(&QuickJsContext, Vec<JSValueRef>) -> Result<JSValueRef, EsError> + 'static,
[src]
M: Fn(&QuickJsContext, Vec<JSValueRef>) -> Result<JSValueRef, EsError> + 'static,
add a static method to the Proxy class, this method will be available as a member of the Proxy class itself
pub fn static_native_method(
self,
name: &str,
method: ProxyStaticNativeMethod
) -> Self
[src]
self,
name: &str,
method: ProxyStaticNativeMethod
) -> Self
add a static method to the Proxy class, this method will be available as a member of the Proxy class itself
pub fn static_getter_setter<G, S>(
self,
name: &str,
getter: G,
setter: S
) -> Self where
G: Fn(&QuickJsContext) -> Result<JSValueRef, EsError> + 'static,
S: Fn(&QuickJsContext, JSValueRef) -> Result<(), EsError> + 'static,
[src]
self,
name: &str,
getter: G,
setter: S
) -> Self where
G: Fn(&QuickJsContext) -> Result<JSValueRef, EsError> + 'static,
S: Fn(&QuickJsContext, JSValueRef) -> Result<(), EsError> + 'static,
add a static getter and setter to the Proxy class
pub fn getter_setter<G, S>(self, name: &str, getter: G, setter: S) -> Self where
G: Fn(&QuickJsContext, &usize) -> Result<JSValueRef, EsError> + 'static,
S: Fn(&QuickJsContext, &usize, JSValueRef) -> Result<(), EsError> + 'static,
[src]
G: Fn(&QuickJsContext, &usize) -> Result<JSValueRef, EsError> + 'static,
S: Fn(&QuickJsContext, &usize, JSValueRef) -> Result<(), EsError> + 'static,
add a getter and setter to the Proxy class, these will be available as a member of an instance of this Proxy class
pub fn event_target(self) -> Self
[src]
indicate the Proxy class should implement the EventTarget interface, this will result in the addEventListener, removeEventListener and dispatchEvent methods to be available on instances of the Proxy class
pub fn static_event_target(self)
[src]
indicate the Proxy class should implement the EventTarget interface, this will result in the addEventListener, removeEventListener and dispatchEvent methods to be available
pub fn install(
self,
q_ctx: &QuickJsContext,
add_variable_to_global: bool
) -> Result<JSValueRef, EsError>
[src]
self,
q_ctx: &QuickJsContext,
add_variable_to_global: bool
) -> Result<JSValueRef, EsError>
install the Proxy class in a QuickJsContext, this is always needed as a final step to actually make the Proxy class work
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Proxy
impl !Send for Proxy
impl !Sync for Proxy
impl Unpin for Proxy
impl !UnwindSafe for Proxy
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Pointable for T
pub const ALIGN: usize
type Init = T
The type for initializers.
pub unsafe fn init(init: <T as Pointable>::Init) -> usize
pub unsafe fn deref<'a>(ptr: usize) -> &'a T
pub unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T
pub unsafe fn drop(ptr: usize)
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,