From 9e14f1834753843a3b778647d1efc4e9720e1ada Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sun, 26 Jan 2020 17:42:28 +0100 Subject: [PATCH] Add Object::creation_context() (#255) And also Array::creation_context(), Function::creation_context(), etc., because they inherit from Object. --- src/binding.cc | 4 ++++ src/object.rs | 12 ++++++++++++ tests/test_api.rs | 9 +++++++++ 3 files changed, 25 insertions(+) diff --git a/src/binding.cc b/src/binding.cc index b045972f..1c57c4a9 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -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)); } diff --git a/src/object.rs b/src/object.rs index 60550955..79055eb4 100644 --- a/src/object.rs +++ b/src/object.rs @@ -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 { diff --git a/tests/test_api.rs b/tests/test_api.rs index aabbac7d..f7ef1cde 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -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");