Skip to content

Commit b7ff2bd

Browse files
camillobruniCommit bot
authored andcommitted
[proxies] Better print for proxies in d8
Function proxies would not be printed so far since they ended up in Function.prototype.toString which only works with Function as a receiver but no Proxy. Additionally added support for more gracefully dealing with recursive __proto__ structures introduced by proxies. drive-by-fix: use IS_PROXY if possible in .js files. BUG=v8:1543 LOG=n Committed: https://crrev.com/8bfb7189a3472bc9d0820a1bd4534eaaf78ff847 Cr-Commit-Position: refs/heads/master@{#32985} Review URL: https://codereview.chromium.org/1530293004 Cr-Commit-Position: refs/heads/master@{#33010}
1 parent b00d9e2 commit b7ff2bd

10 files changed

Lines changed: 73 additions & 37 deletions

File tree

src/d8.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,28 @@
88
// Used by the d8 shell to output results.
99
var stringifyDepthLimit = 4; // To avoid crashing on cyclic objects
1010

11+
// Hacky solution to circumvent forcing --allow-natives-syntax for d8
12+
function isProxy(o) { return false };
13+
function JSProxyGetTarget(proxy) { };
14+
function JSProxyGetHandler(proxy) { };
15+
16+
try {
17+
isProxy = Function(['object'], 'return %_IsJSProxy(object)');
18+
JSProxyGetTarget = Function(['proxy'],
19+
'return %JSProxyGetTarget(proxy)');
20+
JSProxyGetHandler = Function(['proxy'],
21+
'return %JSProxyGetHandler(proxy)');
22+
} catch(e) {};
23+
24+
1125
function Stringify(x, depth) {
1226
if (depth === undefined)
1327
depth = stringifyDepthLimit;
1428
else if (depth === 0)
15-
return "*";
29+
return "...";
30+
if (isProxy(x)) {
31+
return StringifyProxy(x, depth);
32+
}
1633
switch (typeof x) {
1734
case "undefined":
1835
return "undefined";
@@ -63,3 +80,12 @@ function Stringify(x, depth) {
6380
return "[crazy non-standard value]";
6481
}
6582
}
83+
84+
function StringifyProxy(proxy, depth) {
85+
var proxy_type = typeof proxy;
86+
var info_object = {
87+
target: JSProxyGetTarget(proxy),
88+
handler: JSProxyGetHandler(proxy)
89+
}
90+
return '[' + proxy_type + ' Proxy ' + Stringify(info_object, depth-1) + ']';
91+
}

src/js/collection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function GetExistingHash(key) {
100100
if ((field & 1 /* Name::kHashNotComputedMask */) === 0) {
101101
return field >>> 2 /* Name::kHashShift */;
102102
}
103-
} else if (IS_RECEIVER(key) && !%_IsJSProxy(key) && !IS_GLOBAL(key)) {
103+
} else if (IS_RECEIVER(key) && !IS_PROXY(key) && !IS_GLOBAL(key)) {
104104
var hash = GET_PRIVATE(key, hashCodeSymbol);
105105
return hash;
106106
}

src/js/json.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ function JSONSerialize(key, holder, replacer, stack, indent, gap) {
188188

189189

190190
function JSONStringify(value, replacer, space) {
191-
if (%_ArgumentsLength() == 1 && !%_IsJSProxy(value)) {
191+
if (%_ArgumentsLength() == 1 && !IS_PROXY(value)) {
192192
return %BasicJSONStringify(value);
193193
}
194194
if (!IS_CALLABLE(replacer) && %is_arraylike(replacer)) {

src/js/macros.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,37 +88,38 @@
8888
# Note: We have special support for typeof(foo) === 'bar' in the compiler.
8989
# It will *not* generate a runtime typeof call for the most important
9090
# values of 'bar'.
91-
macro IS_NULL(arg) = (arg === null);
92-
macro IS_NULL_OR_UNDEFINED(arg) = (arg == null);
93-
macro IS_UNDEFINED(arg) = (arg === (void 0));
94-
macro IS_NUMBER(arg) = (typeof(arg) === 'number');
95-
macro IS_STRING(arg) = (typeof(arg) === 'string');
96-
macro IS_BOOLEAN(arg) = (typeof(arg) === 'boolean');
97-
macro IS_SYMBOL(arg) = (typeof(arg) === 'symbol');
98-
macro IS_OBJECT(arg) = (typeof(arg) === 'object');
9991
macro IS_ARRAY(arg) = (%_IsArray(arg));
92+
macro IS_ARRAYBUFFER(arg) = (%_ClassOf(arg) === 'ArrayBuffer');
93+
macro IS_BOOLEAN(arg) = (typeof(arg) === 'boolean');
94+
macro IS_BOOLEAN_WRAPPER(arg) = (%_ClassOf(arg) === 'Boolean');
95+
macro IS_DATAVIEW(arg) = (%_ClassOf(arg) === 'DataView');
10096
macro IS_DATE(arg) = (%_IsDate(arg));
97+
macro IS_ERROR(arg) = (%_ClassOf(arg) === 'Error');
10198
macro IS_FUNCTION(arg) = (%_IsFunction(arg));
102-
macro IS_REGEXP(arg) = (%_IsRegExp(arg));
103-
macro IS_SIMD_VALUE(arg) = (%_IsSimdValue(arg));
104-
macro IS_SET(arg) = (%_ClassOf(arg) === 'Set');
99+
macro IS_GENERATOR(arg) = (%_ClassOf(arg) === 'Generator');
100+
macro IS_GLOBAL(arg) = (%_ClassOf(arg) === 'global');
105101
macro IS_MAP(arg) = (%_ClassOf(arg) === 'Map');
106-
macro IS_WEAKMAP(arg) = (%_ClassOf(arg) === 'WeakMap');
107-
macro IS_WEAKSET(arg) = (%_ClassOf(arg) === 'WeakSet');
102+
macro IS_MAP_ITERATOR(arg) = (%_ClassOf(arg) === 'Map Iterator');
103+
macro IS_NULL(arg) = (arg === null);
104+
macro IS_NULL_OR_UNDEFINED(arg) = (arg == null);
105+
macro IS_NUMBER(arg) = (typeof(arg) === 'number');
108106
macro IS_NUMBER_WRAPPER(arg) = (%_ClassOf(arg) === 'Number');
109-
macro IS_STRING_WRAPPER(arg) = (%_ClassOf(arg) === 'String');
110-
macro IS_SYMBOL_WRAPPER(arg) = (%_ClassOf(arg) === 'Symbol');
111-
macro IS_BOOLEAN_WRAPPER(arg) = (%_ClassOf(arg) === 'Boolean');
112-
macro IS_ERROR(arg) = (%_ClassOf(arg) === 'Error');
107+
macro IS_OBJECT(arg) = (typeof(arg) === 'object');
108+
macro IS_PROXY(arg) = (%_IsJSProxy(arg));
109+
macro IS_REGEXP(arg) = (%_IsRegExp(arg));
113110
macro IS_SCRIPT(arg) = (%_ClassOf(arg) === 'Script');
114-
macro IS_GLOBAL(arg) = (%_ClassOf(arg) === 'global');
115-
macro IS_ARRAYBUFFER(arg) = (%_ClassOf(arg) === 'ArrayBuffer');
116-
macro IS_DATAVIEW(arg) = (%_ClassOf(arg) === 'DataView');
117-
macro IS_SHAREDARRAYBUFFER(arg) = (%_ClassOf(arg) === 'SharedArrayBuffer');
118-
macro IS_GENERATOR(arg) = (%_ClassOf(arg) === 'Generator');
111+
macro IS_SET(arg) = (%_ClassOf(arg) === 'Set');
119112
macro IS_SET_ITERATOR(arg) = (%_ClassOf(arg) === 'Set Iterator');
120-
macro IS_MAP_ITERATOR(arg) = (%_ClassOf(arg) === 'Map Iterator');
113+
macro IS_SHAREDARRAYBUFFER(arg) = (%_ClassOf(arg) === 'SharedArrayBuffer');
114+
macro IS_SIMD_VALUE(arg) = (%_IsSimdValue(arg));
115+
macro IS_STRING(arg) = (typeof(arg) === 'string');
116+
macro IS_STRING_WRAPPER(arg) = (%_ClassOf(arg) === 'String');
121117
macro IS_STRONG(arg) = (%IsStrong(arg));
118+
macro IS_SYMBOL(arg) = (typeof(arg) === 'symbol');
119+
macro IS_SYMBOL_WRAPPER(arg) = (%_ClassOf(arg) === 'Symbol');
120+
macro IS_UNDEFINED(arg) = (arg === (void 0));
121+
macro IS_WEAKMAP(arg) = (%_ClassOf(arg) === 'WeakMap');
122+
macro IS_WEAKSET(arg) = (%_ClassOf(arg) === 'WeakSet');
122123

123124
# Macro for ES queries of the type: "Type(O) is Object."
124125
macro IS_RECEIVER(arg) = (%_IsJSReceiver(arg));

src/js/messages.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ function FormatStackTrace(obj, raw_stack) {
841841

842842
function GetTypeName(receiver, requireConstructor) {
843843
if (IS_NULL_OR_UNDEFINED(receiver)) return null;
844-
if (%_IsJSProxy(receiver)) return "Proxy";
844+
if (IS_PROXY(receiver)) return "Proxy";
845845

846846
var constructor = %GetDataProperty(TO_OBJECT(receiver), "constructor");
847847
if (!IS_FUNCTION(constructor)) {

src/js/object-observe.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ function ObserverIsActive(observer, objectInfo) {
192192
function ObjectInfoGetOrCreate(object) {
193193
var objectInfo = ObjectInfoGet(object);
194194
if (IS_UNDEFINED(objectInfo)) {
195-
if (!%_IsJSProxy(object)) {
195+
if (!IS_PROXY(object)) {
196196
%SetIsObserved(object);
197197
}
198198
objectInfo = {

src/js/proxy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ utils.Import(function(from) {
2222

2323
function ProxyCreateRevocable(target, handler) {
2424
var p = new GlobalProxy(target, handler);
25-
return {proxy: p, revoke: () => %RevokeProxy(p)};
25+
return {proxy: p, revoke: () => %JSProxyRevoke(p)};
2626
}
2727

2828
// -------------------------------------------------------------------

src/js/v8natives.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,11 +486,11 @@ function CallTrap2(handler, name, defaultTrap, x, y) {
486486
// ObjectGetOwnPropertyDescriptor and delete this.
487487
function GetOwnPropertyJS(obj, v) {
488488
var p = TO_NAME(v);
489-
if (%_IsJSProxy(obj)) {
489+
if (IS_PROXY(obj)) {
490490
// TODO(rossberg): adjust once there is a story for symbols vs proxies.
491491
if (IS_SYMBOL(v)) return UNDEFINED;
492492

493-
var handler = %GetHandler(obj);
493+
var handler = %JSProxyGetHandler(obj);
494494
var descriptor = CallTrap1(
495495
handler, "getOwnPropertyDescriptor", UNDEFINED, p);
496496
if (IS_UNDEFINED(descriptor)) return descriptor;
@@ -524,7 +524,7 @@ function DefineProxyProperty(obj, p, attributes, should_throw) {
524524
// TODO(rossberg): adjust once there is a story for symbols vs proxies.
525525
if (IS_SYMBOL(p)) return false;
526526

527-
var handler = %GetHandler(obj);
527+
var handler = %JSProxyGetHandler(obj);
528528
var result = CallTrap2(handler, "defineProperty", UNDEFINED, p, attributes);
529529
if (!result) {
530530
if (should_throw) {
@@ -747,7 +747,7 @@ function DefineArrayProperty(obj, p, desc, should_throw) {
747747

748748
// ES5 section 8.12.9, ES5 section 15.4.5.1 and Harmony proxies.
749749
function DefineOwnProperty(obj, p, desc, should_throw) {
750-
if (%_IsJSProxy(obj)) {
750+
if (IS_PROXY(obj)) {
751751
// TODO(rossberg): adjust once there is a story for symbols vs proxies.
752752
if (IS_SYMBOL(p)) return false;
753753

src/runtime/runtime-proxy.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,23 @@ RUNTIME_FUNCTION(Runtime_IsJSProxy) {
149149
}
150150

151151

152-
RUNTIME_FUNCTION(Runtime_GetHandler) {
152+
RUNTIME_FUNCTION(Runtime_JSProxyGetHandler) {
153153
SealHandleScope shs(isolate);
154154
DCHECK(args.length() == 1);
155155
CONVERT_ARG_CHECKED(JSProxy, proxy, 0);
156156
return proxy->handler();
157157
}
158158

159159

160-
RUNTIME_FUNCTION(Runtime_RevokeProxy) {
160+
RUNTIME_FUNCTION(Runtime_JSProxyGetTarget) {
161+
SealHandleScope shs(isolate);
162+
DCHECK(args.length() == 1);
163+
CONVERT_ARG_CHECKED(JSProxy, proxy, 0);
164+
return proxy->target();
165+
}
166+
167+
168+
RUNTIME_FUNCTION(Runtime_JSProxyRevoke) {
161169
HandleScope scope(isolate);
162170
DCHECK(args.length() == 1);
163171
CONVERT_ARG_HANDLE_CHECKED(JSProxy, proxy, 0);

src/runtime/runtime.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,9 @@ namespace internal {
539539
F(IsJSProxy, 1, 1) \
540540
F(JSProxyCall, -1 /* >= 2 */, 1) \
541541
F(JSProxyConstruct, -1 /* >= 3 */, 1) \
542-
F(GetHandler, 1, 1) \
543-
F(RevokeProxy, 1, 1)
542+
F(JSProxyGetTarget, 1, 1) \
543+
F(JSProxyGetHandler, 1, 1) \
544+
F(JSProxyRevoke, 1, 1)
544545

545546
#define FOR_EACH_INTRINSIC_REGEXP(F) \
546547
F(StringReplaceGlobalRegExpWithString, 4, 1) \

0 commit comments

Comments
 (0)