Skip to content

Commit

Permalink
Exposing Value::TypeOf (denoland#1133)
Browse files Browse the repository at this point in the history
  • Loading branch information
WingZer0o committed Nov 27, 2022
1 parent 04a5fa6 commit 3c6d259
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,10 @@ bool v8__Value__BooleanValue(const v8::Value& self, v8::Isolate* isolate) {
return self.BooleanValue(isolate);
}

const v8::String* v8__Value__TypeOf(v8::Value& self, v8::Isolate* isolate) {
return local_to_ptr(self.TypeOf(isolate));
}

const v8::Primitive* v8__Null(v8::Isolate* isolate) {
return local_to_ptr(v8::Null(isolate));
}
Expand Down
15 changes: 15 additions & 0 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ extern "C" {
fn v8__Value__BooleanValue(this: *const Value, isolate: *mut Isolate)
-> bool;
fn v8__Value__GetHash(this: *const Value) -> int;
fn v8__Value__TypeOf(
this: *const Value,
isolate: *mut Isolate,
) -> *const String;
}

impl Value {
Expand Down Expand Up @@ -702,4 +706,15 @@ impl Value {
pub fn get_hash(&self) -> NonZeroI32 {
unsafe { NonZeroI32::new_unchecked(v8__Value__GetHash(self)) }
}

#[inline(always)]
pub fn type_of<'s>(
&self,
scope: &mut HandleScope<'s, ()>,
) -> Local<'s, String> {
unsafe {
scope.cast_local(|sd| v8__Value__TypeOf(self, sd.get_isolate_ptr()))
}
.unwrap()
}
}
19 changes: 19 additions & 0 deletions tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4424,6 +4424,25 @@ fn shared_array_buffer() {
}
}

#[test]
fn typeof_checker() {
let _setup_guard = setup();

let isolate = &mut v8::Isolate::new(Default::default());
let scope = &mut v8::HandleScope::new(isolate);
let context = v8::Context::new(scope);
let scope = &mut v8::ContextScope::new(scope, context);

let value_1 = eval(scope, "").unwrap();
let type_of = value_1.type_of(scope);
let value_2 = eval(scope, "").unwrap();
let type_of_2 = value_2.type_of(scope);
assert_eq!(type_of, type_of_2);
let value_3 = eval(scope, "1").unwrap();
let type_of_3 = value_3.type_of(scope);
assert_ne!(type_of_2, type_of_3);
}

#[test]
#[allow(clippy::cognitive_complexity)]
#[allow(clippy::eq_op)]
Expand Down

0 comments on commit 3c6d259

Please sign in to comment.