emscripten
emscripten copied to clipboard
Call through member function pointer can cause emcc -O2 to fail
Test file, main.cpp:
#include <iostream>
#include <string>
#include <utility>
class A
{
public:
void HandleMessage(
const std::string &message);
private:
void _HandleSpecificMesssage(
const std::string &message);
};
void
A::HandleMessage(
const std::string &message)
{
// The type of all the functions used to handle messages.
typedef void (A::*_HandleFunction)(const std::string &message);
// A dispatch is a name with a corresponding handler function.
typedef std::pair<const char *, _HandleFunction> _Dispatch;
// All the possible dispatches.
static const _Dispatch dispatchTable[] =
{
std::make_pair("specificMessage", &A::_HandleSpecificMesssage),
// normally more handlers here..
};
// Split message into name and argument at the first occurrence
// of the ':' delimeter.
std::string::size_type n = message.find(':');
bool hasArgument = n != std::string::npos;
std::string name = hasArgument ? message.substr(0, n) : message;
std::string argument = hasArgument ? message.substr(n + 1) : "";
bool didGetHandled = false;
for (int n = 0; n < 1; n++)
{
if (name == dispatchTable[n].first)
{
// Call the handle function.
(this->*dispatchTable[n].second)(argument);
didGetHandled = true;
break;
}
}
}
void
A::_HandleSpecificMesssage(
const std::string &message)
{
std::cout << "Called A::_HandleSpecificMesssage() with message: "
<< message << std::endl;
}
int main(int argc, char *argv[])
{
A a;
a.HandleMessage("specificMessage:hello!");
return 0;
}
This causes emcc to crash with the following output shows:
[Work] [none] 22:01 22053M y:\emscripten>python em++ -O2 main.cpp
clang++: warning: argument unused during compilation: '-nostdinc++' [warn_drv_unused_argument]
emcc: warning: using libcxx turns on CORRECT_* options
emcc: warning: using libcxxabi, this may need CORRECT_* options
Warning: .ll contains floating-point values with more than 64 bits. Faking values for them. If they are used, this will almost certainly break horribly!
Traceback (most recent call last):
File "Y:\emscripten\emcc", line 1097, in <module>
flush_js_optimizer_queue()
File "Y:\emscripten\emcc", line 1078, in flush_js_optimizer_queue
final = shared.Building.js_optimizer(final, js_optimizer_queue)
File "Y:\emscripten\tools\shared.py", line 738, in js_optimizer
assert len(output) > 0 and not output.startswith('Assertion failed'), 'Error in js optimizer: ' + err + '\n\n' + output
AssertionError: Error in js optimizer:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: object is not a function
at Object.CALL_NON_FUNCTION (native)
at new JS_Parse_Error (Y:\emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:261:20)
at js_error (Y:\emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:272:15)
at croak (Y:\emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:721:17)
at token_error (Y:\emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:728:17)
at expect_token (Y:\emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:741:17)
at Object.expect (Y:\emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:744:40)
at Object.1 (Y:\emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:1289:38)
at prog1 (Y:\emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:1296:28)
at Y:\emscripten\tools\eliminator\node_modules\uglify-js\lib\parse-js.js:1082:51
When compiling without -O2, I do not get a crash and running a.out.js gives the expected output:
[Work] [none] 22:09 22050M y:\emscripten>python em++ main.cpp
clang++: warning: argument unused during compilation: '-nostdinc++'
emcc: warning: using libcxx turns on CORRECT_* options
emcc: warning: using libcxxabi, this may need CORRECT_* options
Warning: .ll contains floating-point values with more than 64 bits. Faking values for them. If they are used, this will almost certainly break horribly!
[Work] [none] 22:09 22050M y:\emscripten>node a.out.js
Called A::_HandleSpecificMesssage() with message: hello!
Calling the member function through a pointer outside the loop works, so it might be something else that is causing the real problem here..
The crash does not happen if dispatchTable is not declared to be static.
Thanks for reporting. I'm surprised we didn't catch this earlier with our test suite, but it was very easy to fix.