From 82f5a04af522f85a1462dbd35e4dd4930b468fe6 Mon Sep 17 00:00:00 2001 From: Charlie Ruan <53290280+CharlieFRuan@users.noreply.github.com> Date: Tue, 28 May 2024 07:25:07 -0400 Subject: [PATCH] [Web] Fix string to uint8 array for special characters --- web/src/memory.ts | 5 +++-- web/src/support.ts | 11 ++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/web/src/memory.ts b/web/src/memory.ts index dbbb449a0b0d..b0d4ff3bf194 100644 --- a/web/src/memory.ts +++ b/web/src/memory.ts @@ -375,8 +375,9 @@ export class CachedCallStack implements Disposable { * @param data The string content. */ allocThenSetArgString(offset: PtrOffset, data: string): void { - const strOffset = this.allocRawBytes(data.length + 1); - this.storeRawBytes(strOffset, StringToUint8Array(data)); + const dataUint8: Uint8Array = StringToUint8Array(data); + const strOffset = this.allocRawBytes(dataUint8.length); + this.storeRawBytes(strOffset, dataUint8); this.addressToSetTargetValue.push([offset, strOffset]); } /** diff --git a/web/src/support.ts b/web/src/support.ts index 2fa87ed291a2..be85e85b7bab 100644 --- a/web/src/support.ts +++ b/web/src/support.ts @@ -35,12 +35,13 @@ export function isPromise(value: any): boolean { * @returns The corresponding Uint8Array. */ export function StringToUint8Array(str: string): Uint8Array { - const arr = new Uint8Array(str.length + 1); - for (let i = 0; i < str.length; ++i) { - arr[i] = str.charCodeAt(i); + const arr: Uint8Array = new TextEncoder().encode(str); + const resArr = new Uint8Array(arr.length + 1); + for (let i = 0; i < arr.length; ++i) { + resArr[i] = arr[i]; } - arr[str.length] = 0; - return arr; + resArr[arr.length] = 0; + return resArr; } /**