Skip to content

Commit 18ff415

Browse files
Fix WASM
1 parent b4235eb commit 18ff415

File tree

2 files changed

+22
-135
lines changed

2 files changed

+22
-135
lines changed

hkmc2/shared/src/main/scala/hkmc2/codegen/wasm/text/WatBuilder.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,16 @@ class WatBuilder(using TraceLogger, State) extends CodeBuilder:
252252
)
253253

254254
case Instantiate(_, cls, as) =>
255-
val ctorClsPath = cls match
256-
case sel: Select => sel
255+
val ctorClsSymOpt = cls match
256+
case ref: Value.Ref => ref.disamb
257+
case sel: Select => sel.symbol_SelectSymbol
257258
case cls => return errExpr(
258259
Ls(
259-
msg"WatBuilder::result for Instantiate(...) where `cls` is not a Select(...) path not implemented yet " -> cls.toLoc
260+
msg"WatBuilder::result for Instantiate(...) where `cls` is not a Ref(...) or Select(...) path not implemented yet " -> cls.toLoc
260261
),
261262
extraInfo = S(s"Block IR of `cls` expression: ${cls.toString}")
262263
)
263-
val ctorClsSym = ctorClsPath.symbol_SelectSymbol match
264+
val ctorClsSym = ctorClsSymOpt match
264265
case S(sym) => sym
265266
case N => return errExpr(
266267
Ls(

hkmc2/shared/src/test/mlscript/wasm/Basics.mls

Lines changed: 17 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -4,114 +4,42 @@
44

55
false
66
//│ Wasm result:
7-
//│ FAILURE: Unexpected runtime error
8-
//│ FAILURE LOCATION: mkQuery (WasmDiffMaker.scala:128)
9-
//│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'binaryenPrintFuncRes')
10-
//│ at REPL7:1:33
11-
//│ at REPL7:2:4
12-
//│ at ContextifyScript.runInThisContext (node:vm:137:12)
13-
//│ at REPLServer.defaultEval (node:repl:593:22)
14-
//│ at bound (node:domain:433:15)
15-
//│ at REPLServer.runBound [as eval] (node:domain:444:12)
16-
//│ at REPLServer.onLine (node:repl:922:10)
17-
//│ at REPLServer.emit (node:events:518:28)
18-
//│ at REPLServer.emit (node:domain:489:12)
19-
//│ at [_onLine] [as _onLine] (node:internal/readline/interface:419:12)
7+
//│ = 0
208

219

2210
true
2311
//│ Wasm result:
24-
//│ FAILURE: Unexpected runtime error
25-
//│ FAILURE LOCATION: mkQuery (WasmDiffMaker.scala:128)
26-
//│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'binaryenPrintFuncRes')
27-
//│ at REPL8:1:33
28-
//│ at REPL8:2:4
29-
//│ at ContextifyScript.runInThisContext (node:vm:137:12)
30-
//│ at REPLServer.defaultEval (node:repl:593:22)
31-
//│ at bound (node:domain:433:15)
32-
//│ at REPLServer.runBound [as eval] (node:domain:444:12)
33-
//│ at REPLServer.onLine (node:repl:922:10)
34-
//│ at REPLServer.emit (node:events:518:28)
35-
//│ at REPLServer.emit (node:domain:489:12)
36-
//│ at [_onLine] [as _onLine] (node:internal/readline/interface:419:12)
12+
//│ = 1
3713

3814

3915
2
4016
//│ Wasm result:
41-
//│ FAILURE: Unexpected runtime error
42-
//│ FAILURE LOCATION: mkQuery (WasmDiffMaker.scala:128)
43-
//│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'binaryenPrintFuncRes')
44-
//│ at REPL9:1:33
45-
//│ at REPL9:2:4
46-
//│ at ContextifyScript.runInThisContext (node:vm:137:12)
47-
//│ at REPLServer.defaultEval (node:repl:593:22)
48-
//│ at bound (node:domain:433:15)
49-
//│ at REPLServer.runBound [as eval] (node:domain:444:12)
50-
//│ at REPLServer.onLine (node:repl:922:10)
51-
//│ at REPLServer.emit (node:events:518:28)
52-
//│ at REPLServer.emit (node:domain:489:12)
53-
//│ at [_onLine] [as _onLine] (node:internal/readline/interface:419:12)
17+
//│ = 2
5418

5519

5620
1 + 1
5721
//│ Wasm result:
58-
//│ FAILURE: Unexpected runtime error
59-
//│ FAILURE LOCATION: mkQuery (WasmDiffMaker.scala:128)
60-
//│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'binaryenPrintFuncRes')
61-
//│ at REPL10:1:33
62-
//│ at REPL10:2:4
63-
//│ at ContextifyScript.runInThisContext (node:vm:137:12)
64-
//│ at REPLServer.defaultEval (node:repl:593:22)
65-
//│ at bound (node:domain:433:15)
66-
//│ at REPLServer.runBound [as eval] (node:domain:444:12)
67-
//│ at REPLServer.onLine (node:repl:922:10)
68-
//│ at REPLServer.emit (node:events:518:28)
69-
//│ at REPLServer.emit (node:domain:489:12)
70-
//│ at [_onLine] [as _onLine] (node:internal/readline/interface:419:12)
22+
//│ = 2
7123

7224

7325
let x = 0
7426
x
7527
//│ Wasm result:
76-
//│ FAILURE: Unexpected runtime error
77-
//│ FAILURE LOCATION: mkQuery (WasmDiffMaker.scala:128)
78-
//│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'binaryenPrintFuncRes')
79-
//│ at REPL11:1:33
80-
//│ at REPL11:2:4
81-
//│ at ContextifyScript.runInThisContext (node:vm:137:12)
82-
//│ at REPLServer.defaultEval (node:repl:593:22)
83-
//│ at bound (node:domain:433:15)
84-
//│ at REPLServer.runBound [as eval] (node:domain:444:12)
85-
//│ at REPLServer.onLine (node:repl:922:10)
86-
//│ at REPLServer.emit (node:events:518:28)
87-
//│ at REPLServer.emit (node:domain:489:12)
88-
//│ at [_onLine] [as _onLine] (node:internal/readline/interface:419:12)
28+
//│ = 0
8929

9030

9131
:ge
9232
fun foo() = 42
9333
foo
9434
//│ ╔══[COMPILATION ERROR] Returning function instances is not supported
95-
//│ ║ l.93: foo
35+
//│ ║ l.33: foo
9636
//│ ╙── ^^^
9737

9838

9939
fun foo() = 42
10040
foo()
10141
//│ Wasm result:
102-
//│ FAILURE: Unexpected runtime error
103-
//│ FAILURE LOCATION: mkQuery (WasmDiffMaker.scala:128)
104-
//│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'binaryenPrintFuncRes')
105-
//│ at REPL12:1:33
106-
//│ at REPL12:2:4
107-
//│ at ContextifyScript.runInThisContext (node:vm:137:12)
108-
//│ at REPLServer.defaultEval (node:repl:593:22)
109-
//│ at bound (node:domain:433:15)
110-
//│ at REPLServer.runBound [as eval] (node:domain:444:12)
111-
//│ at REPLServer.onLine (node:repl:922:10)
112-
//│ at REPLServer.emit (node:events:518:28)
113-
//│ at REPLServer.emit (node:domain:489:12)
114-
//│ at [_onLine] [as _onLine] (node:internal/readline/interface:419:12)
42+
//│ = 42
11543

11644

11745
:wat
@@ -164,70 +92,24 @@ foo() + foo()
16492
//│ (export "entry" (func $entry))
16593
//│ (elem declare func $entry))
16694
//│ Wasm result:
167-
//│ FAILURE: Unexpected runtime error
168-
//│ FAILURE LOCATION: mkQuery (WasmDiffMaker.scala:128)
169-
//│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'binaryenPrintFuncRes')
170-
//│ at REPL13:1:33
171-
//│ at REPL13:2:4
172-
//│ at ContextifyScript.runInThisContext (node:vm:137:12)
173-
//│ at REPLServer.defaultEval (node:repl:593:22)
174-
//│ at bound (node:domain:433:15)
175-
//│ at REPLServer.runBound [as eval] (node:domain:444:12)
176-
//│ at REPLServer.onLine (node:repl:922:10)
177-
//│ at REPLServer.emit (node:events:518:28)
178-
//│ at REPLServer.emit (node:domain:489:12)
179-
//│ at [_onLine] [as _onLine] (node:internal/readline/interface:419:12)
95+
//│ = 84
18096

18197

18298
fun foo(x) = x + 1
18399
foo(41)
184100
//│ Wasm result:
185-
//│ FAILURE: Unexpected runtime error
186-
//│ FAILURE LOCATION: mkQuery (WasmDiffMaker.scala:128)
187-
//│ ═══[RUNTIME ERROR] TypeError: Cannot read properties of undefined (reading 'binaryenPrintFuncRes')
188-
//│ at REPL14:1:33
189-
//│ at REPL14:2:4
190-
//│ at ContextifyScript.runInThisContext (node:vm:137:12)
191-
//│ at REPLServer.defaultEval (node:repl:593:22)
192-
//│ at bound (node:domain:433:15)
193-
//│ at REPLServer.runBound [as eval] (node:domain:444:12)
194-
//│ at REPLServer.onLine (node:repl:922:10)
195-
//│ at REPLServer.emit (node:events:518:28)
196-
//│ at REPLServer.emit (node:domain:489:12)
197-
//│ at [_onLine] [as _onLine] (node:internal/readline/interface:419:12)
198-
101+
//│ = 42
199102

103+
:lot
200104
class Foo(val x)
201105
new Foo(0)
202-
//│ FAILURE: Unexpected compilation error
203-
//│ FAILURE LOCATION: errExpr (WatBuilder.scala:262)
204-
//│ FAILURE INFO: Block IR of `cls` expression: Ref(member:Foo,Some(class:Foo))
205-
//│ ╔══[COMPILATION ERROR] WatBuilder::result for Instantiate(...) where `cls` is not a Select(...) path not implemented yet
206-
//│ ║ l.201: new Foo(0)
207-
//│ ╙── ^^^
208-
//│ FAILURE: Unexpected exception
209-
//│ /!!!\ Uncaught error: java.lang.UnsupportedOperationException: UnreachableType is a compiler-internal type and cannot be converted to WAT
210-
//│ at: hkmc2.codegen.wasm.text.UnreachableType$.toWat(Wasm.scala:50)
211-
//│ at: hkmc2.codegen.wasm.text.Type.asValType_$bang$$anonfun$1(Wasm.scala:36)
212-
//│ at: scala.Option.getOrElse(Option.scala:201)
213-
//│ at: hkmc2.codegen.wasm.text.Type.asValType_$bang(Wasm.scala:36)
214-
//│ at: hkmc2.codegen.wasm.text.WatBuilder.returningTerm$$anonfun$6(WatBuilder.scala:537)
215-
//│ at: scala.collection.immutable.List.map(List.scala:247)
216-
//│ at: scala.collection.immutable.List.map(List.scala:79)
217-
//│ at: hkmc2.codegen.wasm.text.WatBuilder.returningTerm(WatBuilder.scala:537)
218-
//│ at: hkmc2.codegen.wasm.text.WatBuilder.block(WatBuilder.scala:634)
219-
//│ at: hkmc2.codegen.wasm.text.WatBuilder.program(WatBuilder.scala:599)
106+
//│ Wasm result:
107+
//│ = {}
220108

221109

222110
:wat
223111
class Foo(val a)
224112
(new Foo(42)).Foo#a
225-
//│ FAILURE: Unexpected compilation error
226-
//│ FAILURE LOCATION: errExpr (WatBuilder.scala:262)
227-
//│ FAILURE INFO: Block IR of `cls` expression: Ref(member:Foo,Some(class:Foo))
228-
//│ ╔══[COMPILATION ERROR] WatBuilder::result for Instantiate(...) where `cls` is not a Select(...) path not implemented yet
229-
//│ ║ l.224: (new Foo(42)).Foo#a
230-
//│ ╙── ^^^
231113
//│ Wat:
232114
//│ (module
233115
//│ (type $Foo (struct (field $a (mut (ref null any)))))
@@ -255,9 +137,13 @@ class Foo(val a)
255137
//│ (nop)
256138
//│ (block (result (ref null any))
257139
//│ (local.set $tmp
258-
//│ (unreachable))
140+
//│ (call $Foo
141+
//│ (ref.i31
142+
//│ (i32.const 42))))
259143
//│ (struct.get $Foo 0
260144
//│ (ref.cast (ref $Foo)
261145
//│ (local.get $tmp))))))
262146
//│ (export "entry" (func $entry))
263147
//│ (elem declare func $entry))
148+
//│ Wasm result:
149+
//│ = 42

0 commit comments

Comments
 (0)