From 868b895a5fddab1d2a87a4385eac67fae31c0212 Mon Sep 17 00:00:00 2001 From: "K.J. Valencik" Date: Mon, 12 Feb 2024 11:49:36 -0500 Subject: [PATCH] Updates for 1.0 --- docs/arrays.md | 6 +++--- docs/example-projects.md | 6 +----- docs/hello-world.md | 3 ++- docs/objects.md | 8 ++++---- docs/quick-start.md | 2 +- src/pages/index.js | 6 +++--- 6 files changed, 14 insertions(+), 17 deletions(-) diff --git a/docs/arrays.md b/docs/arrays.md index 9c1ee9a4..ecb2e086 100644 --- a/docs/arrays.md +++ b/docs/arrays.md @@ -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 = a.get(&mut cx, 1)?; ``` This is equivalent to the JavaScript code: @@ -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, 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, 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); diff --git a/docs/example-projects.md b/docs/example-projects.md index 692d51c2..794609d5 100644 --- a/docs/example-projects.md +++ b/docs/example-projects.md @@ -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. @@ -25,8 +25,4 @@ Neon is used to power a growing community of applications and libraries—maybe And many more! -## 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)! diff --git a/docs/hello-world.md b/docs/hello-world.md index 64f6f6c1..b0d12b02 100644 --- a/docs/hello-world.md +++ b/docs/hello-world.md @@ -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"] ``` diff --git a/docs/objects.md b/docs/objects.md index 3f761452..62d429ec 100644 --- a/docs/objects.md +++ b/docs/objects.md @@ -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); @@ -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... } } diff --git a/docs/quick-start.md b/docs/quick-start.md index 69b106ae..8693362b 100644 --- a/docs/quick-start.md +++ b/docs/quick-start.md @@ -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). diff --git a/src/pages/index.js b/src/pages/index.js index ec7675d0..50cfd0c9 100755 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -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; } @@ -24,8 +24,8 @@ function hello() { const neonExample = ` // Neon fn hello(mut cx: FunctionContext) -> JsResult { - let result = fibonacci(10000); - println!("{}", result); + let result = fibonacci(75); + println!("{result}"); Ok(cx.number(result)) }`.trim();