diff options
-rwxr-xr-x | src/embind/embind.js | 15 | ||||
-rwxr-xr-x | system/include/emscripten/wire.h | 14 |
2 files changed, 21 insertions, 8 deletions
diff --git a/src/embind/embind.js b/src/embind/embind.js index c7848d9c..c164b326 100755 --- a/src/embind/embind.js +++ b/src/embind/embind.js @@ -270,17 +270,24 @@ function RegisteredString(stringType, name) { } RegisteredString.prototype.toWireType = function(destructors, value) { - var ptr = _malloc(value.length + 1); - writeStringToMemory(value, ptr); + // assumes 4-byte alignment + var length = value.length; + var ptr = _malloc(4 + length); + HEAP32[ptr >> 2] = length; + writeStringToMemory(value, ptr + 4); destructors.push(_free); destructors.push(ptr); return ptr; }; RegisteredString.prototype.fromWireType = function(value) { - var rv = Pointer_stringify(value); + var length = HEAP32[value >> 2]; + var a = new Array(length); + for (var i = 0; i < length; ++i) { + a[i] = String.fromCharCode(HEAP8[value + 4 + i]); + } _free(value); - return rv; + return a.join(''); }; function __embind_register_cstring(rawType, name) { diff --git a/system/include/emscripten/wire.h b/system/include/emscripten/wire.h index dc612e11..10484a61 100755 --- a/system/include/emscripten/wire.h +++ b/system/include/emscripten/wire.h @@ -180,12 +180,18 @@ namespace emscripten { template<> struct BindingType<std::string> { - typedef char* WireType; + typedef struct { + size_t length; + char data[1]; // trailing data + }* WireType; static WireType toWireType(const std::string& v) { - return strdup(v.c_str()); + WireType wt = (WireType)malloc(sizeof(size_t) + v.length()); + wt->length = v.length(); + memcpy(wt->data, v.data(), v.length()); + return wt; } - static std::string fromWireType(char* v) { - return std::string(v); + static std::string fromWireType(WireType v) { + return std::string(v->data, v->length); } static void destroy(WireType v) { free(v); |