7

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 *~
2
  • Would be interesting to know! I was trying to use wasm files on the web site, so I can share my code with the smart contract and the front end.
    – friedger
    Commented Oct 25, 2018 at 7:26
  • Any news on this? Very interesting.
    – cmadh
    Commented Mar 26, 2019 at 11:10

1 Answer 1

1

Theoretically, the final linking stage of the compiler pulls different wasm modules together, so you could include your own wasm code in a module and then link it at that stage.

However, this isn't supported by block.one and might not be possible at some future time.

If you succeed, please share the code and build structure that you used, it would be interesting for many people I believe.


Source

Telegram, EOS Developers channel (https://t.me/joinchat/Esi1OkPktgcFeJ3Lmlcrqg)

Todd Fleming, [11.01.19 15:16] [In reply to Phillip Hamnett - EOS42] There isn't a asm directive, but the final link stage pulls wasm modules together. You could include a non-C++ wasm in the mix

Todd Fleming, [11.01.19 15:17] Note that isn't a supported path and may disappear in the future

Matt Witherspoon, [11.01.19 15:17] [In reply to Todd Fleming] I've had trouble doing that. Unless there is a bunch of magic sections lld won't link them

Matt Witherspoon, [11.01.19 15:18] And I don't know of a way to generate those magic sections

Matt Witherspoon, [11.01.19 15:21] at the llvm layer I believe there actually is an asm directive even for wasm. but I don't know if that is plumbed through clang

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.