binaryen icon indicating copy to clipboard operation
binaryen copied to clipboard

Missing nan on wasm2js

Open willcohen opened this issue 3 years ago • 6 comments

When running the wasm2js's javascript version of the emscripten-compiled WASM of https://github.com/OSGeo/PROJ/blob/c0e2a9a49e01a8167944092a825044a3c5f468b9/src/geodesic.c#L83, that line fails with the following error:

Uncaught TypeError: nan is not a function
    at geod_init (projjs.js:378084:10)
    at pj_obj_create_28pj_ctx__2c_20dropbox__oxygen__nn_std____2__shared_ptr_osgeo__proj__common__IdentifiedObject__20__20const__29 (projjs.js:820529:7)
    at proj_create (projjs.js:820699:11)
    at proj_create_crs_to_crs (projjs.js:707136:12)
    at projjs.js:848414:22
    at ccall (projjs.js:847846:18)
    at <anonymous>:1:6

The js for the function in question:

 function geod_init($0, $1, $2) {
...
   $3 = +nan(258340 | 0);
...

Is there an issue with a collision between math.h's nan function and javascript's, leading to that function not being present?

willcohen avatar Jun 17 '22 20:06 willcohen

As a followup, sed -i 's/nan("0")/NAN/g' ../src/geodesic.c on the source code gets the transpiled JS working, though I haven't checked enough in this case about what could eventually break with a logical change from C's nan().

willcohen avatar Jun 17 '22 20:06 willcohen

Looks like we emit this:

https://github.com/WebAssembly/binaryen/blob/bab21052c3e12df7ad2fd3711d0fe6e69e05f449/src/tools/wasm2js.cpp#L789-L794

Fixing that TODO should fix this issue. That is, since we don't need to obey the old asm.js rules, we can instead just use NaN normally from the global JS scope. The fix should remove that quoted code and replace our codegen for nan to use NaN. Hopefully that's all that is needed.

kripken avatar Jun 21 '22 16:06 kripken

Hi ! I'll take this up !

architsinghal-mriirs avatar Jun 28 '22 06:06 architsinghal-mriirs

Great, thanks @architsinghal-mriirs !

kripken avatar Jun 28 '22 16:06 kripken

Hi @kripken As per my understanding, I have to remove the above mentioned code ( line 791 to 794 ) & refactor nan with NaN in the JS files. Is this correct ?

architsinghal-mriirs avatar Jun 29 '22 09:06 architsinghal-mriirs

@architsinghal-mriirs Yes, but NaN may be used not just in JS files. I'm actually not sure where the code is that turns a wasm (f32.const nan) into a JS nan - could be in wasm2js.h? That would need to be changed too.

Searching for "nan" in the codebase might be safest. For example, this code would need to be removed too:

https://github.com/WebAssembly/binaryen/blob/d252c3e9e5dee98150c5ac625b6deb0e95139ede/src/wasm2js.h#L582-L590

kripken avatar Jun 29 '22 17:06 kripken