Skip to content

Commit

Permalink
[embind] Simplify string.toWireType. NFC
Browse files Browse the repository at this point in the history
- Simplify the type check
- Remove the final argument from sringToUTF8
- Restructure the loop to avoid checking stdStringIsUTF8 twice
  • Loading branch information
sbc100 committed Jan 10, 2025
1 parent 5627123 commit bf74d1c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
19 changes: 10 additions & 9 deletions src/embind/embind.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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];
}
}

Expand Down
8 changes: 4 additions & 4 deletions test/code_size/embind_hello_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 552,
"a.html.gz": 380,
"a.js": 9879,
"a.js.gz": 4288,
"a.js": 9371,
"a.js.gz": 4140,
"a.wasm": 7348,
"a.wasm.gz": 3375,
"total": 17779,
"total_gz": 8043
"total": 17271,
"total_gz": 7895
}
8 changes: 4 additions & 4 deletions test/code_size/embind_val_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 552,
"a.html.gz": 380,
"a.js": 7153,
"a.js.gz": 3042,
"a.js": 6645,
"a.js.gz": 2892,
"a.wasm": 9119,
"a.wasm.gz": 4701,
"total": 16824,
"total_gz": 8123
"total": 16316,
"total_gz": 7973
}

0 comments on commit bf74d1c

Please sign in to comment.