6

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 *~
  • 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 Oct 25 '18 at 7:26
0

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 acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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