0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2025-02-01 20:24:56 -05:00

Add PrimitiveArray and ScriptOrModule (#105)

This commit is contained in:
Ry Dahl 2019-12-21 06:38:26 -05:00 committed by GitHub
parent c361ee2faf
commit 99baef0169
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 204 additions and 0 deletions

View file

@ -151,6 +151,10 @@ bool v8__Value__IsNullOrUndefined(const v8::Value& self) {
return self.IsNullOrUndefined();
}
bool v8__Value__IsString(const v8::Value& self) { return self.IsString(); }
bool v8__Value__IsNumber(const v8::Value& self) { return self.IsNumber(); }
v8::Primitive* v8__Null(v8::Isolate* isolate) {
return local_to_ptr(v8::Null(isolate));
}
@ -167,6 +171,24 @@ v8::Boolean* v8__False(v8::Isolate* isolate) {
return local_to_ptr(v8::False(isolate));
}
v8::PrimitiveArray* v8__PrimitiveArray__New(v8::Isolate* isolate, int length) {
return local_to_ptr(v8::PrimitiveArray::New(isolate, length));
}
int v8__PrimitiveArray__Length(v8::PrimitiveArray& self) {
return self.Length();
}
void v8__PrimitiveArray__Set(v8::PrimitiveArray& self, v8::Isolate* isolate,
int index, v8::Local<v8::Primitive> item) {
self.Set(isolate, index, item);
}
v8::Primitive* v8__PrimitiveArray__Get(v8::PrimitiveArray& self,
v8::Isolate* isolate, int index) {
return local_to_ptr(self.Get(isolate, index));
}
v8::String* v8__String__NewFromUtf8(v8::Isolate* isolate, const char* data,
v8::NewStringType type, int length) {
return maybe_local_to_ptr(
@ -402,6 +424,15 @@ void v8__ScriptOrigin__CONSTRUCT(
ptr_to_local(is_wasm), ptr_to_local(is_module));
}
v8::Value* v8__ScriptOrModule__GetResourceName(v8::ScriptOrModule& self) {
return local_to_ptr(self.GetResourceName());
}
v8::PrimitiveArray* v8__ScriptOrModule__GetHostDefinedOptions(
v8::ScriptOrModule& self) {
return local_to_ptr(self.GetHostDefinedOptions());
}
v8::Value* v8__JSON__Parse(v8::Local<v8::Context> context,
v8::Local<v8::String> json_string) {
return maybe_local_to_ptr(v8::JSON::Parse(context, json_string));

View file

@ -19,10 +19,12 @@ mod locker;
mod module;
mod number;
mod object;
mod primitive_array;
mod primitives;
mod promise;
mod property;
mod script;
mod script_or_module;
mod string;
mod support;
mod try_catch;
@ -51,6 +53,7 @@ pub use locker::Locker;
pub use module::Module;
pub use number::{Integer, Number};
pub use object::Object;
pub use primitive_array::PrimitiveArray;
pub use primitives::*;
pub use promise::{
Promise, PromiseRejectEvent, PromiseRejectMessage, PromiseResolver,
@ -58,6 +61,7 @@ pub use promise::{
};
pub use property::PropertyCallbackInfo;
pub use script::{Script, ScriptOrigin};
pub use script_or_module::ScriptOrModule;
pub use string::NewStringType;
pub use string::String;
pub use try_catch::{TryCatch, TryCatchScope};

75
src/primitive_array.rs Normal file
View file

@ -0,0 +1,75 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::support::int;
use crate::support::Opaque;
use crate::HandleScope;
use crate::Isolate;
use crate::Local;
use crate::Primitive;
extern "C" {
fn v8__PrimitiveArray__New(
isolate: *mut Isolate,
length: int,
) -> *mut PrimitiveArray;
fn v8__PrimitiveArray__Length(this: &PrimitiveArray) -> int;
fn v8__PrimitiveArray__Set(
this: &PrimitiveArray,
isolate: *mut Isolate,
index: int,
item: &Primitive,
);
fn v8__PrimitiveArray__Get(
this: &PrimitiveArray,
isolate: *mut Isolate,
index: int,
) -> *mut Primitive;
}
/// An array to hold Primitive values. This is used by the embedder to pass host
/// defined options to the ScriptOptions during compilation.
///
/// This is passed back to the embedder as part of
/// HostImportModuleDynamicallyCallback for module loading.
#[repr(C)]
pub struct PrimitiveArray(Opaque);
impl PrimitiveArray {
pub fn new<'sc>(
scope: &mut HandleScope<'sc>,
length: usize,
) -> Local<'sc, PrimitiveArray> {
unsafe {
let ptr = v8__PrimitiveArray__New(scope.as_mut(), length as int);
Local::from_raw(ptr).unwrap()
}
}
pub fn length(&self) -> usize {
unsafe { v8__PrimitiveArray__Length(self) as usize }
}
pub fn set<'sc>(
&self,
scope: &mut HandleScope<'sc>,
index: usize,
item: Local<'_, Primitive>,
) {
unsafe {
v8__PrimitiveArray__Set(self, scope.as_mut(), index as int, &item)
}
}
pub fn get<'sc>(
&self,
scope: &mut HandleScope<'sc>,
index: usize,
) -> Local<'sc, Primitive> {
unsafe {
let ptr = v8__PrimitiveArray__Get(self, scope.as_mut(), index as int);
Local::from_raw(ptr).unwrap()
}
}
}

40
src/script_or_module.rs Normal file
View file

@ -0,0 +1,40 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::support::Opaque;
use crate::Local;
use crate::PrimitiveArray;
use crate::Value;
extern "C" {
fn v8__ScriptOrModule__GetResourceName(this: &ScriptOrModule) -> *mut Value;
fn v8__ScriptOrModule__GetHostDefinedOptions(
this: &ScriptOrModule,
) -> *mut PrimitiveArray;
}
/// A container type that holds relevant metadata for module loading.
///
/// This is passed back to the embedder as part of
/// HostImportModuleDynamicallyCallback for module loading.
#[repr(C)]
pub struct ScriptOrModule(Opaque);
impl ScriptOrModule {
/// The name that was passed by the embedder as ResourceName to the
/// ScriptOrigin. This can be either a v8::String or v8::Undefined.
pub fn get_resource_name(&self) -> Local<'_, Value> {
unsafe {
let ptr = v8__ScriptOrModule__GetResourceName(self);
Local::from_raw(ptr).unwrap()
}
}
/// The options that were passed by the embedder as HostDefinedOptions to the
/// ScriptOrigin.
pub fn get_host_defined_options(&self) -> Local<'_, PrimitiveArray> {
unsafe {
let ptr = v8__ScriptOrModule__GetHostDefinedOptions(self);
Local::from_raw(ptr).unwrap()
}
}
}

View file

@ -4,6 +4,8 @@ extern "C" {
fn v8__Value__IsUndefined(this: &Value) -> bool;
fn v8__Value__IsNull(this: &Value) -> bool;
fn v8__Value__IsNullOrUndefined(this: &Value) -> bool;
fn v8__Value__IsString(this: &Value) -> bool;
fn v8__Value__IsNumber(this: &Value) -> bool;
}
/// The superclass of all JavaScript values and objects.
@ -26,4 +28,15 @@ impl Value {
pub fn is_null_or_undefined(&self) -> bool {
unsafe { v8__Value__IsNullOrUndefined(self) }
}
/// Returns true if this value is an instance of the String type.
/// See ECMA-262 8.4.
pub fn is_string(&self) -> bool {
unsafe { v8__Value__IsString(self) }
}
/// Returns true if this value is a number.
pub fn is_number(&self) -> bool {
unsafe { v8__Value__IsNumber(self) }
}
}

View file

@ -685,3 +685,44 @@ fn script_compiler_source() {
isolate.exit();
drop(g);
}
#[test]
fn primitive_array() {
let g = setup();
let mut params = v8::Isolate::create_params();
params.set_array_buffer_allocator(
v8::array_buffer::Allocator::new_default_allocator(),
);
let mut isolate = v8::Isolate::new(params);
isolate.enter();
let mut locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| {
let mut context = v8::Context::new(scope);
context.enter();
let length = 3;
let array = v8::PrimitiveArray::new(scope, length);
assert_eq!(length, array.length());
for i in 0..length {
let item = array.get(scope, i);
assert!(item.is_undefined());
}
let string = v8_str(scope, "test");
array.set(scope, 1, cast(string));
assert!(array.get(scope, 0).is_undefined());
assert!(array.get(scope, 1).is_string());
let num = v8::Number::new(scope, 0.42);
array.set(scope, 2, cast(num));
assert!(array.get(scope, 0).is_undefined());
assert!(array.get(scope, 1).is_string());
assert!(array.get(scope, 2).is_number());
context.exit();
});
drop(locker);
isolate.exit();
drop(g);
}