Skip to content

Commit

Permalink
Switch to using SafeArray
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryAstafyev committed Sep 9, 2024
1 parent 097f6c4 commit 5cc8772
Show file tree
Hide file tree
Showing 12 changed files with 460 additions and 598 deletions.
984 changes: 428 additions & 556 deletions application/apps/rustcore/rs-bindings/Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions application/apps/rustcore/rs-bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ crate-type = ["cdylib"]

[build-dependencies]
# Added "default-features = false" becuase: https://github.com/rust-lang/rust/issues/98903
node-bindgen = { version="6.0", default-features = false, features = ["build"] }
node-bindgen = { version="6.1", default-features = false, features = ["build"] }
[dependencies]
anyhow = "1.0"
crossbeam-channel = "0.5"
Expand All @@ -20,7 +20,7 @@ indexer_base = { path = "../../indexer/indexer_base" }
log = "0.4"
log4rs = "1.2"
merging = { path = "../../indexer/merging" }
node-bindgen = { version="6.0", features = ["serde-json"] }
node-bindgen = { version="6.1", features = ["serde-json"] }
processor = { path = "../../indexer/processor" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use node_bindgen::{
core::{val::JsEnv, NjError, TryIntoJs},
core::{safebuffer::SafeArrayBuffer, val::JsEnv, NjError, TryIntoJs},
sys::napi_value,
};
use proto::*;
Expand Down Expand Up @@ -37,7 +37,6 @@ impl From<AttachmentInfoList> for Vec<u8> {

impl TryIntoJs for AttachmentInfoList {
fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value, NjError> {
let bytes: Vec<u8> = self.into();
bytes.try_to_js(js_env)
SafeArrayBuffer::new(self.into()).try_to_js(js_env)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use error::{
grab_error, search_error,
};
use node_bindgen::{
core::{val::JsEnv, NjError, TryIntoJs},
core::{safebuffer::SafeArrayBuffer, val::JsEnv, NjError, TryIntoJs},
sys::napi_value,
};
use processor::{grabber::GrabError, search::error::SearchError};
Expand All @@ -29,8 +29,7 @@ impl From<E> for ComputationErrorWrapper {

impl TryIntoJs for ComputationErrorWrapper {
fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value, NjError> {
let bytes: Vec<u8> = self.into();
bytes.try_to_js(js_env)
SafeArrayBuffer::new(self.into()).try_to_js(js_env)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::errors::get_native_err;
use event::callback_event::search_values_updated;
use event::callback_event::{self, Event};
use node_bindgen::{
core::{val::JsEnv, NjError, TryIntoJs},
core::{safebuffer::SafeArrayBuffer, val::JsEnv, NjError, TryIntoJs},
sys::napi_value,
};
use proto::*;
Expand All @@ -21,8 +21,7 @@ impl CallbackEventWrapped {

impl TryIntoJs for CallbackEventWrapped {
fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value, NjError> {
let bytes: Vec<u8> = self.into();
bytes.try_to_js(js_env)
SafeArrayBuffer::new(self.into()).try_to_js(js_env)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use node_bindgen::{
core::{val::JsEnv, NjError, TryIntoJs},
core::{safebuffer::SafeArrayBuffer, val::JsEnv, NjError, TryIntoJs},
sys::napi_value,
};
use proto::*;
Expand Down Expand Up @@ -34,7 +34,6 @@ impl From<GrabbedElements> for Vec<u8> {

impl TryIntoJs for GrabbedElements {
fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value, NjError> {
let bytes: Vec<u8> = self.into();
bytes.try_to_js(js_env)
SafeArrayBuffer::new(self.into()).try_to_js(js_env)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use node_bindgen::{
core::{val::JsEnv, NjError, TryIntoJs},
core::{safebuffer::SafeArrayBuffer, val::JsEnv, NjError, TryIntoJs},
sys::napi_value,
};
use progress::{self, lifecycle_transition};
Expand Down Expand Up @@ -52,7 +52,6 @@ impl From<LifecycleTransitionWrapper> for Vec<u8> {

impl TryIntoJs for LifecycleTransitionWrapper {
fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value, NjError> {
let bytes: Vec<u8> = self.into();
bytes.try_to_js(js_env)
SafeArrayBuffer::new(self.into()).try_to_js(js_env)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use node_bindgen::{
core::{val::JsEnv, NjError, TryIntoJs},
core::{safebuffer::SafeArrayBuffer, val::JsEnv, NjError, TryIntoJs},
sys::napi_value,
};
use proto::*;
Expand Down Expand Up @@ -33,7 +33,6 @@ impl From<RangeInclusiveList> for Vec<u8> {

impl TryIntoJs for RangeInclusiveList {
fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value, NjError> {
let bytes: Vec<u8> = self.into();
bytes.try_to_js(js_env)
SafeArrayBuffer::new(self.into()).try_to_js(js_env)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use sources::sde::{SdeRequest, SdeResponse};
use std::{convert::TryInto, ops::Deref};
pub struct SdeResponseWrapped(pub SdeResponse);
use node_bindgen::{
core::{val::JsEnv, NjError, TryIntoJs},
core::{safebuffer::SafeArrayBuffer, val::JsEnv, NjError, TryIntoJs},
sys::napi_value,
};

Expand Down Expand Up @@ -40,7 +40,6 @@ impl TryInto<SdeRequest> for JsIncomeBuffer {

impl TryIntoJs for SdeResponseWrapped {
fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value, NjError> {
let bytes: Vec<u8> = self.into();
bytes.try_to_js(js_env)
SafeArrayBuffer::new(self.into()).try_to_js(js_env)
}
}
20 changes: 7 additions & 13 deletions application/apps/rustcore/rs-bindings/src/js/jobs/converting.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use command_outcome::Cancelled;
use node_bindgen::{
core::{val::JsEnv, NjError, TryIntoJs},
core::{safebuffer::SafeArrayBuffer, val::JsEnv, NjError, TryIntoJs},
sys::napi_value,
};
use output::{Empty, StringVec};
Expand Down Expand Up @@ -138,42 +138,36 @@ impl From<CommandOutcomeWrapper<i64>> for Vec<u8> {

impl TryIntoJs for CommandOutcomeWrapper<String> {
fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value, NjError> {
let bytes: Vec<u8> = self.into();
bytes.try_to_js(js_env)
SafeArrayBuffer::new(self.into()).try_to_js(js_env)
}
}

impl TryIntoJs for CommandOutcomeWrapper<i64> {
fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value, NjError> {
let bytes: Vec<u8> = self.into();
bytes.try_to_js(js_env)
SafeArrayBuffer::new(self.into()).try_to_js(js_env)
}
}

impl TryIntoJs for CommandOutcomeWrapper<()> {
fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value, NjError> {
let bytes: Vec<u8> = self.into();
bytes.try_to_js(js_env)
SafeArrayBuffer::new(self.into()).try_to_js(js_env)
}
}

impl TryIntoJs for CommandOutcomeWrapper<bool> {
fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value, NjError> {
let bytes: Vec<u8> = self.into();
bytes.try_to_js(js_env)
SafeArrayBuffer::new(self.into()).try_to_js(js_env)
}
}

impl TryIntoJs for CommandOutcomeWrapper<Vec<String>> {
fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value, NjError> {
let bytes: Vec<u8> = self.into();
bytes.try_to_js(js_env)
SafeArrayBuffer::new(self.into()).try_to_js(js_env)
}
}

impl TryIntoJs for CommandOutcomeWrapper<Option<String>> {
fn try_to_js(self, js_env: &JsEnv) -> Result<napi_value, NjError> {
let bytes: Vec<u8> = self.into();
bytes.try_to_js(js_env)
SafeArrayBuffer::new(self.into()).try_to_js(js_env)
}
}
9 changes: 6 additions & 3 deletions application/apps/rustcore/rs-bindings/src/js/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use crate::{
logging::targets,
};
use log::{debug, error, info, warn};
use node_bindgen::{core::buffer::JSArrayBuffer, derive::node_bindgen};
use node_bindgen::{
core::{buffer::JSArrayBuffer, safebuffer::SafeArrayBuffer},
derive::node_bindgen,
};
use processor::grabber::LineRange;
use session::{
events::{CallbackEvent, ComputationError, NativeError},
Expand Down Expand Up @@ -660,7 +663,7 @@ impl RustSession {
}

#[node_bindgen]
fn test_grab_els_as_proto(&self) -> Result<Vec<u8>, ComputationErrorWrapper> {
fn test_grab_els_as_proto(&self) -> Result<SafeArrayBuffer, ComputationErrorWrapper> {
let var_name = Vec::new();
let mut elements = var_name;
for i in 0..50 {
Expand All @@ -674,6 +677,6 @@ impl RustSession {
})
}
let msg = proto::GrabbedElementList { elements };
Ok(prost::Message::encode_to_vec(&msg))
Ok(SafeArrayBuffer::new(prost::Message::encode_to_vec(&msg)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('Protocol', function () {
.then((session: Session) => {
// Set provider into debug mode
session.debug(true);
const MESSAGES_COUNT = 100;
const MESSAGES_COUNT = 1000;
{
const meausere: { json: number; proto: number } = { json: 0, proto: 0 };
meausere.json = Date.now();
Expand All @@ -75,7 +75,7 @@ describe('Protocol', function () {
}
meausere.proto = Date.now() - meausere.proto;
console.log(
`Receiving messages count: ${MESSAGES_COUNT}\nJSON: ${
`Receiving messages (no decoding) count: ${MESSAGES_COUNT}\nJSON: ${
meausere.json
}ms (per msg ${(meausere.json / MESSAGES_COUNT).toFixed(2)});\nPROTO: ${
meausere.proto
Expand All @@ -97,7 +97,7 @@ describe('Protocol', function () {
}
meausere.proto = Date.now() - meausere.proto;
console.log(
`Grabbing messages count: ${MESSAGES_COUNT}\nJSON: ${
`Grabbing messages (with decoding) count: ${MESSAGES_COUNT}\nJSON: ${
meausere.json
}ms (per msg ${(meausere.json / MESSAGES_COUNT).toFixed(2)});\nPROTO: ${
meausere.proto
Expand Down

0 comments on commit 5cc8772

Please sign in to comment.