mirror of
https://github.com/denoland/rusty_v8.git
synced 2024-11-24 15:19:31 -05:00
feat: v8::Object::PreviewEntries (#1276)
This commit is contained in:
parent
70239dc4ce
commit
c6fe9e70d0
3 changed files with 119 additions and 7 deletions
|
@ -1444,6 +1444,12 @@ const v8::Value* v8__Object__GetOwnPropertyDescriptor(
|
||||||
ptr_to_local(&context), ptr_to_local(&key)));
|
ptr_to_local(&context), ptr_to_local(&key)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const v8::Array* v8__Object__PreviewEntries(
|
||||||
|
const v8::Object& self,
|
||||||
|
bool* is_key_value) {
|
||||||
|
return maybe_local_to_ptr(ptr_to_local(&self)->PreviewEntries(is_key_value));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const v8::Array* v8__Array__New(v8::Isolate* isolate, int length) {
|
const v8::Array* v8__Array__New(v8::Isolate* isolate, int length) {
|
||||||
return local_to_ptr(v8::Array::New(isolate, length));
|
return local_to_ptr(v8::Array::New(isolate, length));
|
||||||
|
|
|
@ -26,6 +26,7 @@ use crate::String;
|
||||||
use crate::Value;
|
use crate::Value;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::ffi::c_void;
|
use std::ffi::c_void;
|
||||||
|
use std::mem::MaybeUninit;
|
||||||
use std::num::NonZeroI32;
|
use std::num::NonZeroI32;
|
||||||
use std::ptr::null;
|
use std::ptr::null;
|
||||||
|
|
||||||
|
@ -192,6 +193,10 @@ extern "C" {
|
||||||
context: *const Context,
|
context: *const Context,
|
||||||
key: *const Name,
|
key: *const Name,
|
||||||
) -> *const Value;
|
) -> *const Value;
|
||||||
|
fn v8__Object__PreviewEntries(
|
||||||
|
this: *const Object,
|
||||||
|
is_key_value: *mut bool,
|
||||||
|
) -> *const Array;
|
||||||
|
|
||||||
fn v8__Array__New(isolate: *mut Isolate, length: int) -> *const Array;
|
fn v8__Array__New(isolate: *mut Isolate, length: int) -> *const Array;
|
||||||
fn v8__Array__New_with_elements(
|
fn v8__Array__New_with_elements(
|
||||||
|
@ -807,6 +812,28 @@ impl Object {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// If this object is a Set, Map, WeakSet or WeakMap, this returns a
|
||||||
|
/// representation of the elements of this object as an array.
|
||||||
|
/// If this object is a SetIterator or MapIterator, this returns all elements
|
||||||
|
/// of the underlying collection, starting at the iterator's current position.
|
||||||
|
///
|
||||||
|
/// Also returns a boolean, indicating whether the returned array contains
|
||||||
|
/// key & values (for example when the value is Set.entries()).
|
||||||
|
pub fn preview_entries<'s>(
|
||||||
|
&self,
|
||||||
|
scope: &mut HandleScope<'s>,
|
||||||
|
) -> (Option<Local<'s, Array>>, bool) {
|
||||||
|
let mut is_key_value = MaybeUninit::uninit();
|
||||||
|
unsafe {
|
||||||
|
let val = scope.cast_local(|_| {
|
||||||
|
v8__Object__PreviewEntries(self, is_key_value.as_mut_ptr())
|
||||||
|
});
|
||||||
|
let is_key_value = is_key_value.assume_init();
|
||||||
|
|
||||||
|
(val, is_key_value)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Object integrity levels can be used to restrict what can be done to an
|
/// Object integrity levels can be used to restrict what can be done to an
|
||||||
|
|
|
@ -6741,6 +6741,85 @@ fn get_own_property_descriptor() {
|
||||||
assert!(desc.is_undefined());
|
assert!(desc.is_undefined());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn preview_entries() {
|
||||||
|
let _setup_guard = setup::parallel_test();
|
||||||
|
let isolate = &mut v8::Isolate::new(Default::default());
|
||||||
|
let scope = &mut v8::HandleScope::new(isolate);
|
||||||
|
let context = v8::Context::new(scope);
|
||||||
|
let scope = &mut v8::ContextScope::new(scope, context);
|
||||||
|
|
||||||
|
{
|
||||||
|
let obj = eval(
|
||||||
|
scope,
|
||||||
|
"var set = new Set([1,2,3]); set.delete(1); set.keys()",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let obj = obj.to_object(scope).unwrap();
|
||||||
|
let (preview, is_key_value) = obj.preview_entries(scope);
|
||||||
|
let preview = preview.unwrap();
|
||||||
|
assert!(!is_key_value);
|
||||||
|
assert_eq!(preview.length(), 2);
|
||||||
|
assert_eq!(
|
||||||
|
preview
|
||||||
|
.get_index(scope, 0)
|
||||||
|
.unwrap()
|
||||||
|
.number_value(scope)
|
||||||
|
.unwrap(),
|
||||||
|
2.0
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
preview
|
||||||
|
.get_index(scope, 1)
|
||||||
|
.unwrap()
|
||||||
|
.number_value(scope)
|
||||||
|
.unwrap(),
|
||||||
|
3.0
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let obj = eval(
|
||||||
|
scope,
|
||||||
|
"var set = new Set([1,2,3]); set.delete(2); set.entries()",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
let obj = obj.to_object(scope).unwrap();
|
||||||
|
let (preview, is_key_value) = obj.preview_entries(scope);
|
||||||
|
let preview = preview.unwrap();
|
||||||
|
assert!(is_key_value);
|
||||||
|
assert_eq!(preview.length(), 4);
|
||||||
|
let first = preview
|
||||||
|
.get_index(scope, 0)
|
||||||
|
.unwrap()
|
||||||
|
.number_value(scope)
|
||||||
|
.unwrap();
|
||||||
|
let second = preview
|
||||||
|
.get_index(scope, 2)
|
||||||
|
.unwrap()
|
||||||
|
.number_value(scope)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(first, 1.0);
|
||||||
|
assert_eq!(second, 3.0);
|
||||||
|
assert_eq!(
|
||||||
|
first,
|
||||||
|
preview
|
||||||
|
.get_index(scope, 1)
|
||||||
|
.unwrap()
|
||||||
|
.number_value(scope)
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
second,
|
||||||
|
preview
|
||||||
|
.get_index(scope, 3)
|
||||||
|
.unwrap()
|
||||||
|
.number_value(scope)
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_prototype_api() {
|
fn test_prototype_api() {
|
||||||
let _setup_guard = setup::parallel_test();
|
let _setup_guard = setup::parallel_test();
|
||||||
|
|
Loading…
Reference in a new issue