@@ -68,7 +68,7 @@ Writing native node-js requires lots of boilerplate code. Node-bindgen generate
68
68
69
69
Install nj-cli command line, which will be used to generate the native library.
70
70
71
- ```
71
+ ``` sh
72
72
cargo install nj-cli
73
73
```
74
74
@@ -79,26 +79,26 @@ This is a one time step.
79
79
Add two dependencies to your projects' ``` Cargo.toml ``` .
80
80
81
81
Add ``` node-bindgen ``` as a regular dependency (as below):
82
- ```
82
+ ``` toml
83
83
[dependencies ]
84
84
node-bindgen = { version = " 6.0" }
85
85
```
86
86
87
87
Then add ``` node-bindgen ``` 's procedure macro to your build-dependencies as below:
88
- ```
88
+ ``` toml
89
89
[build-dependencies ]
90
90
node-bindgen = { version = " 6.0" , default-features = false , features = [" build" ] }
91
91
```
92
92
93
93
Then update crate type to ``` cdylib ``` to generate node.js compatible native module:
94
- ```
94
+ ``` toml
95
95
[lib ]
96
96
crate-type = [" cdylib" ]
97
97
```
98
98
99
99
Finally, add ``` build.rs ``` at the top of the project with following content:
100
100
101
- ```
101
+ ``` rust,ignore
102
102
fn main() {
103
103
node_bindgen::build::configure();
104
104
}
@@ -110,7 +110,7 @@ fn main() {
110
110
Here is a function that adds two numbers. Note that you don't need to worry about JS conversion.
111
111
112
112
113
- ``` rust
113
+ ``` rust,ignore
114
114
115
115
use node_bindgen::derive::node_bindgen;
116
116
@@ -126,14 +126,14 @@ fn sum(first: i32, second: i32) -> i32 {
126
126
127
127
To build node.js library, using ``` nj-cli ``` to build:
128
128
129
- ```
129
+ ``` sh
130
130
nj-cli build
131
131
```
132
132
133
133
This will generate Node.js module in "./dist" folder.
134
134
135
135
To build a release version:
136
- ```
136
+ ``` sh
137
137
nj-cli build --release
138
138
```
139
139
@@ -169,7 +169,7 @@ undefined
169
169
170
170
## Function name or method can be renamed instead of default mapping
171
171
172
- ``` rust
172
+ ``` rust,ignore
173
173
#[node_bindgen(name="multiply")]
174
174
fn mul(first: i32,second: i32) -> i32 {
175
175
first * second
@@ -181,7 +181,7 @@ Rust function mul is re-mapped as ```multiply```
181
181
## Optional argument
182
182
183
183
Argument can be skipped if it is marked as optional
184
- ``` rust
184
+ ``` rust,ignore
185
185
#[node_bindgen]
186
186
fn sum(first: i32, second: Option<i32>) -> i32 {
187
187
first + second.unwrap_or(0)
@@ -195,7 +195,7 @@ Then sum can be invoked as
195
195
196
196
JS callback are mapped as Rust closure.
197
197
198
- ``` rust
198
+ ``` rust,ignore
199
199
#[node_bindgen]
200
200
fn hello<F: Fn(String)>(first: f64, second: F) {
201
201
@@ -222,7 +222,7 @@ Callback are supported in Async rust as well.
222
222
223
223
Async rust function is mapped to Node.js promise.
224
224
225
- ``` rust
225
+ ``` rust,ignore
226
226
227
227
use std::time::Duration;
228
228
use flv_future_aio::time::sleep;
@@ -252,7 +252,7 @@ addon.hello(5).then((val) => {
252
252
Structs, including generic structs, can have have the to-JS conversion boilerplate autogenerated.
253
253
Just apply the ` node_bindgen ` macro to your struct:
254
254
255
- ``` rust
255
+ ``` rust,ignore
256
256
#[node_bindgen]
257
257
struct MyJson {
258
258
some_name: String,
@@ -285,7 +285,7 @@ Field names will be converted to camelCase.
285
285
286
286
Enums will also have their JS representation autogenerated with the help of ` node_bindgen ` :
287
287
288
- ``` rust
288
+ ``` rust,ignore
289
289
#[node_bindgen]
290
290
enum ErrorType {
291
291
WithMessage(String, usize),
@@ -332,7 +332,7 @@ Generics and references are supported, with the same caveats as for structs.
332
332
333
333
JavaScript class is supported.
334
334
335
- ``` rust
335
+ ``` rust,ignore
336
336
337
337
struct MyClass {
338
338
val: f64,
@@ -380,8 +380,8 @@ environment has a valid C/C++ compiler.
380
380
In the future, this file will be re-written in Rust, removing this dependency.
381
381
382
382
Just make sure that you are compiling the rust module using
383
- ```
384
- $ npx electron-build-env nj-cli build --release
383
+ ``` sh
384
+ npx electron-build-env nj-cli build --release
385
385
```
386
386
387
387
otherwise you will get dreaded ` A dynamic link library (DLL) initialization routine failed ` when importing the rust module in electron
@@ -398,7 +398,7 @@ In addition, because `tslink` generates TypeScript types definitions, any change
398
398
399
399
For example,
400
400
401
- ``` ignore
401
+ ``` rust, ignore
402
402
#[macro_use] extern crate tslink;
403
403
use tslink::tslink;
404
404
use node_bindgen::derive::node_bindgen;
@@ -426,7 +426,7 @@ impl MyScruct {
426
426
427
427
Would be represented (` *.d.ts ` ) as
428
428
429
- ``` ignore
429
+ ``` ts
430
430
export declare class MyStruct {
431
431
constructor (inc : number );
432
432
incMyNumber(a : number ): number ;
@@ -441,11 +441,11 @@ Also, please **note**, `node-bindgen` by default applies snake case naming to me
441
441
442
442
File: ` ./Cargo.toml ` (in a ` root ` of project):
443
443
444
- ``` ignore
444
+ ``` toml
445
445
[project ]
446
- ...
446
+ # ...
447
447
[lib ]
448
- ...
448
+ # ...
449
449
[tslink ]
450
450
node = " ./dist/index.node"
451
451
```
0 commit comments