pub struct JSTypedArray { /* private fields */ }Expand description
A JavaScript Typed Array.
A Typed Array is a special JavaScript object that represent a family of buffer views. Learn more by reading the documentation.
Implementations§
Source§impl JSTypedArray
impl JSTypedArray
Sourcepub fn ty(&self) -> Result<JSTypedArrayType, JSException>
pub fn ty(&self) -> Result<JSTypedArrayType, JSException>
Returns a value of type JSTypedArrayType that identifies value’s
Typed Array type, or JSTypedArrayType::None if the value is not a Typed Array
object.
let ctx = JSContext::default();
let array = evaluate_script(&ctx, "new Uint8Array([1, 2, 3, 4, 5])", None, "foo.js", 1)
.unwrap()
.as_typed_array()
.unwrap();
assert_eq!(array.ty().unwrap(), JSTypedArrayType::Uint8Array);Sourcepub fn len(&self) -> Result<usize, JSException>
pub fn len(&self) -> Result<usize, JSException>
Returns the length of the Typed Array.
let ctx = JSContext::default();
let array = evaluate_script(&ctx, "new Uint8Array([1, 2, 3, 4, 5])", None, "foo.js", 1)
.unwrap()
.as_typed_array()
.unwrap();
assert_eq!(array.len().unwrap(), 5);Sourcepub fn byte_offset(&self) -> Result<usize, JSException>
pub fn byte_offset(&self) -> Result<usize, JSException>
Returns the byte offset of the Typed Array.
The byte offset is the offset used when a Typed Array is created from another Typed Array, it’s a “subview” that can start from an offset, up to a certain length, which is the byte length.
let ctx = JSContext::default();
let array = evaluate_script(&ctx, "const array = new Uint8Array([1, 2, 3, 4, 5]); new Uint8Array(array.buffer, 3)", None, "foo.js", 1)
.unwrap()
.as_typed_array()
.unwrap();
assert_eq!(array.byte_offset().unwrap(), 3);Sourcepub fn byte_length(&self) -> Result<usize, JSException>
pub fn byte_length(&self) -> Result<usize, JSException>
Returns the byte length of the Typed Array.
The byte length is the length used when a Typed Array is created from another Typed Array, it’s a “subview” that can start from an offset, up to a certain length, which is the byte length.
let ctx = JSContext::default();
let array = evaluate_script(&ctx, "const array = new Uint8Array([1, 2, 3, 4, 5]); new Uint8Array(array.buffer, 1, 2)", None, "foo.js", 1)
.unwrap()
.as_typed_array()
.unwrap();
assert_eq!(array.byte_length().unwrap(), 2);Sourcepub unsafe fn as_mut_slice(&mut self) -> Result<&mut [u8], JSException>
pub unsafe fn as_mut_slice(&mut self) -> Result<&mut [u8], JSException>
Returns a mutable slice of the underlying buffer represented by the Typed Array.
§Safety
The pointer of the slice returned by this function is temporary and is not guaranteed to remain valid across JavaScriptCore API calls.
§Example
let ctx = JSContext::default();
/// Create a Typed Array from the Rust API.
let mut bytes = vec![1u8, 2, 3, 4, 5];
let array_as_value =
unsafe { JSValue::new_typed_array_with_bytes(&ctx, bytes.as_mut_slice()) }.unwrap();
let mut array = array_as_value.as_typed_array().unwrap();
ctx.global_object().unwrap().set_property("array", array_as_value).unwrap();
/// Create a sub-Typed Array from `array` in JavaScript.
let mut sub_array = evaluate_script(
&ctx,
"new Uint8Array(array.buffer, 1, 3)",
None,
"foo.js",
1,
)
.unwrap()
.as_typed_array()
.unwrap();
let sub_slice = unsafe { sub_array.as_mut_slice() }.unwrap();
// Items are untouched.
assert_eq!(sub_slice, &[2, 3, 4]);
assert_eq!(bytes, &[1, 2, 3, 4, 5]);
// Now let's mutate them.
sub_slice[0] = 12;
sub_slice[2] = 14;
// See, they are mutated.
assert_eq!(sub_slice, &[12, 3, 14]);
assert_eq!(bytes, &[1, 12, 3, 14, 5]);Sourcepub fn to_vec(&self) -> Result<Vec<u8>, JSException>
pub fn to_vec(&self) -> Result<Vec<u8>, JSException>
Returns a Vec (so a copy) of the underlying buffer represented by the
Typed Array.
let ctx = JSContext::default();
let mut array = evaluate_script(&ctx, "const array = new Uint8Array([1, 2, 3, 4, 5]); new Uint8Array(array.buffer, 1, 3)", None, "foo.js", 1)
.unwrap()
.as_typed_array()
.unwrap();
assert_eq!(array.to_vec().unwrap(), &[2, 3, 4]);