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)
......
This diff is collapsed.
......@@ -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