use lengthBytesUTF8() to compute the exact bytes of string

This commit is contained in:
cs8425 2022-05-18 15:35:43 +08:00
parent 005cd56829
commit d8085cb533

View File

@ -1,7 +1,7 @@
module.exports = (Core, args) => { module.exports = (Core, args) => {
const argsPtr = Core._malloc(args.length * Uint32Array.BYTES_PER_ELEMENT); const argsPtr = Core._malloc(args.length * Uint32Array.BYTES_PER_ELEMENT);
args.forEach((s, idx) => { args.forEach((s, idx) => {
const sz = s.length*4 + 1; const sz = Core.lengthBytesUTF8(s) + 1;
const buf = Core._malloc(sz); const buf = Core._malloc(sz);
Core.stringToUTF8(s, buf, sz); Core.stringToUTF8(s, buf, sz);
Core.setValue(argsPtr + (Uint32Array.BYTES_PER_ELEMENT * idx), buf, 'i32'); Core.setValue(argsPtr + (Uint32Array.BYTES_PER_ELEMENT * idx), buf, 'i32');