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