From 3775779256b6d723b91b90b217104b175216712d Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Thu, 29 Feb 2024 07:35:19 +0100 Subject: [PATCH] zig 0.12.0-dev.3074+ae7f3fc36 --- build.zig | 2 +- src/Parser.zig | 40 +++++---- src/lib/buzz_fs.zig | 10 ++- src/lib/buzz_http.zig | 35 +++++--- src/lib/buzz_io.zig | 5 +- src/lib/buzz_os.zig | 124 +++++---------------------- src/lib/errors.buzz | 3 +- src/lib/http.buzz | 7 +- src/lib/os.buzz | 4 +- src/vm.zig | 194 +++++++++++++++++++++--------------------- src/wasm.zig | 38 ++------- src/wasm_repl.zig | 2 +- 12 files changed, 186 insertions(+), 278 deletions(-) diff --git a/build.zig b/build.zig index 7ed1ee65..484c0239 100644 --- a/build.zig +++ b/build.zig @@ -96,7 +96,7 @@ fn getBuzzPrefix(b: *Build) []const u8 { pub fn build(b: *Build) !void { // Check minimum zig version const current_zig = builtin.zig_version; - const min_zig = std.SemanticVersion.parse("0.12.0-dev.2833+8802ec583") catch return; + const min_zig = std.SemanticVersion.parse("0.12.0-dev.3074+ae7f3fc3") catch return; if (current_zig.order(min_zig).compare(.lt)) { @panic(b.fmt("Your Zig version v{} does not meet the minimum build requirement of v{}", .{ current_zig, min_zig })); } diff --git a/src/Parser.zig b/src/Parser.zig index 4fc3511e..9596745d 100644 --- a/src/Parser.zig +++ b/src/Parser.zig @@ -3841,14 +3841,16 @@ fn dot(self: *Self, can_assign: bool, callee: Ast.Node.Index) Error!Ast.Node.Ind if (try self.match(.LeftParen)) { // `call` will look to the parent node for the function definition self.ast.nodes.items(.type_def)[dot_node] = member; - const components = self.ast.nodes.items(.components); + var components = self.ast.nodes.items(.components); components[dot_node].Dot.member_type_def = member.?; components[dot_node].Dot.member_kind = .Call; + const dot_call = try self.call( + can_assign, + dot_node, + ); + components = self.ast.nodes.items(.components); components[dot_node].Dot.value_or_call_or_enum = .{ - .Call = try self.call( - can_assign, - dot_node, - ), + .Call = dot_call, }; // Node type is the return type of the call @@ -3879,14 +3881,16 @@ fn dot(self: *Self, can_assign: bool, callee: Ast.Node.Index) Error!Ast.Node.Ind if (try self.match(.LeftParen)) { // `call` will look to the parent node for the function definition self.ast.nodes.items(.type_def)[dot_node] = member; - const components = self.ast.nodes.items(.components); + var components = self.ast.nodes.items(.components); components[dot_node].Dot.member_type_def = member.?; components[dot_node].Dot.member_kind = .Call; + const dot_call = try self.call( + can_assign, + dot_node, + ); + components = self.ast.nodes.items(.components); components[dot_node].Dot.value_or_call_or_enum = .{ - .Call = try self.call( - can_assign, - dot_node, - ), + .Call = dot_call, }; // Node type is the return type of the call @@ -3917,14 +3921,16 @@ fn dot(self: *Self, can_assign: bool, callee: Ast.Node.Index) Error!Ast.Node.Ind if (try self.match(.LeftParen)) { // `call` will look to the parent node for the function definition self.ast.nodes.items(.type_def)[dot_node] = member; - const components = self.ast.nodes.items(.components); + var components = self.ast.nodes.items(.components); components[dot_node].Dot.member_kind = .Call; components[dot_node].Dot.member_type_def = member.?; + const dot_call = try self.call( + can_assign, + dot_node, + ); + components = self.ast.nodes.items(.components); components[dot_node].Dot.value_or_call_or_enum = .{ - .Call = try self.call( - can_assign, - dot_node, - ), + .Call = dot_call, }; // Node type is the return type of the call @@ -4111,8 +4117,10 @@ fn dot(self: *Self, can_assign: bool, callee: Ast.Node.Index) Error!Ast.Node.Ind var components = self.ast.nodes.items(.components); if (can_assign and try self.match(.Equal)) { components[dot_node].Dot.member_kind = .Value; + const expr = try self.expression(false); + components = self.ast.nodes.items(.components); components[dot_node].Dot.value_or_call_or_enum = .{ - .Value = try self.expression(false), + .Value = expr, }; self.ast.nodes.items(.type_def)[dot_node] = property_type; } else if (try self.match(.LeftParen)) { // If it's a method or placeholder we can call it diff --git a/src/lib/buzz_fs.zig b/src/lib/buzz_fs.zig index 7682fe7b..cf235d27 100644 --- a/src/lib/buzz_fs.zig +++ b/src/lib/buzz_fs.zig @@ -3,6 +3,7 @@ const api = @import("buzz_api.zig"); fn handleMakeDirectoryError(ctx: *api.NativeCtx, err: anytype) void { switch (err) { + error.InvalidWtf8, error.AccessDenied, error.BadPathName, error.DiskQuota, @@ -92,6 +93,7 @@ pub export fn delete(ctx: *api.NativeCtx) c_int { fn handleMoveError(ctx: *api.NativeCtx, err: anytype) void { switch (err) { + error.InvalidWtf8, error.AccessDenied, error.AntivirusInterference, error.BadPathName, @@ -121,6 +123,7 @@ fn handleMoveError(ctx: *api.NativeCtx, err: anytype) void { fn handleRealpathError(ctx: *api.NativeCtx, err: anytype) void { switch (err) { + error.InvalidWtf8, error.AccessDenied, error.UnrecognizedVolume, error.AntivirusInterference, @@ -129,8 +132,6 @@ fn handleRealpathError(ctx: *api.NativeCtx, err: anytype) void { error.FileSystem, error.FileTooBig, error.InputOutput, - error.InvalidHandle, - error.InvalidUtf8, error.IsDir, error.NameTooLong, error.NoDevice, @@ -209,6 +210,7 @@ pub export fn move(ctx: *api.NativeCtx) c_int { fn handleOpenDirAbsoluteError(ctx: *api.NativeCtx, err: anytype) void { switch (err) { + error.InvalidWtf8, error.AccessDenied, error.AntivirusInterference, error.BadPathName, @@ -217,7 +219,6 @@ fn handleOpenDirAbsoluteError(ctx: *api.NativeCtx, err: anytype) void { error.FileLocksNotSupported, error.FileNotFound, error.FileTooBig, - error.InvalidHandle, error.InvalidUtf8, error.IsDir, error.NameTooLong, @@ -241,10 +242,10 @@ fn handleOpenDirAbsoluteError(ctx: *api.NativeCtx, err: anytype) void { fn handleOpenDirError(ctx: *api.NativeCtx, err: anytype) void { switch (err) { + error.InvalidWtf8, error.AccessDenied, error.BadPathName, error.DeviceBusy, - error.InvalidHandle, error.InvalidUtf8, error.NameTooLong, error.NoDevice, @@ -265,6 +266,7 @@ fn handleDirIterateError(ctx: *api.NativeCtx, err: anytype) void { switch (err) { error.AccessDenied, error.SystemResources, + error.InvalidUtf8, => ctx.vm.pushErrorEnum("errors.FileSystemError", @errorName(err)), error.Unexpected => ctx.vm.pushError("errors.UnexpectedError", null), diff --git a/src/lib/buzz_http.zig b/src/lib/buzz_http.zig index 014107da..01311f4c 100644 --- a/src/lib/buzz_http.zig +++ b/src/lib/buzz_http.zig @@ -9,7 +9,7 @@ pub export fn HttpClientNew(ctx: *api.NativeCtx) c_int { .allocator = api.VM.allocator, }; - client.loadDefaultProxies() catch @panic("Out of memory"); + client.initDefaultProxies(api.VM.allocator) catch @panic("Out of memory"); if (api.ObjUserData.bz_newUserData(ctx.vm, @intFromPtr(client))) |userdata| { ctx.vm.bz_pushUserData(userdata); @@ -44,7 +44,7 @@ pub export fn HttpClientSend(ctx: *api.NativeCtx) c_int { } const header_values = ctx.vm.bz_peek(0); - var headers = http.Headers.init(api.VM.allocator); + var headers = std.ArrayList(http.Header).init(api.VM.allocator); var next_header_key = api.Value.Null; var next_header_value = api.ObjMap.bz_mapNext(ctx.vm, header_values, &next_header_key); while (next_header_key.val != api.Value.Null.val) : (next_header_value = api.ObjMap.bz_mapNext(ctx.vm, header_values, &next_header_key)) { @@ -57,10 +57,16 @@ pub export fn HttpClientSend(ctx: *api.NativeCtx) c_int { @panic("Out of memory"); } - headers.append(key.?[0..key_len], value.?[0..value_len]) catch @panic("Could not send request"); + headers.append( + .{ + .name = key.?[0..key_len], + .value = value.?[0..value_len], + }, + ) catch @panic("Could not send request"); } const request = api.VM.allocator.create(http.Client.Request) catch @panic("Out of memory"); + const server_header_buffer = api.VM.allocator.alloc(u8, 1024) catch @panic("Out of memory"); // FIXME: what do i do we this?? request.* = client.open( method, @@ -69,8 +75,10 @@ pub export fn HttpClientSend(ctx: *api.NativeCtx) c_int { return -1; }, - headers, - .{}, + .{ + .extra_headers = headers.items, + .server_header_buffer = server_header_buffer, + }, ) catch |err| { handleError(ctx, err); @@ -190,7 +198,8 @@ pub export fn HttpRequestRead(ctx: *api.NativeCtx) c_int { headers, ); - for (request.response.headers.list.items) |header| { + var header_it = request.response.iterateHeaders(); + while (header_it.next()) |header| { api.ObjMap.bz_mapSet( ctx.vm, headers, @@ -220,10 +229,9 @@ fn handleWaitError(ctx: *api.NativeCtx, err: anytype) void { switch (err) { error.OutOfMemory => @panic("Out of memory"), - error.RedirectRequiresResend, error.CertificateBundleLoadFailure, error.CompressionInitializationFailed, - error.CompressionNotSupported, + error.CompressionUnsupported, error.ConnectionRefused, error.ConnectionResetByPeer, error.ConnectionTimedOut, @@ -232,23 +240,22 @@ fn handleWaitError(ctx: *api.NativeCtx, err: anytype) void { error.HttpChunkInvalid, error.HttpConnectionHeaderUnsupported, error.HttpHeaderContinuationsUnsupported, - error.HttpHeadersExceededSizeLimit, error.HttpHeadersInvalid, - error.HttpRedirectMissingLocation, + error.HttpHeadersOversize, + error.HttpRedirectLocationInvalid, + error.HttpRedirectLocationMissing, error.HttpTransferEncodingUnsupported, error.InvalidCharacter, error.InvalidContentLength, - error.InvalidFormat, - error.InvalidPort, error.NameServerFailure, error.NetworkUnreachable, error.Overflow, + error.RedirectRequiresResend, error.TemporaryNameServerFailure, error.TlsAlert, error.TlsFailure, error.TlsInitializationFailed, error.TooManyHttpRedirects, - error.UnexpectedCharacter, error.UnexpectedConnectFailure, error.UnexpectedReadFailure, error.UnexpectedWriteFailure, @@ -307,10 +314,10 @@ fn handleResponseError(ctx: *api.NativeCtx, err: anytype) void { error.UnexpectedReadFailure, error.EndOfStream, error.HttpChunkInvalid, - error.HttpHeadersExceededSizeLimit, error.DecompressionFailure, error.InvalidTrailers, error.StreamTooLong, + error.HttpHeadersOversize, => ctx.vm.pushErrorEnum("http.HttpError", @errorName(err)), } } diff --git a/src/lib/buzz_io.zig b/src/lib/buzz_io.zig index 0faf822b..379ff10b 100644 --- a/src/lib/buzz_io.zig +++ b/src/lib/buzz_io.zig @@ -38,7 +38,7 @@ fn handleFileOpenError(ctx: *api.NativeCtx, err: anytype) void { error.FileBusy, error.FileLocksNotSupported, error.FileTooBig, - error.InvalidHandle, + error.InvalidWtf8, error.InvalidUtf8, error.IsDir, error.NameTooLong, @@ -125,7 +125,6 @@ fn handleFileReadWriteError(ctx: *api.NativeCtx, err: anytype) void { error.ConnectionResetByPeer, error.ConnectionTimedOut, error.NotOpenForReading, - error.NetNameDeleted, => ctx.vm.pushErrorEnum("errors.ReadWriteError", @errorName(err)), error.Unexpected => ctx.vm.pushError("errors.UnexpectedError", null), @@ -176,7 +175,6 @@ fn handleFileReadLineError(ctx: *api.NativeCtx, err: anytype) void { error.BrokenPipe, error.ConnectionResetByPeer, error.ConnectionTimedOut, - error.NetNameDeleted, error.NotOpenForReading, error.OperationAborted, error.StreamTooLong, @@ -242,7 +240,6 @@ fn handleFileReadAllError(ctx: *api.NativeCtx, err: anytype) void { error.ConnectionResetByPeer, error.ConnectionTimedOut, error.NotOpenForReading, - error.NetNameDeleted, => ctx.vm.pushErrorEnum("errors.ReadWriteError", @errorName(err)), error.Unexpected => ctx.vm.pushError("errors.UnexpectedError", null), diff --git a/src/lib/buzz_os.zig b/src/lib/buzz_os.zig index 16e82c13..00574173 100644 --- a/src/lib/buzz_os.zig +++ b/src/lib/buzz_os.zig @@ -109,6 +109,7 @@ fn handleSpawnError(ctx: *api.NativeCtx, err: anytype) void { error.FileSystem, error.InvalidHandle, error.InvalidUtf8, + error.InvalidWtf8, error.IsDir, error.NameTooLong, error.NoDevice, @@ -222,7 +223,6 @@ fn handleConnectError(ctx: *api.NativeCtx, err: anytype) void { error.FileSystem, error.FileTooBig, error.InputOutput, - error.InvalidHandle, error.InvalidUtf8, error.IsDir, error.NameTooLong, @@ -235,12 +235,12 @@ fn handleConnectError(ctx: *api.NativeCtx, err: anytype) void { error.ReadOnlyFileSystem, error.SharingViolation, error.SymLinkLoop, + error.InvalidWtf8, error.NetworkNotFound, error.SocketNotConnected, => ctx.vm.pushErrorEnum("errors.FileSystemError", @errorName(err)), error.BrokenPipe, - error.NetNameDeleted, error.NotOpenForReading, error.OperationAborted, => ctx.vm.pushErrorEnum("errors.ReadWriteError", @errorName(err)), @@ -336,7 +336,7 @@ pub export fn SocketClose(ctx: *api.NativeCtx) c_int { ctx.vm.bz_peek(0).integer(), ); - std.os.closeSocket(socket); + std.os.shutdown(socket, .both) catch @panic("Could not stop socket"); return 0; } @@ -353,7 +353,6 @@ fn handleReadAllError(ctx: *api.NativeCtx, err: anytype) void { error.OperationAborted, error.NotOpenForReading, error.ConnectionTimedOut, - error.NetNameDeleted, // error.StreamTooLong, => ctx.vm.pushErrorEnum("errors.ReadWriteError", @errorName(err)), @@ -421,7 +420,6 @@ fn handleReadLineError(ctx: *api.NativeCtx, err: anytype) void { error.BrokenPipe, error.ConnectionResetByPeer, error.ConnectionTimedOut, - error.NetNameDeleted, error.NotOpenForReading, error.OperationAborted, error.StreamTooLong, @@ -579,92 +577,21 @@ pub export fn SocketServerStart(ctx: *api.NativeCtx) c_int { const reuse_address: bool = ctx.vm.bz_peek(1).boolean(); const reuse_port: bool = ctx.vm.bz_peek(0).boolean(); - var server = std.net.StreamServer.init(.{ - .reuse_address = reuse_address, - .reuse_port = reuse_port, - }); - - const list = std.net.getAddressList(api.VM.allocator, address, @as(u16, @intCast(port.?))) catch |err| { - switch (err) { - error.ServiceUnavailable, - error.UnknownHostName, - error.NameServerFailure, - error.TemporaryNameServerFailure, - error.HostLacksNetworkAddresses, - error.AddressFamilyNotSupported, - error.AddressInUse, - error.AddressNotAvailable, - error.AlreadyBound, - error.AlreadyConnected, - error.ConnectionResetByPeer, - error.ConnectionTimedOut, - error.FileDescriptorNotASocket, - error.Incomplete, - error.InterfaceNotFound, - error.InvalidCharacter, - error.InvalidEnd, - error.InvalidIPAddressFormat, - error.InvalidIpv4Mapping, - error.InvalidProtocolOption, - error.NetworkSubsystemFailed, - error.NonCanonical, - error.PermissionDenied, - error.ProtocolFamilyNotAvailable, - error.ProtocolNotSupported, - error.SocketNotBound, - error.SocketTypeNotSupported, - error.SystemFdQuotaExceeded, - error.TimeoutTooBig, - => ctx.vm.pushErrorEnum("errors.SocketError", @errorName(err)), - error.AccessDenied, - error.BadPathName, - error.DeviceBusy, - error.FileBusy, - error.FileLocksNotSupported, - error.FileNotFound, - error.FileSystem, - error.FileTooBig, - error.InputOutput, - error.InvalidHandle, - error.InvalidUtf8, - error.IsDir, - error.NameTooLong, - error.NoDevice, - error.NoSpaceLeft, - error.NotDir, - error.PathAlreadyExists, - error.PipeBusy, - error.ProcessFdQuotaExceeded, - error.ReadOnlyFileSystem, - error.SharingViolation, - error.SymLinkLoop, - error.SystemResources, - error.AntivirusInterference, - error.WouldBlock, - error.NetworkNotFound, - error.SocketNotConnected, - => ctx.vm.pushErrorEnum("errors.FileSystemError", @errorName(err)), - error.Unexpected => ctx.vm.pushError("errors.UnexpectedError", null), - error.OutOfMemory => @panic("Out of memory"), - error.Overflow => ctx.vm.pushError("errors.OverflowError", null), - error.BrokenPipe, - error.NetNameDeleted, - error.NotOpenForReading, - error.OperationAborted, - => ctx.vm.pushErrorEnum("errors.ReadWriteError", @errorName(err)), - } + const resolved_address = std.net.Address.parseIp( + address, + @intCast(port.?), + ) catch { + ctx.vm.pushError("errors.InvalidArgumentError", null); return -1; }; - defer list.deinit(); - if (list.addrs.len == 0) { - ctx.vm.pushErrorEnum("errors.SocketError", "AddressNotResolved"); - - return -1; - } - - server.listen(list.addrs[0]) catch |err| { + const server = resolved_address.listen( + .{ + .reuse_address = reuse_address, + .reuse_port = reuse_port, + }, + ) catch |err| { switch (err) { error.NoDevice, error.SymLinkLoop, @@ -699,26 +626,19 @@ pub export fn SocketServerStart(ctx: *api.NativeCtx) c_int { return -1; }; - ctx.vm.bz_pushInteger(@intCast(server.sockfd.?)); + ctx.vm.bz_pushInteger(@intCast(server.stream.handle)); return 1; } pub export fn SocketServerAccept(ctx: *api.NativeCtx) c_int { - const server_socket: std.os.socket_t = @intCast( - ctx.vm.bz_peek(2).integer(), - ); - const reuse_address: bool = ctx.vm.bz_peek(1).boolean(); - const reuse_port: bool = ctx.vm.bz_peek(0).boolean(); - - const default_options = std.net.StreamServer.Options{}; - var server = std.net.StreamServer{ - .sockfd = server_socket, - .kernel_backlog = default_options.kernel_backlog, - .reuse_address = reuse_address, - .reuse_port = reuse_port, - .listen_address = undefined, - .force_nonblocking = false, + var server = std.net.Server{ + .listen_address = undefined, // FIXME: we lose this + .stream = std.net.Stream{ + .handle = @intCast( + ctx.vm.bz_peek(0).integer(), + ), + }, }; const connection = server.accept() catch |err| { diff --git a/src/lib/errors.buzz b/src/lib/errors.buzz index 78978c51..b31e4431 100644 --- a/src/lib/errors.buzz +++ b/src/lib/errors.buzz @@ -12,6 +12,7 @@ export enum FileSystemError { InputOutput, InvalidHandle, InvalidUtf8, + InvalidWtf8, IsDir, LinkQuotaExceeded, NameTooLong, @@ -32,8 +33,8 @@ export enum FileSystemError { SymLinkLoop, SystemFdQuotaExceeded, SystemResources, - WouldBlock, UnrecognizedVolume, + WouldBlock, } export enum ExecError { diff --git a/src/lib/http.buzz b/src/lib/http.buzz index a2f1e521..20876346 100644 --- a/src/lib/http.buzz +++ b/src/lib/http.buzz @@ -7,7 +7,7 @@ import "errors"; export enum HttpError { CertificateBundleLoadFailure, CompressionInitializationFailed, - CompressionNotSupported, + CompressionUnsupported, ConnectionRefused, ConnectionResetByPeer, ConnectionTimedOut, @@ -17,9 +17,10 @@ export enum HttpError { HttpChunkInvalid, HttpConnectionHeaderUnsupported, HttpHeaderContinuationsUnsupported, - HttpHeadersExceededSizeLimit, HttpHeadersInvalid, - HttpRedirectMissingLocation, + HttpHeadersOversize + HttpRedirectLocationInvalid, + HttpRedirectLocationMissing, HttpTransferEncodingUnsupported, InvalidCharacter, InvalidContentLength, diff --git a/src/lib/os.buzz b/src/lib/os.buzz index 71fe75ae..1b35a348 100644 --- a/src/lib/os.buzz +++ b/src/lib/os.buzz @@ -39,7 +39,7 @@ extern fun SocketWrite(int fd, str bytes) > void !> FileSystemError, ReadWriteEr || @private extern fun SocketServerStart(str address, int port, bool reuseAddr, bool reusePort) > int !> InvalidArgumentError, SocketError, UnexpectedError, FileSystemError; || @private -extern fun SocketServerAccept(int fd, bool reuseAddr, bool reusePort) > int !> SocketError, UnexpectedError; +extern fun SocketServerAccept(int fd) > int !> SocketError, UnexpectedError; || @private extern fun SocketReadLine(int fd, int? maxSize) > str? !> FileSystemError, UnexpectedError, ReadWriteError; || @private @@ -128,7 +128,7 @@ export object TcpServer { || @return Socket opened with the client fun accept() > Socket !> SocketError, UnexpectedError { return Socket{ - fd = SocketServerAccept(this.serverSocket.fd, reuseAddr: this.reuseAddr, reusePort: this.reusePort), + fd = SocketServerAccept(this.serverSocket.fd), }; } diff --git a/src/vm.zig b/src/vm.zig index d6ddf6d0..a6926ec3 100644 --- a/src/vm.zig +++ b/src/vm.zig @@ -770,7 +770,7 @@ pub const VM = struct { ); } - inline fn panic(e: anytype) void { + fn vmPanic(e: anytype) void { std.debug.print("{}\n", .{e}); if (!is_wasm) { std.os.exit(1); @@ -881,7 +881,7 @@ pub const VM = struct { fn OP_CLONE(self: *Self, _: *CallFrame, _: u32, _: OpCode, _: u24) void { self.clone() catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -918,7 +918,7 @@ pub const VM = struct { fn OP_DEFINE_GLOBAL(self: *Self, _: *CallFrame, _: u32, _: OpCode, arg: u24) void { self.globals.ensureTotalCapacity(arg + 1) catch |e| { - panic(e); + vmPanic(e); unreachable; }; self.globals.expandToCapacity(); @@ -1061,13 +1061,13 @@ pub const VM = struct { fn OP_TO_STRING(self: *Self, _: *CallFrame, _: u32, _: OpCode, _: u24) void { const str = self.pop().toStringAlloc(self.gc.allocator) catch |e| { - panic(e); + vmPanic(e); unreachable; }; self.push( Value.fromObj( (self.gc.copyString(str.items) catch |e| { - panic(e); + vmPanic(e); unreachable; }).toObj(), ), @@ -1116,11 +1116,11 @@ pub const VM = struct { var closure: *ObjClosure = self.gc.allocateObject( ObjClosure, ObjClosure.init(self.gc.allocator, self, function) catch |e| { - panic(e); + vmPanic(e); unreachable; }, ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1133,15 +1133,15 @@ pub const VM = struct { if (is_local) { closure.upvalues.append(self.captureUpvalue(&(current_frame.slots[index])) catch |e| { - panic(e); + vmPanic(e); unreachable; }) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } else { closure.upvalues.append(current_frame.closure.upvalues.items[index]) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } @@ -1188,7 +1188,7 @@ pub const VM = struct { const stack_slice = stack_ptr[0..stack_len]; var fiber = self.gc.allocator.create(Fiber) catch |e| { - panic(e); + vmPanic(e); unreachable; }; fiber.* = Fiber.init( @@ -1201,7 +1201,7 @@ pub const VM = struct { catch_count > 0, null, ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1214,7 +1214,7 @@ pub const VM = struct { var obj_fiber = self.gc.allocateObject(ObjFiber, ObjFiber{ .fiber = fiber, }) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1245,7 +1245,7 @@ pub const VM = struct { const stack_slice = stack_ptr[0..stack_len]; var fiber = self.gc.allocator.create(Fiber) catch |e| { - panic(e); + vmPanic(e); unreachable; }; fiber.* = Fiber.init( @@ -1258,7 +1258,7 @@ pub const VM = struct { catch_count > 0, method, ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1271,7 +1271,7 @@ pub const VM = struct { var obj_fiber = self.gc.allocateObject(ObjFiber, ObjFiber{ .fiber = fiber, }) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1294,7 +1294,7 @@ pub const VM = struct { fn OP_RESUME(self: *Self, _: *CallFrame, _: u32, _: OpCode, _: u24) void { const obj_fiber = self.pop().obj().access(ObjFiber, .Fiber, self.gc).?; obj_fiber.fiber.@"resume"(self) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1315,7 +1315,7 @@ pub const VM = struct { fn OP_RESOLVE(self: *Self, _: *CallFrame, _: u32, _: OpCode, _: u24) void { const obj_fiber = self.pop().obj().access(ObjFiber, .Fiber, self.gc).?; obj_fiber.fiber.resolve_(self) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1363,7 +1363,7 @@ pub const VM = struct { catch_value, false, ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1394,7 +1394,7 @@ pub const VM = struct { catch_value, false, ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1427,7 +1427,7 @@ pub const VM = struct { (self.current_fiber.stack_top - arg_count - 1)[0] = field; self.callValue(field, arg_count, catch_value, false) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } else { @@ -1439,7 +1439,7 @@ pub const VM = struct { false, false, ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } @@ -1473,7 +1473,7 @@ pub const VM = struct { (self.current_fiber.stack_top - arg_count - 1)[0] = field; self.tailCall(field, arg_count, catch_value, false) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } else { @@ -1485,7 +1485,7 @@ pub const VM = struct { false, true, ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } @@ -1512,14 +1512,14 @@ pub const VM = struct { const catch_value = if (catch_count > 0) self.pop() else null; const member = (ObjString.member(self, method) catch |e| { - panic(e); + vmPanic(e); unreachable; }).?; const member_value: Value = member.toValue(); (self.current_fiber.stack_top - arg_count - 1)[0] = member_value; self.callValue(member_value, arg_count, catch_value, false) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1545,14 +1545,14 @@ pub const VM = struct { const catch_value = if (catch_count > 0) self.pop() else null; const member = (ObjPattern.member(self, method) catch |e| { - panic(e); + vmPanic(e); unreachable; }).?; const member_value: Value = member.toValue(); (self.current_fiber.stack_top - arg_count - 1)[0] = member_value; self.callValue(member_value, arg_count, catch_value, false) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1578,13 +1578,13 @@ pub const VM = struct { const catch_value = if (catch_count > 0) self.pop() else null; const member = (ObjFiber.member(self, method) catch |e| { - panic(e); + vmPanic(e); unreachable; }).?; const member_value: Value = member.toValue(); (self.current_fiber.stack_top - arg_count - 1)[0] = member_value; self.callValue(member_value, arg_count, catch_value, false) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1611,14 +1611,14 @@ pub const VM = struct { const list = self.peek(arg_count).obj().access(ObjList, .List, self.gc).?; const member = (list.member(self, method) catch |e| { - panic(e); + vmPanic(e); unreachable; }).?; const member_value: Value = member.toValue(); (self.current_fiber.stack_top - arg_count - 1)[0] = member_value; self.callValue(member_value, arg_count, catch_value, false) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1645,14 +1645,14 @@ pub const VM = struct { const map = self.peek(arg_count).obj().access(ObjMap, .Map, self.gc).?; const member = (map.member(self, method) catch |e| { - panic(e); + vmPanic(e); unreachable; }).?; const member_value: Value = member.toValue(); (self.current_fiber.stack_top - arg_count - 1)[0] = member_value; self.callValue(member_value, arg_count, catch_value, false) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1686,7 +1686,7 @@ pub const VM = struct { // We're in a fiber if (self.current_fiber.parent_fiber != null) { self.current_fiber.finish(self, result) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1770,18 +1770,18 @@ pub const VM = struct { if (self.import_registry.get(fullpath)) |globals| { for (globals.items) |global| { self.globals.append(global) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } } else { var vm = self.gc.allocator.create(VM) catch |e| { - panic(e); + vmPanic(e); unreachable; }; // FIXME: give reference to JIT? vm.* = VM.init(self.gc, self.import_registry, self.flavor) catch |e| { - panic(e); + vmPanic(e); unreachable; }; // TODO: how to free this since we copy things to new vm, also fails anyway @@ -1791,7 +1791,7 @@ pub const VM = struct { // } vm.interpret(self.current_ast, closure.function, null) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1805,18 +1805,18 @@ pub const VM = struct { while (i > 0) : (i -= 1) { const global = vm.peek(@intCast(i)); self.globals.append(global) catch |e| { - panic(e); + vmPanic(e); unreachable; }; import_cache.append(global) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } } self.import_registry.put(fullpath, import_cache) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } @@ -1883,7 +1883,7 @@ pub const VM = struct { null, null, ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1906,7 +1906,7 @@ pub const VM = struct { ObjList, ObjList.init(self.gc.allocator, self.readConstant(arg).obj().access(ObjTypeDef, .Type, self.gc).?), ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1941,7 +1941,7 @@ pub const VM = struct { ) catch @panic("Could not instanciate list"), ), ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -1978,7 +1978,7 @@ pub const VM = struct { const list_value = self.peek(0); list.rawAppend(self.gc, list_value) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -2003,7 +2003,7 @@ pub const VM = struct { self.gc.allocator, self.readConstant(arg).obj().access(ObjTypeDef, .Type, self.gc).?, )) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -2029,7 +2029,7 @@ pub const VM = struct { const value = self.peek(0); map.set(self.gc, key, value) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -2058,13 +2058,13 @@ pub const VM = struct { self.throw( Error.OutOfBound, (self.gc.copyString("Out of bound list access.") catch |e| { - panic(e); + vmPanic(e); unreachable; }).toValue(), null, null, ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } @@ -2075,13 +2075,13 @@ pub const VM = struct { self.throw( Error.OutOfBound, (self.gc.copyString("Out of bound list access.") catch |e| { - panic(e); + vmPanic(e); unreachable; }).toValue(), null, null, ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -2148,13 +2148,13 @@ pub const VM = struct { self.throw( Error.OutOfBound, (self.gc.copyString("Out of bound string access.") catch |e| { - panic(e); + vmPanic(e); unreachable; }).toValue(), null, null, ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } @@ -2163,7 +2163,7 @@ pub const VM = struct { if (str_index < str.string.len) { const str_item = (self.gc.copyString(&([_]u8{str.string[str_index]})) catch |e| { - panic(e); + vmPanic(e); unreachable; }).toValue(); @@ -2177,13 +2177,13 @@ pub const VM = struct { self.throw( Error.OutOfBound, (self.gc.copyString("Out of bound str access.") catch |e| { - panic(e); + vmPanic(e); unreachable; }).toValue(), null, null, ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } @@ -2211,13 +2211,13 @@ pub const VM = struct { self.throw( Error.OutOfBound, (self.gc.copyString("Out of bound list access.") catch |e| { - panic(e); + vmPanic(e); unreachable; }).toValue(), null, null, ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } @@ -2226,7 +2226,7 @@ pub const VM = struct { if (list_index < list.items.items.len) { list.set(self.gc, list_index, value) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -2241,13 +2241,13 @@ pub const VM = struct { self.throw( Error.OutOfBound, (self.gc.copyString("Out of bound list access.") catch |e| { - panic(e); + vmPanic(e); unreachable; }).toValue(), null, null, ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } @@ -2272,7 +2272,7 @@ pub const VM = struct { const value = self.peek(0); map.set(self.gc, index, value) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -2310,7 +2310,7 @@ pub const VM = struct { .case = @intCast(arg), }, ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -2361,7 +2361,7 @@ pub const VM = struct { .enum_ref = enum_, .case = @intCast(index), }) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -2399,7 +2399,7 @@ pub const VM = struct { self.readConstant(@as(u24, @intCast(self.readInstruction()))).obj().access(ObjTypeDef, .Type, self.gc).?, ), ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -2471,11 +2471,11 @@ pub const VM = struct { self.gc, kv.key_ptr.*, self.cloneValue(kv.value_ptr.*) catch |e| { - panic(e); + vmPanic(e); unreachable; }, ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } @@ -2506,7 +2506,7 @@ pub const VM = struct { name, method.obj().access(ObjClosure, .Closure, self.gc).?, ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -2533,13 +2533,13 @@ pub const VM = struct { if (object.type_def.resolved_type.?.Object.fields.contains(name.string)) { object.setField(self.gc, name, property) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } else { assert(object.type_def.resolved_type.?.Object.static_fields.contains(name.string)); object.setStaticField(self.gc, name, property) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } @@ -2619,7 +2619,7 @@ pub const VM = struct { } else if (obj_instance.object) |object| { if (object.methods.get(name)) |method| { self.bindMethod(method, null) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } else { @@ -2646,11 +2646,11 @@ pub const VM = struct { const name: *ObjString = self.readString(arg); if (list.member(self, name) catch |e| { - panic(e); + vmPanic(e); unreachable; }) |member| { self.bindMethod(null, member) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } else { @@ -2675,11 +2675,11 @@ pub const VM = struct { const name: *ObjString = self.readString(arg); if (map.member(self, name) catch |e| { - panic(e); + vmPanic(e); unreachable; }) |member| { self.bindMethod(null, member) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } else { @@ -2704,11 +2704,11 @@ pub const VM = struct { const name: *ObjString = self.readString(arg); if (ObjString.member(self, name) catch |e| { - panic(e); + vmPanic(e); unreachable; }) |member| { self.bindMethod(null, member) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } else { @@ -2733,11 +2733,11 @@ pub const VM = struct { const name: *ObjString = self.readString(arg); if (ObjPattern.member(self, name) catch |e| { - panic(e); + vmPanic(e); unreachable; }) |member| { self.bindMethod(null, member) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } else { @@ -2762,11 +2762,11 @@ pub const VM = struct { const name: *ObjString = self.readString(arg); if (ObjFiber.member(self, name) catch |e| { - panic(e); + vmPanic(e); unreachable; }) |member| { self.bindMethod(null, member) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } @@ -2791,7 +2791,7 @@ pub const VM = struct { // Set new value object.setStaticField(self.gc, name, self.peek(0)) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -2998,7 +2998,7 @@ pub const VM = struct { const left: *ObjString = self.pop().obj().access(ObjString, .String, self.gc).?; self.push(Value.fromObj((left.concat(self, right) catch |e| { - panic(e); + vmPanic(e); unreachable; }).toObj())); @@ -3022,11 +3022,11 @@ pub const VM = struct { var new_list = std.ArrayList(Value).init(self.gc.allocator); new_list.appendSlice(left.items.items) catch |e| { - panic(e); + vmPanic(e); unreachable; }; new_list.appendSlice(right.items.items) catch |e| { - panic(e); + vmPanic(e); unreachable; }; @@ -3036,7 +3036,7 @@ pub const VM = struct { .methods = left.methods, .items = new_list, }) catch |e| { - panic(e); + vmPanic(e); unreachable; }).toValue(), ); @@ -3060,13 +3060,13 @@ pub const VM = struct { const left: *ObjMap = self.pop().obj().access(ObjMap, .Map, self.gc).?; var new_map = left.map.clone() catch |e| { - panic(e); + vmPanic(e); unreachable; }; var it = right.map.iterator(); while (it.next()) |entry| { new_map.put(entry.key_ptr.*, entry.value_ptr.*) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } @@ -3077,7 +3077,7 @@ pub const VM = struct { .methods = left.methods, .map = new_map, }) catch |e| { - panic(e); + vmPanic(e); unreachable; }).toValue(), ); @@ -3520,7 +3520,7 @@ pub const VM = struct { const str: *ObjString = self.peek(0).obj().access(ObjString, .String, self.gc).?; key_slot.* = if (str.next(self, if (key_slot.*.isNull()) null else key_slot.integer()) catch |e| { - panic(e); + vmPanic(e); unreachable; }) |new_index| Value.fromInteger(new_index) @@ -3530,7 +3530,7 @@ pub const VM = struct { // Set new value if (key_slot.*.isInteger()) { value_slot.* = (self.gc.copyString(&([_]u8{str.string[@as(usize, @intCast(key_slot.integer()))]})) catch |e| { - panic(e); + vmPanic(e); unreachable; }).toValue(); } @@ -3559,7 +3559,7 @@ pub const VM = struct { self, if (key_slot.*.isNull()) null else key_slot.integer(), ) catch |e| { - panic(e); + vmPanic(e); unreachable; }) |new_index| Value.fromInteger(new_index) @@ -3595,7 +3595,7 @@ pub const VM = struct { // Get next enum case const next_case = enum_.rawNext(self, enum_case) catch |e| { - panic(e); + vmPanic(e); unreachable; }; value_slot.* = (if (next_case) |new_case| Value.fromObj(new_case.toObj()) else Value.Null); @@ -3649,7 +3649,7 @@ pub const VM = struct { value_slot.* = Value.Null; } else { fiber.fiber.@"resume"(self) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } @@ -3674,13 +3674,13 @@ pub const VM = struct { self.throw( Error.UnwrappedNull, (self.gc.copyString("Force unwrapped optional is null") catch |e| { - panic(e); + vmPanic(e); unreachable; }).toValue(), null, null, ) catch |e| { - panic(e); + vmPanic(e); unreachable; }; } diff --git a/src/wasm.zig b/src/wasm.zig index 78dd8540..96af9f5b 100644 --- a/src/wasm.zig +++ b/src/wasm.zig @@ -7,17 +7,17 @@ extern fn readFromStdin(buffer_ptr: [*]const u8, buffer_length: usize) isize; pub const system = struct { var errno: E = undefined; - pub const E = std.os.wasi.E; + pub const E = std.os.emscripten.E; pub fn getErrno(rc: anytype) E { return if (rc == -1) errno else .SUCCESS; } - pub const fd_t = std.os.wasi.fd_t; + pub const fd_t = std.os.emscripten.fd_t; - pub const STDERR_FILENO = std.os.wasi.STDERR_FILENO; - pub const STDOUT_FILENO = std.os.wasi.STDOUT_FILENO; - pub const STDIN_FILENO = std.os.wasi.STDIN_FILENO; + pub const STDERR_FILENO = std.os.emscripten.STDERR_FILENO; + pub const STDOUT_FILENO = std.os.emscripten.STDOUT_FILENO; + pub const STDIN_FILENO = std.os.emscripten.STDIN_FILENO; pub fn write(fd: i32, buf: [*]const u8, count: usize) isize { // We only support writing to stderr or stdout @@ -47,31 +47,3 @@ pub const system = struct { ); } }; - -// Find a way to share BuildOptions between exe and buzz_api -pub const BuildOptions = .{ - .version = "0.4.0", - .sha = "aaaaaaa", - .mimalloc = false, - .cycle_limit = @as(?u128, null), - .recursive_call_limit = @as(?u32, null), - .stack_size = @as(usize, 100_000), - .debug = false, - .debug_stack = false, - .debug_current_instruction = false, - .show_perf = false, - .stop_on_report = false, - .debug_placeholders = false, - .gc_debug = false, - .gc_debug_light = false, - .gc_debug_access = false, - .gc = true, - .initial_gc = @as(usize, 1), - .next_gc_ratio = @as(usize, 2), - .next_full_gc_ratio = @as(usize, 4), - .memory_limit = @as(?usize, null), - .jit_debug = false, - .jit_always_on = false, - .jit = false, - .jit_prof_threshold = @as(f128, 5.0e-02), -}; diff --git a/src/wasm_repl.zig b/src/wasm_repl.zig index daf473eb..876e6f4c 100644 --- a/src/wasm_repl.zig +++ b/src/wasm_repl.zig @@ -4,7 +4,7 @@ const _vm = @import("vm.zig"); const VM = _vm.VM; const ImportRegistry = _vm.ImportRegistry; const wasm = @import("wasm.zig"); -const BuildOptions = wasm.BuildOptions; +const BuildOptions = @import("build_options"); const _mem = @import("memory.zig"); const GarbageCollector = _mem.GarbageCollector; const TypeRegistry = _mem.TypeRegistry;