0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-24 08:09:16 -05:00

Add Object::creation_context() (#255)

And also Array::creation_context(), Function::creation_context(), etc.,
because they inherit from Object.
This commit is contained in:
Ben Noordhuis 2020-01-26 17:42:28 +01:00 committed by Bert Belder
parent 683aa2b2a9
commit 9e14f18347
3 changed files with 25 additions and 0 deletions

View file

@ -710,6 +710,10 @@ int v8__Object__GetIdentityHash(v8::Object& self) {
return self.GetIdentityHash();
}
v8::Context* v8__Object__CreationContext(v8::Object& self) {
return local_to_ptr(self.CreationContext());
}
v8::Array* v8__Array__New(v8::Isolate* isolate, int length) {
return local_to_ptr(v8::Array::New(isolate, length));
}

View file

@ -52,6 +52,7 @@ extern "C" {
attr: PropertyAttribute,
) -> MaybeBool;
fn v8__Object__GetIdentityHash(object: &Object) -> int;
fn v8__Object__CreationContext(object: &Object) -> *mut Context;
fn v8__Array__New(isolate: *mut Isolate, length: int) -> *mut Array;
}
@ -163,6 +164,17 @@ impl Object {
pub fn get_identity_hash(&self) -> int {
unsafe { v8__Object__GetIdentityHash(self) }
}
/// Returns the context in which the object was created.
pub fn creation_context<'a>(
&self,
scope: &mut impl ToLocal<'a>,
) -> Local<'a, Context> {
unsafe {
let ptr = v8__Object__CreationContext(self);
scope.to_local(ptr).unwrap()
}
}
}
impl Array {

View file

@ -1023,6 +1023,9 @@ fn object() {
&[v1, v2],
);
assert!(!object.is_null_or_undefined());
let lhs = object.creation_context(scope).global(scope);
let rhs = context.global(scope);
assert!(lhs.strict_equals(rhs.into()));
let object_ = v8::Object::new(scope);
assert!(!object_.is_null_or_undefined());
@ -1050,6 +1053,9 @@ fn array() {
let s2 = v8::String::new(scope, "b").unwrap();
let index2 = v8::Integer::new(scope, 1);
let array = v8::Array::new(scope, 2);
let lhs = array.creation_context(scope).global(scope);
let rhs = context.global(scope);
assert!(lhs.strict_equals(rhs.into()));
array.set(context, index1.into(), s1.into());
array.set(context, index2.into(), s2.into());
@ -1296,6 +1302,9 @@ fn function() {
let function = fn_template
.get_function(scope, context)
.expect("Unable to create function");
let lhs = function.creation_context(scope).global(scope);
let rhs = context.global(scope);
assert!(lhs.strict_equals(rhs.into()));
function
.call(scope, context, recv, &[])
.expect("Function call failed");