emscripten-core / emsdk

Emscripten SDK
http://emscripten.org
Other
2.96k stars 676 forks source link

If else judgment logic exception inside int type function #1282

Closed Shen-wb closed 11 months ago

Shen-wb commented 11 months ago

In my WebAssembly code, compiled using the WebIDL approach, I have defined an int type function in the main module and exported it to JavaScript. Inside this int type function, I have implemented conditional logic using 'if else' statements as follows:

int MainModule::testDebug(int n) { if (n == 8 || n == 16) { return n; } else if ((emscripten_log(EM_LOG_CONSOLE, "number: %d", n), true) && n == 2 && (emscripten_log(EM_LOG_CONSOLE, "number: %d", n), true) ) { (emscripten_log(EM_LOG_CONSOLE, "number: %d", n), true) return n; } }

After compiling this code to WebAssembly, I noticed that when I pass the value 4 as an argument, it appears to be assigned the value 2 within the 'else if' block, causing the program to execute the logic within that block. This behavior seems unexpected and might be indicative of a potential bug.

The resulting output of the given sample code is '4 2 2,' whereas the expected output should have been '4.' This discrepancy occurs because the conditional statement terminates when 'n == 2,' as intended.

Shen-wb commented 11 months ago

The reason for the logical exception is that if all conditions are not met, a return value is not given.