Skip to content

Commit

Permalink
Js typed array methods (#3481)
Browse files Browse the repository at this point in the history
* implemented jstypedarray find_index

* implemented find_last

* implemented find_last_index

* implemented forEach

* implemented includes

* implemented set

* implemented subarray

* implemented get buffer

* implemented constructor

* implemented copyWithin

* implemented toLocaleString

* implemented toStringTag

* make linter happy

* doc test for buffer and update example

* rename foreach to for_each

* format

---------

Co-authored-by: Angelo Ceccato <angeloceccato@IT-mac-cean-669341865.local>
Co-authored-by: Angelo Ceccato <angeloceccato@IT-mac-cean-669341865-old.local>
  • Loading branch information
3 people authored Feb 24, 2024
1 parent c94e10d commit 14b34fe
Show file tree
Hide file tree
Showing 3 changed files with 683 additions and 9 deletions.
20 changes: 14 additions & 6 deletions core/engine/src/builtins/typed_array/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl IntrinsicObject for BuiltinTypedArray {
.method(Self::find_index, js_string!("findIndex"), 1)
.method(Self::find_last, js_string!("findLast"), 1)
.method(Self::find_last_index, js_string!("findLastIndex"), 1)
.method(Self::foreach, js_string!("forEach"), 1)
.method(Self::for_each, js_string!("forEach"), 1)
.method(Self::includes, js_string!("includes"), 1)
.method(Self::index_of, js_string!("indexOf"), 1)
.method(Self::join, js_string!("join"), 1)
Expand Down Expand Up @@ -402,7 +402,7 @@ impl BuiltinTypedArray {
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-get-%typedarray%.prototype.buffer
fn buffer(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
pub(crate) fn buffer(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
// 1. Let O be the this value.
// 2. Perform ? RequireInternalSlot(O, [[TypedArrayName]]).
// 3. Assert: O has a [[ViewedArrayBuffer]] internal slot.
Expand Down Expand Up @@ -488,7 +488,11 @@ impl BuiltinTypedArray {
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-%typedarray%.prototype.copywithin
fn copy_within(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
pub(crate) fn copy_within(
this: &JsValue,
args: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
// 1. Let O be the this value.
// 2. Let taRecord be ? ValidateTypedArray(O, seq-cst).
let (ta, buf_len) = TypedArray::validate(this, Ordering::SeqCst)?;
Expand Down Expand Up @@ -994,7 +998,7 @@ impl BuiltinTypedArray {
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-%typedarray%.prototype.foreach
pub(crate) fn foreach(
pub(crate) fn for_each(
this: &JsValue,
args: &[JsValue],
context: &mut Context,
Expand Down Expand Up @@ -2432,7 +2436,7 @@ impl BuiltinTypedArray {
///
/// [spec]: https://tc39.es/ecma262/#sec-%typedarray%.prototype.tolocalestring
/// [spec-402]: https://402.ecma-international.org/10.0/#sup-array.prototype.tolocalestring
fn to_locale_string(
pub(crate) fn to_locale_string(
this: &JsValue,
args: &[JsValue],
context: &mut Context,
Expand Down Expand Up @@ -2604,7 +2608,11 @@ impl BuiltinTypedArray {
///
/// [spec]: https://tc39.es/ecma262/#sec-get-%typedarray%.prototype-@@tostringtag
#[allow(clippy::unnecessary_wraps)]
fn to_string_tag(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> {
pub(crate) fn to_string_tag(
this: &JsValue,
_: &[JsValue],
_: &mut Context,
) -> JsResult<JsValue> {
// 1. Let O be the this value.
// 2. If Type(O) is not Object, return undefined.
// 3. If O does not have a [[TypedArrayName]] internal slot, return undefined.
Expand Down
Loading

0 comments on commit 14b34fe

Please sign in to comment.