Skip to content

Commit

Permalink
Fix Array.prototype.toSpliced
Browse files Browse the repository at this point in the history
  • Loading branch information
shiroyk committed Aug 6, 2024
1 parent 14f2d06 commit 41690ac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
21 changes: 9 additions & 12 deletions builtin_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -1338,11 +1338,9 @@ func (r *Runtime) arrayproto_toSpliced(call FunctionCall) Value {
length := toLength(o.self.getStr("length", nil))
actualStart := relToIdx(call.Argument(0).ToInteger(), length)
var actualSkipCount int64
switch len(call.Arguments) {
case 0:
case 1:
if len(call.Arguments) == 1 {
actualSkipCount = length - actualStart
default:
} else if len(call.Arguments) > 1 {
actualSkipCount = min(max(call.Argument(1).ToInteger(), 0), length-actualStart)
}
itemCount := max(int64(len(call.Arguments)-2), 0)
Expand All @@ -1354,24 +1352,23 @@ func (r *Runtime) arrayproto_toSpliced(call FunctionCall) Value {
if src := r.checkStdArrayObj(o); src != nil {
var values []Value
if itemCount < actualSkipCount {
values = src.values
values = make([]Value, len(src.values))
copy(values, src.values)
copy(values[actualStart+itemCount:], values[actualStart+actualSkipCount:])
tail := values[newLength:]
for k := range tail {
tail[k] = nil
}
values = values[:newLength]
} else if itemCount > actualSkipCount {
if int64(cap(src.values)) >= newLength {
values = src.values[:newLength]
copy(values[actualStart+itemCount:], values[actualStart+actualSkipCount:length])
values = make([]Value, newLength)
copy(values, src.values[:actualStart])
copy(values[actualStart+itemCount:], src.values[actualStart+actualSkipCount:])
} else {
values = make([]Value, newLength)
copy(values, src.values[:actualStart])
copy(values[actualStart+itemCount:], src.values[actualStart+actualSkipCount:])
}
} else {
values = src.values
values = make([]Value, len(src.values))
copy(values, src.values)
}
if itemCount > 0 {
copy(values[actualStart:], call.Arguments[2:])
Expand Down
24 changes: 23 additions & 1 deletion builtin_arrray_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package sobek

import "testing"
import (
"testing"
)

func TestArrayProtoProp(t *testing.T) {
const SCRIPT = `
Expand Down Expand Up @@ -339,3 +341,23 @@ func TestArrayProto(t *testing.T) {
`
testScriptWithTestLib(SCRIPT, _undefined, t)
}

func TestArrayToSpliced(t *testing.T) {
const SCRIPT = `
const a = [1, 2, 3];
a.push(4)
assert(compareArray(a, [1, 2, 3, 4]));
const b = a.toSpliced(2)
assert(compareArray(a, [1, 2, 3, 4]));
assert(compareArray(b, [1, 2]));
a.push(5)
const c = a.toSpliced(1, 2);
assert(compareArray(a, [1, 2, 3, 4, 5]));
assert(compareArray(c, [1, 4, 5]));
assert(compareArray(a.toSpliced(4, 2, 'a', 'b'), [1, 2, 3, 4, 'a', 'b']));
assert(compareArray(a.toSpliced(-2, 2), [1, 2, 3]));
assert(compareArray(a.toSpliced(2, 10), [1, 2]));
assert(compareArray(a.toSpliced(1, 0, 'a'), [1, 'a', 2, 3, 4, 5]));
`
testScriptWithTestLib(SCRIPT, _undefined, t)
}

0 comments on commit 41690ac

Please sign in to comment.