Skip to content

Navigation Menu

Sign in
Sign up

Commit 44f7ddd

Browse files
papercloverautofix-ci[bot]
andauthored
fix: ConsoleObject handles proxy better (oven-sh#9042)
* fix: ConsoleObject handles proxy better * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 048ae7c commit 44f7ddd

6 files changed

Lines changed: 98 additions & 45 deletions

File tree

‎src/bun.js/ConsoleObject.zig‎

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -915,14 +915,16 @@ pub const Formatter = struct {
915915
JSON,
916916
toJSON,
917917
NativeCode,
918-
ArrayBuffer,
919918

920919
JSX,
921920
Event,
922921

923922
GetterSetter,
924923
CustomGetterSetter,
925924

925+
Proxy,
926+
RevokedProxy,
927+
926928
pub fn isPrimitive(this: Tag) bool {
927929
return switch (this) {
928930
.String,
@@ -971,15 +973,20 @@ pub const Formatter = struct {
971973
JSON: void,
972974
toJSON: void,
973975
NativeCode: void,
974-
ArrayBuffer: void,
975976
JSX: void,
976977
Event: void,
977978
GetterSetter: void,
978979
CustomGetterSetter: void,
980+
Proxy: void,
981+
RevokedProxy: void,
979982

980983
pub fn isPrimitive(this: @This()) bool {
981984
return @as(Tag, this).isPrimitive();
982985
}
986+
987+
pub fn tag(this: @This()) Tag {
988+
return @as(Tag, this);
989+
}
983990
},
984991
cell: JSValue.JSType = JSValue.JSType.Cell,
985992
};
@@ -1126,10 +1133,17 @@ pub const Formatter = struct {
11261133

11271134
.Object,
11281135
.FinalObject,
1129-
.ProxyObject,
11301136
.ModuleNamespaceObject,
11311137
=> .Object,
11321138

1139+
.ProxyObject => tag: {
1140+
const handler = value.getProxyInternalField(.handler);
1141+
if (handler == .zero or handler == .undefined or handler == .null) {
1142+
break :tag .RevokedProxy;
1143+
}
1144+
break :tag .Proxy;
1145+
},
1146+
11331147
.GlobalObject => if (!opts.hide_global)
11341148
.Object
11351149
else
@@ -2877,7 +2891,20 @@ pub const Formatter = struct {
28772891

28782892
writer.writeAll(" ]");
28792893
},
2880-
else => {},
2894+
.RevokedProxy => {
2895+
this.addForNewLine("<Revoked Proxy>".len);
2896+
writer.print(comptime Output.prettyFmt("<r><cyan>\\<Revoked Proxy\\><r>", enable_ansi_colors), .{});
2897+
},
2898+
.Proxy => {
2899+
const target = value.getProxyInternalField(.target);
2900+
if (Environment.allow_assert) {
2901+
// Proxy does not allow non-objects here.
2902+
std.debug.assert(target.isCell());
2903+
}
2904+
// TODO: if (options.showProxy), print like `Proxy { target: ..., handlers: ... }`
2905+
// this is default off so it is not used.
2906+
this.format(ConsoleObject.Formatter.Tag.get(target, this.globalThis), Writer, writer_, target, this.globalThis, enable_ansi_colors);
2907+
},
28812908
}
28822909
}
28832910

@@ -2908,9 +2935,6 @@ pub const Formatter = struct {
29082935
}
29092936

29102937
pub fn format(this: *ConsoleObject.Formatter, result: Tag.Result, comptime Writer: type, writer: Writer, value: JSValue, globalThis: *JSGlobalObject, comptime enable_ansi_colors: bool) void {
2911-
if (comptime is_bindgen) {
2912-
return;
2913-
}
29142938
const prevGlobalThis = this.globalThis;
29152939
defer this.globalThis = prevGlobalThis;
29162940
this.globalThis = globalThis;
@@ -2919,44 +2943,11 @@ pub const Formatter = struct {
29192943
// comptime var so we have to repeat it here. The rationale there is
29202944
// it _should_ limit the stack usage because each version of the
29212945
// function will be relatively small
2922-
switch (result.tag) {
2923-
.StringPossiblyFormatted => this.printAs(.StringPossiblyFormatted, Writer, writer, value, result.cell, enable_ansi_colors),
2924-
.String => this.printAs(.String, Writer, writer, value, result.cell, enable_ansi_colors),
2925-
.Undefined => this.printAs(.Undefined, Writer, writer, value, result.cell, enable_ansi_colors),
2926-
.Double => this.printAs(.Double, Writer, writer, value, result.cell, enable_ansi_colors),
2927-
.Integer => this.printAs(.Integer, Writer, writer, value, result.cell, enable_ansi_colors),
2928-
.Null => this.printAs(.Null, Writer, writer, value, result.cell, enable_ansi_colors),
2929-
.Boolean => this.printAs(.Boolean, Writer, writer, value, result.cell, enable_ansi_colors),
2930-
.Array => this.printAs(.Array, Writer, writer, value, result.cell, enable_ansi_colors),
2931-
.Object => this.printAs(.Object, Writer, writer, value, result.cell, enable_ansi_colors),
2932-
.Function => this.printAs(.Function, Writer, writer, value, result.cell, enable_ansi_colors),
2933-
.Class => this.printAs(.Class, Writer, writer, value, result.cell, enable_ansi_colors),
2934-
.Error => this.printAs(.Error, Writer, writer, value, result.cell, enable_ansi_colors),
2935-
.ArrayBuffer, .TypedArray => this.printAs(.TypedArray, Writer, writer, value, result.cell, enable_ansi_colors),
2936-
.Map => this.printAs(.Map, Writer, writer, value, result.cell, enable_ansi_colors),
2937-
.MapIterator => this.printAs(.MapIterator, Writer, writer, value, result.cell, enable_ansi_colors),
2938-
.SetIterator => this.printAs(.SetIterator, Writer, writer, value, result.cell, enable_ansi_colors),
2939-
.Set => this.printAs(.Set, Writer, writer, value, result.cell, enable_ansi_colors),
2940-
.Symbol => this.printAs(.Symbol, Writer, writer, value, result.cell, enable_ansi_colors),
2941-
.BigInt => this.printAs(.BigInt, Writer, writer, value, result.cell, enable_ansi_colors),
2942-
.GlobalObject => this.printAs(.GlobalObject, Writer, writer, value, result.cell, enable_ansi_colors),
2943-
.Private => this.printAs(.Private, Writer, writer, value, result.cell, enable_ansi_colors),
2944-
.Promise => this.printAs(.Promise, Writer, writer, value, result.cell, enable_ansi_colors),
2945-
2946-
// Call JSON.stringify on the value
2947-
.JSON => this.printAs(.JSON, Writer, writer, value, result.cell, enable_ansi_colors),
2948-
2949-
// Call value.toJSON() and print as an object
2950-
.toJSON => this.printAs(.toJSON, Writer, writer, value, result.cell, enable_ansi_colors),
2951-
2952-
.NativeCode => this.printAs(.NativeCode, Writer, writer, value, result.cell, enable_ansi_colors),
2953-
.JSX => this.printAs(.JSX, Writer, writer, value, result.cell, enable_ansi_colors),
2954-
.Event => this.printAs(.Event, Writer, writer, value, result.cell, enable_ansi_colors),
2955-
.GetterSetter => this.printAs(.GetterSetter, Writer, writer, value, result.cell, enable_ansi_colors),
2956-
.CustomGetterSetter => this.printAs(.CustomGetterSetter, Writer, writer, value, result.cell, enable_ansi_colors),
2957-
2958-
.CustomFormattedObject => |callback| {
2959-
this.custom_formatted_object = callback;
2946+
switch (result.tag.tag()) {
2947+
inline else => |tag| this.printAs(tag, Writer, writer, value, result.cell, enable_ansi_colors),
2948+
2949+
.CustomFormattedObject => {
2950+
this.custom_formatted_object = result.tag.CustomFormattedObject;
29602951
this.printAs(.CustomFormattedObject, Writer, writer, value, result.cell, enable_ansi_colors);
29612952
},
29622953
}

‎src/bun.js/bindings/bindings.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "JavaScriptCore/JSObject.h"
3333
#include "JavaScriptCore/JSSet.h"
3434
#include "JavaScriptCore/JSString.h"
35+
#include "JavaScriptCore/ProxyObject.h"
3536
#include "JavaScriptCore/Microtask.h"
3637
#include "JavaScriptCore/ObjectConstructor.h"
3738
#include "JavaScriptCore/ParserError.h"
@@ -5433,3 +5434,8 @@ CPP_DECL bool JSC__CustomGetterSetter__isSetterNull(JSC__CustomGetterSetter* get
54335434
{
54345435
return gettersetter->setter() == nullptr;
54355436
}
5437+
5438+
CPP_DECL JSC__JSValue Bun__ProxyObject__getInternalField(JSC__JSValue value, uint32_t id)
5439+
{
5440+
return JSValue::encode(jsCast<ProxyObject*>(JSValue::decode(value))->internalField((ProxyObject::Field)id).get());
5441+
}

‎src/bun.js/bindings/bindings.zig‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5310,6 +5310,18 @@ pub const JSValue = enum(JSValueReprInt) {
53105310
else
53115311
null;
53125312
}
5313+
5314+
extern fn Bun__ProxyObject__getInternalField(this: JSValue, field: ProxyInternalField) JSValue;
5315+
5316+
const ProxyInternalField = enum(u32) {
5317+
target = 0,
5318+
handler = 1,
5319+
};
5320+
5321+
/// Asserts `this` is a proxy
5322+
pub fn getProxyInternalField(this: JSValue, field: ProxyInternalField) JSValue {
5323+
return Bun__ProxyObject__getInternalField(this, field);
5324+
}
53135325
};
53145326

53155327
extern "c" fn AsyncContextFrame__withAsyncContextIfNeeded(global: *JSGlobalObject, callback: JSValue) JSValue;

‎src/js/builtins/ProcessObjectInternals.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,14 @@ export function windowsEnv(internalEnv: InternalEnvMap, envMapList: Array<string
353353
//
354354
// it throws "Cannot convert a Symbol value to a string"
355355

356+
(internalEnv as any)[Bun.inspect.custom] = () => {
357+
let o = {};
358+
for (let k of envMapList) {
359+
o[k] = internalEnv[k.toUpperCase()];
360+
}
361+
return o;
362+
};
363+
356364
return new Proxy(internalEnv, {
357365
get(_, p) {
358366
return typeof p === "string" ? Reflect.get(internalEnv, p.toUpperCase()) : undefined;

‎test/js/web/console/console-log.expected.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,8 @@ myCustomName {
244244
{
245245
"": "",
246246
}
247+
{
248+
hello: 2,
249+
}
250+
<Revoked Proxy>
251+
custom inspect

‎test/js/web/console/console-log.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,34 @@ console.log(hole([1, 2, 3], 0));
173173
// It appears to not be set and I don't know why.
174174

175175
console.log({ "": "" });
176+
177+
{
178+
// proxy
179+
const proxy = Proxy.revocable(
180+
{ hello: 2 },
181+
{
182+
get(target, prop, receiver) {
183+
console.log("FAILED: GET", prop);
184+
return Reflect.get(target, prop, receiver);
185+
},
186+
set(target, prop, value, receiver) {
187+
console.log("FAILED: SET", prop, value);
188+
return Reflect.set(target, prop, value, receiver);
189+
},
190+
},
191+
);
192+
console.log(proxy.proxy);
193+
proxy.revoke();
194+
console.log(proxy.proxy);
195+
}
196+
197+
{
198+
// proxy custom inspect
199+
const proxy = new Proxy(
200+
{
201+
[Bun.inspect.custom]: () => "custom inspect",
202+
},
203+
{},
204+
);
205+
console.log(proxy);
206+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /