From a2ea8fb53ea5cce02744a7dd0a5beeea6c79cd24 Mon Sep 17 00:00:00 2001 From: Luna Date: Tue, 2 Jun 2020 17:12:34 -0300 Subject: [PATCH 1/4] make args live through lifetime of runner we just assign cloned runners the args of the parent runner and don't free the parent's, maintaining okay memory usage. --- src/runner.zig | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/runner.zig b/src/runner.zig index 234e59e..3ca9bc9 100644 --- a/src/runner.zig +++ b/src/runner.zig @@ -26,10 +26,13 @@ pub const Runner = struct { /// If the runner is in REPL mode repl: bool = false, + args: [][]u8, + pub fn init(allocator: *std.mem.Allocator, repl: bool) Runner { return Runner{ .allocator = allocator, .repl = repl, + .args = std.process.argsAlloc(allocator) catch unreachable, }; } @@ -41,7 +44,11 @@ pub const Runner = struct { pub fn clone(self: *Runner) !Runner { var cloned_image = if (self.image) |image| try image.clone() else null; - return Runner{ .allocator = self.allocator, .image = cloned_image }; + return Runner{ + .allocator = self.allocator, + .image = cloned_image, + .args = self.args, + }; } fn resolveArg(self: *Runner, load_path: []const u8) ![]const u8 { @@ -57,14 +64,12 @@ pub const Runner = struct { // ':0' should ALWAYS point to the image. if (self.repl) index += 3 else index += 2; - var args = try std.process.argsAlloc(self.allocator); - defer std.process.argsFree(self.allocator, args); - std.debug.warn("ARGS!! {} \n", .{args.len}); - for (args) |arg, idx| { + std.debug.warn("ARGS!! {} \n", .{self.args.len}); + for (self.args) |arg, idx| { std.debug.warn("arg{} = {}\n", .{ idx, arg }); } - std.debug.warn("fetch arg idx={}, val={}\n", .{ index, args[index] }); - return args[index]; + std.debug.warn("fetch arg idx={}, val={}\n", .{ index, self.args[index] }); + return self.args[index]; } else { return load_path; } From 1b59705eae99d84f9c189a35f87114d7c4fe7f55 Mon Sep 17 00:00:00 2001 From: Luna Date: Tue, 2 Jun 2020 17:15:50 -0300 Subject: [PATCH 2/4] cloned runners inherit repl flag from parent --- src/runner.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/src/runner.zig b/src/runner.zig index 3ca9bc9..66a77c8 100644 --- a/src/runner.zig +++ b/src/runner.zig @@ -47,6 +47,7 @@ pub const Runner = struct { return Runner{ .allocator = self.allocator, .image = cloned_image, + .repl = self.repl, .args = self.args, }; } From af0ea574e1d8c08e788b72ab8a2584b85e32fc68 Mon Sep 17 00:00:00 2001 From: Luna Date: Tue, 2 Jun 2020 17:16:02 -0300 Subject: [PATCH 3/4] remove default value for Runner.repl field --- src/runner.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runner.zig b/src/runner.zig index 66a77c8..a089a67 100644 --- a/src/runner.zig +++ b/src/runner.zig @@ -24,7 +24,7 @@ pub const Runner = struct { image: ?*Image = null, /// If the runner is in REPL mode - repl: bool = false, + repl: bool, args: [][]u8, From 6d8614e6784cc4987771323f8b520bb4922e7481 Mon Sep 17 00:00:00 2001 From: Luna Date: Tue, 2 Jun 2020 17:25:03 -0300 Subject: [PATCH 4/4] revert cloned runner using parsed cmd list --- src/main.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.zig b/src/main.zig index 7d6cc00..be1b672 100644 --- a/src/main.zig +++ b/src/main.zig @@ -222,9 +222,9 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void { // taking address is fine, because runqs_cmd lives in the lifetime // of this function. - try cmds.append(&runqs_cmd.base); + try cmds_parsed.append(&runqs_cmd.base); - try runner_clone.runCommands(cmds, true); + try runner_clone.runCommands(cmds_parsed, true); _ = try stdout.write("\n"); } }