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

Updates for 1.0 #257

Open
wants to merge 1 commit 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
6 changes: 3 additions & 3 deletions docs/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let s = cx.string("hello!");

a.set(&mut cx, 0, s)?;

let v = a.get(&mut cx, 1)?;
let v: Handle<JsValue> = a.get(&mut cx, 1)?;
```

This is equivalent to the JavaScript code:
Expand Down Expand Up @@ -71,8 +71,8 @@ array[len] = value;
An iterable Rust data structure such as `Vec` can be converted to a JavaScript array by looping over the elements. The [`JsArray::new()`](https://docs.rs/neon/latest/neon/types/struct.JsArray.html#method.new) method can be used to preallocate enough capacity for the number of elements.

```rust
fn vec_to_array<'a, C: Context<'a>>(vec: &Vec<String>, cx: &mut C) -> JsResult<'a, JsArray> {
let a = JsArray::new(cx, vec.len() as u32);
fn vec_to_array<'cx, C: Context<'cx>>(vec: &Vec<String>, cx: &mut C) -> JsResult<'cx, JsArray> {
let a = JsArray::new(cx, vec.len());

for (i, s) in vec.iter().enumerate() {
let v = cx.string(s);
Expand Down
6 changes: 1 addition & 5 deletions docs/example-projects.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Neon is used to power a growing community of applications and libraries—maybe

## Applications

- **[1password:](https://dteare.medium.com/behind-the-scenes-of-1password-for-linux-d59b19143a23)** Secure password manager
- **[1Password:](https://dteare.medium.com/behind-the-scenes-of-1password-for-linux-d59b19143a23)** Secure password manager
- **[Signal:](https://github.com/signalapp/libsignal-client)** Secure, private messaging
- **[Finda:](https://keminglabs.com/finda/)** Type stuff, find things. No mousing.
- **[Parsify Desktop:](https://parsify.app)** Extendable notepad calculator for the 21st Century.
Expand All @@ -25,8 +25,4 @@ Neon is used to power a growing community of applications and libraries—maybe

And <a href="https://github.com/search?q=path%3Apackage.json+cargo-cp-artifact&type=code" target="_blank">many more!</a>

## Rust Libraries

- **[neon-serde:](https://crates.io/crates/neon-serde2)** Easily convert between Rust and JavaScript datatypes

Want to add a project to this list? [Submit a PR](https://github.com/neon-bindings/website)!
3 changes: 2 additions & 1 deletion docs/hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ The first thing to notice about this layout is that **a Neon project is both a N

The Rust source lives in `src/`, but JavaScript that augments Rust can live side-by-side.

Similar to how [Babel](https://babeljs.io/) can be adjusted to target a minimum JavaScript version, Neon can target a Node version by adjusting the `napi` feature in the `Cargo.toml`. By default, `npm init neon` will use the currently installed Node version.
Similar to how [Babel](https://babeljs.io/) can be adjusted to target a minimum JavaScript version, Neon can target a Node version by adjusting the `napi` feature in the `Cargo.toml`. By default, `npm init neon` will use the latest stable Node-API version. Overriding:

```toml
[dependencies.neon]
default-features = false
features = ["napi-6"]
```

Expand Down
8 changes: 4 additions & 4 deletions docs/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ First let's look at the signature of `Book::to_object()`, which we define as a m

```rust
impl Book {
fn to_object<'a>(&self, cx: &mut FunctionContext<'a>) -> JsResult<'a, JsObject> {
fn to_object<'cx>(&self, cx: &mut FunctionContext<'cx>) -> JsResult<'cx, JsObject> {
// ...
}
}
```

This is our first example using a _[lifetime annotation](https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html)_ `'a`. This allows the Rust compiler to ensure that our code never accidentally makes an unsafe reference to JavaScript values managed by the Node runtime. Specifically, this signature tells Neon that the result object returned by this function (which has lifetime `'a`) is managed by the runtime context that was passed in as an argument (which also has that same lifetime `'a`).
This is our first example using a _[lifetime annotation](https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html)_ `'cx`. This allows the Rust compiler to ensure that our code never accidentally makes an unsafe reference to JavaScript values managed by the Node runtime. Specifically, this signature tells Neon that the result object returned by this function (which has lifetime `'cx`) is managed by the runtime context that was passed in as an argument (which also has that same lifetime `'cx`).

If you've never seen lifetimes before or are not yet confident using them, don't worry! For now, you can use this code as a template, and know that the Rust compiler will keep you safe.

Now here is the full implementation:

```rust
fn to_object<'a>(&self, cx: &mut FunctionContext<'a>) -> JsResult<'a, JsObject> {
fn to_object<'cx>(&self, cx: &mut FunctionContext<'cx>) -> JsResult<'cx, JsObject> {
let obj = cx.empty_object();

let title = cx.string(&self.title);
Expand All @@ -99,7 +99,7 @@ One thing worth noticing about this function is that it doesn't use anything spe

```rust
impl Book {
fn to_object<'a>(&self, cx: &mut impl Context<'a>) -> JsResult<'a, JsObject> {
fn to_object<'cx>(&self, cx: &mut impl Context<'cx>) -> JsResult<'cx, JsObject> {
// same as before...
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ Neon requires Node.js and supports **the latest Node version and all LTS release

## Install Rust

Neon requires Rust for development. If you don't already have Rust installed, or don't have a supported version, go to the [Rust web site](https://www.rust-lang.org/install.html) for installation instructions.
Neon requires Rust for development. If you don't already have Rust installed, or don't have a supported version, go to the [Rust web site](https://www.rust-lang.org/) for installation instructions.

Rust may have additional dependencies depending on your target platform (for example, Visual Studio on Windows).
6 changes: 3 additions & 3 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import "../css/bootstrap.css";
const jsExample = `
// JavaScript
function hello() {
let result = fibonacci(10000);
let result = fibonacci(75);
console.log(result);
return result;
}
Expand All @@ -24,8 +24,8 @@ function hello() {
const neonExample = `
// Neon
fn hello(mut cx: FunctionContext) -> JsResult<JsNumber> {
let result = fibonacci(10000);
println!("{}", result);
let result = fibonacci(75);
println!("{result}");
Ok(cx.number(result))
}`.trim();

Expand Down