1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00

Fix Function.bind to use newBoundFunction...

...instead of newBoundFunctionObject

Basically, newBoundFunctionObject returns an incomplete object (without a prototype, __prototype__, etc.)
This commit is contained in:
Robert Krimen 2014-01-20 08:27:19 -08:00
parent c47cf7be6e
commit 4860ef573b
3 changed files with 30 additions and 3 deletions

View File

@ -90,5 +90,5 @@ func builtinFunction_bind(call FunctionCall) Value {
this = toValue_object(call.runtime.GlobalObject) this = toValue_object(call.runtime.GlobalObject)
} }
return toValue_object(call.runtime.newBoundFunctionObject(targetObject, this, argumentList)) return toValue_object(call.runtime.newBoundFunction(targetObject, this, argumentList))
} }

View File

@ -135,8 +135,8 @@ func TestFunction_bind(t *testing.T) {
return "abc"; return "abc";
}; };
def = abc.bind(); def = abc.bind();
def(); [ typeof def.prototype, typeof def.hasOwnProperty, def() ];
`, "abc") `, "object,function,abc")
test(` test(`
abc = function(){ abc = function(){
@ -146,4 +146,22 @@ func TestFunction_bind(t *testing.T) {
ghi = abc.bind(undefined, "abc", "ghi"); ghi = abc.bind(undefined, "abc", "ghi");
[ def(), def("def"), ghi("def") ]; [ def(), def("def"), ghi("def") ];
`, ",def,ghi") `, ",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")
} }

View File

@ -43,6 +43,15 @@ func (runtime *_runtime) newBoundFunctionObject(target *_object, this Value, arg
return self 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 { func (self *_object) functionValue() _functionObject {
value, _ := self.value.(_functionObject) value, _ := self.value.(_functionObject)
return value return value