Skip to content

Commit

Permalink
Merge pull request #868 from neon-bindings/migration-guide-get-union-…
Browse files Browse the repository at this point in the history
…types

Migration guide entry for `Object::get_value()` and `Object::get_opt()`
  • Loading branch information
dherman committed Mar 4, 2022
2 parents f942e99 + f3b7eb5 commit b386ec2
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions MIGRATION_GUIDE_0.10.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,46 @@ obj.get::<V, _, _>(&mut cx, "name")?
let v: Handle<V> = 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<Handle<JsBoolean>> = obj
.get(&mut cx, "optionalField")?
.downcast(&mut cx)
.ok();
```

**After:**

```rust
let field: Option<Handle<JsBoolean>> = obj.get_opt(&mut cx, "optionalField")?;
```

**Before:**

```rust
let length = obj.get(&mut cx, "length")?;
let length: Option<Handle<JsNumber>> = if length.is_a::<JsNull, _>(&mut cx) {
None
} else {
Some(length.downcast_or_throw(&mut cx)?)
};
```

**After:**

```rust
let length = obj.get_value(&mut cx, "length")?;
let length: Option<Handle<JsNumber>> = if length.is_a::<JsNull, _>(&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.
Expand Down

0 comments on commit b386ec2

Please sign in to comment.