chore(napi): remove unsafe from CallContext::this_unchecked

This commit is contained in:
LongYinan 2020-11-20 09:09:57 +08:00
parent 89d507d522
commit e337a58714
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
4 changed files with 7 additions and 7 deletions

View file

@ -74,7 +74,7 @@ impl<'env> CallContext<'env> {
} }
#[inline(always)] #[inline(always)]
pub unsafe fn this_unchecked<T: NapiValue>(&self) -> T { pub fn this_unchecked<T: NapiValue>(&self) -> T {
T::from_raw_unchecked(self.env.0, self.raw_this) unsafe { T::from_raw_unchecked(self.env.0, self.raw_this) }
} }
} }

View file

@ -19,7 +19,7 @@ fn create_test_class(ctx: CallContext) -> Result<JsFunction> {
#[js_function(1)] #[js_function(1)]
fn test_class_constructor(ctx: CallContext) -> Result<JsUndefined> { fn test_class_constructor(ctx: CallContext) -> Result<JsUndefined> {
let count: i32 = ctx.get::<JsNumber>(0)?.try_into()?; let count: i32 = ctx.get::<JsNumber>(0)?.try_into()?;
let mut this: JsObject = unsafe { ctx.this_unchecked() }; let mut this: JsObject = ctx.this_unchecked();
ctx ctx
.env .env
.wrap(&mut this, NativeClass { value: count + 100 })?; .wrap(&mut this, NativeClass { value: count + 100 })?;
@ -30,7 +30,7 @@ fn test_class_constructor(ctx: CallContext) -> Result<JsUndefined> {
#[js_function(1)] #[js_function(1)]
fn add_count(ctx: CallContext) -> Result<JsUndefined> { fn add_count(ctx: CallContext) -> Result<JsUndefined> {
let add: i32 = ctx.get::<JsNumber>(0)?.try_into()?; let add: i32 = ctx.get::<JsNumber>(0)?.try_into()?;
let mut this: JsObject = unsafe { ctx.this_unchecked() }; let mut this: JsObject = ctx.this_unchecked();
let count: i32 = this.get_named_property::<JsNumber>("count")?.try_into()?; let count: i32 = this.get_named_property::<JsNumber>("count")?.try_into()?;
this.set_named_property("count", ctx.env.create_int32(count + add)?)?; this.set_named_property("count", ctx.env.create_int32(count + add)?)?;
ctx.env.get_undefined() ctx.env.get_undefined()
@ -39,7 +39,7 @@ fn add_count(ctx: CallContext) -> Result<JsUndefined> {
#[js_function(1)] #[js_function(1)]
fn add_native_count(ctx: CallContext) -> Result<JsNumber> { fn add_native_count(ctx: CallContext) -> Result<JsNumber> {
let add: i32 = ctx.get::<JsNumber>(0)?.try_into()?; let add: i32 = ctx.get::<JsNumber>(0)?.try_into()?;
let this: JsObject = unsafe { ctx.this_unchecked() }; let this: JsObject = ctx.this_unchecked();
let native_class: &mut NativeClass = ctx.env.unwrap(&this)?; let native_class: &mut NativeClass = ctx.env.unwrap(&this)?;
native_class.value = native_class.value + add; native_class.value = native_class.value + add;
ctx.env.create_int32(native_class.value) ctx.env.create_int32(native_class.value)

View file

@ -13,7 +13,7 @@ pub fn call_function(ctx: CallContext) -> Result<JsNull> {
#[js_function(1)] #[js_function(1)]
pub fn call_function_with_this(ctx: CallContext) -> Result<JsNull> { pub fn call_function_with_this(ctx: CallContext) -> Result<JsNull> {
let js_this: JsObject = unsafe { ctx.this_unchecked() }; let js_this: JsObject = ctx.this_unchecked();
let js_func = ctx.get::<JsFunction>(0)?; let js_func = ctx.get::<JsFunction>(0)?;
js_func.call(Some(&js_this), &[])?; js_func.call(Some(&js_this), &[])?;

View file

@ -146,7 +146,7 @@ fn test_define_properties(ctx: CallContext) -> Result<JsUndefined> {
#[js_function(1)] #[js_function(1)]
fn add(ctx: CallContext) -> Result<JsUndefined> { fn add(ctx: CallContext) -> Result<JsUndefined> {
let mut this: JsObject = unsafe { ctx.this_unchecked() }; let mut this: JsObject = ctx.this_unchecked();
let count: i32 = this.get_named_property::<JsNumber>("count")?.try_into()?; let count: i32 = this.get_named_property::<JsNumber>("count")?.try_into()?;
let value_to_add: i32 = ctx.get::<JsNumber>(0)?.try_into()?; let value_to_add: i32 = ctx.get::<JsNumber>(0)?.try_into()?;
this.set_named_property("count", ctx.env.create_int32(count + value_to_add)?)?; this.set_named_property("count", ctx.env.create_int32(count + value_to_add)?)?;