Skip to content

Commit 727096d

Browse files
committed
Handle OOM error during luau_load (Luau >= 0.679)
1 parent 2445230 commit 727096d

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

mlua-sys/src/luau/compat.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ pub unsafe fn luaL_loadbufferenv(
388388
}
389389
}
390390

391-
if chunk_is_text {
391+
let status = if chunk_is_text {
392392
if env < 0 {
393393
env -= 1;
394394
}
@@ -397,14 +397,21 @@ pub unsafe fn luaL_loadbufferenv(
397397
ptr::write(data_ud, data);
398398
// By deferring the `free(data)` to the userdata destructor, we ensure that
399399
// even if `luau_load` throws an error, the `data` is still released.
400-
let ok = luau_load(L, name, data, size, env) == 0;
400+
let status = luau_load(L, name, data, size, env);
401401
lua_replace(L, -2); // replace data with the result
402-
if !ok {
403-
return LUA_ERRSYNTAX;
402+
status
403+
} else {
404+
luau_load(L, name, data, size, env)
405+
};
406+
407+
if status != 0 {
408+
if lua_isstring(L, -1) != 0 && CStr::from_ptr(lua_tostring(L, -1)) == c"not enough memory" {
409+
// A case for Luau >= 0.679
410+
return LUA_ERRMEM;
404411
}
405-
} else if luau_load(L, name, data, size, env) != 0 {
406412
return LUA_ERRSYNTAX;
407413
}
414+
408415
LUA_OK
409416
}
410417

0 commit comments

Comments
 (0)