arnetheduck / nlvm

LLVM-based compiler for the Nim language
Other
712 stars 42 forks source link

How to compile wasm with with extern functions #40

Closed NevsDev closed 1 year ago

NevsDev commented 1 year ago

Hello,

as far as i understand wasm, not only the native methods could be imported into js (that worked for me), but also javascript functions could be imported into wasm.

const importObject = {
  imports: {
    myFunc: function() {
      console.log("hi");
    },
  },
};
const instance = new WebAssembly.Instance(mod, importObject);

For this, the functions are simply declared with extern in C and compiled, as far as I've seen. Is there a way to easily declare and compile this in nlvm?

I tried the following:

proc myFunc() {.importc.}

result is

nlvm c --cpu:wasm32 --os:standalone --gc:none  --passl:--no-entry test.nim

wasm-ld: error: .../test.o: undefined symbol: myFunc
Error: linking failed

PS: this is a very great project. I've tried debugging with gdb and it works really well. Good work!

arnetheduck commented 1 year ago

you will want passl:-Wl,--allow-undefined in your command line, same as when linking with wasm-ld.