Skip to content

Commit

Permalink
Handle holey arrays in console.log, don't omit length property, cle…
Browse files Browse the repository at this point in the history
…anup `logLevel` option (#7557)

* Add `fromJS` helper method to `ComptimeStringMap` and `logLevel` parsing better

* Handle holey arrays

* Update console-log.expected.txt

* More tests

* Add TODO

* More fixture

* [autofix.ci] apply automated fixes

* Update bindings.cpp

* Cleanup some spacing

* update

* Handle externals in `bun build` from package.json "imports"

* Prevent recursion in Web Worker

* Fix failing test

---------

Co-authored-by: Jarred Sumner <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 10, 2023
1 parent 7541f4c commit 800fb12
Show file tree
Hide file tree
Showing 16 changed files with 470 additions and 95 deletions.
46 changes: 21 additions & 25 deletions src/bun.js/api/JSTranspiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -513,37 +513,37 @@ fn transformOptionsFromJSC(globalObject: JSC.C.JSContextRef, temp_allocator: std
}
}

if (object.get(globalThis, "autoImportJSX")) |flag| {
transpiler.runtime.auto_import_jsx = flag.toBoolean();
if (object.getOptional(globalThis, "autoImportJSX", bool) catch return transpiler) |flag| {
transpiler.runtime.auto_import_jsx = flag;
}

if (object.get(globalThis, "allowBunRuntime")) |flag| {
transpiler.runtime.allow_runtime = flag.toBoolean();
if (object.getOptional(globalThis, "allowBunRuntime", bool) catch return transpiler) |flag| {
transpiler.runtime.allow_runtime = flag;
}

if (object.get(globalThis, "jsxOptimizationInline")) |flag| {
transpiler.runtime.jsx_optimization_inline = flag.toBoolean();
if (object.getOptional(globalThis, "jsxOptimizationInline", bool) catch return transpiler) |flag| {
transpiler.runtime.jsx_optimization_inline = flag;
}

if (object.get(globalThis, "jsxOptimizationHoist")) |flag| {
transpiler.runtime.jsx_optimization_hoist = flag.toBoolean();
if (object.getOptional(globalThis, "jsxOptimizationHoist", bool) catch return transpiler) |flag| {
transpiler.runtime.jsx_optimization_hoist = flag;

if (!transpiler.runtime.jsx_optimization_inline and transpiler.runtime.jsx_optimization_hoist) {
JSC.throwInvalidArguments("jsxOptimizationHoist requires jsxOptimizationInline", .{}, globalObject, exception);
return transpiler;
}
}

if (object.get(globalThis, "inline")) |flag| {
transpiler.runtime.inlining = flag.toBoolean();
if (object.getOptional(globalThis, "inline", bool) catch return transpiler) |flag| {
transpiler.runtime.inlining = flag;
}

if (object.get(globalThis, "minifyWhitespace")) |flag| {
transpiler.minify_whitespace = flag.toBoolean();
if (object.getOptional(globalThis, "minifyWhitespace", bool) catch return transpiler) |flag| {
transpiler.minify_whitespace = flag;
}

if (object.get(globalThis, "deadCodeElimination")) |flag| {
transpiler.dead_code_elimination = flag.toBoolean();
if (object.getOptional(globalThis, "deadCodeElimination", bool) catch return transpiler) |flag| {
transpiler.dead_code_elimination = flag;
}

if (object.getTruthy(globalThis, "minify")) |hot| {
Expand Down Expand Up @@ -575,8 +575,7 @@ fn transformOptionsFromJSC(globalObject: JSC.C.JSContextRef, temp_allocator: std
transpiler.transform.source_map = Api.SourceMapMode.inline_into_file;
}
} else {
var sourcemap = flag.toSlice(globalThis, allocator);
if (options.SourceMapOption.Map.get(sourcemap.slice())) |source| {
if (options.SourceMapOption.Map.fromJS(globalObject, flag)) |source| {
transpiler.transform.source_map = source.toAPI();
} else {
JSC.throwInvalidArguments("sourcemap must be one of \"inline\", \"external\", or \"none\"", .{}, globalObject, exception);
Expand All @@ -586,13 +585,13 @@ fn transformOptionsFromJSC(globalObject: JSC.C.JSContextRef, temp_allocator: std
}

var tree_shaking: ?bool = null;
if (object.get(globalThis, "treeShaking")) |treeShaking| {
tree_shaking = treeShaking.toBoolean();
if (object.getOptional(globalThis, "treeShaking", bool) catch return transpiler) |treeShaking| {
tree_shaking = treeShaking;
}

var trim_unused_imports: ?bool = null;
if (object.get(globalThis, "trimUnusedImports")) |trimUnusedImports| {
trim_unused_imports = trimUnusedImports.toBoolean();
if (object.getOptional(globalThis, "trimUnusedImports", bool) catch return transpiler) |trimUnusedImports| {
trim_unused_imports = trimUnusedImports;
}

if (object.getTruthy(globalThis, "exports")) |exports| {
Expand Down Expand Up @@ -723,11 +722,8 @@ fn transformOptionsFromJSC(globalObject: JSC.C.JSContextRef, temp_allocator: std
transpiler.runtime.replace_exports = replacements;
}

if (object.get(globalThis, "logLevel")) |logLevel| {
const logLevelString = logLevel.toBunString(globalThis);
defer logLevelString.deref();
const key = logLevelString.toOwnedSlice(allocator) catch bun.outOfMemory();
if (logger.Log.Level.Map.get(key)) |level| {
if (object.getTruthy(globalThis, "logLevel")) |logLevel| {
if (logger.Log.Level.Map.fromJS(globalObject, logLevel)) |level| {
transpiler.log.level = level;
} else {
JSC.throwInvalidArguments("logLevel must be one of \"verbose\", \"debug\", \"info\", \"warn\", or \"error\"", .{}, globalObject, exception);
Expand Down
10 changes: 7 additions & 3 deletions src/bun.js/bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2117,6 +2117,12 @@ JSC__JSValue JSC__JSObject__getIndex(JSC__JSValue jsValue, JSC__JSGlobalObject*
{
return JSC::JSValue::encode(JSC::JSValue::decode(jsValue).toObject(arg1)->getIndex(arg1, arg3));
}
JSC__JSValue JSC__JSValue__getDirectIndex(JSC__JSValue jsValue, JSC__JSGlobalObject* arg1,
uint32_t arg3)
{
JSC::JSObject* object = JSC::JSValue::decode(jsValue).getObject();
return JSC::JSValue::encode(object->getDirectIndex(arg1, arg3));
}
JSC__JSValue JSC__JSObject__getDirect(JSC__JSObject* arg0, JSC__JSGlobalObject* arg1,
const ZigString* arg2)
{
Expand Down Expand Up @@ -4610,7 +4616,6 @@ void JSC__JSValue__forEachProperty(JSC__JSValue JSValue0, JSC__JSGlobalObject* g
auto* prop = entry.key();

if (prop == vm.propertyNames->constructor
|| prop == vm.propertyNames->length
|| prop == vm.propertyNames->underscoreProto
|| prop == vm.propertyNames->toStringTagSymbol)
return true;
Expand Down Expand Up @@ -4698,8 +4703,7 @@ void JSC__JSValue__forEachProperty(JSC__JSValue JSValue0, JSC__JSGlobalObject* g
continue;

if ((slot.attributes() & PropertyAttribute::DontEnum) != 0) {
if (property == vm.propertyNames->length
|| property == vm.propertyNames->underscoreProto
if (property == vm.propertyNames->underscoreProto
|| property == vm.propertyNames->toStringTagSymbol)
continue;
}
Expand Down
23 changes: 21 additions & 2 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3415,6 +3415,11 @@ pub const JSValue = enum(JSValueReprInt) {
return JSC.JSObject.getIndex(this, globalThis, i);
}

extern fn JSC__JSValue__getDirectIndex(JSValue, *JSGlobalObject, u32) JSValue;
pub fn getDirectIndex(this: JSValue, globalThis: *JSGlobalObject, i: u32) JSValue {
return JSC__JSValue__getDirectIndex(this, globalThis, i);
}

const PropertyIteratorFn = *const fn (
globalObject_: *JSGlobalObject,
ctx_ptr: ?*anyopaque,
Expand Down Expand Up @@ -4728,14 +4733,28 @@ pub const JSValue = enum(JSValueReprInt) {
}

pub fn asBoolean(this: JSValue) bool {
if (comptime bun.Environment.allow_assert) {
if (!this.isBoolean()) {
Output.panic("Expected boolean but found {s}", .{@tagName(this.jsTypeLoose())});
}
}
return FFI.JSVALUE_TO_BOOL(.{ .asJSValue = this });
}

pub inline fn asInt52(this: JSValue) i64 {
if (comptime bun.Environment.allow_assert) {
std.debug.assert(this.isNumber());
}
return @as(i64, @intFromFloat(@max(@min(this.asDouble(), std.math.maxInt(i52)), std.math.minInt(i52))));
const double = this.asDouble();
if (std.math.isPositiveInf(double)) {
return std.math.maxInt(i52);
} else if (std.math.isNegativeInf(double)) {
return std.math.minInt(i52);
} else if (std.math.isNan(double)) {
return 0;
}

return @as(i64, @intFromFloat(@max(@min(double, std.math.maxInt(i52)), std.math.minInt(i52))));
}

pub fn toInt32(this: JSValue) i32 {
Expand All @@ -4744,7 +4763,7 @@ pub const JSValue = enum(JSValueReprInt) {
}

if (this.isNumber()) {
return @as(i32, @truncate(this.asInt52()));
return @as(i32, @intCast(@min(@max(this.asInt52(), std.math.minInt(i32)), std.math.maxInt(i32))));
}

if (comptime bun.Environment.allow_assert) {
Expand Down
Loading

0 comments on commit 800fb12

Please sign in to comment.