Commit 62127d00 authored by neis's avatar neis Committed by Commit bot

[proxies] Implement Proxy.revocable.

For now, we revoke a proxy by setting its handler to null (as in the spec).

Change the "target" field from Object to JSReceiver as there's no point in
allowing more.

R=jkummerow@chromium.org, rossberg
BUG=v8:1543
LOG=n

Review URL: https://codereview.chromium.org/1496243003

Cr-Commit-Position: refs/heads/master@{#32608}
parent 6f4d477f
......@@ -42,6 +42,11 @@ function ProxyCreateFunction(handler, callTrap, constructTrap) {
{}, handler, callTrap, constructTrap, GlobalFunction.prototype)
}
function ProxyCreateRevocable(target, handler) {
var p = new GlobalProxy(target, handler);
return {proxy: p, revoke: () => %RevokeProxy(p)};
}
// -------------------------------------------------------------------
// Proxy Builtins
......@@ -144,6 +149,7 @@ function ProxyEnumerate(trap, handler, target) {
//Set up non-enumerable properties of the Proxy object.
utils.InstallFunctions(GlobalProxy, DONT_ENUM, [
"revocable", ProxyCreateRevocable,
"createFunction", ProxyCreateFunction
]);
......
......@@ -6336,10 +6336,12 @@ int JSFunction::NumberOfLiterals() {
}
ACCESSORS(JSProxy, target, Object, kTargetOffset)
ACCESSORS(JSProxy, handler, Object, kHandlerOffset)
ACCESSORS(JSProxy, target, JSReceiver, kTargetOffset)
ACCESSORS(JSProxy, hash, Object, kHashOffset)
bool JSProxy::IsRevoked() const { return !handler()->IsJSReceiver(); }
ACCESSORS(JSFunctionProxy, call_trap, JSReceiver, kCallTrapOffset)
ACCESSORS(JSFunctionProxy, construct_trap, Object, kConstructTrapOffset)
......
......@@ -758,16 +758,14 @@ MaybeHandle<Object> JSProxy::GetProperty(Isolate* isolate,
// 2. Let handler be the value of the [[ProxyHandler]] internal slot of O.
Handle<Object> handler(proxy->handler(), isolate);
// 3. If handler is null, throw a TypeError exception.
// 4. Assert: Type(handler) is Object.
if (proxy->IsRevoked()) {
THROW_NEW_ERROR(isolate,
NewTypeError(MessageTemplate::kProxyRevoked, trap_name),
Object);
}
// 4. Assert: Type(handler) is Object.
DCHECK(handler->IsJSReceiver());
DCHECK(proxy->target()->IsJSReceiver());
// 5. Let target be the value of the [[ProxyTarget]] internal slot of O.
Handle<JSReceiver> target(JSReceiver::cast(proxy->target()), isolate);
Handle<JSReceiver> target(proxy->target(), isolate);
// 6. Let trap be ? GetMethod(handler, "get").
Handle<Object> trap;
ASSIGN_RETURN_ON_EXCEPTION(
......@@ -957,22 +955,22 @@ Handle<FixedArray> JSObject::EnsureWritableFastElements(
// static
MaybeHandle<Object> JSProxy::GetPrototype(Handle<JSProxy> proxy) {
Isolate* isolate = proxy->GetIsolate();
Handle<String> trap_name = isolate->factory()->getPrototypeOf_string();
// 1. Let handler be the value of the [[ProxyHandler]] internal slot.
Handle<Object> raw_handler(proxy->handler(), isolate);
// 2. If handler is null, throw a TypeError exception.
// 3. Assert: Type(handler) is Object.
if (!raw_handler->IsJSReceiver()) {
// TODO(cbruni): Throw correct error message.
THROW_NEW_ERROR(
isolate, NewTypeError(MessageTemplate::kProxyHandlerNonObject), Object);
}
Handle<JSReceiver> handler = Handle<JSReceiver>::cast(raw_handler);
// 4. Let target be the value of the [[ProxyTarget]] internal slot.
// TODO(cbruni): Change target type to JSReceiver by default.
Handle<JSReceiver> target(JSReceiver::cast(proxy->target()), isolate);
if (proxy->IsRevoked()) {
THROW_NEW_ERROR(isolate,
NewTypeError(MessageTemplate::kProxyRevoked, trap_name),
Object);
}
Handle<JSReceiver> target(proxy->target(), isolate);
Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate);
// 5. Let trap be ? GetMethod(handler, "getPrototypeOf").
Handle<Object> trap;
Handle<String> trap_name = isolate->factory()->getPrototypeOf_string();
ASSIGN_RETURN_ON_EXCEPTION(isolate, trap, GetMethod(handler, trap_name),
Object);
// 6. If trap is undefined, then return target.[[GetPrototypeOf]]().
......@@ -1013,15 +1011,6 @@ MaybeHandle<Object> JSProxy::GetPrototype(Handle<JSProxy> proxy) {
}
bool JSProxy::IsRevoked() const {
// TODO(neis): Decide on how to represent revocation. For now, revocation is
// unsupported.
DCHECK(target()->IsJSReceiver());
DCHECK(handler()->IsJSReceiver());
return false;
}
MaybeHandle<Object> Object::GetPropertyWithAccessor(
LookupIterator* it, LanguageMode language_mode) {
Isolate* isolate = it->isolate();
......@@ -4620,22 +4609,27 @@ Handle<Map> JSObject::GetElementsTransitionMap(Handle<JSObject> object,
}
void JSProxy::Revoke(Handle<JSProxy> proxy) {
Isolate* isolate = proxy->GetIsolate();
if (!proxy->IsRevoked()) proxy->set_handler(isolate->heap()->null_value());
DCHECK(proxy->IsRevoked());
}
Maybe<bool> JSProxy::HasProperty(Isolate* isolate, Handle<JSProxy> proxy,
Handle<Name> name) {
// 1. (Assert)
// 2. Let handler be the value of the [[ProxyHandler]] internal slot of O.
Handle<Object> handler(proxy->handler(), isolate);
// 3. If handler is null, throw a TypeError exception.
// 4. Assert: Type(handler) is Object.
if (proxy->IsRevoked()) {
isolate->Throw(*isolate->factory()->NewTypeError(
MessageTemplate::kProxyRevoked, isolate->factory()->has_string()));
return Nothing<bool>();
}
// 4. Assert: Type(handler) is Object.
DCHECK(handler->IsJSReceiver());
DCHECK(proxy->target()->IsJSReceiver());
// 5. Let target be the value of the [[ProxyTarget]] internal slot of O.
Handle<JSReceiver> target(JSReceiver::cast(proxy->target()), isolate);
Handle<JSReceiver> target(proxy->target(), isolate);
// 6. Let trap be ? GetMethod(handler, "has").
Handle<Object> trap;
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
......@@ -4700,8 +4694,7 @@ Maybe<bool> JSProxy::SetProperty(Handle<JSProxy> proxy, Handle<Name> name,
*factory->NewTypeError(MessageTemplate::kProxyRevoked, trap_name));
return Nothing<bool>();
}
Handle<JSReceiver> target(JSReceiver::cast(proxy->target()), isolate);
Handle<JSReceiver> target(proxy->target(), isolate);
Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate);
Handle<Object> trap;
......@@ -4762,8 +4755,7 @@ Maybe<bool> JSProxy::DeletePropertyOrElement(Handle<JSProxy> proxy,
*factory->NewTypeError(MessageTemplate::kProxyRevoked, trap_name));
return Nothing<bool>();
}
Handle<JSReceiver> target(JSReceiver::cast(proxy->target()), isolate);
Handle<JSReceiver> target(proxy->target(), isolate);
Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate);
Handle<Object> trap;
......@@ -6835,17 +6827,14 @@ bool JSProxy::DefineOwnProperty(Isolate* isolate, Handle<JSProxy> proxy,
// 2. Let handler be the value of the [[ProxyHandler]] internal slot of O.
Handle<Object> handler(proxy->handler(), isolate);
// 3. If handler is null, throw a TypeError exception.
// 4. Assert: Type(handler) is Object.
if (proxy->IsRevoked()) {
isolate->Throw(*isolate->factory()->NewTypeError(
MessageTemplate::kProxyRevoked, trap_name));
return false;
}
// 4. Assert: Type(handler) is Object.
DCHECK(handler->IsJSReceiver());
// If the handler is not null, the target can't be null either.
DCHECK(proxy->target()->IsJSReceiver());
// 5. Let target be the value of the [[ProxyTarget]] internal slot of O.
Handle<JSReceiver> target(JSReceiver::cast(proxy->target()), isolate);
Handle<JSReceiver> target(proxy->target(), isolate);
// 6. Let trap be ? GetMethod(handler, "defineProperty").
Handle<Object> trap;
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
......@@ -7019,17 +7008,14 @@ bool JSProxy::GetOwnPropertyDescriptor(Isolate* isolate, Handle<JSProxy> proxy,
// 2. Let handler be the value of the [[ProxyHandler]] internal slot of O.
Handle<Object> handler(proxy->handler(), isolate);
// 3. If handler is null, throw a TypeError exception.
// 4. Assert: Type(handler) is Object.
if (proxy->IsRevoked()) {
isolate->Throw(*isolate->factory()->NewTypeError(
MessageTemplate::kProxyRevoked, trap_name));
return false;
}
// 4. Assert: Type(handler) is Object.
DCHECK(handler->IsJSReceiver());
// If the handler is not null, the target can't be null either.
DCHECK(proxy->target()->IsJSReceiver());
// 5. Let target be the value of the [[ProxyTarget]] internal slot of O.
Handle<JSReceiver> target(JSReceiver::cast(proxy->target()), isolate);
Handle<JSReceiver> target(proxy->target(), isolate);
// 6. Let trap be ? GetMethod(handler, "getOwnPropertyDescriptor").
Handle<Object> trap;
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
......@@ -7277,8 +7263,7 @@ Maybe<bool> JSProxy::PreventExtensions(Handle<JSProxy> proxy,
*factory->NewTypeError(MessageTemplate::kProxyRevoked, trap_name));
return Nothing<bool>();
}
Handle<JSReceiver> target(JSReceiver::cast(proxy->target()), isolate);
Handle<JSReceiver> target(proxy->target(), isolate);
Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate);
Handle<Object> trap;
......@@ -7387,8 +7372,7 @@ Maybe<bool> JSProxy::IsExtensible(Handle<JSProxy> proxy) {
*factory->NewTypeError(MessageTemplate::kProxyRevoked, trap_name));
return Nothing<bool>();
}
Handle<JSReceiver> target(JSReceiver::cast(proxy->target()), isolate);
Handle<JSReceiver> target(proxy->target(), isolate);
Handle<JSReceiver> handler(JSReceiver::cast(proxy->handler()), isolate);
Handle<Object> trap;
......@@ -8296,16 +8280,15 @@ bool JSProxy::Enumerate(Isolate* isolate, Handle<JSReceiver> receiver,
// 1. Let handler be the value of the [[ProxyHandler]] internal slot of O.
Handle<Object> handler(proxy->handler(), isolate);
// 2. If handler is null, throw a TypeError exception.
// 3. Assert: Type(handler) is Object.
if (proxy->IsRevoked()) {
isolate->Throw(*isolate->factory()->NewTypeError(
MessageTemplate::kProxyRevoked,
isolate->factory()->enumerate_string()));
return false;
}
// 3. Assert: Type(handler) is Object.
DCHECK(handler->IsJSReceiver());
// 4. Let target be the value of the [[ProxyTarget]] internal slot of O.
Handle<JSReceiver> target(JSReceiver::cast(proxy->target()), isolate);
Handle<JSReceiver> target(proxy->target(), isolate);
// 5. Let trap be ? GetMethod(handler, "enumerate").
Handle<Object> trap;
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
......@@ -8402,15 +8385,14 @@ bool JSProxy::OwnPropertyKeys(Isolate* isolate, Handle<JSReceiver> receiver,
// 1. Let handler be the value of the [[ProxyHandler]] internal slot of O.
Handle<Object> handler(proxy->handler(), isolate);
// 2. If handler is null, throw a TypeError exception.
// 3. Assert: Type(handler) is Object.
if (proxy->IsRevoked()) {
isolate->Throw(*isolate->factory()->NewTypeError(
MessageTemplate::kProxyRevoked, isolate->factory()->ownKeys_string()));
return false;
}
// 3. Assert: Type(handler) is Object.
DCHECK(handler->IsJSReceiver());
// 4. Let target be the value of the [[ProxyTarget]] internal slot of O.
Handle<JSReceiver> target(JSReceiver::cast(proxy->target()), isolate);
Handle<JSReceiver> target(proxy->target(), isolate);
// 5. Let trap be ? GetMethod(handler, "ownKeys").
Handle<Object> trap;
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
......@@ -14891,28 +14873,26 @@ Maybe<bool> JSProxy::SetPrototype(Handle<JSProxy> proxy, Handle<Object> value,
bool from_javascript,
ShouldThrow should_throw) {
Isolate* isolate = proxy->GetIsolate();
Handle<JSReceiver> handle;
Handle<Name> trap_name = isolate->factory()->setPrototypeOf_string();
// 1. Assert: Either Type(V) is Object or Type(V) is Null.
DCHECK(value->IsJSReceiver() || value->IsNull());
// 2. Let handler be the value of the [[ProxyHandler]] internal slot of O.
Handle<Object> raw_handler(proxy->handler(), isolate);
Handle<Object> handler(proxy->handler(), isolate);
// 3. If handler is null, throw a TypeError exception.
// 4. Assert: Type(handler) is Object.
if (proxy->IsRevoked()) {
DCHECK(raw_handler->IsNull());
DCHECK(proxy->target()->IsNull());
isolate->Throw(
*isolate->factory()->NewTypeError(MessageTemplate::kProxyRevoked));
isolate->Throw(*isolate->factory()->NewTypeError(
MessageTemplate::kProxyRevoked, trap_name));
return Nothing<bool>();
}
Handle<JSReceiver> handler = Handle<JSReceiver>::cast(raw_handler);
// 5. Let target be the value of the [[ProxyTarget]] internal slot.
Handle<JSReceiver> target(JSReceiver::cast(proxy->target()), isolate);
Handle<JSReceiver> target(proxy->target(), isolate);
// 6. Let trap be ? GetMethod(handler, "getPrototypeOf").
Handle<Object> trap;
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
isolate, trap, Object::GetMethod(handler, trap_name), Nothing<bool>());
isolate, trap,
Object::GetMethod(Handle<JSReceiver>::cast(handler), trap_name),
Nothing<bool>());
// 7. If trap is undefined, then return target.[[SetPrototypeOf]]().
if (trap->IsUndefined()) {
return JSReceiver::SetPrototype(target, value, from_javascript,
......
......@@ -9500,7 +9500,7 @@ class JSProxy: public JSReceiver {
// [handler]: The handler property.
DECL_ACCESSORS(handler, Object)
// [target]: The target property.
DECL_ACCESSORS(target, Object)
DECL_ACCESSORS(target, JSReceiver)
// [hash]: The hash code property (undefined if not initialized yet).
DECL_ACCESSORS(hash, Object)
......@@ -9508,7 +9508,8 @@ class JSProxy: public JSReceiver {
DECLARE_CAST(JSProxy)
bool IsRevoked() const;
INLINE(bool IsRevoked() const);
static void Revoke(Handle<JSProxy> proxy);
// ES6 9.5.1
static MaybeHandle<Object> GetPrototype(Handle<JSProxy> receiver);
......
......@@ -65,5 +65,15 @@ RUNTIME_FUNCTION(Runtime_GetConstructTrap) {
return proxy->construct_trap();
}
RUNTIME_FUNCTION(Runtime_RevokeProxy) {
HandleScope scope(isolate);
DCHECK(args.length() == 1);
CONVERT_ARG_HANDLE_CHECKED(JSProxy, proxy, 0);
JSProxy::Revoke(proxy);
return isolate->heap()->undefined_value();
}
} // namespace internal
} // namespace v8
......@@ -547,7 +547,8 @@ namespace internal {
F(IsJSFunctionProxy, 1, 1) \
F(GetHandler, 1, 1) \
F(GetCallTrap, 1, 1) \
F(GetConstructTrap, 1, 1)
F(GetConstructTrap, 1, 1) \
F(RevokeProxy, 1, 1)
#define FOR_EACH_INTRINSIC_REGEXP(F) \
......
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-proxies --harmony-reflect
traps = [
"getPrototypeOf", "setPrototypeOf", "isExtensible", "preventExtensions",
"getOwnPropertyDescriptor", "has", "get", "set", "deleteProperty",
"defineProperty", "ownKeys", "apply", "construct"
];
// TODO(neis): Fix enumerate.
var {proxy, revoke} = Proxy.revocable({}, {});
assertEquals(0, revoke.length);
assertEquals(undefined, revoke());
for (var trap of traps) {
assertThrows(() => Reflect[trap](proxy), TypeError);
}
assertEquals(undefined, revoke());
for (var trap of traps) {
assertThrows(() => Reflect[trap](proxy), TypeError);
}
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