From 5a5bad9adf295d28956e478e8bf8be7a2480a11e Mon Sep 17 00:00:00 2001 From: FrankBlue Date: Fri, 3 Apr 2020 01:37:13 +0800 Subject: [PATCH] add Map::size and Map::as_array (#333) --- src/binding.cc | 6 ++++++ src/object.rs | 18 ++++++++++++++++++ tests/test_api.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/src/binding.cc b/src/binding.cc index 52cf95cd..7414e278 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -793,6 +793,12 @@ v8::Array* v8__Array__New_with_elements(v8::Isolate* isolate, uint32_t v8__Array__Length(const v8::Array& self) { return self.Length(); } +size_t v8__Map__Size(const v8::Map& self) { return self.Size(); } + +v8::Array* v8__Map__As__Array(const v8::Map& self) { + return local_to_ptr(self.AsArray()); +} + v8::Number* v8__Number__New(v8::Isolate* isolate, double value) { return *v8::Number::New(isolate, value); } diff --git a/src/object.rs b/src/object.rs index a44cd872..a41f90bc 100644 --- a/src/object.rs +++ b/src/object.rs @@ -6,6 +6,7 @@ use crate::AccessorNameGetterCallback; use crate::Array; use crate::Context; use crate::Local; +use crate::Map; use crate::Name; use crate::Object; use crate::PropertyAttribute; @@ -78,6 +79,8 @@ extern "C" { length: usize, ) -> *mut Array; fn v8__Array__Length(array: &Array) -> u32; + fn v8__Map__Size(map: &Map) -> usize; + fn v8__Map__As__Array(map: &Map) -> *mut Array; } impl Object { @@ -278,3 +281,18 @@ impl Array { unsafe { v8__Array__Length(self) } } } + +impl Map { + pub fn size(&self) -> usize { + unsafe { v8__Map__Size(self) } + } + /// Returns an array of length size() * 2, where index N is the Nth key and + /// index N + 1 is the Nth value. + pub fn as_array<'sc>( + &self, + scope: &mut impl ToLocal<'sc>, + ) -> Local<'sc, Array> { + let ptr = unsafe { v8__Map__As__Array(self) }; + unsafe { scope.to_local(ptr) }.unwrap() + } +} diff --git a/tests/test_api.rs b/tests/test_api.rs index acbbefa9..986dd2b1 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -2887,3 +2887,44 @@ fn test_prototype_api() { assert!(obj.get_prototype(scope).unwrap().is_null()); } } + +#[test] +fn test_map_api() { + let _setup_guard = setup(); + let mut params = v8::Isolate::create_params(); + params.set_array_buffer_allocator(v8::new_default_allocator()); + let mut isolate = v8::Isolate::new(params); + { + let mut hs = v8::HandleScope::new(&mut isolate); + let scope = hs.enter(); + let context = v8::Context::new(scope); + let mut cs = v8::ContextScope::new(scope, context); + let scope = cs.enter(); + + let value = eval(scope, context, "new Map([['r','s'],['v',8]])").unwrap(); + assert!(value.is_map()); + assert!(value == v8::Local::::try_from(value).unwrap()); + assert!(value != v8::Object::new(scope)); + assert_eq!(v8::Local::::try_from(value).unwrap().size(), 2); + let map = v8::Local::::try_from(value).unwrap(); + assert_eq!(map.size(), 2); + let map_array = map.as_array(scope); + assert_eq!(map_array.length(), 4); + assert!( + map_array.get_index(scope, context, 0).unwrap() + == v8::String::new(scope, "r").unwrap() + ); + assert!( + map_array.get_index(scope, context, 1).unwrap() + == v8::String::new(scope, "s").unwrap() + ); + assert!( + map_array.get_index(scope, context, 2).unwrap() + == v8::String::new(scope, "v").unwrap() + ); + assert!( + map_array.get_index(scope, context, 3).unwrap() + == v8::Number::new(scope, 8f64) + ); + } +}