refactor(generators): style of generating and generated lua dispatch code

This commit is contained in:
bfredl
2024-02-01 12:30:25 +01:00
parent f9d81c43d2
commit ca2635adf9

View File

@@ -666,11 +666,11 @@ local function include_headers(output_handle, headers_to_include)
end
end
local function write_shifted_output(_, str)
local function write_shifted_output(str, ...)
str = str:gsub('\n ', '\n')
str = str:gsub('^ ', '')
str = str:gsub(' +$', '')
output:write(str)
output:write(string.format(str, ...))
end
-- start building lua output
@@ -701,8 +701,6 @@ local lua_c_functions = {}
local function process_function(fn)
local lua_c_function_name = ('nlua_api_%s'):format(fn.name)
write_shifted_output(
output,
string.format(
[[
static int %s(lua_State *lstate)
@@ -719,7 +717,6 @@ local function process_function(fn)
#fn.parameters,
(#fn.parameters == 1) and '' or 's'
)
)
lua_c_functions[#lua_c_functions + 1] = {
binding = lua_c_function_name,
api = fn.name,
@@ -727,8 +724,6 @@ local function process_function(fn)
if not fn.fast then
write_shifted_output(
output,
string.format(
[[
if (!nlua_is_deferred_safe()) {
return luaL_error(lstate, e_luv_api_disabled, "%s");
@@ -736,29 +731,22 @@ local function process_function(fn)
]],
fn.name
)
)
end
if fn.textlock then
write_shifted_output(
output,
[[
write_shifted_output([[
if (text_locked()) {
api_set_error(&err, kErrorTypeException, "%s", get_text_locked_msg());
api_set_error(&err, kErrorTypeException, "%%s", get_text_locked_msg());
goto exit_0;
}
]]
)
]])
elseif fn.textlock_allow_cmdwin then
write_shifted_output(
output,
[[
write_shifted_output([[
if (textlock != 0 || expr_map_locked()) {
api_set_error(&err, kErrorTypeException, "%s", e_textlock);
api_set_error(&err, kErrorTypeException, "%%s", e_textlock);
goto exit_0;
}
]]
)
]])
end
local cparams = ''
@@ -776,22 +764,19 @@ local function process_function(fn)
local seterr = ''
if string.match(param_type, '^KeyDict_') then
write_shifted_output(
output,
string.format(
[[
%s %s = { 0 }; nlua_pop_keydict(lstate, &%s, %s_get_field, &err_param, &err);]],
%s %s = { 0 };
nlua_pop_keydict(lstate, &%s, %s_get_field, &err_param, &err);
]],
param_type,
cparam,
cparam,
param_type
)
)
cparam = '&' .. cparam
errshift = 1 -- free incomplete dict on error
else
write_shifted_output(
output,
string.format(
[[
const %s %s = nlua_pop_%s(lstate, %s&err);]],
param[1],
@@ -799,21 +784,17 @@ local function process_function(fn)
param_type,
extra
)
)
seterr = [[
err_param = "]] .. param[2] .. [[";]]
seterr = '\n err_param = "' .. param[2] .. '";'
end
write_shifted_output(
output,
string.format([[
write_shifted_output([[
if (ERROR_SET(&err)) {]] .. seterr .. [[
goto exit_%u;
}
]], #fn.parameters - j + errshift)
)
free_code[#free_code + 1] = ('api_free_%s(%s);'):format(lc_param_type, cparam)
cparams = cparam .. ', ' .. cparams
end
@@ -822,12 +803,7 @@ local function process_function(fn)
end
if fn.arena_return then
cparams = cparams .. '&arena, '
write_shifted_output(
output,
[[
Arena arena = ARENA_EMPTY;
]]
)
write_shifted_output(' Arena arena = ARENA_EMPTY;\n')
end
if fn.has_lua_imp then
@@ -878,59 +854,31 @@ local function process_function(fn)
else
free_retval = ' api_free_' .. return_type:lower() .. '(ret);'
end
write_shifted_output(
output,
string.format(
[[
%s ret = %s(%s);
]],
fn.return_type,
fn.name,
cparams
)
)
write_shifted_output(' %s ret = %s(%s);\n', fn.return_type, fn.name, cparams)
local ret_type = real_type(fn.return_type)
if fn.has_lua_imp then
-- only push onto the Lua stack if we haven't already
write_shifted_output(
output,
string.format(
write_shifted_output(string.format(
[[
if (lua_gettop(lstate) == 0) {
nlua_push_%s(lstate, ret, true);
}
]],
return_type
)
)
))
elseif string.match(ret_type, '^KeyDict_') then
write_shifted_output(
output,
string.format(
[[
nlua_push_keydict(lstate, &ret, %s_table);
]],
' nlua_push_keydict(lstate, &ret, %s_table);\n',
string.sub(ret_type, 9)
)
)
else
local special = (fn.since ~= nil and fn.since < 11)
write_shifted_output(
output,
string.format(
[[
nlua_push_%s(lstate, ret, %s);
]],
return_type,
tostring(special)
)
)
write_shifted_output(' nlua_push_%s(lstate, ret, %s);\n', return_type, tostring(special))
end
write_shifted_output(
output,
string.format(
[[
%s
%s
@@ -941,11 +889,8 @@ local function process_function(fn)
free_at_exit_code,
err_throw_code
)
)
else
write_shifted_output(
output,
string.format(
[[
%s(%s);
%s
@@ -957,14 +902,10 @@ local function process_function(fn)
free_at_exit_code,
err_throw_code
)
)
end
write_shifted_output(
output,
[[
write_shifted_output([[
}
]]
)
]])
end
for _, fn in ipairs(functions) do