run zig fmt on the codebase

See #1003
This commit is contained in:
Andrew Kelley 2018-05-28 20:23:55 -04:00
parent cdf30c31ea
commit 0c16cd2d0e
102 changed files with 5957 additions and 3587 deletions

View File

@ -81,6 +81,7 @@ pub const Base64Decoder = struct {
/// e.g. 'A' => 0.
/// undefined for any value not in the 64 alphabet chars.
char_to_index: [256]u8,
/// true only for the 64 chars in the alphabet, not the pad char.
char_in_alphabet: [256]bool,
pad_char: u8,

View File

@ -49,16 +49,16 @@ fn Blake2s(comptime out_len: usize) type {
};
const sigma = [10][16]u8{
[]const u8 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
[]const u8 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
[]const u8 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
[]const u8 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
[]const u8 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
[]const u8 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
[]const u8 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
[]const u8 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
[]const u8 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
[]const u8 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
[]const u8{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
[]const u8{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
[]const u8{ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
[]const u8{ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
[]const u8{ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
[]const u8{ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
[]const u8{ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
[]const u8{ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
[]const u8{ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
[]const u8{ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
};
h: [8]u32,
@ -282,222 +282,18 @@ fn Blake2b(comptime out_len: usize) type {
};
const sigma = [12][16]u8{
[]const u8{
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
},
[]const u8{
14,
10,
4,
8,
9,
15,
13,
6,
1,
12,
0,
2,
11,
7,
5,
3,
},
[]const u8{
11,
8,
12,
0,
5,
2,
15,
13,
10,
14,
3,
6,
7,
1,
9,
4,
},
[]const u8{
7,
9,
3,
1,
13,
12,
11,
14,
2,
6,
5,
10,
4,
0,
15,
8,
},
[]const u8{
9,
0,
5,
7,
2,
4,
10,
15,
14,
1,
11,
12,
6,
8,
3,
13,
},
[]const u8{
2,
12,
6,
10,
0,
11,
8,
3,
4,
13,
7,
5,
15,
14,
1,
9,
},
[]const u8{
12,
5,
1,
15,
14,
13,
4,
10,
0,
7,
6,
3,
9,
2,
8,
11,
},
[]const u8{
13,
11,
7,
14,
12,
1,
3,
9,
5,
0,
15,
4,
8,
6,
2,
10,
},
[]const u8{
6,
15,
14,
9,
11,
3,
0,
8,
12,
2,
13,
7,
1,
4,
10,
5,
},
[]const u8{
10,
2,
8,
4,
7,
6,
1,
5,
15,
11,
9,
14,
3,
12,
13,
0,
},
[]const u8{
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
},
[]const u8{
14,
10,
4,
8,
9,
15,
13,
6,
1,
12,
0,
2,
11,
7,
5,
3,
},
[]const u8{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
[]const u8{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
[]const u8{ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
[]const u8{ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
[]const u8{ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
[]const u8{ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
[]const u8{ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
[]const u8{ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
[]const u8{ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
[]const u8{ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
[]const u8{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
[]const u8{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
};
h: [8]u64,

View File

@ -98,7 +98,6 @@ pub fn errol3(value: f64, buffer: []u8) FloatDecimal {
/// Uncorrected Errol3 double to ASCII conversion.
fn errol3u(val: f64, buffer: []u8) FloatDecimal {
// check if in integer or fixed range
if (val > 9.007199254740992e15 and val < 3.40282366920938e+38) {
return errolInt(val, buffer);
} else if (val >= 16.0 and val < 9.007199254740992e15) {
@ -420,7 +419,7 @@ fn fpprev(val: f64) f64 {
return @bitCast(f64, @bitCast(u64, val) -% 1);
}
pub const c_digits_lut = []u8 {
pub const c_digits_lut = []u8{
'0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6',
'0', '7', '0', '8', '0', '9', '1', '0', '1', '1', '1', '2', '1', '3',
'1', '4', '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', '2', '0',

File diff suppressed because it is too large Load Diff

View File

@ -107,7 +107,7 @@ pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context),
'}' => {
return output(context, args[next_arg]);
},
'0' ... '9' => {
'0'...'9' => {
width_start = i;
state = State.BufWidth;
},
@ -127,7 +127,7 @@ pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context),
state = State.Start;
start_index = i + 1;
},
'0' ... '9' => {
'0'...'9' => {
width_start = i;
state = State.IntegerWidth;
},
@ -141,7 +141,7 @@ pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context),
state = State.Start;
start_index = i + 1;
},
'0' ... '9' => {},
'0'...'9' => {},
else => @compileError("Unexpected character in format string: " ++ []u8{c}),
},
State.FloatScientific => switch (c) {
@ -151,7 +151,7 @@ pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context),
state = State.Start;
start_index = i + 1;
},
'0' ... '9' => {
'0'...'9' => {
width_start = i;
state = State.FloatScientificWidth;
},
@ -165,7 +165,7 @@ pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context),
state = State.Start;
start_index = i + 1;
},
'0' ... '9' => {},
'0'...'9' => {},
else => @compileError("Unexpected character in format string: " ++ []u8{c}),
},
State.Float => switch (c) {
@ -175,7 +175,7 @@ pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context),
state = State.Start;
start_index = i + 1;
},
'0' ... '9' => {
'0'...'9' => {
width_start = i;
state = State.FloatWidth;
},
@ -189,7 +189,7 @@ pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context),
state = State.Start;
start_index = i + 1;
},
'0' ... '9' => {},
'0'...'9' => {},
else => @compileError("Unexpected character in format string: " ++ []u8{c}),
},
State.BufWidth => switch (c) {
@ -200,7 +200,7 @@ pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context),
state = State.Start;
start_index = i + 1;
},
'0' ... '9' => {},
'0'...'9' => {},
else => @compileError("Unexpected character in format string: " ++ []u8{c}),
},
State.Character => switch (c) {
@ -223,7 +223,7 @@ pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context),
radix = 1024;
state = State.BytesBase;
},
'0' ... '9' => {
'0'...'9' => {
width_start = i;
state = State.BytesWidth;
},
@ -236,7 +236,7 @@ pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context),
state = State.Start;
start_index = i + 1;
},
'0' ... '9' => {
'0'...'9' => {
width_start = i;
state = State.BytesWidth;
},
@ -250,7 +250,7 @@ pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context),
state = State.Start;
start_index = i + 1;
},
'0' ... '9' => {},
'0'...'9' => {},
else => @compileError("Unexpected character in format string: " ++ []u8{c}),
},
}
@ -562,9 +562,14 @@ pub fn formatFloatDecimal(value: var, maybe_precision: ?usize, context: var, com
}
}
pub fn formatBytes(value: var, width: ?usize, comptime radix: usize,
context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8)Errors!void) Errors!void
{
pub fn formatBytes(
value: var,
width: ?usize,
comptime radix: usize,
context: var,
comptime Errors: type,
output: fn(@typeOf(context), []const u8) Errors!void,
) Errors!void {
if (value == 0) {
return output(context, "0B");
}
@ -585,16 +590,22 @@ pub fn formatBytes(value: var, width: ?usize, comptime radix: usize,
}
const buf = switch (radix) {
1000 => []u8 { suffix, 'B' },
1024 => []u8 { suffix, 'i', 'B' },
1000 => []u8{ suffix, 'B' },
1024 => []u8{ suffix, 'i', 'B' },
else => unreachable,
};
return output(context, buf);
}
pub fn formatInt(value: var, base: u8, uppercase: bool, width: usize,
context: var, comptime Errors: type, output: fn(@typeOf(context), []const u8)Errors!void) Errors!void
{
pub fn formatInt(
value: var,
base: u8,
uppercase: bool,
width: usize,
context: var,
comptime Errors: type,
output: fn(@typeOf(context), []const u8) Errors!void,
) Errors!void {
if (@typeOf(value).is_signed) {
return formatIntSigned(value, base, uppercase, width, context, Errors, output);
} else {
@ -717,9 +728,9 @@ pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) ParseUnsigned
pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) {
const value = switch (c) {
'0' ... '9' => c - '0',
'A' ... 'Z' => c - 'A' + 10,
'a' ... 'z' => c - 'a' + 10,
'0'...'9' => c - '0',
'A'...'Z' => c - 'A' + 10,
'a'...'z' => c - 'a' + 10,
else => return error.InvalidCharacter,
};
@ -730,8 +741,8 @@ pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) {
fn digitToChar(digit: u8, uppercase: bool) u8 {
return switch (digit) {
0 ... 9 => digit + '0',
10 ... 35 => digit + ((if (uppercase) u8('A') else u8('a')) - 10),
0...9 => digit + '0',
10...35 => digit + ((if (uppercase) u8('A') else u8('a')) - 10),
else => unreachable,
};
}
@ -754,8 +765,7 @@ pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: ...) ![]u8 {
pub fn allocPrint(allocator: &mem.Allocator, comptime fmt: []const u8, args: ...) ![]u8 {
var size: usize = 0;
format(&size, error{}, countSize, fmt, args) catch |err| switch (err) {
};
format(&size, error{}, countSize, fmt, args) catch |err| switch (err) {};
const buf = try allocator.alloc(u8, size);
return bufPrint(buf, fmt, args);
}
@ -1043,8 +1053,7 @@ test "fmt.format" {
fn testFmt(expected: []const u8, comptime template: []const u8, args: ...) !void {
var buf: [100]u8 = undefined;
const result = try bufPrint(buf[0..], template, args);
if (mem.eql(u8, result, expected))
return;
if (mem.eql(u8, result, expected)) return;
std.debug.warn("\n====== expected this output: =========\n");
std.debug.warn("{}", expected);
@ -1082,10 +1091,7 @@ test "fmt.trim" {
pub fn isWhiteSpace(byte: u8) bool {
return switch (byte) {
' ',
'\t',
'\n',
'\r' => true,
' ', '\t', '\n', '\r' => true,
else => false,
};
}

View File

@ -13,9 +13,7 @@ pub const Adler32 = struct {
adler: u32,
pub fn init() Adler32 {
return Adler32 {
.adler = 1,
};
return Adler32{ .adler = 1 };
}
// This fast variant is taken from zlib. It reduces the required modulos and unrolls longer
@ -33,8 +31,7 @@ pub const Adler32 = struct {
if (s2 >= base) {
s2 -= base;
}
}
else if (input.len < 16) {
} else if (input.len < 16) {
for (input) |b| {
s1 +%= b;
s2 +%= s1;
@ -44,8 +41,7 @@ pub const Adler32 = struct {
}
s2 %= base;
}
else {
} else {
var i: usize = 0;
while (i + nmax <= input.len) : (i += nmax) {
const n = nmax / 16; // note: 16 | nmax
@ -98,15 +94,14 @@ test "adler32 sanity" {
}
test "adler32 long" {
const long1 = []u8 {1} ** 1024;
const long1 = []u8{1} ** 1024;
debug.assert(Adler32.hash(long1[0..]) == 0x06780401);
const long2 = []u8 {1} ** 1025;
const long2 = []u8{1} ** 1025;
debug.assert(Adler32.hash(long2[0..]) == 0x0a7a0402);
}
test "adler32 very long" {
const long = []u8 {1} ** 5553;
const long = []u8{1} ** 5553;
debug.assert(Adler32.hash(long[0..]) == 0x707f15b2);
}

View File

@ -69,7 +69,6 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
self.crc ^= (u32(p[2]) << 16);
self.crc ^= (u32(p[3]) << 24);
self.crc =
lookup_tables[0][p[7]] ^
lookup_tables[1][p[6]] ^
@ -77,8 +76,8 @@ pub fn Crc32WithPoly(comptime poly: u32) type {
lookup_tables[3][p[4]] ^
lookup_tables[4][@truncate(u8, self.crc >> 24)] ^
lookup_tables[5][@truncate(u8, self.crc >> 16)] ^
lookup_tables[6][@truncate(u8, self.crc >> 8)] ^
lookup_tables[7][@truncate(u8, self.crc >> 0)];
lookup_tables[6][@truncate(u8, self.crc >> 8)] ^
lookup_tables[7][@truncate(u8, self.crc >> 0)];
}
while (i < input.len) : (i += 1) {

View File

@ -7,7 +7,7 @@
const std = @import("../index.zig");
const debug = std.debug;
pub const Fnv1a_32 = Fnv1a(u32, 0x01000193 , 0x811c9dc5);
pub const Fnv1a_32 = Fnv1a(u32, 0x01000193, 0x811c9dc5);
pub const Fnv1a_64 = Fnv1a(u64, 0x100000001b3, 0xcbf29ce484222325);
pub const Fnv1a_128 = Fnv1a(u128, 0x1000000000000000000013b, 0x6c62272e07bb014262b821756295c58d);
@ -18,9 +18,7 @@ fn Fnv1a(comptime T: type, comptime prime: T, comptime offset: T) type {
value: T,
pub fn init() Self {
return Self {
.value = offset,
};
return Self{ .value = offset };
}
pub fn update(self: &Self, input: []const u8) void {

View File

@ -45,7 +45,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
const k0 = mem.readInt(key[0..8], u64, Endian.Little);
const k1 = mem.readInt(key[8..16], u64, Endian.Little);
var d = Self {
var d = Self{
.v0 = k0 ^ 0x736f6d6570736575,
.v1 = k1 ^ 0x646f72616e646f6d,
.v2 = k0 ^ 0x6c7967656e657261,
@ -162,7 +162,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize)
const test_key = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f";
test "siphash64-2-4 sanity" {
const vectors = [][]const u8 {
const vectors = [][]const u8{
"\x31\x0e\x0e\xdd\x47\xdb\x6f\x72", // ""
"\xfd\x67\xdc\x93\xc5\x39\xf8\x74", // "\x00"
"\x5a\x4f\xa9\xd9\x09\x80\x6c\x0d", // "\x00\x01" ... etc
@ -241,7 +241,7 @@ test "siphash64-2-4 sanity" {
}
test "siphash128-2-4 sanity" {
const vectors = [][]const u8 {
const vectors = [][]const u8{
"\xa3\x81\x7f\x04\xba\x25\xa8\xe6\x6d\xf6\x72\x14\xc7\x55\x02\x93",
"\xda\x87\xc1\xd8\x6b\x99\xaf\x44\x34\x76\x59\x11\x9b\x22\xfc\x45",
"\x81\x77\x22\x8d\xa4\xa4\x5d\xc7\xfc\xa3\x8b\xde\xf6\x0a\xff\xe4",

View File

@ -68,9 +68,7 @@ pub const DirectAllocator = struct {
const self = @fieldParentPtr(DirectAllocator, "allocator", allocator);
switch (builtin.os) {
Os.linux,
Os.macosx,
Os.ios => {
Os.linux, Os.macosx, Os.ios => {
const p = os.posix;
const alloc_size = if (alignment <= os.page_size) n else n + alignment;
const addr = p.mmap(null, alloc_size, p.PROT_READ | p.PROT_WRITE, p.MAP_PRIVATE | p.MAP_ANONYMOUS, -1, 0);
@ -121,9 +119,7 @@ pub const DirectAllocator = struct {
const self = @fieldParentPtr(DirectAllocator, "allocator", allocator);
switch (builtin.os) {
Os.linux,
Os.macosx,
Os.ios => {
Os.linux, Os.macosx, Os.ios => {
if (new_size <= old_mem.len) {
const base_addr = @ptrToInt(old_mem.ptr);
const old_addr_end = base_addr + old_mem.len;
@ -168,9 +164,7 @@ pub const DirectAllocator = struct {
const self = @fieldParentPtr(DirectAllocator, "allocator", allocator);
switch (builtin.os) {
Os.linux,
Os.macosx,
Os.ios => {
Os.linux, Os.macosx, Os.ios => {
_ = os.posix.munmap(@ptrToInt(bytes.ptr), bytes.len);
},
Os.windows => {
@ -430,7 +424,7 @@ fn testAllocator(allocator: &mem.Allocator) !void {
}
fn testAllocatorLargeAlignment(allocator: &mem.Allocator) mem.Allocator.Error!void {
//Maybe a platform's page_size is actually the same as or
//Maybe a platform's page_size is actually the same as or
// very near usize?
if (os.page_size << 2 > @maxValue(usize)) return;

View File

@ -42,7 +42,7 @@ test "write a file, read it, then delete it" {
assert(mem.eql(u8, contents[0.."begin".len], "begin"));
assert(mem.eql(u8, contents["begin".len..contents.len - "end".len], data));
assert(mem.eql(u8, contents[contents.len - "end".len ..], "end"));
assert(mem.eql(u8, contents[contents.len - "end".len..], "end"));
}
try os.deleteFile(allocator, tmp_file_name);
}

View File

@ -252,7 +252,7 @@ pub const StreamingJsonParser = struct {
p.after_value_state = State.TopLevelEnd;
p.count = 0;
},
'1' ... '9' => {
'1'...'9' => {
p.number_is_integer = true;
p.state = State.NumberMaybeDigitOrDotOrExponent;
p.after_value_state = State.TopLevelEnd;
@ -281,10 +281,7 @@ pub const StreamingJsonParser = struct {
p.after_value_state = State.TopLevelEnd;
p.count = 0;
},
0x09,
0x0A,
0x0D,
0x20 => {
0x09, 0x0A, 0x0D, 0x20 => {
// whitespace
},
else => {
@ -293,10 +290,7 @@ pub const StreamingJsonParser = struct {
},
State.TopLevelEnd => switch (c) {
0x09,
0x0A,
0x0D,
0x20 => {
0x09, 0x0A, 0x0D, 0x20 => {
// whitespace
},
else => {
@ -392,7 +386,7 @@ pub const StreamingJsonParser = struct {
p.state = State.NumberMaybeDotOrExponent;
p.count = 0;
},
'1' ... '9' => {
'1'...'9' => {
p.state = State.NumberMaybeDigitOrDotOrExponent;
p.count = 0;
},
@ -412,10 +406,7 @@ pub const StreamingJsonParser = struct {
p.state = State.NullLiteral1;
p.count = 0;
},
0x09,
0x0A,
0x0D,
0x20 => {
0x09, 0x0A, 0x0D, 0x20 => {
// whitespace
},
else => {
@ -461,7 +452,7 @@ pub const StreamingJsonParser = struct {
p.state = State.NumberMaybeDotOrExponent;
p.count = 0;
},
'1' ... '9' => {
'1'...'9' => {
p.state = State.NumberMaybeDigitOrDotOrExponent;
p.count = 0;
},
@ -481,10 +472,7 @@ pub const StreamingJsonParser = struct {
p.state = State.NullLiteral1;
p.count = 0;
},
0x09,
0x0A,
0x0D,
0x20 => {
0x09, 0x0A, 0x0D, 0x20 => {
// whitespace
},
else => {
@ -533,10 +521,7 @@ pub const StreamingJsonParser = struct {
token.* = Token.initMarker(Token.Id.ObjectEnd);
},
0x09,
0x0A,
0x0D,
0x20 => {
0x09, 0x0A, 0x0D, 0x20 => {
// whitespace
},
else => {
@ -549,10 +534,7 @@ pub const StreamingJsonParser = struct {
p.state = State.ValueBegin;
p.after_string_state = State.ValueEnd;
},
0x09,
0x0A,
0x0D,
0x20 => {
0x09, 0x0A, 0x0D, 0x20 => {
// whitespace
},
else => {
@ -561,7 +543,7 @@ pub const StreamingJsonParser = struct {
},
State.String => switch (c) {
0x00 ... 0x1F => {
0x00...0x1F => {
return error.InvalidControlCharacter;
},
'"' => {
@ -576,19 +558,16 @@ pub const StreamingJsonParser = struct {
'\\' => {
p.state = State.StringEscapeCharacter;
},
0x20,
0x21,
0x23 ... 0x5B,
0x5D ... 0x7F => {
0x20, 0x21, 0x23...0x5B, 0x5D...0x7F => {
// non-control ascii
},
0xC0 ... 0xDF => {
0xC0...0xDF => {
p.state = State.StringUtf8Byte1;
},
0xE0 ... 0xEF => {
0xE0...0xEF => {
p.state = State.StringUtf8Byte2;
},
0xF0 ... 0xFF => {
0xF0...0xFF => {
p.state = State.StringUtf8Byte3;
},
else => {
@ -620,14 +599,7 @@ pub const StreamingJsonParser = struct {
// The current JSONTestSuite tests rely on both of this behaviour being present
// however, so we default to the status quo where both are accepted until this
// is further clarified.
'"',
'\\',
'/',
'b',
'f',
'n',
'r',
't' => {
'"', '\\', '/', 'b', 'f', 'n', 'r', 't' => {
p.string_has_escape = true;
p.state = State.String;
},
@ -641,36 +613,28 @@ pub const StreamingJsonParser = struct {
},
State.StringEscapeHexUnicode4 => switch (c) {
'0' ... '9',
'A' ... 'F',
'a' ... 'f' => {
'0'...'9', 'A'...'F', 'a'...'f' => {
p.state = State.StringEscapeHexUnicode3;
},
else => return error.InvalidUnicodeHexSymbol,
},
State.StringEscapeHexUnicode3 => switch (c) {
'0' ... '9',
'A' ... 'F',
'a' ... 'f' => {
'0'...'9', 'A'...'F', 'a'...'f' => {
p.state = State.StringEscapeHexUnicode2;
},
else => return error.InvalidUnicodeHexSymbol,
},
State.StringEscapeHexUnicode2 => switch (c) {
'0' ... '9',
'A' ... 'F',
'a' ... 'f' => {
'0'...'9', 'A'...'F', 'a'...'f' => {
p.state = State.StringEscapeHexUnicode1;
},
else => return error.InvalidUnicodeHexSymbol,
},
State.StringEscapeHexUnicode1 => switch (c) {
'0' ... '9',
'A' ... 'F',
'a' ... 'f' => {
'0'...'9', 'A'...'F', 'a'...'f' => {
p.state = State.String;
},
else => return error.InvalidUnicodeHexSymbol,
@ -682,7 +646,7 @@ pub const StreamingJsonParser = struct {
'0' => {
p.state = State.NumberMaybeDotOrExponent;
},
'1' ... '9' => {
'1'...'9' => {
p.state = State.NumberMaybeDigitOrDotOrExponent;
},
else => {
@ -698,8 +662,7 @@ pub const StreamingJsonParser = struct {
p.number_is_integer = false;
p.state = State.NumberFractionalRequired;
},
'e',
'E' => {
'e', 'E' => {
p.number_is_integer = false;
p.state = State.NumberExponent;
},
@ -718,12 +681,11 @@ pub const StreamingJsonParser = struct {
p.number_is_integer = false;
p.state = State.NumberFractionalRequired;
},
'e',
'E' => {
'e', 'E' => {
p.number_is_integer = false;
p.state = State.NumberExponent;
},
'0' ... '9' => {
'0'...'9' => {
// another digit
},
else => {
@ -737,7 +699,7 @@ pub const StreamingJsonParser = struct {
State.NumberFractionalRequired => {
p.complete = p.after_value_state == State.TopLevelEnd;
switch (c) {
'0' ... '9' => {
'0'...'9' => {
p.state = State.NumberFractional;
},
else => {
@ -749,11 +711,10 @@ pub const StreamingJsonParser = struct {
State.NumberFractional => {
p.complete = p.after_value_state == State.TopLevelEnd;
switch (c) {
'0' ... '9' => {
'0'...'9' => {
// another digit
},
'e',
'E' => {
'e', 'E' => {
p.number_is_integer = false;
p.state = State.NumberExponent;
},
@ -768,8 +729,7 @@ pub const StreamingJsonParser = struct {
State.NumberMaybeExponent => {
p.complete = p.after_value_state == State.TopLevelEnd;
switch (c) {
'e',
'E' => {
'e', 'E' => {
p.number_is_integer = false;
p.state = State.NumberExponent;
},
@ -782,12 +742,11 @@ pub const StreamingJsonParser = struct {
},
State.NumberExponent => switch (c) {
'-',
'+' => {
'-', '+' => {
p.complete = false;
p.state = State.NumberExponentDigitsRequired;
},
'0' ... '9' => {
'0'...'9' => {
p.complete = p.after_value_state == State.TopLevelEnd;
p.state = State.NumberExponentDigits;
},
@ -797,7 +756,7 @@ pub const StreamingJsonParser = struct {
},
State.NumberExponentDigitsRequired => switch (c) {
'0' ... '9' => {
'0'...'9' => {
p.complete = p.after_value_state == State.TopLevelEnd;
p.state = State.NumberExponentDigits;
},
@ -809,7 +768,7 @@ pub const StreamingJsonParser = struct {
State.NumberExponentDigits => {
p.complete = p.after_value_state == State.TopLevelEnd;
switch (c) {
'0' ... '9' => {
'0'...'9' => {
// another digit
},
else => {
@ -1257,8 +1216,7 @@ pub const JsonParser = struct {
Token.Id.Null => {
try p.stack.append(Value.Null);
},
Token.Id.ObjectEnd,
Token.Id.ArrayEnd => {
Token.Id.ObjectEnd, Token.Id.ArrayEnd => {
unreachable;
},
},

View File

@ -58,15 +58,15 @@ pub const SymbolTable = struct {
// code, its displacement is different.
pub fn deinit(self: &SymbolTable) void {
self.allocator.free(self.symbols);
self.symbols = []const Symbol {};
self.symbols = []const Symbol{};
self.allocator.free(self.strings);
self.strings = []const u8 {};
self.strings = []const u8{};
}
pub fn search(self: &const SymbolTable, address: usize) ?&const Symbol {
var min: usize = 0;
var max: usize = self.symbols.len - 1; // Exclude sentinel.
var max: usize = self.symbols.len - 1; // Exclude sentinel.
while (min < max) {
const mid = min + (max - min) / 2;
const curr = &self.symbols[mid];
@ -118,10 +118,11 @@ pub fn loadSymbols(allocator: &mem.Allocator, in: &io.FileInStream) !SymbolTable
try in.stream.readNoEof(strings);
var nsyms: usize = 0;
for (syms) |sym| if (isSymbol(sym)) nsyms += 1;
for (syms) |sym|
if (isSymbol(sym)) nsyms += 1;
if (nsyms == 0) return error.MissingDebugInfo;
var symbols = try allocator.alloc(Symbol, nsyms + 1); // Room for sentinel.
var symbols = try allocator.alloc(Symbol, nsyms + 1); // Room for sentinel.
errdefer allocator.free(symbols);
var pie_slide: usize = 0;
@ -132,7 +133,7 @@ pub fn loadSymbols(allocator: &mem.Allocator, in: &io.FileInStream) !SymbolTable
const end = ??mem.indexOfScalarPos(u8, strings, start, 0);
const name = strings[start..end];
const address = sym.n_value;
symbols[nsym] = Symbol { .name = name, .address = address };
symbols[nsym] = Symbol{ .name = name, .address = address };
nsym += 1;
if (is_pie and mem.eql(u8, name, "_SymbolTable_deinit")) {
pie_slide = @ptrToInt(SymbolTable.deinit) - address;
@ -145,13 +146,14 @@ pub fn loadSymbols(allocator: &mem.Allocator, in: &io.FileInStream) !SymbolTable
// Insert the sentinel. Since we don't know where the last function ends,
// we arbitrarily limit it to the start address + 4 KB.
const top = symbols[nsyms - 1].address + 4096;
symbols[nsyms] = Symbol { .name = "", .address = top };
symbols[nsyms] = Symbol{ .name = "", .address = top };
if (pie_slide != 0) {
for (symbols) |*symbol| symbol.address += pie_slide;
for (symbols) |*symbol|
symbol.address += pie_slide;
}
return SymbolTable {
return SymbolTable{
.allocator = allocator,
.symbols = symbols,
.strings = strings,

View File

@ -17,25 +17,25 @@ pub fn atan(x: var) @typeOf(x) {
}
fn atan32(x_: f32) f32 {
const atanhi = []const f32 {
const atanhi = []const f32{
4.6364760399e-01, // atan(0.5)hi
7.8539812565e-01, // atan(1.0)hi
9.8279368877e-01, // atan(1.5)hi
1.5707962513e+00, // atan(inf)hi
};
const atanlo = []const f32 {
const atanlo = []const f32{
5.0121582440e-09, // atan(0.5)lo
3.7748947079e-08, // atan(1.0)lo
3.4473217170e-08, // atan(1.5)lo
7.5497894159e-08, // atan(inf)lo
};
const aT = []const f32 {
const aT = []const f32{
3.3333328366e-01,
-1.9999158382e-01,
-1.9999158382e-01,
1.4253635705e-01,
-1.0648017377e-01,
-1.0648017377e-01,
6.1687607318e-02,
};
@ -80,8 +80,7 @@ fn atan32(x_: f32) f32 {
id = 1;
x = (x - 1.0) / (x + 1.0);
}
}
else {
} else {
// |x| < 2.4375
if (ix < 0x401C0000) {
id = 2;
@ -109,31 +108,31 @@ fn atan32(x_: f32) f32 {
}
fn atan64(x_: f64) f64 {
const atanhi = []const f64 {
const atanhi = []const f64{
4.63647609000806093515e-01, // atan(0.5)hi
7.85398163397448278999e-01, // atan(1.0)hi
9.82793723247329054082e-01, // atan(1.5)hi
1.57079632679489655800e+00, // atan(inf)hi
};
const atanlo = []const f64 {
const atanlo = []const f64{
2.26987774529616870924e-17, // atan(0.5)lo
3.06161699786838301793e-17, // atan(1.0)lo
1.39033110312309984516e-17, // atan(1.5)lo
6.12323399573676603587e-17, // atan(inf)lo
};
const aT = []const f64 {
const aT = []const f64{
3.33333333333329318027e-01,
-1.99999999998764832476e-01,
-1.99999999998764832476e-01,
1.42857142725034663711e-01,
-1.11111104054623557880e-01,
-1.11111104054623557880e-01,
9.09088713343650656196e-02,
-7.69187620504482999495e-02,
-7.69187620504482999495e-02,
6.66107313738753120669e-02,
-5.83357013379057348645e-02,
-5.83357013379057348645e-02,
4.97687799461593236017e-02,
-3.65315727442169155270e-02,
-3.65315727442169155270e-02,
1.62858201153657823623e-02,
};
@ -179,8 +178,7 @@ fn atan64(x_: f64) f64 {
id = 1;
x = (x - 1.0) / (x + 1.0);
}
}
else {
} else {
// |x| < 2.4375
if (ix < 0x40038000) {
id = 2;

View File

@ -53,8 +53,7 @@ fn atan2_32(y: f32, x: f32) f32 {
if (iy == 0) {
switch (m) {
0,
1 => return y, // atan(+-0, +...)
0, 1 => return y, // atan(+-0, +...)
2 => return pi, // atan(+0, -...)
3 => return -pi, // atan(-0, -...)
else => unreachable,
@ -144,8 +143,7 @@ fn atan2_64(y: f64, x: f64) f64 {
if (iy | ly == 0) {
switch (m) {
0,
1 => return y, // atan(+-0, +...)
0, 1 => return y, // atan(+-0, +...)
2 => return pi, // atan(+0, -...)
3 => return -pi, // atan(-0, -...)
else => unreachable,

View File

@ -31,28 +31,28 @@ pub fn Complex(comptime T: type) type {
im: T,
pub fn new(re: T, im: T) Self {
return Self {
return Self{
.re = re,
.im = im,
};
}
pub fn add(self: &const Self, other: &const Self) Self {
return Self {
return Self{
.re = self.re + other.re,
.im = self.im + other.im,
};
}
pub fn sub(self: &const Self, other: &const Self) Self {
return Self {
return Self{
.re = self.re - other.re,
.im = self.im - other.im,
};
}
pub fn mul(self: &const Self, other: &const Self) Self {
return Self {
return Self{
.re = self.re * other.re - self.im * other.im,
.im = self.im * other.re + self.re * other.im,
};
@ -63,14 +63,14 @@ pub fn Complex(comptime T: type) type {
const im_num = self.im * other.re - self.re * other.im;
const den = other.re * other.re + other.im * other.im;
return Self {
return Self{
.re = re_num / den,
.im = im_num / den,
};
}
pub fn conjugate(self: &const Self) Self {
return Self {
return Self{
.re = self.re,
.im = -self.im,
};
@ -78,7 +78,7 @@ pub fn Complex(comptime T: type) type {
pub fn reciprocal(self: &const Self) Self {
const m = self.re * self.re + self.im * self.im;
return Self {
return Self{
.re = self.re / m,
.im = -self.im / m,
};
@ -121,8 +121,8 @@ test "complex.div" {
const b = Complex(f32).new(2, 7);
const c = a.div(b);
debug.assert(math.approxEq(f32, c.re, f32(31)/53, epsilon) and
math.approxEq(f32, c.im, f32(-29)/53, epsilon));
debug.assert(math.approxEq(f32, c.re, f32(31) / 53, epsilon) and
math.approxEq(f32, c.im, f32(-29) / 53, epsilon));
}
test "complex.conjugate" {
@ -136,8 +136,8 @@ test "complex.reciprocal" {
const a = Complex(f32).new(5, 3);
const c = a.reciprocal();
debug.assert(math.approxEq(f32, c.re, f32(5)/34, epsilon) and
math.approxEq(f32, c.im, f32(-3)/34, epsilon));
debug.assert(math.approxEq(f32, c.re, f32(5) / 34, epsilon) and
math.approxEq(f32, c.im, f32(-3) / 34, epsilon));
}
test "complex.magnitude" {

View File

@ -98,7 +98,7 @@ test "complex.ctanh32" {
const a = Complex(f32).new(5, 3);
const c = tanh(a);
debug.assert(math.approxEq(f32, c.re, 0.999913, epsilon));
debug.assert(math.approxEq(f32, c.re, 0.999913, epsilon));
debug.assert(math.approxEq(f32, c.im, -0.000025, epsilon));
}
@ -106,6 +106,6 @@ test "complex.ctanh64" {
const a = Complex(f64).new(5, 3);
const c = tanh(a);
debug.assert(math.approxEq(f64, c.re, 0.999913, epsilon));
debug.assert(math.approxEq(f64, c.re, 0.999913, epsilon));
debug.assert(math.approxEq(f64, c.im, -0.000025, epsilon));
}

View File

@ -20,10 +20,10 @@ pub fn exp(x: var) @typeOf(x) {
fn exp32(x_: f32) f32 {
@setFloatMode(this, builtin.FloatMode.Strict);
const half = []f32 { 0.5, -0.5 };
const half = []f32{ 0.5, -0.5 };
const ln2hi = 6.9314575195e-1;
const ln2lo = 1.4286067653e-6;
const invln2 = 1.4426950216e+0;
const invln2 = 1.4426950216e+0;
const P1 = 1.6666625440e-1;
const P2 = -2.7667332906e-3;
@ -47,7 +47,7 @@ fn exp32(x_: f32) f32 {
return x * 0x1.0p127;
}
if (sign != 0) {
math.forceEval(-0x1.0p-149 / x); // overflow
math.forceEval(-0x1.0p-149 / x); // overflow
// x <= -103.972084
if (hx >= 0x42CFF1B5) {
return 0;
@ -64,8 +64,7 @@ fn exp32(x_: f32) f32 {
// |x| > 1.5 * ln2
if (hx > 0x3F851592) {
k = i32(invln2 * x + half[usize(sign)]);
}
else {
} else {
k = 1 - sign - sign;
}
@ -79,8 +78,7 @@ fn exp32(x_: f32) f32 {
k = 0;
hi = x;
lo = 0;
}
else {
} else {
math.forceEval(0x1.0p127 + x); // inexact
return 1 + x;
}
@ -99,15 +97,15 @@ fn exp32(x_: f32) f32 {
fn exp64(x_: f64) f64 {
@setFloatMode(this, builtin.FloatMode.Strict);
const half = []const f64 { 0.5, -0.5 };
const half = []const f64{ 0.5, -0.5 };
const ln2hi: f64 = 6.93147180369123816490e-01;
const ln2lo: f64 = 1.90821492927058770002e-10;
const invln2: f64 = 1.44269504088896338700e+00;
const P1: f64 = 1.66666666666666019037e-01;
const P2: f64 = -2.77777777770155933842e-03;
const P3: f64 = 6.61375632143793436117e-05;
const P4: f64 = -1.65339022054652515390e-06;
const P5: f64 = 4.13813679705723846039e-08;
const P1: f64 = 1.66666666666666019037e-01;
const P2: f64 = -2.77777777770155933842e-03;
const P3: f64 = 6.61375632143793436117e-05;
const P4: f64 = -1.65339022054652515390e-06;
const P5: f64 = 4.13813679705723846039e-08;
var x = x_;
var ux = @bitCast(u64, x);
@ -151,8 +149,7 @@ fn exp64(x_: f64) f64 {
// |x| >= 1.5 * ln2
if (hx > 0x3FF0A2B2) {
k = i32(invln2 * x + half[usize(sign)]);
}
else {
} else {
k = 1 - sign - sign;
}
@ -166,8 +163,7 @@ fn exp64(x_: f64) f64 {
k = 0;
hi = x;
lo = 0;
}
else {
} else {
// inexact if x != 0
// math.forceEval(0x1.0p1023 + x);
return 1 + x;

View File

@ -16,7 +16,7 @@ pub fn exp2(x: var) @typeOf(x) {
};
}
const exp2ft = []const f64 {
const exp2ft = []const f64{
0x1.6a09e667f3bcdp-1,
0x1.7a11473eb0187p-1,
0x1.8ace5422aa0dbp-1,
@ -92,195 +92,195 @@ fn exp2_32(x: f32) f32 {
return f32(r * uk);
}
const exp2dt = []f64 {
const exp2dt = []f64{
// exp2(z + eps) eps
0x1.6a09e667f3d5dp-1, 0x1.9880p-44,
0x1.6b052fa751744p-1, 0x1.8000p-50,
0x1.6a09e667f3d5dp-1, 0x1.9880p-44,
0x1.6b052fa751744p-1, 0x1.8000p-50,
0x1.6c012750bd9fep-1, -0x1.8780p-45,
0x1.6cfdcddd476bfp-1, 0x1.ec00p-46,
0x1.6cfdcddd476bfp-1, 0x1.ec00p-46,
0x1.6dfb23c651a29p-1, -0x1.8000p-50,
0x1.6ef9298593ae3p-1, -0x1.c000p-52,
0x1.6ff7df9519386p-1, -0x1.fd80p-45,
0x1.70f7466f42da3p-1, -0x1.c880p-45,
0x1.71f75e8ec5fc3p-1, 0x1.3c00p-46,
0x1.71f75e8ec5fc3p-1, 0x1.3c00p-46,
0x1.72f8286eacf05p-1, -0x1.8300p-44,
0x1.73f9a48a58152p-1, -0x1.0c00p-47,
0x1.74fbd35d7ccfcp-1, 0x1.f880p-45,
0x1.75feb564267f1p-1, 0x1.3e00p-47,
0x1.74fbd35d7ccfcp-1, 0x1.f880p-45,
0x1.75feb564267f1p-1, 0x1.3e00p-47,
0x1.77024b1ab6d48p-1, -0x1.7d00p-45,
0x1.780694fde5d38p-1, -0x1.d000p-50,
0x1.790b938ac1d00p-1, 0x1.3000p-49,
0x1.790b938ac1d00p-1, 0x1.3000p-49,
0x1.7a11473eb0178p-1, -0x1.d000p-49,
0x1.7b17b0976d060p-1, 0x1.0400p-45,
0x1.7c1ed0130c133p-1, 0x1.0000p-53,
0x1.7b17b0976d060p-1, 0x1.0400p-45,
0x1.7c1ed0130c133p-1, 0x1.0000p-53,
0x1.7d26a62ff8636p-1, -0x1.6900p-45,
0x1.7e2f336cf4e3bp-1, -0x1.2e00p-47,
0x1.7f3878491c3e8p-1, -0x1.4580p-45,
0x1.80427543e1b4ep-1, 0x1.3000p-44,
0x1.814d2add1071ap-1, 0x1.f000p-47,
0x1.80427543e1b4ep-1, 0x1.3000p-44,
0x1.814d2add1071ap-1, 0x1.f000p-47,
0x1.82589994ccd7ep-1, -0x1.1c00p-45,
0x1.8364c1eb942d0p-1, 0x1.9d00p-45,
0x1.8471a4623cab5p-1, 0x1.7100p-43,
0x1.857f4179f5bbcp-1, 0x1.2600p-45,
0x1.8364c1eb942d0p-1, 0x1.9d00p-45,
0x1.8471a4623cab5p-1, 0x1.7100p-43,
0x1.857f4179f5bbcp-1, 0x1.2600p-45,
0x1.868d99b4491afp-1, -0x1.2c40p-44,
0x1.879cad931a395p-1, -0x1.3000p-45,
0x1.88ac7d98a65b8p-1, -0x1.a800p-45,
0x1.89bd0a4785800p-1, -0x1.d000p-49,
0x1.8ace5422aa223p-1, 0x1.3280p-44,
0x1.8be05bad619fap-1, 0x1.2b40p-43,
0x1.8ace5422aa223p-1, 0x1.3280p-44,
0x1.8be05bad619fap-1, 0x1.2b40p-43,
0x1.8cf3216b54383p-1, -0x1.ed00p-45,
0x1.8e06a5e08664cp-1, -0x1.0500p-45,
0x1.8f1ae99157807p-1, 0x1.8280p-45,
0x1.8f1ae99157807p-1, 0x1.8280p-45,
0x1.902fed0282c0ep-1, -0x1.cb00p-46,
0x1.9145b0b91ff96p-1, -0x1.5e00p-47,
0x1.925c353aa2ff9p-1, 0x1.5400p-48,
0x1.93737b0cdc64ap-1, 0x1.7200p-46,
0x1.925c353aa2ff9p-1, 0x1.5400p-48,
0x1.93737b0cdc64ap-1, 0x1.7200p-46,
0x1.948b82b5f98aep-1, -0x1.9000p-47,
0x1.95a44cbc852cbp-1, 0x1.5680p-45,
0x1.95a44cbc852cbp-1, 0x1.5680p-45,
0x1.96bdd9a766f21p-1, -0x1.6d00p-44,
0x1.97d829fde4e2ap-1, -0x1.1000p-47,
0x1.98f33e47a23a3p-1, 0x1.d000p-45,
0x1.98f33e47a23a3p-1, 0x1.d000p-45,
0x1.9a0f170ca0604p-1, -0x1.8a40p-44,
0x1.9b2bb4d53ff89p-1, 0x1.55c0p-44,
0x1.9c49182a3f15bp-1, 0x1.6b80p-45,
0x1.9b2bb4d53ff89p-1, 0x1.55c0p-44,
0x1.9c49182a3f15bp-1, 0x1.6b80p-45,
0x1.9d674194bb8c5p-1, -0x1.c000p-49,
0x1.9e86319e3238ep-1, 0x1.7d00p-46,
0x1.9fa5e8d07f302p-1, 0x1.6400p-46,
0x1.9e86319e3238ep-1, 0x1.7d00p-46,
0x1.9fa5e8d07f302p-1, 0x1.6400p-46,
0x1.a0c667b5de54dp-1, -0x1.5000p-48,
0x1.a1e7aed8eb8f6p-1, 0x1.9e00p-47,
0x1.a309bec4a2e27p-1, 0x1.ad80p-45,
0x1.a1e7aed8eb8f6p-1, 0x1.9e00p-47,
0x1.a309bec4a2e27p-1, 0x1.ad80p-45,
0x1.a42c980460a5dp-1, -0x1.af00p-46,
0x1.a5503b23e259bp-1, 0x1.b600p-47,
0x1.a674a8af46213p-1, 0x1.8880p-44,
0x1.a799e1330b3a7p-1, 0x1.1200p-46,
0x1.a8bfe53c12e8dp-1, 0x1.6c00p-47,
0x1.a5503b23e259bp-1, 0x1.b600p-47,
0x1.a674a8af46213p-1, 0x1.8880p-44,
0x1.a799e1330b3a7p-1, 0x1.1200p-46,
0x1.a8bfe53c12e8dp-1, 0x1.6c00p-47,
0x1.a9e6b5579fcd2p-1, -0x1.9b80p-45,
0x1.ab0e521356fb8p-1, 0x1.b700p-45,
0x1.ac36bbfd3f381p-1, 0x1.9000p-50,
0x1.ad5ff3a3c2780p-1, 0x1.4000p-49,
0x1.ab0e521356fb8p-1, 0x1.b700p-45,
0x1.ac36bbfd3f381p-1, 0x1.9000p-50,
0x1.ad5ff3a3c2780p-1, 0x1.4000p-49,
0x1.ae89f995ad2a3p-1, -0x1.c900p-45,
0x1.afb4ce622f367p-1, 0x1.6500p-46,
0x1.b0e07298db790p-1, 0x1.fd40p-45,
0x1.b20ce6c9a89a9p-1, 0x1.2700p-46,
0x1.b33a2b84f1a4bp-1, 0x1.d470p-43,
0x1.afb4ce622f367p-1, 0x1.6500p-46,
0x1.b0e07298db790p-1, 0x1.fd40p-45,
0x1.b20ce6c9a89a9p-1, 0x1.2700p-46,
0x1.b33a2b84f1a4bp-1, 0x1.d470p-43,
0x1.b468415b747e7p-1, -0x1.8380p-44,
0x1.b59728de5593ap-1, 0x1.8000p-54,
0x1.b6c6e29f1c56ap-1, 0x1.ad00p-47,
0x1.b7f76f2fb5e50p-1, 0x1.e800p-50,
0x1.b59728de5593ap-1, 0x1.8000p-54,
0x1.b6c6e29f1c56ap-1, 0x1.ad00p-47,
0x1.b7f76f2fb5e50p-1, 0x1.e800p-50,
0x1.b928cf22749b2p-1, -0x1.4c00p-47,
0x1.ba5b030a10603p-1, -0x1.d700p-47,
0x1.bb8e0b79a6f66p-1, 0x1.d900p-47,
0x1.bcc1e904bc1ffp-1, 0x1.2a00p-47,
0x1.bb8e0b79a6f66p-1, 0x1.d900p-47,
0x1.bcc1e904bc1ffp-1, 0x1.2a00p-47,
0x1.bdf69c3f3a16fp-1, -0x1.f780p-46,
0x1.bf2c25bd71db8p-1, -0x1.0a00p-46,
0x1.c06286141b2e9p-1, -0x1.1400p-46,
0x1.c199bdd8552e0p-1, 0x1.be00p-47,
0x1.c199bdd8552e0p-1, 0x1.be00p-47,
0x1.c2d1cd9fa64eep-1, -0x1.9400p-47,
0x1.c40ab5fffd02fp-1, -0x1.ed00p-47,
0x1.c544778fafd15p-1, 0x1.9660p-44,
0x1.c544778fafd15p-1, 0x1.9660p-44,
0x1.c67f12e57d0cbp-1, -0x1.a100p-46,
0x1.c7ba88988c1b6p-1, -0x1.8458p-42,
0x1.c8f6d9406e733p-1, -0x1.a480p-46,
0x1.ca3405751c4dfp-1, 0x1.b000p-51,
0x1.cb720dcef9094p-1, 0x1.1400p-47,
0x1.ccb0f2e6d1689p-1, 0x1.0200p-48,
0x1.cdf0b555dc412p-1, 0x1.3600p-48,
0x1.ca3405751c4dfp-1, 0x1.b000p-51,
0x1.cb720dcef9094p-1, 0x1.1400p-47,
0x1.ccb0f2e6d1689p-1, 0x1.0200p-48,
0x1.cdf0b555dc412p-1, 0x1.3600p-48,
0x1.cf3155b5bab3bp-1, -0x1.6900p-47,
0x1.d072d4a0789bcp-1, 0x1.9a00p-47,
0x1.d072d4a0789bcp-1, 0x1.9a00p-47,
0x1.d1b532b08c8fap-1, -0x1.5e00p-46,
0x1.d2f87080d8a85p-1, 0x1.d280p-46,
0x1.d43c8eacaa203p-1, 0x1.1a00p-47,
0x1.d5818dcfba491p-1, 0x1.f000p-50,
0x1.d2f87080d8a85p-1, 0x1.d280p-46,
0x1.d43c8eacaa203p-1, 0x1.1a00p-47,
0x1.d5818dcfba491p-1, 0x1.f000p-50,
0x1.d6c76e862e6a1p-1, -0x1.3a00p-47,
0x1.d80e316c9834ep-1, -0x1.cd80p-47,
0x1.d955d71ff6090p-1, 0x1.4c00p-48,
0x1.da9e603db32aep-1, 0x1.f900p-48,
0x1.dbe7cd63a8325p-1, 0x1.9800p-49,
0x1.d955d71ff6090p-1, 0x1.4c00p-48,
0x1.da9e603db32aep-1, 0x1.f900p-48,
0x1.dbe7cd63a8325p-1, 0x1.9800p-49,
0x1.dd321f301b445p-1, -0x1.5200p-48,
0x1.de7d5641c05bfp-1, -0x1.d700p-46,
0x1.dfc97337b9aecp-1, -0x1.6140p-46,
0x1.e11676b197d5ep-1, 0x1.b480p-47,
0x1.e264614f5a3e7p-1, 0x1.0ce0p-43,
0x1.e3b333b16ee5cp-1, 0x1.c680p-47,
0x1.e11676b197d5ep-1, 0x1.b480p-47,
0x1.e264614f5a3e7p-1, 0x1.0ce0p-43,
0x1.e3b333b16ee5cp-1, 0x1.c680p-47,
0x1.e502ee78b3fb4p-1, -0x1.9300p-47,
0x1.e653924676d68p-1, -0x1.5000p-49,
0x1.e7a51fbc74c44p-1, -0x1.7f80p-47,
0x1.e8f7977cdb726p-1, -0x1.3700p-48,
0x1.ea4afa2a490e8p-1, 0x1.5d00p-49,
0x1.eb9f4867ccae4p-1, 0x1.61a0p-46,
0x1.ecf482d8e680dp-1, 0x1.5500p-48,
0x1.ee4aaa2188514p-1, 0x1.6400p-51,
0x1.ea4afa2a490e8p-1, 0x1.5d00p-49,
0x1.eb9f4867ccae4p-1, 0x1.61a0p-46,
0x1.ecf482d8e680dp-1, 0x1.5500p-48,
0x1.ee4aaa2188514p-1, 0x1.6400p-51,
0x1.efa1bee615a13p-1, -0x1.e800p-49,
0x1.f0f9c1cb64106p-1, -0x1.a880p-48,
0x1.f252b376bb963p-1, -0x1.c900p-45,
0x1.f3ac948dd7275p-1, 0x1.a000p-53,
0x1.f3ac948dd7275p-1, 0x1.a000p-53,
0x1.f50765b6e4524p-1, -0x1.4f00p-48,
0x1.f6632798844fdp-1, 0x1.a800p-51,
0x1.f7bfdad9cbe38p-1, 0x1.abc0p-48,
0x1.f6632798844fdp-1, 0x1.a800p-51,
0x1.f7bfdad9cbe38p-1, 0x1.abc0p-48,
0x1.f91d802243c82p-1, -0x1.4600p-50,
0x1.fa7c1819e908ep-1, -0x1.b0c0p-47,
0x1.fbdba3692d511p-1, -0x1.0e00p-51,
0x1.fd3c22b8f7194p-1, -0x1.0de8p-46,
0x1.fe9d96b2a23eep-1, 0x1.e430p-49,
0x1.0000000000000p+0, 0x0.0000p+0,
0x1.fe9d96b2a23eep-1, 0x1.e430p-49,
0x1.0000000000000p+0, 0x0.0000p+0,
0x1.00b1afa5abcbep+0, -0x1.3400p-52,
0x1.0163da9fb3303p+0, -0x1.2170p-46,
0x1.02168143b0282p+0, 0x1.a400p-52,
0x1.02c9a3e77806cp+0, 0x1.f980p-49,
0x1.02168143b0282p+0, 0x1.a400p-52,
0x1.02c9a3e77806cp+0, 0x1.f980p-49,
0x1.037d42e11bbcap+0, -0x1.7400p-51,
0x1.04315e86e7f89p+0, 0x1.8300p-50,
0x1.04315e86e7f89p+0, 0x1.8300p-50,
0x1.04e5f72f65467p+0, -0x1.a3f0p-46,
0x1.059b0d315855ap+0, -0x1.2840p-47,
0x1.0650a0e3c1f95p+0, 0x1.1600p-48,
0x1.0706b29ddf71ap+0, 0x1.5240p-46,
0x1.0650a0e3c1f95p+0, 0x1.1600p-48,
0x1.0706b29ddf71ap+0, 0x1.5240p-46,
0x1.07bd42b72a82dp+0, -0x1.9a00p-49,
0x1.0874518759bd0p+0, 0x1.6400p-49,
0x1.0874518759bd0p+0, 0x1.6400p-49,
0x1.092bdf66607c8p+0, -0x1.0780p-47,
0x1.09e3ecac6f383p+0, -0x1.8000p-54,
0x1.0a9c79b1f3930p+0, 0x1.fa00p-48,
0x1.0a9c79b1f3930p+0, 0x1.fa00p-48,
0x1.0b5586cf988fcp+0, -0x1.ac80p-48,
0x1.0c0f145e46c8ap+0, 0x1.9c00p-50,
0x1.0cc922b724816p+0, 0x1.5200p-47,
0x1.0c0f145e46c8ap+0, 0x1.9c00p-50,
0x1.0cc922b724816p+0, 0x1.5200p-47,
0x1.0d83b23395dd8p+0, -0x1.ad00p-48,
0x1.0e3ec32d3d1f3p+0, 0x1.bac0p-46,
0x1.0e3ec32d3d1f3p+0, 0x1.bac0p-46,
0x1.0efa55fdfa9a6p+0, -0x1.4e80p-47,
0x1.0fb66affed2f0p+0, -0x1.d300p-47,
0x1.1073028d7234bp+0, 0x1.1500p-48,
0x1.11301d0125b5bp+0, 0x1.c000p-49,
0x1.11edbab5e2af9p+0, 0x1.6bc0p-46,
0x1.12abdc06c31d5p+0, 0x1.8400p-49,
0x1.1073028d7234bp+0, 0x1.1500p-48,
0x1.11301d0125b5bp+0, 0x1.c000p-49,
0x1.11edbab5e2af9p+0, 0x1.6bc0p-46,
0x1.12abdc06c31d5p+0, 0x1.8400p-49,
0x1.136a814f2047dp+0, -0x1.ed00p-47,
0x1.1429aaea92de9p+0, 0x1.8e00p-49,
0x1.14e95934f3138p+0, 0x1.b400p-49,
0x1.15a98c8a58e71p+0, 0x1.5300p-47,
0x1.166a45471c3dfp+0, 0x1.3380p-47,
0x1.172b83c7d5211p+0, 0x1.8d40p-45,
0x1.1429aaea92de9p+0, 0x1.8e00p-49,
0x1.14e95934f3138p+0, 0x1.b400p-49,
0x1.15a98c8a58e71p+0, 0x1.5300p-47,
0x1.166a45471c3dfp+0, 0x1.3380p-47,
0x1.172b83c7d5211p+0, 0x1.8d40p-45,
0x1.17ed48695bb9fp+0, -0x1.5d00p-47,
0x1.18af9388c8d93p+0, -0x1.c880p-46,
0x1.1972658375d66p+0, 0x1.1f00p-46,
0x1.1a35beb6fcba7p+0, 0x1.0480p-46,
0x1.1972658375d66p+0, 0x1.1f00p-46,
0x1.1a35beb6fcba7p+0, 0x1.0480p-46,
0x1.1af99f81387e3p+0, -0x1.7390p-43,
0x1.1bbe084045d54p+0, 0x1.4e40p-45,
0x1.1bbe084045d54p+0, 0x1.4e40p-45,
0x1.1c82f95281c43p+0, -0x1.a200p-47,
0x1.1d4873168b9b2p+0, 0x1.3800p-49,
0x1.1e0e75eb44031p+0, 0x1.ac00p-49,
0x1.1ed5022fcd938p+0, 0x1.1900p-47,
0x1.1d4873168b9b2p+0, 0x1.3800p-49,
0x1.1e0e75eb44031p+0, 0x1.ac00p-49,
0x1.1ed5022fcd938p+0, 0x1.1900p-47,
0x1.1f9c18438cdf7p+0, -0x1.b780p-46,
0x1.2063b88628d8fp+0, 0x1.d940p-45,
0x1.212be3578a81ep+0, 0x1.8000p-50,
0x1.21f49917ddd41p+0, 0x1.b340p-45,
0x1.22bdda2791323p+0, 0x1.9f80p-46,
0x1.2063b88628d8fp+0, 0x1.d940p-45,
0x1.212be3578a81ep+0, 0x1.8000p-50,
0x1.21f49917ddd41p+0, 0x1.b340p-45,
0x1.22bdda2791323p+0, 0x1.9f80p-46,
0x1.2387a6e7561e7p+0, -0x1.9c80p-46,
0x1.2451ffb821427p+0, 0x1.2300p-47,
0x1.2451ffb821427p+0, 0x1.2300p-47,
0x1.251ce4fb2a602p+0, -0x1.3480p-46,
0x1.25e85711eceb0p+0, 0x1.2700p-46,
0x1.26b4565e27d16p+0, 0x1.1d00p-46,
0x1.2780e341de00fp+0, 0x1.1ee0p-44,
0x1.25e85711eceb0p+0, 0x1.2700p-46,
0x1.26b4565e27d16p+0, 0x1.1d00p-46,
0x1.2780e341de00fp+0, 0x1.1ee0p-44,
0x1.284dfe1f5633ep+0, -0x1.4c00p-46,
0x1.291ba7591bb30p+0, -0x1.3d80p-46,
0x1.29e9df51fdf09p+0, 0x1.8b00p-47,
0x1.29e9df51fdf09p+0, 0x1.8b00p-47,
0x1.2ab8a66d10e9bp+0, -0x1.27c0p-45,
0x1.2b87fd0dada3ap+0, 0x1.a340p-45,
0x1.2b87fd0dada3ap+0, 0x1.a340p-45,
0x1.2c57e39771af9p+0, -0x1.0800p-46,
0x1.2d285a6e402d9p+0, -0x1.ed00p-47,
0x1.2df961f641579p+0, -0x1.4200p-48,
@ -290,78 +290,78 @@ const exp2dt = []f64 {
0x1.31432edeea50bp+0, -0x1.0df8p-40,
0x1.32170fc4cd7b8p+0, -0x1.2480p-45,
0x1.32eb83ba8e9a2p+0, -0x1.5980p-45,
0x1.33c08b2641766p+0, 0x1.ed00p-46,
0x1.33c08b2641766p+0, 0x1.ed00p-46,
0x1.3496266e3fa27p+0, -0x1.c000p-50,
0x1.356c55f929f0fp+0, -0x1.0d80p-44,
0x1.36431a2de88b9p+0, 0x1.2c80p-45,
0x1.371a7373aaa39p+0, 0x1.0600p-45,
0x1.36431a2de88b9p+0, 0x1.2c80p-45,
0x1.371a7373aaa39p+0, 0x1.0600p-45,
0x1.37f26231e74fep+0, -0x1.6600p-46,
0x1.38cae6d05d838p+0, -0x1.ae00p-47,
0x1.39a401b713ec3p+0, -0x1.4720p-43,
0x1.3a7db34e5a020p+0, 0x1.8200p-47,
0x1.3b57fbfec6e95p+0, 0x1.e800p-44,
0x1.3c32dc313a8f2p+0, 0x1.f800p-49,
0x1.3a7db34e5a020p+0, 0x1.8200p-47,
0x1.3b57fbfec6e95p+0, 0x1.e800p-44,
0x1.3c32dc313a8f2p+0, 0x1.f800p-49,
0x1.3d0e544ede122p+0, -0x1.7a00p-46,
0x1.3dea64c1234bbp+0, 0x1.6300p-45,
0x1.3dea64c1234bbp+0, 0x1.6300p-45,
0x1.3ec70df1c4eccp+0, -0x1.8a60p-43,
0x1.3fa4504ac7e8cp+0, -0x1.cdc0p-44,
0x1.40822c367a0bbp+0, 0x1.5b80p-45,
0x1.4160a21f72e95p+0, 0x1.ec00p-46,
0x1.40822c367a0bbp+0, 0x1.5b80p-45,
0x1.4160a21f72e95p+0, 0x1.ec00p-46,
0x1.423fb27094646p+0, -0x1.3600p-46,
0x1.431f5d950a920p+0, 0x1.3980p-45,
0x1.43ffa3f84b9ebp+0, 0x1.a000p-48,
0x1.431f5d950a920p+0, 0x1.3980p-45,
0x1.43ffa3f84b9ebp+0, 0x1.a000p-48,
0x1.44e0860618919p+0, -0x1.6c00p-48,
0x1.45c2042a7d201p+0, -0x1.bc00p-47,
0x1.46a41ed1d0016p+0, -0x1.2800p-46,
0x1.4786d668b3326p+0, 0x1.0e00p-44,
0x1.4786d668b3326p+0, 0x1.0e00p-44,
0x1.486a2b5c13c00p+0, -0x1.d400p-45,
0x1.494e1e192af04p+0, 0x1.c200p-47,
0x1.494e1e192af04p+0, 0x1.c200p-47,
0x1.4a32af0d7d372p+0, -0x1.e500p-46,
0x1.4b17dea6db801p+0, 0x1.7800p-47,
0x1.4b17dea6db801p+0, 0x1.7800p-47,
0x1.4bfdad53629e1p+0, -0x1.3800p-46,
0x1.4ce41b817c132p+0, 0x1.0800p-47,
0x1.4dcb299fddddbp+0, 0x1.c700p-45,
0x1.4ce41b817c132p+0, 0x1.0800p-47,
0x1.4dcb299fddddbp+0, 0x1.c700p-45,
0x1.4eb2d81d8ab96p+0, -0x1.ce00p-46,
0x1.4f9b2769d2d02p+0, 0x1.9200p-46,
0x1.4f9b2769d2d02p+0, 0x1.9200p-46,
0x1.508417f4531c1p+0, -0x1.8c00p-47,
0x1.516daa2cf662ap+0, -0x1.a000p-48,
0x1.5257de83f51eap+0, 0x1.a080p-43,
0x1.5257de83f51eap+0, 0x1.a080p-43,
0x1.5342b569d4edap+0, -0x1.6d80p-45,
0x1.542e2f4f6ac1ap+0, -0x1.2440p-44,
0x1.551a4ca5d94dbp+0, 0x1.83c0p-43,
0x1.56070dde9116bp+0, 0x1.4b00p-45,
0x1.56f4736b529dep+0, 0x1.15a0p-43,
0x1.551a4ca5d94dbp+0, 0x1.83c0p-43,
0x1.56070dde9116bp+0, 0x1.4b00p-45,
0x1.56f4736b529dep+0, 0x1.15a0p-43,
0x1.57e27dbe2c40ep+0, -0x1.9e00p-45,
0x1.58d12d497c76fp+0, -0x1.3080p-45,
0x1.59c0827ff0b4cp+0, 0x1.dec0p-43,
0x1.59c0827ff0b4cp+0, 0x1.dec0p-43,
0x1.5ab07dd485427p+0, -0x1.4000p-51,
0x1.5ba11fba87af4p+0, 0x1.0080p-44,
0x1.5ba11fba87af4p+0, 0x1.0080p-44,
0x1.5c9268a59460bp+0, -0x1.6c80p-45,
0x1.5d84590998e3fp+0, 0x1.69a0p-43,
0x1.5d84590998e3fp+0, 0x1.69a0p-43,
0x1.5e76f15ad20e1p+0, -0x1.b400p-46,
0x1.5f6a320dcebcap+0, 0x1.7700p-46,
0x1.605e1b976dcb8p+0, 0x1.6f80p-45,
0x1.6152ae6cdf715p+0, 0x1.1000p-47,
0x1.5f6a320dcebcap+0, 0x1.7700p-46,
0x1.605e1b976dcb8p+0, 0x1.6f80p-45,
0x1.6152ae6cdf715p+0, 0x1.1000p-47,
0x1.6247eb03a5531p+0, -0x1.5d00p-46,
0x1.633dd1d1929b5p+0, -0x1.2d00p-46,
0x1.6434634ccc313p+0, -0x1.a800p-49,
0x1.652b9febc8efap+0, -0x1.8600p-45,
0x1.6623882553397p+0, 0x1.1fe0p-40,
0x1.6623882553397p+0, 0x1.1fe0p-40,
0x1.671c1c708328ep+0, -0x1.7200p-44,
0x1.68155d44ca97ep+0, 0x1.6800p-49,
0x1.68155d44ca97ep+0, 0x1.6800p-49,
0x1.690f4b19e9471p+0, -0x1.9780p-45,
};
fn exp2_64(x: f64) f64 {
@setFloatMode(this, @import("builtin").FloatMode.Strict);
const tblsiz = u32(exp2dt.len / 2);
const tblsiz = u32(exp2dt.len / 2);
const redux: f64 = 0x1.8p52 / f64(tblsiz);
const P1: f64 = 0x1.62e42fefa39efp-1;
const P2: f64 = 0x1.ebfbdff82c575p-3;
const P3: f64 = 0x1.c6b08d704a0a6p-5;
const P4: f64 = 0x1.3b2ab88f70400p-7;
const P5: f64 = 0x1.5d88003875c74p-10;
const P1: f64 = 0x1.62e42fefa39efp-1;
const P2: f64 = 0x1.ebfbdff82c575p-3;
const P3: f64 = 0x1.c6b08d704a0a6p-5;
const P4: f64 = 0x1.3b2ab88f70400p-7;
const P5: f64 = 0x1.5d88003875c74p-10;
const ux = @bitCast(u64, x);
const ix = u32(ux >> 32) & 0x7FFFFFFF;

View File

@ -21,11 +21,11 @@ pub fn expm1(x: var) @typeOf(x) {
fn expm1_32(x_: f32) f32 {
@setFloatMode(this, builtin.FloatMode.Strict);
const o_threshold: f32 = 8.8721679688e+01;
const ln2_hi: f32 = 6.9313812256e-01;
const ln2_lo: f32 = 9.0580006145e-06;
const invln2: f32 = 1.4426950216e+00;
const ln2_hi: f32 = 6.9313812256e-01;
const ln2_lo: f32 = 9.0580006145e-06;
const invln2: f32 = 1.4426950216e+00;
const Q1: f32 = -3.3333212137e-2;
const Q2: f32 = 1.5807170421e-3;
const Q2: f32 = 1.5807170421e-3;
var x = x_;
const ux = @bitCast(u32, x);
@ -93,8 +93,7 @@ fn expm1_32(x_: f32) f32 {
math.forceEval(x * x);
}
return x;
}
else {
} else {
k = 0;
}
@ -148,13 +147,13 @@ fn expm1_32(x_: f32) f32 {
fn expm1_64(x_: f64) f64 {
@setFloatMode(this, builtin.FloatMode.Strict);
const o_threshold: f64 = 7.09782712893383973096e+02;
const ln2_hi: f64 = 6.93147180369123816490e-01;
const ln2_lo: f64 = 1.90821492927058770002e-10;
const invln2: f64 = 1.44269504088896338700e+00;
const ln2_hi: f64 = 6.93147180369123816490e-01;
const ln2_lo: f64 = 1.90821492927058770002e-10;
const invln2: f64 = 1.44269504088896338700e+00;
const Q1: f64 = -3.33333333333331316428e-02;
const Q2: f64 = 1.58730158725481460165e-03;
const Q2: f64 = 1.58730158725481460165e-03;
const Q3: f64 = -7.93650757867487942473e-05;
const Q4: f64 = 4.00821782732936239552e-06;
const Q4: f64 = 4.00821782732936239552e-06;
const Q5: f64 = -2.01099218183624371326e-07;
var x = x_;
@ -223,8 +222,7 @@ fn expm1_64(x_: f64) f64 {
math.forceEval(f32(x));
}
return x;
}
else {
} else {
k = 0;
}

View File

@ -501,7 +501,7 @@ test "math.negateCast" {
if (negateCast(u32(@maxValue(i32) + 10))) |_| unreachable else |err| assert(err == error.Overflow);
}
/// Cast an integer to a different integer type. If the value doesn't fit,
/// Cast an integer to a different integer type. If the value doesn't fit,
/// return an error.
pub fn cast(comptime T: type, x: var) (error{Overflow}!T) {
comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer

View File

@ -138,8 +138,7 @@ fn log1p_64(x: f64) f64 {
c = 0;
f = x;
}
}
else if (hx >= 0x7FF00000) {
} else if (hx >= 0x7FF00000) {
return x;
}

View File

@ -28,7 +28,6 @@ const assert = std.debug.assert;
// This implementation is taken from the go stlib, musl is a bit more complex.
pub fn pow(comptime T: type, x: T, y: T) T {
@setFloatMode(this, @import("builtin").FloatMode.Strict);
if (T != f32 and T != f64) {

View File

@ -19,24 +19,30 @@ pub const Address = struct {
os_addr: OsAddress,
pub fn initIp4(ip4: u32, port: u16) Address {
return Address{ .os_addr = posix.sockaddr{ .in = posix.sockaddr_in{
.family = posix.AF_INET,
.port = std.mem.endianSwapIfLe(u16, port),
.addr = ip4,
.zero = []u8{0} ** 8,
} } };
return Address{
.os_addr = posix.sockaddr{
.in = posix.sockaddr_in{
.family = posix.AF_INET,
.port = std.mem.endianSwapIfLe(u16, port),
.addr = ip4,
.zero = []u8{0} ** 8,
},
},
};
}
pub fn initIp6(ip6: &const Ip6Addr, port: u16) Address {
return Address{
.family = posix.AF_INET6,
.os_addr = posix.sockaddr{ .in6 = posix.sockaddr_in6{
.family = posix.AF_INET6,
.port = std.mem.endianSwapIfLe(u16, port),
.flowinfo = 0,
.addr = ip6.addr,
.scope_id = ip6.scope_id,
} },
.os_addr = posix.sockaddr{
.in6 = posix.sockaddr_in6{
.family = posix.AF_INET6,
.port = std.mem.endianSwapIfLe(u16, port),
.flowinfo = 0,
.addr = ip6.addr,
.scope_id = ip6.scope_id,
},
},
};
}

View File

@ -387,15 +387,12 @@ pub const ChildProcess = struct {
const pid_err = posix.getErrno(pid_result);
if (pid_err > 0) {
return switch (pid_err) {
posix.EAGAIN,
posix.ENOMEM,
posix.ENOSYS => error.SystemResources,
posix.EAGAIN, posix.ENOMEM, posix.ENOSYS => error.SystemResources,
else => os.unexpectedErrorPosix(pid_err),
};
}
if (pid_result == 0) {
// we are the child
setUpChildIo(self.stdin_behavior, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
setUpChildIo(self.stdout_behavior, stdout_pipe[1], posix.STDOUT_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
setUpChildIo(self.stderr_behavior, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
@ -646,8 +643,7 @@ fn windowsCreateProcess(app_name: &u8, cmd_line: &u8, envp_ptr: ?&u8, cwd_ptr: ?
if (windows.CreateProcessA(app_name, cmd_line, null, null, windows.TRUE, 0, @ptrCast(?&c_void, envp_ptr), cwd_ptr, lpStartupInfo, lpProcessInformation) == 0) {
const err = windows.GetLastError();
return switch (err) {
windows.ERROR.FILE_NOT_FOUND,
windows.ERROR.PATH_NOT_FOUND => error.FileNotFound,
windows.ERROR.FILE_NOT_FOUND, windows.ERROR.PATH_NOT_FOUND => error.FileNotFound,
windows.ERROR.INVALID_PARAMETER => unreachable,
windows.ERROR.INVALID_NAME => error.InvalidName,
else => os.unexpectedErrorWindows(err),
@ -745,8 +741,7 @@ fn makePipe() ![2]i32 {
const err = posix.getErrno(posix.pipe(&fds));
if (err > 0) {
return switch (err) {
posix.EMFILE,
posix.ENFILE => error.SystemResources,
posix.EMFILE, posix.ENFILE => error.SystemResources,
else => os.unexpectedErrorPosix(err),
};
}

View File

@ -12,52 +12,71 @@ pub const STDERR_FILENO = 2;
/// [MC2] no permissions
pub const PROT_NONE = 0x00;
/// [MC2] pages can be read
pub const PROT_READ = 0x01;
/// [MC2] pages can be written
pub const PROT_WRITE = 0x02;
/// [MC2] pages can be executed
pub const PROT_EXEC = 0x04;
/// allocated from memory, swap space
pub const MAP_ANONYMOUS = 0x1000;
/// map from file (default)
pub const MAP_FILE = 0x0000;
/// interpret addr exactly
pub const MAP_FIXED = 0x0010;
/// region may contain semaphores
pub const MAP_HASSEMAPHORE = 0x0200;
/// changes are private
pub const MAP_PRIVATE = 0x0002;
/// share changes
pub const MAP_SHARED = 0x0001;
/// don't cache pages for this mapping
pub const MAP_NOCACHE = 0x0400;
/// don't reserve needed swap area
pub const MAP_NORESERVE = 0x0040;
pub const MAP_FAILED = @maxValue(usize);
/// [XSI] no hang in wait/no child to reap
pub const WNOHANG = 0x00000001;
/// [XSI] notify on stop, untraced child
pub const WUNTRACED = 0x00000002;
/// take signal on signal stack
pub const SA_ONSTACK = 0x0001;
/// restart system on signal return
pub const SA_RESTART = 0x0002;
/// reset to SIG_DFL when taking signal
pub const SA_RESETHAND = 0x0004;
/// do not generate SIGCHLD on child stop
pub const SA_NOCLDSTOP = 0x0008;
/// don't mask the signal we're delivering
pub const SA_NODEFER = 0x0010;
/// don't keep zombies around
pub const SA_NOCLDWAIT = 0x0020;
/// signal handler with SA_SIGINFO args
pub const SA_SIGINFO = 0x0040;
/// do not bounce off kernel's sigtramp
pub const SA_USERTRAMP = 0x0100;
/// signal handler with SA_SIGINFO args with 64bit regs information
pub const SA_64REGSET = 0x0200;
@ -71,30 +90,43 @@ pub const R_OK = 4;
/// open for reading only
pub const O_RDONLY = 0x0000;
/// open for writing only
pub const O_WRONLY = 0x0001;
/// open for reading and writing
pub const O_RDWR = 0x0002;
/// do not block on open or for data to become available
pub const O_NONBLOCK = 0x0004;
/// append on each write
pub const O_APPEND = 0x0008;
/// create file if it does not exist
pub const O_CREAT = 0x0200;
/// truncate size to 0
pub const O_TRUNC = 0x0400;
/// error if O_CREAT and the file exists
pub const O_EXCL = 0x0800;
/// atomically obtain a shared lock
pub const O_SHLOCK = 0x0010;
/// atomically obtain an exclusive lock
pub const O_EXLOCK = 0x0020;
/// do not follow symlinks
pub const O_NOFOLLOW = 0x0100;
/// allow open of symlinks
pub const O_SYMLINK = 0x200000;
/// descriptor requested for event notifications only
pub const O_EVTONLY = 0x8000;
/// mark as close-on-exec
pub const O_CLOEXEC = 0x1000000;
@ -126,75 +158,109 @@ pub const DT_WHT = 14;
/// block specified signal set
pub const SIG_BLOCK = 1;
/// unblock specified signal set
pub const SIG_UNBLOCK = 2;
/// set specified signal set
pub const SIG_SETMASK = 3;
/// hangup
pub const SIGHUP = 1;
/// interrupt
pub const SIGINT = 2;
/// quit
pub const SIGQUIT = 3;
/// illegal instruction (not reset when caught)
pub const SIGILL = 4;
/// trace trap (not reset when caught)
pub const SIGTRAP = 5;
/// abort()
pub const SIGABRT = 6;
/// pollable event ([XSR] generated, not supported)
pub const SIGPOLL = 7;
/// compatibility
pub const SIGIOT = SIGABRT;
/// EMT instruction
pub const SIGEMT = 7;
/// floating point exception
pub const SIGFPE = 8;
/// kill (cannot be caught or ignored)
pub const SIGKILL = 9;
/// bus error
pub const SIGBUS = 10;
/// segmentation violation
pub const SIGSEGV = 11;
/// bad argument to system call
pub const SIGSYS = 12;
/// write on a pipe with no one to read it
pub const SIGPIPE = 13;
/// alarm clock
pub const SIGALRM = 14;
/// software termination signal from kill
pub const SIGTERM = 15;
/// urgent condition on IO channel
pub const SIGURG = 16;
/// sendable stop signal not from tty
pub const SIGSTOP = 17;
/// stop signal from tty
pub const SIGTSTP = 18;
/// continue a stopped process
pub const SIGCONT = 19;
/// to parent on child stop or exit
pub const SIGCHLD = 20;
/// to readers pgrp upon background tty read
pub const SIGTTIN = 21;
/// like TTIN for output if (tp->t_local&LTOSTOP)
pub const SIGTTOU = 22;
/// input/output possible signal
pub const SIGIO = 23;
/// exceeded CPU time limit
pub const SIGXCPU = 24;
/// exceeded file size limit
pub const SIGXFSZ = 25;
/// virtual time alarm
pub const SIGVTALRM = 26;
/// profiling time alarm
pub const SIGPROF = 27;
/// window size changes
pub const SIGWINCH = 28;
/// information request
pub const SIGINFO = 29;
/// user defined signal 1
pub const SIGUSR1 = 30;
/// user defined signal 2
pub const SIGUSR2 = 31;

View File

@ -1,142 +1,328 @@
/// Operation not permitted
pub const EPERM = 1;
pub const EPERM = 1; /// Operation not permitted
pub const ENOENT = 2; /// No such file or directory
pub const ESRCH = 3; /// No such process
pub const EINTR = 4; /// Interrupted system call
pub const EIO = 5; /// Input/output error
pub const ENXIO = 6; /// Device not configured
pub const E2BIG = 7; /// Argument list too long
pub const ENOEXEC = 8; /// Exec format error
pub const EBADF = 9; /// Bad file descriptor
pub const ECHILD = 10; /// No child processes
pub const EDEADLK = 11; /// Resource deadlock avoided
/// No such file or directory
pub const ENOENT = 2;
pub const ENOMEM = 12; /// Cannot allocate memory
pub const EACCES = 13; /// Permission denied
pub const EFAULT = 14; /// Bad address
pub const ENOTBLK = 15; /// Block device required
pub const EBUSY = 16; /// Device / Resource busy
pub const EEXIST = 17; /// File exists
pub const EXDEV = 18; /// Cross-device link
pub const ENODEV = 19; /// Operation not supported by device
pub const ENOTDIR = 20; /// Not a directory
pub const EISDIR = 21; /// Is a directory
pub const EINVAL = 22; /// Invalid argument
pub const ENFILE = 23; /// Too many open files in system
pub const EMFILE = 24; /// Too many open files
pub const ENOTTY = 25; /// Inappropriate ioctl for device
pub const ETXTBSY = 26; /// Text file busy
pub const EFBIG = 27; /// File too large
pub const ENOSPC = 28; /// No space left on device
pub const ESPIPE = 29; /// Illegal seek
pub const EROFS = 30; /// Read-only file system
pub const EMLINK = 31; /// Too many links
pub const EPIPE = 32; /// Broken pipe
/// No such process
pub const ESRCH = 3;
/// Interrupted system call
pub const EINTR = 4;
/// Input/output error
pub const EIO = 5;
/// Device not configured
pub const ENXIO = 6;
/// Argument list too long
pub const E2BIG = 7;
/// Exec format error
pub const ENOEXEC = 8;
/// Bad file descriptor
pub const EBADF = 9;
/// No child processes
pub const ECHILD = 10;
/// Resource deadlock avoided
pub const EDEADLK = 11;
/// Cannot allocate memory
pub const ENOMEM = 12;
/// Permission denied
pub const EACCES = 13;
/// Bad address
pub const EFAULT = 14;
/// Block device required
pub const ENOTBLK = 15;
/// Device / Resource busy
pub const EBUSY = 16;
/// File exists
pub const EEXIST = 17;
/// Cross-device link
pub const EXDEV = 18;
/// Operation not supported by device
pub const ENODEV = 19;
/// Not a directory
pub const ENOTDIR = 20;
/// Is a directory
pub const EISDIR = 21;
/// Invalid argument
pub const EINVAL = 22;
/// Too many open files in system
pub const ENFILE = 23;
/// Too many open files
pub const EMFILE = 24;
/// Inappropriate ioctl for device
pub const ENOTTY = 25;
/// Text file busy
pub const ETXTBSY = 26;
/// File too large
pub const EFBIG = 27;
/// No space left on device
pub const ENOSPC = 28;
/// Illegal seek
pub const ESPIPE = 29;
/// Read-only file system
pub const EROFS = 30;
/// Too many links
pub const EMLINK = 31;
/// Broken pipe
// math software
pub const EDOM = 33; /// Numerical argument out of domain
pub const ERANGE = 34; /// Result too large
pub const EPIPE = 32;
/// Numerical argument out of domain
pub const EDOM = 33;
/// Result too large
// non-blocking and interrupt i/o
pub const EAGAIN = 35; /// Resource temporarily unavailable
pub const EWOULDBLOCK = EAGAIN; /// Operation would block
pub const EINPROGRESS = 36; /// Operation now in progress
pub const EALREADY = 37; /// Operation already in progress
pub const ERANGE = 34;
/// Resource temporarily unavailable
pub const EAGAIN = 35;
/// Operation would block
pub const EWOULDBLOCK = EAGAIN;
/// Operation now in progress
pub const EINPROGRESS = 36;
/// Operation already in progress
// ipc/network software -- argument errors
pub const ENOTSOCK = 38; /// Socket operation on non-socket
pub const EDESTADDRREQ = 39; /// Destination address required
pub const EMSGSIZE = 40; /// Message too long
pub const EPROTOTYPE = 41; /// Protocol wrong type for socket
pub const ENOPROTOOPT = 42; /// Protocol not available
pub const EPROTONOSUPPORT = 43; /// Protocol not supported
pub const EALREADY = 37;
pub const ESOCKTNOSUPPORT = 44; /// Socket type not supported
/// Socket operation on non-socket
pub const ENOTSOCK = 38;
pub const ENOTSUP = 45; /// Operation not supported
/// Destination address required
pub const EDESTADDRREQ = 39;
pub const EPFNOSUPPORT = 46; /// Protocol family not supported
pub const EAFNOSUPPORT = 47; /// Address family not supported by protocol family
pub const EADDRINUSE = 48; /// Address already in use
pub const EADDRNOTAVAIL = 49; /// Can't assign requested address
/// Message too long
pub const EMSGSIZE = 40;
/// Protocol wrong type for socket
pub const EPROTOTYPE = 41;
/// Protocol not available
pub const ENOPROTOOPT = 42;
/// Protocol not supported
pub const EPROTONOSUPPORT = 43;
/// Socket type not supported
pub const ESOCKTNOSUPPORT = 44;
/// Operation not supported
pub const ENOTSUP = 45;
/// Protocol family not supported
pub const EPFNOSUPPORT = 46;
/// Address family not supported by protocol family
pub const EAFNOSUPPORT = 47;
/// Address already in use
pub const EADDRINUSE = 48;
/// Can't assign requested address
// ipc/network software -- operational errors
pub const ENETDOWN = 50; /// Network is down
pub const ENETUNREACH = 51; /// Network is unreachable
pub const ENETRESET = 52; /// Network dropped connection on reset
pub const ECONNABORTED = 53; /// Software caused connection abort
pub const ECONNRESET = 54; /// Connection reset by peer
pub const ENOBUFS = 55; /// No buffer space available
pub const EISCONN = 56; /// Socket is already connected
pub const ENOTCONN = 57; /// Socket is not connected
pub const EADDRNOTAVAIL = 49;
pub const ESHUTDOWN = 58; /// Can't send after socket shutdown
pub const ETOOMANYREFS = 59; /// Too many references: can't splice
/// Network is down
pub const ENETDOWN = 50;
pub const ETIMEDOUT = 60; /// Operation timed out
pub const ECONNREFUSED = 61; /// Connection refused
/// Network is unreachable
pub const ENETUNREACH = 51;
pub const ELOOP = 62; /// Too many levels of symbolic links
pub const ENAMETOOLONG = 63; /// File name too long
/// Network dropped connection on reset
pub const ENETRESET = 52;
pub const EHOSTDOWN = 64; /// Host is down
pub const EHOSTUNREACH = 65; /// No route to host
pub const ENOTEMPTY = 66; /// Directory not empty
/// Software caused connection abort
pub const ECONNABORTED = 53;
/// Connection reset by peer
pub const ECONNRESET = 54;
/// No buffer space available
pub const ENOBUFS = 55;
/// Socket is already connected
pub const EISCONN = 56;
/// Socket is not connected
pub const ENOTCONN = 57;
/// Can't send after socket shutdown
pub const ESHUTDOWN = 58;
/// Too many references: can't splice
pub const ETOOMANYREFS = 59;
/// Operation timed out
pub const ETIMEDOUT = 60;
/// Connection refused
pub const ECONNREFUSED = 61;
/// Too many levels of symbolic links
pub const ELOOP = 62;
/// File name too long
pub const ENAMETOOLONG = 63;
/// Host is down
pub const EHOSTDOWN = 64;
/// No route to host
pub const EHOSTUNREACH = 65;
/// Directory not empty
// quotas & mush
pub const EPROCLIM = 67; /// Too many processes
pub const EUSERS = 68; /// Too many users
pub const EDQUOT = 69; /// Disc quota exceeded
pub const ENOTEMPTY = 66;
/// Too many processes
pub const EPROCLIM = 67;
/// Too many users
pub const EUSERS = 68;
/// Disc quota exceeded
// Network File System
pub const ESTALE = 70; /// Stale NFS file handle
pub const EREMOTE = 71; /// Too many levels of remote in path
pub const EBADRPC = 72; /// RPC struct is bad
pub const ERPCMISMATCH = 73; /// RPC version wrong
pub const EPROGUNAVAIL = 74; /// RPC prog. not avail
pub const EPROGMISMATCH = 75; /// Program version wrong
pub const EPROCUNAVAIL = 76; /// Bad procedure for program
pub const EDQUOT = 69;
pub const ENOLCK = 77; /// No locks available
pub const ENOSYS = 78; /// Function not implemented
/// Stale NFS file handle
pub const ESTALE = 70;
pub const EFTYPE = 79; /// Inappropriate file type or format
pub const EAUTH = 80; /// Authentication error
pub const ENEEDAUTH = 81; /// Need authenticator
/// Too many levels of remote in path
pub const EREMOTE = 71;
/// RPC struct is bad
pub const EBADRPC = 72;
/// RPC version wrong
pub const ERPCMISMATCH = 73;
/// RPC prog. not avail
pub const EPROGUNAVAIL = 74;
/// Program version wrong
pub const EPROGMISMATCH = 75;
/// Bad procedure for program
pub const EPROCUNAVAIL = 76;
/// No locks available
pub const ENOLCK = 77;
/// Function not implemented
pub const ENOSYS = 78;
/// Inappropriate file type or format
pub const EFTYPE = 79;
/// Authentication error
pub const EAUTH = 80;
/// Need authenticator
// Intelligent device errors
pub const EPWROFF = 82; /// Device power is off
pub const EDEVERR = 83; /// Device error, e.g. paper out
pub const ENEEDAUTH = 81;
pub const EOVERFLOW = 84; /// Value too large to be stored in data type
/// Device power is off
pub const EPWROFF = 82;
/// Device error, e.g. paper out
pub const EDEVERR = 83;
/// Value too large to be stored in data type
// Program loading errors
pub const EBADEXEC = 85; /// Bad executable
pub const EBADARCH = 86; /// Bad CPU type in executable
pub const ESHLIBVERS = 87; /// Shared library version mismatch
pub const EBADMACHO = 88; /// Malformed Macho file
pub const EOVERFLOW = 84;
pub const ECANCELED = 89; /// Operation canceled
/// Bad executable
pub const EBADEXEC = 85;
pub const EIDRM = 90; /// Identifier removed
pub const ENOMSG = 91; /// No message of desired type
pub const EILSEQ = 92; /// Illegal byte sequence
pub const ENOATTR = 93; /// Attribute not found
/// Bad CPU type in executable
pub const EBADARCH = 86;
pub const EBADMSG = 94; /// Bad message
pub const EMULTIHOP = 95; /// Reserved
pub const ENODATA = 96; /// No message available on STREAM
pub const ENOLINK = 97; /// Reserved
pub const ENOSR = 98; /// No STREAM resources
pub const ENOSTR = 99; /// Not a STREAM
pub const EPROTO = 100; /// Protocol error
pub const ETIME = 101; /// STREAM ioctl timeout
/// Shared library version mismatch
pub const ESHLIBVERS = 87;
pub const ENOPOLICY = 103; /// No such policy registered
/// Malformed Macho file
pub const EBADMACHO = 88;
pub const ENOTRECOVERABLE = 104; /// State not recoverable
pub const EOWNERDEAD = 105; /// Previous owner died
/// Operation canceled
pub const ECANCELED = 89;
pub const EQFULL = 106; /// Interface output queue is full
pub const ELAST = 106; /// Must be equal largest errno
/// Identifier removed
pub const EIDRM = 90;
/// No message of desired type
pub const ENOMSG = 91;
/// Illegal byte sequence
pub const EILSEQ = 92;
/// Attribute not found
pub const ENOATTR = 93;
/// Bad message
pub const EBADMSG = 94;
/// Reserved
pub const EMULTIHOP = 95;
/// No message available on STREAM
pub const ENODATA = 96;
/// Reserved
pub const ENOLINK = 97;
/// No STREAM resources
pub const ENOSR = 98;
/// Not a STREAM
pub const ENOSTR = 99;
/// Protocol error
pub const EPROTO = 100;
/// STREAM ioctl timeout
pub const ETIME = 101;
/// No such policy registered
pub const ENOPOLICY = 103;
/// State not recoverable
pub const ENOTRECOVERABLE = 104;
/// Previous owner died
pub const EOWNERDEAD = 105;
/// Interface output queue is full
pub const EQFULL = 106;
/// Must be equal largest errno
pub const ELAST = 106;

View File

@ -1,26 +1,26 @@
/// Epoch reference times in terms of their difference from
/// posix epoch in seconds.
pub const posix = 0; //Jan 01, 1970 AD
pub const dos = 315532800; //Jan 01, 1980 AD
pub const ios = 978307200; //Jan 01, 2001 AD
pub const openvms = -3506716800; //Nov 17, 1858 AD
pub const zos = -2208988800; //Jan 01, 1900 AD
pub const windows = -11644473600; //Jan 01, 1601 AD
pub const amiga = 252460800; //Jan 01, 1978 AD
pub const pickos = -63244800; //Dec 31, 1967 AD
pub const gps = 315964800; //Jan 06, 1980 AD
pub const clr = -62135769600; //Jan 01, 0001 AD
pub const posix = 0; //Jan 01, 1970 AD
pub const dos = 315532800; //Jan 01, 1980 AD
pub const ios = 978307200; //Jan 01, 2001 AD
pub const openvms = -3506716800; //Nov 17, 1858 AD
pub const zos = -2208988800; //Jan 01, 1900 AD
pub const windows = -11644473600; //Jan 01, 1601 AD
pub const amiga = 252460800; //Jan 01, 1978 AD
pub const pickos = -63244800; //Dec 31, 1967 AD
pub const gps = 315964800; //Jan 06, 1980 AD
pub const clr = -62135769600; //Jan 01, 0001 AD
pub const unix = posix;
pub const android = posix;
pub const os2 = dos;
pub const bios = dos;
pub const vfat = dos;
pub const ntfs = windows;
pub const ntp = zos;
pub const jbase = pickos;
pub const aros = amiga;
pub const morphos = amiga;
pub const brew = gps;
pub const atsc = gps;
pub const go = clr;
pub const unix = posix;
pub const android = posix;
pub const os2 = dos;
pub const bios = dos;
pub const vfat = dos;
pub const ntfs = windows;
pub const ntp = zos;
pub const jbase = pickos;
pub const aros = amiga;
pub const morphos = amiga;
pub const brew = gps;
pub const atsc = gps;
pub const go = clr;

View File

@ -21,12 +21,18 @@ pub const File = struct {
/// Call close to clean up.
pub fn openRead(allocator: &mem.Allocator, path: []const u8) OpenError!File {
if (is_posix) {
const flags = posix.O_LARGEFILE|posix.O_RDONLY;
const flags = posix.O_LARGEFILE | posix.O_RDONLY;
const fd = try os.posixOpen(allocator, path, flags, 0);
return openHandle(fd);
} else if (is_windows) {
const handle = try os.windowsOpen(allocator, path, windows.GENERIC_READ, windows.FILE_SHARE_READ,
windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL);
const handle = try os.windowsOpen(
allocator,
path,
windows.GENERIC_READ,
windows.FILE_SHARE_READ,
windows.OPEN_EXISTING,
windows.FILE_ATTRIBUTE_NORMAL,
);
return openHandle(handle);
} else {
@compileError("TODO implement openRead for this OS");
@ -36,7 +42,6 @@ pub const File = struct {
/// Calls `openWriteMode` with os.default_file_mode for the mode.
pub fn openWrite(allocator: &mem.Allocator, path: []const u8) OpenError!File {
return openWriteMode(allocator, path, os.default_file_mode);
}
/// If the path does not exist it will be created.
@ -45,18 +50,22 @@ pub const File = struct {
/// Call close to clean up.
pub fn openWriteMode(allocator: &mem.Allocator, path: []const u8, file_mode: os.FileMode) OpenError!File {
if (is_posix) {
const flags = posix.O_LARGEFILE|posix.O_WRONLY|posix.O_CREAT|posix.O_CLOEXEC|posix.O_TRUNC;
const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_TRUNC;
const fd = try os.posixOpen(allocator, path, flags, file_mode);
return openHandle(fd);
} else if (is_windows) {
const handle = try os.windowsOpen(allocator, path, windows.GENERIC_WRITE,
windows.FILE_SHARE_WRITE|windows.FILE_SHARE_READ|windows.FILE_SHARE_DELETE,
windows.CREATE_ALWAYS, windows.FILE_ATTRIBUTE_NORMAL);
const handle = try os.windowsOpen(
allocator,
path,
windows.GENERIC_WRITE,
windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE,
windows.CREATE_ALWAYS,
windows.FILE_ATTRIBUTE_NORMAL,
);
return openHandle(handle);
} else {
@compileError("TODO implement openWriteMode for this OS");
}
}
/// If the path does not exist it will be created.
@ -65,24 +74,26 @@ pub const File = struct {
/// Call close to clean up.
pub fn openWriteNoClobber(allocator: &mem.Allocator, path: []const u8, file_mode: os.FileMode) OpenError!File {
if (is_posix) {
const flags = posix.O_LARGEFILE|posix.O_WRONLY|posix.O_CREAT|posix.O_CLOEXEC|posix.O_EXCL;
const flags = posix.O_LARGEFILE | posix.O_WRONLY | posix.O_CREAT | posix.O_CLOEXEC | posix.O_EXCL;
const fd = try os.posixOpen(allocator, path, flags, file_mode);
return openHandle(fd);
} else if (is_windows) {
const handle = try os.windowsOpen(allocator, path, windows.GENERIC_WRITE,
windows.FILE_SHARE_WRITE|windows.FILE_SHARE_READ|windows.FILE_SHARE_DELETE,
windows.CREATE_NEW, windows.FILE_ATTRIBUTE_NORMAL);
const handle = try os.windowsOpen(
allocator,
path,
windows.GENERIC_WRITE,
windows.FILE_SHARE_WRITE | windows.FILE_SHARE_READ | windows.FILE_SHARE_DELETE,
windows.CREATE_NEW,
windows.FILE_ATTRIBUTE_NORMAL,
);
return openHandle(handle);
} else {
@compileError("TODO implement openWriteMode for this OS");
}
}
pub fn openHandle(handle: os.FileHandle) File {
return File {
.handle = handle,
};
return File{ .handle = handle };
}
pub fn access(allocator: &mem.Allocator, path: []const u8, file_mode: os.FileMode) !bool {
@ -217,7 +228,7 @@ pub const File = struct {
return result;
},
Os.windows => {
var pos : windows.LARGE_INTEGER = undefined;
var pos: windows.LARGE_INTEGER = undefined;
if (windows.SetFilePointerEx(self.handle, 0, &pos, windows.FILE_CURRENT) == 0) {
const err = windows.GetLastError();
return switch (err) {
@ -268,7 +279,7 @@ pub const File = struct {
}
}
pub const ModeError = error {
pub const ModeError = error{
BadFd,
SystemResources,
Unexpected,
@ -296,7 +307,7 @@ pub const File = struct {
}
}
pub const ReadError = error {};
pub const ReadError = error{};
pub fn read(self: &File, buffer: []u8) !usize {
if (is_posix) {
@ -306,12 +317,12 @@ pub const File = struct {
const read_err = posix.getErrno(amt_read);
if (read_err > 0) {
switch (read_err) {
posix.EINTR => continue,
posix.EINTR => continue,
posix.EINVAL => unreachable,
posix.EFAULT => unreachable,
posix.EBADF => return error.BadFd,
posix.EIO => return error.Io,
else => return os.unexpectedErrorPosix(read_err),
posix.EBADF => return error.BadFd,
posix.EIO => return error.Io,
else => return os.unexpectedErrorPosix(read_err),
}
}
if (amt_read == 0) return index;

View File

@ -74,7 +74,7 @@ pub fn posixGetUserInfo(name: []const u8) !UserInfo {
'\n' => return error.CorruptPasswordFile,
else => {
const digit = switch (byte) {
'0' ... '9' => byte - '0',
'0'...'9' => byte - '0',
else => return error.CorruptPasswordFile,
};
if (@mulWithOverflow(u32, uid, 10, &uid)) return error.CorruptPasswordFile;
@ -83,14 +83,14 @@ pub fn posixGetUserInfo(name: []const u8) !UserInfo {
},
State.ReadGroupId => switch (byte) {
'\n', ':' => {
return UserInfo {
return UserInfo{
.uid = uid,
.gid = gid,
};
},
else => {
const digit = switch (byte) {
'0' ... '9' => byte - '0',
'0'...'9' => byte - '0',
else => return error.CorruptPasswordFile,
};
if (@mulWithOverflow(u32, gid, 10, &gid)) return error.CorruptPasswordFile;

View File

@ -3,8 +3,7 @@ const builtin = @import("builtin");
const Os = builtin.Os;
const is_windows = builtin.os == Os.windows;
const is_posix = switch (builtin.os) {
builtin.Os.linux,
builtin.Os.macosx => true,
builtin.Os.linux, builtin.Os.macosx => true,
else => false,
};
const os = this;
@ -27,8 +26,7 @@ pub const linux = @import("linux/index.zig");
pub const zen = @import("zen.zig");
pub const posix = switch (builtin.os) {
Os.linux => linux,
Os.macosx,
Os.ios => darwin,
Os.macosx, Os.ios => darwin,
Os.zen => zen,
else => @compileError("Unsupported OS"),
};
@ -112,8 +110,7 @@ pub fn getRandomBytes(buf: []u8) !void {
}
return;
},
Os.macosx,
Os.ios => {
Os.macosx, Os.ios => {
const fd = try posixOpenC(c"/dev/urandom", posix.O_RDONLY | posix.O_CLOEXEC, 0);
defer close(fd);
@ -175,9 +172,7 @@ pub fn abort() noreturn {
c.abort();
}
switch (builtin.os) {
Os.linux,
Os.macosx,
Os.ios => {
Os.linux, Os.macosx, Os.ios => {
_ = posix.raise(posix.SIGABRT);
_ = posix.raise(posix.SIGKILL);
while (true) {}
@ -199,9 +194,7 @@ pub fn exit(status: u8) noreturn {
c.exit(status);
}
switch (builtin.os) {
Os.linux,
Os.macosx,
Os.ios => {
Os.linux, Os.macosx, Os.ios => {
posix.exit(status);
},
Os.windows => {
@ -250,14 +243,12 @@ pub fn posixRead(fd: i32, buf: []u8) !void {
if (err > 0) {
return switch (err) {
posix.EINTR => continue,
posix.EINVAL,
posix.EFAULT => unreachable,
posix.EINVAL, posix.EFAULT => unreachable,
posix.EAGAIN => error.WouldBlock,
posix.EBADF => error.FileClosed,
posix.EIO => error.InputOutput,
posix.EISDIR => error.IsDir,
posix.ENOBUFS,
posix.ENOMEM => error.SystemResources,
posix.ENOBUFS, posix.ENOMEM => error.SystemResources,
else => unexpectedErrorPosix(err),
};
}
@ -292,8 +283,7 @@ pub fn posixWrite(fd: i32, bytes: []const u8) !void {
if (write_err > 0) {
return switch (write_err) {
posix.EINTR => continue,
posix.EINVAL,
posix.EFAULT => unreachable,
posix.EINVAL, posix.EFAULT => unreachable,
posix.EAGAIN => PosixWriteError.WouldBlock,
posix.EBADF => PosixWriteError.FileClosed,
posix.EDESTADDRREQ => PosixWriteError.DestinationAddressRequired,
@ -349,8 +339,7 @@ pub fn posixOpenC(file_path: &const u8, flags: u32, perm: usize) !i32 {
posix.EFAULT => unreachable,
posix.EINVAL => unreachable,
posix.EACCES => return PosixOpenError.AccessDenied,
posix.EFBIG,
posix.EOVERFLOW => return PosixOpenError.FileTooBig,
posix.EFBIG, posix.EOVERFLOW => return PosixOpenError.FileTooBig,
posix.EISDIR => return PosixOpenError.IsDir,
posix.ELOOP => return PosixOpenError.SymLinkLoop,
posix.EMFILE => return PosixOpenError.ProcessFdQuotaExceeded,
@ -375,8 +364,7 @@ pub fn posixDup2(old_fd: i32, new_fd: i32) !void {
const err = posix.getErrno(posix.dup2(old_fd, new_fd));
if (err > 0) {
return switch (err) {
posix.EBUSY,
posix.EINTR => continue,
posix.EBUSY, posix.EINTR => continue,
posix.EMFILE => error.ProcessFdQuotaExceeded,
posix.EINVAL => unreachable,
else => unexpectedErrorPosix(err),
@ -493,17 +481,10 @@ fn posixExecveErrnoToErr(err: usize) PosixExecveError {
assert(err > 0);
return switch (err) {
posix.EFAULT => unreachable,
posix.E2BIG,
posix.EMFILE,
posix.ENAMETOOLONG,
posix.ENFILE,
posix.ENOMEM => error.SystemResources,
posix.EACCES,
posix.EPERM => error.AccessDenied,
posix.EINVAL,
posix.ENOEXEC => error.InvalidExe,
posix.EIO,
posix.ELOOP => error.FileSystem,
posix.E2BIG, posix.EMFILE, posix.ENAMETOOLONG, posix.ENFILE, posix.ENOMEM => error.SystemResources,
posix.EACCES, posix.EPERM => error.AccessDenied,
posix.EINVAL, posix.ENOEXEC => error.InvalidExe,
posix.EIO, posix.ELOOP => error.FileSystem,
posix.EISDIR => error.IsDir,
posix.ENOENT => error.FileNotFound,
posix.ENOTDIR => error.NotDir,
@ -717,10 +698,8 @@ pub fn symLinkPosix(allocator: &Allocator, existing_path: []const u8, new_path:
const err = posix.getErrno(posix.symlink(existing_buf.ptr, new_buf.ptr));
if (err > 0) {
return switch (err) {
posix.EFAULT,
posix.EINVAL => unreachable,
posix.EACCES,
posix.EPERM => error.AccessDenied,
posix.EFAULT, posix.EINVAL => unreachable,
posix.EACCES, posix.EPERM => error.AccessDenied,
posix.EDQUOT => error.DiskQuota,
posix.EEXIST => error.PathAlreadyExists,
posix.EIO => error.FileSystem,
@ -787,8 +766,7 @@ pub fn deleteFileWindows(allocator: &Allocator, file_path: []const u8) !void {
return switch (err) {
windows.ERROR.FILE_NOT_FOUND => error.FileNotFound,
windows.ERROR.ACCESS_DENIED => error.AccessDenied,
windows.ERROR.FILENAME_EXCED_RANGE,
windows.ERROR.INVALID_PARAMETER => error.NameTooLong,
windows.ERROR.FILENAME_EXCED_RANGE, windows.ERROR.INVALID_PARAMETER => error.NameTooLong,
else => unexpectedErrorWindows(err),
};
}
@ -804,11 +782,9 @@ pub fn deleteFilePosix(allocator: &Allocator, file_path: []const u8) !void {
const err = posix.getErrno(posix.unlink(buf.ptr));
if (err > 0) {
return switch (err) {
posix.EACCES,
posix.EPERM => error.AccessDenied,
posix.EACCES, posix.EPERM => error.AccessDenied,
posix.EBUSY => error.FileBusy,
posix.EFAULT,
posix.EINVAL => unreachable,
posix.EFAULT, posix.EINVAL => unreachable,
posix.EIO => error.FileSystem,
posix.EISDIR => error.IsDir,
posix.ELOOP => error.SymLinkLoop,
@ -948,12 +924,10 @@ pub fn rename(allocator: &Allocator, old_path: []const u8, new_path: []const u8)
const err = posix.getErrno(posix.rename(old_buf.ptr, new_buf.ptr));
if (err > 0) {
return switch (err) {
posix.EACCES,
posix.EPERM => error.AccessDenied,
posix.EACCES, posix.EPERM => error.AccessDenied,
posix.EBUSY => error.FileBusy,
posix.EDQUOT => error.DiskQuota,
posix.EFAULT,
posix.EINVAL => unreachable,
posix.EFAULT, posix.EINVAL => unreachable,
posix.EISDIR => error.IsDir,
posix.ELOOP => error.SymLinkLoop,
posix.EMLINK => error.LinkQuotaExceeded,
@ -962,8 +936,7 @@ pub fn rename(allocator: &Allocator, old_path: []const u8, new_path: []const u8)
posix.ENOTDIR => error.NotDir,
posix.ENOMEM => error.SystemResources,
posix.ENOSPC => error.NoSpaceLeft,
posix.EEXIST,
posix.ENOTEMPTY => error.PathAlreadyExists,
posix.EEXIST, posix.ENOTEMPTY => error.PathAlreadyExists,
posix.EROFS => error.ReadOnlyFileSystem,
posix.EXDEV => error.RenameAcrossMountPoints,
else => unexpectedErrorPosix(err),
@ -1001,8 +974,7 @@ pub fn makeDirPosix(allocator: &Allocator, dir_path: []const u8) !void {
const err = posix.getErrno(posix.mkdir(path_buf.ptr, 0o755));
if (err > 0) {
return switch (err) {
posix.EACCES,
posix.EPERM => error.AccessDenied,
posix.EACCES, posix.EPERM => error.AccessDenied,
posix.EDQUOT => error.DiskQuota,
posix.EEXIST => error.PathAlreadyExists,
posix.EFAULT => unreachable,
@ -1065,18 +1037,15 @@ pub fn deleteDir(allocator: &Allocator, dir_path: []const u8) !void {
const err = posix.getErrno(posix.rmdir(path_buf.ptr));
if (err > 0) {
return switch (err) {
posix.EACCES,
posix.EPERM => error.AccessDenied,
posix.EACCES, posix.EPERM => error.AccessDenied,
posix.EBUSY => error.FileBusy,
posix.EFAULT,
posix.EINVAL => unreachable,
posix.EFAULT, posix.EINVAL => unreachable,
posix.ELOOP => error.SymLinkLoop,
posix.ENAMETOOLONG => error.NameTooLong,
posix.ENOENT => error.FileNotFound,
posix.ENOMEM => error.SystemResources,
posix.ENOTDIR => error.NotDir,
posix.EEXIST,
posix.ENOTEMPTY => error.DirNotEmpty,
posix.EEXIST, posix.ENOTEMPTY => error.DirNotEmpty,
posix.EROFS => error.ReadOnlyFileSystem,
else => unexpectedErrorPosix(err),
};
@ -1128,7 +1097,8 @@ pub fn deleteTree(allocator: &Allocator, full_path: []const u8) DeleteTreeError!
error.NotDir,
error.FileSystem,
error.FileBusy,
error.Unexpected => return err,
error.Unexpected,
=> return err,
}
{
var dir = Dir.open(allocator, full_path) catch |err| switch (err) {
@ -1152,7 +1122,8 @@ pub fn deleteTree(allocator: &Allocator, full_path: []const u8) DeleteTreeError!
error.SystemResources,
error.NoSpaceLeft,
error.PathAlreadyExists,
error.Unexpected => return err,
error.Unexpected,
=> return err,
};
defer dir.close();
@ -1182,8 +1153,7 @@ pub const Dir = struct {
end_index: usize,
const darwin_seek_t = switch (builtin.os) {
Os.macosx,
Os.ios => i64,
Os.macosx, Os.ios => i64,
else => void,
};
@ -1208,13 +1178,16 @@ pub const Dir = struct {
const fd = switch (builtin.os) {
Os.windows => @compileError("TODO support Dir.open for windows"),
Os.linux => try posixOpen(allocator, dir_path, posix.O_RDONLY | posix.O_DIRECTORY | posix.O_CLOEXEC, 0),
Os.macosx,
Os.ios => try posixOpen(allocator, dir_path, posix.O_RDONLY | posix.O_NONBLOCK | posix.O_DIRECTORY | posix.O_CLOEXEC, 0),
Os.macosx, Os.ios => try posixOpen(
allocator,
dir_path,
posix.O_RDONLY | posix.O_NONBLOCK | posix.O_DIRECTORY | posix.O_CLOEXEC,
0,
),
else => @compileError("Dir.open is not supported for this platform"),
};
const darwin_seek_init = switch (builtin.os) {
Os.macosx,
Os.ios => 0,
Os.macosx, Os.ios => 0,
else => {},
};
return Dir{
@ -1237,8 +1210,7 @@ pub const Dir = struct {
pub fn next(self: &Dir) !?Entry {
switch (builtin.os) {
Os.linux => return self.nextLinux(),
Os.macosx,
Os.ios => return self.nextDarwin(),
Os.macosx, Os.ios => return self.nextDarwin(),
Os.windows => return self.nextWindows(),
else => @compileError("Dir.next not supported on " ++ @tagName(builtin.os)),
}
@ -1256,9 +1228,7 @@ pub const Dir = struct {
const err = posix.getErrno(result);
if (err > 0) {
switch (err) {
posix.EBADF,
posix.EFAULT,
posix.ENOTDIR => unreachable,
posix.EBADF, posix.EFAULT, posix.ENOTDIR => unreachable,
posix.EINVAL => {
self.buf = try self.allocator.realloc(u8, self.buf, self.buf.len * 2);
continue;
@ -1317,9 +1287,7 @@ pub const Dir = struct {
const err = posix.getErrno(result);
if (err > 0) {
switch (err) {
posix.EBADF,
posix.EFAULT,
posix.ENOTDIR => unreachable,
posix.EBADF, posix.EFAULT, posix.ENOTDIR => unreachable,
posix.EINVAL => {
self.buf = try self.allocator.realloc(u8, self.buf, self.buf.len * 2);
continue;
@ -1402,8 +1370,7 @@ pub fn readLink(allocator: &Allocator, pathname: []const u8) ![]u8 {
if (err > 0) {
return switch (err) {
posix.EACCES => error.AccessDenied,
posix.EFAULT,
posix.EINVAL => unreachable,
posix.EFAULT, posix.EINVAL => unreachable,
posix.EIO => error.FileSystem,
posix.ELOOP => error.SymLinkLoop,
posix.ENAMETOOLONG => error.NameTooLong,
@ -1545,8 +1512,7 @@ pub const ArgIteratorWindows = struct {
const byte = self.cmd_line[self.index];
switch (byte) {
0 => return null,
' ',
'\t' => continue,
' ', '\t' => continue,
else => break,
}
}
@ -1560,8 +1526,7 @@ pub const ArgIteratorWindows = struct {
const byte = self.cmd_line[self.index];
switch (byte) {
0 => return false,
' ',
'\t' => continue,
' ', '\t' => continue,
else => break,
}
}
@ -1580,8 +1545,7 @@ pub const ArgIteratorWindows = struct {
'\\' => {
backslash_count += 1;
},
' ',
'\t' => {
' ', '\t' => {
if (self.seen_quote_count % 2 == 0 or self.seen_quote_count == self.quote_count) {
return true;
}
@ -1621,8 +1585,7 @@ pub const ArgIteratorWindows = struct {
'\\' => {
backslash_count += 1;
},
' ',
'\t' => {
' ', '\t' => {
try self.emitBackslashes(&buf, backslash_count);
backslash_count = 0;
if (self.seen_quote_count % 2 == 1 and self.seen_quote_count != self.quote_count) {
@ -1840,8 +1803,7 @@ pub fn openSelfExe() !os.File {
var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
return os.File.openRead(&fixed_allocator.allocator, proc_file_path);
},
Os.macosx,
Os.ios => {
Os.macosx, Os.ios => {
var fixed_buffer_mem: [darwin.PATH_MAX * 2]u8 = undefined;
var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
const self_exe_path = try selfExePath(&fixed_allocator.allocator);
@ -1853,9 +1815,7 @@ pub fn openSelfExe() !os.File {
test "openSelfExe" {
switch (builtin.os) {
Os.linux,
Os.macosx,
Os.ios => (try openSelfExe()).close(),
Os.linux, Os.macosx, Os.ios => (try openSelfExe()).close(),
else => return, // Unsupported OS.
}
}
@ -1893,8 +1853,7 @@ pub fn selfExePath(allocator: &mem.Allocator) ![]u8 {
try out_path.resize(new_len);
}
},
Os.macosx,
Os.ios => {
Os.macosx, Os.ios => {
var u32_len: u32 = 0;
const ret1 = c._NSGetExecutablePath(undefined, &u32_len);
assert(ret1 != 0);
@ -1922,9 +1881,7 @@ pub fn selfExeDirPath(allocator: &mem.Allocator) ![]u8 {
const dir = path.dirname(full_exe_path);
return allocator.shrink(u8, full_exe_path, dir.len);
},
Os.windows,
Os.macosx,
Os.ios => {
Os.windows, Os.macosx, Os.ios => {
const self_exe_path = try selfExePath(allocator);
errdefer allocator.free(self_exe_path);
const dirname = os.path.dirname(self_exe_path);
@ -1981,8 +1938,7 @@ pub fn posixSocket(domain: u32, socket_type: u32, protocol: u32) !i32 {
posix.EINVAL => return PosixSocketError.ProtocolFamilyNotAvailable,
posix.EMFILE => return PosixSocketError.ProcessFdQuotaExceeded,
posix.ENFILE => return PosixSocketError.SystemFdQuotaExceeded,
posix.ENOBUFS,
posix.ENOMEM => return PosixSocketError.SystemResources,
posix.ENOBUFS, posix.ENOMEM => return PosixSocketError.SystemResources,
posix.EPROTONOSUPPORT => return PosixSocketError.ProtocolNotSupported,
else => return unexpectedErrorPosix(err),
}
@ -1990,7 +1946,7 @@ pub fn posixSocket(domain: u32, socket_type: u32, protocol: u32) !i32 {
pub const PosixBindError = error{
/// The address is protected, and the user is not the superuser.
/// For UNIX domain sockets: Search permission is denied on a component
/// For UNIX domain sockets: Search permission is denied on a component
/// of the path prefix.
AccessDenied,
@ -2151,8 +2107,7 @@ pub fn posixAccept(fd: i32, addr: &posix.sockaddr, flags: u32) PosixAcceptError!
posix.EINVAL => return PosixAcceptError.InvalidSyscall,
posix.EMFILE => return PosixAcceptError.ProcessFdQuotaExceeded,
posix.ENFILE => return PosixAcceptError.SystemFdQuotaExceeded,
posix.ENOBUFS,
posix.ENOMEM => return PosixAcceptError.SystemResources,
posix.ENOBUFS, posix.ENOMEM => return PosixAcceptError.SystemResources,
posix.ENOTSOCK => return PosixAcceptError.FileDescriptorNotASocket,
posix.EOPNOTSUPP => return PosixAcceptError.OperationNotSupported,
posix.EPROTO => return PosixAcceptError.ProtocolFailure,
@ -2363,8 +2318,7 @@ pub fn posixConnectAsync(sockfd: i32, sockaddr: &const posix.sockaddr) PosixConn
const rc = posix.connect(sockfd, sockaddr, @sizeOf(posix.sockaddr));
const err = posix.getErrno(rc);
switch (err) {
0,
posix.EINPROGRESS => return,
0, posix.EINPROGRESS => return,
else => return unexpectedErrorPosix(err),
posix.EACCES => return PosixConnectError.PermissionDenied,
@ -2416,7 +2370,7 @@ pub fn posixGetSockOptConnectError(sockfd: i32) PosixConnectError!void {
},
else => return unexpectedErrorPosix(err),
posix.EBADF => unreachable, // The argument sockfd is not a valid file descriptor.
posix.EFAULT => unreachable, // The address pointed to by optval or optlen is not in a valid part of the process address space.
posix.EFAULT => unreachable, // The address pointed to by optval or optlen is not in a valid part of the process address space.
posix.EINVAL => unreachable,
posix.ENOPROTOOPT => unreachable, // The option is unknown at the level indicated.
posix.ENOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket.
@ -2427,11 +2381,13 @@ pub const Thread = struct {
data: Data,
pub const use_pthreads = is_posix and builtin.link_libc;
const Data = if (use_pthreads) struct {
handle: c.pthread_t,
stack_addr: usize,
stack_len: usize,
} else switch (builtin.os) {
const Data = if (use_pthreads)
struct {
handle: c.pthread_t,
stack_addr: usize,
stack_len: usize,
}
else switch (builtin.os) {
builtin.Os.linux => struct {
pid: i32,
stack_addr: usize,

View File

@ -1,146 +1,427 @@
pub const EPERM = 1; /// Operation not permitted
pub const ENOENT = 2; /// No such file or directory
pub const ESRCH = 3; /// No such process
pub const EINTR = 4; /// Interrupted system call
pub const EIO = 5; /// I/O error
pub const ENXIO = 6; /// No such device or address
pub const E2BIG = 7; /// Arg list too long
pub const ENOEXEC = 8; /// Exec format error
pub const EBADF = 9; /// Bad file number
pub const ECHILD = 10; /// No child processes
pub const EAGAIN = 11; /// Try again
pub const ENOMEM = 12; /// Out of memory
pub const EACCES = 13; /// Permission denied
pub const EFAULT = 14; /// Bad address
pub const ENOTBLK = 15; /// Block device required
pub const EBUSY = 16; /// Device or resource busy
pub const EEXIST = 17; /// File exists
pub const EXDEV = 18; /// Cross-device link
pub const ENODEV = 19; /// No such device
pub const ENOTDIR = 20; /// Not a directory
pub const EISDIR = 21; /// Is a directory
pub const EINVAL = 22; /// Invalid argument
pub const ENFILE = 23; /// File table overflow
pub const EMFILE = 24; /// Too many open files
pub const ENOTTY = 25; /// Not a typewriter
pub const ETXTBSY = 26; /// Text file busy
pub const EFBIG = 27; /// File too large
pub const ENOSPC = 28; /// No space left on device
pub const ESPIPE = 29; /// Illegal seek
pub const EROFS = 30; /// Read-only file system
pub const EMLINK = 31; /// Too many links
pub const EPIPE = 32; /// Broken pipe
pub const EDOM = 33; /// Math argument out of domain of func
pub const ERANGE = 34; /// Math result not representable
pub const EDEADLK = 35; /// Resource deadlock would occur
pub const ENAMETOOLONG = 36; /// File name too long
pub const ENOLCK = 37; /// No record locks available
pub const ENOSYS = 38; /// Function not implemented
pub const ENOTEMPTY = 39; /// Directory not empty
pub const ELOOP = 40; /// Too many symbolic links encountered
pub const EWOULDBLOCK = EAGAIN; /// Operation would block
pub const ENOMSG = 42; /// No message of desired type
pub const EIDRM = 43; /// Identifier removed
pub const ECHRNG = 44; /// Channel number out of range
pub const EL2NSYNC = 45; /// Level 2 not synchronized
pub const EL3HLT = 46; /// Level 3 halted
pub const EL3RST = 47; /// Level 3 reset
pub const ELNRNG = 48; /// Link number out of range
pub const EUNATCH = 49; /// Protocol driver not attached
pub const ENOCSI = 50; /// No CSI structure available
pub const EL2HLT = 51; /// Level 2 halted
pub const EBADE = 52; /// Invalid exchange
pub const EBADR = 53; /// Invalid request descriptor
pub const EXFULL = 54; /// Exchange full
pub const ENOANO = 55; /// No anode
pub const EBADRQC = 56; /// Invalid request code
pub const EBADSLT = 57; /// Invalid slot
/// Operation not permitted
pub const EPERM = 1;
pub const EBFONT = 59; /// Bad font file format
pub const ENOSTR = 60; /// Device not a stream
pub const ENODATA = 61; /// No data available
pub const ETIME = 62; /// Timer expired
pub const ENOSR = 63; /// Out of streams resources
pub const ENONET = 64; /// Machine is not on the network
pub const ENOPKG = 65; /// Package not installed
pub const EREMOTE = 66; /// Object is remote
pub const ENOLINK = 67; /// Link has been severed
pub const EADV = 68; /// Advertise error
pub const ESRMNT = 69; /// Srmount error
pub const ECOMM = 70; /// Communication error on send
pub const EPROTO = 71; /// Protocol error
pub const EMULTIHOP = 72; /// Multihop attempted
pub const EDOTDOT = 73; /// RFS specific error
pub const EBADMSG = 74; /// Not a data message
pub const EOVERFLOW = 75; /// Value too large for defined data type
pub const ENOTUNIQ = 76; /// Name not unique on network
pub const EBADFD = 77; /// File descriptor in bad state
pub const EREMCHG = 78; /// Remote address changed
pub const ELIBACC = 79; /// Can not access a needed shared library
pub const ELIBBAD = 80; /// Accessing a corrupted shared library
pub const ELIBSCN = 81; /// .lib section in a.out corrupted
pub const ELIBMAX = 82; /// Attempting to link in too many shared libraries
pub const ELIBEXEC = 83; /// Cannot exec a shared library directly
pub const EILSEQ = 84; /// Illegal byte sequence
pub const ERESTART = 85; /// Interrupted system call should be restarted
pub const ESTRPIPE = 86; /// Streams pipe error
pub const EUSERS = 87; /// Too many users
pub const ENOTSOCK = 88; /// Socket operation on non-socket
pub const EDESTADDRREQ = 89; /// Destination address required
pub const EMSGSIZE = 90; /// Message too long
pub const EPROTOTYPE = 91; /// Protocol wrong type for socket
pub const ENOPROTOOPT = 92; /// Protocol not available
pub const EPROTONOSUPPORT = 93; /// Protocol not supported
pub const ESOCKTNOSUPPORT = 94; /// Socket type not supported
pub const EOPNOTSUPP = 95; /// Operation not supported on transport endpoint
pub const EPFNOSUPPORT = 96; /// Protocol family not supported
pub const EAFNOSUPPORT = 97; /// Address family not supported by protocol
pub const EADDRINUSE = 98; /// Address already in use
pub const EADDRNOTAVAIL = 99; /// Cannot assign requested address
pub const ENETDOWN = 100; /// Network is down
pub const ENETUNREACH = 101; /// Network is unreachable
pub const ENETRESET = 102; /// Network dropped connection because of reset
pub const ECONNABORTED = 103; /// Software caused connection abort
pub const ECONNRESET = 104; /// Connection reset by peer
pub const ENOBUFS = 105; /// No buffer space available
pub const EISCONN = 106; /// Transport endpoint is already connected
pub const ENOTCONN = 107; /// Transport endpoint is not connected
pub const ESHUTDOWN = 108; /// Cannot send after transport endpoint shutdown
pub const ETOOMANYREFS = 109; /// Too many references: cannot splice
pub const ETIMEDOUT = 110; /// Connection timed out
pub const ECONNREFUSED = 111; /// Connection refused
pub const EHOSTDOWN = 112; /// Host is down
pub const EHOSTUNREACH = 113; /// No route to host
pub const EALREADY = 114; /// Operation already in progress
pub const EINPROGRESS = 115; /// Operation now in progress
pub const ESTALE = 116; /// Stale NFS file handle
pub const EUCLEAN = 117; /// Structure needs cleaning
pub const ENOTNAM = 118; /// Not a XENIX named type file
pub const ENAVAIL = 119; /// No XENIX semaphores available
pub const EISNAM = 120; /// Is a named type file
pub const EREMOTEIO = 121; /// Remote I/O error
pub const EDQUOT = 122; /// Quota exceeded
/// No such file or directory
pub const ENOENT = 2;
pub const ENOMEDIUM = 123; /// No medium found
pub const EMEDIUMTYPE = 124; /// Wrong medium type
/// No such process
pub const ESRCH = 3;
/// Interrupted system call
pub const EINTR = 4;
/// I/O error
pub const EIO = 5;
/// No such device or address
pub const ENXIO = 6;
/// Arg list too long
pub const E2BIG = 7;
/// Exec format error
pub const ENOEXEC = 8;
/// Bad file number
pub const EBADF = 9;
/// No child processes
pub const ECHILD = 10;
/// Try again
pub const EAGAIN = 11;
/// Out of memory
pub const ENOMEM = 12;
/// Permission denied
pub const EACCES = 13;
/// Bad address
pub const EFAULT = 14;
/// Block device required
pub const ENOTBLK = 15;
/// Device or resource busy
pub const EBUSY = 16;
/// File exists
pub const EEXIST = 17;
/// Cross-device link
pub const EXDEV = 18;
/// No such device
pub const ENODEV = 19;
/// Not a directory
pub const ENOTDIR = 20;
/// Is a directory
pub const EISDIR = 21;
/// Invalid argument
pub const EINVAL = 22;
/// File table overflow
pub const ENFILE = 23;
/// Too many open files
pub const EMFILE = 24;
/// Not a typewriter
pub const ENOTTY = 25;
/// Text file busy
pub const ETXTBSY = 26;
/// File too large
pub const EFBIG = 27;
/// No space left on device
pub const ENOSPC = 28;
/// Illegal seek
pub const ESPIPE = 29;
/// Read-only file system
pub const EROFS = 30;
/// Too many links
pub const EMLINK = 31;
/// Broken pipe
pub const EPIPE = 32;
/// Math argument out of domain of func
pub const EDOM = 33;
/// Math result not representable
pub const ERANGE = 34;
/// Resource deadlock would occur
pub const EDEADLK = 35;
/// File name too long
pub const ENAMETOOLONG = 36;
/// No record locks available
pub const ENOLCK = 37;
/// Function not implemented
pub const ENOSYS = 38;
/// Directory not empty
pub const ENOTEMPTY = 39;
/// Too many symbolic links encountered
pub const ELOOP = 40;
/// Operation would block
pub const EWOULDBLOCK = EAGAIN;
/// No message of desired type
pub const ENOMSG = 42;
/// Identifier removed
pub const EIDRM = 43;
/// Channel number out of range
pub const ECHRNG = 44;
/// Level 2 not synchronized
pub const EL2NSYNC = 45;
/// Level 3 halted
pub const EL3HLT = 46;
/// Level 3 reset
pub const EL3RST = 47;
/// Link number out of range
pub const ELNRNG = 48;
/// Protocol driver not attached
pub const EUNATCH = 49;
/// No CSI structure available
pub const ENOCSI = 50;
/// Level 2 halted
pub const EL2HLT = 51;
/// Invalid exchange
pub const EBADE = 52;
/// Invalid request descriptor
pub const EBADR = 53;
/// Exchange full
pub const EXFULL = 54;
/// No anode
pub const ENOANO = 55;
/// Invalid request code
pub const EBADRQC = 56;
/// Invalid slot
pub const EBADSLT = 57;
/// Bad font file format
pub const EBFONT = 59;
/// Device not a stream
pub const ENOSTR = 60;
/// No data available
pub const ENODATA = 61;
/// Timer expired
pub const ETIME = 62;
/// Out of streams resources
pub const ENOSR = 63;
/// Machine is not on the network
pub const ENONET = 64;
/// Package not installed
pub const ENOPKG = 65;
/// Object is remote
pub const EREMOTE = 66;
/// Link has been severed
pub const ENOLINK = 67;
/// Advertise error
pub const EADV = 68;
/// Srmount error
pub const ESRMNT = 69;
/// Communication error on send
pub const ECOMM = 70;
/// Protocol error
pub const EPROTO = 71;
/// Multihop attempted
pub const EMULTIHOP = 72;
/// RFS specific error
pub const EDOTDOT = 73;
/// Not a data message
pub const EBADMSG = 74;
/// Value too large for defined data type
pub const EOVERFLOW = 75;
/// Name not unique on network
pub const ENOTUNIQ = 76;
/// File descriptor in bad state
pub const EBADFD = 77;
/// Remote address changed
pub const EREMCHG = 78;
/// Can not access a needed shared library
pub const ELIBACC = 79;
/// Accessing a corrupted shared library
pub const ELIBBAD = 80;
/// .lib section in a.out corrupted
pub const ELIBSCN = 81;
/// Attempting to link in too many shared libraries
pub const ELIBMAX = 82;
/// Cannot exec a shared library directly
pub const ELIBEXEC = 83;
/// Illegal byte sequence
pub const EILSEQ = 84;
/// Interrupted system call should be restarted
pub const ERESTART = 85;
/// Streams pipe error
pub const ESTRPIPE = 86;
/// Too many users
pub const EUSERS = 87;
/// Socket operation on non-socket
pub const ENOTSOCK = 88;
/// Destination address required
pub const EDESTADDRREQ = 89;
/// Message too long
pub const EMSGSIZE = 90;
/// Protocol wrong type for socket
pub const EPROTOTYPE = 91;
/// Protocol not available
pub const ENOPROTOOPT = 92;
/// Protocol not supported
pub const EPROTONOSUPPORT = 93;
/// Socket type not supported
pub const ESOCKTNOSUPPORT = 94;
/// Operation not supported on transport endpoint
pub const EOPNOTSUPP = 95;
/// Protocol family not supported
pub const EPFNOSUPPORT = 96;
/// Address family not supported by protocol
pub const EAFNOSUPPORT = 97;
/// Address already in use
pub const EADDRINUSE = 98;
/// Cannot assign requested address
pub const EADDRNOTAVAIL = 99;
/// Network is down
pub const ENETDOWN = 100;
/// Network is unreachable
pub const ENETUNREACH = 101;
/// Network dropped connection because of reset
pub const ENETRESET = 102;
/// Software caused connection abort
pub const ECONNABORTED = 103;
/// Connection reset by peer
pub const ECONNRESET = 104;
/// No buffer space available
pub const ENOBUFS = 105;
/// Transport endpoint is already connected
pub const EISCONN = 106;
/// Transport endpoint is not connected
pub const ENOTCONN = 107;
/// Cannot send after transport endpoint shutdown
pub const ESHUTDOWN = 108;
/// Too many references: cannot splice
pub const ETOOMANYREFS = 109;
/// Connection timed out
pub const ETIMEDOUT = 110;
/// Connection refused
pub const ECONNREFUSED = 111;
/// Host is down
pub const EHOSTDOWN = 112;
/// No route to host
pub const EHOSTUNREACH = 113;
/// Operation already in progress
pub const EALREADY = 114;
/// Operation now in progress
pub const EINPROGRESS = 115;
/// Stale NFS file handle
pub const ESTALE = 116;
/// Structure needs cleaning
pub const EUCLEAN = 117;
/// Not a XENIX named type file
pub const ENOTNAM = 118;
/// No XENIX semaphores available
pub const ENAVAIL = 119;
/// Is a named type file
pub const EISNAM = 120;
/// Remote I/O error
pub const EREMOTEIO = 121;
/// Quota exceeded
pub const EDQUOT = 122;
/// No medium found
pub const ENOMEDIUM = 123;
/// Wrong medium type
pub const EMEDIUMTYPE = 124;
// nameserver query return codes
pub const ENSROK = 0; /// DNS server returned answer with no data
pub const ENSRNODATA = 160; /// DNS server returned answer with no data
pub const ENSRFORMERR = 161; /// DNS server claims query was misformatted
pub const ENSRSERVFAIL = 162; /// DNS server returned general failure
pub const ENSRNOTFOUND = 163; /// Domain name not found
pub const ENSRNOTIMP = 164; /// DNS server does not implement requested operation
pub const ENSRREFUSED = 165; /// DNS server refused query
pub const ENSRBADQUERY = 166; /// Misformatted DNS query
pub const ENSRBADNAME = 167; /// Misformatted domain name
pub const ENSRBADFAMILY = 168; /// Unsupported address family
pub const ENSRBADRESP = 169; /// Misformatted DNS reply
pub const ENSRCONNREFUSED = 170; /// Could not contact DNS servers
pub const ENSRTIMEOUT = 171; /// Timeout while contacting DNS servers
pub const ENSROF = 172; /// End of file
pub const ENSRFILE = 173; /// Error reading file
pub const ENSRNOMEM = 174; /// Out of memory
pub const ENSRDESTRUCTION = 175; /// Application terminated lookup
pub const ENSRQUERYDOMAINTOOLONG = 176; /// Domain name is too long
pub const ENSRCNAMELOOP = 177; /// Domain name is too long
/// DNS server returned answer with no data
pub const ENSROK = 0;
/// DNS server returned answer with no data
pub const ENSRNODATA = 160;
/// DNS server claims query was misformatted
pub const ENSRFORMERR = 161;
/// DNS server returned general failure
pub const ENSRSERVFAIL = 162;
/// Domain name not found
pub const ENSRNOTFOUND = 163;
/// DNS server does not implement requested operation
pub const ENSRNOTIMP = 164;
/// DNS server refused query
pub const ENSRREFUSED = 165;
/// Misformatted DNS query
pub const ENSRBADQUERY = 166;
/// Misformatted domain name
pub const ENSRBADNAME = 167;
/// Unsupported address family
pub const ENSRBADFAMILY = 168;
/// Misformatted DNS reply
pub const ENSRBADRESP = 169;
/// Could not contact DNS servers
pub const ENSRCONNREFUSED = 170;
/// Timeout while contacting DNS servers
pub const ENSRTIMEOUT = 171;
/// End of file
pub const ENSROF = 172;
/// Error reading file
pub const ENSRFILE = 173;
/// Out of memory
pub const ENSRNOMEM = 174;
/// Application terminated lookup
pub const ENSRDESTRUCTION = 175;
/// Domain name is too long
pub const ENSRQUERYDOMAINTOOLONG = 176;
/// Domain name is too long
pub const ENSRCNAMELOOP = 177;

View File

@ -823,8 +823,7 @@ pub fn clock_gettime(clk_id: i32, tp: &timespec) usize {
if (@ptrToInt(f) != 0) {
const rc = f(clk_id, tp);
switch (rc) {
0,
@bitCast(usize, isize(-EINVAL)) => return rc,
0, @bitCast(usize, isize(-EINVAL)) => return rc,
else => {},
}
}

View File

@ -11,22 +11,22 @@ test "timer" {
const timer_fd = linux.timerfd_create(linux.CLOCK_MONOTONIC, 0);
assert(linux.getErrno(timer_fd) == 0);
const time_interval = linux.timespec {
const time_interval = linux.timespec{
.tv_sec = 0,
.tv_nsec = 2000000
.tv_nsec = 2000000,
};
const new_time = linux.itimerspec {
const new_time = linux.itimerspec{
.it_interval = time_interval,
.it_value = time_interval
.it_value = time_interval,
};
err = linux.timerfd_settime(i32(timer_fd), 0, &new_time, null);
assert(err == 0);
var event = linux.epoll_event {
var event = linux.epoll_event{
.events = linux.EPOLLIN | linux.EPOLLOUT | linux.EPOLLET,
.data = linux.epoll_data { .ptr = 0 },
.data = linux.epoll_data{ .ptr = 0 },
};
err = linux.epoll_ctl(i32(epoll_fd), linux.EPOLL_CTL_ADD, i32(timer_fd), &event);

View File

@ -16,7 +16,10 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {
var base: usize = @maxValue(usize);
{
var i: usize = 0;
while (i < eh.e_phnum) : ({i += 1; ph_addr += eh.e_phentsize;}) {
while (i < eh.e_phnum) : ({
i += 1;
ph_addr += eh.e_phentsize;
}) {
const this_ph = @intToPtr(&elf.Phdr, ph_addr);
switch (this_ph.p_type) {
elf.PT_LOAD => base = vdso_addr + this_ph.p_offset - this_ph.p_vaddr,
@ -54,15 +57,14 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {
const hashtab = maybe_hashtab ?? return 0;
if (maybe_verdef == null) maybe_versym = null;
const OK_TYPES = (1<<elf.STT_NOTYPE | 1<<elf.STT_OBJECT | 1<<elf.STT_FUNC | 1<<elf.STT_COMMON);
const OK_BINDS = (1<<elf.STB_GLOBAL | 1<<elf.STB_WEAK | 1<<elf.STB_GNU_UNIQUE);
const OK_TYPES = (1 << elf.STT_NOTYPE | 1 << elf.STT_OBJECT | 1 << elf.STT_FUNC | 1 << elf.STT_COMMON);
const OK_BINDS = (1 << elf.STB_GLOBAL | 1 << elf.STB_WEAK | 1 << elf.STB_GNU_UNIQUE);
var i: usize = 0;
while (i < hashtab[1]) : (i += 1) {
if (0==(u32(1)<<u5(syms[i].st_info&0xf) & OK_TYPES)) continue;
if (0==(u32(1)<<u5(syms[i].st_info>>4) & OK_BINDS)) continue;
if (0==syms[i].st_shndx) continue;
if (0 == (u32(1) << u5(syms[i].st_info & 0xf) & OK_TYPES)) continue;
if (0 == (u32(1) << u5(syms[i].st_info >> 4) & OK_BINDS)) continue;
if (0 == syms[i].st_shndx) continue;
if (!mem.eql(u8, name, cstr.toSliceConst(&strings[syms[i].st_name]))) continue;
if (maybe_versym) |versym| {
if (!checkver(??maybe_verdef, versym[i], vername, strings))
@ -78,12 +80,12 @@ fn checkver(def_arg: &elf.Verdef, vsym_arg: i32, vername: []const u8, strings: &
var def = def_arg;
const vsym = @bitCast(u32, vsym_arg) & 0x7fff;
while (true) {
if (0==(def.vd_flags & elf.VER_FLG_BASE) and (def.vd_ndx & 0x7fff) == vsym)
if (0 == (def.vd_flags & elf.VER_FLG_BASE) and (def.vd_ndx & 0x7fff) == vsym)
break;
if (def.vd_next == 0)
return false;
def = @intToPtr(&elf.Verdef, @ptrToInt(def) + def.vd_next);
}
const aux = @intToPtr(&elf.Verdaux, @ptrToInt(def ) + def.vd_aux);
const aux = @intToPtr(&elf.Verdaux, @ptrToInt(def) + def.vd_aux);
return mem.eql(u8, vername, cstr.toSliceConst(&strings[aux.vda_name]));
}

View File

@ -330,26 +330,26 @@ pub const SYS_userfaultfd = 323;
pub const SYS_membarrier = 324;
pub const SYS_mlock2 = 325;
pub const O_CREAT = 0o100;
pub const O_EXCL = 0o200;
pub const O_NOCTTY = 0o400;
pub const O_TRUNC = 0o1000;
pub const O_APPEND = 0o2000;
pub const O_NONBLOCK = 0o4000;
pub const O_DSYNC = 0o10000;
pub const O_SYNC = 0o4010000;
pub const O_RSYNC = 0o4010000;
pub const O_CREAT = 0o100;
pub const O_EXCL = 0o200;
pub const O_NOCTTY = 0o400;
pub const O_TRUNC = 0o1000;
pub const O_APPEND = 0o2000;
pub const O_NONBLOCK = 0o4000;
pub const O_DSYNC = 0o10000;
pub const O_SYNC = 0o4010000;
pub const O_RSYNC = 0o4010000;
pub const O_DIRECTORY = 0o200000;
pub const O_NOFOLLOW = 0o400000;
pub const O_CLOEXEC = 0o2000000;
pub const O_NOFOLLOW = 0o400000;
pub const O_CLOEXEC = 0o2000000;
pub const O_ASYNC = 0o20000;
pub const O_DIRECT = 0o40000;
pub const O_LARGEFILE = 0;
pub const O_NOATIME = 0o1000000;
pub const O_PATH = 0o10000000;
pub const O_ASYNC = 0o20000;
pub const O_DIRECT = 0o40000;
pub const O_LARGEFILE = 0;
pub const O_NOATIME = 0o1000000;
pub const O_PATH = 0o10000000;
pub const O_TMPFILE = 0o20200000;
pub const O_NDELAY = O_NONBLOCK;
pub const O_NDELAY = O_NONBLOCK;
pub const F_DUPFD = 0;
pub const F_GETFD = 1;
@ -371,7 +371,6 @@ pub const F_GETOWN_EX = 16;
pub const F_GETOWNER_UIDS = 17;
pub const VDSO_USEFUL = true;
pub const VDSO_CGT_SYM = "__vdso_clock_gettime";
pub const VDSO_CGT_VER = "LINUX_2.6";
@ -382,72 +381,85 @@ pub fn syscall0(number: usize) usize {
return asm volatile ("syscall"
: [ret] "={rax}" (-> usize)
: [number] "{rax}" (number)
: "rcx", "r11");
: "rcx", "r11"
);
}
pub fn syscall1(number: usize, arg1: usize) usize {
return asm volatile ("syscall"
: [ret] "={rax}" (-> usize)
: [number] "{rax}" (number),
[arg1] "{rdi}" (arg1)
: "rcx", "r11");
[arg1] "{rdi}" (arg1)
: "rcx", "r11"
);
}
pub fn syscall2(number: usize, arg1: usize, arg2: usize) usize {
return asm volatile ("syscall"
: [ret] "={rax}" (-> usize)
: [number] "{rax}" (number),
[arg1] "{rdi}" (arg1),
[arg2] "{rsi}" (arg2)
: "rcx", "r11");
[arg1] "{rdi}" (arg1),
[arg2] "{rsi}" (arg2)
: "rcx", "r11"
);
}
pub fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) usize {
return asm volatile ("syscall"
: [ret] "={rax}" (-> usize)
: [number] "{rax}" (number),
[arg1] "{rdi}" (arg1),
[arg2] "{rsi}" (arg2),
[arg3] "{rdx}" (arg3)
: "rcx", "r11");
[arg1] "{rdi}" (arg1),
[arg2] "{rsi}" (arg2),
[arg3] "{rdx}" (arg3)
: "rcx", "r11"
);
}
pub fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize {
return asm volatile ("syscall"
: [ret] "={rax}" (-> usize)
: [number] "{rax}" (number),
[arg1] "{rdi}" (arg1),
[arg2] "{rsi}" (arg2),
[arg3] "{rdx}" (arg3),
[arg4] "{r10}" (arg4)
: "rcx", "r11");
[arg1] "{rdi}" (arg1),
[arg2] "{rsi}" (arg2),
[arg3] "{rdx}" (arg3),
[arg4] "{r10}" (arg4)
: "rcx", "r11"
);
}
pub fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) usize {
return asm volatile ("syscall"
: [ret] "={rax}" (-> usize)
: [number] "{rax}" (number),
[arg1] "{rdi}" (arg1),
[arg2] "{rsi}" (arg2),
[arg3] "{rdx}" (arg3),
[arg4] "{r10}" (arg4),
[arg5] "{r8}" (arg5)
: "rcx", "r11");
[arg1] "{rdi}" (arg1),
[arg2] "{rsi}" (arg2),
[arg3] "{rdx}" (arg3),
[arg4] "{r10}" (arg4),
[arg5] "{r8}" (arg5)
: "rcx", "r11"
);
}
pub fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize,
arg5: usize, arg6: usize) usize
{
pub fn syscall6(
number: usize,
arg1: usize,
arg2: usize,
arg3: usize,
arg4: usize,
arg5: usize,
arg6: usize,
) usize {
return asm volatile ("syscall"
: [ret] "={rax}" (-> usize)
: [number] "{rax}" (number),
[arg1] "{rdi}" (arg1),
[arg2] "{rsi}" (arg2),
[arg3] "{rdx}" (arg3),
[arg4] "{r10}" (arg4),
[arg5] "{r8}" (arg5),
[arg6] "{r9}" (arg6)
: "rcx", "r11");
[arg1] "{rdi}" (arg1),
[arg2] "{rsi}" (arg2),
[arg3] "{rdx}" (arg3),
[arg4] "{r10}" (arg4),
[arg5] "{r8}" (arg5),
[arg6] "{r9}" (arg6)
: "rcx", "r11"
);
}
/// This matches the libc clone function.
@ -457,10 +469,10 @@ pub nakedcc fn restore_rt() void {
return asm volatile ("syscall"
:
: [number] "{rax}" (usize(SYS_rt_sigreturn))
: "rcx", "r11");
: "rcx", "r11"
);
}
pub const msghdr = extern struct {
msg_name: &u8,
msg_namelen: socklen_t,

View File

@ -55,9 +55,7 @@ test "os.path.join" {
assert(mem.eql(u8, try joinWindows(debug.global_allocator, "c:\\", "a", "b\\", "c"), "c:\\a\\b\\c"));
assert(mem.eql(u8, try joinWindows(debug.global_allocator, "c:\\a\\", "b\\", "c"), "c:\\a\\b\\c"));
assert(mem.eql(u8, try joinWindows(debug.global_allocator,
"c:\\home\\andy\\dev\\zig\\build\\lib\\zig\\std", "io.zig"),
"c:\\home\\andy\\dev\\zig\\build\\lib\\zig\\std\\io.zig"));
assert(mem.eql(u8, try joinWindows(debug.global_allocator, "c:\\home\\andy\\dev\\zig\\build\\lib\\zig\\std", "io.zig"), "c:\\home\\andy\\dev\\zig\\build\\lib\\zig\\std\\io.zig"));
assert(mem.eql(u8, try joinPosix(debug.global_allocator, "/a/b", "c"), "/a/b/c"));
assert(mem.eql(u8, try joinPosix(debug.global_allocator, "/a/b/", "c"), "/a/b/c"));
@ -65,8 +63,7 @@ test "os.path.join" {
assert(mem.eql(u8, try joinPosix(debug.global_allocator, "/", "a", "b/", "c"), "/a/b/c"));
assert(mem.eql(u8, try joinPosix(debug.global_allocator, "/a/", "b/", "c"), "/a/b/c"));
assert(mem.eql(u8, try joinPosix(debug.global_allocator, "/home/andy/dev/zig/build/lib/zig/std", "io.zig"),
"/home/andy/dev/zig/build/lib/zig/std/io.zig"));
assert(mem.eql(u8, try joinPosix(debug.global_allocator, "/home/andy/dev/zig/build/lib/zig/std", "io.zig"), "/home/andy/dev/zig/build/lib/zig/std/io.zig"));
}
pub fn isAbsolute(path: []const u8) bool {
@ -151,22 +148,22 @@ pub const WindowsPath = struct {
pub fn windowsParsePath(path: []const u8) WindowsPath {
if (path.len >= 2 and path[1] == ':') {
return WindowsPath {
return WindowsPath{
.is_abs = isAbsoluteWindows(path),
.kind = WindowsPath.Kind.Drive,
.disk_designator = path[0..2],
};
}
if (path.len >= 1 and (path[0] == '/' or path[0] == '\\') and
(path.len == 1 or (path[1] != '/' and path[1] != '\\')))
(path.len == 1 or (path[1] != '/' and path[1] != '\\')))
{
return WindowsPath {
return WindowsPath{
.is_abs = true,
.kind = WindowsPath.Kind.None,
.disk_designator = path[0..0],
};
}
const relative_path = WindowsPath {
const relative_path = WindowsPath{
.kind = WindowsPath.Kind.None,
.disk_designator = []u8{},
.is_abs = false,
@ -178,7 +175,7 @@ pub fn windowsParsePath(path: []const u8) WindowsPath {
// TODO when I combined these together with `inline for` the compiler crashed
{
const this_sep = '/';
const two_sep = []u8{this_sep, this_sep};
const two_sep = []u8{ this_sep, this_sep };
if (mem.startsWith(u8, path, two_sep)) {
if (path[2] == this_sep) {
return relative_path;
@ -187,7 +184,7 @@ pub fn windowsParsePath(path: []const u8) WindowsPath {
var it = mem.split(path, []u8{this_sep});
_ = (it.next() ?? return relative_path);
_ = (it.next() ?? return relative_path);
return WindowsPath {
return WindowsPath{
.is_abs = isAbsoluteWindows(path),
.kind = WindowsPath.Kind.NetworkShare,
.disk_designator = path[0..it.index],
@ -196,7 +193,7 @@ pub fn windowsParsePath(path: []const u8) WindowsPath {
}
{
const this_sep = '\\';
const two_sep = []u8{this_sep, this_sep};
const two_sep = []u8{ this_sep, this_sep };
if (mem.startsWith(u8, path, two_sep)) {
if (path[2] == this_sep) {
return relative_path;
@ -205,7 +202,7 @@ pub fn windowsParsePath(path: []const u8) WindowsPath {
var it = mem.split(path, []u8{this_sep});
_ = (it.next() ?? return relative_path);
_ = (it.next() ?? return relative_path);
return WindowsPath {
return WindowsPath{
.is_abs = isAbsoluteWindows(path),
.kind = WindowsPath.Kind.NetworkShare,
.disk_designator = path[0..it.index],
@ -296,7 +293,7 @@ fn compareDiskDesignators(kind: WindowsPath.Kind, p1: []const u8, p2: []const u8
fn asciiUpper(byte: u8) u8 {
return switch (byte) {
'a' ... 'z' => 'A' + (byte - 'a'),
'a'...'z' => 'A' + (byte - 'a'),
else => byte,
};
}
@ -372,7 +369,6 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) ![]u8 {
max_size += p.len + 1;
}
// if we will result with a disk designator, loop again to determine
// which is the last time the disk designator is absolutely specified, if any
// and count up the max bytes for paths related to this disk designator
@ -386,8 +382,7 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) ![]u8 {
const parsed = windowsParsePath(p);
if (parsed.kind != WindowsPath.Kind.None) {
if (parsed.kind == have_drive_kind) {
correct_disk_designator = compareDiskDesignators(have_drive_kind,
result_disk_designator, parsed.disk_designator);
correct_disk_designator = compareDiskDesignators(have_drive_kind, result_disk_designator, parsed.disk_designator);
} else {
continue;
}
@ -404,7 +399,6 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) ![]u8 {
}
}
// Allocate result and fill in the disk designator, calling getCwd if we have to.
var result: []u8 = undefined;
var result_index: usize = 0;
@ -433,7 +427,7 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) ![]u8 {
result_index += 1;
mem.copy(u8, result[result_index..], other_name);
result_index += other_name.len;
result_disk_designator = result[0..result_index];
},
WindowsPath.Kind.None => {
@ -478,8 +472,7 @@ pub fn resolveWindows(allocator: &Allocator, paths: []const []const u8) ![]u8 {
if (parsed.kind != WindowsPath.Kind.None) {
if (parsed.kind == have_drive_kind) {
correct_disk_designator = compareDiskDesignators(have_drive_kind,
result_disk_designator, parsed.disk_designator);
correct_disk_designator = compareDiskDesignators(have_drive_kind, result_disk_designator, parsed.disk_designator);
} else {
continue;
}
@ -591,7 +584,7 @@ test "os.path.resolve" {
}
assert(mem.eql(u8, testResolveWindows([][]const u8{"."}), cwd));
} else {
assert(mem.eql(u8, testResolvePosix([][]const u8{"a/b/c/", "../../.."}), cwd));
assert(mem.eql(u8, testResolvePosix([][]const u8{ "a/b/c/", "../../.." }), cwd));
assert(mem.eql(u8, testResolvePosix([][]const u8{"."}), cwd));
}
}
@ -601,16 +594,15 @@ test "os.path.resolveWindows" {
const cwd = try os.getCwd(debug.global_allocator);
const parsed_cwd = windowsParsePath(cwd);
{
const result = testResolveWindows([][]const u8{"/usr/local", "lib\\zig\\std\\array_list.zig"});
const expected = try join(debug.global_allocator,
parsed_cwd.disk_designator, "usr\\local\\lib\\zig\\std\\array_list.zig");
const result = testResolveWindows([][]const u8{ "/usr/local", "lib\\zig\\std\\array_list.zig" });
const expected = try join(debug.global_allocator, parsed_cwd.disk_designator, "usr\\local\\lib\\zig\\std\\array_list.zig");
if (parsed_cwd.kind == WindowsPath.Kind.Drive) {
expected[0] = asciiUpper(parsed_cwd.disk_designator[0]);
}
assert(mem.eql(u8, result, expected));
}
{
const result = testResolveWindows([][]const u8{"usr/local", "lib\\zig"});
const result = testResolveWindows([][]const u8{ "usr/local", "lib\\zig" });
const expected = try join(debug.global_allocator, cwd, "usr\\local\\lib\\zig");
if (parsed_cwd.kind == WindowsPath.Kind.Drive) {
expected[0] = asciiUpper(parsed_cwd.disk_designator[0]);
@ -619,33 +611,32 @@ test "os.path.resolveWindows" {
}
}
assert(mem.eql(u8, testResolveWindows([][]const u8{"c:\\a\\b\\c", "/hi", "ok"}), "C:\\hi\\ok"));
assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/blah\\blah", "d:/games", "c:../a"}), "C:\\blah\\a"));
assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/blah\\blah", "d:/games", "C:../a"}), "C:\\blah\\a"));
assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/ignore", "d:\\a/b\\c/d", "\\e.exe"}), "D:\\e.exe"));
assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/ignore", "c:/some/file"}), "C:\\some\\file"));
assert(mem.eql(u8, testResolveWindows([][]const u8{"d:/ignore", "d:some/dir//"}), "D:\\ignore\\some\\dir"));
assert(mem.eql(u8, testResolveWindows([][]const u8{"//server/share", "..", "relative\\"}), "\\\\server\\share\\relative"));
assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/", "//"}), "C:\\"));
assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/", "//dir"}), "C:\\dir"));
assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/", "//server/share"}), "\\\\server\\share\\"));
assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/", "//server//share"}), "\\\\server\\share\\"));
assert(mem.eql(u8, testResolveWindows([][]const u8{"c:/", "///some//dir"}), "C:\\some\\dir"));
assert(mem.eql(u8, testResolveWindows([][]const u8{"C:\\foo\\tmp.3\\", "..\\tmp.3\\cycles\\root.js"}),
"C:\\foo\\tmp.3\\cycles\\root.js"));
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:\\a\\b\\c", "/hi", "ok" }), "C:\\hi\\ok"));
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/blah\\blah", "d:/games", "c:../a" }), "C:\\blah\\a"));
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/blah\\blah", "d:/games", "C:../a" }), "C:\\blah\\a"));
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/ignore", "d:\\a/b\\c/d", "\\e.exe" }), "D:\\e.exe"));
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/ignore", "c:/some/file" }), "C:\\some\\file"));
assert(mem.eql(u8, testResolveWindows([][]const u8{ "d:/ignore", "d:some/dir//" }), "D:\\ignore\\some\\dir"));
assert(mem.eql(u8, testResolveWindows([][]const u8{ "//server/share", "..", "relative\\" }), "\\\\server\\share\\relative"));
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "//" }), "C:\\"));
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "//dir" }), "C:\\dir"));
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "//server/share" }), "\\\\server\\share\\"));
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "//server//share" }), "\\\\server\\share\\"));
assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "///some//dir" }), "C:\\some\\dir"));
assert(mem.eql(u8, testResolveWindows([][]const u8{ "C:\\foo\\tmp.3\\", "..\\tmp.3\\cycles\\root.js" }), "C:\\foo\\tmp.3\\cycles\\root.js"));
}
test "os.path.resolvePosix" {
assert(mem.eql(u8, testResolvePosix([][]const u8{"/a/b", "c"}), "/a/b/c"));
assert(mem.eql(u8, testResolvePosix([][]const u8{"/a/b", "c", "//d", "e///"}), "/d/e"));
assert(mem.eql(u8, testResolvePosix([][]const u8{"/a/b/c", "..", "../"}), "/a"));
assert(mem.eql(u8, testResolvePosix([][]const u8{"/", "..", ".."}), "/"));
assert(mem.eql(u8, testResolvePosix([][]const u8{ "/a/b", "c" }), "/a/b/c"));
assert(mem.eql(u8, testResolvePosix([][]const u8{ "/a/b", "c", "//d", "e///" }), "/d/e"));
assert(mem.eql(u8, testResolvePosix([][]const u8{ "/a/b/c", "..", "../" }), "/a"));
assert(mem.eql(u8, testResolvePosix([][]const u8{ "/", "..", ".." }), "/"));
assert(mem.eql(u8, testResolvePosix([][]const u8{"/a/b/c/"}), "/a/b/c"));
assert(mem.eql(u8, testResolvePosix([][]const u8{"/var/lib", "../", "file/"}), "/var/file"));
assert(mem.eql(u8, testResolvePosix([][]const u8{"/var/lib", "/../", "file/"}), "/file"));
assert(mem.eql(u8, testResolvePosix([][]const u8{"/some/dir", ".", "/absolute/"}), "/absolute"));
assert(mem.eql(u8, testResolvePosix([][]const u8{"/foo/tmp.3/", "../tmp.3/cycles/root.js"}), "/foo/tmp.3/cycles/root.js"));
assert(mem.eql(u8, testResolvePosix([][]const u8{ "/var/lib", "../", "file/" }), "/var/file"));
assert(mem.eql(u8, testResolvePosix([][]const u8{ "/var/lib", "/../", "file/" }), "/file"));
assert(mem.eql(u8, testResolvePosix([][]const u8{ "/some/dir", ".", "/absolute/" }), "/absolute"));
assert(mem.eql(u8, testResolvePosix([][]const u8{ "/foo/tmp.3/", "../tmp.3/cycles/root.js" }), "/foo/tmp.3/cycles/root.js"));
}
fn testResolveWindows(paths: []const []const u8) []u8 {
@ -1079,9 +1070,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) ![]u8 {
mem.copy(u8, pathname_buf, pathname);
pathname_buf[pathname.len] = 0;
const h_file = windows.CreateFileA(pathname_buf.ptr,
windows.GENERIC_READ, windows.FILE_SHARE_READ, null, windows.OPEN_EXISTING,
windows.FILE_ATTRIBUTE_NORMAL, null);
const h_file = windows.CreateFileA(pathname_buf.ptr, windows.GENERIC_READ, windows.FILE_SHARE_READ, null, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL, null);
if (h_file == windows.INVALID_HANDLE_VALUE) {
const err = windows.GetLastError();
return switch (err) {
@ -1161,7 +1150,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) ![]u8 {
return allocator.shrink(u8, result_buf, cstr.len(result_buf.ptr));
},
Os.linux => {
const fd = try os.posixOpen(allocator, pathname, posix.O_PATH|posix.O_NONBLOCK|posix.O_CLOEXEC, 0);
const fd = try os.posixOpen(allocator, pathname, posix.O_PATH | posix.O_NONBLOCK | posix.O_CLOEXEC, 0);
defer os.close(fd);
var buf: ["/proc/self/fd/-2147483648".len]u8 = undefined;

View File

@ -27,7 +27,7 @@ pub fn sleep(seconds: usize, nanoseconds: usize) void {
const u63 = @IntType(false, 63);
pub fn posixSleep(seconds: u63, nanoseconds: u63) void {
var req = posix.timespec {
var req = posix.timespec{
.tv_sec = seconds,
.tv_nsec = nanoseconds,
};
@ -71,7 +71,7 @@ fn milliTimestampWindows() u64 {
var ft: i64 = undefined;
windows.GetSystemTimeAsFileTime(&ft);
const hns_per_ms = (ns_per_s / 100) / ms_per_s;
const epoch_adj = epoch.windows * ms_per_s;
const epoch_adj = epoch.windows * ms_per_s;
return u64(@divFloor(ft, hns_per_ms) + epoch_adj);
}
@ -83,7 +83,7 @@ fn milliTimestampDarwin() u64 {
debug.assert(err == 0);
const sec_ms = u64(tv.tv_sec) * ms_per_s;
const usec_ms = @divFloor(u64(tv.tv_usec), us_per_s / ms_per_s);
return u64(sec_ms) + u64(usec_ms);
return u64(sec_ms) + u64(usec_ms);
}
fn milliTimestampPosix() u64 {
@ -110,17 +110,16 @@ pub const s_per_hour = s_per_min * 60;
pub const s_per_day = s_per_hour * 24;
pub const s_per_week = s_per_day * 7;
/// A monotonic high-performance timer.
/// Timer.start() must be called to initialize the struct, which captures
/// the counter frequency on windows and darwin, records the resolution,
/// and gives the user an oportunity to check for the existnece of
/// monotonic clocks without forcing them to check for error on each read.
/// .resolution is in nanoseconds on all platforms but .start_time's meaning
/// depends on the OS. On Windows and Darwin it is a hardware counter
/// .resolution is in nanoseconds on all platforms but .start_time's meaning
/// depends on the OS. On Windows and Darwin it is a hardware counter
/// value that requires calculation to convert to a meaninful unit.
pub const Timer = struct {
//if we used resolution's value when performing the
// performance counter calc on windows/darwin, it would
// be less precise
@ -131,10 +130,9 @@ pub const Timer = struct {
},
resolution: u64,
start_time: u64,
//At some point we may change our minds on RAW, but for now we're
// sticking with posix standard MONOTONIC. For more information, see:
// sticking with posix standard MONOTONIC. For more information, see:
// https://github.com/ziglang/zig/pull/933
//
//const monotonic_clock_id = switch(builtin.os) {
@ -142,20 +140,21 @@ pub const Timer = struct {
// else => posix.CLOCK_MONOTONIC,
//};
const monotonic_clock_id = posix.CLOCK_MONOTONIC;
/// Initialize the timer structure.
//This gives us an oportunity to grab the counter frequency in windows.
//On Windows: QueryPerformanceCounter will succeed on anything >= XP/2000.
//On Posix: CLOCK_MONOTONIC will only fail if the monotonic counter is not
// supported, or if the timespec pointer is out of bounds, which should be
//On Posix: CLOCK_MONOTONIC will only fail if the monotonic counter is not
// supported, or if the timespec pointer is out of bounds, which should be
// impossible here barring cosmic rays or other such occurances of
// incredibly bad luck.
//On Darwin: This cannot fail, as far as I am able to tell.
const TimerError = error{TimerUnsupported, Unexpected};
const TimerError = error{
TimerUnsupported,
Unexpected,
};
pub fn start() TimerError!Timer {
var self: Timer = undefined;
switch (builtin.os) {
Os.windows => {
var freq: i64 = undefined;
@ -163,7 +162,7 @@ pub const Timer = struct {
if (err == windows.FALSE) return error.TimerUnsupported;
self.frequency = u64(freq);
self.resolution = @divFloor(ns_per_s, self.frequency);
var start_time: i64 = undefined;
err = windows.QueryPerformanceCounter(&start_time);
debug.assert(err != windows.FALSE);
@ -171,9 +170,9 @@ pub const Timer = struct {
},
Os.linux => {
//On Linux, seccomp can do arbitrary things to our ability to call
// syscalls, including return any errno value it wants and
// syscalls, including return any errno value it wants and
// inconsistently throwing errors. Since we can't account for
// abuses of seccomp in a reasonable way, we'll assume that if
// abuses of seccomp in a reasonable way, we'll assume that if
// seccomp is going to block us it will at least do so consistently
var ts: posix.timespec = undefined;
var result = posix.clock_getres(monotonic_clock_id, &ts);
@ -184,7 +183,7 @@ pub const Timer = struct {
else => return std.os.unexpectedErrorPosix(errno),
}
self.resolution = u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec);
result = posix.clock_gettime(monotonic_clock_id, &ts);
errno = posix.getErrno(result);
if (errno != 0) return std.os.unexpectedErrorPosix(errno);
@ -199,7 +198,7 @@ pub const Timer = struct {
}
return self;
}
/// Reads the timer value since start or the last reset in nanoseconds
pub fn read(self: &Timer) u64 {
var clock = clockNative() - self.start_time;
@ -210,13 +209,12 @@ pub const Timer = struct {
else => @compileError("Unsupported OS"),
};
}
/// Resets the timer value to 0/now.
pub fn reset(self: &Timer) void
{
pub fn reset(self: &Timer) void {
self.start_time = clockNative();
}
/// Returns the current value of the timer in nanoseconds, then resets it
pub fn lap(self: &Timer) u64 {
var now = clockNative();
@ -224,26 +222,25 @@ pub const Timer = struct {
self.start_time = now;
return lap_time;
}
const clockNative = switch (builtin.os) {
Os.windows => clockWindows,
Os.linux => clockLinux,
Os.macosx, Os.ios => clockDarwin,
else => @compileError("Unsupported OS"),
};
fn clockWindows() u64 {
var result: i64 = undefined;
var err = windows.QueryPerformanceCounter(&result);
debug.assert(err != windows.FALSE);
return u64(result);
}
fn clockDarwin() u64 {
return darwin.mach_absolute_time();
}
fn clockLinux() u64 {
var ts: posix.timespec = undefined;
var result = posix.clock_gettime(monotonic_clock_id, &ts);
@ -252,10 +249,6 @@ pub const Timer = struct {
}
};
test "os.time.sleep" {
sleep(0, 1);
}
@ -263,7 +256,7 @@ test "os.time.sleep" {
test "os.time.timestamp" {
const ns_per_ms = (ns_per_s / ms_per_s);
const margin = 50;
const time_0 = milliTimestamp();
sleep(0, ns_per_ms);
const time_1 = milliTimestamp();
@ -274,15 +267,15 @@ test "os.time.timestamp" {
test "os.time.Timer" {
const ns_per_ms = (ns_per_s / ms_per_s);
const margin = ns_per_ms * 50;
var timer = try Timer.start();
sleep(0, 10 * ns_per_ms);
const time_0 = timer.read();
debug.assert(time_0 > 0 and time_0 < margin);
const time_1 = timer.lap();
debug.assert(time_1 >= time_0);
timer.reset();
debug.assert(timer.read() < time_1);
}

File diff suppressed because it is too large Load Diff

View File

@ -1,33 +1,59 @@
pub const ERROR = @import("error.zig");
pub extern "advapi32" stdcallcc fn CryptAcquireContextA(phProv: &HCRYPTPROV, pszContainer: ?LPCSTR,
pszProvider: ?LPCSTR, dwProvType: DWORD, dwFlags: DWORD) BOOL;
pub extern "advapi32" stdcallcc fn CryptAcquireContextA(
phProv: &HCRYPTPROV,
pszContainer: ?LPCSTR,
pszProvider: ?LPCSTR,
dwProvType: DWORD,
dwFlags: DWORD,
) BOOL;
pub extern "advapi32" stdcallcc fn CryptReleaseContext(hProv: HCRYPTPROV, dwFlags: DWORD) BOOL;
pub extern "advapi32" stdcallcc fn CryptGenRandom(hProv: HCRYPTPROV, dwLen: DWORD, pbBuffer: &BYTE) BOOL;
pub extern "kernel32" stdcallcc fn CloseHandle(hObject: HANDLE) BOOL;
pub extern "kernel32" stdcallcc fn CreateDirectoryA(lpPathName: LPCSTR,
lpSecurityAttributes: ?&SECURITY_ATTRIBUTES) BOOL;
pub extern "kernel32" stdcallcc fn CreateDirectoryA(
lpPathName: LPCSTR,
lpSecurityAttributes: ?&SECURITY_ATTRIBUTES,
) BOOL;
pub extern "kernel32" stdcallcc fn CreateFileA(lpFileName: LPCSTR, dwDesiredAccess: DWORD,
dwShareMode: DWORD, lpSecurityAttributes: ?LPSECURITY_ATTRIBUTES, dwCreationDisposition: DWORD,
dwFlagsAndAttributes: DWORD, hTemplateFile: ?HANDLE) HANDLE;
pub extern "kernel32" stdcallcc fn CreateFileA(
lpFileName: LPCSTR,
dwDesiredAccess: DWORD,
dwShareMode: DWORD,
lpSecurityAttributes: ?LPSECURITY_ATTRIBUTES,
dwCreationDisposition: DWORD,
dwFlagsAndAttributes: DWORD,
hTemplateFile: ?HANDLE,
) HANDLE;
pub extern "kernel32" stdcallcc fn CreatePipe(hReadPipe: &HANDLE, hWritePipe: &HANDLE,
lpPipeAttributes: &const SECURITY_ATTRIBUTES, nSize: DWORD) BOOL;
pub extern "kernel32" stdcallcc fn CreatePipe(
hReadPipe: &HANDLE,
hWritePipe: &HANDLE,
lpPipeAttributes: &const SECURITY_ATTRIBUTES,
nSize: DWORD,
) BOOL;
pub extern "kernel32" stdcallcc fn CreateProcessA(lpApplicationName: ?LPCSTR, lpCommandLine: LPSTR,
lpProcessAttributes: ?&SECURITY_ATTRIBUTES, lpThreadAttributes: ?&SECURITY_ATTRIBUTES, bInheritHandles: BOOL,
dwCreationFlags: DWORD, lpEnvironment: ?&c_void, lpCurrentDirectory: ?LPCSTR, lpStartupInfo: &STARTUPINFOA,
lpProcessInformation: &PROCESS_INFORMATION) BOOL;
pub extern "kernel32" stdcallcc fn CreateSymbolicLinkA(lpSymlinkFileName: LPCSTR, lpTargetFileName: LPCSTR,
dwFlags: DWORD) BOOLEAN;
pub extern "kernel32" stdcallcc fn CreateProcessA(
lpApplicationName: ?LPCSTR,
lpCommandLine: LPSTR,
lpProcessAttributes: ?&SECURITY_ATTRIBUTES,
lpThreadAttributes: ?&SECURITY_ATTRIBUTES,
bInheritHandles: BOOL,
dwCreationFlags: DWORD,
lpEnvironment: ?&c_void,
lpCurrentDirectory: ?LPCSTR,
lpStartupInfo: &STARTUPINFOA,
lpProcessInformation: &PROCESS_INFORMATION,
) BOOL;
pub extern "kernel32" stdcallcc fn CreateSymbolicLinkA(
lpSymlinkFileName: LPCSTR,
lpTargetFileName: LPCSTR,
dwFlags: DWORD,
) BOOLEAN;
pub extern "kernel32" stdcallcc fn CreateThread(lpThreadAttributes: ?LPSECURITY_ATTRIBUTES, dwStackSize: SIZE_T, lpStartAddress: LPTHREAD_START_ROUTINE, lpParameter: ?LPVOID, dwCreationFlags: DWORD, lpThreadId: ?LPDWORD) ?HANDLE;
@ -55,12 +81,19 @@ pub extern "kernel32" stdcallcc fn GetModuleFileNameA(hModule: ?HMODULE, lpFilen
pub extern "kernel32" stdcallcc fn GetLastError() DWORD;
pub extern "kernel32" stdcallcc fn GetFileInformationByHandleEx(in_hFile: HANDLE,
in_FileInformationClass: FILE_INFO_BY_HANDLE_CLASS, out_lpFileInformation: &c_void,
in_dwBufferSize: DWORD) BOOL;
pub extern "kernel32" stdcallcc fn GetFileInformationByHandleEx(
in_hFile: HANDLE,
in_FileInformationClass: FILE_INFO_BY_HANDLE_CLASS,
out_lpFileInformation: &c_void,
in_dwBufferSize: DWORD,
) BOOL;
pub extern "kernel32" stdcallcc fn GetFinalPathNameByHandleA(hFile: HANDLE, lpszFilePath: LPSTR,
cchFilePath: DWORD, dwFlags: DWORD) DWORD;
pub extern "kernel32" stdcallcc fn GetFinalPathNameByHandleA(
hFile: HANDLE,
lpszFilePath: LPSTR,
cchFilePath: DWORD,
dwFlags: DWORD,
) DWORD;
pub extern "kernel32" stdcallcc fn GetProcessHeap() ?HANDLE;
@ -80,21 +113,32 @@ pub extern "kernel32" stdcallcc fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBy
pub extern "kernel32" stdcallcc fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: &c_void) BOOL;
pub extern "kernel32" stdcallcc fn MoveFileExA(lpExistingFileName: LPCSTR, lpNewFileName: LPCSTR,
dwFlags: DWORD) BOOL;
pub extern "kernel32" stdcallcc fn MoveFileExA(
lpExistingFileName: LPCSTR,
lpNewFileName: LPCSTR,
dwFlags: DWORD,
) BOOL;
pub extern "kernel32" stdcallcc fn QueryPerformanceCounter(lpPerformanceCount: &LARGE_INTEGER) BOOL;
pub extern "kernel32" stdcallcc fn QueryPerformanceFrequency(lpFrequency: &LARGE_INTEGER) BOOL;
pub extern "kernel32" stdcallcc fn PathFileExists(pszPath: ?LPCTSTR) BOOL;
pub extern "kernel32" stdcallcc fn ReadFile(in_hFile: HANDLE, out_lpBuffer: &c_void,
in_nNumberOfBytesToRead: DWORD, out_lpNumberOfBytesRead: &DWORD,
in_out_lpOverlapped: ?&OVERLAPPED) BOOL;
pub extern "kernel32" stdcallcc fn ReadFile(
in_hFile: HANDLE,
out_lpBuffer: &c_void,
in_nNumberOfBytesToRead: DWORD,
out_lpNumberOfBytesRead: &DWORD,
in_out_lpOverlapped: ?&OVERLAPPED,
) BOOL;
pub extern "kernel32" stdcallcc fn SetFilePointerEx(in_fFile: HANDLE, in_liDistanceToMove: LARGE_INTEGER,
out_opt_ldNewFilePointer: ?&LARGE_INTEGER, in_dwMoveMethod: DWORD) BOOL;
pub extern "kernel32" stdcallcc fn SetFilePointerEx(
in_fFile: HANDLE,
in_liDistanceToMove: LARGE_INTEGER,
out_opt_ldNewFilePointer: ?&LARGE_INTEGER,
in_dwMoveMethod: DWORD,
) BOOL;
pub extern "kernel32" stdcallcc fn SetHandleInformation(hObject: HANDLE, dwMask: DWORD, dwFlags: DWORD) BOOL;
@ -104,14 +148,18 @@ pub extern "kernel32" stdcallcc fn TerminateProcess(hProcess: HANDLE, uExitCode:
pub extern "kernel32" stdcallcc fn WaitForSingleObject(hHandle: HANDLE, dwMilliseconds: DWORD) DWORD;
pub extern "kernel32" stdcallcc fn WriteFile(in_hFile: HANDLE, in_lpBuffer: &const c_void,
in_nNumberOfBytesToWrite: DWORD, out_lpNumberOfBytesWritten: ?&DWORD,
in_out_lpOverlapped: ?&OVERLAPPED) BOOL;
pub extern "kernel32" stdcallcc fn WriteFile(
in_hFile: HANDLE,
in_lpBuffer: &const c_void,
in_nNumberOfBytesToWrite: DWORD,
out_lpNumberOfBytesWritten: ?&DWORD,
in_out_lpOverlapped: ?&OVERLAPPED,
) BOOL;
//TODO: call unicode versions instead of relying on ANSI code page
pub extern "kernel32" stdcallcc fn LoadLibraryA(lpLibFileName: LPCSTR) ?HMODULE;
pub extern "kernel32" stdcallcc fn FreeLibrary(hModule: HMODULE) BOOL;
pub extern "kernel32" stdcallcc fn FreeLibrary(hModule: HMODULE) BOOL;
pub extern "user32" stdcallcc fn MessageBoxA(hWnd: ?HANDLE, lpText: ?LPCTSTR, lpCaption: ?LPCTSTR, uType: UINT) c_int;
@ -176,49 +224,51 @@ pub const MAX_PATH = 260;
// TODO issue #305
pub const FILE_INFO_BY_HANDLE_CLASS = u32;
pub const FileBasicInfo = 0;
pub const FileStandardInfo = 1;
pub const FileNameInfo = 2;
pub const FileRenameInfo = 3;
pub const FileDispositionInfo = 4;
pub const FileAllocationInfo = 5;
pub const FileEndOfFileInfo = 6;
pub const FileStreamInfo = 7;
pub const FileCompressionInfo = 8;
pub const FileAttributeTagInfo = 9;
pub const FileIdBothDirectoryInfo = 10;
pub const FileIdBothDirectoryRestartInfo = 11;
pub const FileIoPriorityHintInfo = 12;
pub const FileRemoteProtocolInfo = 13;
pub const FileFullDirectoryInfo = 14;
pub const FileFullDirectoryRestartInfo = 15;
pub const FileStorageInfo = 16;
pub const FileAlignmentInfo = 17;
pub const FileIdInfo = 18;
pub const FileIdExtdDirectoryInfo = 19;
pub const FileIdExtdDirectoryRestartInfo = 20;
pub const FileBasicInfo = 0;
pub const FileStandardInfo = 1;
pub const FileNameInfo = 2;
pub const FileRenameInfo = 3;
pub const FileDispositionInfo = 4;
pub const FileAllocationInfo = 5;
pub const FileEndOfFileInfo = 6;
pub const FileStreamInfo = 7;
pub const FileCompressionInfo = 8;
pub const FileAttributeTagInfo = 9;
pub const FileIdBothDirectoryInfo = 10;
pub const FileIdBothDirectoryRestartInfo = 11;
pub const FileIoPriorityHintInfo = 12;
pub const FileRemoteProtocolInfo = 13;
pub const FileFullDirectoryInfo = 14;
pub const FileFullDirectoryRestartInfo = 15;
pub const FileStorageInfo = 16;
pub const FileAlignmentInfo = 17;
pub const FileIdInfo = 18;
pub const FileIdExtdDirectoryInfo = 19;
pub const FileIdExtdDirectoryRestartInfo = 20;
pub const FILE_NAME_INFO = extern struct {
FileNameLength: DWORD,
FileName: [1]WCHAR,
};
/// Return the normalized drive name. This is the default.
pub const FILE_NAME_NORMALIZED = 0x0;
/// Return the opened file name (not normalized).
pub const FILE_NAME_OPENED = 0x8;
/// Return the path with the drive letter. This is the default.
pub const VOLUME_NAME_DOS = 0x0;
/// Return the path with a volume GUID path instead of the drive name.
pub const VOLUME_NAME_GUID = 0x1;
/// Return the path with no drive information.
pub const VOLUME_NAME_NONE = 0x4;
/// Return the path with the volume device path.
pub const VOLUME_NAME_NT = 0x2;
pub const SECURITY_ATTRIBUTES = extern struct {
nLength: DWORD,
lpSecurityDescriptor: ?&c_void,
@ -227,7 +277,6 @@ pub const SECURITY_ATTRIBUTES = extern struct {
pub const PSECURITY_ATTRIBUTES = &SECURITY_ATTRIBUTES;
pub const LPSECURITY_ATTRIBUTES = &SECURITY_ATTRIBUTES;
pub const GENERIC_READ = 0x80000000;
pub const GENERIC_WRITE = 0x40000000;
pub const GENERIC_EXECUTE = 0x20000000;
@ -243,7 +292,6 @@ pub const OPEN_ALWAYS = 4;
pub const OPEN_EXISTING = 3;
pub const TRUNCATE_EXISTING = 5;
pub const FILE_ATTRIBUTE_ARCHIVE = 0x20;
pub const FILE_ATTRIBUTE_ENCRYPTED = 0x4000;
pub const FILE_ATTRIBUTE_HIDDEN = 0x2;

View File

@ -7,7 +7,7 @@ const mem = std.mem;
const BufMap = std.BufMap;
const cstr = std.cstr;
pub const WaitError = error {
pub const WaitError = error{
WaitAbandoned,
WaitTimeOut,
Unexpected,
@ -33,7 +33,7 @@ pub fn windowsClose(handle: windows.HANDLE) void {
assert(windows.CloseHandle(handle) != 0);
}
pub const WriteError = error {
pub const WriteError = error{
SystemResources,
OperationAborted,
IoPending,
@ -68,20 +68,18 @@ pub fn windowsIsCygwinPty(handle: windows.HANDLE) bool {
const size = @sizeOf(windows.FILE_NAME_INFO);
var name_info_bytes align(@alignOf(windows.FILE_NAME_INFO)) = []u8{0} ** (size + windows.MAX_PATH);
if (windows.GetFileInformationByHandleEx(handle, windows.FileNameInfo,
@ptrCast(&c_void, &name_info_bytes[0]), u32(name_info_bytes.len)) == 0)
{
if (windows.GetFileInformationByHandleEx(handle, windows.FileNameInfo, @ptrCast(&c_void, &name_info_bytes[0]), u32(name_info_bytes.len)) == 0) {
return true;
}
const name_info = @ptrCast(&const windows.FILE_NAME_INFO, &name_info_bytes[0]);
const name_bytes = name_info_bytes[size..size + usize(name_info.FileNameLength)];
const name_wide = ([]u16)(name_bytes);
return mem.indexOf(u16, name_wide, []u16{'m','s','y','s','-'}) != null or
mem.indexOf(u16, name_wide, []u16{'-','p','t','y'}) != null;
const name_wide = ([]u16)(name_bytes);
return mem.indexOf(u16, name_wide, []u16{ 'm', 's', 'y', 's', '-' }) != null or
mem.indexOf(u16, name_wide, []u16{ '-', 'p', 't', 'y' }) != null;
}
pub const OpenError = error {
pub const OpenError = error{
SharingViolation,
PathAlreadyExists,
FileNotFound,
@ -92,15 +90,18 @@ pub const OpenError = error {
};
/// `file_path` needs to be copied in memory to add a null terminating byte, hence the allocator.
pub fn windowsOpen(allocator: &mem.Allocator, file_path: []const u8, desired_access: windows.DWORD, share_mode: windows.DWORD,
creation_disposition: windows.DWORD, flags_and_attrs: windows.DWORD)
OpenError!windows.HANDLE
{
pub fn windowsOpen(
allocator: &mem.Allocator,
file_path: []const u8,
desired_access: windows.DWORD,
share_mode: windows.DWORD,
creation_disposition: windows.DWORD,
flags_and_attrs: windows.DWORD,
) OpenError!windows.HANDLE {
const path_with_null = try cstr.addNullByte(allocator, file_path);
defer allocator.free(path_with_null);
const result = windows.CreateFileA(path_with_null.ptr, desired_access, share_mode, null, creation_disposition,
flags_and_attrs, null);
const result = windows.CreateFileA(path_with_null.ptr, desired_access, share_mode, null, creation_disposition, flags_and_attrs, null);
if (result == windows.INVALID_HANDLE_VALUE) {
const err = windows.GetLastError();
@ -156,18 +157,16 @@ pub fn windowsLoadDll(allocator: &mem.Allocator, dll_path: []const u8) !windows.
}
pub fn windowsUnloadDll(hModule: windows.HMODULE) void {
assert(windows.FreeLibrary(hModule)!= 0);
assert(windows.FreeLibrary(hModule) != 0);
}
test "InvalidDll" {
if (builtin.os != builtin.Os.windows) return;
const DllName = "asdf.dll";
const allocator = std.debug.global_allocator;
const handle = os.windowsLoadDll(allocator, DllName) catch |err| {
const handle = os.windowsLoadDll(allocator, DllName) catch |err| {
assert(err == error.DllNotFound);
return;
};
}

View File

@ -3,35 +3,35 @@
//////////////////////////
pub const Message = struct {
sender: MailboxId,
sender: MailboxId,
receiver: MailboxId,
type: usize,
payload: usize,
type: usize,
payload: usize,
pub fn from(mailbox_id: &const MailboxId) Message {
return Message {
.sender = MailboxId.Undefined,
return Message{
.sender = MailboxId.Undefined,
.receiver = *mailbox_id,
.type = 0,
.payload = 0,
.type = 0,
.payload = 0,
};
}
pub fn to(mailbox_id: &const MailboxId, msg_type: usize) Message {
return Message {
.sender = MailboxId.This,
return Message{
.sender = MailboxId.This,
.receiver = *mailbox_id,
.type = msg_type,
.payload = 0,
.type = msg_type,
.payload = 0,
};
}
pub fn withData(mailbox_id: &const MailboxId, msg_type: usize, payload: usize) Message {
return Message {
.sender = MailboxId.This,
return Message{
.sender = MailboxId.This,
.receiver = *mailbox_id,
.type = msg_type,
.payload = payload,
.type = msg_type,
.payload = payload,
};
}
};
@ -40,27 +40,25 @@ pub const MailboxId = union(enum) {
Undefined,
This,
Kernel,
Port: u16,
Port: u16,
Thread: u16,
};
//////////////////////////////////////
//// Ports reserved for servers ////
//////////////////////////////////////
pub const Server = struct {
pub const Keyboard = MailboxId { .Port = 0 };
pub const Terminal = MailboxId { .Port = 1 };
pub const Keyboard = MailboxId{ .Port = 0 };
pub const Terminal = MailboxId{ .Port = 1 };
};
////////////////////////
//// POSIX things ////
////////////////////////
// Standard streams.
pub const STDIN_FILENO = 0;
pub const STDIN_FILENO = 0;
pub const STDOUT_FILENO = 1;
pub const STDERR_FILENO = 2;
@ -101,26 +99,24 @@ pub fn write(fd: i32, buf: &const u8, count: usize) usize {
return count;
}
///////////////////////////
//// Syscall numbers ////
///////////////////////////
pub const Syscall = enum(usize) {
exit = 0,
createPort = 1,
send = 2,
receive = 3,
subscribeIRQ = 4,
inb = 5,
map = 6,
createThread = 7,
exit = 0,
createPort = 1,
send = 2,
receive = 3,
subscribeIRQ = 4,
inb = 5,
map = 6,
createThread = 7,
createProcess = 8,
wait = 9,
portReady = 10,
wait = 9,
portReady = 10,
};
////////////////////
//// Syscalls ////
////////////////////
@ -157,7 +153,7 @@ pub fn map(v_addr: usize, p_addr: usize, size: usize, writable: bool) bool {
return syscall4(Syscall.map, v_addr, p_addr, size, usize(writable)) != 0;
}
pub fn createThread(function: fn()void) u16 {
pub fn createThread(function: fn() void) u16 {
return u16(syscall1(Syscall.createThread, @ptrToInt(function)));
}
@ -180,66 +176,84 @@ pub fn portReady(port: u16) bool {
inline fn syscall0(number: Syscall) usize {
return asm volatile ("int $0x80"
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number));
: [number] "{eax}" (number)
);
}
inline fn syscall1(number: Syscall, arg1: usize) usize {
return asm volatile ("int $0x80"
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number),
[arg1] "{ecx}" (arg1));
[arg1] "{ecx}" (arg1)
);
}
inline fn syscall2(number: Syscall, arg1: usize, arg2: usize) usize {
return asm volatile ("int $0x80"
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number),
[arg1] "{ecx}" (arg1),
[arg2] "{edx}" (arg2));
[arg1] "{ecx}" (arg1),
[arg2] "{edx}" (arg2)
);
}
inline fn syscall3(number: Syscall, arg1: usize, arg2: usize, arg3: usize) usize {
return asm volatile ("int $0x80"
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number),
[arg1] "{ecx}" (arg1),
[arg2] "{edx}" (arg2),
[arg3] "{ebx}" (arg3));
[arg1] "{ecx}" (arg1),
[arg2] "{edx}" (arg2),
[arg3] "{ebx}" (arg3)
);
}
inline fn syscall4(number: Syscall, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize {
return asm volatile ("int $0x80"
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number),
[arg1] "{ecx}" (arg1),
[arg2] "{edx}" (arg2),
[arg3] "{ebx}" (arg3),
[arg4] "{esi}" (arg4));
[arg1] "{ecx}" (arg1),
[arg2] "{edx}" (arg2),
[arg3] "{ebx}" (arg3),
[arg4] "{esi}" (arg4)
);
}
inline fn syscall5(number: Syscall, arg1: usize, arg2: usize, arg3: usize,
arg4: usize, arg5: usize) usize
{
inline fn syscall5(
number: Syscall,
arg1: usize,
arg2: usize,
arg3: usize,
arg4: usize,
arg5: usize,
) usize {
return asm volatile ("int $0x80"
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number),
[arg1] "{ecx}" (arg1),
[arg2] "{edx}" (arg2),
[arg3] "{ebx}" (arg3),
[arg4] "{esi}" (arg4),
[arg5] "{edi}" (arg5));
[arg1] "{ecx}" (arg1),
[arg2] "{edx}" (arg2),
[arg3] "{ebx}" (arg3),
[arg4] "{esi}" (arg4),
[arg5] "{edi}" (arg5)
);
}
inline fn syscall6(number: Syscall, arg1: usize, arg2: usize, arg3: usize,
arg4: usize, arg5: usize, arg6: usize) usize
{
inline fn syscall6(
number: Syscall,
arg1: usize,
arg2: usize,
arg3: usize,
arg4: usize,
arg5: usize,
arg6: usize,
) usize {
return asm volatile ("int $0x80"
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number),
[arg1] "{ecx}" (arg1),
[arg2] "{edx}" (arg2),
[arg3] "{ebx}" (arg3),
[arg4] "{esi}" (arg4),
[arg5] "{edi}" (arg5),
[arg6] "{ebp}" (arg6));
[arg1] "{ecx}" (arg1),
[arg2] "{edx}" (arg2),
[arg3] "{ebx}" (arg3),
[arg4] "{esi}" (arg4),
[arg5] "{edi}" (arg5),
[arg6] "{ebp}" (arg6)
);
}

View File

@ -69,7 +69,7 @@ pub const Random = struct {
break :x start;
} else x: {
// Can't overflow because the range is over signed ints
break :x math.negateCast(value - end_uint) catch unreachable;
break :x math.negateCast(value - end_uint) catch unreachable;
};
return result;
} else {
@ -156,7 +156,7 @@ const SplitMix64 = struct {
s: u64,
pub fn init(seed: u64) SplitMix64 {
return SplitMix64 { .s = seed };
return SplitMix64{ .s = seed };
}
pub fn next(self: &SplitMix64) u64 {
@ -172,7 +172,7 @@ const SplitMix64 = struct {
test "splitmix64 sequence" {
var r = SplitMix64.init(0xaeecf86f7878dd75);
const seq = []const u64 {
const seq = []const u64{
0x5dbd39db0178eb44,
0xa9900fb66b397da3,
0x5c1a28b1aeebcf5c,
@ -198,8 +198,8 @@ pub const Pcg = struct {
i: u64,
pub fn init(init_s: u64) Pcg {
var pcg = Pcg {
.random = Random { .fillFn = fill },
var pcg = Pcg{
.random = Random{ .fillFn = fill },
.s = undefined,
.i = undefined,
};
@ -265,7 +265,7 @@ test "pcg sequence" {
const s1: u64 = 0x84e9c579ef59bbf7;
r.seedTwo(s0, s1);
const seq = []const u32 {
const seq = []const u32{
2881561918,
3063928540,
1199791034,
@ -288,8 +288,8 @@ pub const Xoroshiro128 = struct {
s: [2]u64,
pub fn init(init_s: u64) Xoroshiro128 {
var x = Xoroshiro128 {
.random = Random { .fillFn = fill },
var x = Xoroshiro128{
.random = Random{ .fillFn = fill },
.s = undefined,
};
@ -314,9 +314,9 @@ pub const Xoroshiro128 = struct {
var s0: u64 = 0;
var s1: u64 = 0;
const table = []const u64 {
const table = []const u64{
0xbeac0467eba5facb,
0xd86b048b86aa9922
0xd86b048b86aa9922,
};
inline for (table) |entry| {
@ -374,7 +374,7 @@ test "xoroshiro sequence" {
r.s[0] = 0xaeecf86f7878dd75;
r.s[1] = 0x01cd153642e72622;
const seq1 = []const u64 {
const seq1 = []const u64{
0xb0ba0da5bb600397,
0x18a08afde614dccc,
0xa2635b956a31b929,
@ -387,10 +387,9 @@ test "xoroshiro sequence" {
std.debug.assert(s == r.next());
}
r.jump();
const seq2 = []const u64 {
const seq2 = []const u64{
0x95344a13556d3e22,
0xb4fb32dafa4d00df,
0xb2011d9ccdcfe2dd,
@ -421,8 +420,8 @@ pub const Isaac64 = struct {
i: usize,
pub fn init(init_s: u64) Isaac64 {
var isaac = Isaac64 {
.random = Random { .fillFn = fill },
var isaac = Isaac64{
.random = Random{ .fillFn = fill },
.r = undefined,
.m = undefined,
.a = undefined,
@ -456,20 +455,20 @@ pub const Isaac64 = struct {
{
var i: usize = 0;
while (i < midpoint) : (i += 4) {
self.step( ~(self.a ^ (self.a << 21)), i + 0, 0, midpoint);
self.step( self.a ^ (self.a >> 5) , i + 1, 0, midpoint);
self.step( self.a ^ (self.a << 12) , i + 2, 0, midpoint);
self.step( self.a ^ (self.a >> 33) , i + 3, 0, midpoint);
self.step(~(self.a ^ (self.a << 21)), i + 0, 0, midpoint);
self.step(self.a ^ (self.a >> 5), i + 1, 0, midpoint);
self.step(self.a ^ (self.a << 12), i + 2, 0, midpoint);
self.step(self.a ^ (self.a >> 33), i + 3, 0, midpoint);
}
}
{
var i: usize = 0;
while (i < midpoint) : (i += 4) {
self.step( ~(self.a ^ (self.a << 21)), i + 0, midpoint, 0);
self.step( self.a ^ (self.a >> 5) , i + 1, midpoint, 0);
self.step( self.a ^ (self.a << 12) , i + 2, midpoint, 0);
self.step( self.a ^ (self.a >> 33) , i + 3, midpoint, 0);
self.step(~(self.a ^ (self.a << 21)), i + 0, midpoint, 0);
self.step(self.a ^ (self.a >> 5), i + 1, midpoint, 0);
self.step(self.a ^ (self.a << 12), i + 2, midpoint, 0);
self.step(self.a ^ (self.a >> 33), i + 3, midpoint, 0);
}
}
@ -493,7 +492,7 @@ pub const Isaac64 = struct {
self.m[0] = init_s;
// prescrambled golden ratio constants
var a = []const u64 {
var a = []const u64{
0x647c4677a2884b7c,
0xb9f8b322c73ac862,
0x8c0ea5053d4712a0,
@ -513,14 +512,30 @@ pub const Isaac64 = struct {
a[x1] +%= self.m[j + x1];
}
a[0] -%= a[4]; a[5] ^= a[7] >> 9; a[7] +%= a[0];
a[1] -%= a[5]; a[6] ^= a[0] << 9; a[0] +%= a[1];
a[2] -%= a[6]; a[7] ^= a[1] >> 23; a[1] +%= a[2];
a[3] -%= a[7]; a[0] ^= a[2] << 15; a[2] +%= a[3];
a[4] -%= a[0]; a[1] ^= a[3] >> 14; a[3] +%= a[4];
a[5] -%= a[1]; a[2] ^= a[4] << 20; a[4] +%= a[5];
a[6] -%= a[2]; a[3] ^= a[5] >> 17; a[5] +%= a[6];
a[7] -%= a[3]; a[4] ^= a[6] << 14; a[6] +%= a[7];
a[0] -%= a[4];
a[5] ^= a[7] >> 9;
a[7] +%= a[0];
a[1] -%= a[5];
a[6] ^= a[0] << 9;
a[0] +%= a[1];
a[2] -%= a[6];
a[7] ^= a[1] >> 23;
a[1] +%= a[2];
a[3] -%= a[7];
a[0] ^= a[2] << 15;
a[2] +%= a[3];
a[4] -%= a[0];
a[1] ^= a[3] >> 14;
a[3] +%= a[4];
a[5] -%= a[1];
a[2] ^= a[4] << 20;
a[4] +%= a[5];
a[6] -%= a[2];
a[3] ^= a[5] >> 17;
a[5] +%= a[6];
a[7] -%= a[3];
a[4] ^= a[6] << 14;
a[6] +%= a[7];
comptime var x2: usize = 0;
inline while (x2 < 8) : (x2 += 1) {
@ -533,7 +548,7 @@ pub const Isaac64 = struct {
self.a = 0;
self.b = 0;
self.c = 0;
self.i = self.r.len; // trigger refill on first value
self.i = self.r.len; // trigger refill on first value
}
fn fill(r: &Random, buf: []u8) void {
@ -567,7 +582,7 @@ test "isaac64 sequence" {
var r = Isaac64.init(0);
// from reference implementation
const seq = []const u64 {
const seq = []const u64{
0xf67dfba498e4937c,
0x84a5066a9204f380,
0xfee34bd5f5514dbb,
@ -609,7 +624,7 @@ test "Random float" {
test "Random scalar" {
var prng = DefaultPrng.init(0);
const s = prng .random.scalar(u64);
const s = prng.random.scalar(u64);
}
test "Random bytes" {
@ -621,8 +636,8 @@ test "Random bytes" {
test "Random shuffle" {
var prng = DefaultPrng.init(0);
var seq = []const u8 { 0, 1, 2, 3, 4 };
var seen = []bool {false} ** 5;
var seq = []const u8{ 0, 1, 2, 3, 4 };
var seen = []bool{false} ** 5;
var i: usize = 0;
while (i < 1000) : (i += 1) {
@ -639,7 +654,8 @@ test "Random shuffle" {
fn sumArray(s: []const u8) u32 {
var r: u32 = 0;
for (s) |e| r += e;
for (s) |e|
r += e;
return r;
}

View File

@ -64,8 +64,14 @@ pub const ZigTable = struct {
};
// zigNorInit
fn ZigTableGen(comptime is_symmetric: bool, comptime r: f64, comptime v: f64, comptime f: fn(f64) f64,
comptime f_inv: fn(f64) f64, comptime zero_case: fn(&Random, f64) f64) ZigTable {
fn ZigTableGen(
comptime is_symmetric: bool,
comptime r: f64,
comptime v: f64,
comptime f: fn(f64) f64,
comptime f_inv: fn(f64) f64,
comptime zero_case: fn(&Random, f64) f64,
) ZigTable {
var tables: ZigTable = undefined;
tables.is_symmetric = is_symmetric;
@ -98,8 +104,12 @@ pub const NormDist = blk: {
const norm_r = 3.6541528853610088;
const norm_v = 0.00492867323399;
fn norm_f(x: f64) f64 { return math.exp(-x * x / 2.0); }
fn norm_f_inv(y: f64) f64 { return math.sqrt(-2.0 * math.ln(y)); }
fn norm_f(x: f64) f64 {
return math.exp(-x * x / 2.0);
}
fn norm_f_inv(y: f64) f64 {
return math.sqrt(-2.0 * math.ln(y));
}
fn norm_zero_case(random: &Random, u: f64) f64 {
var x: f64 = 1;
var y: f64 = 0;
@ -133,9 +143,15 @@ pub const ExpDist = blk: {
const exp_r = 7.69711747013104972;
const exp_v = 0.0039496598225815571993;
fn exp_f(x: f64) f64 { return math.exp(-x); }
fn exp_f_inv(y: f64) f64 { return -math.ln(y); }
fn exp_zero_case(random: &Random, _: f64) f64 { return exp_r - math.ln(random.float(f64)); }
fn exp_f(x: f64) f64 {
return math.exp(-x);
}
fn exp_f_inv(y: f64) f64 {
return -math.ln(y);
}
fn exp_zero_case(random: &Random, _: f64) f64 {
return exp_r - math.ln(random.float(f64));
}
test "ziggurant exp dist sanity" {
var prng = std.rand.DefaultPrng.init(0);

View File

@ -5,7 +5,7 @@ const Allocator = std.mem.Allocator;
// Imagine that `fn at(self: &Self, index: usize) &T` is a customer asking for a box
// from a warehouse, based on a flat array, boxes ordered from 0 to N - 1.
// But the warehouse actually stores boxes in shelves of increasing powers of 2 sizes.
// So when the customer requests a box index, we have to translate it to shelf index
// So when the customer requests a box index, we have to translate it to shelf index
// and box index within that shelf. Illustration:
//
// customer indexes:
@ -37,14 +37,14 @@ const Allocator = std.mem.Allocator;
// Now we complicate it a little bit further by adding a preallocated shelf, which must be
// a power of 2:
// prealloc=4
//
//
// customer indexes:
// prealloc: 0 1 2 3
// shelf 0: 4 5 6 7 8 9 10 11
// shelf 1: 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// shelf 2: 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
// ...
//
//
// warehouse indexes:
// prealloc: 0 1 2 3
// shelf 0: 0 1 2 3 4 5 6 7

View File

@ -317,7 +317,6 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons
// 6. merge each A block with any B values that follow, using the cache or the second internal buffer
// 7. sort the second internal buffer if it exists
// 8. redistribute the two internal buffers back into the items
var block_size: usize = math.sqrt(iterator.length());
var buffer_size = iterator.length() / block_size + 1;

View File

@ -27,10 +27,14 @@ extern fn zen_start() noreturn {
nakedcc fn _start() noreturn {
switch (builtin.arch) {
builtin.Arch.x86_64 => {
argc_ptr = asm ("lea (%%rsp), %[argc]" : [argc] "=r" (-> &usize));
argc_ptr = asm ("lea (%%rsp), %[argc]"
: [argc] "=r" (-> &usize)
);
},
builtin.Arch.i386 => {
argc_ptr = asm ("lea (%%esp), %[argc]" : [argc] "=r" (-> &usize));
argc_ptr = asm ("lea (%%esp), %[argc]"
: [argc] "=r" (-> &usize)
);
},
else => @compileError("unsupported arch"),
}

View File

@ -7,8 +7,10 @@ comptime {
@export("_DllMainCRTStartup", _DllMainCRTStartup, builtin.GlobalLinkage.Strong);
}
stdcallcc fn _DllMainCRTStartup(hinstDLL: std.os.windows.HINSTANCE, fdwReason: std.os.windows.DWORD,
lpReserved: std.os.windows.LPVOID) std.os.windows.BOOL
{
stdcallcc fn _DllMainCRTStartup(
hinstDLL: std.os.windows.HINSTANCE,
fdwReason: std.os.windows.DWORD,
lpReserved: std.os.windows.LPVOID,
) std.os.windows.BOOL {
return std.os.windows.TRUE;
}

View File

@ -24,7 +24,6 @@ pub fn main() !void {
const allocator = &arena.allocator;
// skip my own exe name
_ = arg_it.skip();
@ -175,8 +174,7 @@ fn usage(builder: &Builder, already_ran_build: bool, out_stream: var) !void {
try out_stream.print(" (none)\n");
} else {
for (builder.available_options_list.toSliceConst()) |option| {
const name = try fmt.allocPrint(allocator,
" -D{}=[{}]", option.name, Builder.typeIdName(option.type_id));
const name = try fmt.allocPrint(allocator, " -D{}=[{}]", option.name, Builder.typeIdName(option.type_id));
defer allocator.free(name);
try out_stream.print("{s24} {}\n", name, option.description);
}
@ -202,7 +200,7 @@ fn usageAndErr(builder: &Builder, already_ran_build: bool, out_stream: var) erro
return error.InvalidArgs;
}
const UnwrapArgError = error {OutOfMemory};
const UnwrapArgError = error{OutOfMemory};
fn unwrapArg(arg: UnwrapArgError![]u8) UnwrapArgError![]u8 {
return arg catch |err| {

View File

@ -56,7 +56,8 @@ export fn memmove(dest: ?&u8, src: ?&const u8, n: usize) ?&u8 {
comptime {
if (builtin.mode != builtin.Mode.ReleaseFast and
builtin.mode != builtin.Mode.ReleaseSmall and
builtin.os != builtin.Os.windows) {
builtin.os != builtin.Os.windows)
{
@export("__stack_chk_fail", __stack_chk_fail, builtin.GlobalLinkage.Strong);
}
if (builtin.os == builtin.Os.linux and builtin.arch == builtin.Arch.x86_64) {
@ -101,15 +102,27 @@ nakedcc fn clone() void {
const math = @import("../math/index.zig");
export fn fmodf(x: f32, y: f32) f32 { return generic_fmod(f32, x, y); }
export fn fmod(x: f64, y: f64) f64 { return generic_fmod(f64, x, y); }
export fn fmodf(x: f32, y: f32) f32 {
return generic_fmod(f32, x, y);
}
export fn fmod(x: f64, y: f64) f64 {
return generic_fmod(f64, x, y);
}
// TODO add intrinsics for these (and probably the double version too)
// and have the math stuff use the intrinsic. same as @mod and @rem
export fn floorf(x: f32) f32 { return math.floor(x); }
export fn ceilf(x: f32) f32 { return math.ceil(x); }
export fn floor(x: f64) f64 { return math.floor(x); }
export fn ceil(x: f64) f64 { return math.ceil(x); }
export fn floorf(x: f32) f32 {
return math.floor(x);
}
export fn ceilf(x: f32) f32 {
return math.ceil(x);
}
export fn floor(x: f64) f64 {
return math.floor(x);
}
export fn ceil(x: f64) f64 {
return math.ceil(x);
}
fn generic_fmod(comptime T: type, x: T, y: T) T {
@setRuntimeSafety(false);
@ -139,7 +152,10 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
// normalize x and y
if (ex == 0) {
i = ux << exp_bits;
while (i >> bits_minus_1 == 0) : (b: {ex -= 1; break :b i <<= 1;}) {}
while (i >> bits_minus_1 == 0) : (b: {
ex -= 1;
i <<= 1;
}) {}
ux <<= log2uint(@bitCast(u32, -ex + 1));
} else {
ux &= @maxValue(uint) >> exp_bits;
@ -147,7 +163,10 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
}
if (ey == 0) {
i = uy << exp_bits;
while (i >> bits_minus_1 == 0) : (b: {ey -= 1; break :b i <<= 1;}) {}
while (i >> bits_minus_1 == 0) : (b: {
ey -= 1;
i <<= 1;
}) {}
uy <<= log2uint(@bitCast(u32, -ey + 1));
} else {
uy &= @maxValue(uint) >> exp_bits;
@ -170,7 +189,10 @@ fn generic_fmod(comptime T: type, x: T, y: T) T {
return 0 * x;
ux = i;
}
while (ux >> digits == 0) : (b: {ux <<= 1; break :b ex -= 1;}) {}
while (ux >> digits == 0) : (b: {
ux <<= 1;
ex -= 1;
}) {}
// scale result up
if (ex > 0) {
@ -298,7 +320,7 @@ export fn sqrt(x: f64) f64 {
// rounding direction
if (ix0 | ix1 != 0) {
var z = 1.0 - tiny; // raise inexact
var z = 1.0 - tiny; // raise inexact
if (z >= 1.0) {
z = 1.0 + tiny;
if (q1 == 0xFFFFFFFF) {
@ -336,13 +358,13 @@ export fn sqrtf(x: f32) f32 {
var ix: i32 = @bitCast(i32, x);
if ((ix & 0x7F800000) == 0x7F800000) {
return x * x + x; // sqrt(nan) = nan, sqrt(+inf) = +inf, sqrt(-inf) = snan
return x * x + x; // sqrt(nan) = nan, sqrt(+inf) = +inf, sqrt(-inf) = snan
}
// zero
if (ix <= 0) {
if (ix & ~sign == 0) {
return x; // sqrt (+-0) = +-0
return x; // sqrt (+-0) = +-0
}
if (ix < 0) {
return math.snan(f32);
@ -360,20 +382,20 @@ export fn sqrtf(x: f32) f32 {
m -= i - 1;
}
m -= 127; // unbias exponent
m -= 127; // unbias exponent
ix = (ix & 0x007FFFFF) | 0x00800000;
if (m & 1 != 0) { // odd m, double x to even
if (m & 1 != 0) { // odd m, double x to even
ix += ix;
}
m >>= 1; // m = [m / 2]
m >>= 1; // m = [m / 2]
// sqrt(x) bit by bit
ix += ix;
var q: i32 = 0; // q = sqrt(x)
var q: i32 = 0; // q = sqrt(x)
var s: i32 = 0;
var r: i32 = 0x01000000; // r = moving bit right -> left
var r: i32 = 0x01000000; // r = moving bit right -> left
while (r != 0) {
const t = s + r;
@ -388,7 +410,7 @@ export fn sqrtf(x: f32) f32 {
// floating add to find rounding direction
if (ix != 0) {
var z = 1.0 - tiny; // inexact
var z = 1.0 - tiny; // inexact
if (z >= 1.0) {
z = 1.0 + tiny;
if (z > 1.0) {

View File

@ -38,25 +38,22 @@ pub extern fn __letf2(a: f128, b: f128) c_int {
// If at least one of a and b is positive, we get the same result comparing
// a and b as signed integers as we would with a floating-point compare.
return if ((aInt & bInt) >= 0)
if (aInt < bInt)
LE_LESS
else if (aInt == bInt)
LE_EQUAL
else
LE_GREATER
return if ((aInt & bInt) >= 0) if (aInt < bInt)
LE_LESS
else if (aInt == bInt)
LE_EQUAL
else
// Otherwise, both are negative, so we need to flip the sense of the
// comparison to get the correct result. (This assumes a twos- or ones-
// complement integer representation; if integers are represented in a
// sign-magnitude representation, then this flip is incorrect).
if (aInt > bInt)
LE_LESS
else if (aInt == bInt)
LE_EQUAL
else
LE_GREATER
;
LE_GREATER else
// Otherwise, both are negative, so we need to flip the sense of the
// comparison to get the correct result. (This assumes a twos- or ones-
// complement integer representation; if integers are represented in a
// sign-magnitude representation, then this flip is incorrect).
if (aInt > bInt)
LE_LESS
else if (aInt == bInt)
LE_EQUAL
else
LE_GREATER;
}
// TODO https://github.com/ziglang/zig/issues/305
@ -76,21 +73,17 @@ pub extern fn __getf2(a: f128, b: f128) c_int {
if (aAbs > infRep or bAbs > infRep) return GE_UNORDERED;
if ((aAbs | bAbs) == 0) return GE_EQUAL;
return if ((aInt & bInt) >= 0)
if (aInt < bInt)
GE_LESS
else if (aInt == bInt)
GE_EQUAL
else
GE_GREATER
return if ((aInt & bInt) >= 0) if (aInt < bInt)
GE_LESS
else if (aInt == bInt)
GE_EQUAL
else
if (aInt > bInt)
GE_LESS
else if (aInt == bInt)
GE_EQUAL
else
GE_GREATER
;
GE_GREATER else if (aInt > bInt)
GE_LESS
else if (aInt == bInt)
GE_EQUAL
else
GE_GREATER;
}
pub extern fn __unordtf2(a: f128, b: f128) c_int {

View File

@ -44,4 +44,3 @@ test "fixunsdfti" {
test__fixunsdfti(-0x1.FFFFFFFFFFFFFp+62, 0);
test__fixunsdfti(-0x1.FFFFFFFFFFFFEp+62, 0);
}

View File

@ -92,10 +92,10 @@ pub fn setXmm0(comptime T: type, value: T) void {
const aligned_value: T align(16) = value;
asm volatile (
\\movaps (%[ptr]), %%xmm0
:
:
: [ptr] "r" (&aligned_value)
: "xmm0");
: "xmm0"
);
}
extern fn __udivdi3(a: u64, b: u64) u64 {
@ -159,7 +159,8 @@ fn isArmArch() bool {
builtin.Arch.armebv6t2,
builtin.Arch.armebv5,
builtin.Arch.armebv5te,
builtin.Arch.armebv4t => true,
builtin.Arch.armebv4t,
=> true,
else => false,
};
}
@ -174,7 +175,10 @@ nakedcc fn __aeabi_uidivmod() void {
\\ ldr r1, [sp]
\\ add sp, sp, #4
\\ pop { pc }
::: "r2", "r1");
:
:
: "r2", "r1"
);
}
// _chkstk (_alloca) routine - probe stack between %esp and (%esp-%eax) in 4k increments,

View File

@ -58,6 +58,7 @@ pub fn utf8Encode(c: u32, out: []u8) !u3 {
}
const Utf8DecodeError = Utf8Decode2Error || Utf8Decode3Error || Utf8Decode4Error;
/// Decodes the UTF-8 codepoint encoded in the given slice of bytes.
/// bytes.len must be equal to utf8ByteSequenceLength(bytes[0]) catch unreachable.
/// If you already know the length at comptime, you can call one of
@ -150,7 +151,9 @@ pub fn utf8ValidateSlice(s: []const u8) bool {
return false;
}
if (utf8Decode(s[i..i+cp_len])) |_| {} else |_| { return false; }
if (utf8Decode(s[i..i + cp_len])) |_| {} else |_| {
return false;
}
i += cp_len;
} else |err| {
return false;
@ -179,9 +182,7 @@ pub const Utf8View = struct {
}
pub fn initUnchecked(s: []const u8) Utf8View {
return Utf8View {
.bytes = s,
};
return Utf8View{ .bytes = s };
}
pub fn initComptime(comptime s: []const u8) Utf8View {
@ -191,12 +192,12 @@ pub const Utf8View = struct {
error.InvalidUtf8 => {
@compileError("invalid utf8");
unreachable;
}
},
}
}
pub fn iterator(s: &const Utf8View) Utf8Iterator {
return Utf8Iterator {
return Utf8Iterator{
.bytes = s.bytes,
.i = 0,
};
@ -215,7 +216,7 @@ const Utf8Iterator = struct {
const cp_len = utf8ByteSequenceLength(it.bytes[it.i]) catch unreachable;
it.i += cp_len;
return it.bytes[it.i-cp_len..it.i];
return it.bytes[it.i - cp_len..it.i];
}
pub fn nextCodepoint(it: &Utf8Iterator) ?u32 {
@ -304,9 +305,12 @@ test "utf8 view bad" {
fn testUtf8ViewBad() void {
// Compile-time error.
// const s3 = Utf8View.initComptime("\xfe\xf2");
const s = Utf8View.init("hel\xadlo");
if (s) |_| { unreachable; } else |err| { debug.assert(err == error.InvalidUtf8); }
if (s) |_| {
unreachable;
} else |err| {
debug.assert(err == error.InvalidUtf8);
}
}
test "utf8 view ok" {

View File

@ -388,7 +388,8 @@ pub const Node = struct {
Id.SwitchElse,
Id.FieldInitializer,
Id.DocComment,
Id.TestDecl => return false,
Id.TestDecl,
=> return false,
Id.While => {
const while_node = @fieldParentPtr(While, "base", n);
if (while_node.@"else") |@"else"| {
@ -608,8 +609,7 @@ pub const Node = struct {
if (i < 1) return t;
i -= 1;
},
InitArg.None,
InitArg.Enum => {},
InitArg.None, InitArg.Enum => {},
}
if (i < self.fields_and_decls.len) return self.fields_and_decls.at(i).*;
@ -1475,7 +1475,8 @@ pub const Node = struct {
Op.Range,
Op.Sub,
Op.SubWrap,
Op.UnwrapMaybe => {},
Op.UnwrapMaybe,
=> {},
}
if (i < 1) return self.rhs;

File diff suppressed because it is too large Load Diff

View File

@ -1021,7 +1021,7 @@ test "zig fmt: extern declaration" {
}
test "zig fmt: alignment" {
try testCanonical(
try testCanonical(
\\var foo: c_int align(1);
\\
);
@ -1070,7 +1070,7 @@ test "zig fmt: slice attributes" {
}
test "zig fmt: test declaration" {
try testCanonical(
try testCanonical(
\\test "test name" {
\\ const a = 1;
\\ var b = 1;
@ -1312,7 +1312,7 @@ test "zig fmt: struct declaration" {
}
test "zig fmt: enum declaration" {
try testCanonical(
try testCanonical(
\\const E = enum {
\\ Ok,
\\ SomethingElse = 0,
@ -1340,7 +1340,7 @@ test "zig fmt: enum declaration" {
}
test "zig fmt: union declaration" {
try testCanonical(
try testCanonical(
\\const U = union {
\\ Int: u8,
\\ Float: f32,
@ -1860,10 +1860,15 @@ fn testTransform(source: []const u8, expected_source: []const u8) !void {
} else |err| switch (err) {
error.OutOfMemory => {
if (failing_allocator.allocated_bytes != failing_allocator.freed_bytes) {
warn("\nfail_index: {}/{}\nallocated bytes: {}\nfreed bytes: {}\nallocations: {}\ndeallocations: {}\n",
fail_index, needed_alloc_count,
failing_allocator.allocated_bytes, failing_allocator.freed_bytes,
failing_allocator.index, failing_allocator.deallocations);
warn(
"\nfail_index: {}/{}\nallocated bytes: {}\nfreed bytes: {}\nallocations: {}\ndeallocations: {}\n",
fail_index,
needed_alloc_count,
failing_allocator.allocated_bytes,
failing_allocator.freed_bytes,
failing_allocator.index,
failing_allocator.deallocations,
);
return error.MemoryLeakDetected;
}
},
@ -1876,4 +1881,3 @@ fn testTransform(source: []const u8, expected_source: []const u8) !void {
fn testCanonical(source: []const u8) !void {
return testTransform(source, source);
}

View File

@ -161,7 +161,15 @@ fn renderTopLevelDecl(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, i
}
}
fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, indent: usize, start_col: &usize, base: &ast.Node, space: Space,) (@typeOf(stream).Child.Error || Error)!void {
fn renderExpression(
allocator: &mem.Allocator,
stream: var,
tree: &ast.Tree,
indent: usize,
start_col: &usize,
base: &ast.Node,
space: Space,
) (@typeOf(stream).Child.Error || Error)!void {
switch (base.id) {
ast.Node.Id.Identifier => {
const identifier = @fieldParentPtr(ast.Node.Identifier, "base", base);
@ -259,8 +267,7 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
try renderExpression(allocator, stream, tree, indent, start_col, infix_op_node.lhs, op_space);
const after_op_space = blk: {
const loc = tree.tokenLocation(tree.tokens.at(infix_op_node.op_token).end,
tree.nextToken(infix_op_node.op_token));
const loc = tree.tokenLocation(tree.tokens.at(infix_op_node.op_token).end, tree.nextToken(infix_op_node.op_token));
break :blk if (loc.line == 0) op_space else Space.Newline;
};
@ -367,14 +374,16 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
ast.Node.PrefixOp.Op.NegationWrap,
ast.Node.PrefixOp.Op.UnwrapMaybe,
ast.Node.PrefixOp.Op.MaybeType,
ast.Node.PrefixOp.Op.PointerType => {
ast.Node.PrefixOp.Op.PointerType,
=> {
try renderToken(tree, stream, prefix_op_node.op_token, indent, start_col, Space.None);
},
ast.Node.PrefixOp.Op.Try,
ast.Node.PrefixOp.Op.Await,
ast.Node.PrefixOp.Op.Cancel,
ast.Node.PrefixOp.Op.Resume => {
ast.Node.PrefixOp.Op.Resume,
=> {
try renderToken(tree, stream, prefix_op_node.op_token, indent, start_col, Space.Space);
},
}
@ -1568,13 +1577,19 @@ fn renderExpression(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, ind
ast.Node.Id.VarDecl,
ast.Node.Id.Use,
ast.Node.Id.TestDecl,
ast.Node.Id.ParamDecl => unreachable,
ast.Node.Id.ParamDecl,
=> unreachable,
}
}
fn renderVarDecl(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, indent: usize, start_col: &usize,
var_decl: &ast.Node.VarDecl,) (@typeOf(stream).Child.Error || Error)!void
{
fn renderVarDecl(
allocator: &mem.Allocator,
stream: var,
tree: &ast.Tree,
indent: usize,
start_col: &usize,
var_decl: &ast.Node.VarDecl,
) (@typeOf(stream).Child.Error || Error)!void {
if (var_decl.visib_token) |visib_token| {
try renderToken(tree, stream, visib_token, indent, start_col, Space.Space); // pub
}
@ -1623,7 +1638,15 @@ fn renderVarDecl(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, indent
try renderToken(tree, stream, var_decl.semicolon_token, indent, start_col, Space.Newline);
}
fn renderParamDecl(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, indent: usize, start_col: &usize, base: &ast.Node, space: Space,) (@typeOf(stream).Child.Error || Error)!void {
fn renderParamDecl(
allocator: &mem.Allocator,
stream: var,
tree: &ast.Tree,
indent: usize,
start_col: &usize,
base: &ast.Node,
space: Space,
) (@typeOf(stream).Child.Error || Error)!void {
const param_decl = @fieldParentPtr(ast.Node.ParamDecl, "base", base);
if (param_decl.comptime_token) |comptime_token| {
@ -1643,7 +1666,14 @@ fn renderParamDecl(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, inde
}
}
fn renderStatement(allocator: &mem.Allocator, stream: var, tree: &ast.Tree, indent: usize, start_col: &usize, base: &ast.Node,) (@typeOf(stream).Child.Error || Error)!void {
fn renderStatement(
allocator: &mem.Allocator,
stream: var,
tree: &ast.Tree,
indent: usize,
start_col: &usize,
base: &ast.Node,
) (@typeOf(stream).Child.Error || Error)!void {
switch (base.id) {
ast.Node.Id.VarDecl => {
const var_decl = @fieldParentPtr(ast.Node.VarDecl, "base", base);
@ -1840,7 +1870,13 @@ fn renderToken(tree: &ast.Tree, stream: var, token_index: ast.TokenIndex, indent
}
}
fn renderDocComments(tree: &ast.Tree, stream: var, node: var, indent: usize, start_col: &usize,) (@typeOf(stream).Child.Error || Error)!void {
fn renderDocComments(
tree: &ast.Tree,
stream: var,
node: var,
indent: usize,
start_col: &usize,
) (@typeOf(stream).Child.Error || Error)!void {
const comment = node.doc_comments ?? return;
var it = comment.lines.iterator(0);
const first_token = node.firstToken();

View File

@ -11,55 +11,55 @@ pub const Token = struct {
id: Id,
};
pub const keywords = []Keyword {
Keyword{.bytes="align", .id = Id.Keyword_align},
Keyword{.bytes="and", .id = Id.Keyword_and},
Keyword{.bytes="asm", .id = Id.Keyword_asm},
Keyword{.bytes="async", .id = Id.Keyword_async},
Keyword{.bytes="await", .id = Id.Keyword_await},
Keyword{.bytes="break", .id = Id.Keyword_break},
Keyword{.bytes="catch", .id = Id.Keyword_catch},
Keyword{.bytes="cancel", .id = Id.Keyword_cancel},
Keyword{.bytes="comptime", .id = Id.Keyword_comptime},
Keyword{.bytes="const", .id = Id.Keyword_const},
Keyword{.bytes="continue", .id = Id.Keyword_continue},
Keyword{.bytes="defer", .id = Id.Keyword_defer},
Keyword{.bytes="else", .id = Id.Keyword_else},
Keyword{.bytes="enum", .id = Id.Keyword_enum},
Keyword{.bytes="errdefer", .id = Id.Keyword_errdefer},
Keyword{.bytes="error", .id = Id.Keyword_error},
Keyword{.bytes="export", .id = Id.Keyword_export},
Keyword{.bytes="extern", .id = Id.Keyword_extern},
Keyword{.bytes="false", .id = Id.Keyword_false},
Keyword{.bytes="fn", .id = Id.Keyword_fn},
Keyword{.bytes="for", .id = Id.Keyword_for},
Keyword{.bytes="if", .id = Id.Keyword_if},
Keyword{.bytes="inline", .id = Id.Keyword_inline},
Keyword{.bytes="nakedcc", .id = Id.Keyword_nakedcc},
Keyword{.bytes="noalias", .id = Id.Keyword_noalias},
Keyword{.bytes="null", .id = Id.Keyword_null},
Keyword{.bytes="or", .id = Id.Keyword_or},
Keyword{.bytes="packed", .id = Id.Keyword_packed},
Keyword{.bytes="promise", .id = Id.Keyword_promise},
Keyword{.bytes="pub", .id = Id.Keyword_pub},
Keyword{.bytes="resume", .id = Id.Keyword_resume},
Keyword{.bytes="return", .id = Id.Keyword_return},
Keyword{.bytes="section", .id = Id.Keyword_section},
Keyword{.bytes="stdcallcc", .id = Id.Keyword_stdcallcc},
Keyword{.bytes="struct", .id = Id.Keyword_struct},
Keyword{.bytes="suspend", .id = Id.Keyword_suspend},
Keyword{.bytes="switch", .id = Id.Keyword_switch},
Keyword{.bytes="test", .id = Id.Keyword_test},
Keyword{.bytes="this", .id = Id.Keyword_this},
Keyword{.bytes="true", .id = Id.Keyword_true},
Keyword{.bytes="try", .id = Id.Keyword_try},
Keyword{.bytes="undefined", .id = Id.Keyword_undefined},
Keyword{.bytes="union", .id = Id.Keyword_union},
Keyword{.bytes="unreachable", .id = Id.Keyword_unreachable},
Keyword{.bytes="use", .id = Id.Keyword_use},
Keyword{.bytes="var", .id = Id.Keyword_var},
Keyword{.bytes="volatile", .id = Id.Keyword_volatile},
Keyword{.bytes="while", .id = Id.Keyword_while},
pub const keywords = []Keyword{
Keyword{ .bytes = "align", .id = Id.Keyword_align },
Keyword{ .bytes = "and", .id = Id.Keyword_and },
Keyword{ .bytes = "asm", .id = Id.Keyword_asm },
Keyword{ .bytes = "async", .id = Id.Keyword_async },
Keyword{ .bytes = "await", .id = Id.Keyword_await },
Keyword{ .bytes = "break", .id = Id.Keyword_break },
Keyword{ .bytes = "catch", .id = Id.Keyword_catch },
Keyword{ .bytes = "cancel", .id = Id.Keyword_cancel },
Keyword{ .bytes = "comptime", .id = Id.Keyword_comptime },
Keyword{ .bytes = "const", .id = Id.Keyword_const },
Keyword{ .bytes = "continue", .id = Id.Keyword_continue },
Keyword{ .bytes = "defer", .id = Id.Keyword_defer },
Keyword{ .bytes = "else", .id = Id.Keyword_else },
Keyword{ .bytes = "enum", .id = Id.Keyword_enum },
Keyword{ .bytes = "errdefer", .id = Id.Keyword_errdefer },
Keyword{ .bytes = "error", .id = Id.Keyword_error },
Keyword{ .bytes = "export", .id = Id.Keyword_export },
Keyword{ .bytes = "extern", .id = Id.Keyword_extern },
Keyword{ .bytes = "false", .id = Id.Keyword_false },
Keyword{ .bytes = "fn", .id = Id.Keyword_fn },
Keyword{ .bytes = "for", .id = Id.Keyword_for },
Keyword{ .bytes = "if", .id = Id.Keyword_if },
Keyword{ .bytes = "inline", .id = Id.Keyword_inline },
Keyword{ .bytes = "nakedcc", .id = Id.Keyword_nakedcc },
Keyword{ .bytes = "noalias", .id = Id.Keyword_noalias },
Keyword{ .bytes = "null", .id = Id.Keyword_null },
Keyword{ .bytes = "or", .id = Id.Keyword_or },
Keyword{ .bytes = "packed", .id = Id.Keyword_packed },
Keyword{ .bytes = "promise", .id = Id.Keyword_promise },
Keyword{ .bytes = "pub", .id = Id.Keyword_pub },
Keyword{ .bytes = "resume", .id = Id.Keyword_resume },
Keyword{ .bytes = "return", .id = Id.Keyword_return },
Keyword{ .bytes = "section", .id = Id.Keyword_section },
Keyword{ .bytes = "stdcallcc", .id = Id.Keyword_stdcallcc },
Keyword{ .bytes = "struct", .id = Id.Keyword_struct },
Keyword{ .bytes = "suspend", .id = Id.Keyword_suspend },
Keyword{ .bytes = "switch", .id = Id.Keyword_switch },
Keyword{ .bytes = "test", .id = Id.Keyword_test },
Keyword{ .bytes = "this", .id = Id.Keyword_this },
Keyword{ .bytes = "true", .id = Id.Keyword_true },
Keyword{ .bytes = "try", .id = Id.Keyword_try },
Keyword{ .bytes = "undefined", .id = Id.Keyword_undefined },
Keyword{ .bytes = "union", .id = Id.Keyword_union },
Keyword{ .bytes = "unreachable", .id = Id.Keyword_unreachable },
Keyword{ .bytes = "use", .id = Id.Keyword_use },
Keyword{ .bytes = "var", .id = Id.Keyword_var },
Keyword{ .bytes = "volatile", .id = Id.Keyword_volatile },
Keyword{ .bytes = "while", .id = Id.Keyword_while },
};
// TODO perfect hash at comptime
@ -72,7 +72,10 @@ pub const Token = struct {
return null;
}
const StrLitKind = enum {Normal, C};
const StrLitKind = enum {
Normal,
C,
};
pub const Id = union(enum) {
Invalid,
@ -202,7 +205,7 @@ pub const Tokenizer = struct {
}
pub fn init(buffer: []const u8) Tokenizer {
return Tokenizer {
return Tokenizer{
.buffer = buffer,
.index = 0,
.pending_invalid_token = null,
@ -269,7 +272,7 @@ pub const Tokenizer = struct {
}
const start_index = self.index;
var state = State.Start;
var result = Token {
var result = Token{
.id = Token.Id.Eof,
.start = self.index,
.end = undefined,
@ -290,7 +293,7 @@ pub const Tokenizer = struct {
},
'"' => {
state = State.StringLiteral;
result.id = Token.Id { .StringLiteral = Token.StrLitKind.Normal };
result.id = Token.Id{ .StringLiteral = Token.StrLitKind.Normal };
},
'\'' => {
state = State.CharLiteral;
@ -369,7 +372,7 @@ pub const Tokenizer = struct {
},
'\\' => {
state = State.Backslash;
result.id = Token.Id { .MultilineStringLiteralLine = Token.StrLitKind.Normal };
result.id = Token.Id{ .MultilineStringLiteralLine = Token.StrLitKind.Normal };
},
'{' => {
result.id = Token.Id.LBrace;
@ -455,7 +458,7 @@ pub const Tokenizer = struct {
else => {
result.id = Token.Id.Asterisk;
break;
}
},
},
State.AsteriskPercent => switch (c) {
@ -467,7 +470,7 @@ pub const Tokenizer = struct {
else => {
result.id = Token.Id.AsteriskPercent;
break;
}
},
},
State.QuestionMark => switch (c) {
@ -535,7 +538,7 @@ pub const Tokenizer = struct {
else => {
result.id = Token.Id.Caret;
break;
}
},
},
State.Identifier => switch (c) {
@ -560,11 +563,11 @@ pub const Tokenizer = struct {
State.C => switch (c) {
'\\' => {
state = State.Backslash;
result.id = Token.Id { .MultilineStringLiteralLine = Token.StrLitKind.C };
result.id = Token.Id{ .MultilineStringLiteralLine = Token.StrLitKind.C };
},
'"' => {
state = State.StringLiteral;
result.id = Token.Id { .StringLiteral = Token.StrLitKind.C };
result.id = Token.Id{ .StringLiteral = Token.StrLitKind.C };
},
'a'...'z', 'A'...'Z', '_', '0'...'9' => {
state = State.Identifier;
@ -605,7 +608,7 @@ pub const Tokenizer = struct {
}
state = State.CharLiteralEnd;
}
},
},
State.CharLiteralBackslash => switch (c) {
@ -736,7 +739,7 @@ pub const Tokenizer = struct {
else => {
result.id = Token.Id.MinusPercent;
break;
}
},
},
State.AngleBracketLeft => switch (c) {
@ -944,7 +947,7 @@ pub const Tokenizer = struct {
// reinterpret as a normal exponent number
self.index -= 1;
state = State.FloatExponentNumber;
}
},
},
State.FloatExponentUnsignedHex => switch (c) {
'+', '-' => {
@ -954,7 +957,7 @@ pub const Tokenizer = struct {
// reinterpret as a normal exponent number
self.index -= 1;
state = State.FloatExponentNumberHex;
}
},
},
State.FloatExponentNumber => switch (c) {
'0'...'9' => {},
@ -978,15 +981,15 @@ pub const Tokenizer = struct {
State.FloatExponentNumberHex,
State.StringLiteral, // find this error later
State.MultilineStringLiteralLine,
State.Builtin => {},
State.Builtin,
=> {},
State.Identifier => {
if (Token.getKeyword(self.buffer[result.start..self.index])) |id| {
result.id = id;
}
},
State.LineCommentStart,
State.LineComment => {
State.LineCommentStart, State.LineComment => {
result.id = Token.Id.LineComment;
},
State.DocComment, State.DocCommentStart => {
@ -1004,7 +1007,8 @@ pub const Tokenizer = struct {
State.CharLiteralEscape1,
State.CharLiteralEscape2,
State.CharLiteralEnd,
State.StringLiteralBackslash => {
State.StringLiteralBackslash,
=> {
result.id = Token.Id.Invalid;
},
@ -1089,7 +1093,7 @@ pub const Tokenizer = struct {
if (self.pending_invalid_token != null) return;
const invalid_length = self.getInvalidCharacterLength();
if (invalid_length == 0) return;
self.pending_invalid_token = Token {
self.pending_invalid_token = Token{
.id = Token.Id.Invalid,
.start = self.index,
.end = self.index + invalid_length,
@ -1134,23 +1138,18 @@ pub const Tokenizer = struct {
}
};
test "tokenizer" {
testTokenize("test", []Token.Id {
Token.Id.Keyword_test,
});
testTokenize("test", []Token.Id{Token.Id.Keyword_test});
}
test "tokenizer - char literal with hex escape" {
testTokenize( \\'\x1b'
, []Token.Id {
Token.Id.CharLiteral,
});
testTokenize(
\\'\x1b'
, []Token.Id{Token.Id.CharLiteral});
}
test "tokenizer - float literal e exponent" {
testTokenize("a = 4.94065645841246544177e-324;\n", []Token.Id {
testTokenize("a = 4.94065645841246544177e-324;\n", []Token.Id{
Token.Id.Identifier,
Token.Id.Equal,
Token.Id.FloatLiteral,
@ -1159,7 +1158,7 @@ test "tokenizer - float literal e exponent" {
}
test "tokenizer - float literal p exponent" {
testTokenize("a = 0x1.a827999fcef32p+1022;\n", []Token.Id {
testTokenize("a = 0x1.a827999fcef32p+1022;\n", []Token.Id{
Token.Id.Identifier,
Token.Id.Equal,
Token.Id.FloatLiteral,
@ -1168,31 +1167,31 @@ test "tokenizer - float literal p exponent" {
}
test "tokenizer - chars" {
testTokenize("'c'", []Token.Id {Token.Id.CharLiteral});
testTokenize("'c'", []Token.Id{Token.Id.CharLiteral});
}
test "tokenizer - invalid token characters" {
testTokenize("#", []Token.Id{Token.Id.Invalid});
testTokenize("`", []Token.Id{Token.Id.Invalid});
testTokenize("'c", []Token.Id {Token.Id.Invalid});
testTokenize("'", []Token.Id {Token.Id.Invalid});
testTokenize("''", []Token.Id {Token.Id.Invalid, Token.Id.Invalid});
testTokenize("'c", []Token.Id{Token.Id.Invalid});
testTokenize("'", []Token.Id{Token.Id.Invalid});
testTokenize("''", []Token.Id{ Token.Id.Invalid, Token.Id.Invalid });
}
test "tokenizer - invalid literal/comment characters" {
testTokenize("\"\x00\"", []Token.Id {
Token.Id { .StringLiteral = Token.StrLitKind.Normal },
testTokenize("\"\x00\"", []Token.Id{
Token.Id{ .StringLiteral = Token.StrLitKind.Normal },
Token.Id.Invalid,
});
testTokenize("//\x00", []Token.Id {
testTokenize("//\x00", []Token.Id{
Token.Id.LineComment,
Token.Id.Invalid,
});
testTokenize("//\x1f", []Token.Id {
testTokenize("//\x1f", []Token.Id{
Token.Id.LineComment,
Token.Id.Invalid,
});
testTokenize("//\x7f", []Token.Id {
testTokenize("//\x7f", []Token.Id{
Token.Id.LineComment,
Token.Id.Invalid,
});
@ -1261,18 +1260,16 @@ test "tokenizer - illegal unicode codepoints" {
test "tokenizer - string identifier and builtin fns" {
testTokenize(
\\const @"if" = @import("std");
,
[]Token.Id{
Token.Id.Keyword_const,
Token.Id.Identifier,
Token.Id.Equal,
Token.Id.Builtin,
Token.Id.LParen,
Token.Id {.StringLiteral = Token.StrLitKind.Normal},
Token.Id.RParen,
Token.Id.Semicolon,
}
);
, []Token.Id{
Token.Id.Keyword_const,
Token.Id.Identifier,
Token.Id.Equal,
Token.Id.Builtin,
Token.Id.LParen,
Token.Id{ .StringLiteral = Token.StrLitKind.Normal },
Token.Id.RParen,
Token.Id.Semicolon,
});
}
test "tokenizer - pipe and then invalid" {
@ -1314,7 +1311,10 @@ fn testTokenize(source: []const u8, expected_tokens: []const Token.Id) void {
}
switch (expected_token_id) {
Token.Id.StringLiteral => |expected_kind| {
std.debug.assert(expected_kind == switch (token.id) { Token.Id.StringLiteral => |kind| kind, else => unreachable });
std.debug.assert(expected_kind == switch (token.id) {
Token.Id.StringLiteral => |kind| kind,
else => unreachable,
});
},
else => {},
}

View File

@ -70,7 +70,7 @@ test "specifying alignment allows pointer cast" {
testBytesAlign(0x33);
}
fn testBytesAlign(b: u8) void {
var bytes align(4) = []u8 {
var bytes align(4) = []u8{
b,
b,
b,
@ -84,7 +84,7 @@ test "specifying alignment allows slice cast" {
testBytesAlignSlice(0x33);
}
fn testBytesAlignSlice(b: u8) void {
var bytes align(4) = []u8 {
var bytes align(4) = []u8{
b,
b,
b,
@ -107,7 +107,7 @@ fn expects4(x: &align(4) u32) void {
}
test "@alignCast slices" {
var array align(4) = []u32 {
var array align(4) = []u32{
1,
1,
};
@ -169,7 +169,7 @@ test "@ptrCast preserves alignment of bigger source" {
test "compile-time known array index has best alignment possible" {
// take full advantage of over-alignment
var array align(4) = []u8 {
var array align(4) = []u8{
1,
2,
3,
@ -181,7 +181,7 @@ test "compile-time known array index has best alignment possible" {
assert(@typeOf(&array[3]) == &u8);
// because align is too small but we still figure out to use 2
var bigger align(2) = []u64 {
var bigger align(2) = []u64{
1,
2,
3,
@ -193,7 +193,7 @@ test "compile-time known array index has best alignment possible" {
assert(@typeOf(&bigger[3]) == &align(2) u64);
// because pointer is align 2 and u32 align % 2 == 0 we can assume align 2
var smaller align(2) = []u32 {
var smaller align(2) = []u32{
1,
2,
3,

View File

@ -34,7 +34,7 @@ test "void arrays" {
}
test "array literal" {
const hex_mult = []u16 {
const hex_mult = []u16{
4096,
256,
16,
@ -54,7 +54,7 @@ test "array dot len const expr" {
const ArrayDotLenConstExpr = struct {
y: [some_array.len]u8,
};
const some_array = []u8 {
const some_array = []u8{
0,
1,
2,
@ -62,7 +62,7 @@ const some_array = []u8 {
};
test "nested arrays" {
const array_of_strings = [][]const u8 {
const array_of_strings = [][]const u8{
"hello",
"this",
"is",
@ -86,9 +86,7 @@ const Str = struct {
a: []Sub,
};
test "set global var array via slice embedded in struct" {
var s = Str {
.a = s_array[0..],
};
var s = Str{ .a = s_array[0..] };
s.a[0].b = 1;
s.a[1].b = 2;
@ -100,7 +98,7 @@ test "set global var array via slice embedded in struct" {
}
test "array literal with specified size" {
var array = [2]u8 {
var array = [2]u8{
1,
2,
};

View File

@ -10,11 +10,9 @@ const S = struct {
const assert = @import("std").debug.assert;
test "bug 394 fixed" {
const x = S {
const x = S{
.x = 3,
.y = E {
.B = 1,
},
.y = E{ .B = 1 },
};
assert(x.x == 3);
}

View File

@ -14,10 +14,8 @@ test "nullable if after an if in a switch prong of a switch with 2 prongs in an
}
fn foo(a: bool, b: bool) void {
var prefix_op = PrefixOp {
.AddrOf = Value {
.align_expr = 1234,
},
var prefix_op = PrefixOp{
.AddrOf = Value{ .align_expr = 1234 },
};
if (a) {} else {
switch (prefix_op) {

View File

@ -1,14 +1,10 @@
const CountBy = struct {
a: usize,
const One = CountBy {
.a = 1,
};
const One = CountBy{ .a = 1 };
pub fn counter(self: &const CountBy) Counter {
return Counter {
.i = 0,
};
return Counter{ .i = 0 };
}
};

View File

@ -33,27 +33,21 @@ fn funcWithConstPtrPtr(x: &const &i32) void {
}
test "implicitly cast a container to a const pointer of it" {
const z = Struct(void) {
.x = void{},
};
const z = Struct(void){ .x = void{} };
assert(0 == @sizeOf(@typeOf(z)));
assert(void{} == Struct(void).pointer(z).x);
assert(void{} == Struct(void).pointer(&z).x);
assert(void{} == Struct(void).maybePointer(z).x);
assert(void{} == Struct(void).maybePointer(&z).x);
assert(void{} == Struct(void).maybePointer(null).x);
const s = Struct(u8) {
.x = 42,
};
const s = Struct(u8){ .x = 42 };
assert(0 != @sizeOf(@typeOf(s)));
assert(42 == Struct(u8).pointer(s).x);
assert(42 == Struct(u8).pointer(&s).x);
assert(42 == Struct(u8).maybePointer(s).x);
assert(42 == Struct(u8).maybePointer(&s).x);
assert(0 == Struct(u8).maybePointer(null).x);
const u = Union {
.x = 42,
};
const u = Union{ .x = 42 };
assert(42 == Union.pointer(u).x);
assert(42 == Union.pointer(&u).x);
assert(42 == Union.maybePointer(u).x);
@ -77,9 +71,7 @@ fn Struct(comptime T: type) type {
}
fn maybePointer(self: ?&const Self) Self {
const none = Self {
.x = if (T == void) void{} else 0,
};
const none = Self{ .x = if (T == void) void{} else 0 };
return (self ?? &none).*;
}
};
@ -93,9 +85,7 @@ const Union = union {
}
fn maybePointer(self: ?&const Union) Union {
const none = Union {
.x = 0,
};
const none = Union{ .x = 0 };
return (self ?? &none).*;
}
};
@ -130,9 +120,7 @@ test "implicitly cast indirect pointer to maybe-indirect pointer" {
return ((??p).*.*).x;
}
};
const s = S {
.x = 42,
};
const s = S{ .x = 42 };
const p = &s;
const q = &p;
const r = &q;
@ -202,9 +190,7 @@ fn castToMaybeTypeError(z: i32) void {
const f = z;
const g: error!?i32 = f;
const a = A {
.a = z,
};
const a = A{ .a = z };
const b: error!?A = a;
assert((??(b catch unreachable)).a == 1);
}
@ -343,7 +329,6 @@ test "peer type resolution: error and [N]T" {
// TODO: implicit error!T to error!U where T can implicitly cast to U
//assert(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));
//comptime assert(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));
assert(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK"));
comptime assert(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK"));
}
@ -387,7 +372,7 @@ fn cast128Float(x: u128) f128 {
}
test "const slice widen cast" {
const bytes align(4) = []u8 {
const bytes align(4) = []u8{
0x12,
0x12,
0x12,

View File

@ -4,7 +4,7 @@ const assert = debug.assert;
var argv: &const &const u8 = undefined;
test "const slice child" {
const strs = ([]&const u8) {
const strs = ([]&const u8){
c"one",
c"two",
c"three",

View File

@ -10,7 +10,6 @@ test "create a coroutine and cancel it" {
cancel p;
assert(x == 2);
}
async fn simpleAsyncFn() void {
x += 1;
suspend;
@ -28,7 +27,6 @@ test "coroutine suspend, resume, cancel" {
assert(std.mem.eql(u8, points, "abcdefg"));
}
async fn testAsyncSeq() void {
defer seq('e');
@ -36,7 +34,7 @@ async fn testAsyncSeq() void {
suspend;
seq('d');
}
var points = []u8 {0} ** "abcdefg".len;
var points = []u8{0} ** "abcdefg".len;
var index: usize = 0;
fn seq(c: u8) void {
@ -54,7 +52,6 @@ test "coroutine suspend with block" {
var a_promise: promise = undefined;
var result = false;
async fn testSuspendBlock() void {
suspend |p| {
comptime assert(@typeOf(p) == promise->void);
@ -75,7 +72,6 @@ test "coroutine await" {
assert(await_final_result == 1234);
assert(std.mem.eql(u8, await_points, "abcdefghi"));
}
async fn await_amain() void {
await_seq('b');
const p = async await_another() catch unreachable;
@ -83,7 +79,6 @@ async fn await_amain() void {
await_final_result = await p;
await_seq('h');
}
async fn await_another() i32 {
await_seq('c');
suspend |p| {
@ -94,7 +89,7 @@ async fn await_another() i32 {
return 1234;
}
var await_points = []u8 {0} ** "abcdefghi".len;
var await_points = []u8{0} ** "abcdefghi".len;
var await_seq_index: usize = 0;
fn await_seq(c: u8) void {
@ -111,7 +106,6 @@ test "coroutine await early return" {
assert(early_final_result == 1234);
assert(std.mem.eql(u8, early_points, "abcdef"));
}
async fn early_amain() void {
early_seq('b');
const p = async early_another() catch unreachable;
@ -119,13 +113,12 @@ async fn early_amain() void {
early_final_result = await p;
early_seq('e');
}
async fn early_another() i32 {
early_seq('c');
return 1234;
}
var early_points = []u8 {0} ** "abcdef".len;
var early_points = []u8{0} ** "abcdef".len;
var early_seq_index: usize = 0;
fn early_seq(c: u8) void {
@ -141,7 +134,6 @@ test "coro allocation failure" {
error.OutOfMemory => {},
}
}
async fn asyncFuncThatNeverGetsRun() void {
@panic("coro frame allocation should fail");
}
@ -164,15 +156,12 @@ test "async fn pointer in a struct field" {
const Foo = struct {
bar: async<&std.mem.Allocator> fn(&i32) void,
};
var foo = Foo {
.bar = simpleAsyncFn2,
};
var foo = Foo{ .bar = simpleAsyncFn2 };
const p = (async<std.debug.global_allocator> foo.bar(&data)) catch unreachable;
assert(data == 2);
cancel p;
assert(data == 4);
}
async<&std.mem.Allocator> fn simpleAsyncFn2(y: &i32) void {
defer y.* += 2;
y.* += 1;
@ -184,7 +173,6 @@ test "async fn with inferred error set" {
resume p;
cancel p;
}
async fn failing() !void {
suspend;
return error.Fail;
@ -208,12 +196,10 @@ test "error return trace across suspend points - async return" {
fn nonFailing() (promise->error!void) {
return async<std.debug.global_allocator> suspendThenFail() catch unreachable;
}
async fn suspendThenFail() error!void {
suspend;
return error.Fail;
}
async fn printTrace(p: promise->error!void) void {
(await p) catch |e| {
std.debug.assert(e == error.Fail);
@ -234,7 +220,6 @@ test "break from suspend" {
cancel p;
std.debug.assert(my_result == 2);
}
async fn testBreakFromSuspend(my_result: &i32) void {
s: suspend |p| {
break :s;

View File

@ -2,11 +2,9 @@ const assert = @import("std").debug.assert;
const mem = @import("std").mem;
test "enum type" {
const foo1 = Foo {
.One = 13,
};
const foo2 = Foo {
.Two = Point {
const foo1 = Foo{ .One = 13 };
const foo2 = Foo{
.Two = Point{
.x = 1234,
.y = 5678,
},
@ -48,18 +46,12 @@ const Bar = enum {
};
fn returnAnInt(x: i32) Foo {
return Foo {
.One = x,
};
return Foo{ .One = x };
}
test "constant enum with payload" {
var empty = AnEnumWithPayload {
.Empty = {},
};
var full = AnEnumWithPayload {
.Full = 13,
};
var empty = AnEnumWithPayload{ .Empty = {} };
var full = AnEnumWithPayload{ .Full = 13 };
shouldBeEmpty(empty);
shouldBeNotEmpty(full);
}
@ -737,7 +729,7 @@ const BitFieldOfEnums = packed struct {
c: C,
};
const bit_field_1 = BitFieldOfEnums {
const bit_field_1 = BitFieldOfEnums{
.a = A.Two,
.b = B.Three3,
.c = C.Four4,

View File

@ -15,12 +15,8 @@ const ET = union(enum) {
};
test "enum with members" {
const a = ET {
.SINT = -42,
};
const b = ET {
.UINT = 42,
};
const a = ET{ .SINT = -42 };
const b = ET{ .UINT = 42 };
var buf: [20]u8 = undefined;
assert((a.print(buf[0..]) catch unreachable) == 3);

View File

@ -92,7 +92,7 @@ test "error set type " {
comptime testErrorSetType();
}
const MyErrSet = error {
const MyErrSet = error{
OutOfMemory,
FileNotFound,
};
@ -114,11 +114,11 @@ test "explicit error set cast" {
comptime testExplicitErrorSetCast(Set1.A);
}
const Set1 = error {
const Set1 = error{
A,
B,
};
const Set2 = error {
const Set2 = error{
A,
C,
};
@ -134,8 +134,7 @@ test "comptime test error for empty error set" {
comptime testComptimeTestErrorEmptySet(1234);
}
const EmptyErrorSet = error {
};
const EmptyErrorSet = error{};
fn testComptimeTestErrorEmptySet(x: EmptyErrorSet!i32) void {
if (x) |v| assert(v == 1234) else |err| @compileError("bad");
@ -151,9 +150,10 @@ test "comptime err to int of error set with only 1 possible value" {
testErrToIntWithOnePossibleValue(error.A, u32(error.A));
comptime testErrToIntWithOnePossibleValue(error.A, u32(error.A));
}
fn testErrToIntWithOnePossibleValue(x: error {
A,
}, comptime value: u32) void {
fn testErrToIntWithOnePossibleValue(
x: error{A},
comptime value: u32,
) void {
if (u32(x) != value) {
@compileError("bad");
}
@ -197,16 +197,14 @@ fn foo2(f: fn() error!void) void {
const x = f();
}
fn bar2() (error {
}!void) {}
fn bar2() (error{}!void) {}
test "error: Zero sized error set returned with value payload crash" {
_ = foo3(0);
_ = comptime foo3(0);
}
const Error = error {
};
const Error = error{};
fn foo3(b: usize) Error!usize {
return b;
}

View File

@ -72,12 +72,12 @@ const Point = struct {
x: i32,
y: i32,
};
const static_point_list = []Point {
const static_point_list = []Point{
makePoint(1, 2),
makePoint(3, 4),
};
fn makePoint(x: i32, y: i32) Point {
return Point {
return Point{
.x = x,
.y = y,
};
@ -92,13 +92,11 @@ pub const Vec3 = struct {
data: [3]f32,
};
pub fn vec3(x: f32, y: f32, z: f32) Vec3 {
return Vec3 {
.data = []f32 {
x,
y,
z,
},
};
return Vec3{ .data = []f32{
x,
y,
z,
} };
}
test "constant expressions" {
@ -117,22 +115,22 @@ const Vertex = struct {
g: f32,
b: f32,
};
const vertices = []Vertex {
Vertex {
const vertices = []Vertex{
Vertex{
.x = -0.6,
.y = -0.4,
.r = 1.0,
.g = 0.0,
.b = 0.0,
},
Vertex {
Vertex{
.x = 0.6,
.y = -0.4,
.r = 0.0,
.g = 1.0,
.b = 0.0,
},
Vertex {
Vertex{
.x = 0.0,
.y = 0.6,
.r = 0.0,
@ -149,7 +147,7 @@ const StInitStrFoo = struct {
x: i32,
y: bool,
};
var st_init_str_foo = StInitStrFoo {
var st_init_str_foo = StInitStrFoo{
.x = 13,
.y = true,
};
@ -158,7 +156,7 @@ test "statically initalized array literal" {
const y: [4]u8 = st_init_arr_lit_x;
assert(y[3] == 4);
}
const st_init_arr_lit_x = []u8 {
const st_init_arr_lit_x = []u8{
1,
2,
3,
@ -220,16 +218,16 @@ const CmdFn = struct {
func: fn(i32) i32,
};
const cmd_fns = []CmdFn {
CmdFn {
const cmd_fns = []CmdFn{
CmdFn{
.name = "one",
.func = one,
},
CmdFn {
CmdFn{
.name = "two",
.func = two,
},
CmdFn {
CmdFn{
.name = "three",
.func = three,
},
@ -289,9 +287,7 @@ const SimpleStruct = struct {
}
};
var simple_struct = SimpleStruct {
.field = 1234,
};
var simple_struct = SimpleStruct{ .field = 1234 };
const bound_fn = simple_struct.method;
@ -341,9 +337,7 @@ const Foo = struct {
name: []const u8,
};
var foo_contents = Foo {
.name = "a",
};
var foo_contents = Foo{ .name = "a" };
const foo_ref = &foo_contents;
test "create global array with for loop" {
@ -529,9 +523,7 @@ const SingleFieldStruct = struct {
};
test "const ptr to comptime mutable data is not memoized" {
comptime {
var foo = SingleFieldStruct {
.x = 1,
};
var foo = SingleFieldStruct{ .x = 1 };
assert(foo.read_x() == 1);
foo.x = 2;
assert(foo.read_x() == 2);
@ -574,9 +566,7 @@ pub const Info = struct {
version: u8,
};
pub const diamond_info = Info {
.version = 0,
};
pub const diamond_info = Info{ .version = 0 };
test "comptime modification of const struct field" {
comptime {

View File

@ -17,7 +17,7 @@ const Foo = struct {
d: i32,
};
const foo = Foo {
const foo = Foo{
.a = true,
.b = 0.123,
.c = 1234,

View File

@ -73,7 +73,7 @@ fn fnWithUnreachable() noreturn {
}
test "function pointers" {
const fns = []@typeOf(fn1) {
const fns = []@typeOf(fn1){
fn1,
fn2,
fn3,

View File

@ -1,6 +1,6 @@
const assert = @import("std").debug.assert;
fn get_foo() fn(&u8)usize {
fn get_foo() fn(&u8) usize {
comptime {
return struct {
fn func(ptr: &u8) usize {

View File

@ -3,7 +3,7 @@ const assert = std.debug.assert;
const mem = std.mem;
test "continue in for loop" {
const array = []i32 {
const array = []i32{
1,
2,
3,
@ -35,7 +35,7 @@ fn mangleString(s: []u8) void {
}
test "basic for loop" {
const expected_result = []u8 {
const expected_result = []u8{
9,
8,
7,
@ -57,7 +57,7 @@ test "basic for loop" {
var buffer: [expected_result.len]u8 = undefined;
var buf_index: usize = 0;
const array = []u8 {
const array = []u8{
9,
8,
7,

View File

@ -81,11 +81,11 @@ test "function with return type type" {
}
test "generic struct" {
var a1 = GenNode(i32) {
var a1 = GenNode(i32){
.value = 13,
.next = null,
};
var b1 = GenNode(bool) {
var b1 = GenNode(bool){
.value = true,
.next = null,
};
@ -120,8 +120,8 @@ fn aGenericFn(comptime T: type, comptime a: T, b: T) T {
}
test "generic fn with implicit cast" {
assert(getFirstByte(u8, []u8 {13}) == 13);
assert(getFirstByte(u16, []u16 {
assert(getFirstByte(u8, []u8{13}) == 13);
assert(getFirstByte(u16, []u16{
0,
13,
}) == 0);
@ -133,7 +133,7 @@ fn getFirstByte(comptime T: type, mem: []const T) u8 {
return getByte(@ptrCast(&const u8, &mem[0]));
}
const foos = []fn(var) bool {
const foos = []fn(var) bool{
foo1,
foo2,
};

View File

@ -21,11 +21,9 @@ fn foo(a: &const A) i32 {
}
test "incomplete struct param top level declaration" {
const a = A {
.b = B {
.c = C {
.x = 13,
},
const a = A{
.b = B{
.c = C{ .x = 13 },
},
};
assert(foo(a) == 13);

View File

@ -197,7 +197,7 @@ fn test_u64_div() void {
assert(result.remainder == 100663296);
}
fn divWithResult(a: u64, b: u64) DivResult {
return DivResult {
return DivResult{
.quotient = a / b,
.remainder = a % b,
};

View File

@ -232,7 +232,7 @@ test "string escapes" {
}
test "multiline string" {
const s1 =
const s1 =
\\one
\\two)
\\three
@ -242,7 +242,7 @@ test "multiline string" {
}
test "multiline C string" {
const s1 =
const s1 =
c\\one
c\\two)
c\\three
@ -350,15 +350,13 @@ const Test3Point = struct {
x: i32,
y: i32,
};
const test3_foo = Test3Foo {
.Three = Test3Point {
const test3_foo = Test3Foo{
.Three = Test3Point{
.x = 3,
.y = 4,
},
};
const test3_bar = Test3Foo {
.Two = 13,
};
const test3_bar = Test3Foo{ .Two = 13 };
fn test3_1(f: &const Test3Foo) void {
switch (f.*) {
Test3Foo.Three => |pt| {
@ -417,7 +415,7 @@ test "C string concatenation" {
test "cast slice to u8 slice" {
assert(@sizeOf(i32) == 4);
var big_thing_array = []i32 {
var big_thing_array = []i32{
1,
2,
3,
@ -458,9 +456,9 @@ test "non const ptr to aliased type" {
}
test "array 2D const double ptr" {
const rect_2d_vertexes = [][1]f32 {
[]f32 {1.0},
[]f32 {2.0},
const rect_2d_vertexes = [][1]f32{
[]f32{1.0},
[]f32{2.0},
};
testArray2DConstDoublePtr(&rect_2d_vertexes[0][0]);
}
@ -565,7 +563,7 @@ test "volatile load and store" {
test "slice string literal has type []const u8" {
comptime {
assert(@typeOf("aoeu"[0..]) == []const u8);
const array = []i32 {
const array = []i32{
1,
2,
3,
@ -581,13 +579,9 @@ test "global variable initialized to global variable array element" {
const GDTEntry = struct {
field: i32,
};
var gdt = []GDTEntry {
GDTEntry {
.field = 1,
},
GDTEntry {
.field = 2,
},
var gdt = []GDTEntry{
GDTEntry{ .field = 1 },
GDTEntry{ .field = 2 },
};
var global_ptr = &gdt[0];
@ -648,9 +642,7 @@ fn testStructInFn() void {
kind: BlockKind,
};
var block = Block {
.kind = 1234,
};
var block = Block{ .kind = 1234 };
block.kind += 1;
@ -694,12 +686,10 @@ const PackedEnum = packed enum {
};
test "packed struct, enum, union parameters in extern function" {
testPackedStuff(PackedStruct {
testPackedStuff(PackedStruct{
.a = 1,
.b = 2,
}, PackedUnion {
.a = 1,
}, PackedEnum.A);
}, PackedUnion{ .a = 1 }, PackedEnum.A);
}
export fn testPackedStuff(a: &const PackedStruct, b: &const PackedUnion, c: PackedEnum) void {}

View File

@ -58,7 +58,7 @@ fn foo(x: ?i32) ?bool {
}
test "if var maybe pointer" {
assert(shouldBeAPlus1(Particle {
assert(shouldBeAPlus1(Particle{
.a = 14,
.b = 1,
.c = 1,
@ -92,9 +92,7 @@ test "null literal outside function" {
const SillyStruct = struct {
context: ?i32,
};
const here_is_a_null_literal = SillyStruct {
.context = null,
};
const here_is_a_null_literal = SillyStruct{ .context = null };
test "test null runtime" {
testTestNullRuntime(null);

View File

@ -59,7 +59,7 @@ test "reflection: enum member types and names" {
}
test "reflection: @field" {
var f = Foo {
var f = Foo{
.one = 42,
.two = true,
.three = void{},

View File

@ -18,7 +18,7 @@ test "slice child property" {
}
test "runtime safety lets us slice from len..len" {
var an_array = []u8 {
var an_array = []u8{
1,
2,
3,

View File

@ -27,7 +27,7 @@ test "invake static method in global scope" {
}
test "void struct fields" {
const foo = VoidStructFieldsFoo {
const foo = VoidStructFieldsFoo{
.a = void{},
.b = 1,
.c = void{},
@ -96,16 +96,12 @@ test "struct byval assign" {
}
fn structInitializer() void {
const val = Val {
.x = 42,
};
const val = Val{ .x = 42 };
assert(val.x == 42);
}
test "fn call of struct field" {
assert(callStructField(Foo {
.ptr = aFunc,
}) == 13);
assert(callStructField(Foo{ .ptr = aFunc }) == 13);
}
const Foo = struct {
@ -121,9 +117,7 @@ fn callStructField(foo: &const Foo) i32 {
}
test "store member function in variable" {
const instance = MemberFnTestFoo {
.x = 1234,
};
const instance = MemberFnTestFoo{ .x = 1234 };
const memberFn = MemberFnTestFoo.member;
const result = memberFn(instance);
assert(result == 1234);
@ -136,17 +130,13 @@ const MemberFnTestFoo = struct {
};
test "call member function directly" {
const instance = MemberFnTestFoo {
.x = 1234,
};
const instance = MemberFnTestFoo{ .x = 1234 };
const result = MemberFnTestFoo.member(instance);
assert(result == 1234);
}
test "member functions" {
const r = MemberFnRand {
.seed = 1234,
};
const r = MemberFnRand{ .seed = 1234 };
assert(r.getSeed() == 1234);
}
const MemberFnRand = struct {
@ -165,7 +155,7 @@ const Bar = struct {
y: i32,
};
fn makeBar(x: i32, y: i32) Bar {
return Bar {
return Bar{
.x = x,
.y = y,
};
@ -190,7 +180,7 @@ fn testReturnEmptyStructFromFn() EmptyStruct2 {
}
test "pass slice of empty struct to fn" {
assert(testPassSliceOfEmptyStructToFn([]EmptyStruct2 {EmptyStruct2{}}) == 1);
assert(testPassSliceOfEmptyStructToFn([]EmptyStruct2{EmptyStruct2{}}) == 1);
}
fn testPassSliceOfEmptyStructToFn(slice: []const EmptyStruct2) usize {
return slice.len;
@ -202,7 +192,7 @@ const APackedStruct = packed struct {
};
test "packed struct" {
var foo = APackedStruct {
var foo = APackedStruct{
.x = 1,
.y = 2,
};
@ -217,7 +207,7 @@ const BitField1 = packed struct {
c: u2,
};
const bit_field_1 = BitField1 {
const bit_field_1 = BitField1{
.a = 1,
.b = 2,
.c = 3,
@ -267,7 +257,7 @@ test "packed struct 24bits" {
assert(@sizeOf(Foo96Bits) == 12);
}
var value = Foo96Bits {
var value = Foo96Bits{
.a = 0,
.b = 0,
.c = 0,
@ -310,7 +300,7 @@ test "packed array 24bits" {
assert(@sizeOf(FooArray24Bits) == 2 + 2 * 3 + 2);
}
var bytes = []u8 {0} ** (@sizeOf(FooArray24Bits) + 1);
var bytes = []u8{0} ** (@sizeOf(FooArray24Bits) + 1);
bytes[bytes.len - 1] = 0xaa;
const ptr = &([]FooArray24Bits)(bytes[0..bytes.len - 1])[0];
assert(ptr.a == 0);
@ -360,7 +350,7 @@ test "aligned array of packed struct" {
assert(@sizeOf(FooArrayOfAligned) == 2 * 2);
}
var bytes = []u8 {0xbb} ** @sizeOf(FooArrayOfAligned);
var bytes = []u8{0xbb} ** @sizeOf(FooArrayOfAligned);
const ptr = &([]FooArrayOfAligned)(bytes[0..bytes.len])[0];
assert(ptr.a[0].a == 0xbb);
@ -370,11 +360,11 @@ test "aligned array of packed struct" {
}
test "runtime struct initialization of bitfield" {
const s1 = Nibbles {
const s1 = Nibbles{
.x = x1,
.y = x1,
};
const s2 = Nibbles {
const s2 = Nibbles{
.x = u4(x2),
.y = u4(x2),
};

View File

@ -6,31 +6,31 @@ const Node = struct {
};
test "struct contains slice of itself" {
var other_nodes = []Node {
Node {
var other_nodes = []Node{
Node{
.payload = 31,
.children = []Node{},
},
Node {
Node{
.payload = 32,
.children = []Node{},
},
};
var nodes = []Node {
Node {
var nodes = []Node{
Node{
.payload = 1,
.children = []Node{},
},
Node {
Node{
.payload = 2,
.children = []Node{},
},
Node {
Node{
.payload = 3,
.children = other_nodes[0..],
},
};
const root = Node {
const root = Node{
.payload = 1234,
.children = nodes[0..],
};

View File

@ -6,10 +6,7 @@ test "switch with numbers" {
fn testSwitchWithNumbers(x: u32) void {
const result = switch (x) {
1,
2,
3,
4 ... 8 => false,
1, 2, 3, 4...8 => false,
13 => true,
else => false,
};
@ -25,9 +22,9 @@ test "switch with all ranges" {
fn testSwitchWithAllRanges(x: u32, y: u32) u32 {
return switch (x) {
0 ... 100 => 1,
101 ... 200 => 2,
201 ... 300 => 3,
0...100 => 1,
101...200 => 2,
201...300 => 3,
else => y,
};
}
@ -37,10 +34,8 @@ test "implicit comptime switch" {
const result = switch (x) {
3 => 10,
4 => 11,
5,
6 => 12,
7,
8 => 13,
5, 6 => 12,
7, 8 => 13,
else => 14,
};
@ -86,15 +81,9 @@ const SwitchStatmentFoo = enum {
};
test "switch prong with variable" {
switchProngWithVarFn(SwitchProngWithVarEnum {
.One = 13,
});
switchProngWithVarFn(SwitchProngWithVarEnum {
.Two = 13.0,
});
switchProngWithVarFn(SwitchProngWithVarEnum {
.Meh = {},
});
switchProngWithVarFn(SwitchProngWithVarEnum{ .One = 13 });
switchProngWithVarFn(SwitchProngWithVarEnum{ .Two = 13.0 });
switchProngWithVarFn(SwitchProngWithVarEnum{ .Meh = {} });
}
const SwitchProngWithVarEnum = union(enum) {
One: i32,
@ -121,9 +110,7 @@ test "switch on enum using pointer capture" {
}
fn testSwitchEnumPtrCapture() void {
var value = SwitchProngWithVarEnum {
.One = 1234,
};
var value = SwitchProngWithVarEnum{ .One = 1234 };
switch (value) {
SwitchProngWithVarEnum.One => |*x| x.* += 1,
else => unreachable,
@ -136,12 +123,8 @@ fn testSwitchEnumPtrCapture() void {
test "switch with multiple expressions" {
const x = switch (returnsFive()) {
1,
2,
3 => 1,
4,
5,
6 => 2,
1, 2, 3 => 1,
4, 5, 6 => 2,
else => i32(3),
};
assert(x == 2);
@ -156,9 +139,7 @@ const Number = union(enum) {
Three: f32,
};
const number = Number {
.Three = 1.23,
};
const number = Number{ .Three = 1.23 };
fn returnsFalse() bool {
switch (number) {
@ -212,12 +193,11 @@ fn testSwitchHandleAllCasesExhaustive(x: u2) u2 {
fn testSwitchHandleAllCasesRange(x: u8) u8 {
return switch (x) {
0 ... 100 => u8(0),
101 ... 200 => 1,
201,
203 => 2,
0...100 => u8(0),
101...200 => 1,
201, 203 => 2,
202 => 4,
204 ... 255 => 3,
204...255 => 3,
};
}

View File

@ -14,9 +14,7 @@ const FormValue = union(enum) {
fn doThing(form_id: u64) error!FormValue {
return switch (form_id) {
17 => FormValue {
.Address = try readOnce(),
},
17 => FormValue{ .Address = try readOnce() },
else => error.InvalidDebugInfo,
};
}

View File

@ -7,12 +7,8 @@ const FormValue = union(enum) {
fn foo(id: u64) !FormValue {
return switch (id) {
2 => FormValue {
.Two = true,
},
1 => FormValue {
.One = {},
},
2 => FormValue{ .Two = true },
1 => FormValue{ .One = {} },
else => return error.Whatever,
};
}

View File

@ -29,7 +29,7 @@ test "this refer to module call private fn" {
}
test "this refer to container" {
var pt = Point(i32) {
var pt = Point(i32){
.x = 12,
.y = 34,
};

View File

@ -7,8 +7,7 @@ test "try on error union" {
fn tryOnErrorUnionImpl() void {
const x = if (returnsTen()) |val| val + 1 else |err| switch (err) {
error.ItBroke,
error.NoMem => 1,
error.ItBroke, error.NoMem => 1,
error.CrappedOut => i32(2),
else => unreachable,
};

View File

@ -103,7 +103,7 @@ test "type info: error set, error union info" {
}
fn testErrorSet() void {
const TestErrorSet = error {
const TestErrorSet = error{
First,
Second,
Third,
@ -196,7 +196,7 @@ fn testStruct() void {
assert(!struct_info.Struct.defs[0].data.Fn.is_extern);
assert(struct_info.Struct.defs[0].data.Fn.lib_name == null);
assert(struct_info.Struct.defs[0].data.Fn.return_type == void);
assert(struct_info.Struct.defs[0].data.Fn.fn_type == fn(&const TestStruct)void);
assert(struct_info.Struct.defs[0].data.Fn.fn_type == fn(&const TestStruct) void);
}
const TestStruct = packed struct {

View File

@ -50,10 +50,10 @@ test "basic unions" {
test "comptime union field access" {
comptime {
var foo = Foo { .int = 0 };
var foo = Foo{ .int = 0 };
assert(foo.int == 0);
foo = Foo { .float = 42.42 };
foo = Foo{ .float = 42.42 };
assert(foo.float == 42.42);
}
}
@ -286,7 +286,6 @@ const PartialInstWithPayload = union(enum) {
Compiled: i32,
};
test "access a member of tagged union with conflicting enum tag name" {
const Bar = union(enum) {
A: A,

View File

@ -58,7 +58,7 @@ fn extraFn(extra: u32, args: ...) usize {
return args.len;
}
const foos = []fn(...) bool {
const foos = []fn(...) bool{
foo1,
foo2,
};

View File

@ -8,7 +8,7 @@ const Foo = struct {
test "compare void with void compile time known" {
comptime {
const foo = Foo {
const foo = Foo{
.a = {},
.b = 1,
.c = {},

View File

@ -151,7 +151,7 @@ test "while on nullable with else result follow break prong" {
test "while on error union with else result follow else prong" {
const result = while (returnError()) |value| {
break value;
} else|err|
} else |err|
i32(2);
assert(result == 2);
}
@ -159,7 +159,7 @@ test "while on error union with else result follow else prong" {
test "while on error union with else result follow break prong" {
const result = while (returnSuccess(10)) |value| {
break value;
} else|err|
} else |err|
i32(2);
assert(result == 10);
}

View File

@ -475,7 +475,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) void {
\\
);
tc.setCommandLineArgs([][]const u8 {
tc.setCommandLineArgs([][]const u8{
"first arg",
"'a' 'b' \\",
"bare",
@ -516,7 +516,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) void {
\\
);
tc.setCommandLineArgs([][]const u8 {
tc.setCommandLineArgs([][]const u8{
"first arg",
"'a' 'b' \\",
"bare",

File diff suppressed because it is too large Load Diff

View File

@ -76,5 +76,4 @@ pub fn addCases(cases: &tests.GenHContext) void {
\\TEST_EXPORT void entry(struct Foo foo, uint8_t bar[]);
\\
);
}

View File

@ -29,8 +29,7 @@ fn tokenize(input: []const u8) !ArrayList(Token) {
for (input) |b, i| {
switch (state) {
State.Start => switch (b) {
'a' ... 'z',
'A' ... 'Z' => {
'a'...'z', 'A'...'Z' => {
state = State.Word;
tok_begin = i;
},
@ -40,11 +39,8 @@ fn tokenize(input: []const u8) !ArrayList(Token) {
else => return error.InvalidInput,
},
State.Word => switch (b) {
'a' ... 'z',
'A' ... 'Z' => {},
'{',
'}',
',' => {
'a'...'z', 'A'...'Z' => {},
'{', '}', ',' => {
try token_list.append(Token{ .Word = input[tok_begin..i] });
switch (b) {
'{' => try token_list.append(Token.OpenBrace),
@ -103,8 +99,7 @@ fn parse(tokens: &const ArrayList(Token), token_index: &usize) ParseError!Node {
};
switch (tokens.items[token_index.*]) {
Token.Word,
Token.OpenBrace => {
Token.Word, Token.OpenBrace => {
const pair = try global_allocator.alloc(Node, 2);
pair[0] = result_node;
pair[1] = try parse(tokens, token_index);

View File

@ -1,5 +1,8 @@
const StackTrace = @import("builtin").StackTrace;
pub fn panic(msg: []const u8, stack_trace: ?&StackTrace) noreturn { @breakpoint(); while (true) {} }
pub fn panic(msg: []const u8, stack_trace: ?&StackTrace) noreturn {
@breakpoint();
while (true) {}
}
fn bar() error!void {}

View File

@ -1 +1,3 @@
pub fn add(a: i32, b: i32) i32 { return a + b; }
pub fn add(a: i32, b: i32) i32 {
return a + b;
}

Some files were not shown because too many files have changed in this diff Show More