Skip to content

Commit 2117561

Browse files
fix(bindgen): revert utf16 encoding changes
1 parent 0dfa5d2 commit 2117561

2 files changed

Lines changed: 12 additions & 37 deletions

File tree

crates/js-component-bindgen/src/intrinsics/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,11 +1195,7 @@ pub fn render_intrinsics(args: RenderIntrinsicsArgs) -> Source {
11951195
.intrinsics
11961196
.contains(&Intrinsic::String(StringIntrinsic::Utf16EncodeAsync))
11971197
{
1198-
args.intrinsics.extend([
1199-
&Intrinsic::IsLE,
1200-
&Intrinsic::String(StringIntrinsic::GlobalTextEncoderUtf16),
1201-
&Intrinsic::String(StringIntrinsic::GlobalTextEncoderUtf16LittleEndian),
1202-
]);
1198+
args.intrinsics.extend([&Intrinsic::IsLE]);
12031199
}
12041200

12051201
// Attempting to perform a debug message hoist will require string encoding to memory

crates/js-component-bindgen/src/intrinsics/string.rs

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ use crate::{intrinsics::Intrinsic, source::Source};
99
pub enum StringIntrinsic {
1010
Utf16Decoder,
1111

12-
GlobalTextEncoderUtf16,
13-
GlobalTextEncoderUtf16LittleEndian,
14-
1512
Utf16Encode,
1613

1714
Utf16EncodeAsync,
@@ -50,8 +47,6 @@ impl StringIntrinsic {
5047
Self::Utf8EncodeAsync.name(),
5148
Self::ValidateGuestChar.name(),
5249
Self::ValidateHostChar.name(),
53-
Self::GlobalTextEncoderUtf16.name(),
54-
Self::GlobalTextEncoderUtf16LittleEndian.name(),
5550
]
5651
}
5752

@@ -67,8 +62,6 @@ impl StringIntrinsic {
6762
Self::Utf8EncodeAsync => "_utf8AllocateAndEncodeAsync",
6863
Self::ValidateGuestChar => "validateGuestChar",
6964
Self::ValidateHostChar => "validateHostChar",
70-
Self::GlobalTextEncoderUtf16 => "TEXT_DECODER_UTF16",
71-
Self::GlobalTextEncoderUtf16LittleEndian => "TEXT_DECODER_UTF16_LE",
7265
}
7366
}
7467

@@ -78,17 +71,8 @@ impl StringIntrinsic {
7871
match self {
7972
Self::Utf16Decoder => uwriteln!(output, "const {name} = new TextDecoder('utf-16');"),
8073

81-
Self::GlobalTextEncoderUtf16LittleEndian => {
82-
uwriteln!(output, "const {name} = new TextEncoder('utf-16le');")
83-
}
84-
Self::GlobalTextEncoderUtf16 => {
85-
uwriteln!(output, "const {name} = new TextEncoder('utf-16');")
86-
}
87-
8874
Self::Utf16Encode | Self::Utf16EncodeAsync => {
8975
let is_le = Intrinsic::IsLE.name();
90-
let utf16_encoder_le = Self::GlobalTextEncoderUtf16LittleEndian.name();
91-
let utf16_encoder = Self::GlobalTextEncoderUtf16.name();
9276

9377
let (fn_preamble, realloc_call) = match self {
9478
Self::Utf16Encode => ("", "realloc"),
@@ -98,25 +82,20 @@ impl StringIntrinsic {
9882
uwriteln!(
9983
output,
10084
r#"
101-
{fn_preamble}function {name}(s, realloc, memory) {{
102-
if (typeof s !== 'string') {{
103-
throw new TypeError('expected a string, received [' + typeof s + ']');
104-
}}
105-
if (s.length === 0) {{ return {{ ptr: 1, len: 0 }}; }}
106-
107-
let encoder;
85+
{fn_preamble}function {name}(str, realloc, memory) {{
86+
const len = str.length;
87+
const ptr = {realloc_call}(0, 0, 2, len * 2);
88+
const out = new Uint16Array(memory.buffer, ptr, len);
89+
let i = 0;
10890
if ({is_le}) {{
109-
encoder = {utf16_encoder_le};
91+
while (i < len) {{ out[i] = str.charCodeAt(i++); }}
11092
}} else {{
111-
encoder = {utf16_encoder};
93+
while (i < len) {{
94+
const ch = str.charCodeAt(i);
95+
out[i++] = (ch & 0xff) << 8 | ch >>> 8;
96+
}}
11297
}}
113-
114-
const bytes = encoder.encode(s);
115-
let ptr = {realloc_call}(0, 0, 1, bytes.byteLength);
116-
new Uint8Array(memory.buffer).set(bytes, ptr);
117-
118-
const res = {{ ptr, len: bytes.length, codepoints: [...s].length }};
119-
return res;
98+
return {{ ptr, len, codepoints: [...str].length }};
12099
}}
121100
"#
122101
);

0 commit comments

Comments
 (0)