-
-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
204 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
use crate::{ | ||
builtins::typed_array::TypedArray, | ||
object::{JsArray, JsObject, JsObjectType}, | ||
Context, JsResult, JsValue, | ||
}; | ||
use boa_gc::{Finalize, Trace}; | ||
use std::ops::Deref; | ||
|
||
/// JavaScript `TypedArray` rust object. | ||
#[derive(Debug, Clone, Trace, Finalize)] | ||
pub struct JsTypedArray { | ||
inner: JsObject, | ||
} | ||
|
||
impl JsTypedArray { | ||
/// Get the length of the array. | ||
/// | ||
/// Same a `array.length` in JavaScript. | ||
#[inline] | ||
pub fn length(&self, context: &mut Context) -> JsResult<usize> { | ||
self.inner.length_of_array_like(context) | ||
} | ||
|
||
/// Check if the array is empty, i.e. the `length` is zero. | ||
#[inline] | ||
pub fn is_empty(&self, context: &mut Context) -> JsResult<bool> { | ||
self.inner.length_of_array_like(context).map(|len| len == 0) | ||
} | ||
|
||
#[inline] | ||
pub fn at<T>(&self, index: T, context: &mut Context) -> JsResult<JsValue> | ||
where | ||
T: Into<i64>, | ||
{ | ||
TypedArray::at(&self.inner.clone().into(), &[index.into().into()], context) | ||
} | ||
} | ||
|
||
impl From<JsTypedArray> for JsObject { | ||
#[inline] | ||
fn from(o: JsTypedArray) -> Self { | ||
o.inner.clone() | ||
} | ||
} | ||
|
||
impl From<JsTypedArray> for JsValue { | ||
#[inline] | ||
fn from(o: JsTypedArray) -> Self { | ||
o.inner.clone().into() | ||
} | ||
} | ||
|
||
impl Deref for JsTypedArray { | ||
type Target = JsObject; | ||
|
||
#[inline] | ||
fn deref(&self) -> &Self::Target { | ||
&self.inner | ||
} | ||
} | ||
|
||
impl JsObjectType for JsTypedArray {} | ||
|
||
macro_rules! JsTypedArrayType { | ||
($name:ident, $constructor_function:ident, $constructor_object:ident, $element:ty) => { | ||
#[doc = concat!("JavaScript `", stringify!($constructor_function), "` rust object.")] | ||
#[derive(Debug, Clone, Trace, Finalize)] | ||
pub struct $name { | ||
inner: JsTypedArray, | ||
} | ||
|
||
impl $name { | ||
#[inline] | ||
pub fn from_iter<I>(elements: I, context: &mut Context) -> JsResult<Self> | ||
where | ||
I: IntoIterator<Item = $element>, | ||
{ | ||
// TODO: This is pretty inefficient, find a better way of creating typed arrays from iterators | ||
let array = JsArray::from_iter(elements.into_iter().map(JsValue::new), context); | ||
let new_target = context | ||
.intrinsics() | ||
.constructors() | ||
.$constructor_object() | ||
.constructor() | ||
.into(); | ||
let object = crate::builtins::typed_array::$constructor_function::constructor( | ||
&new_target, | ||
&[array.into()], | ||
context, | ||
)? | ||
.as_object() | ||
.expect("object") | ||
.clone(); | ||
|
||
Ok(Self { | ||
inner: JsTypedArray { inner: object }, | ||
}) | ||
} | ||
} | ||
|
||
impl From<$name> for JsObject { | ||
#[inline] | ||
fn from(o: $name) -> Self { | ||
o.inner.inner.clone() | ||
} | ||
} | ||
|
||
impl From<$name> for JsValue { | ||
#[inline] | ||
fn from(o: $name) -> Self { | ||
o.inner.inner.clone().into() | ||
} | ||
} | ||
|
||
impl Deref for $name { | ||
type Target = JsTypedArray; | ||
|
||
#[inline] | ||
fn deref(&self) -> &Self::Target { | ||
&self.inner | ||
} | ||
} | ||
}; | ||
} | ||
|
||
JsTypedArrayType!(JsUint8Array, Uint8Array, typed_uint8_array, u8); | ||
JsTypedArrayType!(JsInt8Array, Int8Array, typed_int8_array, i8); | ||
JsTypedArrayType!(JsUint16Array, Uint16Array, typed_uint16_array, u16); | ||
JsTypedArrayType!(JsInt16Array, Int16Array, typed_int16_array, i16); | ||
JsTypedArrayType!(JsUint32Array, Uint32Array, typed_uint32_array, u32); | ||
JsTypedArrayType!(JsInt32Array, Int32Array, typed_int32_array, i32); | ||
JsTypedArrayType!(JsFloat32Array, Float32Array, typed_float32_array, f32); | ||
JsTypedArrayType!(JsFloat64Array, Float64Array, typed_float64_array, f64); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// This example shows how to manipulate a Javascript array using Rust code. | ||
|
||
use boa_engine::{object::JsUint8Array, Context, JsResult, JsValue, property::Attribute}; | ||
|
||
fn main() -> JsResult<()> { | ||
// We create a new `Context` to create a new Javascript executor. | ||
let context = &mut Context::default(); | ||
|
||
let data: Vec<u8> = (0..=255).collect(); | ||
|
||
let array = JsUint8Array::from_iter(data, context)?; | ||
|
||
assert_eq!(array.get(0, context)?, JsValue::new(0)); | ||
|
||
for i in 0..=255 { | ||
assert_eq!(array.at(i, context)?, JsValue::new(i)); | ||
} | ||
|
||
context.register_global_property( | ||
"myUint8Array", | ||
array, | ||
Attribute::WRITABLE | Attribute::ENUMERABLE | Attribute::CONFIGURABLE, | ||
); | ||
|
||
Ok(()) | ||
} |