Skip to content
This repository has been archived by the owner on Dec 1, 2018. It is now read-only.

Change hello world to Neon 0.2 #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 7 additions & 7 deletions _guides/hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,27 @@ Now let's edit the Rust code to make use of the new dependency. First we have to
extern crate num_cpus;
```

Next we can replace the sample `hello` function that was generated by `neon new` with the function we actually want. Instead of returning a string, our function should return a JavaScript number. So we'll use the [`JsNumber::new()`](https://api.neon-bindings.com/neon/js/struct.jsnumber#method.new) API. Since `JsNumber` expects a Rust [`f64`](https://doc.rust-lang.org/std/primitive.f64.html) (i.e., a 64-bit floating-point number), and [`num_cpus::get()`](https://docs.rs/num_cpus/1.4.0/num_cpus/fn.get.html) returns a [`usize`](https://doc.rust-lang.org/std/primitive.usize.html) (i.e., a pointer-sized integer), we'll use Rust's `as` operator to cast to convert the integer to floating-point:
Next we can replace the sample `hello` function that was generated by `neon new` with the function we actually want. Instead of returning a string, our function should return a JavaScript number. So we'll use the [`JsNumber::new()`](https://api.neon-bindings.com/neon/types/struct.jsnumber#method.new) API. Since `JsNumber` expects a Rust [`f64`](https://doc.rust-lang.org/std/primitive.f64.html) (i.e., a 64-bit floating-point number), and [`num_cpus::get()`](https://docs.rs/num_cpus/1.4.0/num_cpus/fn.get.html) returns a [`usize`](https://doc.rust-lang.org/std/primitive.usize.html) (i.e., a pointer-sized integer), we'll use Rust's `as` operator to cast to convert the integer to floating-point:

```rust
use neon::js::JsNumber;
use neon::prelude::*;

fn threading_hint(call: Call) -> JsResult<JsNumber> {
Ok(JsNumber::new(call.scope, num_cpus::get() as f64))
fn threading_hint(mut cx: FunctionContext) -> JsResult<JsNumber> {
Ok(JsNumber::new(&mut cx, num_cpus::get() as f64))
}
```

A few more things to note about this code:

* The `call` argument to `threading_hint`: this contains information about the function call, such as the arguments and the value of `this`.
* The [`JsResult`](https://api.neon-bindings.com/neon/vm/type.jsresult) output type: this is a Rust [`Result`](https://doc.rust-lang.org/std/result/enum.Result.html) type that indicates whether the function returned (`Ok`) or threw a JavaScript exception (`Err`). You can learn more in the [Handling Errors](../handling-errors) guide. It also tracks the lifetime of the returned _handle_. You can read more about handles in the [Handles and Memory](../handles-and-memory/) guide.
* The [`JsResult`](https://api.neon-bindings.com/neon/result/type.jsresult) output type: this is a Rust [`Result`](https://doc.rust-lang.org/std/result/enum.Result.html) type that indicates whether the function returned (`Ok`) or threw a JavaScript exception (`Err`). You can learn more in the [Handling Errors](../handling-errors) guide. It also tracks the lifetime of the returned _handle_. You can read more about handles in the [Handles and Memory](../handles-and-memory/) guide.
* The `call.scope` argument to `JsNumber::new()`: this tells the JavaScript garbage collector that we need to keep the value we allocate alive long enough to return it to the caller of `threading_hint`. You can learn more about memory management in the [Handles and Memory](../handles-and-memory/) guide.

Finally, we'll modify the code that `neon new` created for us to set up the module exports with this function instead of the initial "hello world" function it created for us:

```rust
register_module!(m, {
m.export("threading_hint", threading_hint)
register_module!(mut cx, {
cx.export_function("threading_hint", threading_hint)
});
```

Expand Down