Skip to content

chore(tesseract): Native utils refactoring #9685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions packages/cubejs-backend-native/src/node_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,13 +577,15 @@ pub fn reset_logger(mut cx: FunctionContext) -> JsResult<JsUndefined> {

fn build_sql_and_params(cx: FunctionContext) -> JsResult<JsValue> {
neon_run_with_guarded_lifetime(cx, |neon_context_holder| {
let options =
NativeObjectHandle::<NeonInnerTypes<FunctionContext<'static>>>::new(NeonObject::new(
let options = NativeObjectHandle::<NeonInnerTypes<FunctionContext<'static>>>::new(
NeonObject::new(
neon_context_holder.clone(),
neon_context_holder
.with_context(|cx| cx.argument::<JsValue>(0))
.unwrap()?,
));
)
.unwrap(),
);

let safe_call_fn = neon_context_holder
.with_context(|cx| {
Expand All @@ -608,7 +610,7 @@ fn build_sql_and_params(cx: FunctionContext) -> JsResult<JsValue> {
let res = base_query.build_sql_and_params();

let result: NeonObject<FunctionContext<'static>> = res.into_object();
let result = result.into_object();
let result = result.get_object().unwrap();
Ok(result)
})
}
Expand Down
16 changes: 8 additions & 8 deletions rust/cubenativeutils/src/wrappers/neon/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,47 +229,47 @@ impl<C: Context<'static> + 'static> NativeContext<NeonInnerTypes<C>> for Context
let obj = NeonObject::new(
self.clone(),
self.with_context(|cx| cx.boolean(v).upcast())?,
);
)?;
obj.into_boolean()
}

fn string(&self, v: String) -> Result<NeonString<C>, CubeError> {
let obj = NeonObject::new(self.clone(), self.with_context(|cx| cx.string(v).upcast())?);
let obj = NeonObject::new(self.clone(), self.with_context(|cx| cx.string(v).upcast())?)?;
obj.into_string()
}

fn number(&self, v: f64) -> Result<NeonNumber<C>, CubeError> {
let obj = NeonObject::new(self.clone(), self.with_context(|cx| cx.number(v).upcast())?);
let obj = NeonObject::new(self.clone(), self.with_context(|cx| cx.number(v).upcast())?)?;
obj.into_number()
}

fn undefined(&self) -> Result<NativeObjectHandle<NeonInnerTypes<C>>, CubeError> {
Ok(NativeObjectHandle::new(NeonObject::new(
self.clone(),
self.with_context(|cx| cx.undefined().upcast())?,
)))
)?))
}

fn null(&self) -> Result<NativeObjectHandle<NeonInnerTypes<C>>, CubeError> {
Ok(NativeObjectHandle::new(NeonObject::new(
self.clone(),
self.with_context(|cx| cx.null().upcast())?,
)))
)?))
}

fn empty_array(&self) -> Result<NeonArray<C>, CubeError> {
let obj = NeonObject::new(
self.clone(),
self.with_context(|cx| cx.empty_array().upcast())?,
);
)?;
obj.into_array()
}

fn empty_struct(&self) -> Result<NeonStruct<C>, CubeError> {
let obj = NeonObject::new(
self.clone(),
self.with_context(|cx| cx.empty_object().upcast())?,
);
)?;
obj.into_struct()
}
fn to_string_fn(&self, result: String) -> Result<NeonFunction<C>, CubeError> {
Expand All @@ -280,7 +280,7 @@ impl<C: Context<'static> + 'static> NativeContext<NeonInnerTypes<C>> for Context
.unwrap()
.upcast()
})?,
);
)?;
obj.into_function()
}
}
Expand Down
44 changes: 23 additions & 21 deletions rust/cubenativeutils/src/wrappers/neon/object/base_types.rs
Original file line number Diff line number Diff line change
@@ -1,82 +1,84 @@
use super::{NeonObject, NeonTypeHandle};
use super::primitive_root_holder::*;
use super::{NeonObject, RootHolder};
use crate::wrappers::neon::inner_types::NeonInnerTypes;
use std::marker::PhantomData;

use crate::wrappers::object::{NativeBoolean, NativeBox, NativeNumber, NativeString, NativeType};
use crate::wrappers::object::{NativeBoolean, NativeNumber, NativeString, NativeType};
use cubesql::CubeError;
use neon::prelude::*;
use std::ops::Deref;

pub struct NeonString<C: Context<'static>> {
object: NeonTypeHandle<C, JsString>,
holder: PrimitiveNeonTypeHolder<C, JsString>,
}

impl<C: Context<'static>> NeonString<C> {
pub fn new(object: NeonTypeHandle<C, JsString>) -> Self {
Self { object }
pub fn new(holder: PrimitiveNeonTypeHolder<C, JsString>) -> Self {
Self { holder }
}
}

impl<C: Context<'static> + 'static> NativeType<NeonInnerTypes<C>> for NeonString<C> {
fn into_object(self) -> NeonObject<C> {
self.object.upcast()
let root_holder = RootHolder::from_typed(self.holder);
NeonObject::form_root(root_holder)
}
}

impl<C: Context<'static> + 'static> NativeString<NeonInnerTypes<C>> for NeonString<C> {
fn value(&self) -> Result<String, CubeError> {
self.object
self.holder
.map_neon_object::<_, _>(|cx, object| Ok(object.value(cx)))?
}
}

pub struct NeonNumber<C: Context<'static>> {
object: NeonTypeHandle<C, JsNumber>,
holder: PrimitiveNeonTypeHolder<C, JsNumber>,
}

impl<C: Context<'static>> NeonNumber<C> {
pub fn new(object: NeonTypeHandle<C, JsNumber>) -> Self {
Self { object }
pub fn new(holder: PrimitiveNeonTypeHolder<C, JsNumber>) -> Self {
Self { holder }
}
}

impl<C: Context<'static> + 'static> NativeType<NeonInnerTypes<C>> for NeonNumber<C> {
fn into_object(self) -> NeonObject<C> {
self.object.upcast()
let root_holder = RootHolder::from_typed(self.holder);
NeonObject::form_root(root_holder)
}
}

impl<C: Context<'static> + 'static> NativeNumber<NeonInnerTypes<C>> for NeonNumber<C> {
fn value(&self) -> Result<f64, CubeError> {
self.object
self.holder
.map_neon_object::<_, _>(|cx, object| Ok(object.value(cx)))?
}
}

pub struct NeonBoolean<C: Context<'static>> {
object: NeonTypeHandle<C, JsBoolean>,
holder: PrimitiveNeonTypeHolder<C, JsBoolean>,
}

impl<C: Context<'static>> NeonBoolean<C> {
pub fn new(object: NeonTypeHandle<C, JsBoolean>) -> Self {
Self { object }
pub fn new(holder: PrimitiveNeonTypeHolder<C, JsBoolean>) -> Self {
Self { holder }
}
}

impl<C: Context<'static> + 'static> NativeType<NeonInnerTypes<C>> for NeonBoolean<C> {
fn into_object(self) -> NeonObject<C> {
self.object.upcast()
let root_holder = RootHolder::from_typed(self.holder);
NeonObject::form_root(root_holder)
}
}

impl<C: Context<'static> + 'static> NativeBoolean<NeonInnerTypes<C>> for NeonBoolean<C> {
fn value(&self) -> Result<bool, CubeError> {
self.object
self.holder
.map_neon_object::<_, _>(|cx, object| Ok(object.value(cx)))?
}
}

pub struct NeonBox<C: Context<'static>, T: 'static> {
/* pub struct NeonBox<C: Context<'static>, T: 'static> {
object: NeonTypeHandle<C, JsBox<T>>,
_marker: PhantomData<T>,
}
Expand All @@ -100,4 +102,4 @@ impl<C: Context<'static> + 'static, T: 'static> NativeBox<NeonInnerTypes<C>, T>
fn deref_value(&self) -> &T {
self.object.get_object_ref().deref()
}
}
} */
191 changes: 9 additions & 182 deletions rust/cubenativeutils/src/wrappers/neon/object/mod.rs
Original file line number Diff line number Diff line change
@@ -1,186 +1,13 @@
pub mod base_types;
pub mod neon_array;
pub mod neon_function;
pub mod neon_object;
pub mod neon_struct;

use self::{
base_types::{NeonBoolean, NeonNumber, NeonString},
neon_array::NeonArray,
neon_function::NeonFunction,
neon_struct::NeonStruct,
};
use super::inner_types::NeonInnerTypes;
use crate::wrappers::{
neon::context::{ContextHolder, SafeCallFn},
object::NativeObject,
};
use cubesql::CubeError;
use neon::prelude::*;

pub struct NeonTypeHandle<C: Context<'static>, V: Value + 'static> {
context: ContextHolder<C>,
object: Handle<'static, V>,
}

impl<C: Context<'static> + 'static, V: Value + 'static> NeonTypeHandle<C, V> {
pub fn new(context: ContextHolder<C>, object: Handle<'static, V>) -> Self {
Self { context, object }
}

fn get_context(&self) -> ContextHolder<C> {
self.context.clone()
}

pub fn get_object(&self) -> Handle<'static, V> {
self.object
}

pub fn get_object_ref(&self) -> &Handle<'static, V> {
&self.object
}

pub fn into_object(self) -> Handle<'static, V> {
self.object
}

pub fn upcast(&self) -> NeonObject<C> {
NeonObject::new(self.context.clone(), self.object.upcast())
}

pub fn map_neon_object<T, F>(&self, f: F) -> Result<T, CubeError>
where
F: FnOnce(&mut C, &Handle<'static, V>) -> T,
{
self.context.with_context(|cx| f(cx, &self.object))
}

pub fn map_downcast_neon_object<JT: Value, T, F>(&self, f: F) -> Result<T, CubeError>
where
F: FnOnce(&mut C, &Handle<'static, JT>) -> Result<T, CubeError>,
{
self.context.with_context(|cx| {
let obj = self
.object
.downcast::<JT, _>(cx)
.map_err(|_| CubeError::internal("Downcast error".to_string()))?;
f(cx, &obj)
})?
}

pub fn map_neon_object_with_safe_call_fn<T, F>(&self, f: F) -> Result<T, CubeError>
where
F: FnOnce(&mut C, &Handle<'static, V>, SafeCallFn) -> T,
{
self.context
.with_context_and_safe_fn(|cx, safe_call_fn| f(cx, &self.object, safe_call_fn))
}

pub fn is_a<U: Value>(&self) -> Result<bool, CubeError> {
self.context.with_context(|cx| self.object.is_a::<U, _>(cx))
}
}

impl<C: Context<'static>, V: Value + 'static> Clone for NeonTypeHandle<C, V> {
fn clone(&self) -> Self {
Self {
context: self.context.clone(),
object: self.object,
}
}
}

pub struct NeonObject<C: Context<'static>> {
context: ContextHolder<C>,
object: Handle<'static, JsValue>,
}

impl<C: Context<'static> + 'static> NeonObject<C> {
pub fn new(context: ContextHolder<C>, object: Handle<'static, JsValue>) -> Self {
Self { context, object }
}

pub fn get_object(&self) -> Handle<'static, JsValue> {
self.object
}

pub fn get_object_ref(&self) -> &Handle<'static, JsValue> {
&self.object
}

pub fn into_object(self) -> Handle<'static, JsValue> {
self.object
}

pub fn is_a<U: Value>(&self) -> Result<bool, CubeError> {
self.context.with_context(|cx| self.object.is_a::<U, _>(cx))
}

pub fn downcast<U: Value>(&self) -> Result<NeonTypeHandle<C, U>, CubeError> {
let obj = self.context.with_context(|cx| {
self.object
.downcast::<U, _>(cx)
.map_err(|_| CubeError::internal("Downcast error".to_string()))
})??;
Ok(NeonTypeHandle::new(self.context.clone(), obj))
}

pub fn downcast_with_err_msg<U: Value>(
&self,
msg: &str,
) -> Result<NeonTypeHandle<C, U>, CubeError> {
let obj = self.context.with_context(|cx| {
self.object
.downcast::<U, _>(cx)
.map_err(|_| CubeError::internal(msg.to_string()))
})??;
Ok(NeonTypeHandle::new(self.context.clone(), obj))
}
}

impl<C: Context<'static> + 'static> NativeObject<NeonInnerTypes<C>> for NeonObject<C> {
fn get_context(&self) -> ContextHolder<C> {
self.context.clone()
}

fn into_struct(self) -> Result<NeonStruct<C>, CubeError> {
let obj = self.downcast_with_err_msg::<JsObject>("NeonObject is not the JsObject")?;
Ok(NeonStruct::new(obj))
}
fn into_function(self) -> Result<NeonFunction<C>, CubeError> {
let obj = self.downcast_with_err_msg::<JsFunction>("NeonObject is not the JsArray")?;
Ok(NeonFunction::new(obj))
}
fn into_array(self) -> Result<NeonArray<C>, CubeError> {
let obj = self.downcast_with_err_msg::<JsArray>("NeonObject is not the JsArray")?;
Ok(NeonArray::new(obj))
}
fn into_string(self) -> Result<NeonString<C>, CubeError> {
let obj = self.downcast_with_err_msg::<JsString>("NeonObject is not the JsString")?;
Ok(NeonString::new(obj))
}
fn into_number(self) -> Result<NeonNumber<C>, CubeError> {
let obj = self.downcast_with_err_msg::<JsNumber>("NeonObject is not the JsNumber")?;
Ok(NeonNumber::new(obj))
}
fn into_boolean(self) -> Result<NeonBoolean<C>, CubeError> {
let obj = self.downcast_with_err_msg::<JsBoolean>("NeonObject is not the JsBoolean")?;
Ok(NeonBoolean::new(obj))
}

fn is_null(&self) -> Result<bool, CubeError> {
self.is_a::<JsNull>()
}

fn is_undefined(&self) -> Result<bool, CubeError> {
self.is_a::<JsUndefined>()
}
}

impl<C: Context<'static>> Clone for NeonObject<C> {
fn clone(&self) -> Self {
Self {
context: self.context.clone(),
object: self.object,
}
}
}
pub mod object_root_holder;
pub mod primitive_root_holder;
pub mod root_holder;

pub use neon_object::*;
use object_root_holder::*;
use primitive_root_holder::*;
use root_holder::*;
Loading
Loading