Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration guide entry for Object::get_value() and Object::get_opt() #868

Merged
merged 2 commits into from
Mar 4, 2022

Conversation

dherman
Copy link
Collaborator

@dherman dherman commented Mar 4, 2022

This PR adds information to the migration guide for Object::get() use cases that encode optional or union types.

@dherman dherman requested a review from kjvalencik March 4, 2022 18:45
@dherman dherman merged commit b386ec2 into main Mar 4, 2022
@dherman dherman deleted the migration-guide-get-union-types branch March 4, 2022 18:48
@thekingofcity
Copy link

I can't find an issue for the original MIGRATION_GUIDE_0.10.md so I'm commenting here. Thanks for this wonderful guide and this helps me keep a simple js_log function up to date.


pub fn js_log<'a, C: Context<'a>>(cx: &mut C, s: String) -> NeonResult<()> {
    let global = cx.global();
    let console = global.get(cx, "console")?.downcast_or_throw::<JsObject, _>(cx)?;
    let log = console.get(cx, "log")?.downcast_or_throw::<JsFunction, _>(cx)?;
    let null = cx.null();

    let args: Vec<Handle<JsString>> = vec![cx.string(s)];
    log.call(cx, null, args)?;

    Ok(())
}

version: 0.9.0


pub fn js_log<'a, C: Context<'a>>(cx: &mut C, s: String) -> NeonResult<()> {
    let global = cx.global();
    let console = global.get(cx, "console")?.downcast_or_throw::<JsObject, _>(cx)?;
    let log = console.get(cx, "log")?.downcast_or_throw::<JsFunction, _>(cx)?;

    log.call_with(cx).arg(cx.string(s)).apply::<JsUndefined, C>(cx)?;

    Ok(())
}

version 0.10.0-alpha2


pub fn js_log<'a, C: Context<'a>>(cx: &mut C, s: String) -> NeonResult<()> {
    let global = cx.global();
    let console = global.get::<JsObject, _, _>(cx, "console")?;
    let log = console.get::<JsFunction, _, _>(cx, "log")?;

    log.call_with(cx).arg(cx.string(s)).apply::<JsUndefined, C>(cx)?;

    Ok(())
}

version 0.10.0


BTW, console.log should be a very important feature especially during debugging, but I can't find an example about this in doc or code. Maybe it would be great to have it somewhere in the repo?

@kjvalencik
Copy link
Member

Thanks for the feedback @thekingofcity! FYI, here's the PR for the migration guide.

console.log is an excellent suggestion! It would be nice to have a simple example like that. There's a PR for a more complex example: (connecting Rust's log logging facade to the debug node module): neon-bindings/examples#84.

You can simplify the js_log example a little bit by using CallOptions::exec instead of apply. It discards the return value which avoids the need to provide the type.;

 pub fn js_log<'a, C: Context<'a>>(cx: &mut C, s: String) -> NeonResult<()> {
     let global = cx.global();
     let console = global.get::<JsObject, _, _>(cx, "console")?;
     let log = console.get::<JsFunction, _, _>(cx, "log")?;

-    log.call_with(cx).arg(cx.string(s)).apply::<JsUndefined, C>(cx)?;
+    log.call_with(cx).arg(cx.string(s)).exec(cx)?;

     Ok(())
 }

It also looks like this could benefit from a recent proposal for Object::call_method. #873

@thekingofcity
Copy link

thekingofcity commented Mar 14, 2022

Would be great to see that logging example merged! Of course an out-of-box crate or snippets will be even more user-friendly. I also changed apply to exec, thanks for the suggestion. 😁

PS I find a migration guide for 0.2 in the wiki, but the 0.10 guide is in the repo. Should we place them in one place?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants