diff --git a/builtin_function.go b/builtin_function.go index 080b624..f244344 100644 --- a/builtin_function.go +++ b/builtin_function.go @@ -90,5 +90,5 @@ func builtinFunction_bind(call FunctionCall) Value { this = toValue_object(call.runtime.GlobalObject) } - return toValue_object(call.runtime.newBoundFunctionObject(targetObject, this, argumentList)) + return toValue_object(call.runtime.newBoundFunction(targetObject, this, argumentList)) } diff --git a/function_test.go b/function_test.go index 62313f9..0530a3d 100644 --- a/function_test.go +++ b/function_test.go @@ -135,8 +135,8 @@ func TestFunction_bind(t *testing.T) { return "abc"; }; def = abc.bind(); - def(); - `, "abc") + [ typeof def.prototype, typeof def.hasOwnProperty, def() ]; + `, "object,function,abc") test(` abc = function(){ @@ -146,4 +146,22 @@ func TestFunction_bind(t *testing.T) { ghi = abc.bind(undefined, "abc", "ghi"); [ def(), def("def"), ghi("def") ]; `, ",def,ghi") + + test(` + var abc = function () {}; + var ghi; + try { + Object.defineProperty(Function.prototype, "xyzzy", { + value: 1001, + writable: true, + enumerable: true, + configurable: true + }); + var def = abc.bind({}); + ghi = !def.hasOwnProperty("xyzzy") && ghi.xyzzy === 1001; + } finally { + delete Function.prototype.xyzzy; + } + [ ghi ]; + `, "true") } diff --git a/type_function.go b/type_function.go index eee7cb7..4203551 100644 --- a/type_function.go +++ b/type_function.go @@ -43,6 +43,15 @@ func (runtime *_runtime) newBoundFunctionObject(target *_object, this Value, arg return self } +func (runtime *_runtime) newBoundFunction(target *_object, this Value, argumentList []Value) *_object { + self := runtime.newBoundFunctionObject(target, this, argumentList) + self.prototype = runtime.Global.FunctionPrototype + prototype := runtime.newObject() + self.defineProperty("prototype", toValue_object(prototype), 0100, false) + prototype.defineProperty("constructor", toValue_object(self), 0100, false) + return self +} + func (self *_object) functionValue() _functionObject { value, _ := self.value.(_functionObject) return value