From d6196dcc8087831d0874c29b526c6a217cc2ecae Mon Sep 17 00:00:00 2001 From: David Herman Date: Fri, 4 Mar 2022 10:43:39 -0800 Subject: [PATCH 1/2] Migration guide entry for `Object::get_value()` and `Object::get_opt()`. --- MIGRATION_GUIDE_0.10.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/MIGRATION_GUIDE_0.10.md b/MIGRATION_GUIDE_0.10.md index b10aeddb2..aed4aabc5 100644 --- a/MIGRATION_GUIDE_0.10.md +++ b/MIGRATION_GUIDE_0.10.md @@ -28,6 +28,46 @@ obj.get::(&mut cx, "name")? let v: Handle = obj.get(&mut cx, "name")? ``` +Since `Object::get()` throws an exception when types don't match, use the new `Object::get_value()` or `Object::get_opt()` methods for cases that accept a wider range of allowable types. + +**Before:** + +```rust +let field: Option> = obj + .get(&mut cx, "optionalField")? + .downcast(&mut cx) + .ok(); +``` + +**After:** + +```rust +let field: Option> = obj.get_opt(&mut cx, "optionalField")?; +``` + +**Before:** + +```rust +let length = obj.get(&mut cx, "length")?; +let length: Option> = if length.is_a::(&mut cx) { + None +} else { + Some(length.downcast_or_throw(&mut cx)?); +}; +``` + +**After:** + +```rust +let length = obj.get_value(&mut cx, "length")?; +let length: Option> = if length.is_a::(&mut cx) { + None +} else { + Some(length.downcast_or_throw(&mut cx)?); +}; +``` + + ## Layered APIs for function calls The API for calling (or constructing, i.e. the Neon equivalent of the JavaScript `new` operator) a JS function has been split into two layered alternatives. The existing `.call()` and `.construct()` functions are now a lower-level primitive, which no longer offers automatic downcasting of arguments or result. But Neon 0.10 now offers a more convenient API for calling functions with an options object and method chaining, with the introduction of the `.call_with()` and `.construct_with()` methods. From f3b7eb504082c1d26b9eb12b6be2e5c77d68120b Mon Sep 17 00:00:00 2001 From: David Herman Date: Fri, 4 Mar 2022 10:47:46 -0800 Subject: [PATCH 2/2] Delete stray semicolons --- MIGRATION_GUIDE_0.10.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MIGRATION_GUIDE_0.10.md b/MIGRATION_GUIDE_0.10.md index aed4aabc5..be4b2985a 100644 --- a/MIGRATION_GUIDE_0.10.md +++ b/MIGRATION_GUIDE_0.10.md @@ -52,7 +52,7 @@ let length = obj.get(&mut cx, "length")?; let length: Option> = if length.is_a::(&mut cx) { None } else { - Some(length.downcast_or_throw(&mut cx)?); + Some(length.downcast_or_throw(&mut cx)?) }; ``` @@ -63,7 +63,7 @@ let length = obj.get_value(&mut cx, "length")?; let length: Option> = if length.is_a::(&mut cx) { None } else { - Some(length.downcast_or_throw(&mut cx)?); + Some(length.downcast_or_throw(&mut cx)?) }; ```