Skip to content

Commit 5b8188e

Browse files
authored
chore: Upgrading Napi -> V3 (#341)
Upgrading Napi to v3 --------- Co-authored-by: Bidek56 <[email protected]>
1 parent b69b33a commit 5b8188e

File tree

9 files changed

+242
-248
lines changed

9 files changed

+242
-248
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ crate-type = ["cdylib", "lib"]
1515
[dependencies]
1616
ahash = "0.8.12"
1717
bincode = "1.3.3"
18-
napi = { version = "2.16.17", default-features = false, features = [
18+
napi = { version = "3.1.4", default-features = false, features = [
1919
"napi8",
2020
"serde-json",
21+
"compat-mode"
2122
] }
22-
napi-derive = { version = "2.16.13", default-features = false }
23+
napi-derive = { version = "3.1.2", default-features = false }
2324
polars-core = { version = "0.49.1", default-features = false }
2425
polars-io = { version = "0.49.1", default-features = false }
2526
polars-lazy = { version = "0.49.1", default-features = false }

src/conversion.rs

Lines changed: 96 additions & 97 deletions
Large diffs are not rendered by default.

src/dataframe.rs

Lines changed: 59 additions & 60 deletions
Large diffs are not rendered by default.

src/datatypes.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::prelude::*;
22
use crate::series::JsSeries;
3+
use napi::bindgen_prelude::TypedArrayType;
4+
35
#[napi(js_name = "DataType")]
46
pub enum JsDataType {
57
Int8,
@@ -79,9 +81,9 @@ impl From<&DataType> for JsDataType {
7981
}
8082
}
8183

82-
impl From<napi::TypedArrayType> for JsDataType {
83-
fn from(dt: napi::TypedArrayType) -> Self {
84-
use napi::TypedArrayType::*;
84+
impl From<TypedArrayType> for JsDataType {
85+
fn from(dt: TypedArrayType) -> Self {
86+
use napi::bindgen_prelude::TypedArrayType::*;
8587
match dt {
8688
Int8 => JsDataType::Int8,
8789
Uint8 => JsDataType::UInt8,

src/file.rs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
1-
use napi::bindgen_prelude::{Buffer, Null};
2-
use napi::threadsafe_function::*;
3-
use napi::{Either, JsFunction, JsObject};
1+
use napi::bindgen_prelude::ToNapiValue;
2+
use napi::bindgen_prelude::{Buffer, BufferSlice, Function, JsObjectValue, Null, Object};
3+
use napi::threadsafe_function::{ThreadsafeFunction, ThreadsafeFunctionCallMode};
4+
use napi::Either;
45
use std::io;
56
use std::io::Write;
7+
use std::io::{Error, ErrorKind};
68

79
pub struct JsFileLike<'a> {
8-
pub inner: JsObject,
10+
pub inner: Object<'a>,
911
pub env: &'a napi::Env,
1012
}
1113

1214
pub struct JsWriteStream<'a> {
13-
pub inner: JsObject,
15+
pub inner: Object<'a>,
1416
pub env: &'a napi::Env,
1517
}
1618

1719
impl<'a> JsFileLike<'a> {
18-
pub fn new(inner: JsObject, env: &'a napi::Env) -> Self {
20+
pub fn new(inner: Object<'a>, env: &'a napi::Env) -> Self {
1921
JsFileLike { inner, env }
2022
}
2123
}
2224

2325
impl<'a> JsWriteStream<'a> {
24-
pub fn new(inner: JsObject, env: &'a napi::Env) -> Self {
26+
pub fn new(inner: Object<'a>, env: &'a napi::Env) -> Self {
2527
JsWriteStream { inner, env }
2628
}
2729
}
2830
pub struct ThreadsafeWriteable {
29-
pub inner: ThreadsafeFunction<Either<Buffer, Null>, ErrorStrategy::CalleeHandled>,
31+
pub inner: ThreadsafeFunction<Either<Buffer, Null>>,
3032
}
3133

3234
impl Write for ThreadsafeWriteable {
3335
fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
34-
let tsfn = self.inner.clone();
36+
let tsfn = &self.inner;
3537
tsfn.call(
3638
Ok(Either::A(buf.into())),
3739
ThreadsafeFunctionCallMode::Blocking,
@@ -46,10 +48,10 @@ impl Write for ThreadsafeWriteable {
4648
}
4749
impl Write for JsFileLike<'_> {
4850
fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
49-
let stream_write: JsFunction = self.inner.get_named_property("push").unwrap();
50-
let bytes = self.env.create_buffer_with_data(buf.to_owned()).unwrap();
51-
let js_buff = bytes.into_raw();
52-
stream_write.call(Some(&self.inner), &[js_buff]).unwrap();
51+
let stream_write: Function = self.inner.get_named_property("push").unwrap();
52+
let bytes = BufferSlice::from_data(self.env, buf.to_owned()).unwrap();
53+
let js_buff = bytes.into_unknown(self.env).unwrap();
54+
stream_write.call(js_buff).unwrap();
5355
Ok(buf.len())
5456
}
5557

@@ -61,11 +63,21 @@ impl Write for JsFileLike<'_> {
6163

6264
impl Write for JsWriteStream<'_> {
6365
fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
64-
let stream_write: JsFunction = self.inner.get_named_property("write").unwrap();
65-
let bytes = self.env.create_buffer_with_data(buf.to_owned()).unwrap();
66-
let js_buff = bytes.into_raw();
67-
stream_write.call(Some(&self.inner), &[js_buff]).unwrap();
68-
Ok(buf.len())
66+
let stream_write: Function = self.inner.get_named_property("write").unwrap();
67+
let bytes = BufferSlice::from_data(self.env, buf.to_owned()).unwrap();
68+
let js_buff: Buffer = bytes.into_buffer(self.env).unwrap();
69+
let js_buff = js_buff.into_unknown(self.env).unwrap();
70+
let result = stream_write.apply(Some(&self.inner), js_buff);
71+
72+
match result {
73+
Ok(_) => Ok(buf.len()),
74+
Err(e) => {
75+
return Err(Error::new(
76+
ErrorKind::InvalidData,
77+
format!("Error writing: {:?}", e),
78+
))
79+
}
80+
}
6981
}
7082

7183
fn flush(&mut self) -> Result<(), io::Error> {

src/lazy/dataframe.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl JsLazyGroupBy {
4747
#[napi]
4848
impl JsLazyFrame {
4949
#[napi(catch_unwind)]
50-
pub fn to_js(&self, env: Env) -> napi::Result<napi::JsUnknown> {
50+
pub fn to_js(&self, env: Env) -> napi::Result<napi::Unknown<'_>> {
5151
env.to_js_value(&self.ldf.logical_plan)
5252
}
5353

@@ -293,8 +293,8 @@ impl JsLazyFrame {
293293
other: &JsLazyFrame,
294294
left_on: &JsExpr,
295295
right_on: &JsExpr,
296-
left_by: Option<Vec<&str>>,
297-
right_by: Option<Vec<&str>>,
296+
left_by: Option<Vec<String>>,
297+
right_by: Option<Vec<String>>,
298298
allow_parallel: bool,
299299
force_parallel: bool,
300300
suffix: String,
@@ -506,10 +506,10 @@ impl JsLazyFrame {
506506
#[napi(catch_unwind)]
507507
pub fn unpivot(
508508
&self,
509-
id_vars: Vec<&str>,
510-
value_vars: Vec<&str>,
511-
variable_name: Option<&str>,
512-
value_name: Option<&str>,
509+
id_vars: Vec<String>,
510+
value_vars: Vec<String>,
511+
variable_name: Option<String>,
512+
value_name: Option<String>,
513513
) -> JsLazyFrame {
514514
let args = UnpivotArgsDSL {
515515
on: strings_to_selector(value_vars),

src/lazy/dsl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl ToExprs for Vec<&JsExpr> {
4949
#[napi]
5050
impl JsExpr {
5151
#[napi(catch_unwind)]
52-
pub fn to_js(&self, env: Env) -> napi::Result<napi::JsUnknown> {
52+
pub fn to_js(&self, env: Env) -> napi::Result<napi::Unknown<'_>> {
5353
env.to_js_value(&self.inner)
5454
}
5555
#[napi(catch_unwind)]

src/list_construction.rs

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::prelude::*;
2-
use napi::JsTypedArrayValue;
2+
use napi::bindgen_prelude::*;
33

44
macro_rules! typed_to_chunked {
55
($arr:expr, $type:ty, $pl_type:ty) => {{
@@ -11,7 +11,7 @@ macro_rules! typed_to_chunked {
1111
}
1212

1313
macro_rules! typed_option_or_null {
14-
($name:expr, $arr:expr, $type:ty, $dtype:expr, $pl_type:ty) => {{
14+
($name:expr, $arr:expr, $type:ty, $dtype:expr, $pl_type:ty, $arry_type:ty) => {{
1515
let len = $arr.len();
1616
let mut builder = ListPrimitiveChunkedBuilder::<$pl_type>::new(
1717
$name,
@@ -20,11 +20,10 @@ macro_rules! typed_option_or_null {
2020
$dtype,
2121
);
2222
for idx in 0..len {
23-
let obj: napi::JsUnknown = $arr.get(idx)?.unwrap();
23+
let obj: napi::Unknown = $arr.get(idx)?.unwrap();
2424
if obj.is_typedarray()? {
25-
let buff: napi::JsTypedArray = unsafe { obj.cast() };
26-
let v = buff.into_value()?;
27-
let ca = typed_to_chunked!(v, $type, $pl_type);
25+
let buff: $arry_type = unsafe { obj.cast()? };
26+
let ca = typed_to_chunked!(buff, $type, $pl_type);
2827
builder.append_iter(ca.into_iter())
2928
} else {
3029
let values: Either<Array, Null> = $arr.get(idx)?.unwrap();
@@ -51,7 +50,7 @@ macro_rules! typed_option_or_null {
5150
}};
5251
}
5352
macro_rules! build_list_with_downcast {
54-
($name:expr, $arr:expr, $type:ty, $dtype:expr, $pl_type:ty) => {{
53+
($name:expr, $arr:expr, $type:ty, $dtype:expr, $pl_type:ty, $arry_type:ty) => {{
5554
let len = $arr.len();
5655
let mut builder = ListPrimitiveChunkedBuilder::<$pl_type>::new(
5756
$name,
@@ -60,11 +59,10 @@ macro_rules! build_list_with_downcast {
6059
$dtype,
6160
);
6261
for idx in 0..len {
63-
let obj: napi::JsUnknown = $arr.get(idx)?.unwrap();
62+
let obj: napi::Unknown = $arr.get(idx)?.unwrap();
6463
if obj.is_typedarray()? {
65-
let buff: napi::JsTypedArray = unsafe { obj.cast() };
66-
let v = buff.into_value()?;
67-
let ca = typed_to_chunked!(v, $type, $pl_type);
64+
let buff: $arry_type = unsafe { obj.cast()? };
65+
let ca = typed_to_chunked!(buff, $type, $pl_type);
6866
builder.append_iter(ca.into_iter())
6967
} else {
7068
let values: Either<Array, Null> = $arr.get(idx)?.unwrap();
@@ -100,89 +98,84 @@ pub fn js_arr_to_list(name: &str, arr: &Array, dtype: &DataType) -> napi::Result
10098
arr,
10199
i8,
102100
DataType::Int8,
103-
Int8Type
101+
Int8Type,
102+
Int8Array
104103
),
105104
DataType::UInt8 => build_list_with_downcast!(
106105
PlSmallStr::from_str(name),
107106
arr,
108107
u8,
109108
DataType::UInt8,
110-
UInt8Type
109+
UInt8Type,
110+
Uint8Array
111111
),
112112
DataType::Int16 => build_list_with_downcast!(
113113
PlSmallStr::from_str(name),
114114
arr,
115115
i16,
116116
DataType::Int16,
117-
Int16Type
117+
Int16Type,
118+
Int16Array
118119
),
119120
DataType::UInt16 => build_list_with_downcast!(
120121
PlSmallStr::from_str(name),
121122
arr,
122123
u16,
123124
DataType::UInt16,
124-
UInt16Type
125+
UInt16Type,
126+
Uint16Array
125127
),
126128
DataType::Int32 => typed_option_or_null!(
127129
PlSmallStr::from_str(name),
128130
arr,
129131
i32,
130132
DataType::Int32,
131-
Int32Type
133+
Int32Type,
134+
Int32Array
132135
),
133136
DataType::UInt32 => typed_option_or_null!(
134137
PlSmallStr::from_str(name),
135138
arr,
136139
u32,
137140
DataType::UInt32,
138-
UInt32Type
141+
UInt32Type,
142+
Uint32Array
139143
),
140144
DataType::Float32 => {
141145
build_list_with_downcast!(
142146
PlSmallStr::from_str(name),
143147
arr,
144148
f32,
145149
DataType::Float32,
146-
Float32Type
150+
Float32Type,
151+
Float32Array
147152
)
148153
}
149154
DataType::Int64 => typed_option_or_null!(
150155
PlSmallStr::from_str(name),
151156
arr,
152157
i64,
153158
DataType::Int64,
154-
Int64Type
159+
Int64Type,
160+
BigInt64Array
155161
),
156162
DataType::Float64 => typed_option_or_null!(
157163
PlSmallStr::from_str(name),
158164
arr,
159165
f64,
160166
DataType::Float64,
161-
Float64Type
167+
Float64Type,
168+
Float64Array
162169
),
163170
DataType::UInt64 => build_list_with_downcast!(
164171
PlSmallStr::from_str(name),
165172
arr,
166173
u64,
167174
DataType::UInt64,
168-
UInt64Type
175+
UInt64Type,
176+
BigUint64Array
169177
),
170-
DataType::String => {
171-
let mut builder = ListStringChunkedBuilder::new(
172-
PlSmallStr::from_str(name),
173-
len as usize,
174-
(len as usize) * 5,
175-
);
176-
for idx in 0..len {
177-
let values: Either<Vec<Option<&str>>, Null> = arr.get(idx)?.unwrap();
178-
179-
match values {
180-
Either::A(inner_arr) => builder.append_trusted_len_iter(inner_arr.into_iter()),
181-
Either::B(_) => builder.append_null(),
182-
}
183-
}
184-
builder.finish().into_series()
185-
}
178+
DataType::String => arr.to_series(name),
186179
DataType::Boolean => {
187180
let mut builder = ListBooleanChunkedBuilder::new(
188181
PlSmallStr::from_str(name),
@@ -245,7 +238,8 @@ pub fn js_arr_to_list(name: &str, arr: &Array, dtype: &DataType) -> napi::Result
245238
Ok(s)
246239
}
247240

248-
pub fn from_typed_array(arr: &JsTypedArrayValue) -> JsResult<Series> {
241+
/*
242+
pub fn from_typed_array(arr: &napi::JsTypedArrayValue) -> JsResult<Series> {
249243
let dtype: JsDataType = arr.typedarray_type.into();
250244
let series = match dtype {
251245
JsDataType::Int8 => typed_to_chunked!(arr, i8, Int8Type).into(),
@@ -262,4 +256,4 @@ pub fn from_typed_array(arr: &JsTypedArrayValue) -> JsResult<Series> {
262256
};
263257
264258
Ok(series)
265-
}
259+
} */

0 commit comments

Comments
 (0)