Optimize userdata methods call when __index and fields_getters are nil

This commit is contained in:
Alex Orlenko 2023-03-04 11:58:34 +00:00
parent 87d027e8ac
commit 33c276d0b4
No known key found for this signature in database
GPG Key ID: 4C150C250863B96D
1 changed files with 8 additions and 2 deletions

View File

@ -409,11 +409,17 @@ unsafe fn init_userdata_metatable_index(state: *mut ffi::lua_State) -> Result<()
}
ffi::lua_pop(state, 1);
// Create and cache `__index` helper
// Create and cache `__index` generator
let code = cstr!(
r#"
local error, isfunction = ...
return function (__index, field_getters, methods)
-- Fastpath to return methods table for index access
if __index == nil and field_getters == nil then
return methods
end
-- Alternatively return a function for index access
return function (self, key)
if field_getters ~= nil then
local field_getter = field_getters[key]
@ -463,7 +469,7 @@ pub unsafe fn init_userdata_metatable_newindex(state: *mut ffi::lua_State) -> Re
}
ffi::lua_pop(state, 1);
// Create and cache `__newindex` helper
// Create and cache `__newindex` generator
let code = cstr!(
r#"
local error, isfunction = ...