Skip to content

Is it possible to rust data type to JsObject? #3891

Answered by jedel1043
bubbajoe asked this question in Q&A
Discussion options

You must be logged in to vote

Ahhh, right. To construct an instance of Person you need to call either Class::from_data (if you already have the data you want to initialize) or Class::construct, if you want to construct from JS args. That will set the correct prototype, since you're setting it to Object in the snippet you provided.

fn native_func(_this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
    let from_data = Person::from_data(Person::new(), context)?;
    let new_target = context.get_global_class::<Self>().expect("must have the class registered").constructor();
    let from_args = Person::construct(&new_target, &[js_string!("John").into(), JsValue::new(30)],
            context);
    

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@jedel1043
Comment options

@jedel1043
Comment options

@bubbajoe
Comment options

@jedel1043
Comment options

Answer selected by bubbajoe
@bubbajoe
Comment options

@jedel1043
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants