mirror of
https://github.com/denoland/rusty_v8.git
synced 2024-12-23 15:50:11 -05:00
add Map::size and Map::as_array (#333)
This commit is contained in:
parent
5e879cfa04
commit
5a5bad9adf
3 changed files with 65 additions and 0 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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::<v8::Map>::try_from(value).unwrap());
|
||||
assert!(value != v8::Object::new(scope));
|
||||
assert_eq!(v8::Local::<v8::Map>::try_from(value).unwrap().size(), 2);
|
||||
let map = v8::Local::<v8::Map>::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)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue