0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-01-13 09:33:02 -05:00

feat: Add bindings for "v8::Set" (#1221)

This commit is contained in:
Giovanny Gutiérrez 2023-04-27 17:38:16 -05:00 committed by GitHub
parent 1ddc7aad87
commit fb20194130
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 145 additions and 0 deletions

View file

@ -1502,6 +1502,38 @@ const v8::Array* v8__Map__As__Array(const v8::Map& self) {
return local_to_ptr(self.AsArray());
}
const v8::Set* v8__Set__New(v8::Isolate* isolate) {
return local_to_ptr(v8::Set::New(isolate));
}
size_t v8__Set__Size(const v8::Set& self) { return self.Size(); }
void v8__Set__Clear(const v8::Set& self) {
return ptr_to_local(&self)->Clear();
}
v8::Set* v8__Set__Add(const v8::Set& self, const v8::Context& context,
const v8::Value& key) {
return maybe_local_to_ptr(
ptr_to_local(&self)->Add(ptr_to_local(&context), ptr_to_local(&key)));
}
MaybeBool v8__Set__Has(const v8::Set& self, const v8::Context& context,
const v8::Value& key) {
return maybe_to_maybe_bool(
ptr_to_local(&self)->Has(ptr_to_local(&context), ptr_to_local(&key)));
}
MaybeBool v8__Set__Delete(const v8::Set& self, const v8::Context& context,
const v8::Value& key) {
return maybe_to_maybe_bool(
ptr_to_local(&self)->Delete(ptr_to_local(&context), ptr_to_local(&key)));
}
const v8::Array* v8__Set__As__Array(const v8::Set& self) {
return local_to_ptr(self.AsArray());
}
const v8::Number* v8__Number__New(v8::Isolate* isolate, double value) {
return *v8::Number::New(isolate, value);
}

View file

@ -19,6 +19,7 @@ use crate::Private;
use crate::PropertyAttribute;
use crate::PropertyDescriptor;
use crate::PropertyFilter;
use crate::Set;
use crate::String;
use crate::Value;
use std::convert::TryFrom;
@ -214,6 +215,25 @@ extern "C" {
) -> MaybeBool;
fn v8__Map__Size(map: *const Map) -> usize;
fn v8__Map__As__Array(this: *const Map) -> *const Array;
fn v8__Set__New(isolate: *mut Isolate) -> *const Set;
fn v8__Set__Clear(this: *const Set);
fn v8__Set__Add(
this: *const Set,
context: *const Context,
key: *const Value,
) -> *const Set;
fn v8__Set__Has(
this: *const Set,
context: *const Context,
key: *const Value,
) -> MaybeBool;
fn v8__Set__Delete(
this: *const Set,
context: *const Context,
key: *const Value,
) -> MaybeBool;
fn v8__Set__Size(map: *const Set) -> usize;
fn v8__Set__As__Array(this: *const Set) -> *const Array;
}
impl Object {
@ -850,3 +870,58 @@ impl Map {
unsafe { scope.cast_local(|_| v8__Map__As__Array(self)) }.unwrap()
}
}
impl Set {
#[inline(always)]
pub fn new<'s>(scope: &mut HandleScope<'s>) -> Local<'s, Set> {
unsafe { scope.cast_local(|sd| v8__Set__New(sd.get_isolate_ptr())) }
.unwrap()
}
#[inline(always)]
pub fn size(&self) -> usize {
unsafe { v8__Set__Size(self) }
}
#[inline(always)]
pub fn clear(&self) {
unsafe { v8__Set__Clear(self) }
}
#[inline(always)]
pub fn add<'s>(
&self,
scope: &mut HandleScope<'s>,
key: Local<Value>,
) -> Option<Local<'s, Set>> {
unsafe {
scope.cast_local(|sd| v8__Set__Add(self, sd.get_current_context(), &*key))
}
}
#[inline(always)]
pub fn has(
&self,
scope: &mut HandleScope,
key: Local<Value>,
) -> Option<bool> {
unsafe { v8__Set__Has(self, &*scope.get_current_context(), &*key) }.into()
}
#[inline(always)]
pub fn delete(
&self,
scope: &mut HandleScope,
key: Local<Value>,
) -> Option<bool> {
unsafe { v8__Set__Delete(self, &*scope.get_current_context(), &*key) }
.into()
}
/// Returns an array of length size() * 2, where index N is the Nth key and
/// index N + 1 is the Nth value.
#[inline(always)]
pub fn as_array<'s>(&self, scope: &mut HandleScope<'s>) -> Local<'s, Array> {
unsafe { scope.cast_local(|_| v8__Set__As__Array(self)) }.unwrap()
}
}

View file

@ -2350,6 +2350,44 @@ fn map() {
}
}
#[test]
fn set() {
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 set = v8::Set::new(scope);
assert_eq!(set.size(), 0);
{
let key = v8::Object::new(scope).into();
assert_eq!(set.has(scope, key), Some(false));
assert_eq!(set.add(scope, key), Some(set));
assert_eq!(set.has(scope, key), Some(true));
assert_eq!(set.size(), 1);
}
set.clear();
assert_eq!(set.size(), 0);
{
let key = v8::String::new(scope, "key").unwrap().into();
assert_eq!(set.delete(scope, key), Some(false));
set.add(scope, key);
assert_eq!(set.size(), 1);
assert_eq!(set.delete(scope, key), Some(true));
assert_eq!(set.size(), 0);
}
}
}
#[test]
fn array() {
let _setup_guard = setup::parallel_test();