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.
This commit is contained in:
Luna 2020-06-02 17:12:34 -03:00
parent c78ca9dd5b
commit a2ea8fb53e

View file

@ -26,10 +26,13 @@ pub const Runner = struct {
/// If the runner is in REPL mode /// If the runner is in REPL mode
repl: bool = false, repl: bool = false,
args: [][]u8,
pub fn init(allocator: *std.mem.Allocator, repl: bool) Runner { pub fn init(allocator: *std.mem.Allocator, repl: bool) Runner {
return Runner{ return Runner{
.allocator = allocator, .allocator = allocator,
.repl = repl, .repl = repl,
.args = std.process.argsAlloc(allocator) catch unreachable,
}; };
} }
@ -41,7 +44,11 @@ pub const Runner = struct {
pub fn clone(self: *Runner) !Runner { pub fn clone(self: *Runner) !Runner {
var cloned_image = if (self.image) |image| try image.clone() else null; 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 { 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. // ':0' should ALWAYS point to the image.
if (self.repl) index += 3 else index += 2; if (self.repl) index += 3 else index += 2;
var args = try std.process.argsAlloc(self.allocator); std.debug.warn("ARGS!! {} \n", .{self.args.len});
defer std.process.argsFree(self.allocator, args); for (self.args) |arg, idx| {
std.debug.warn("ARGS!! {} \n", .{args.len});
for (args) |arg, idx| {
std.debug.warn("arg{} = {}\n", .{ idx, arg }); std.debug.warn("arg{} = {}\n", .{ idx, arg });
} }
std.debug.warn("fetch arg idx={}, val={}\n", .{ index, args[index] }); std.debug.warn("fetch arg idx={}, val={}\n", .{ index, self.args[index] });
return args[index]; return self.args[index];
} else { } else {
return load_path; return load_path;
} }