-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
shell: add 'exit' builtin command #9705
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
41bbfcd
shell: add 'exit' builtin command
nektro d401cdf
remove loop here
nektro 2032936
[autofix.ci] apply automated fixes
autofix-ci[bot] a56ca42
Merge branch 'main' into nektro-patch-25869
Jarred-Sumner a0b8596
Merge branch 'main' into nektro-patch-25869
nektro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
//! use undefined memory. | ||
const std = @import("std"); | ||
const builtin = @import("builtin"); | ||
const string = []const u8; | ||
const bun = @import("root").bun; | ||
const os = std.os; | ||
const Arena = std.heap.ArenaAllocator; | ||
|
@@ -504,6 +505,7 @@ pub const CowEnvMap = Cow(EnvMap, struct { | |
|
||
pub const EnvMap = struct { | ||
map: MapType, | ||
|
||
pub const Iterator = MapType.Iterator; | ||
|
||
const MapType = std.ArrayHashMap(EnvStr, EnvStr, struct { | ||
|
@@ -3909,7 +3911,9 @@ pub const Interpreter = struct { | |
args_slice: ?[]const [:0]const u8 = null, | ||
cwd: bun.FileDescriptor, | ||
|
||
impl: union(Kind) { | ||
impl: RealImpl, | ||
|
||
const RealImpl = union(Kind) { | ||
cat: Cat, | ||
touch: Touch, | ||
mkdir: Mkdir, | ||
|
@@ -3921,7 +3925,8 @@ pub const Interpreter = struct { | |
rm: Rm, | ||
mv: Mv, | ||
ls: Ls, | ||
}, | ||
exit: Exit, | ||
}; | ||
|
||
const Result = @import("../result.zig").Result; | ||
|
||
|
@@ -3937,6 +3942,7 @@ pub const Interpreter = struct { | |
rm, | ||
mv, | ||
ls, | ||
exit, | ||
|
||
pub fn parentType(this: Kind) type { | ||
_ = this; | ||
|
@@ -3955,6 +3961,7 @@ pub const Interpreter = struct { | |
.rm => "usage: rm [-f | -i] [-dIPRrvWx] file ...\n unlink [--] file\n", | ||
.mv => "usage: mv [-f | -i | -n] [-hv] source target\n mv [-f | -i | -n] [-v] source ... directory\n", | ||
.ls => "usage: ls [-@ABCFGHILOPRSTUWabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]\n", | ||
.exit => "usage: exit [n]\n", | ||
}; | ||
} | ||
|
||
|
@@ -3971,6 +3978,7 @@ pub const Interpreter = struct { | |
.rm => "rm", | ||
.mv => "mv", | ||
.ls => "ls", | ||
.exit => "exit", | ||
}; | ||
} | ||
|
||
|
@@ -4132,6 +4140,7 @@ pub const Interpreter = struct { | |
.pwd => this.callImplWithType(Pwd, Ret, "pwd", field, args_), | ||
.mv => this.callImplWithType(Mv, Ret, "mv", field, args_), | ||
.ls => this.callImplWithType(Ls, Ret, "ls", field, args_), | ||
.exit => this.callImplWithType(Exit, Ret, "exit", field, args_), | ||
}; | ||
} | ||
|
||
|
@@ -4201,26 +4210,6 @@ pub const Interpreter = struct { | |
}; | ||
|
||
switch (kind) { | ||
.cat => { | ||
cmd.exec.bltn.impl = .{ | ||
.cat = Cat{ .bltn = &cmd.exec.bltn }, | ||
}; | ||
}, | ||
.touch => { | ||
cmd.exec.bltn.impl = .{ | ||
.touch = Touch{ .bltn = &cmd.exec.bltn }, | ||
}; | ||
}, | ||
.mkdir => { | ||
cmd.exec.bltn.impl = .{ | ||
.mkdir = Mkdir{ .bltn = &cmd.exec.bltn }, | ||
}; | ||
}, | ||
.@"export" => { | ||
cmd.exec.bltn.impl = .{ | ||
.@"export" = Export{ .bltn = &cmd.exec.bltn }, | ||
}; | ||
}, | ||
.rm => { | ||
cmd.exec.bltn.impl = .{ | ||
.rm = Rm{ | ||
|
@@ -4237,36 +4226,10 @@ pub const Interpreter = struct { | |
}, | ||
}; | ||
}, | ||
.cd => { | ||
cmd.exec.bltn.impl = .{ | ||
.cd = Cd{ | ||
.bltn = &cmd.exec.bltn, | ||
}, | ||
}; | ||
}, | ||
.which => { | ||
cmd.exec.bltn.impl = .{ | ||
.which = Which{ | ||
.bltn = &cmd.exec.bltn, | ||
}, | ||
}; | ||
}, | ||
.pwd => { | ||
cmd.exec.bltn.impl = .{ | ||
.pwd = Pwd{ .bltn = &cmd.exec.bltn }, | ||
}; | ||
}, | ||
.mv => { | ||
cmd.exec.bltn.impl = .{ | ||
.mv = Mv{ .bltn = &cmd.exec.bltn }, | ||
}; | ||
}, | ||
.ls => { | ||
cmd.exec.bltn.impl = .{ | ||
.ls = Ls{ | ||
.bltn = &cmd.exec.bltn, | ||
}, | ||
}; | ||
inline else => |tag| { | ||
cmd.exec.bltn.impl = @unionInit(RealImpl, @tagName(tag), .{ | ||
.bltn = &cmd.exec.bltn, | ||
}); | ||
}, | ||
} | ||
|
||
|
@@ -8669,6 +8632,84 @@ pub const Interpreter = struct { | |
} | ||
}; | ||
}; | ||
|
||
pub const Exit = struct { | ||
bltn: *Builtin, | ||
state: enum { | ||
idle, | ||
waiting_io, | ||
err, | ||
done, | ||
} = .idle, | ||
|
||
pub fn start(this: *Exit) Maybe(void) { | ||
const args = this.bltn.argsSlice(); | ||
switch (args.len) { | ||
0 => { | ||
this.bltn.done(0); | ||
return Maybe(void).success; | ||
}, | ||
1 => { | ||
const first_arg = args[0][0..std.mem.len(args[0]) :0]; | ||
const exit_code: ExitCode = std.fmt.parseInt(u8, first_arg, 10) catch |err| switch (err) { | ||
error.Overflow => @intCast((std.fmt.parseInt(usize, first_arg, 10) catch return this.fail("exit: numeric argument required")) % 256), | ||
error.InvalidCharacter => return this.fail("exit: numeric argument required"), | ||
}; | ||
this.bltn.done(exit_code); | ||
return Maybe(void).success; | ||
}, | ||
else => { | ||
return this.fail("exit: too many arguments"); | ||
}, | ||
} | ||
} | ||
|
||
fn fail(this: *Exit, msg: string) Maybe(void) { | ||
if (this.bltn.stderr.needsIO()) { | ||
this.state = .waiting_io; | ||
this.bltn.stderr.enqueue(this, msg); | ||
return Maybe(void).success; | ||
} | ||
_ = this.bltn.writeNoIO(.stderr, msg); | ||
this.bltn.done(1); | ||
return Maybe(void).success; | ||
} | ||
|
||
pub fn next(this: *Exit) void { | ||
switch (this.state) { | ||
.idle => unreachable, | ||
.waiting_io => { | ||
return; | ||
}, | ||
.err => { | ||
this.bltn.done(1); | ||
return; | ||
}, | ||
.done => { | ||
this.bltn.done(1); | ||
return; | ||
}, | ||
} | ||
} | ||
|
||
pub fn onIOWriterChunk(this: *Exit, _: usize, maybe_e: ?JSC.SystemError) void { | ||
if (comptime bun.Environment.allow_assert) { | ||
std.debug.assert(this.state == .waiting_io); | ||
} | ||
if (maybe_e) |e| { | ||
defer e.deref(); | ||
this.state = .err; | ||
this.next(); | ||
return; | ||
} | ||
this.state = .done; | ||
this.next(); | ||
} | ||
|
||
pub fn deinit(this: *Exit) void { | ||
_ = this; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is the memory owned by the parent struct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. appears so checking all the other builtins |
||
} | ||
}; | ||
}; | ||
|
||
/// This type is reference counted, but deinitialization is queued onto the event loop | ||
|
@@ -9677,6 +9718,7 @@ pub const IOWriterChildPtr = struct { | |
Interpreter.Builtin.Touch, | ||
Interpreter.Builtin.Touch.ShellTouchOutputTask, | ||
Interpreter.Builtin.Cat, | ||
Interpreter.Builtin.Exit, | ||
shell.subproc.PipeReader.CapturedWriter, | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { $ } from "bun"; | ||
import { describe, test, expect } from "bun:test"; | ||
import { TestBuilder } from "../test_builder"; | ||
import { sortedShellOutput } from "../util"; | ||
import { join } from "path"; | ||
|
||
describe("exit", async () => { | ||
TestBuilder.command`exit`.exitCode(0).runAsTest("works"); | ||
|
||
describe("argument sets exit code", async () => { | ||
for (const arg of [0, 2, 11]) { | ||
TestBuilder.command`exit ${arg}`.exitCode(arg).runAsTest(`${arg}`); | ||
} | ||
}); | ||
|
||
TestBuilder.command`exit 3 5`.exitCode(1).stderr("exit: too many arguments").runAsTest("too many arguments"); | ||
|
||
TestBuilder.command`exit 62757836`.exitCode(204).runAsTest("exit code wraps u8"); | ||
|
||
// prettier-ignore | ||
TestBuilder.command`exit abc`.exitCode(1).stderr("exit: numeric argument required").runAsTest("numeric argument required"); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly not sure how I feel about this, it's definitely more concise than what we had previously but also makes it harder to understand what exactly is going on IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you elaborate on this a bit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
personally, i like it
it also encourages all the commands have the same interface