Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[embind] Simplify string.toWireType. NFC #23369

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like tests are still failing, + 1 is needed or something else going on?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, you are correct.

Strange.. his code doesn't actually require null termination (it uses lenght-pefix instead)... but stringToUTF8 unconditionally does.

} 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": 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
}
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": 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
}
Loading