Skip to content

Commit dd116a8

Browse files
committed
fix(5.0): handle the utf-8 conversion correctly for loadstring
1 parent a8a35cb commit dd116a8

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

scripts/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cd ../..
2020
if [[ "$1" == "lua-5.0.3" ]]; then
2121
emcc -Ithirdparty/$1 thirdparty/$1/lib/liblua.a thirdparty/$1/lib/liblualib.a \
2222
-s WASM=1 -O3 -o dist/glue/glue-$1.js \
23-
-s EXPORTED_RUNTIME_METHODS="['cwrap']" \
23+
-s EXPORTED_RUNTIME_METHODS="['cwrap', 'allocateUTF8', 'lengthBytesUTF8']" \
2424
-s MODULARIZE=1 \
2525
-s ALLOW_TABLE_GROWTH \
2626
-s EXPORT_NAME="glue" \

src/binding-factory.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,14 @@ const lauxBindings: Record<string, lauxBindingFactoryFunc> = {
165165
return (this as LauxLib).luaL_loadstring(L, s) || lua.lua_pcall(L, 0, LUA_MULTRET, 0);
166166
},
167167
luaL_loadstring: function(L: LuaState, s: string) {
168-
return (this as LauxLib).luaL_loadbuffer(L, s, s.length, s);
168+
const ptr = luaGlue.allocateUTF8(s) as unknown;
169+
return (this as LauxLib).luaL_loadbuffer(L, ptr as string, luaGlue.lengthBytesUTF8(s), s);
169170
},
171+
luaL_loadbuffer: luaGlue.cwrap("luaL_loadbuffer", "number", ["number", "number", "number", "string"]),
170172
luaL_newstate: luaGlue.cwrap("lua_open", "number", []),
171173
}
172174
},
173-
"<=5.1.x": function(luaGlue: LuaEmscriptenModule, _lua: Lua) {
175+
"5.1.x": function(luaGlue: LuaEmscriptenModule, _lua: Lua) {
174176
return {
175177
luaL_loadbuffer: luaGlue.cwrap("luaL_loadbuffer", "number", ["number", "string", "number", "string"]),
176178
}

src/glue/glue.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export interface LuaEmscriptenModule extends EmscriptenModule {
2+
allocateUTF8: typeof allocateUTF8;
3+
lengthBytesUTF8: typeof lengthBytesUTF8;
24
cwrap: typeof cwrap;
35
}
46

0 commit comments

Comments
 (0)