SwadicalRag / wasm2lua

wasm2lua: converting WASM into Lua
MIT License
190 stars 10 forks source link

Allow usage of `wasm2lua` without static linking to `libc` / Incorrect error message when `mallocName` is undefined #30

Closed Vurv78 closed 2 years ago

Vurv78 commented 2 years ago

So I've been getting this project to work and I can't seem to find how to define __MALLOC__ and __FREE__.. leading to undefined is not defined (or x is not defined if using --mallocName x etc) I've had to manually replace them with function() return 0 end, function() end after generating.

In the future I want to be able to automate this, so I wanted to put this here to see how to do so ( The examples didn't give me any clues either :v )

Also seems that someone else had the same issue as seen in the README https://github.com/EntireTwix/wasm2minecraft

This project is amazing btw, thank you for your work on it! (If I have time hopefully I can try and contribute..) Already got it (mostly) working on windows! (examples may or may not work)

SwadicalRag commented 2 years ago

1) undefined is not defined is a user experience bug, this code should be

-this.writeLn(this.outBuf,`local __MALLOC__ = ${malloc ? malloc.id : `function() error "${this.options.webidl.mallocName} is not defined" end`}`);
+this.writeLn(this.outBuf,`local __MALLOC__ = ${malloc ? malloc.id : `function() error "${this.options.webidl.mallocName || "malloc"} is not defined" end`}`);

2) hm, uhh, hmm

When I last used this project, malloc was defined and exported alongside whatever program you were compiling to a .wasm file. The default symbol for malloc was just simply malloc. This is because libc was being compiled into the WASM file too.

Here's what the code was trying to reference

image

image

Perhaps you should investigate your wasm binary - is the malloc symbol defined and exported there? Is it called something else? If so you should plug the right symbol name into mallocName. If nothing seems to make sense, perhaps the WASI ecosystem has changed since I last looked at this project.

HOWEVER, it seems like you want a way to use wasm2lua without libc. I can certainly add a flag to include a stub malloc in, but that'd mean that I have to dig my hands into a repository that I haven't touched in a while and do scary things to it.

Perhaps what you can try instead is to define your own stub malloc export in the code you're writing.

extern "C" void* malloc(int size) {
  return 0;
}

Something like that should be enough for your troubles.

I hope this.. helps? Unfortunately I'm held up with real life responsibilities and other projects, so I can't do very much here unless things are looking dire.

SwadicalRag commented 2 years ago

I lied, sorry I fixed it.

You can try the above fix, or by running wasm2lua with the nolibc flag

Vurv78 commented 2 years ago

Awesome, tysm!