Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 23e9de2

Browse filesBrowse files
authored
refactor!(napi): add lifetime to Unkonwn (#2611)
Remove JsUnknown
1 parent ed9ca20 commit 23e9de2
Copy full SHA for 23e9de2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner
Expand file treeCollapse file tree

42 files changed

+262
-381
lines changed

‎bench/src/async_compute.rs

Copy file name to clipboardExpand all lines: bench/src/async_compute.rs
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ impl Task for BufferLength {
1717
}
1818

1919
#[js_function(1)]
20-
fn bench_async_task(ctx: CallContext) -> Result<Unknown> {
20+
fn bench_async_task(ctx: CallContext) -> Result<PromiseRaw<u32>> {
2121
let n = ctx.get::<Buffer>(0)?;
2222
let task = BufferLength(n);
2323
let async_promise = ctx.env.spawn(task)?;
24-
Ok(async_promise.promise_object().into_unknown())
24+
Ok(async_promise.promise_object())
2525
}
2626

2727
#[js_function(2)]

‎bench/src/create_array.rs

Copy file name to clipboardExpand all lines: bench/src/create_array.rs
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use napi::{ContextlessResult, Env, JsObject, JsString, JsUnknown, Result};
1+
use napi::{ContextlessResult, Env, JsObject, JsString, Result, Unknown};
22
use serde_json::to_string;
33

44
pub fn register_js(exports: &mut JsObject) -> Result<()> {
@@ -26,7 +26,7 @@ pub fn create_array(env: Env) -> ContextlessResult<JsObject> {
2626
}
2727

2828
#[contextless_function]
29-
pub fn create_array_with_serde_trait(env: Env) -> ContextlessResult<JsUnknown> {
29+
pub fn create_array_with_serde_trait(env: Env) -> ContextlessResult<Unknown<'static>> {
3030
let a: Vec<u32> = vec![42; 1000];
3131
env.to_js_value(&a).map(Some)
3232
}

‎bench/src/get_set_property.rs

Copy file name to clipboardExpand all lines: bench/src/get_set_property.rs
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn set_miter_native(ctx: CallContext) -> Result<JsUndefined> {
9494
}
9595

9696
#[js_function]
97-
fn get_miter(ctx: CallContext) -> Result<JsUnknown> {
97+
fn get_miter(ctx: CallContext) -> Result<Unknown> {
9898
let this = ctx.this_unchecked::<JsObject>();
9999
this.get_named_property("_miterLimit")
100100
}
@@ -136,7 +136,7 @@ fn set_line_join_native(ctx: CallContext) -> Result<JsUndefined> {
136136
}
137137

138138
#[js_function]
139-
fn get_line_join(ctx: CallContext) -> Result<JsUnknown> {
139+
fn get_line_join(ctx: CallContext) -> Result<Unknown> {
140140
let this = ctx.this_unchecked::<JsObject>();
141141

142142
this.get_named_property("_lineJoin")

‎bench/src/get_value_from_js.rs

Copy file name to clipboardExpand all lines: bench/src/get_value_from_js.rs
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use napi::{CallContext, JsObject, JsString, JsUndefined, JsUnknown, Result};
1+
use napi::{CallContext, JsObject, JsString, JsUndefined, Result, Unknown};
22
use serde_json::from_str;
33

44
pub fn register_js(exports: &mut JsObject) -> Result<()> {
@@ -26,9 +26,9 @@ fn get_array_from_js_array(ctx: CallContext) -> Result<JsUndefined> {
2626
fn get_array_with_for_loop(ctx: CallContext) -> Result<JsUndefined> {
2727
let input = ctx.get::<JsObject>(0)?;
2828
let array_length = input.get_array_length_unchecked()? as usize;
29-
let mut result: Vec<JsUnknown> = Vec::with_capacity(array_length);
29+
let mut result: Vec<Unknown> = Vec::with_capacity(array_length);
3030
for i in 0..array_length {
31-
result.insert(i, input.get_element::<JsUnknown>(i as u32)?);
31+
result.insert(i, input.get_element::<Unknown>(i as u32)?);
3232
}
3333
ctx.env.get_undefined()
3434
}

‎crates/napi/src/async_work.rs

Copy file name to clipboardExpand all lines: crates/napi/src/async_work.rs
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct AsyncWorkPromise<T> {
3030
}
3131

3232
impl<T> AsyncWorkPromise<T> {
33-
pub fn promise_object(&self) -> PromiseRaw<T> {
33+
pub fn promise_object<'env>(&self) -> PromiseRaw<'env, T> {
3434
PromiseRaw::new(self.env, self.raw_promise)
3535
}
3636

‎crates/napi/src/bindgen_runtime/async_iterator.rs

Copy file name to clipboardExpand all lines: crates/napi/src/bindgen_runtime/async_iterator.rs
+16-10Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -396,20 +396,26 @@ extern "C" fn generator_throw<T: AsyncGenerator>(
396396
);
397397
g.catch(
398398
Env(env),
399-
Unknown(Value {
400-
env,
401-
value: undefined,
402-
value_type: crate::ValueType::Undefined,
403-
}),
399+
Unknown(
400+
Value {
401+
env,
402+
value: undefined,
403+
value_type: crate::ValueType::Undefined,
404+
},
405+
std::marker::PhantomData,
406+
),
404407
)
405408
} else {
406409
g.catch(
407410
Env(env),
408-
Unknown(Value {
409-
env,
410-
value: argv[0],
411-
value_type: crate::ValueType::Unknown,
412-
}),
411+
Unknown(
412+
Value {
413+
env,
414+
value: argv[0],
415+
value_type: crate::ValueType::Unknown,
416+
},
417+
std::marker::PhantomData,
418+
),
413419
)
414420
};
415421
match Env::from_raw(env).spawn_future_with_callback(caught, |env, value| {

‎crates/napi/src/bindgen_runtime/iterator.rs

Copy file name to clipboardExpand all lines: crates/napi/src/bindgen_runtime/iterator.rs
+21-11Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ pub trait Generator {
2929
#[allow(unused_variables)]
3030
/// Implement catch to handle the `Generator.throw()`
3131
/// <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator/throw>
32-
fn catch(&mut self, env: Env, value: Unknown) -> Result<Option<Self::Yield>, Unknown> {
32+
fn catch<'env>(
33+
&'env mut self,
34+
env: Env,
35+
value: Unknown<'env>,
36+
) -> Result<Option<Self::Yield>, Unknown<'env>> {
3337
Err(value)
3438
}
3539
}
@@ -442,20 +446,26 @@ extern "C" fn generator_throw<T: Generator>(
442446
);
443447
g.catch(
444448
Env(env),
445-
Unknown(Value {
446-
env,
447-
value: undefined,
448-
value_type: crate::ValueType::Undefined,
449-
}),
449+
Unknown(
450+
Value {
451+
env,
452+
value: undefined,
453+
value_type: crate::ValueType::Unknown,
454+
},
455+
std::marker::PhantomData,
456+
),
450457
)
451458
} else {
452459
g.catch(
453460
Env(env),
454-
Unknown(Value {
455-
env,
456-
value: argv[0],
457-
value_type: crate::ValueType::Unknown,
458-
}),
461+
Unknown(
462+
Value {
463+
env,
464+
value: argv[0],
465+
value_type: crate::ValueType::Unknown,
466+
},
467+
std::marker::PhantomData,
468+
),
459469
)
460470
};
461471
let mut result = ptr::null_mut();

‎crates/napi/src/bindgen_runtime/js_values.rs

Copy file name to clipboardExpand all lines: crates/napi/src/bindgen_runtime/js_values.rs
+3-18Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
sync::{Arc, Mutex},
55
};
66

7-
use crate::{check_status, sys, Error, JsUnknown, NapiRaw, NapiValue, Result, Status, ValueType};
7+
use crate::{check_status, sys, Error, NapiRaw, NapiValue, Result, Status, ValueType};
88

99
mod array;
1010
mod arraybuffer;
@@ -35,7 +35,7 @@ mod symbol;
3535
mod task;
3636
mod value_ref;
3737

38-
pub use crate::js_values::JsUnknown as Unknown;
38+
pub use crate::js_values::Unknown;
3939
#[cfg(feature = "napi5")]
4040
pub use crate::JsDate as Date;
4141
pub use array::*;
@@ -72,18 +72,6 @@ pub trait ToNapiValue {
7272
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value>;
7373
}
7474

75-
impl TypeName for JsUnknown {
76-
fn type_name() -> &'static str {
77-
"unknown"
78-
}
79-
80-
fn value_type() -> ValueType {
81-
ValueType::Unknown
82-
}
83-
}
84-
85-
impl ValidateNapiValue for JsUnknown {}
86-
8775
impl ToNapiValue for sys::napi_value {
8876
unsafe fn to_napi_value(_env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
8977
Ok(val)
@@ -108,7 +96,7 @@ pub trait FromNapiValue: Sized {
10896
/// this function called to convert napi values to native rust values
10997
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self>;
11098

111-
fn from_unknown(value: JsUnknown) -> Result<Self> {
99+
fn from_unknown(value: Unknown) -> Result<Self> {
112100
unsafe { Self::from_napi_value(value.0.env, value.0.value) }
113101
}
114102
}
@@ -139,9 +127,6 @@ pub trait ValidateNapiValue: TypeName {
139127
/// So we need to create `Promise.reject(T)` in this function.
140128
unsafe fn validate(env: sys::napi_env, napi_val: sys::napi_value) -> Result<sys::napi_value> {
141129
let value_type = Self::value_type();
142-
if value_type == ValueType::Unknown {
143-
return Ok(ptr::null_mut());
144-
}
145130

146131
let mut result = -1;
147132
check_status!(

‎crates/napi/src/bindgen_runtime/js_values/function.rs

Copy file name to clipboardExpand all lines: crates/napi/src/bindgen_runtime/js_values/function.rs
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ impl_tuple_conversion!(
111111
/// It can only live in the scope of a function call.
112112
/// If you want to use it outside the scope of a function call, you can turn it into a reference.
113113
/// By calling the `create_ref` method.
114-
pub struct Function<'scope, Args: JsValuesTupleIntoVec = Unknown, Return = Unknown> {
114+
pub struct Function<'scope, Args: JsValuesTupleIntoVec = Unknown<'scope>, Return = Unknown<'scope>>
115+
{
115116
pub(crate) env: sys::napi_env,
116117
pub(crate) value: sys::napi_value,
117118
pub(crate) _args: std::marker::PhantomData<Args>,

‎crates/napi/src/bindgen_runtime/js_values/promise_raw.rs

Copy file name to clipboardExpand all lines: crates/napi/src/bindgen_runtime/js_values/promise_raw.rs
+38-55Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,52 @@ use std::ptr;
66
use crate::bindgen_runtime::Promise;
77
use crate::{
88
bindgen_prelude::{FromNapiValue, Result, ToNapiValue, TypeName, ValidateNapiValue},
9-
check_status, sys,
9+
check_status, sys, Env, Error, JsObjectValue, JsValue, Value, ValueType,
1010
};
11-
use crate::{Env, Error, NapiRaw, NapiValue, Status};
12-
13-
use super::Unknown;
1411

1512
pub struct PromiseRaw<'env, T> {
1613
pub(crate) inner: sys::napi_value,
1714
env: sys::napi_env,
1815
_phantom: &'env PhantomData<T>,
1916
}
2017

18+
impl<T> JsValue for PromiseRaw<'_, T> {
19+
fn value(&self) -> Value {
20+
Value {
21+
env: self.env,
22+
value: self.inner,
23+
value_type: ValueType::Object,
24+
}
25+
}
26+
}
27+
28+
impl<T> JsObjectValue for PromiseRaw<'_, T> {}
29+
30+
impl<T: FromNapiValue> TypeName for PromiseRaw<'_, T> {
31+
fn type_name() -> &'static str {
32+
"Promise"
33+
}
34+
35+
fn value_type() -> crate::ValueType {
36+
crate::ValueType::Object
37+
}
38+
}
39+
40+
impl<T: FromNapiValue> ValidateNapiValue for PromiseRaw<'_, T> {
41+
unsafe fn validate(
42+
env: napi_sys::napi_env,
43+
napi_val: napi_sys::napi_value,
44+
) -> Result<napi_sys::napi_value> {
45+
validate_promise(env, napi_val)
46+
}
47+
}
48+
49+
impl<T: FromNapiValue> FromNapiValue for PromiseRaw<'_, T> {
50+
unsafe fn from_napi_value(env: napi_sys::napi_env, value: napi_sys::napi_value) -> Result<Self> {
51+
Ok(PromiseRaw::new(env, value))
52+
}
53+
}
54+
2155
impl<T> PromiseRaw<'_, T> {
2256
pub(crate) fn new(env: sys::napi_env, inner: sys::napi_value) -> Self {
2357
Self {
@@ -26,10 +60,6 @@ impl<T> PromiseRaw<'_, T> {
2660
_phantom: &PhantomData,
2761
}
2862
}
29-
30-
pub fn into_unknown(self) -> Unknown {
31-
unsafe { Unknown::from_raw_unchecked(self.env, self.inner) }
32-
}
3363
}
3464

3565
impl<'env, T: FromNapiValue> PromiseRaw<'env, T> {
@@ -222,53 +252,6 @@ impl<'env, T: FromNapiValue> PromiseRaw<'env, T> {
222252
}
223253
}
224254

225-
impl<T: FromNapiValue> TypeName for PromiseRaw<'_, T> {
226-
fn type_name() -> &'static str {
227-
"Promise"
228-
}
229-
230-
fn value_type() -> crate::ValueType {
231-
crate::ValueType::Object
232-
}
233-
}
234-
235-
impl<T: FromNapiValue> ValidateNapiValue for PromiseRaw<'_, T> {
236-
unsafe fn validate(
237-
env: napi_sys::napi_env,
238-
napi_val: napi_sys::napi_value,
239-
) -> Result<napi_sys::napi_value> {
240-
validate_promise(env, napi_val)
241-
}
242-
}
243-
244-
impl<T> NapiRaw for PromiseRaw<'_, T> {
245-
unsafe fn raw(&self) -> sys::napi_value {
246-
self.inner
247-
}
248-
}
249-
250-
impl<T> NapiValue for PromiseRaw<'_, T> {
251-
unsafe fn from_raw(env: napi_sys::napi_env, value: napi_sys::napi_value) -> Result<Self> {
252-
let mut is_promise = false;
253-
check_status!(unsafe { sys::napi_is_promise(env, value, &mut is_promise) })?;
254-
is_promise
255-
.then_some(Self {
256-
env,
257-
inner: value,
258-
_phantom: &PhantomData,
259-
})
260-
.ok_or_else(|| Error::new(Status::InvalidArg, "JavaScript value is not Promise"))
261-
}
262-
263-
unsafe fn from_raw_unchecked(env: napi_sys::napi_env, value: napi_sys::napi_value) -> Self {
264-
Self {
265-
env,
266-
inner: value,
267-
_phantom: &PhantomData,
268-
}
269-
}
270-
}
271-
272255
pub(crate) fn validate_promise(
273256
env: napi_sys::napi_env,
274257
napi_val: napi_sys::napi_value,

‎crates/napi/src/call_context.rs

Copy file name to clipboardExpand all lines: crates/napi/src/call_context.rs
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ impl<'env> CallContext<'env> {
7373
}
7474
}
7575

76-
pub fn get_all(&self) -> Vec<crate::JsUnknown> {
76+
pub fn get_all(&self) -> Vec<crate::Unknown> {
7777
/* (0 .. self.arg_len()).map(|i| self.get(i).unwrap()).collect() */
7878
self
7979
.args
8080
.iter()
81-
.map(|&raw| unsafe { crate::JsUnknown::from_raw_unchecked(self.env.0, raw) })
81+
.map(|&raw| unsafe { crate::Unknown::from_raw_unchecked(self.env.0, raw) })
8282
.collect()
8383
}
8484

‎crates/napi/src/env.rs

Copy file name to clipboardExpand all lines: crates/napi/src/env.rs
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,12 +1333,14 @@ impl Env {
13331333
/// ```
13341334
#[cfg(feature = "serde-json")]
13351335
#[allow(clippy::wrong_self_convention)]
1336-
pub fn to_js_value<T>(&self, node: &T) -> Result<JsUnknown>
1336+
pub fn to_js_value<'env, T>(&self, node: &T) -> Result<Unknown<'env>>
13371337
where
13381338
T: Serialize,
13391339
{
13401340
let s = Ser(self);
1341-
node.serialize(s).map(JsUnknown)
1341+
node
1342+
.serialize(s)
1343+
.map(|v| Unknown(v, std::marker::PhantomData))
13421344
}
13431345

13441346
/// ### Deserialize data from `JsValue`

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.