Skip to content

Commit

Permalink
feat: Add v8::Object::get_constructor_name (#1212)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju authored Apr 21, 2023
1 parent 2c45f72 commit f049ad0
Show file tree
Hide file tree
Showing 3 changed files with 51 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 @@ -1241,6 +1241,10 @@ MaybeBool v8__Object__SetPrototype(const v8::Object& self,
ptr_to_local(&context), ptr_to_local(&prototype)));
}

const v8::String* v8__Object__GetConstructorName(v8::Object& self) {
return local_to_ptr(self.GetConstructorName());
}

MaybeBool v8__Object__CreateDataProperty(const v8::Object& self,
const v8::Context& context,
const v8::Name& key,
Expand Down
8 changes: 8 additions & 0 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::Private;
use crate::PropertyAttribute;
use crate::PropertyDescriptor;
use crate::PropertyFilter;
use crate::String;
use crate::Value;
use std::convert::TryFrom;
use std::ffi::c_void;
Expand Down Expand Up @@ -74,6 +75,7 @@ extern "C" {
context: *const Context,
prototype: *const Value,
) -> MaybeBool;
fn v8__Object__GetConstructorName(this: *const Object) -> *const String;
fn v8__Object__CreateDataProperty(
this: *const Object,
context: *const Context,
Expand Down Expand Up @@ -295,6 +297,12 @@ impl Object {
.into()
}

/// Returns the name of the function invoked as a constructor for this object.
#[inline(always)]
pub fn get_constructor_name(&self) -> Local<String> {
unsafe { Local::from_raw(v8__Object__GetConstructorName(self)) }.unwrap()
}

/// Implements CreateDataProperty (ECMA-262, 7.3.4).
///
/// Defines a configurable, writable, enumerable property with the given value
Expand Down
39 changes: 39 additions & 0 deletions tests/test_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5719,6 +5719,45 @@ fn take_heap_snapshot() {
}
}

#[test]
fn get_constructor_name() {
let _setup_guard = setup::parallel_test();
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);

fn check_ctor_name(
scope: &mut v8::HandleScope,
script: &str,
expected_name: &str,
) {
let val = eval(scope, script).unwrap();
let obj: v8::Local<v8::Object> = val.try_into().unwrap();
assert_eq!(
obj.get_constructor_name().to_rust_string_lossy(scope),
expected_name
);
}

let code = r#"
function Parent() {};
function Child() {};
Child.prototype = new Parent();
Child.prototype.constructor = Child;
var outer = { inner: (0, function() { }) };
var p = new Parent();
var c = new Child();
var x = new outer.inner();
var proto = Child.prototype;
"#;
eval(scope, code).unwrap();
check_ctor_name(scope, "p", "Parent");
check_ctor_name(scope, "c", "Child");
check_ctor_name(scope, "x", "outer.inner");
check_ctor_name(scope, "proto", "Parent");
}

#[test]
fn test_prototype_api() {
let _setup_guard = setup::parallel_test();
Expand Down

0 comments on commit f049ad0

Please sign in to comment.