Skip to content

Commit 5ef83b8

Browse files
committed
Correctly handle null or undefined second argument to Function.prototype.apply(). Fixes #655
1 parent 12bd88c commit 5ef83b8

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

builtin_function.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ func (r *Runtime) createListFromArrayLike(a Value) []Value {
9090

9191
func (r *Runtime) functionproto_apply(call FunctionCall) Value {
9292
var args []Value
93-
if len(call.Arguments) >= 2 {
94-
args = r.createListFromArrayLike(call.Arguments[1])
93+
94+
argArray := call.Argument(1)
95+
if !IsUndefined(argArray) && !IsNull(argArray) {
96+
args = r.createListFromArrayLike(argArray)
9597
}
9698

9799
f := r.toCallable(call.This)

builtin_function_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ func TestHashbangInFunctionConstructor(t *testing.T) {
1212
`
1313
testScriptWithTestLib(SCRIPT, _undefined, t)
1414
}
15+
16+
func TestFunctionApplyNullArgArray(t *testing.T) {
17+
const SCRIPT = `
18+
assert.sameValue(0, (function() {return arguments.length}).apply(undefined, null))
19+
`
20+
testScriptWithTestLib(SCRIPT, _undefined, t)
21+
}

0 commit comments

Comments
 (0)