From 6c5f1890632651a81af2d648158c3358b2e842c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 2 Jan 2020 19:56:28 +0100 Subject: [PATCH] add Object::get_identity_hash (#169) --- src/binding.cc | 4 ++++ src/object.rs | 10 ++++++++++ tests/test_api.rs | 3 ++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/binding.cc b/src/binding.cc index bc1dc7f3..774ed23c 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -610,6 +610,10 @@ v8::Isolate* v8__Object__GetIsolate(v8::Object& self) { return self.GetIsolate(); } +int v8__Object__GetIdentityHash(v8::Object& self) { + return self.GetIdentityHash(); +} + 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 f342bdc0..ce89da52 100644 --- a/src/object.rs +++ b/src/object.rs @@ -47,6 +47,7 @@ extern "C" { key: *const Name, value: *const Value, ) -> MaybeBool; + fn v8__Object__GetIdentityHash(object: &Object) -> int; fn v8__Array__New(isolate: *mut Isolate, length: int) -> *mut Array; } @@ -148,6 +149,15 @@ impl Object { pub fn get_isolate(&mut self) -> &Isolate { unsafe { v8__Object__GetIsolate(self) } } + + /// Returns the identity hash for this object. The current implementation + /// uses a hidden property on the object to store the identity hash. + /// + /// The return value will never be 0. Also, it is not guaranteed to be + /// unique. + pub fn get_identity_hash(&self) -> int { + unsafe { v8__Object__GetIdentityHash(self) } + } } impl Array { diff --git a/tests/test_api.rs b/tests/test_api.rs index e476af8f..59c73c46 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -827,7 +827,8 @@ fn object() { let object_ = v8::Object::new(scope); assert!(!object_.is_null_or_undefined()); - + let id = object_.get_identity_hash(); + assert_ne!(id, 0); context.exit(); } drop(locker);