From fac1431634b759f26ced54ccbed1995663768436 Mon Sep 17 00:00:00 2001 From: Drew Youngwerth Date: Tue, 3 Sep 2024 23:54:16 -0700 Subject: [PATCH] Less fragile test --- .../__snapshots__/compiler.test.ts.snap | 32 ------------------- src/__tests__/compiler.test.ts | 9 +++--- src/assembler.ts | 3 +- src/assembler/return-call.ts | 11 +++++++ 4 files changed, 18 insertions(+), 37 deletions(-) delete mode 100644 src/__tests__/__snapshots__/compiler.test.ts.snap create mode 100644 src/assembler/return-call.ts diff --git a/src/__tests__/__snapshots__/compiler.test.ts.snap b/src/__tests__/__snapshots__/compiler.test.ts.snap deleted file mode 100644 index 824ecb42..00000000 --- a/src/__tests__/__snapshots__/compiler.test.ts.snap +++ /dev/null @@ -1,32 +0,0 @@ -// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html - -exports[`E2E Compiler Pipeline > Compiler can do tco 1`] = ` -"(module - (type $0 (func (param i32 i32 i32) (result i32))) - (memory $0 1 150) - (export "buffer" (memory $0)) - (export "fib" (func $fib#15065)) - (func $fib#15065 (type $0) (param $0 i32) (param $1 i32) (param $2 i32) (result i32) - (if (result i32) - (local.get $0) - (then - (return_call $fib#15065 - (i32.sub - (local.get $0) - (i32.const 1) - ) - (local.get $2) - (i32.add - (local.get $1) - (local.get $2) - ) - ) - ) - (else - (local.get $1) - ) - ) - ) -) -" -`; diff --git a/src/__tests__/compiler.test.ts b/src/__tests__/compiler.test.ts index 33b27106..9cd0dbfd 100644 --- a/src/__tests__/compiler.test.ts +++ b/src/__tests__/compiler.test.ts @@ -1,8 +1,9 @@ import { e2eVoidText, gcVoidText, tcoText } from "./fixtures/e2e-file.js"; import { compile } from "../compiler.js"; -import { describe, expect, test } from "vitest"; +import { describe, expect, test, vi } from "vitest"; import assert from "node:assert"; import { getWasmFn, getWasmInstance } from "../lib/wasm.js"; +import * as rCallUtil from "../assembler/return-call.js"; describe("E2E Compiler Pipeline", () => { test("Compiler can compile and run a basic void program", async (t) => { @@ -37,8 +38,8 @@ describe("E2E Compiler Pipeline", () => { }); test("Compiler can do tco", async (t) => { - const mod = await compile(tcoText); - mod.optimize(); - t.expect(mod.emitText()).toMatchSnapshot(); + const spy = vi.spyOn(rCallUtil, "returnCall"); + await compile(tcoText); + t.expect(spy).toHaveBeenCalledTimes(1); }); }); diff --git a/src/assembler.ts b/src/assembler.ts index 481c92c9..91b530c2 100644 --- a/src/assembler.ts +++ b/src/assembler.ts @@ -21,6 +21,7 @@ import { HeapTypeRef } from "./lib/binaryen-gc/types.js"; import { getExprType } from "./semantics/resolution/get-expr-type.js"; import { Match, MatchCase } from "./syntax-objects/match.js"; import { initExtensionHelpers } from "./assembler/extension-helpers.js"; +import { returnCall } from "./assembler/return-call.js"; export const assemble = (ast: Expr) => { const mod = new binaryen.Module(); @@ -182,7 +183,7 @@ const compileCall = (opts: CompileExprOpts): number => { const returnType = mapBinaryenType(opts, expr.fn!.returnType!); if (isReturnExpr && id === expr.parentFn?.id) { - return mod.return_call(id, args, returnType); + return returnCall(mod, id, args, returnType); } return mod.call(id, args, returnType); diff --git a/src/assembler/return-call.ts b/src/assembler/return-call.ts new file mode 100644 index 00000000..820402a7 --- /dev/null +++ b/src/assembler/return-call.ts @@ -0,0 +1,11 @@ +import binaryen from "binaryen"; +import { ExpressionRef, TypeRef } from "../lib/binaryen-gc/types.js"; + +export const returnCall = ( + mod: binaryen.Module, + fnId: string, + args: ExpressionRef[], + returnType: TypeRef +) => { + return mod.return_call(fnId, args, returnType); +};