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

Update code examples for 0.10.1 version of neon #241

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions docs/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,24 +127,27 @@ fn create_job(mut cx: FunctionContext) -> JsResult<JsObject> {
You can call a JavaScript function from Rust with [`JsFunction::call()`](https://docs.rs/neon/latest/neon/types/struct.JsFunction.html#method.call). This example extracts the [`parseInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt) function and calls it on a string:

```rust
let func = cx.global()
.get(&mut cx, "parseInt")?
let func = cx.global().get::<JsFunction, _, _>(&mut cx, "parseInt")?;
let s = cx.string("2022");
let result = func.call_with(&cx).arg(s).apply(&mut cx)?;
// `this`, ^^^, `args` are also available.
```

There is a lower level variant of writing `func` variable using `downcast_or_throw` function.
```rust
let func = cx
.global()
.get_value(&mut cx, "parseInt")?
.downcast_or_throw::<JsFunction, _>(&mut cx)?;
let null = cx.null();
let s = cx.string(s);
let result = func.call(&mut cx, null, vec![s])?;
```

Notice that `func.call()` takes a runtime context, a value for the `this` binding, and a sequence of arguments. In this case, `parseInt` ignores its `this` value, so we just pass `null`.

## Calling Constructor Functions

You can call a JavaScript function as a constructor, as if with the [`new`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new) operator, with [`JsFunction::construct()`](https://docs.rs/neon/latest/neon/types/struct.JsFunction.html#method.construct). This example extracts the [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) constructor and invokes it with a URL string:

```rust
let global = cx.global()
.get(&mut cx, "URL")?
.downcast_or_throw::<JsFunction, _>(&mut cx)?;
let ctor = cx.global().get::<JsFunction, _, _>(&mut cx, "URL")?;
let url: Handle<JsString> = cx.string("https://neon-bindings.com");
let result = ctor.construct(&mut cx, vec![url]);
let result = ctor.construct_with(&cx).arg(url).apply(&mut cx)?;
```
10 changes: 5 additions & 5 deletions docs/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ Now here is the full implementation:
fn to_object<'a>(&self, cx: &mut FunctionContext<'a>) -> JsResult<'a, JsObject> {
let obj = cx.empty_object();

let title = cx.string(self.title);
obj.set(&mut cx, "title", title)?;
let title = cx.string(&self.title);
obj.set(cx, "title", title)?;

let author = cx.string(self.author);
obj.set(&mut cx, "author", author)?;
let author = cx.string(&self.author);
obj.set(cx, "author", author)?;

let year = cx.number(self.year);
obj.set(&mut cx, "year", year)?;
obj.set(cx, "year", year)?;

Ok(obj)
}
Expand Down