From 8dc40aa699c36186f250425b5913e070d05f74eb Mon Sep 17 00:00:00 2001 From: Ilya Rezvov Date: Mon, 19 Feb 2018 14:30:06 -0800 Subject: [PATCH] make serializer work correct --- .gitignore | 3 ++- src/Language/Wasm/Binary.hs | 13 ++++++++++--- tests/Test.hs | 9 +++++++++ tests/runnable/index.html | 11 +++++++++++ tests/samples/fact.wast | 22 ++++++++++++++++++++++ 5 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 tests/runnable/index.html diff --git a/.gitignore b/.gitignore index c1d9b4c..ede7fdb 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.stack-work \ No newline at end of file +.stack-work +tests/runnable/* \ No newline at end of file diff --git a/src/Language/Wasm/Binary.hs b/src/Language/Wasm/Binary.hs index e100db9..da829c4 100644 --- a/src/Language/Wasm/Binary.hs +++ b/src/Language/Wasm/Binary.hs @@ -1,7 +1,8 @@ {-# LANGUAGE NamedFieldPuns #-} module Language.Wasm.Binary ( - + dumpModule, + dumpModuleLazy ) where import Language.Wasm.Structure @@ -542,7 +543,7 @@ instance Serialize Module where putSection TypeSection $ putVec $ types mod putSection ImportSection $ putVec $ imports mod - putSection FunctionSection $ putVec $ map funcType $ functions mod + putSection FunctionSection $ putVec $ map (Index . funcType) $ functions mod putSection TableSection $ putVec $ tables mod putSection MemorySection $ putVec $ mems mod putSection GlobalSection $ putVec $ globals mod @@ -554,4 +555,10 @@ instance Serialize Module where putSection CodeSection $ putVec $ functions mod putSection DataSection $ putVec $ datas mod - get = undefined \ No newline at end of file + get = undefined + +dumpModule :: Module -> BS.ByteString +dumpModule = encode + +dumpModuleLazy :: Module -> LBS.ByteString +dumpModuleLazy = encodeLazy \ No newline at end of file diff --git a/tests/Test.hs b/tests/Test.hs index f59d567..5f181c9 100644 --- a/tests/Test.hs +++ b/tests/Test.hs @@ -12,6 +12,7 @@ import qualified Data.ByteString.Lazy as LBS import qualified Language.Wasm.Lexer as Lexer import qualified Language.Wasm.Parser as Parser import qualified Language.Wasm.Structure as Structure +import qualified Language.Wasm.Binary as Binary import qualified Debug.Trace as Debug @@ -19,9 +20,17 @@ isRight :: (Show b) => Either a b -> Bool isRight (Right x) = x `seq` True isRight _ = False +compile :: String -> IO () +compile file = do + content <- LBS.readFile $ "tests/samples/" ++ file + let Right mod = Parser.parseModule <$> Lexer.scanner content + LBS.writeFile ("tests/runnable/" ++ file) $ Binary.dumpModuleLazy mod + -- to run: python -m SimpleHTTPServer 8081 && open http://localhost:8081/tests/runnable + main :: IO () main = do files <- Directory.listDirectory "tests/samples" + compile "fact.wast" testCases <- (`mapM` files) $ \file -> do content <- LBS.readFile $ "tests/samples/" ++ file let result = Parser.parseModule <$> Lexer.scanner content diff --git a/tests/runnable/index.html b/tests/runnable/index.html new file mode 100644 index 0000000..41cf73f --- /dev/null +++ b/tests/runnable/index.html @@ -0,0 +1,11 @@ + diff --git a/tests/samples/fact.wast b/tests/samples/fact.wast index d9cbcf7..258d271 100644 --- a/tests/samples/fact.wast +++ b/tests/samples/fact.wast @@ -65,6 +65,28 @@ (get_local $res) ) + ;; Iterative factorial named + (func (export "fac-iter-named-32") (param $n i32) (result i32) + (local $i i32) + (local $res i32) + (set_local $i (get_local $n)) + (set_local $res (i32.const 1)) + (block $done + (loop $loop + (if + (i32.eq (get_local $i) (i32.const 0)) + (then (br $done)) + (else + (set_local $res (i32.mul (get_local $i) (get_local $res))) + (set_local $i (i32.sub (get_local $i) (i32.const 1))) + ) + ) + (br $loop) + ) + ) + (get_local $res) + ) + ;; Optimized factorial. (func (export "fac-opt") (param i64) (result i64) (local i64)