Skip to content

Commit

Permalink
wala
Browse files Browse the repository at this point in the history
  • Loading branch information
paperclover committed Jan 10, 2025
1 parent eadc1b4 commit 6e273f8
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 209 deletions.
148 changes: 73 additions & 75 deletions src/bun.js/ConsoleObject.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3262,90 +3262,88 @@ pub const Formatter = struct {
else
bun.asByteSlice(@tagName(arrayBuffer.typed_array_type)),
);
if (arrayBuffer.len == 0) {
if (slice.len == 0) {
writer.print("({d}) []", .{arrayBuffer.len});
return;
}
writer.print("({d}) [ ", .{arrayBuffer.len});

if (slice.len > 0) {
switch (jsType) {
.Int8Array => this.writeTypedArray(
*@TypeOf(writer),
&writer,
i8,
@as([]align(std.meta.alignment([]i8)) i8, @alignCast(std.mem.bytesAsSlice(i8, slice))),
enable_ansi_colors,
),
.Int16Array => this.writeTypedArray(
*@TypeOf(writer),
&writer,
i16,
@as([]align(std.meta.alignment([]i16)) i16, @alignCast(std.mem.bytesAsSlice(i16, slice))),
enable_ansi_colors,
),
.Uint16Array => this.writeTypedArray(
*@TypeOf(writer),
&writer,
u16,
@as([]align(std.meta.alignment([]u16)) u16, @alignCast(std.mem.bytesAsSlice(u16, slice))),
enable_ansi_colors,
),
.Int32Array => this.writeTypedArray(
*@TypeOf(writer),
&writer,
i32,
@as([]align(std.meta.alignment([]i32)) i32, @alignCast(std.mem.bytesAsSlice(i32, slice))),
enable_ansi_colors,
),
.Uint32Array => this.writeTypedArray(
*@TypeOf(writer),
&writer,
u32,
@as([]align(std.meta.alignment([]u32)) u32, @alignCast(std.mem.bytesAsSlice(u32, slice))),
enable_ansi_colors,
),
.Float16Array => this.writeTypedArray(
*@TypeOf(writer),
&writer,
f16,
@as([]align(std.meta.alignment([]f16)) f16, @alignCast(std.mem.bytesAsSlice(f16, slice))),
enable_ansi_colors,
),
.Float32Array => this.writeTypedArray(
*@TypeOf(writer),
&writer,
f32,
@as([]align(std.meta.alignment([]f32)) f32, @alignCast(std.mem.bytesAsSlice(f32, slice))),
enable_ansi_colors,
),
.Float64Array => this.writeTypedArray(
*@TypeOf(writer),
&writer,
f64,
@as([]align(std.meta.alignment([]f64)) f64, @alignCast(std.mem.bytesAsSlice(f64, slice))),
enable_ansi_colors,
),
.BigInt64Array => this.writeTypedArray(
switch (jsType) {
.Int8Array => this.writeTypedArray(
*@TypeOf(writer),
&writer,
i8,
@alignCast(std.mem.bytesAsSlice(i8, slice)),
enable_ansi_colors,
),
.Int16Array => this.writeTypedArray(
*@TypeOf(writer),
&writer,
i16,
@alignCast(std.mem.bytesAsSlice(i16, slice)),
enable_ansi_colors,
),
.Uint16Array => this.writeTypedArray(
*@TypeOf(writer),
&writer,
u16,
@alignCast(std.mem.bytesAsSlice(u16, slice)),
enable_ansi_colors,
),
.Int32Array => this.writeTypedArray(
*@TypeOf(writer),
&writer,
i32,
@alignCast(std.mem.bytesAsSlice(i32, slice)),
enable_ansi_colors,
),
.Uint32Array => this.writeTypedArray(
*@TypeOf(writer),
&writer,
u32,
@alignCast(std.mem.bytesAsSlice(u32, slice)),
enable_ansi_colors,
),
.Float16Array => this.writeTypedArray(
*@TypeOf(writer),
&writer,
f16,
@alignCast(std.mem.bytesAsSlice(f16, slice)),
enable_ansi_colors,
),
.Float32Array => this.writeTypedArray(
*@TypeOf(writer),
&writer,
f32,
@alignCast(std.mem.bytesAsSlice(f32, slice)),
enable_ansi_colors,
),
.Float64Array => this.writeTypedArray(
*@TypeOf(writer),
&writer,
f64,
@alignCast(std.mem.bytesAsSlice(f64, slice)),
enable_ansi_colors,
),
.BigInt64Array => this.writeTypedArray(
*@TypeOf(writer),
&writer,
i64,
@alignCast(std.mem.bytesAsSlice(i64, slice)),
enable_ansi_colors,
),
.BigUint64Array => {
this.writeTypedArray(
*@TypeOf(writer),
&writer,
i64,
@as([]align(std.meta.alignment([]i64)) i64, @alignCast(std.mem.bytesAsSlice(i64, slice))),
u64,
@as([]align(std.meta.alignment([]u64)) u64, @alignCast(std.mem.bytesAsSlice(u64, slice))),
enable_ansi_colors,
),
.BigUint64Array => {
this.writeTypedArray(
*@TypeOf(writer),
&writer,
u64,
@as([]align(std.meta.alignment([]u64)) u64, @alignCast(std.mem.bytesAsSlice(u64, slice))),
enable_ansi_colors,
);
},
);
},

// Uint8Array, Uint8ClampedArray, DataView, ArrayBuffer
else => this.writeTypedArray(*@TypeOf(writer), &writer, u8, slice, enable_ansi_colors),
}
// Uint8Array, Uint8ClampedArray, DataView, ArrayBuffer
else => this.writeTypedArray(*@TypeOf(writer), &writer, u8, slice, enable_ansi_colors),
}

writer.writeAll(" ]");
Expand Down
12 changes: 8 additions & 4 deletions src/bun.js/node/node_fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2530,7 +2530,7 @@ pub const Arguments = struct {
arguments.eat();
if (arg_position.isNumber() or arg_position.isBigInt()) {
const num = arg_position.to(i52);
if (num > 0)
if (num >= 0)
args.position = @as(ReadPosition, @intCast(num));
}
}
Expand All @@ -2551,7 +2551,7 @@ pub const Arguments = struct {
if (try current.getTruthy(ctx, "position")) |num| {
if (num.isNumber() or num.isBigInt()) {
const n = num.to(i52);
if (n > 0)
if (n >= 0)
args.position = num.to(i52);
}
}
Expand Down Expand Up @@ -3679,7 +3679,11 @@ pub const NodeFS = struct {
defer req.deinit();
const rc = uv.uv_fs_futime(uv.Loop.get(), &req, bun.uvfdcast(args.fd), args.mtime, args.atime, null);
return if (rc.errno()) |e|
Maybe(Return.Futimes){ .err = .{ .errno = e, .syscall = .futime } }
.{ .err = .{
.errno = e,
.syscall = .futime,
.fd = args.fd,
} }
else
Maybe(Return.Futimes).success;
}
Expand All @@ -3689,7 +3693,7 @@ pub const NodeFS = struct {
args.atime,
};

return if (Maybe(Return.Futimes).errnoSys(system.futimens(args.fd.int(), &times), .futime)) |err|
return if (Maybe(Return.Futimes).errnoSysFd(system.futimens(args.fd.int(), &times), .futime, args.fd)) |err|
err
else
Maybe(Return.Futimes).success;
Expand Down
74 changes: 50 additions & 24 deletions src/bun.js/node/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1230,42 +1230,68 @@ pub fn fileDescriptorFromJS(ctx: JSC.C.JSContextRef, value: JSC.JSValue) bun.JSE
null;
}

// Equivalent to `toUnixTimestamp`
//
// Node.js docs:
// > Values can be either numbers representing Unix epoch time in seconds, Dates, or a numeric string like '123456789.0'.
// > If the value can not be converted to a number, or is NaN, Infinity, or -Infinity, an Error will be thrown.
pub fn timeLikeFromJS(globalObject: *JSC.JSGlobalObject, value: JSC.JSValue) ?TimeLike {
if (value.jsType() == .JSDate) {
const milliseconds = value.getUnixTimestamp();
if (!std.math.isFinite(milliseconds)) {
return null;
}

if (comptime Environment.isWindows) {
return milliseconds / 1000.0;
// Number is most common case
if (value.isNumber()) {
const seconds = value.asNumber();
if (std.math.isFinite(seconds)) {
if (seconds < 0) {
return timeLikeFromNow();
}
return timeLikeFromSeconds(seconds);
}

return TimeLike{
.tv_sec = @intFromFloat(@divFloor(milliseconds, std.time.ms_per_s)),
.tv_nsec = @intFromFloat(@mod(milliseconds, std.time.ms_per_s) * std.time.ns_per_ms),
};
}

if (!value.isNumber() and !value.isString()) {
return null;
} else switch (value.jsType()) {
.JSDate => {
const milliseconds = value.getUnixTimestamp();
if (std.math.isFinite(milliseconds)) {
return timeLikeFromMilliseconds(milliseconds);
}
},
.String => {
const seconds = value.coerceToDouble(globalObject);
if (std.math.isFinite(seconds)) {
return timeLikeFromSeconds(seconds);
}
},
else => {},
}
return null;
}

const seconds = value.coerce(f64, globalObject);
if (!std.math.isFinite(seconds)) {
return null;
fn timeLikeFromSeconds(seconds: f64) TimeLike {
if (Environment.isWindows) {
return seconds;
}
return .{
.tv_sec = @intFromFloat(seconds),
.tv_nsec = @intFromFloat(@mod(seconds, 1) * std.time.ns_per_s),
};
}

if (comptime Environment.isWindows) {
return seconds;
fn timeLikeFromMilliseconds(milliseconds: f64) TimeLike {
if (Environment.isWindows) {
return milliseconds / 1000.0;
}
return .{
.tv_sec = @intFromFloat(@divFloor(milliseconds, std.time.ms_per_s)),
.tv_nsec = @intFromFloat(@mod(milliseconds, std.time.ms_per_s) * std.time.ns_per_ms),
};
}

return TimeLike{
.tv_sec = @intFromFloat(seconds),
.tv_nsec = @intFromFloat(@mod(seconds, 1.0) * std.time.ns_per_s),
fn timeLikeFromNow() TimeLike {
const nanos = std.time.nanoTimestamp();
if (Environment.isWindows) {
return @as(TimeLike, nanos) / std.time.ns_per_s;
}
return .{
.tv_sec = @truncate(@divFloor(nanos, std.time.ns_per_s)),
.tv_nsec = @truncate(@mod(nanos, std.time.ns_per_s)),
};
}

Expand Down
29 changes: 1 addition & 28 deletions src/js/internal/promisify.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const kCustomPromisifiedSymbol = Symbol.for("nodejs.util.promisify.custom");
const kCustomPromisifyArgsSymbol = Symbol("customPromisifyArgs");

function defineCustomPromisify(target, callback) {
Object.defineProperty(target, kCustomPromisifiedSymbol, {
Expand All @@ -11,15 +10,6 @@ function defineCustomPromisify(target, callback) {
return callback;
}

function defineCustomPromisifyArgs(target, args) {
Object.defineProperty(target, kCustomPromisifyArgsSymbol, {
__proto__: null,
value: args,
enumerable: false,
});
return args;
}

var promisify = function promisify(original) {
if (typeof original !== "function") throw new TypeError('The "original" argument must be of type Function');
const custom = original[kCustomPromisifiedSymbol];
Expand All @@ -31,8 +21,6 @@ var promisify = function promisify(original) {
return defineCustomPromisify(custom, custom);
}

const callbackArgs = original[kCustomPromisifyArgsSymbol];

function fn(...originalArgs) {
const { promise, resolve, reject } = Promise.withResolvers();
try {
Expand All @@ -43,21 +31,7 @@ var promisify = function promisify(original) {
return reject(err);
}

if (callbackArgs !== undefined && values.length > 0) {
if (!Array.isArray(callbackArgs)) {
throw new TypeError('The "customPromisifyArgs" argument must be of type Array');
}
if (callbackArgs.length !== values.length) {
throw new Error("Mismatched length in promisify callback args");
}
const result = {};
for (let i = 0; i < callbackArgs.length; i++) {
result[callbackArgs[i]] = values[i];
}
resolve(result);
} else {
resolve(values[0]);
}
resolve(values[0]);
},
]);
} catch (err) {
Expand Down Expand Up @@ -100,6 +74,5 @@ promisify.custom = kCustomPromisifiedSymbol;

export default {
defineCustomPromisify,
defineCustomPromisifyArgs,
promisify,
};
Loading

0 comments on commit 6e273f8

Please sign in to comment.