diff --git a/src/embind/embind.js b/src/embind/embind.js index 3a3044cc14304..222919a1ad0ec 100644 --- a/src/embind/embind.js +++ b/src/embind/embind.js @@ -547,7 +547,8 @@ var LibraryEmbind = { var length; var valueIsOfTypeString = (typeof value == 'string'); - if (!(valueIsOfTypeString || value instanceof Uint8Array || value instanceof Uint8ClampedArray || value instanceof Int8Array)) { + // We accept `string` or array views with single byte elements + if (!(valueIsOfTypeString || (ArrayBuffer.isView(value) && value.BYTES_PER_ELEMENT == 1))) { throwBindingError('Cannot pass non-string to std::string'); } if (stdStringIsUTF8 && valueIsOfTypeString) { @@ -560,10 +561,10 @@ var LibraryEmbind = { var base = _malloc({{{ POINTER_SIZE }}} + length + 1); var ptr = base + {{{ POINTER_SIZE }}}; {{{ makeSetValue('base', '0', 'length', SIZE_TYPE) }}}; - if (stdStringIsUTF8 && valueIsOfTypeString) { - stringToUTF8(value, ptr, length + 1); - } else { - if (valueIsOfTypeString) { + if (valueIsOfTypeString) { + if (stdStringIsUTF8) { + stringToUTF8(value, ptr, length); + } else { for (var i = 0; i < length; ++i) { var charCode = value.charCodeAt(i); if (charCode > 255) { @@ -572,10 +573,10 @@ var LibraryEmbind = { } HEAPU8[ptr + i] = charCode; } - } else { - for (var i = 0; i < length; ++i) { - HEAPU8[ptr + i] = value[i]; - } + } + } else { + for (var i = 0; i < length; ++i) { + HEAPU8[ptr + i] = value[i]; } } diff --git a/test/code_size/embind_hello_wasm.json b/test/code_size/embind_hello_wasm.json index 48f278460369d..bcba39e8ebd92 100644 --- a/test/code_size/embind_hello_wasm.json +++ b/test/code_size/embind_hello_wasm.json @@ -1,10 +1,10 @@ { "a.html": 552, "a.html.gz": 380, - "a.js": 9879, - "a.js.gz": 4287, + "a.js": 9765, + "a.js.gz": 4273, "a.wasm": 7348, "a.wasm.gz": 3375, - "total": 17779, - "total_gz": 8042 + "total": 17665, + "total_gz": 8028 } diff --git a/test/code_size/embind_val_wasm.json b/test/code_size/embind_val_wasm.json index 3f5469b911359..93478b34f5a58 100644 --- a/test/code_size/embind_val_wasm.json +++ b/test/code_size/embind_val_wasm.json @@ -1,10 +1,10 @@ { "a.html": 552, "a.html.gz": 380, - "a.js": 7153, - "a.js.gz": 3041, + "a.js": 7039, + "a.js.gz": 3026, "a.wasm": 9119, "a.wasm.gz": 4701, - "total": 16824, - "total_gz": 8122 + "total": 16710, + "total_gz": 8107 }