Skip to content

Commit

Permalink
Optimized Array.prototype.{with, toReversed, toSorted}
Browse files Browse the repository at this point in the history
  • Loading branch information
shiroyk committed Aug 6, 2024
1 parent eb01048 commit 14f2d06
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
60 changes: 41 additions & 19 deletions builtin_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -1239,43 +1239,58 @@ func (r *Runtime) arrayproto_with(call FunctionCall) Value {
if actualIndex >= length || actualIndex < 0 {
panic(r.newError(r.getRangeError(), "Invalid index %s", call.Argument(0).String()))
}
a := r.newArrayLength(length)

for k := int64(0); k < length; k++ {
pk := valueInt(k)
var fromValue Value
if k == actualIndex {
fromValue = value
} else {
fromValue = o.self.getIdx(pk, nil)
if src := r.checkStdArrayObj(o); src != nil {
a := make([]Value, 0, length)
for k := int64(0); k < length; k++ {
pk := valueInt(k)
var fromValue Value
if k == actualIndex {
fromValue = value
} else {
fromValue = src.values[pk]
}
a = append(a, fromValue)
}
return r.newArrayValues(a)
} else {
a := r.newArrayLength(length)
for k := int64(0); k < length; k++ {
pk := valueInt(k)
var fromValue Value
if k == actualIndex {
fromValue = value
} else {
fromValue = o.self.getIdx(pk, nil)
}
createDataPropertyOrThrow(a, pk, fromValue)
}
createDataPropertyOrThrow(a, pk, fromValue)
return a
}
return a
}

func (r *Runtime) arrayproto_toReversed(call FunctionCall) Value {
o := call.This.ToObject(r)
length := toLength(o.self.getStr("length", nil))
a := r.newArrayLength(length)

if src := r.checkStdArrayObj(o); src != nil {
a := make([]Value, 0, length)
for k := int64(0); k < length; k++ {
pk := valueInt(k)
from := valueInt(length - k - 1)
fromValue := src.values[from]
createDataPropertyOrThrow(a, pk, fromValue)
a = append(a, fromValue)
}
return r.newArrayValues(a)
} else {
a := r.newArrayLength(length)
for k := int64(0); k < length; k++ {
pk := valueInt(k)
from := valueInt(length - k - 1)
fromValue := o.self.getIdx(from, nil)
createDataPropertyOrThrow(a, pk, fromValue)
}
return a
}

return a
}

func (r *Runtime) arrayproto_toSorted(call FunctionCall) Value {
Expand All @@ -1295,12 +1310,19 @@ func (r *Runtime) arrayproto_toSorted(call FunctionCall) Value {
if length >= math.MaxUint32 {
panic(r.newError(r.getRangeError(), "Invalid array length"))
}
a := make([]Value, 0, length)
var a []Value

for i := int64(0); i < length; i++ {
idx := valueInt(i)
a = append(a, nilSafe(o.self.getIdx(idx, nil)))
if src := r.checkStdArrayObj(o); src != nil {
a = make([]Value, length)
copy(a, src.values)
} else {
a = make([]Value, 0, length)
for i := int64(0); i < length; i++ {
idx := valueInt(i)
a = append(a, nilSafe(o.self.getIdx(idx, nil)))
}
}

ar := r.newArrayValues(a)
ctx := arraySortCtx{
obj: ar.self,
Expand Down
10 changes: 3 additions & 7 deletions builtin_typedarrays.go
Original file line number Diff line number Diff line change
Expand Up @@ -1171,14 +1171,13 @@ func (r *Runtime) typedArrayProto_with(call FunctionCall) Value {
if !ta.isValidIntegerIndex(actualIndex) {
panic(r.newError(r.getRangeError(), "Invalid typed array index"))
}
ta.viewedArrayBuf.ensureNotDetached(true)

// TODO BigInt
// 7. If O.[[ContentType]] is BIGINT, let numericValue be ? ToBigInt(value).
// 8. Else, let numericValue be ? ToNumber(value).
numericValue := call.Argument(1).ToNumber()

a := (r.toConstructor(ta.defaultCtor)([]Value{intToValue(int64(length))}, ta.defaultCtor)).self.(*typedArrayObject)
a := r.typedArrayCreate(ta.defaultCtor, intToValue(int64(length)))
for k := 0; k < length; k++ {
var fromValue Value
if k == actualIndex {
Expand All @@ -1199,8 +1198,7 @@ func (r *Runtime) typedArrayProto_toReversed(call FunctionCall) Value {
}
length := ta.length

a := (r.toConstructor(ta.defaultCtor)([]Value{intToValue(int64(length))}, ta.defaultCtor)).self.(*typedArrayObject)
ta.viewedArrayBuf.ensureNotDetached(true)
a := r.typedArrayCreate(ta.defaultCtor, intToValue(int64(length)))

for k := 0; k < length; k++ {
from := length - k - 1
Expand Down Expand Up @@ -1231,9 +1229,7 @@ func (r *Runtime) typedArrayProto_toSorted(call FunctionCall) Value {

length := ta.length

a := (r.toConstructor(ta.defaultCtor)([]Value{intToValue(int64(length))}, ta.defaultCtor)).self.(*typedArrayObject)
ta.viewedArrayBuf.ensureNotDetached(true)

a := r.typedArrayCreate(ta.defaultCtor, intToValue(int64(length)))
copy(a.viewedArrayBuf.data, ta.viewedArrayBuf.data)

ctx := typedArraySortCtx{
Expand Down

0 comments on commit 14f2d06

Please sign in to comment.