aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-02-17 22:00:44 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-02-17 22:00:44 -0800
commitfa10631c2f89b68545095acbe4fb9c82573e75f6 (patch)
treeecc5730a5cb65335560d936516e9a85e4e155706
parent1106c8e2f26ec5f521e33b132594b0741e9caaa3 (diff)
downloadbun-fa10631c2f89b68545095acbe4fb9c82573e75f6.tar.gz
bun-fa10631c2f89b68545095acbe4fb9c82573e75f6.tar.zst
bun-fa10631c2f89b68545095acbe4fb9c82573e75f6.zip
use map
-rw-r--r--src/bun.js/node/types.zig91
1 files changed, 70 insertions, 21 deletions
diff --git a/src/bun.js/node/types.zig b/src/bun.js/node/types.zig
index fbae5b3e1..591af3f09 100644
--- a/src/bun.js/node/types.zig
+++ b/src/bun.js/node/types.zig
@@ -964,11 +964,63 @@ 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, default: FileSystemFlags, exception: JSC.C.ExceptionRef) ?FileSystemFlags {
- if (val.isUndefinedOrNull()) {
- return default;
- }
+ const map = bun.ComptimeStringMap(Mode, .{
+ .{ "r", O_RDONLY },
+ .{ "rs", O_RDONLY | O_SYNC },
+ .{ "sr", O_RDONLY | O_SYNC },
+ .{ "r+", O_RDWR },
+ .{ "rs+", O_RDWR | O_SYNC },
+ .{ "sr+", O_RDWR | O_SYNC },
+
+ .{ "R", O_RDONLY },
+ .{ "RS", O_RDONLY | O_SYNC },
+ .{ "SR", O_RDONLY | O_SYNC },
+ .{ "R+", O_RDWR },
+ .{ "RS+", O_RDWR | O_SYNC },
+ .{ "SR+", O_RDWR | O_SYNC },
+
+ .{ "w", O_TRUNC | O_CREAT | O_WRONLY },
+ .{ "wx", O_TRUNC | O_CREAT | O_WRONLY | O_EXCL },
+ .{ "xw", O_TRUNC | O_CREAT | O_WRONLY | O_EXCL },
+
+ .{ "W", O_TRUNC | O_CREAT | O_WRONLY },
+ .{ "WX", O_TRUNC | O_CREAT | O_WRONLY | O_EXCL },
+ .{ "XW", O_TRUNC | O_CREAT | O_WRONLY | O_EXCL },
+
+ .{ "w+", O_TRUNC | O_CREAT | O_RDWR },
+ .{ "wx+", O_TRUNC | O_CREAT | O_RDWR | O_EXCL },
+ .{ "xw+", O_TRUNC | O_CREAT | O_RDWR | O_EXCL },
+
+ .{ "W+", O_TRUNC | O_CREAT | O_RDWR },
+ .{ "WX+", O_TRUNC | O_CREAT | O_RDWR | O_EXCL },
+ .{ "XW+", O_TRUNC | O_CREAT | O_RDWR | O_EXCL },
+
+ .{ "a", O_APPEND | O_CREAT | O_WRONLY },
+ .{ "ax", O_APPEND | O_CREAT | O_WRONLY | O_EXCL },
+ .{ "xa", O_APPEND | O_CREAT | O_WRONLY | O_EXCL },
+ .{ "as", O_APPEND | O_CREAT | O_WRONLY | O_SYNC },
+ .{ "sa", O_APPEND | O_CREAT | O_WRONLY | O_SYNC },
+
+ .{ "A", O_APPEND | O_CREAT | O_WRONLY },
+ .{ "AX", O_APPEND | O_CREAT | O_WRONLY | O_EXCL },
+ .{ "XA", O_APPEND | O_CREAT | O_WRONLY | O_EXCL },
+ .{ "AS", O_APPEND | O_CREAT | O_WRONLY | O_SYNC },
+ .{ "SA", O_APPEND | O_CREAT | O_WRONLY | O_SYNC },
+
+ .{ "a+", O_APPEND | O_CREAT | O_RDWR },
+ .{ "ax+", O_APPEND | O_CREAT | O_RDWR | O_EXCL },
+ .{ "xa+", O_APPEND | O_CREAT | O_RDWR | O_EXCL },
+ .{ "as+", O_APPEND | O_CREAT | O_RDWR | O_SYNC },
+ .{ "sa+", O_APPEND | O_CREAT | O_RDWR | O_SYNC },
+
+ .{ "A+", O_APPEND | O_CREAT | O_RDWR },
+ .{ "AX+", O_APPEND | O_CREAT | O_RDWR | O_EXCL },
+ .{ "XA+", O_APPEND | O_CREAT | O_RDWR | O_EXCL },
+ .{ "AS+", O_APPEND | O_CREAT | O_RDWR | O_SYNC },
+ .{ "SA+", O_APPEND | O_CREAT | O_RDWR | O_SYNC },
+ });
+ pub fn fromJS(ctx: JSC.C.JSContextRef, val: JSC.JSValue, exception: JSC.C.ExceptionRef) ?FileSystemFlags {
if (val.isNumber()) {
const number = val.coerce(i32, ctx);
return @intToEnum(FileSystemFlags, @intCast(Mode, @max(number, 0)));
@@ -986,6 +1038,16 @@ pub const FileSystemFlags = enum(Mode) {
);
return null;
}
+ // it's definitely wrong when the string is super long
+ else if (str.len > 12) {
+ JSC.throwInvalidArguments(
+ "Invalid flag '{any}'. Learn more at https://nodejs.org/api/fs.html#fs_file_system_flags",
+ .{str},
+ ctx,
+ exception,
+ );
+ return null;
+ }
const flags = brk: {
switch (str.is16Bit()) {
@@ -1003,27 +1065,14 @@ pub const FileSystemFlags = enum(Mode) {
break :brk std.fmt.parseInt(Mode, chars, 10) catch null;
}
}
-
- var flags: Mode = 0;
- for (chars) |c| {
- flags |= switch (c) {
- 'R', 'r' => O_RDONLY,
- 'W', 'w' => O_WRONLY | O_CREAT | O_TRUNC,
- 'A', 'a' => O_WRONLY | O_CREAT | O_APPEND,
- '+' => O_RDWR,
- 'X', 'x' => O_WRONLY | O_CREAT | O_EXCL,
- 'S', 's' => O_SYNC,
- else => break :brk null,
- };
- }
-
- break :brk flags;
},
}
+
+ break :brk map.getWithEql(str, JSC.ZigString.eqlComptime);
} orelse {
JSC.throwInvalidArguments(
- "Invalid flag. Learn more at https://nodejs.org/api/fs.html#fs_file_system_flags",
- .{},
+ "Invalid flag '{any}'. Learn more at https://nodejs.org/api/fs.html#fs_file_system_flags",
+ .{str},
ctx,
exception,
);