1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-14 08:27:03 +03:00
juvix/test/BackendC/Positive.hs
Paul Cadman 657de73b98
WASM import all non-compile axioms with alphanum names in entrypoint (#1426)
* import all non-compile axioms with alphanum names in entrypoint

This commit adds `__attribute__((input_name(<name>)))` to the type
signature of all axioms that do not have a compile block. This indicates
to the compiler that this function should be added to the input table of
the WASM binary.

Adds a test that an imported function can be called from Juvix.

* test: Run node command in same directory as WASM output

* Don't generate importName for non-alphnum axioms

* Add a tutorial on Juvix module to JS interop via Wasm
2022-08-02 18:58:45 +02:00

86 lines
3.1 KiB
Haskell

module BackendC.Positive where
import BackendC.Base
import Base
import System.Process qualified as P
data PosTest = PosTest
{ _name :: String,
_relDir :: FilePath,
_compileMode :: CompileMode
}
makeLenses ''PosTest
root :: FilePath
root = "tests/positive/MiniC"
mainFile :: FilePath
mainFile = "Input.juvix"
expectedFile :: FilePath
expectedFile = "expected.golden"
actualCallExport :: Text -> [Text] -> FilePath -> IO Text
actualCallExport funName funArgs outputFile =
pack
<$> P.readProcess
"wasmer"
(["run", outputFile, "--invoke", unpack funName] <> (unpack <$> funArgs))
""
actualCallNode :: FilePath -> FilePath -> IO Text
actualCallNode jsFile outputFile = do
assertCmdExists "node"
let outputDir = takeDirectory outputFile
outputJsFile = outputDir </> jsFile
copyFile jsFile outputJsFile
withCurrentDirectory
outputDir
( pack
<$> P.readProcess
"node"
[outputJsFile]
""
)
testDescr :: PosTest -> TestDescr
testDescr PosTest {..} =
let tRoot = root </> _relDir
in TestDescr
{ _testName = _name,
_testRoot = tRoot,
_testAssertion = Steps $ case _compileMode of
WASI stdlibMode -> wasiClangAssertion stdlibMode mainFile expectedFile ""
WASM i -> wasmClangAssertion i mainFile expectedFile
}
allTests :: TestTree
allTests =
testGroup
"Backend C positive tests"
(map (mkTest . testDescr) tests)
tests :: [PosTest]
tests =
[ PosTest "HelloWorld" "HelloWorld" (WASI StdlibExclude),
PosTest "Inductive types and pattern matching" "Nat" (WASI StdlibExclude),
PosTest "Polymorphic types" "Polymorphism" (WASI StdlibExclude),
PosTest "Polymorphic axioms" "PolymorphicAxioms" (WASI StdlibExclude),
PosTest "Polymorphic target" "PolymorphicTarget" (WASI StdlibExclude),
PosTest "Multiple modules" "MultiModules" (WASI StdlibExclude),
PosTest "Higher Order Functions" "HigherOrder" (WASI StdlibExclude),
PosTest "Higher Order Functions and explicit holes" "PolymorphismHoles" (WASI StdlibExclude),
PosTest "Closures with no environment" "ClosureNoEnv" (WASI StdlibExclude),
PosTest "Closures with environment" "ClosureEnv" (WASI StdlibExclude),
PosTest "SimpleFungibleTokenImplicit" "SimpleFungibleTokenImplicit" (WASI StdlibExclude),
PosTest "Mutually recursive function" "MutuallyRecursive" (WASI StdlibExclude),
PosTest "Nested List type" "NestedList" (WASI StdlibExclude),
PosTest "Builtin types and functions" "Builtins" (WASI StdlibExclude),
PosTest "Import from embedded standard library" "StdlibImport" (WASI StdlibInclude),
PosTest "Axiom without a compile block" "AxiomNoCompile" (WASI StdlibInclude),
PosTest "Invoke a function using exported name" "ExportName" (WASM (WASMInfo (actualCallExport "fun" []))),
PosTest "Invoke a function using exported name with args" "ExportNameArgs" (WASM (WASMInfo (actualCallExport "fun" ["0"]))),
PosTest "Invoke an imported function in Juvix and exported function in JS" "ImportExportName" (WASM (WASMInfo (actualCallNode "input.js")))
]