From 56fc689e22abee3d5e08bb14de1eb0510155c301 Mon Sep 17 00:00:00 2001 From: jkomyno Date: Mon, 27 Nov 2023 15:59:58 +0100 Subject: [PATCH] chore(driver-adapters): continue unifying napi/wasm functions --- query-engine/driver-adapters/src/lib.rs | 30 ++++++++++++++++++- .../driver-adapters/src/napi/proxy.rs | 19 ++++++------ .../src/wasm/js_object_extern.rs | 3 +- .../driver-adapters/src/wasm/proxy.rs | 7 +++-- 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/query-engine/driver-adapters/src/lib.rs b/query-engine/driver-adapters/src/lib.rs index 625d8f6bcbd5..774391268770 100644 --- a/query-engine/driver-adapters/src/lib.rs +++ b/query-engine/driver-adapters/src/lib.rs @@ -27,14 +27,42 @@ pub use wasm::*; #[cfg(target_arch = "wasm32")] mod arch { + use crate::JsObjectExtern as JsObject; + pub(crate) use js_sys::JsString; use wasm_bindgen::JsValue; + pub(crate) fn get_named_property(object: &JsObject, name: &str) -> JsResult + where + T: From, + { + // object.get("queryRaw".into())? + Ok(object.get(name.into())?.into()) + } + + pub(crate) fn to_rust_str(value: JsString) -> JsResult { + Ok(value.into()) + } + pub(crate) type JsResult = core::result::Result; } #[cfg(not(target_arch = "wasm32"))] mod arch { + use napi::bindgen_prelude::FromNapiValue; + pub(crate) use napi::{JsObject, JsString}; + + pub(crate) fn get_named_property(object: &JsObject, name: &str) -> JsResult + where + T: FromNapiValue, + { + object.get_named_property(name).into() + } + + pub(crate) fn to_rust_str(value: JsString) -> JsResult { + Ok(value.into_utf8()?.as_str()?.to_string()) + } + pub(crate) type JsResult = napi::Result; } -pub(crate) use arch::JsResult; +pub(crate) use arch::*; diff --git a/query-engine/driver-adapters/src/napi/proxy.rs b/query-engine/driver-adapters/src/napi/proxy.rs index fd61f87847be..753fdfb56616 100644 --- a/query-engine/driver-adapters/src/napi/proxy.rs +++ b/query-engine/driver-adapters/src/napi/proxy.rs @@ -1,10 +1,9 @@ pub use crate::types::{ColumnType, JSResultSet, Query, TransactionOptions}; -use crate::JsResult; +use crate::{get_named_property, to_rust_str, JsObject, JsResult, JsString}; use super::async_js_function::AsyncJsFunction; use super::transaction::JsTransaction; use metrics::increment_gauge; -use napi::{JsObject, JsString}; use std::sync::atomic::{AtomicBool, Ordering}; /// Proxy is a struct wrapping a javascript object that exhibits basic primitives for @@ -45,12 +44,12 @@ pub(crate) struct TransactionProxy { impl CommonProxy { pub fn new(object: &JsObject) -> JsResult { - let flavour: JsString = object.get_named_property("flavour")?; + let flavour: JsString = get_named_property(object, "flavour")?; Ok(Self { - query_raw: object.get_named_property("queryRaw")?, - execute_raw: object.get_named_property("executeRaw")?, - flavour: flavour.into_utf8()?.as_str()?.to_owned(), + query_raw: get_named_property(object, "queryRaw")?, + execute_raw: get_named_property(object, "executeRaw")?, + flavour: to_rust_str(flavour)?, }) } @@ -66,7 +65,7 @@ impl CommonProxy { impl DriverProxy { pub fn new(driver_adapter: &JsObject) -> JsResult { Ok(Self { - start_transaction: driver_adapter.get_named_property("startTransaction")?, + start_transaction: get_named_property(driver_adapter, "startTransaction")?, }) } @@ -84,9 +83,9 @@ impl DriverProxy { impl TransactionProxy { pub fn new(js_transaction: &JsObject) -> JsResult { - let commit = js_transaction.get_named_property("commit")?; - let rollback = js_transaction.get_named_property("rollback")?; - let options = js_transaction.get_named_property("options")?; + let commit = get_named_property(js_transaction, "commit")?; + let rollback = get_named_property(js_transaction, "rollback")?; + let options = get_named_property(js_transaction, "options")?; let closed = AtomicBool::new(false); Ok(Self { diff --git a/query-engine/driver-adapters/src/wasm/js_object_extern.rs b/query-engine/driver-adapters/src/wasm/js_object_extern.rs index 8804e706f67f..29abc1d6ef6f 100644 --- a/query-engine/driver-adapters/src/wasm/js_object_extern.rs +++ b/query-engine/driver-adapters/src/wasm/js_object_extern.rs @@ -1,8 +1,9 @@ -use js_sys::JsString; +use js_sys::{JsString, Object as JsObject}; use wasm_bindgen::{prelude::wasm_bindgen, JsValue}; #[wasm_bindgen] extern "C" { + #[wasm_bindgen(js_name = String, extends = JsObject, is_type_of = JsValue::is_object, typescript_type = "object")] pub type JsObjectExtern; #[wasm_bindgen(method, catch, structural, indexing_getter)] diff --git a/query-engine/driver-adapters/src/wasm/proxy.rs b/query-engine/driver-adapters/src/wasm/proxy.rs index b9d98739d698..dc64a0c24cba 100644 --- a/query-engine/driver-adapters/src/wasm/proxy.rs +++ b/query-engine/driver-adapters/src/wasm/proxy.rs @@ -1,7 +1,7 @@ use crate::send_future::SendFuture; pub use crate::types::{ColumnType, JSResultSet, Query, TransactionOptions}; use crate::JsObjectExtern; -use crate::JsResult; +use crate::{get_named_property, to_rust_str, JsResult}; use super::{async_js_function::AsyncJsFunction, transaction::JsTransaction}; use futures::Future; @@ -53,12 +53,13 @@ pub(crate) struct TransactionProxy { impl CommonProxy { pub fn new(object: &JsObjectExtern) -> JsResult { - let flavour: String = JsString::from(object.get("flavour".into())?).into(); + let flavour: JsString = get_named_property(object, "flavour")?; Ok(Self { + // TODO: remove the need for `JsFunction::from` (?) query_raw: JsFunction::from(object.get("queryRaw".into())?).into(), execute_raw: JsFunction::from(object.get("executeRaw".into())?).into(), - flavour, + flavour: to_rust_str(flavour)?, }) }