I have to do some heavy computation in my smart-contract, like say a+b
. Fortunately, I have written some very efficient implementation in "native" (web)assembly, like:
File add.wast
:
(module
(func $add (export "add_asm")
(param $x i32)
(param $y i32)
(result i32)
(i32.add (get_local $x) (get_local $y))
)
)
The question is how I can use it in smartcontract? I tried (without luck) something like:
file math.cpp
:
#include <eosiolib/eosio.hpp>
int add_asm(int a, int b);
using namespace eosio;
class math : public eosio::contract {
public:
using contract::contract;
[[eosio::action]]
void add( int a, int b ) {
int res = add_asm(a,b);
print(res);
}
};
EOSIO_ABI( math, (add) )
And:
$ make
eosio-wast2wasm -r add.wast -o add.wasm
eosio-cpp -c math.cpp -o math.o
eosio-ld add.wasm math.o -o math
/usr/local/eosio.cdt/bin/wasm-ld: error: math.o: undefined symbol: add_asm(int, int)
Makefile:8: recipe for target 'math' failed
Is there any way to use webassembly in smart-contract?
Best,
PS.
To make the picture complete, here is the Makefile
:
wat := $(wildcard *.wast)
wasm := $(patsubst %.wast,%.wasm,$(wat))
objs := $(patsubst %.cpp,%.o,$(wildcard *.cpp))
all: math
math: $(wasm) $(objs)
eosio-ld $(wasm) $(objs) -o $@
$(objs): %.o: %.cpp
eosio-cpp -c $< -o $@
$(wasm): %.wasm: %.wast
eosio-wast2wasm -r $< -o $@
clean:
rm -f *.wasm *.o verify *~