add beginning of print stack trace function

introduce std.debug and move std.assert to std.debug.assert
add mem.copy
This commit is contained in:
Andrew Kelley 2016-05-17 13:32:43 -07:00
parent 2c710382a8
commit 7edef4f3fd
9 changed files with 31 additions and 9 deletions

View File

@ -219,6 +219,7 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/mem.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/list.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/hash_map.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/empty.zig" DESTINATION "${ZIG_STD_DEST}")
install(FILES "${CMAKE_SOURCE_DIR}/std/debug.zig" DESTINATION "${ZIG_STD_DEST}")
add_executable(run_tests ${TEST_SOURCES})
target_link_libraries(run_tests)

16
std/debug.zig Normal file
View File

@ -0,0 +1,16 @@
const io = @import("io.zig");
pub fn assert(b: bool) {
if (!b) unreachable{}
}
pub fn print_stack_trace() {
var maybe_fp: ?&const u8 = @frame_address();
while (true) {
const fp = maybe_fp ?? break;
const return_address = *(&const usize)(usize(fp) + @sizeof(usize));
%%io.stderr.print_u64(return_address);
%%io.stderr.printf("\n");
maybe_fp = *(&const ?&const u8)(fp);
}
}

View File

@ -1,4 +1,4 @@
const assert = @import("index.zig").assert;
const assert = @import("debug.zig").assert;
const math = @import("math.zig");
const mem = @import("mem.zig");
const Allocator = mem.Allocator;

View File

@ -8,13 +8,10 @@ pub const net = @import("net.zig");
pub const list = @import("list.zig");
pub const hash_map = @import("hash_map.zig");
pub const mem = @import("mem.zig");
pub const debug = @import("debug.zig");
pub const linux = switch(@compile_var("os")) {
linux => @import("linux.zig"),
else => null_import,
};
pub fn assert(b: bool) {
if (!b) unreachable{}
}
const null_import = @import("empty.zig");

View File

@ -1,4 +1,4 @@
const assert = @import("index.zig").assert;
const assert = @import("debug.zig").assert;
const mem = @import("mem.zig");
const Allocator = mem.Allocator;

View File

@ -1,3 +1,5 @@
const assert = @import("debug.zig").assert;
pub error NoMem;
pub type Context = u8;
@ -8,3 +10,9 @@ pub struct Allocator {
context: ?&Context,
}
/// Copy all of source into dest at position 0.
/// dest.len must be >= source.len.
pub fn copy(T)(dest: []T, source: []T) {
assert(dest.len >= source.len);
@memcpy(dest.ptr, source.ptr, @sizeof(T) * source.len);
}

View File

@ -1,6 +1,6 @@
const linux = @import("linux.zig");
const errno = @import("errno.zig");
const assert = @import("index.zig").assert;
const assert = @import("debug.zig").assert;
pub error SigInterrupt;
pub error Unexpected;

View File

@ -1,4 +1,4 @@
const assert = @import("index.zig").assert;
const assert = @import("debug.zig").assert;
pub const eql = slice_eql(u8);

View File

@ -1,5 +1,5 @@
const std = @import("std");
const assert = std.assert;
const assert = std.debug.assert;
const str = std.str;
const cstr = std.cstr;
const other = @import("other.zig");