From 82dba02040af72cc22ad1556387eef69bfea55a1 Mon Sep 17 00:00:00 2001 From: fubark Date: Tue, 26 Sep 2023 13:24:51 -0400 Subject: [PATCH] Move more builtins to DSL. Reduce embedded lib size. --- build.zig | 4 +++ src/builtins/bindings.zig | 63 ++++----------------------------------- src/builtins/builtins.cy | 54 +++++++++++++++++++++++++++++++++ src/builtins/builtins.zig | 56 ++++++++++++++++++++++++++++++++++ src/codegen.zig | 10 +++++-- src/cyber.zig | 1 + src/http.zig | 4 +-- src/include/cyber.h | 4 ++- src/parser.zig | 6 +++- src/sema.zig | 2 +- src/vm.zig | 6 ++-- test/behavior_test.zig | 9 ++++-- 12 files changed, 148 insertions(+), 71 deletions(-) diff --git a/build.zig b/build.zig index 8260b6e7a..1cd40a9f6 100644 --- a/build.zig +++ b/build.zig @@ -86,6 +86,7 @@ pub fn build(b: *std.build.Builder) !void { var opts = getDefaultOptions(target, optimize); opts.ffi = false; opts.malloc = .malloc; + opts.cli = false; opts.applyOverrides(); var lib: *std.build.Step.Compile = undefined; @@ -309,6 +310,7 @@ pub const Options = struct { static: bool, gc: bool, ffi: bool, + cli: bool, fn applyOverrides(self: *Options) void { if (optMalloc) |malloc| { @@ -347,6 +349,7 @@ fn getDefaultOptions(target: std.zig.CrossTarget, optimize: std.builtin.Optimize .malloc = malloc, .static = !target.getCpuArch().isWasm(), .ffi = !target.getCpuArch().isWasm(), + .cli = !target.getCpuArch().isWasm(), }; } @@ -376,6 +379,7 @@ fn createBuildOptions(b: *std.build.Builder, opts: Options) !*std.build.Step.Opt build_options.addOption(bool, "is32Bit", is32Bit(opts.target)); build_options.addOption(bool, "gc", opts.gc); build_options.addOption(bool, "ffi", opts.ffi); + build_options.addOption(bool, "cli", opts.cli); build_options.addOption([]const u8, "full_version", b.fmt("Cyber {s} build-{s}-{s}", .{Version, buildTag, commitTag})); return build_options; } diff --git a/src/builtins/bindings.zig b/src/builtins/bindings.zig index c7e160c9b..f452e8268 100644 --- a/src/builtins/bindings.zig +++ b/src/builtins/bindings.zig @@ -184,14 +184,12 @@ pub fn bindCore(self: *cy.VM) linksection(cy.InitSection) !void { self.pairIteratorMGID = try b.ensureMethodGroup("pairIterator"); const read = try b.ensureMethodGroup("read"); const readToEnd = try b.ensureMethodGroup("readToEnd"); - const remove = try b.ensureMethodGroup("remove"); const repeat = try b.ensureMethodGroup("repeat"); const replace = try b.ensureMethodGroup("replace"); const runeAt = try b.ensureMethodGroup("runeAt"); const seek = try b.ensureMethodGroup("seek"); const seekFromCur = try b.ensureMethodGroup("seekFromCur"); const seekFromEnd = try b.ensureMethodGroup("seekFromEnd"); - const size = try b.ensureMethodGroup("size"); const slice = try b.ensureMethodGroup("slice"); const sliceAt = try b.ensureMethodGroup("sliceAt"); const split = try b.ensureMethodGroup("split"); @@ -208,6 +206,8 @@ pub fn bindCore(self: *cy.VM) linksection(cy.InitSection) !void { const write = try b.ensureMethodGroup("write"); // Init compile time builtins. + var rsym: sema.Symbol = undefined; + var sb: ModuleBuilder = undefined; // Builtin types. var id = try self.addBuiltinType("none", bt.None); @@ -215,16 +215,9 @@ pub fn bindCore(self: *cy.VM) linksection(cy.InitSection) !void { id = try self.addBuiltinType("boolean", bt.Boolean); std.debug.assert(id == rt.BooleanT); - var rsym = self.compiler.sema.getSymbol(bt.Boolean); - var sb = ModuleBuilder.init(self.compiler, rsym.inner.builtinType.modId); - try sb.setFunc("$call", &.{ bt.Any }, bt.Boolean, booleanCall); id = try self.addBuiltinType("error", bt.Error); std.debug.assert(id == rt.ErrorT); - rsym = self.compiler.sema.getSymbol(bt.Error); - sb = ModuleBuilder.init(self.compiler, rsym.inner.builtinType.modId); - try sb.setFunc("$call", &.{ bt.Any }, bt.Error, errorCall); - try b.addMethod(rt.ErrorT, value, &.{ bt.Any }, bt.Any, errorValue); id = try self.addBuiltinType("StaticAstring", bt.String); std.debug.assert(id == rt.StaticAstringT); @@ -245,47 +238,9 @@ pub fn bindCore(self: *cy.VM) linksection(cy.InitSection) !void { id = try self.addBuiltinType("integer", bt.Integer); std.debug.assert(id == rt.IntegerT); - rsym = self.compiler.sema.getSymbol(bt.Integer); - sb = ModuleBuilder.init(self.compiler, rsym.inner.builtinType.modId); - try sb.setFunc("$call", &.{ bt.Any }, bt.Integer, integerCall); - try sb.addOptimizingMethod(rt.IntegerT, self.@"prefix~MGID", &.{ bt.Any }, bt.Integer, intBitwiseNot); - try sb.addOptimizingMethod(rt.IntegerT, self.@"prefix-MGID", &.{ bt.Any }, bt.Integer, intNeg); - // Inlined opcodes allow the right arg to be dynamic so the compiler can gen more of those. - // So for now, the runtime signature reflects that. - try sb.addOptimizingMethod(rt.IntegerT, self.@"infixMGID", &.{ bt.Any, bt.Any }, bt.Boolean, inlineBinOp(.greaterInt)); - try sb.addOptimizingMethod(rt.IntegerT, self.@"infix>=MGID", &.{ bt.Any, bt.Any }, bt.Boolean, inlineBinOp(.greaterEqualInt)); - try sb.addOptimizingMethod(rt.IntegerT, self.@"infix+MGID", &.{ bt.Any, bt.Any }, bt.Integer, inlineBinOp(.addInt)); - try sb.addOptimizingMethod(rt.IntegerT, self.@"infix+MGID", &.{ bt.Any, bt.Any }, bt.Integer, inlineBinOp(.addInt)); - try sb.addOptimizingMethod(rt.IntegerT, self.@"infix+MGID", &.{ bt.Any, bt.Any }, bt.Integer, inlineBinOp(.addInt)); - try sb.addOptimizingMethod(rt.IntegerT, self.@"infix-MGID", &.{ bt.Any, bt.Any }, bt.Integer, inlineBinOp(.subInt)); - try sb.addOptimizingMethod(rt.IntegerT, self.@"infix*MGID", &.{ bt.Any, bt.Any }, bt.Integer, inlineBinOp(.mulInt)); - try sb.addOptimizingMethod(rt.IntegerT, self.@"infix/MGID", &.{ bt.Any, bt.Any }, bt.Integer, inlineBinOp(.divInt)); - try sb.addOptimizingMethod(rt.IntegerT, self.@"infix%MGID", &.{ bt.Any, bt.Any }, bt.Integer, inlineBinOp(.modInt)); - try sb.addOptimizingMethod(rt.IntegerT, self.@"infix^MGID", &.{ bt.Any, bt.Any }, bt.Integer, inlineBinOp(.powInt)); - try sb.addOptimizingMethod(rt.IntegerT, self.@"infix&MGID", &.{ bt.Any, bt.Any }, bt.Integer, inlineBinOp(.bitwiseAnd)); - try sb.addOptimizingMethod(rt.IntegerT, self.@"infix|MGID", &.{ bt.Any, bt.Any }, bt.Integer, inlineBinOp(.bitwiseOr)); - try sb.addOptimizingMethod(rt.IntegerT, self.@"infix||MGID", &.{ bt.Any, bt.Any }, bt.Integer, inlineBinOp(.bitwiseXor)); - try sb.addOptimizingMethod(rt.IntegerT, self.@"infix<>MGID", &.{ bt.Any, bt.Any }, bt.Integer, inlineBinOp(.bitwiseRightShift)); id = try self.addBuiltinType("float", bt.Float); std.debug.assert(id == rt.FloatT); - rsym = self.compiler.sema.getSymbol(bt.Float); - sb = ModuleBuilder.init(self.compiler, rsym.inner.builtinType.modId); - try sb.setFunc("$call", &.{ bt.Any }, bt.Float, floatCall); - try sb.addOptimizingMethod(rt.FloatT, self.@"infixMGID", &.{ bt.Any, bt.Any }, bt.Boolean, inlineBinOp(.greaterFloat)); - try sb.addOptimizingMethod(rt.FloatT, self.@"infix<=MGID", &.{ bt.Any, bt.Any }, bt.Boolean, inlineBinOp(.greaterEqualFloat)); - try sb.addOptimizingMethod(rt.FloatT, self.@"prefix-MGID", &.{ bt.Any }, bt.Float, floatNeg); - try sb.addOptimizingMethod(rt.FloatT, self.@"infix+MGID", &.{ bt.Any, bt.Any }, bt.Float, inlineBinOp(.addFloat)); - try sb.addOptimizingMethod(rt.FloatT, self.@"infix-MGID", &.{ bt.Any, bt.Any }, bt.Float, inlineBinOp(.subFloat)); - try sb.addOptimizingMethod(rt.FloatT, self.@"infix*MGID", &.{ bt.Any, bt.Any }, bt.Float, inlineBinOp(.mulFloat)); - try sb.addOptimizingMethod(rt.FloatT, self.@"infix/MGID", &.{ bt.Any, bt.Any }, bt.Float, inlineBinOp(.divFloat)); - try sb.addOptimizingMethod(rt.FloatT, self.@"infix%MGID", &.{ bt.Any, bt.Any }, bt.Float, inlineBinOp(.modFloat)); - try sb.addOptimizingMethod(rt.FloatT, self.@"infix^MGID", &.{ bt.Any, bt.Any }, bt.Float, inlineBinOp(.powFloat)); id = try self.addBuiltinType("List", bt.List); std.debug.assert(id == rt.ListT); @@ -297,12 +252,6 @@ pub fn bindCore(self: *cy.VM) linksection(cy.InitSection) !void { id = try self.addBuiltinType("Map", bt.Map); std.debug.assert(id == rt.MapT); - try b.addOptimizingMethod(rt.MapT, self.indexMGID, &.{ bt.Any, bt.Any }, bt.Any, inlineBinOp(.indexMap)); - try b.addOptimizingMethod(rt.MapT, self.setIndexMGID, &.{ bt.Any, bt.Any, bt.Any }, bt.None, inlineTernNoRetOp(.setIndexMap)); - try b.addMethod(rt.MapT, remove, &.{ bt.Any, bt.Any }, bt.None, mapRemove); - try b.addMethod(rt.MapT, size, &.{ bt.Any }, bt.Integer, mapSize); - try b.addMethod(rt.MapT, self.iteratorMGID, &.{ bt.Any }, bt.Any, mapIterator); - try b.addMethod(rt.MapT, self.pairIteratorMGID, &.{ bt.Any }, bt.Any, mapIterator); id = try self.addBuiltinType("MapIterator", cy.NullId); std.debug.assert(id == rt.MapIteratorT); @@ -766,7 +715,7 @@ pub fn listResize(vm: *cy.UserVM, args: [*]const Value, _: u8) linksection(cy.St return Value.None; } -fn mapIterator(vm: *cy.UserVM, args: [*]const Value, _: u8) linksection(Section) Value { +pub fn mapIterator(vm: *cy.UserVM, args: [*]const Value, _: u8) linksection(Section) Value { const obj = args[0].asHeapObject(); vm.retainObject(obj); return vm.allocMapIterator(&obj.map) catch fatal(); @@ -797,13 +746,13 @@ fn mapIteratorNext(vm: *cy.UserVM, args: [*]const Value, _: u8) linksection(cy.S } else return Value.None; } -fn mapSize(_: *cy.UserVM, args: [*]const Value, _: u8) linksection(cy.Section) Value { +pub fn mapSize(_: *cy.UserVM, args: [*]const Value, _: u8) linksection(cy.Section) Value { const obj = args[0].asHeapObject(); const inner = cy.ptrAlignCast(*cy.MapInner, &obj.map.inner); return Value.initInt(@intCast(inner.size)); } -fn mapRemove(vm: *cy.UserVM, args: [*]const Value, _: u8) linksection(cy.StdSection) Value { +pub fn mapRemove(vm: *cy.UserVM, args: [*]const Value, _: u8) linksection(cy.StdSection) Value { const obj = args[0].asHeapObject(); const inner = cy.ptrAlignCast(*cy.MapInner, &obj.map.inner); _ = inner.remove(@ptrCast(vm), args[1]); @@ -841,7 +790,7 @@ pub fn listLen(_: *cy.UserVM, args: [*]const Value, _: u8) linksection(cy.Sectio // return Value.None; // } -fn errorValue(vm: *cy.UserVM, args: [*]const Value, _: u8) Value { +pub fn errorValue(vm: *cy.UserVM, args: [*]const Value, _: u8) Value { const recv = args[0]; const enumId = (recv.val & 0xFF00) >> 8; if (enumId == cy.NullU8) { diff --git a/src/builtins/builtins.cy b/src/builtins/builtins.cy index 6047667d3..5e49a6d67 100644 --- a/src/builtins/builtins.cy +++ b/src/builtins/builtins.cy @@ -1,3 +1,48 @@ +@host +type boolean object: + @host func '$call'(val any) boolean + +@host +type 'error' object: + @host func '$call'(val any) error + @host func value(self) any + +@host +type int object: + @host func '$call'(val any) int + @host func '$prefix~'(self) int + @host func '$prefix-'(self) int + @host func '$infix<'(self, o any) boolean + @host func '$infix<='(self, o any) boolean + @host func '$infix>'(self, o any) boolean + @host func '$infix>='(self, o any) boolean + @host func '$infix+'(self, o any) int + @host func '$infix-'(self, o any) int + @host func '$infix*'(self, o any) int + @host func '$infix/'(self, o any) int + @host func '$infix%'(self, o any) int + @host func '$infix^'(self, o any) int + @host func '$infix&'(self, o any) int + @host func '$infix|'(self, o any) int + @host func '$infix||'(self, o any) int + @host func '$infix<<'(self, o any) int + @host func '$infix>>'(self, o any) int + +@host +type float object: + @host func '$call'(val any) float + @host func '$prefix-'(self) float + @host func '$infix<'(self, o any) boolean + @host func '$infix<='(self, o any) boolean + @host func '$infix>'(self, o any) boolean + @host func '$infix>='(self, o any) boolean + @host func '$infix+'(self, o any) float + @host func '$infix-'(self, o any) float + @host func '$infix*'(self, o any) float + @host func '$infix/'(self, o any) float + @host func '$infix%'(self, o any) float + @host func '$infix^'(self, o any) float + @host type List object: @host func '$index'(self, idx any) any @@ -14,6 +59,15 @@ type List object: @host func resize(self, size int) any @host func sort(self, lessFn any) any +@host +type Map object: + @host func '$index'(self, key any) any + @host func '$setIndex'(self, key any, val any) none + @host func remove(self, key any) none + @host func size(self) int + @host func iterator(self) any + @host func pairIterator(self) any + -- type string trait: -- func append(self, str any) string -- func charAt(self, idx int) any diff --git a/src/builtins/builtins.zig b/src/builtins/builtins.zig index c42c82fa3..28484cb77 100644 --- a/src/builtins/builtins.zig +++ b/src/builtins/builtins.zig @@ -34,6 +34,49 @@ pub fn postLoad(vm: *cy.UserVM, modId: cy.ModuleId) callconv(.C) void { const NameFunc = struct { []const u8, ?*const anyopaque, cy.HostFuncType }; const funcs = [_]NameFunc{ + // boolean + .{"$call", bindings.booleanCall, .standard}, + + // error + .{"$call", bindings.errorCall, .standard}, + .{"value", bindings.errorValue, .standard}, + + // int + .{"$call", bindings.integerCall, .standard}, + .{"$prefix~", bindings.intBitwiseNot, .quicken}, + .{"$prefix-", bindings.intNeg, .quicken}, + // Inlined opcodes allow the right arg to be dynamic so the compiler can gen more of those. + // So for now, the runtime signature reflects that. + .{"$infix<", bindings.inlineBinOp(.lessInt), .quicken}, + .{"$infix<=", bindings.inlineBinOp(.lessEqualInt), .quicken}, + .{"$infix>", bindings.inlineBinOp(.greaterInt), .quicken}, + .{"$infix>=", bindings.inlineBinOp(.greaterEqualInt), .quicken}, + .{"$infix+", bindings.inlineBinOp(.addInt), .quicken}, + .{"$infix-", bindings.inlineBinOp(.subInt), .quicken}, + .{"$infix*", bindings.inlineBinOp(.mulInt), .quicken}, + .{"$infix/", bindings.inlineBinOp(.divInt), .quicken}, + .{"$infix%", bindings.inlineBinOp(.modInt), .quicken}, + .{"$infix^", bindings.inlineBinOp(.powInt), .quicken}, + .{"$infix&", bindings.inlineBinOp(.bitwiseAnd), .quicken}, + .{"$infix|", bindings.inlineBinOp(.bitwiseOr), .quicken}, + .{"$infix||", bindings.inlineBinOp(.bitwiseXor), .quicken}, + .{"$infix<<", bindings.inlineBinOp(.bitwiseLeftShift), .quicken}, + .{"$infix>>", bindings.inlineBinOp(.bitwiseRightShift), .quicken}, + + // float + .{"$call", bindings.floatCall, .standard}, + .{"$prefix-", bindings.floatNeg, .quicken}, + .{"$infix<", bindings.inlineBinOp(.lessFloat), .quicken}, + .{"$infix<=", bindings.inlineBinOp(.lessEqualFloat), .quicken}, + .{"$infix>", bindings.inlineBinOp(.greaterFloat), .quicken}, + .{"$infix>=", bindings.inlineBinOp(.greaterEqualFloat), .quicken}, + .{"$infix+", bindings.inlineBinOp(.addFloat), .quicken}, + .{"$infix-", bindings.inlineBinOp(.subFloat), .quicken}, + .{"$infix*", bindings.inlineBinOp(.mulFloat), .quicken}, + .{"$infix/", bindings.inlineBinOp(.divFloat), .quicken}, + .{"$infix%", bindings.inlineBinOp(.modFloat), .quicken}, + .{"$infix^", bindings.inlineBinOp(.powFloat), .quicken}, + // List .{"$index", bindings.inlineBinOp(.indexList), .quicken}, .{"$setIndex", bindings.inlineTernNoRetOp(.setIndexList), .quicken}, @@ -49,6 +92,14 @@ const funcs = [_]NameFunc{ .{"resize", bindings.listResize, .standard}, .{"sort", bindings.listSort, .standard}, + // Map + .{"$index", bindings.inlineBinOp(.indexMap), .quicken}, + .{"$setIndex", bindings.inlineTernNoRetOp(.setIndexMap), .quicken}, + .{"remove", bindings.mapRemove, .standard}, + .{"size", bindings.mapSize, .standard}, + .{"iterator", bindings.mapIterator, .standard}, + .{"pairIterator", bindings.mapIterator, .standard}, + // Utils. .{"arrayFill", arrayFill, .standard}, .{"asciiCode", asciiCode, .standard}, @@ -75,7 +126,12 @@ const funcs = [_]NameFunc{ const NameType = struct { []const u8, cy.rt.TypeId, cy.types.TypeId }; const types = [_]NameType{ + .{"boolean", rt.BooleanT, bt.Boolean }, + .{"error", rt.ErrorT, bt.Error }, + .{"int", rt.IntegerT, bt.Integer }, + .{"float", rt.FloatT, bt.Float }, .{"List", rt.ListT, bt.List }, + .{"Map", rt.MapT, bt.Map }, }; pub fn typeLoader(_: *cy.UserVM, info: cy.HostTypeInfo, out: *cy.HostTypeResult) callconv(.C) bool { diff --git a/src/codegen.zig b/src/codegen.zig index bb4ee782d..cde33fbd6 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -1233,8 +1233,10 @@ fn statement(c: *Chunk, nodeId: cy.NodeId) !void { const param = c.nodes[decl.paramHead]; const paramName = c.getNodeTokenString(c.nodes[param.head.funcParam.name]); if (std.mem.eql(u8, paramName, "self")) { - // Struct method. - try genMethodDecl(c, sid, func, decl, funcName); + // Object method. + if (func.node_t == .funcDecl) { + try genMethodDecl(c, sid, func, decl, funcName); + } continue; } } @@ -1243,7 +1245,9 @@ fn statement(c: *Chunk, nodeId: cy.NodeId) !void { // .name = try c.alloc.dupe(u8, funcName), // }; // try c.compiler.vm.funcSymDetails.append(c.alloc, detail); - try funcDecl(c, robjSymId, funcId); + if (func.node_t == .funcDecl) { + try funcDecl(c, robjSymId, funcId); + } } }, .if_stmt => { diff --git a/src/cyber.zig b/src/cyber.zig index 35b9f6886..06bc8d575 100644 --- a/src/cyber.zig +++ b/src/cyber.zig @@ -160,6 +160,7 @@ pub const is32Bit = build_options.is32Bit; pub const hasStdFiles = !isWasm; pub const hasGC = build_options.gc; pub const hasFFI = build_options.ffi; +pub const hasCLI = build_options.cli; const build_options = @import("build_options"); pub const Trace = build_options.trace; diff --git a/src/http.zig b/src/http.zig index 6f59bf261..0f9b4c011 100644 --- a/src/http.zig +++ b/src/http.zig @@ -2,8 +2,8 @@ const std = @import("std"); const stdx = @import("stdx"); const cy = @import("cyber.zig"); -const Request = if (!cy.isWasm) std.http.Client.Request else void; -const StdResponse = if (!cy.isWasm) std.http.Client.Response else void; +const Request = if (cy.hasCLI) std.http.Client.Request else void; +const StdResponse = if (cy.hasCLI) std.http.Client.Response else void; /// Interface to http client. pub const HttpClient = struct { diff --git a/src/include/cyber.h b/src/include/cyber.h index 388575b55..6ac19b573 100644 --- a/src/include/cyber.h +++ b/src/include/cyber.h @@ -162,9 +162,11 @@ typedef enum { HOST_TYPE_CORE_OBJECT, } HostTypeType; +// If an object's size in bytes is at or below this maximum, +// it is automatically managed by Cyber's object pool. #define CS_MAX_POOL_OBJECT_SIZE 32 -// If objects allocated for the binded type ever exceeds `CS_MAX_POOL_OBJECT_SIZE`, +// If the memory occupied by an object ever exceeds `CS_MAX_POOL_OBJECT_SIZE`, // then a finalizer is required to explicitly free the memory with `csFree`. // A finalizer can also be used to perform cleanup tasks. eg. Freeing resource handles. // Unlike finalizers declared in user scripts, this finalizer is always guaranteed to be invoked. diff --git a/src/parser.zig b/src/parser.zig index 60d55d777..a814a45f1 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -573,6 +573,10 @@ pub const Parser = struct { const id = try self.pushIdentNode(self.next_pos); self.advanceToken(); return id; + } else if (token.tag() == .error_k) { + const id = try self.pushIdentNode(self.next_pos); + self.advanceToken(); + return id; } return null; } @@ -636,7 +640,7 @@ pub const Parser = struct { // Parse name. var token = self.peekToken(); - if (token.tag() != .ident) { + if (token.tag() != .ident and token.tag() != .string) { return self.reportParseError("Expected type name identifier.", &.{}); } const name = try self.pushIdentNode(self.next_pos); diff --git a/src/sema.zig b/src/sema.zig index 87be7bd75..a85c3f0ad 100644 --- a/src/sema.zig +++ b/src/sema.zig @@ -1118,7 +1118,7 @@ fn declareHostMethod(c: *cy.Chunk, modId: cy.ModuleId, nodeId: cy.NodeId) !void // Insert method entries into VM. const funcSig = c.compiler.sema.getFuncSig(func.funcSigId); if (sym.symT == .hostFunc) { - if (funcSig.isTyped) { + if (funcSig.isParamsTyped) { const m = rt.MethodInit.initHostTyped(func.funcSigId, @ptrCast(sym.inner.hostFunc.func), func.numParams); try c.compiler.vm.addMethod(typeId, mgId, m); } else { diff --git a/src/vm.zig b/src/vm.zig index e3b0cd946..0dd35c028 100644 --- a/src/vm.zig +++ b/src/vm.zig @@ -150,7 +150,7 @@ pub const VM = struct { /// Interface used for imports and fetch. httpClient: http.HttpClient, - stdHttpClient: if (!cy.isWasm) *http.StdHttpClient else *anyopaque, + stdHttpClient: if (cy.hasCLI) *http.StdHttpClient else *anyopaque, iteratorMGID: vmc.MethodGroupId = cy.NullId, pairIteratorMGID: vmc.MethodGroupId = cy.NullId, @@ -264,7 +264,7 @@ pub const VM = struct { self.compiler = try self.alloc.create(cy.VMcompiler); try self.compiler.init(self); - if (!cy.isWasm) { + if (cy.hasCLI) { self.stdHttpClient = try alloc.create(http.StdHttpClient); self.stdHttpClient.* = http.StdHttpClient.init(self.alloc); self.httpClient = self.stdHttpClient.iface(); @@ -470,7 +470,7 @@ pub const VM = struct { } if (!reset) { - if (!cy.isWasm) { + if (cy.hasCLI) { self.httpClient.deinit(); self.alloc.destroy(self.stdHttpClient); } diff --git a/test/behavior_test.zig b/test/behavior_test.zig index fc9c1cb29..ef795cbda 100644 --- a/test/behavior_test.zig +++ b/test/behavior_test.zig @@ -646,6 +646,7 @@ test "Multiple evals persisting state." { defer run.vm.release(global); run.vm.setUserData(&global); + run.vm.setModuleResolver(cy.vm_compiler.defaultModuleResolver); run.vm.setModuleLoader(struct { fn postLoad(vm: *cy.UserVM, modId: vmc.ModuleId) callconv(.C) void { const g = cy.ptrAlignCast(*cy.Value, vm.getUserData()).*; @@ -653,7 +654,7 @@ test "Multiple evals persisting state." { mod.setVar(vm.internal().compiler, "g", g) catch fatal(); } fn loader(vm: *cy.UserVM, spec: cy.Str, out: *cy.ModuleLoaderResult) callconv(.C) bool { - if (std.mem.eql(u8, spec.slice(), "builtins")) { + if (std.mem.eql(u8, spec.slice(), "mod")) { out.* = .{ .src = cy.Str.initSlice(""), .srcIsStatic = true, @@ -667,7 +668,8 @@ test "Multiple evals persisting state." { }.loader); const src1 = - \\g['a'] = 1 + \\import m 'mod' + \\m.g['a'] = 1 ; _ = try run.vm.eval("main", src1, .{ .singleRun = false, @@ -676,8 +678,9 @@ test "Multiple evals persisting state." { }); const src2 = + \\import m 'mod' \\import t 'test' - \\t.eq(g['a'], 1) + \\t.eq(m.g['a'], 1) ; _ = try run.vm.eval("main", src2, .{ .singleRun = false,