(
this: This<Object<'js>>,
ctx: Ctx<'js>,
string: String,
args: Rest<Value<'js>>,
)
| 392 | } |
| 393 | |
| 394 | fn write<'js>( |
| 395 | this: This<Object<'js>>, |
| 396 | ctx: Ctx<'js>, |
| 397 | string: String, |
| 398 | args: Rest<Value<'js>>, |
| 399 | ) -> Result<usize> { |
| 400 | let (offset, length, encoding) = get_write_parameters(&args, this.0.len())?; |
| 401 | |
| 402 | let target = ObjectBytes::from(&ctx, this.0.as_inner())?; |
| 403 | |
| 404 | let mut writable_length = 0; |
| 405 | |
| 406 | if let Some((array_buffer, _, _)) = target.get_array_buffer()? { |
| 407 | let raw = array_buffer |
| 408 | .as_raw() |
| 409 | .ok_or(ERROR_MSG_ARRAY_BUFFER_DETACHED) |
| 410 | .or_throw(&ctx)?; |
| 411 | |
| 412 | let target_bytes = unsafe { slice::from_raw_parts_mut(raw.ptr.as_ptr(), raw.len) }; |
| 413 | |
| 414 | let encoder = Encoder::from_str(&encoding).or_throw(&ctx)?; |
| 415 | |
| 416 | if encoder.as_label() == "utf-8" { |
| 417 | let (source_slice, valid_length) = safe_byte_slice(&string, length.min(string.len())); |
| 418 | writable_length = valid_length; |
| 419 | target_bytes[offset..offset + writable_length].copy_from_slice(source_slice); |
| 420 | } else { |
| 421 | let decode_bytes = encoder.decode_from_string(string).or_throw(&ctx)?; |
| 422 | writable_length = length.min(decode_bytes.len()); |
| 423 | target_bytes[offset..offset + writable_length] |
| 424 | .copy_from_slice(&decode_bytes[..writable_length]); |
| 425 | }; |
| 426 | } |
| 427 | |
| 428 | Ok(writable_length) |
| 429 | } |
| 430 | |
| 431 | fn get_write_parameters(args: &Rest<Value<'_>>, len: usize) -> Result<(usize, usize, String)> { |
| 432 | let mut offset = 0; |
no test coverage detected