can export function to use in wasmer now? I get error as below:
$ cat simple.cr
def sum(a : Int32, b : Int32)
return a + b
end
$ cat simple_cr_to_wasm.sh
set -ve
prefix=simple
base=~/webassembly_crystal/
$base/crystal/bin/crystal build $prefix.cr --cross-compile --target wasm32-unknown-wasi --release
/usr/lib/llvm-12/bin/wasm-ld $prefix.wasm $base/wasi-sysroot/lib/wasm32-wasi/crt1.o -o ${prefix}_final.wasm -L $base/wasi-sysroot/lib/wasm32-wasi -lc -lpcre -lclang_rt.builtins-wasm32
echo done
$ sh simple_cr_to_wasm.sh
prefix=simple
base=~/webassembly_crystal/
$base/crystal/bin/crystal build $prefix.cr --cross-compile --target wasm32-unknown-wasi --release
Using compiled compiler at /home/ubuntu/webassembly_crystal/crystal/.build/crystal
wasm-ld simple.wasm -o simple -lc -L/usr/local/bin/../lib/crystal -lpcre
/usr/lib/llvm-12/bin/wasm-ld $prefix.wasm $base/wasi-sysroot/lib/wasm32-wasi/crt1.o -o ${prefix}_final.wasm -L $base/wasi-sysroot/lib/wasm32-wasi -lc -lpcre -lclang_rt.builtins-wasm32
echo done
done
$ wasmer simple_final.wasm -i sum 1 2
error: failed to run `simple_final.wasm`
╰─▶ 1: Error while importing "wasi_snapshot_preview1"."args_get": unknown import. Expected Function(FunctionType { params: [I32, I32], results: [I32] })
and in wasmer-crystal(GitHub - naqvis/wasmer-crystal: WebAssembly runtime for Crystal):
$ cat use_simple_in_wasmer.cr
require "../src/wasmer"
class AssertionError < RuntimeError
end
def assert
raise AssertionError.new unless yield
end
# Let's define the engine, that holds the compiler.
engine = Wasmer::Engine.new
# Let's define the store, that holds the engine, that holds the compiler.
store = Wasmer::Store.new(engine)
# Above two lines are same as invoking below helper method
# store = Wasmer.default_engine.new_store
# Let's compile the module to be able to execute it!
module_ = Wasmer::Module.new(store, File.read("#{__DIR__}/simple_final.wasm"))
# Now the module is compiled, we can instantiate it.
instance = Wasmer::Instance.new(module_)
# get the exported `sum` function
# function methods returns nil if it can't find the requested function. we know its there, so let's add `not_nil!`
sum = instance.function("sum").not_nil!
# Call the exported `sum` function with Crystal standard values. The WebAssembly
# types are inferred and values are casted automatically.
result = sum.call(5, 37)
puts result # => 42
$ crystal build use_simple_in_wasmer.cr
$ ./use_simple_in_wasmer
Unhandled exception: Missing hash key: "wasi_snapshot_preview1" (KeyError)
from /usr/local/share/crystal/src/hash.cr:1031:11 in '[]'
from /home/ubuntu/wasmer-crystal/src/wasmer/import.cr:25:86 in 'into_inner'
from /home/ubuntu/wasmer-crystal/src/wasmer/instance.cr:23:17 in 'initialize'
from /home/ubuntu/wasmer-crystal/src/wasmer/instance.cr:22:5 in 'new'
from /home/ubuntu/wasmer-crystal/src/wasmer/instance.cr:32:7 in 'new'
from use_simple.cr:16:1 in '__crystal_main'
from /usr/local/share/crystal/src/crystal/main.cr:115:5 in 'main_user_code'
from /usr/local/share/crystal/src/crystal/main.cr:101:7 in 'main'
from /usr/local/share/crystal/src/crystal/main.cr:127:3 in 'main'
from /lib/x86_64-linux-gnu/libc.so.6 in '__libc_start_main'
from ./use_simple in '_start'
from ???