Skip to content

Commit

Permalink
separate parser ast and code generation: issue #9 (#21)
Browse files Browse the repository at this point in the history
add unit tests for syntax checking
remove special handling for Jscallback in the code generator
use unified trait to handle cb argument extraction
update version
add support for optional argument
  • Loading branch information
sehz authored May 10, 2020
1 parent 7777bc2 commit 839ed18
Show file tree
Hide file tree
Showing 96 changed files with 3,960 additions and 1,973 deletions.
30 changes: 15 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
[package]
name = "node-bindgen"
version = "1.3.0"
version = "2.0.0"
authors = ["Fluvio Contributors <team@fluvio.io>"]
edition = "2018"
description = "easy way to write nodejs module using rust"
repository = "https://github.com/infinyon/node-bindgen"
readme = "README.md"
license = "Apache-2.0"

[lib]
test = false

#[patch.crates-io]
#flv-future-aio = { path = "../flv-future/src/future-aio"}

Expand All @@ -21,6 +18,6 @@ build = ["nj-build"]

[dependencies]
nj-sys = { path = "nj-sys", version = "1.0.0", optional = true }
nj-core = { path = "nj-core", version = "1.2.1", optional = true }
nj-core = { path = "nj-core", version = "2.0.0", optional = true }
nj-build = { path = "nj-build", version = "0.2.0", optional = true }
nj-derive = { path = "nj-derive", version = "1.0.0", optional = true}
nj-derive = { path = "nj-derive", version = "2.0.0", optional = true}
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@ build-windows:
cargo build --target=x86_64-pc-windows-gnu


test:
make -C examples test
test: test-derive
make -C examples test


test-derive:
cd nj-derive; RUST_LOG=debug cargo test derive_ui -- --nocapture

test-try:
cd nj-derive; RUST_LOG=debug cargo test derive_try -- --nocapture
36 changes: 32 additions & 4 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
[workspace]
members = [
"class",
"function",
"cb",
"promise",
"async-cb",
"stream"
"json",
"js-env",
"class-simple",
"class-wrapper",
"class-async",
"stream",
]

#[patch.crates-io]
Expand Down
24 changes: 19 additions & 5 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
clean:
cargo clean
make -C function clean
make -C async-cb clean
make -C cb clean
make -C promise clean
make -C class clean
make -C stream clean
make -C json clean
make -C class-simple clean
make -C class-wrapper clean
make -C class-async clean

test: test-function test-cb test-class test-promise test-stream

test: test-function test-cb test-async-cb test-promise test-json test-class-simple test-class-wrapper test-class-async test-stream

test-function:
make -C function test
Expand All @@ -20,8 +25,17 @@ test-cb:
test-promise:
make -C promise test

test-class:
make -C class test
test-json:
make -C json test

test-stream:
make -C stream test
make -C stream test

test-class-simple:
make -C class-simple test

test-class-wrapper:
make -C class-wrapper test

test-class-async:
make -C class-async test
4 changes: 0 additions & 4 deletions examples/async-cb/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@

DYLIB = ../target/debug/libnj_example_async_cb.dylib

all: build


build:
nj-cli build

Expand Down
2 changes: 1 addition & 1 deletion examples/async-cb/README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
simple hello world callback
callback inside async fn
3 changes: 1 addition & 2 deletions examples/async-cb/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
fn main() {

node_bindgen::build::configure();
}
}
15 changes: 10 additions & 5 deletions examples/async-cb/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@

use std::time::Duration;

use flv_future_aio::timer::sleep;
use node_bindgen::derive::node_bindgen;


#[node_bindgen]
async fn basic<F: Fn(f64,f64)>( seconds: i32, cb: F) {

sleep(Duration::from_secs(1)).await;
cb(seconds as f64,(seconds*2) as f64);

}


#[node_bindgen]
async fn hello<F: Fn(f64,String)>( seconds: i32, cb: F) {

// println!("sleeping");
sleep(Duration::from_secs(seconds as u64)).await;
// println!("woke from time");

cb(10.0,"hello world".to_string());

}



8 changes: 8 additions & 0 deletions examples/async-cb/test_cb.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
let addon = require('./dist');
const assert = require('assert');


addon.hello(1,function(val,msg){
assert.equal(val,10);
assert.equal(msg,"hello world");
console.log("callback test succeed");
});


addon.basic(10,function(val,val2){
assert.equal(val,10);
assert.equal(val2,20);
console.log("callback test succeed");
});

3 changes: 0 additions & 3 deletions examples/cb/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@

DYLIB = ../target/debug/libnj_example_cb.dylib

all: build

build:
Expand Down
10 changes: 9 additions & 1 deletion examples/cb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ fn hello<F: Fn(String)>(first: f64, second: F) {
let msg = format!("argument is: {}", first);

second(msg);
}
}


#[node_bindgen]
fn example<F: Fn(i32)>(cb: F,second: i32) {
cb(second*2)
}


8 changes: 6 additions & 2 deletions examples/cb/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ addon.hello(2,function(msg){
});

assert.throws( () => addon.hello(2),{
message: '2 args expected but 1 is present'
});
message: 'expected argument of type: callback'
});

addon.example(function(val){
assert.equal(val,20);
},10);
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "nj-example-class"
name = "nj-example-class-async"
version = "0.1.0"
authors = ["fluvio.io"]
edition = "2018"
Expand Down
Loading

0 comments on commit 839ed18

Please sign in to comment.