Commit 47539761 authored by rossberg@chromium.org's avatar rossberg@chromium.org

Fix handling of this in direct calls to function proxies.

Fix & tweak some proxy-related error messages.

R=kmillikin@chromium.org
BUG=v8:1543
TEST=

Review URL: http://codereview.chromium.org/8229008

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@9613 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent baa29eba
......@@ -413,7 +413,7 @@ Object* CallICBase::TryCallAsFunction(Object* object) {
Handle<Object> target(object, isolate());
Handle<Object> delegate = Execution::GetFunctionDelegate(target);
if (delegate->IsJSFunction()) {
if (delegate->IsJSFunction() && !object->IsJSFunctionProxy()) {
// Patch the receiver and use the delegate as the function to
// invoke. This is used for invoking objects as if they were
// functions.
......@@ -430,6 +430,10 @@ Object* CallICBase::TryCallAsFunction(Object* object) {
void CallICBase::ReceiverToObjectIfRequired(Handle<Object> callee,
Handle<Object> object) {
while (callee->IsJSFunctionProxy()) {
callee = Handle<Object>(JSFunctionProxy::cast(*callee)->call_trap());
}
if (callee->IsJSFunction()) {
Handle<JSFunction> function = Handle<JSFunction>::cast(callee);
if (function->shared()->strict_mode() || function->IsBuiltin()) {
......
......@@ -185,14 +185,14 @@ function FormatMessage(message) {
"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"],
......
......@@ -2507,6 +2507,7 @@ MUST_USE_RESULT MaybeObject* JSProxy::SetPropertyWithHandlerIfDefiningSetter(
*found = true; // except where defined otherwise...
Isolate* isolate = GetHeap()->isolate();
Handle<JSProxy> proxy(this);
Handle<Object> handler(this->handler()); // Trap might morph proxy.
Handle<String> name(name_raw);
Handle<Object> value(value_raw);
Handle<Object> args[] = { name };
......@@ -2530,7 +2531,9 @@ MUST_USE_RESULT MaybeObject* JSProxy::SetPropertyWithHandlerIfDefiningSetter(
Handle<Object> configurable(v8::internal::GetProperty(desc, conf_name));
ASSERT(!isolate->has_pending_exception());
if (configurable->IsFalse()) {
Handle<Object> args[] = { Handle<Object>(proxy->handler()), proxy, name };
Handle<String> trap =
isolate->factory()->LookupAsciiSymbol("getPropertyDescriptor");
Handle<Object> args[] = { handler, trap, name };
Handle<Object> error = isolate->factory()->NewTypeError(
"proxy_prop_not_configurable", HandleVector(args, ARRAY_SIZE(args)));
return isolate->Throw(*error);
......@@ -2610,6 +2613,7 @@ MUST_USE_RESULT PropertyAttributes JSProxy::GetPropertyAttributeWithHandler(
Isolate* isolate = GetIsolate();
HandleScope scope(isolate);
Handle<JSProxy> proxy(this);
Handle<Object> handler(this->handler()); // Trap might morph proxy.
Handle<JSReceiver> receiver(receiver_raw);
Handle<Object> name(name_raw);
......@@ -2639,7 +2643,9 @@ MUST_USE_RESULT PropertyAttributes JSProxy::GetPropertyAttributeWithHandler(
if (isolate->has_pending_exception()) return NONE;
if (configurable->IsFalse()) {
Handle<Object> args[] = { Handle<Object>(proxy->handler()), proxy, name };
Handle<String> trap =
isolate->factory()->LookupAsciiSymbol("getPropertyDescriptor");
Handle<Object> args[] = { handler, trap, name };
Handle<Object> error = isolate->factory()->NewTypeError(
"proxy_prop_not_configurable", HandleVector(args, ARRAY_SIZE(args)));
isolate->Throw(*error);
......
......@@ -46,48 +46,102 @@ var receiver
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) {
......@@ -99,6 +153,10 @@ TestCall(true, 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
}))
......@@ -113,14 +171,37 @@ TestCall(false, CreateFrozen({}, function(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")
}
......@@ -165,17 +246,28 @@ function TestConstruct(proto, constructTrap) {
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)
......@@ -188,6 +280,9 @@ TestConstruct(Object.prototype, CreateFrozen({}, 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)
......@@ -200,11 +295,25 @@ function TestConstructFromCall2(proto, returnsThis, callTrap, handler) {
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)
......@@ -248,6 +357,9 @@ TestConstructFromCall(Object.prototype, false, CreateFrozen(handlerWithPrototype
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 {}}},
......@@ -266,7 +378,7 @@ TestConstructThrow(CreateFrozen({}, function() { throw "myexn" }))
// Getters and setters.
// Using function proxies as getters and setters.
var value
var receiver
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment