diff --git a/lib/index.js b/lib/index.js index 5fbc3e082265ba998b1e134fc589ed0088929eaf..54c7cc1ac1b626fb659fd6e788a76d74021a38a0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -213,7 +213,7 @@ const SafeWoTB = { var instance = Object.create(SafeWoTB) instance.setFilePath("") instance.setMaxCert(this.maxCerts) - let instance_content = binding.safe_mem_copy(this.maxCerts, this.nodes, this.sources); + let instance_content = binding.safe_mem_copy(this.nodes, this.issued_count, this.sources); instance.nodes = instance_content.nodes; instance.issued_count = instance_content.issued_count; instance.sources = instance_content.sources; diff --git a/native/src/lib.rs b/native/src/lib.rs index 80b5f7497a5f21349c66a561c88f715cdbbe3cb1..778a0756ae792974105d3b3c6f687d7371dac060 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -27,45 +27,36 @@ static PATH_FINDER: RustyPathFinder = RustyPathFinder {}; fn safe_mem_copy(call: Call) -> JsResult<JsObject> { let scope = call.scope; - let max_links = try!(try!(call.arguments.require(scope, 0)).check::<JsInteger>()).value(); - let mut wot = RustyWebOfTrust::new(max_links as usize); - let arg_nodes = call.arguments.require(scope, 1); - let arg_sources = call.arguments.require(scope, 2); - let _bool = try!(fill_rust_wot(scope, arg_nodes, arg_sources, &mut wot)); - let mut js_nodes_array = JsArray::new(scope, wot.size() as u32); - let mut js_sources_array = JsArray::new(scope, wot.size() as u32); - let mut rs_issued_counts = vec![0; wot.size()]; - for i in 0..wot.size() { + let arg_nodes = try!(try!(try!(call.arguments.require(scope, 0)).check::<JsArray>()).to_vec(scope)); + let arg_issued_count = try!(try!(try!(call.arguments.require(scope, 1)).check::<JsArray>()).to_vec(scope)); + let arg_sources = try!(try!(try!(call.arguments.require(scope, 2)).check::<JsArray>()).to_vec(scope)); + let mut js_nodes_array = JsArray::new(scope, arg_nodes.len() as u32); + let mut js_issued_count_array = JsArray::new(scope, arg_issued_count.len() as u32); + let mut js_sources_array = JsArray::new(scope, arg_sources.len() as u32); + let mut i: u32 = 0; + for js_node in arg_nodes { let _bool = try!(JsArray::set( - *js_nodes_array.deref_mut(), - i as u32, - JsBoolean::new(scope, wot.is_enabled(NodeId(i as usize)).unwrap()), + *js_nodes_array.deref_mut(), + i, + try!(js_node.check::<JsBoolean>()), )); - let sources = wot.get_links_source(NodeId(i as usize)).unwrap(); - let mut js_sources = JsArray::new(scope, sources.len() as u32); - let mut j: u32 = 0; - for source in sources { - rs_issued_counts[source.0] += 1; - let _bool = try!(JsArray::set( - *js_sources.deref_mut(), - j, - JsInteger::new(scope, source.0 as i32), - )); - j += 1; - } + i += 1; + } + let mut i: u32 = 0; + for js_issued_count in arg_issued_count { let _bool = try!(JsArray::set( - *js_sources_array.deref_mut(), - i as u32, - js_sources, + *js_issued_count_array.deref_mut(), + i, + try!(js_issued_count.check::<JsInteger>()), )); + i += 1; } - let mut js_issued_count_array = JsArray::new(scope, wot.size() as u32); let mut i: u32 = 0; - for issued_count in rs_issued_counts { + for js_source in arg_sources { let _bool = try!(JsArray::set( - *js_issued_count_array.deref_mut(), - i, - JsInteger::new(scope, issued_count as i32), + *js_sources_array.deref_mut(), + i, + try!(js_source.check::<JsArray>()), )); i += 1; } @@ -74,6 +65,7 @@ fn safe_mem_copy(call: Call) -> JsResult<JsObject> { let _bool = try!(JsObject::set(*js_wot.deref_mut(), "issued_count", js_issued_count_array,)); let _bool = try!(JsObject::set(*js_wot.deref_mut(), "sources", js_sources_array,)); Ok(js_wot) + } fn unsafe_mem_copy_from_safe(call: Call) -> JsResult<JsInteger> { diff --git a/test.bin b/test.bin index 5c9b061e07e6907b3bdae3bad66857db4eac1547..a81e6cfdfdbbbff6945eaf21a78cf5d47e1c57ac 100644 Binary files a/test.bin and b/test.bin differ diff --git a/tests/tests_safe_perfs.js b/tests/tests_safe_perfs.js new file mode 100644 index 0000000000000000000000000000000000000000..0623f28bf22a3b23c9691ed3a027d7eb6ccb2fb6 --- /dev/null +++ b/tests/tests_safe_perfs.js @@ -0,0 +1,56 @@ +"use strict"; + +const addon = require('./../lib/index'); +var assert = require('assert'); +var fs = require('fs'); +var path = require('path'); +var should = require('should'); + + +testsSafePerfs() + +function testsSafePerfs() { + function newInstance(launchSafeTests) { + return () => { + let wot = addon.newEmptySafeMemoryInstance(100); + launchSafeTests(wot); + } + } + + describe("wotb-rs performances tests", () => { + describe('Basic operations', newInstance((wot) => { + + it('should add 1_000_000 nodes', function() { + for (let i = 0; i < 1000000; i++) { + should.equal(wot.addNode(), i); + } + should.equal(wot.getWoTSize(), 1000000); + }); + + it('should add 500_000 links', function() { + for (let i = 0; i < 500000; i++) { + for (let j = 0; j < 1; j++) { + wot.addLink(i, Math.random*(999999)) + } + } + }); + + it('should make a real mem copy', function() { + let wot2 = wot.memCopy(); + wot2.addNode(); + wot2.addLink(0, 1); + should.equal(wot.getWoTSize(), 1000000); + should.equal(wot.existsLink(0, 1), false); + }); + + it('should make a unsafe mem copy', function() { + var unsafe_wot = wot.unsafeMemCopy(); + }); + + it('should make a unsafe mem copy and copy them', function() { + var unsafe_wot = wot.unsafeMemCopy(); + unsafe_wot.memCopy(); + }); + })); + }); +} \ No newline at end of file