Embedded programming language for Zig
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix exception printing

IamPyu e83a8100 e3c740bc

+14 -10
+14 -10
src/lang.zig
··· 546 546 } 547 547 548 548 pub fn append(self: *CallStack, value: *Value) !void { 549 - var w = std.Io.Writer.Allocating.init(self.string_arena.allocator()); 550 - try value.format(&w.writer); 551 - const s = try w.toOwnedSlice(); 549 + const s = try std.fmt.allocPrint(self.string_arena.allocator(), "{f}", .{value}); 550 + // var w = std.Io.Writer.Allocating.init(self.string_arena.allocator()); 551 + // try value.format(&w.writer); 552 + // const s = try w.toOwnedSlice(); 552 553 try self.stack.append(self.allocator, s); 553 554 } 554 555 ··· 574 575 tracked_values: ValMap, 575 576 tracked_strings: std.ArrayList([]u8), 576 577 call_stack: CallStack, 577 - exception: ?[]u8 = null, 578 + 579 + /// Shouldn't be used to read current exception message, use `getExeception` instead! 580 + _exception: ?[]u8 = null, 578 581 579 582 /// Initialize a new scope 580 583 pub fn init(allocator: Allocator) !Self { ··· 646 649 pub fn setException(self: *Self, msg: []const u8) !void { 647 650 self.clearException(); 648 651 const s = self.getRoot(); 649 - s.exception = try s.allocator.alloc(u8, msg.len); 650 - @memcpy(s.exception.?, msg); 652 + s._exception = try s.allocator.alloc(u8, msg.len); 653 + @memcpy(s._exception.?, msg); 651 654 } 652 655 653 656 /// Clear the current exception message of the root scope, if any 654 657 pub fn clearException(self: *Self) void { 655 658 const s = self.getRoot(); 656 - if (s.exception) |p| { 659 + if (s._exception) |p| { 657 660 s.allocator.free(p); 658 - s.exception = null; 661 + s._exception = null; 659 662 } 660 663 } 661 664 662 665 /// Get the current exception message from the root scope 663 666 pub fn getException(self: *Self) ?[]const u8 { 664 - return self.getRoot().exception; 667 + return self.getRoot()._exception; 665 668 } 666 669 667 670 /// Get the call stack used by the root scope, other call stacks shouldn't be used. ··· 701 704 pub fn createAst(self: *Self, ast: []const *Value) RuntimeError!AST { 702 705 const root = self.getRoot(); 703 706 const out = try root.arena.allocator().alloc(*Value, ast.len); 707 + 704 708 for (ast, 0..) |v, i| { 705 709 out[i] = try Value.clone(v, self); 706 710 } ··· 754 758 /// Dump the call stack and return `e` 755 759 pub fn dumpStackAndError(self: *Self, e: RuntimeError) RuntimeError { 756 760 const msg = if (self.getException()) |msg| blk: { 757 - self.clearException(); 758 761 break :blk msg; 759 762 } else "*empty*"; 760 763 std.log.err("error occured in scope {*}: {}\nexception message: {s}\nunwinding callstack: {*}", .{ self, e, msg, self.getCallStack() }); 764 + self.clearException(); 761 765 // std.log.err("unwinding callstack {*}:\n", .{&self.getCallStack()}); 762 766 self.getCallStack().dump(); 763 767 return e;