1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
//! A Rust API wrapper for Boa's `DataView` Builtin ECMAScript Object
use crate::{
builtins::{array_buffer::BufferObject, DataView},
object::{JsArrayBuffer, JsObject, JsObjectType},
value::TryFromJs,
Context, JsNativeError, JsResult, JsValue,
};
use boa_gc::{Finalize, Trace};
use std::ops::Deref;
/// `JsDataView` provides a wrapper for Boa's implementation of the ECMAScript `DataView` object
///
/// # Examples
/// ```
/// # use boa_engine::{
/// # object::builtins::{JsArrayBuffer, JsDataView},
/// # Context, JsValue, JsResult,
/// # };
/// # fn main() -> JsResult<()> {
/// // Create a new context and ArrayBuffer
/// let context = &mut Context::default();
/// let array_buffer = JsArrayBuffer::new(4, context)?;
///
/// // Create a new Dataview from pre-existing ArrayBuffer
/// let data_view =
/// JsDataView::from_js_array_buffer(array_buffer, None, None, context)?;
///
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, Trace, Finalize)]
#[boa_gc(unsafe_no_drop)]
pub struct JsDataView {
inner: JsObject<DataView>,
}
impl From<JsDataView> for JsObject<DataView> {
#[inline]
fn from(value: JsDataView) -> Self {
value.inner
}
}
impl From<JsObject<DataView>> for JsDataView {
#[inline]
fn from(value: JsObject<DataView>) -> Self {
Self { inner: value }
}
}
impl JsDataView {
/// Create a new `JsDataView` object from an existing `JsArrayBuffer`.
pub fn from_js_array_buffer(
buffer: JsArrayBuffer,
offset: Option<u64>,
byte_len: Option<u64>,
context: &mut Context,
) -> JsResult<Self> {
let offset = offset.unwrap_or_default();
let (buf_byte_len, is_fixed_len) = {
let buffer = buffer.borrow();
let buffer = &buffer.data;
// 4. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
let Some(slice) = buffer.bytes() else {
return Err(JsNativeError::typ()
.with_message("ArrayBuffer is detached")
.into());
};
// 5. Let bufferByteLength be ArrayBufferByteLength(buffer, seq-cst).
let buf_len = slice.len() as u64;
// 6. If offset > bufferByteLength, throw a RangeError exception.
if offset > buf_len {
return Err(JsNativeError::range()
.with_message("Start offset is outside the bounds of the buffer")
.into());
}
// 7. Let bufferIsFixedLength be IsFixedLengthArrayBuffer(buffer).
(buf_len, buffer.is_fixed_len())
};
// 8. If byteLength is undefined, then
let view_byte_len = if let Some(byte_len) = byte_len {
// 9. Else,
// a. Let viewByteLength be ? ToIndex(byteLength).
// b. If offset + viewByteLength > bufferByteLength, throw a RangeError exception.
if offset + byte_len > buf_byte_len {
return Err(JsNativeError::range()
.with_message("Invalid data view length")
.into());
}
Some(byte_len)
} else {
// a. If bufferIsFixedLength is true, then
// i. Let viewByteLength be bufferByteLength - offset.
// b. Else,
// i. Let viewByteLength be auto.
is_fixed_len.then_some(buf_byte_len - offset)
};
// 10. Let O be ? OrdinaryCreateFromConstructor(NewTarget, "%DataView.prototype%",
// « [[DataView]], [[ViewedArrayBuffer]], [[ByteLength]], [[ByteOffset]] »).
let prototype = context.intrinsics().constructors().data_view().prototype();
// 11. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
// 12. Set bufferByteLength to ArrayBufferByteLength(buffer, seq-cst).
let Some(buf_byte_len) = buffer.borrow().data.bytes().map(|s| s.len() as u64) else {
return Err(JsNativeError::typ()
.with_message("ArrayBuffer is detached")
.into());
};
// 13. If offset > bufferByteLength, throw a RangeError exception.
if offset > buf_byte_len {
return Err(JsNativeError::range()
.with_message("DataView offset outside of buffer array bounds")
.into());
}
// 14. If byteLength is not undefined, then
if let Some(view_byte_len) = view_byte_len.filter(|_| byte_len.is_some()) {
// a. If offset + viewByteLength > bufferByteLength, throw a RangeError exception.
if offset + view_byte_len > buf_byte_len {
return Err(JsNativeError::range()
.with_message("DataView offset outside of buffer array bounds")
.into());
}
}
let obj = JsObject::new(
context.root_shape(),
prototype,
DataView {
// 15. Set O.[[ViewedArrayBuffer]] to buffer.
viewed_array_buffer: BufferObject::Buffer(buffer.into()),
// 16. Set O.[[ByteLength]] to viewByteLength.
byte_length: view_byte_len,
// 17. Set O.[[ByteOffset]] to offset.
byte_offset: offset,
},
);
// 18. Return O.
Ok(Self { inner: obj })
}
/// Create a new `JsDataView` object from an existing object.
#[inline]
pub fn from_object(object: JsObject) -> JsResult<Self> {
object
.downcast::<DataView>()
.map(|inner| Self { inner })
.map_err(|_| {
JsNativeError::typ()
.with_message("object is not a DataView")
.into()
})
}
/// Returns the `viewed_array_buffer` field for [`JsDataView`]
#[inline]
pub fn buffer(&self, context: &mut Context) -> JsResult<JsValue> {
DataView::get_buffer(&self.inner.clone().upcast().into(), &[], context)
}
/// Returns the `byte_length` property of [`JsDataView`] as a u64 integer
#[inline]
pub fn byte_length(&self, context: &mut Context) -> JsResult<u64> {
DataView::get_byte_length(&self.inner.clone().upcast().into(), &[], context)
.map(|v| v.as_number().expect("value should be a number") as u64)
}
/// Returns the `byte_offset` field property of [`JsDataView`] as a u64 integer
#[inline]
pub fn byte_offset(&self, context: &mut Context) -> JsResult<u64> {
DataView::get_byte_offset(&self.inner.clone().upcast().into(), &[], context)
.map(|v| v.as_number().expect("byte_offset value must be a number") as u64)
}
/// Returns a signed 64-bit integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn get_big_int64(
&self,
byte_offset: usize,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<i64> {
DataView::get_big_int64(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), is_little_endian.into()],
context,
)
.map(|v| v.as_number().expect("value must be a number") as i64)
}
/// Returns an unsigned 64-bit integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn get_big_uint64(
&self,
byte_offset: usize,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<u64> {
DataView::get_big_uint64(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), is_little_endian.into()],
context,
)
.map(|v| v.as_number().expect("value must be a number") as u64)
}
/// Returns a signed 32-bit float integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn get_float32(
&self,
byte_offset: usize,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<f32> {
DataView::get_float32(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), is_little_endian.into()],
context,
)
.map(|v| v.as_number().expect("value must be a number") as f32)
}
/// Returns a signed 64-bit float integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn get_float64(
&self,
byte_offset: usize,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<f64> {
DataView::get_float64(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), is_little_endian.into()],
context,
)
.map(|v| v.as_number().expect("value must be a number"))
}
/// Returns a signed 8-bit integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn get_int8(
&self,
byte_offset: usize,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<i8> {
DataView::get_int8(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), is_little_endian.into()],
context,
)
.map(|v| v.as_number().expect("value must be a number") as i8)
}
/// Returns a signed 16-bit integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn get_int16(
&self,
byte_offset: usize,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<i16> {
DataView::get_int16(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), is_little_endian.into()],
context,
)
.map(|v| v.as_number().expect("value must be a number") as i16)
}
/// Returns a signed 32-bit integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn get_int32(
&self,
byte_offset: usize,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<i32> {
DataView::get_int32(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), is_little_endian.into()],
context,
)
.map(|v| v.as_number().expect("value must be a number") as i32)
}
/// Returns an unsigned 8-bit integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn get_uint8(
&self,
byte_offset: usize,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<u8> {
DataView::get_uint8(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), is_little_endian.into()],
context,
)
.map(|v| v.as_number().expect("value must be a number") as u8)
}
/// Returns an unsigned 16-bit integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn get_unit16(
&self,
byte_offset: usize,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<u16> {
DataView::get_uint16(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), is_little_endian.into()],
context,
)
.map(|v| v.as_number().expect("value must be a number") as u16)
}
/// Returns an unsigned 32-bit integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn get_uint32(
&self,
byte_offset: usize,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<u32> {
DataView::get_uint32(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), is_little_endian.into()],
context,
)
.map(|v| v.as_number().expect("value must be a number") as u32)
}
/// Sets a signed 64-bit integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn set_big_int64(
&self,
byte_offset: usize,
value: i64,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<JsValue> {
DataView::set_big_int64(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), value.into(), is_little_endian.into()],
context,
)
}
/// Sets an unsigned 64-bit integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn set_big_uint64(
&self,
byte_offset: usize,
value: u64,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<JsValue> {
DataView::set_big_uint64(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), value.into(), is_little_endian.into()],
context,
)
}
/// Sets a signed 32-bit integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn set_float32(
&self,
byte_offset: usize,
value: f32,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<JsValue> {
DataView::set_float32(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), value.into(), is_little_endian.into()],
context,
)
}
/// Sets a signed 64-bit integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn set_float64(
&self,
byte_offset: usize,
value: f64,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<JsValue> {
DataView::set_float64(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), value.into(), is_little_endian.into()],
context,
)
}
/// Sets a signed 8-bit integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn set_int8(
&self,
byte_offset: usize,
value: i8,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<JsValue> {
DataView::set_int8(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), value.into(), is_little_endian.into()],
context,
)
}
/// Sets a signed 16-bit integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn set_int16(
&self,
byte_offset: usize,
value: i16,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<JsValue> {
DataView::set_int16(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), value.into(), is_little_endian.into()],
context,
)
}
/// Sets a signed 32-bit integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn set_int32(
&self,
byte_offset: usize,
value: i32,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<JsValue> {
DataView::set_int32(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), value.into(), is_little_endian.into()],
context,
)
}
/// Sets an unsigned 8-bit integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn set_uint8(
&self,
byte_offset: usize,
value: u8,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<JsValue> {
DataView::set_uint8(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), value.into(), is_little_endian.into()],
context,
)
}
/// Sets an unsigned 16-bit integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn set_unit16(
&self,
byte_offset: usize,
value: u16,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<JsValue> {
DataView::set_uint16(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), value.into(), is_little_endian.into()],
context,
)
}
/// Sets an unsigned 32-bit integer at the specified offset from the start of the [`JsDataView`]
#[inline]
pub fn set_unit32(
&self,
byte_offset: usize,
value: u32,
is_little_endian: bool,
context: &mut Context,
) -> JsResult<JsValue> {
DataView::set_uint32(
&self.inner.clone().upcast().into(),
&[byte_offset.into(), value.into(), is_little_endian.into()],
context,
)
}
}
impl From<JsDataView> for JsObject {
#[inline]
fn from(o: JsDataView) -> Self {
o.inner.upcast()
}
}
impl From<JsDataView> for JsValue {
#[inline]
fn from(o: JsDataView) -> Self {
o.inner.upcast().into()
}
}
impl Deref for JsDataView {
type Target = JsObject<DataView>;
#[inline]
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl JsObjectType for JsDataView {}
impl TryFromJs for JsDataView {
fn try_from_js(value: &JsValue, _context: &mut Context) -> JsResult<Self> {
match value {
JsValue::Object(o) => Self::from_object(o.clone()),
_ => Err(JsNativeError::typ()
.with_message("value is not an DataView object")
.into()),
}
}
}