Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/bun.js/node/node_fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1027,14 +1027,14 @@ const Arguments = struct {

if (val.isObject()) {
if (val.getIfPropertyExists(ctx.ptr(), "flags")) |flags_| {
flags = FileSystemFlags.fromJS(ctx, flags_, exception) orelse flags;
flags = FileSystemFlags.fromJS(ctx, flags_, flags, exception) orelse flags;
}

if (val.getIfPropertyExists(ctx.ptr(), "mode")) |mode_| {
mode = JSC.Node.modeFromJS(ctx, mode_, exception) orelse mode;
}
} else if (!val.isEmpty()) {
flags = FileSystemFlags.fromJS(ctx, val, exception) orelse flags;
flags = FileSystemFlags.fromJS(ctx, val, flags, exception) orelse flags;

if (arguments.nextEat()) |next| {
mode = JSC.Node.modeFromJS(ctx, next, exception) orelse mode;
Expand Down Expand Up @@ -1508,7 +1508,7 @@ const Arguments = struct {
}

if (arg.getIfPropertyExists(ctx.ptr(), "flag")) |flag_| {
flag = FileSystemFlags.fromJS(ctx, flag_, exception) orelse {
flag = FileSystemFlags.fromJS(ctx, flag_, flag, exception) orelse {
if (exception.* == null) {
JSC.throwInvalidArguments(
"Invalid flag",
Expand Down Expand Up @@ -1615,7 +1615,7 @@ const Arguments = struct {
}

if (arg.getIfPropertyExists(ctx.ptr(), "flag")) |flag_| {
flag = FileSystemFlags.fromJS(ctx, flag_, exception) orelse {
flag = FileSystemFlags.fromJS(ctx, flag_, flag, exception) orelse {
if (exception.* == null) {
JSC.throwInvalidArguments(
"Invalid flag",
Expand Down Expand Up @@ -1785,7 +1785,7 @@ const Arguments = struct {
if (arguments.next()) |arg| {
arguments.eat();
if (arg.isString()) {
mode = FileSystemFlags.fromJS(ctx, arg, exception) orelse {
mode = FileSystemFlags.fromJS(ctx, arg, mode, exception) orelse {
if (exception.* == null) {
JSC.throwInvalidArguments(
"Invalid mode",
Expand Down
4 changes: 2 additions & 2 deletions src/bun.js/node/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -964,9 +964,9 @@ pub const FileSystemFlags = enum(Mode) {
const O_SYNC: Mode = 0;
const O_TRUNC: Mode = std.os.O.TRUNC;

pub fn fromJS(ctx: JSC.C.JSContextRef, val: JSC.JSValue, exception: JSC.C.ExceptionRef) ?FileSystemFlags {
pub fn fromJS(ctx: JSC.C.JSContextRef, val: JSC.JSValue, default: FileSystemFlags, exception: JSC.C.ExceptionRef) ?FileSystemFlags {
if (val.isUndefinedOrNull()) {
return @intToEnum(FileSystemFlags, O_RDONLY);
return default;
}

if (val.isNumber()) {
Expand Down
7 changes: 7 additions & 0 deletions test/bun.js/fs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -987,3 +987,10 @@ it("fs.Dirent", () => {
it("fs.Stats", () => {
expect(Stats).toBeDefined();
});

it("repro 1516: can use undefined/null to specify default flag", () => {
const path = "/tmp/repro_1516.txt";
writeFileSync(path, "b", { flag: undefined });
expect(readFileSync(path, { encoding: "utf8", flag: null })).toBe("b");
rmSync(path);
});