I set up a small example for passing a string from C++ to JS. The string is
correctly received in JS when no optimizations are used but it becomes an
empty string when -O is -O2, -Os or -O3 and I can't figure out how to make
it work. If someone can point me in the right direction I'd be truly
thankful.
I'm using emscripten 1.36.1 on a 64-bit linux machine.
The code and commands used:
========
sample.h
========
extern "C" {
void Test();
}
==========
sample.cpp
==========
#include "sample.h"
#include <emscripten.h>
void Test() {
const char my_string[] = "hello world";
EM_ASM_ARGS({
ShowAlert($0);
}, my_string);
}
===========
sample.html
===========
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="sample.js"></script>
<script type="text/javascript">
function ShowAlert(string_ptr) {
alert(Module.Pointer_stringify(string_ptr));
}
Test = Module.cwrap('Test', null, []);
Test();
</script>
</head>
<body>
</body>
</html>
=====================
compiling and running
=====================
em++ -Os sample.cpp -o sample.js -s EXPORTED_FUNCTIONS="['_Test']"
emrun sample.html
Also doesn't work:
em++ -Os sample.cpp -o sample.o
em++ -Os sample.o -o sample.js -s EXPORTED_FUNCTIONS="['_Test']"
emrun sample.html
But setting -O0 only when compling the js file works (-Os is still used
when compiling the llvm object file):
em++ -Os sample.cpp -o sample.o
em++ *-O0* sample.o -o sample.js -s EXPORTED_FUNCTIONS="['_Test']"
emrun sample.html
Firefox also has a strange behaviour: if I replace the JS ShowAlert
function with
function ShowAlert(string_ptr) {
alert(Module.Pointer_stringify(string_ptr));
alert(Module.Pointer_stringify(string_ptr));
}
the second alert correctly displays "hello world" and all subsequent calls
will work correctly. On chrome, the second alert is still empty.
The following alternatives to stringify also don't work:
alert(Module.getValue(string_ptr, Module.i8)); // Expected to be 104.
Shows 0.
alert(Module.getValue(string_ptr, "i8")); // Expected to be 104. Shows
0.
alert(Module.AsciiToString(string_ptr));
alert(Pointer_stringify(string_ptr, 10));
alert(intArrayToString(HEAPU8.subarray(string_ptr, string_ptr + 10)));
--
You received this message because you are subscribed to the Google Groups
"emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.