LingDong- / wax

A tiny programming language that transpiles to C, C++, Java, TypeScript, Python, C#, Swift, Lua and WebAssembly 🚀
https://waxc.netlify.app/
MIT License
793 stars 45 forks source link

WAT globals interpreted as locals #20

Closed stagas closed 3 years ago

stagas commented 3 years ago

In the example raycast.wax using WebAssembly output, the variable INFINITY is imported from math:

(global $INFINITY f32 (f32.const 340282346638528859811704183484516925440))

but later is called as local:

(call $set__ray__tmax (local.get $r) (local.get $INFINITY))

which results in a parse error using wabt afterwards to convert to binary.

LingDong- commented 3 years ago

Hi, thanks for reporting this!

I did some testing and it appears that, when using the waxc binary compiled locally, global.get is correctly produced. When using the online waxc.netlify.app that uses a JavaScript file generated by emscripten from the C source, the incorrect local.get is produced. Might be something funny going on in the emscripten process... Will look into ways of fixing this!

LingDong- commented 3 years ago

Hi @stagas , I got around to fix this bug in 4874ca0bf34c41e0dd288a3ea1385205d59d391e

Turns out somewhere I forgot to set the parent of the root node to NULL, causing the WAT generator to think that it's not yet at the topmost level, hence producing local.get instead of global.get. I got lucky with the gcc compiled binary, which happens to start with 0s in the field, but not so much with the emscripten output :-P

The site is fixed and producing correct global.get now. Thanks a lot for reporting the bug!

stagas commented 3 years ago

Works like a charm! :tada: Nice find, thanks :)