"define_disallowed", ["Cannot define property:", "%0", ", object is not extensible."],
"non_extensible_proto", ["%0", " is not extensible"],
"handler_non_object", ["Proxy.", "%0", " called with non-object as handler"],
- "trap_function_expected", ["Proxy.", "%0", " called with non-function for ", "%1", " trap"],
+ "trap_function_expected", ["Proxy.", "%0", " called with non-function for '", "%1", "' trap"],
"handler_trap_missing", ["Proxy handler ", "%0", " has no '", "%1", "' trap"],
"handler_trap_must_be_callable", ["Proxy handler ", "%0", " has non-callable '", "%1", "' trap"],
- "handler_returned_false", ["Proxy handler ", "%0", " returned false for '", "%1", "' trap"],
- "handler_returned_undefined", ["Proxy handler ", "%0", " returned undefined for '", "%1", "' trap"],
- "proxy_prop_not_configurable", ["Trap ", "%1", " of proxy handler ", "%0", " returned non-configurable descriptor for property ", "%2"],
- "proxy_non_object_prop_names", ["Trap ", "%1", " returned non-object ", "%0"],
- "proxy_repeated_prop_name", ["Trap ", "%1", " returned repeated property name ", "%2"],
+ "handler_returned_false", ["Proxy handler ", "%0", " returned false from '", "%1", "' trap"],
+ "handler_returned_undefined", ["Proxy handler ", "%0", " returned undefined from '", "%1", "' trap"],
+ "proxy_prop_not_configurable", ["Proxy handler ", "%0", " returned non-configurable descriptor for property '", "%2", "' from '", "%1", "' trap"],
+ "proxy_non_object_prop_names", ["Trap '", "%1", "' returned non-object ", "%0"],
+ "proxy_repeated_prop_name", ["Trap '", "%1", "' returned repeated property name '", "%2", "'"],
"invalid_weakmap_key", ["Invalid value used as weak map key"],
// RangeError
"invalid_array_length", ["Invalid array length"],
function TestCall(isStrict, callTrap) {
assertEquals(42, callTrap(5, 37))
- // TODO(rossberg): unrelated bug: this does not succeed for optimized code:
- // assertEquals(isStrict ? undefined : global_object, receiver)
+ assertEquals(isStrict ? undefined : global_object, receiver)
+
+ var handler = {
+ get: function(r, k) {
+ return k == "length" ? 2 : Function.prototype[k]
+ }
+ }
+ var f = Proxy.createFunction(handler, callTrap)
- var f = Proxy.createFunction({}, callTrap)
receiver = 333
assertEquals(42, f(11, 31))
assertEquals(isStrict ? undefined : global_object, receiver)
- var o = {}
+ var o = {f: f}
+ receiver = 333
+ assertEquals(42, o.f(10, 32))
+ assertSame(o, receiver)
+ receiver = 333
+ assertEquals(42, o["f"](9, 33))
+ assertSame(o, receiver)
+ receiver = 333
+ assertEquals(42, (1, o).f(8, 34))
+ assertSame(o, receiver)
+ receiver = 333
+ assertEquals(42, (1, o)["f"](7, 35))
+ assertSame(o, receiver)
+ receiver = 333
+ assertEquals(42, f.call(o, 32, 10))
+ assertSame(o, receiver)
+ receiver = 333
+ assertEquals(42, f.call(null, 33, 9))
+ assertSame(isStrict ? null : global_object, receiver)
+ receiver = 333
+ assertEquals(44, f.call(2, 21, 23))
+ assertSame(2, receiver.valueOf())
+ receiver = 333
assertEquals(42, Function.prototype.call.call(f, o, 20, 22))
- assertEquals(o, receiver)
+ assertSame(o, receiver)
+ receiver = 333
assertEquals(43, Function.prototype.call.call(f, null, 20, 23))
- assertEquals(isStrict ? null : global_object, receiver)
+ assertSame(isStrict ? null : global_object, receiver)
assertEquals(44, Function.prototype.call.call(f, 2, 21, 23))
assertEquals(2, receiver.valueOf())
receiver = 333
+ assertEquals(32, f.apply(o, [16, 16]))
+ assertSame(o, receiver)
+ receiver = 333
assertEquals(32, Function.prototype.apply.call(f, o, [17, 15]))
- assertEquals(o, receiver)
+ assertSame(o, receiver)
+
var ff = Function.prototype.bind.call(f, o, 12)
+ assertTrue(ff.length <= 1) // TODO(rossberg): Not spec'ed yet, be lax.
receiver = 333
assertEquals(42, ff(30))
- assertEquals(o, receiver)
+ assertSame(o, receiver)
receiver = 333
assertEquals(32, Function.prototype.apply.call(ff, {}, [20]))
- assertEquals(o, receiver)
+ assertSame(o, receiver)
+
+ var fff = Function.prototype.bind.call(ff, o, 30)
+ assertEquals(0, fff.length)
+ receiver = 333
+ assertEquals(42, fff())
+ assertSame(o, receiver)
+ receiver = 333
+ assertEquals(42, Function.prototype.call.call(fff, {}))
+ assertSame(o, receiver)
var f = CreateFrozen({}, callTrap)
receiver = 333
assertEquals(42, f(11, 31))
- // TODO(rossberg): unrelated bug: this does not succeed for optimized code.
- // assertEquals(isStrict ? undefined : global, receiver)
+ assertSame(isStrict ? undefined : global_object, receiver)
+ var o = {f: f}
+ receiver = 333
+ assertEquals(42, o.f(10, 32))
+ assertSame(o, receiver)
+ receiver = 333
+ assertEquals(42, o["f"](9, 33))
+ assertSame(o, receiver)
+ receiver = 333
+ assertEquals(42, (1, o).f(8, 34))
+ assertSame(o, receiver)
+ receiver = 333
+ assertEquals(42, (1, o)["f"](7, 35))
+ assertSame(o, receiver)
receiver = 333
assertEquals(42, Function.prototype.call.call(f, o, 20, 22))
- assertEquals(o, receiver)
+ assertSame(o, receiver)
receiver = 333
assertEquals(32, Function.prototype.apply.call(f, o, [17, 15]))
- assertEquals(o, receiver)
+ assertSame(o, receiver)
receiver = 333
assertEquals(42, ff(30))
- assertEquals(o, receiver)
+ assertSame(o, receiver)
receiver = 333
assertEquals(32, Function.prototype.apply.call(ff, {}, [20]))
- assertEquals(o, receiver)
+ assertSame(o, receiver)
}
TestCall(false, function(x, y) {
receiver = this; return x + y
})
+TestCall(false, function() {
+ receiver = this; return arguments[0] + arguments[1]
+})
+
TestCall(false, Proxy.createFunction({}, function(x, y) {
receiver = this; return x + y
}))
}))
+
+// Using intrinsics as call traps.
+
+function TestCallIntrinsic(type, callTrap) {
+ var f = Proxy.createFunction({}, callTrap)
+ var x = f()
+ assertTrue(typeof x == type)
+}
+
+TestCallIntrinsic("boolean", Boolean)
+TestCallIntrinsic("number", Number)
+TestCallIntrinsic("string", String)
+TestCallIntrinsic("object", Object)
+TestCallIntrinsic("function", Function)
+
+
+
+// Throwing from call trap.
+
function TestCallThrow(callTrap) {
var f = Proxy.createFunction({}, callTrap)
assertThrows(function(){ f(11) }, "myexn")
+ assertThrows(function(){ ({x: f}).x(11) }, "myexn")
+ assertThrows(function(){ ({x: f})["x"](11) }, "myexn")
assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn")
assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn")
var f = CreateFrozen({}, callTrap)
assertThrows(function(){ f(11) }, "myexn")
+ assertThrows(function(){ ({x: f}).x(11) }, "myexn")
+ assertThrows(function(){ ({x: f})["x"](11) }, "myexn")
assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn")
assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn")
}
function TestConstruct2(proto, constructTrap, handler) {
var f = Proxy.createFunction(handler, function() {}, constructTrap)
var o = new f(11, 31)
- // TODO(rossberg): doesn't hold, due to unrelated bug.
- // assertEquals(undefined, receiver)
+ assertEquals(undefined, receiver)
assertEquals(42, o.sum)
assertSame(proto, Object.getPrototypeOf(o))
+// TODO(rossberg): does not work yet.
+// var ff = Function.prototype.bind.call(f, o, 10)
+// var o = new ff(32)
+// assertEquals(undefined, receiver)
+// assertEquals(42, o.sum)
+// assertSame(proto, Object.getPrototypeOf(o))
+
var f = CreateFrozen(handler, function() {}, constructTrap)
var o = new f(11, 32)
- // TODO(rossberg): doesn't hold, due to unrelated bug.
- // assertEquals(undefined, receiver)
+ assertEquals(undefined, receiver)
assertEquals(43, o.sum)
assertSame(proto, Object.getPrototypeOf(o))
+
+ var ff = Function.prototype.bind.call(f, o, 10)
+ var o = new ff(32)
+ assertEquals(undefined, receiver)
+ assertEquals(42, o.sum)
+ assertSame(proto, Object.getPrototypeOf(o))
}
TestConstruct(Object.prototype, ReturnNew)
TestConstruct(prototype, CreateFrozen({}, ReturnNewWithProto))
+
+// Construction with derived construct trap.
+
function TestConstructFromCall(proto, returnsThis, callTrap) {
TestConstructFromCall2(proto, returnsThis, callTrap, handlerWithPrototype)
TestConstructFromCall2(proto, returnsThis, callTrap, handlerSansPrototype)
assertEquals(42, o.sum)
assertSame(proto, Object.getPrototypeOf(o))
+// TODO(rossberg): does not work yet.
+// var ff = Function.prototype.bind.call(f, o, 10)
+// var o = new ff(32)
+// assertEquals(undefined, receiver)
+// assertEquals(42, o.sum)
+// assertSame(proto, Object.getPrototypeOf(o))
+
var f = CreateFrozen(handler, callTrap)
var o = new f(11, 32)
if (returnsThis) assertEquals(o, receiver)
assertEquals(43, o.sum)
assertSame(proto, Object.getPrototypeOf(o))
+
+// TODO(rossberg): does not work yet.
+// var ff = Function.prototype.bind.call(f, o, 10)
+// var o = new ff(32)
+// assertEquals(undefined, receiver)
+// assertEquals(42, o.sum)
+// assertSame(proto, Object.getPrototypeOf(o))
}
TestConstructFromCall(Object.prototype, true, ReturnUndef)
TestConstructFromCall(prototype, false, CreateFrozen(handlerWithPrototype, ReturnNewWithProto))
+
+// Throwing from the construct trap.
+
function TestConstructThrow(trap) {
TestConstructThrow2(Proxy.createFunction({fix: function() {return {}}}, trap))
TestConstructThrow2(Proxy.createFunction({fix: function() {return {}}},
-// Getters and setters.
+// Using function proxies as getters and setters.
var value
var receiver