Unsigned values read as signed values
Hi,
I've encountered a problem using unsigned value with embind. When writing and then reading an unsigned value, it is reinterpreted as signed. For instance, with unsigned char : writing a value from 0 to 127 and reading it returns the correct value, however writing a value between 128 and 255 will return a value between -128 and -1.
Here is a small example :
#include <emscripten/bind.h>
using namespace emscripten;
unsigned char passThrough(unsigned char c) { return c;}
static unsigned char foo = 0;
unsigned char storeAndReturn(unsigned char a) {
foo = a;
return foo;
}
unsigned char getStored() {
return foo;
}
static unsigned short bar = 0;
unsigned short getBar() {
return bar;
}
unsigned short storeToBar(unsigned short a) {
bar = a;
return bar;
}
EMSCRIPTEN_BINDINGS(embindUnsigned) {
emscripten::function("passThrough", &passThrough);
emscripten::function("storeAndReturn", &storeAndReturn);
emscripten::function("getStored", &getStored);
emscripten::function("getBar", &getBar);
emscripten::function("storeToBar", &storeToBar);
}
Here is the generated asm.js :
function __Z11passThroughh($c) {
$c = $c|0;
var label = 0, sp = 0;
sp = STACKTOP;
STACKTOP = sp;return ($c|0);
}
function __Z14storeAndReturnh($a) {
$a = $a|0;
var label = 0, sp = 0;
sp = STACKTOP;
HEAP8[8>>0] = $a;
STACKTOP = sp;return ($a|0);
}
function __Z9getStoredv() {
var $0 = 0, label = 0, sp = 0;
sp = STACKTOP;
$0 = HEAP8[8>>0]|0;
STACKTOP = sp;return ($0|0);
}
function __Z6getBarv() {
var $0 = 0, label = 0, sp = 0;
sp = STACKTOP;
$0 = HEAP16[16>>1]|0;
STACKTOP = sp;return ($0|0);
}
function __Z10storeToBart($a) {
$a = $a|0;
var label = 0, sp = 0;
sp = STACKTOP;
HEAP16[16>>1] = $a;
STACKTOP = sp;return ($a|0);
}
With this code, passThrough returns correct values, because it does not store into HEAP. storeAndReturn also return correct values because of optimizations (it returns the parameter). However, getStored and getBar read from HEAP8 and HEAP16 respectively, so return incorrect values. Shouldn't they rather read from HEAPU8 and HEAPU16 ?
The problem also arises without embind, when compiling the above code as plain C EXPORTED_FUNCTIONS. This time however, storeAndReturn is not optimised and also suffers from the bug.
This should be fixed in embind - if it's intended to work. The issue is that in LLVM, there is no meaning to unsigned or signed values. There are only unsigned or signed operations. That means that we can load as signed all the time (there is no "unsigned load"), and if the value is used as unsigned, it will be cast as required. In this case, it is just returned - so the correct bit pattern is returned, but whether it is signed or not is not set.
When another C method calls such a function, the operation using the unsigned value would then make things work as expected, when it is actually used in an unsigned operation. But when it escapes from compiled C code into JS, that doesn't happen. So embind might want to fix this. When doing a direct call to an exported function, without embind, you would need to unsigned the value, if you want it unsigned.
tl;dr: compiled C methods return bits, not signed values
Here's a PR with a fix in embind code : #3249
Since the PR is merged, I think I can close this...