Skip to content

Commit

Permalink
address issue with mutable borrow using get_value_index (#115)
Browse files Browse the repository at this point in the history
* use get_value_at to look up argument
* implement ExtractArgFromJs that uses borrow instead of mut borrow
  • Loading branch information
sehz authored Dec 24, 2020
1 parent 3534a18 commit f70d6a2
Show file tree
Hide file tree
Showing 35 changed files with 226 additions and 227 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "node-bindgen"
version = "4.0.1"
version = "4.1.0"
authors = ["Fluvio Contributors <team@fluvio.io>"]
edition = "2018"
description = "easy way to write nodejs module using rust"
Expand All @@ -16,9 +16,9 @@ build = ["nj-build"]

[dependencies]
nj-sys = { path = "nj-sys", version = "3.0.0", optional = true }
nj-core = { path = "nj-core", version = "4.0.0", optional = true }
nj-core = { path = "nj-core", version = "4.1.0", optional = true }
nj-build = { path = "nj-build", version = "0.2.1", optional = true }
nj-derive = { path = "nj-derive", version = "2.1.1", optional = true}
nj-derive = { path = "nj-derive", version = "3.0.0", optional = true}

[workspace]
members = [
Expand Down
9 changes: 5 additions & 4 deletions examples/Cargo.lock

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

10 changes: 1 addition & 9 deletions examples/array/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@


use node_bindgen::derive::node_bindgen;



/// create array and fill with increase value
#[node_bindgen]
fn make_array(count: i32) -> Vec<i32> {

let mut array = vec![];
for i in 0..count {
array.push(i);
}
array
}


/// sum array of values
#[node_bindgen]
fn sum_array(array: Vec<i32>) -> i32 {

array.iter().sum()
array.iter().sum()
}

1 change: 1 addition & 0 deletions examples/async-cb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2018"
crate-type = ["cdylib"]

[dependencies]
log = "0.4.8"
node-bindgen = { path = "../.."}
fluvio-future = { version = "0.1.0",features=["timer"]}

Expand Down
20 changes: 7 additions & 13 deletions examples/async-cb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@ use std::time::Duration;
use fluvio_future::timer::sleep;
use node_bindgen::derive::node_bindgen;


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

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);

cb(seconds as f64, (seconds * 2) as f64);
}


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

// println!("sleeping");
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());
// println!("woke from time");

}
cb(10.0, "hello world".to_string());
}
8 changes: 4 additions & 4 deletions examples/async-cb/test_cb.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ const assert = require('assert');


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


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

4 changes: 2 additions & 2 deletions examples/bigint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use node_bindgen::core::bigint::BigInt;
#[node_bindgen]
fn multiply_big_int(arg: BigInt) -> BigInt {
println!("bigint arg: {}", arg);
arg*2
arg * 2
}

// Test that we can go across the FFI without screwing up the bits.
Expand All @@ -19,7 +19,7 @@ fn do_nothing(arg: BigInt) -> BigInt {
#[node_bindgen]
fn go_negative(arg: BigInt) -> BigInt {
println!("bigint arg: {}", arg);
-1*arg
-1 * arg
}

// Test out that we can return a u64 which is automatically converted to a bigint.
Expand Down
50 changes: 21 additions & 29 deletions examples/buffer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,76 @@ use node_bindgen::derive::node_bindgen;
use node_bindgen::core::buffer::ArrayBuffer;
use node_bindgen::core::NjError;


#[derive(Serialize)]
struct MyStruct {
a: String,
b: i32
b: i32,
}

/// byte array buffer from json bytes
#[node_bindgen]
fn test(b: i32) -> Result<ArrayBuffer,NjError> {

fn test(b: i32) -> Result<ArrayBuffer, NjError> {
let my_struct = MyStruct {
a: "b".to_string(),
b
b,
};

let json_string = serde_json::to_vec(&my_struct)
.map_err(|err| NjError::Other(format!("serialization error: {}",err.to_string())))?;
.map_err(|err| NjError::Other(format!("serialization error: {}", err.to_string())))?;

Ok(ArrayBuffer::new(json_string))
}


use node_bindgen::core::val::JsEnv;
use node_bindgen::core::TryIntoJs;
use node_bindgen::core::val::JsObject;
use node_bindgen::sys::napi_value;

struct Record {
buffer: ArrayBuffer,
comment: String
comment: String,
}


impl TryIntoJs for Record {

/// serialize into json object
fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value,NjError> {

fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value, NjError> {
// create JSON
let mut json = JsObject::create(js_env)?;

json.set_property("buffer",self.buffer.try_to_js(js_env)?)?;
json.set_property("comment",js_env.create_string_utf8(&self.comment)?)?;
json.set_property("buffer", self.buffer.try_to_js(js_env)?)?;
json.set_property("comment", js_env.create_string_utf8(&self.comment)?)?;

json.try_to_js(js_env)
}
}



/// create byte array and wrap in side another json obj
#[node_bindgen]
fn test2(b: i32) -> Result<Record,NjError> {

fn test2(b: i32) -> Result<Record, NjError> {
let my_struct = MyStruct {
a: "b".to_string(),
b
b,
};

let json_string = serde_json::to_vec(&my_struct)
.map_err(|err| NjError::Other(format!("serialization error: {}",err.to_string())))?;
.map_err(|err| NjError::Other(format!("serialization error: {}", err.to_string())))?;

Ok(Record {
buffer: ArrayBuffer::new(json_string),
comment: "array buffer is cool!".to_owned()
comment: "array buffer is cool!".to_owned(),
})
}





#[node_bindgen]
fn test3(data: &[u8]) -> Result<String,NjError> {

fn test3(data: &[u8]) -> Result<String, NjError> {
let message = String::from_utf8(data.to_vec())?;
Ok(format!("reply {}",message))
Ok(format!("reply {}", message))
}

#[node_bindgen]
fn test4(first: &[u8], second: &[u8]) -> Result<String, NjError> {
let message1 = String::from_utf8(first.to_vec())?;
let message2 = String::from_utf8(second.to_vec())?;

Ok(format!("{} {}", message1, message2))
}
1 change: 1 addition & 0 deletions examples/buffer/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ assert.deepStrictEqual(JSON.parse(Buffer.from(record.buffer)), { a: 'b', b: 10 }


assert.strictEqual(addon.test3(Buffer.from("hello")),"reply hello");
assert.strictEqual(addon.test4(Buffer.from("hello"),Buffer.from("world")),"hello world");
3 changes: 1 addition & 2 deletions examples/cb/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
fn main() {

node_bindgen::build::configure();
}
}
8 changes: 3 additions & 5 deletions examples/cb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ fn hello<F: Fn(String)>(first: f64, second: F) {
second(msg);
}


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

/*
#[node_bindgen]
fn sum<F: Fn(i32) -> String>(cb: F,second: i32) -> String {
fn sum<F: Fn(i32) -> String>(cb: F,second: i32) -> String {
let message = cb(second*2);
format!("my message: {}",message)
}
*/

2 changes: 1 addition & 1 deletion examples/cb/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ addon.hello(2,function(msg){
});

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

addon.example(function(val){
Expand Down
3 changes: 1 addition & 2 deletions examples/class-async/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
fn main() {

node_bindgen::build::configure();
}
}
Loading

0 comments on commit f70d6a2

Please sign in to comment.