-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try to support some wasm script grammar, for testing (#61)
* try support some webassembly script grammar * a tiny example of wast * fix test * add wast test to ci * cosmetic stuff * remove TestSyntax.scala --------- Co-authored-by: Guannan Wei <[email protected]> Co-authored-by: ahuoguo <[email protected]>
- Loading branch information
1 parent
28b6ad0
commit b2afc58
Showing
7 changed files
with
204 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
(module | ||
(func $one (result i32) | ||
i32.const 1) | ||
(export "one" (func 0)) | ||
) | ||
|
||
(assert_return (invoke "one") (i32.const 1)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package gensym.wasm.miniwasmscript | ||
|
||
import gensym.wasm.miniwasm._ | ||
import gensym.wasm.ast._ | ||
import scala.collection.mutable.{ListBuffer, Map, ArrayBuffer} | ||
|
||
sealed class ScriptRunner { | ||
val instances: ListBuffer[ModuleInstance] = ListBuffer() | ||
val instanceMap: Map[String, ModuleInstance] = Map() | ||
|
||
def getInstance(instName: Option[String]): ModuleInstance = { | ||
instName match { | ||
case Some(name) => instanceMap(name) | ||
case None => instances.head | ||
} | ||
} | ||
|
||
def assertReturn(action: Action, expect: List[Value]): Unit = { | ||
action match { | ||
case Invoke(instName, name, args) => | ||
val module = getInstance(instName) | ||
val func = module.exports.collectFirst({ | ||
case Export(`name`, ExportFunc(index)) => | ||
module.funcs(index) | ||
case _ => throw new RuntimeException("Not Supported") | ||
}).get | ||
val instrs = func match { | ||
case FuncDef(_, FuncBodyDef(ty, _, locals, body)) => body | ||
} | ||
val k = (retStack: List[Value]) => retStack | ||
val actual = Evaluator.eval(instrs, List(), Frame(module, ArrayBuffer(args: _*)), k, List(k)) | ||
assert(actual == expect) | ||
} | ||
} | ||
|
||
def runCmd(cmd: Cmd): Unit = { | ||
cmd match { | ||
case CmdModule(module) => instances += ModuleInstance(module) | ||
case AssertReturn(action, expect) => assertReturn(action, expect) | ||
case AssertTrap(action, message) => ??? | ||
} | ||
} | ||
|
||
def run(script: Script): Unit = { | ||
for (cmd <- script.cmds) { | ||
runCmd(cmd) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package gensym.wasm | ||
|
||
import gensym.wasm.parser.Parser | ||
import gensym.wasm.miniwasmscript.ScriptRunner | ||
|
||
import org.scalatest.FunSuite | ||
|
||
|
||
class TestScriptRun extends FunSuite { | ||
def testFile(filename: String): Unit = { | ||
val script = Parser.parseScriptFile(filename).get | ||
val runner = new ScriptRunner() | ||
runner.run(script) | ||
} | ||
|
||
test("simple script") { | ||
testFile("./benchmarks/wasm/script/script_basic.wast") | ||
} | ||
} |