diff --git a/core/src/demo/xmake.sh b/core/src/demo/xmake.sh index a6342ce4e72..05b1047ca13 100755 --- a/core/src/demo/xmake.sh +++ b/core/src/demo/xmake.sh @@ -32,6 +32,7 @@ target "demo" add_installfiles "${projectdir}/(xmake/scripts/virtualenvs/**)" "share" add_installfiles "${projectdir}/(xmake/scripts/pac/**)" "share" add_installfiles "${projectdir}/(xmake/scripts/conan/**)" "share" + add_installfiles "${projectdir}/(xmake/scripts/module/**)" "share" add_installfiles "${projectdir}/(xmake/templates/**)" "share" add_installfiles "${projectdir}/scripts/xrepo.sh" "bin" "xrepo" diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 827d46d2eaa..b63dcf42b09 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -294,6 +294,9 @@ tb_int_t xm_libc_setbyte(lua_State* lua); // the tty functions tb_int_t xm_tty_term_mode(lua_State* lua); +// the package functions +tb_int_t xm_package_loadxmi(lua_State* lua); + #ifdef XM_CONFIG_API_HAVE_CURSES // register curses functions tb_int_t xm_lua_curses_register(lua_State* lua, tb_char_t const* module); @@ -571,6 +574,13 @@ static luaL_Reg const g_tty_functions[] = , { tb_null, tb_null } }; +// the package functions +static luaL_Reg const g_package_functions[] = +{ + { "loadxmi", xm_package_loadxmi } +, { tb_null, tb_null } +}; + // the lua global instance for signal handler static lua_State* g_lua = tb_null; @@ -1158,6 +1168,9 @@ xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_c // bind tty functions xm_lua_register(engine->lua, "tty", g_tty_functions); + // bind package functions + xm_lua_register(engine->lua, "package", g_package_functions); + #ifdef XM_CONFIG_API_HAVE_CURSES // bind curses xm_lua_curses_register(engine->lua, "curses"); diff --git a/core/src/xmake/package/loadxmi.c b/core/src/xmake/package/loadxmi.c new file mode 100644 index 00000000000..43915aff372 --- /dev/null +++ b/core/src/xmake/package/loadxmi.c @@ -0,0 +1,228 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file loadxmi.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "loadxmi" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#ifdef USE_LUAJIT +# define XMI_USE_LUAJIT +#endif +#include "xmi.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ +typedef int (*xm_setup_func_t)(xmi_lua_ops_t* ops); + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_package_loadxmi(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + tb_char_t const* path = luaL_checkstring(lua, 1); + tb_check_return_val(path, 0); + + tb_char_t const* name = luaL_checkstring(lua, 2); + tb_check_return_val(name, 0); + + // load module library + tb_dynamic_ref_t lib = tb_dynamic_init(path); + if (!lib) + { + lua_pushnil(lua); + lua_pushfstring(lua, "load %s failed", path); + return 2; + } + + // get xmiopen_xxx function + lua_CFunction luaopen_func = (lua_CFunction)tb_dynamic_func(lib, name); + if (!luaopen_func) + { + lua_pushnil(lua); + lua_pushfstring(lua, "cannot get symbol %s failed", name); + return 2; + } + + // get xmisetup function + xm_setup_func_t xmisetup_func = (xm_setup_func_t)tb_dynamic_func(lib, "xmisetup"); + if (!xmisetup_func) + { + lua_pushnil(lua); + lua_pushfstring(lua, "cannot get symbol xmisetup failed"); + return 2; + } + + // setup lua interfaces + static xmi_lua_ops_t s_luaops = {0}; + static tb_bool_t s_luaops_inited = tb_false; + if (!s_luaops_inited) + { + // get functions +#ifdef XMI_USE_LUAJIT + s_luaops._lua_newuserdata = &lua_newuserdata; +#else + s_luaops._lua_getglobal = &lua_getglobal; + s_luaops._lua_geti = &lua_geti; + s_luaops._lua_rawgetp = &lua_rawgetp; + s_luaops._lua_getiuservalue = &lua_getiuservalue; + s_luaops._lua_newuserdatauv = &lua_newuserdatauv; +#endif + s_luaops._lua_gettable = &lua_gettable; + s_luaops._lua_getfield = &lua_getfield; + s_luaops._lua_rawget = &lua_rawget; + s_luaops._lua_rawgeti = &lua_rawgeti; + s_luaops._lua_createtable = &lua_createtable; + s_luaops._lua_getmetatable = &lua_getmetatable; + + // set functions +#ifndef XMI_USE_LUAJIT + s_luaops._lua_setglobal = &lua_setglobal; + s_luaops._lua_seti = &lua_seti; + s_luaops._lua_rawsetp = &lua_rawsetp; + s_luaops._lua_setiuservalue = &lua_setiuservalue; +#endif + s_luaops._lua_settable = &lua_settable; + s_luaops._lua_setfield = &lua_setfield; + s_luaops._lua_rawset = &lua_rawset; + s_luaops._lua_rawseti = &lua_rawseti; + s_luaops._lua_setmetatable = &lua_setmetatable; + + // access functions + s_luaops._lua_isnumber = &lua_isnumber; + s_luaops._lua_isstring = &lua_isstring; + s_luaops._lua_iscfunction = &lua_iscfunction; + s_luaops._lua_isuserdata = &lua_isuserdata; + s_luaops._lua_type = &lua_type; + s_luaops._lua_typename = &lua_typename; +#ifndef XMI_USE_LUAJIT + s_luaops._lua_isinteger = &lua_isinteger; +#endif + + s_luaops._lua_tonumberx = &lua_tonumberx; + s_luaops._lua_tointegerx = &lua_tointegerx; + s_luaops._lua_toboolean = &lua_toboolean; + s_luaops._lua_tolstring = &lua_tolstring; + s_luaops._lua_tocfunction = &lua_tocfunction; + s_luaops._lua_touserdata = &lua_touserdata; + s_luaops._lua_tothread = &lua_tothread; + s_luaops._lua_topointer = &lua_topointer; +#ifndef XMI_USE_LUAJIT + s_luaops._lua_rawlen = &lua_rawlen; +#endif + + // push functions + s_luaops._lua_pushnil = &lua_pushnil; + s_luaops._lua_pushinteger = &lua_pushinteger; + s_luaops._lua_pushboolean = &lua_pushboolean; + s_luaops._lua_pushnumber = &lua_pushnumber; + s_luaops._lua_pushlstring = &lua_pushlstring; + s_luaops._lua_pushstring = &lua_pushstring; + s_luaops._lua_pushvfstring = &lua_pushvfstring; + s_luaops._lua_pushfstring = &lua_pushfstring; + s_luaops._lua_pushcclosure = &lua_pushcclosure; + s_luaops._lua_pushlightuserdata = &lua_pushlightuserdata; + s_luaops._lua_pushthread = &lua_pushthread; + + // stack functions +#ifdef XMI_USE_LUAJIT + s_luaops._lua_insert = &lua_insert; + s_luaops._lua_remove = &lua_remove; + s_luaops._lua_replace = &lua_replace; +#else + s_luaops._lua_absindex = &lua_absindex; + s_luaops._lua_rotate = &lua_rotate; +#endif + s_luaops._lua_gettop = &lua_gettop; + s_luaops._lua_settop = &lua_settop; + s_luaops._lua_pushvalue = &lua_pushvalue; + s_luaops._lua_copy = &lua_copy; + s_luaops._lua_checkstack = &lua_checkstack; + s_luaops._lua_xmove = &lua_xmove; + + // miscellaneous functions + s_luaops._lua_error = &lua_error; + s_luaops._lua_next = &lua_next; + s_luaops._lua_concat = &lua_concat; + s_luaops._lua_getallocf = &lua_getallocf; + s_luaops._lua_setallocf = &lua_setallocf; +#ifndef XMI_USE_LUAJIT + s_luaops._lua_len = &lua_len; + s_luaops._lua_toclose = &lua_toclose; + s_luaops._lua_closeslot = &lua_closeslot; + s_luaops._lua_stringtonumber = &lua_stringtonumber; +#endif + + // 'load' and 'call' functions +#ifdef XMI_USE_LUAJIT + s_luaops._lua_call = &lua_call; + s_luaops._lua_pcall = &lua_pcall; +#else + s_luaops._lua_callk = &lua_callk; + s_luaops._lua_pcallk = &lua_pcallk; +#endif + s_luaops._lua_load = &lua_load; + s_luaops._lua_dump = &lua_dump; + + // luaL functions +#ifndef XMI_USE_LUAJIT + s_luaops._luaL_tolstring = &luaL_tolstring; + s_luaops._luaL_typeerror = &luaL_typeerror; +#endif + s_luaops._luaL_getmetafield = &luaL_getmetafield; + s_luaops._luaL_callmeta = &luaL_callmeta; + s_luaops._luaL_argerror = &luaL_argerror; + s_luaops._luaL_checklstring = &luaL_checklstring; + s_luaops._luaL_optlstring = &luaL_optlstring; + s_luaops._luaL_checknumber = &luaL_checknumber; + s_luaops._luaL_optnumber = &luaL_optnumber; + s_luaops._luaL_checkinteger = &luaL_checkinteger; + s_luaops._luaL_optinteger = &luaL_optinteger; + s_luaops._luaL_checkstack = &luaL_checkstack; + s_luaops._luaL_checktype = &luaL_checktype; + s_luaops._luaL_checkany = &luaL_checkany; + s_luaops._luaL_newmetatable = &luaL_newmetatable; + s_luaops._luaL_testudata = &luaL_testudata; + s_luaops._luaL_checkudata = &luaL_checkudata; + s_luaops._luaL_where = &luaL_where; + s_luaops._luaL_error = &luaL_error; + s_luaops._luaL_checkoption = &luaL_checkoption; + s_luaops._luaL_fileresult = &luaL_fileresult; + s_luaops._luaL_execresult = &luaL_execresult; + s_luaops._luaL_setfuncs = &luaL_setfuncs; + + s_luaops_inited = tb_true; + } + xmisetup_func(&s_luaops); + + // load module + lua_pushcfunction(lua, luaopen_func); + return 1; +} diff --git a/core/src/xmake/package/prefix.h b/core/src/xmake/package/prefix.h new file mode 100644 index 00000000000..e710e526ca4 --- /dev/null +++ b/core/src/xmake/package/prefix.h @@ -0,0 +1,32 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef XM_PACKAGE_PREFIX_H +#define XM_PACKAGE_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +#endif + + diff --git a/core/src/xmake/xmake.lua b/core/src/xmake/xmake.lua index 03e733c9ab2..15702c808f0 100644 --- a/core/src/xmake/xmake.lua +++ b/core/src/xmake/xmake.lua @@ -29,6 +29,7 @@ target("xmake") add_includedirs("..", {interface = true}) add_includedirs("$(buildir)/$(plat)/$(arch)/$(mode)", {public = true}) add_includedirs("../xxhash") + add_includedirs("$(projectdir)/../xmake/scripts/module") -- add header files add_headerfiles("../(xmake/*.h)") diff --git a/core/src/xmake/xmake.sh b/core/src/xmake/xmake.sh index a813cf078e4..62af5b2faee 100755 --- a/core/src/xmake/xmake.sh +++ b/core/src/xmake/xmake.sh @@ -50,6 +50,7 @@ target "xmake" add_includedirs ".." "{public}" add_includedirs "${buildir}/${plat}/${arch}/${mode}" "{public}" add_includedirs "../xxhash" + add_includedirs "${projectdir}/xmake/scripts/module" # add the common source files add_files "*.c" @@ -63,6 +64,7 @@ target "xmake" add_files "lz4/*.c" add_files "os/*.c" add_files "path/*.c" + add_files "package/*.c" add_files "process/*.c" add_files "readline/*.c" add_files "sandbox/*.c" diff --git a/tests/projects/other/native_module/modules/shared/foo/src/foo.c b/tests/projects/other/native_module/modules/shared/foo/src/foo.c index 3024f9380f0..0555b071ffa 100644 --- a/tests/projects/other/native_module/modules/shared/foo/src/foo.c +++ b/tests/projects/other/native_module/modules/shared/foo/src/foo.c @@ -1,6 +1,4 @@ -#include -#include -#include +#include static int add(lua_State* lua) { int a = lua_tointeger(lua, 1); @@ -16,14 +14,13 @@ static int sub(lua_State* lua) { return 1; } -static const luaL_Reg g_funcs[] = { - {"add", add}, - {"sub", sub}, - {NULL, NULL} -}; - -int luaopen_foo(lua_State* lua) { +int luaopen(foo, lua_State* lua) { + static const luaL_Reg funcs[] = { + {"add", add}, + {"sub", sub}, + {NULL, NULL} + }; lua_newtable(lua); - luaL_setfuncs(lua, g_funcs, 0); + luaL_setfuncs(lua, funcs, 0); return 1; } diff --git a/tests/projects/other/native_module/modules/shared/foo/xmake.lua b/tests/projects/other/native_module/modules/shared/foo/xmake.lua index ad4590ffab0..035573d9f5e 100644 --- a/tests/projects/other/native_module/modules/shared/foo/xmake.lua +++ b/tests/projects/other/native_module/modules/shared/foo/xmake.lua @@ -1,9 +1,6 @@ add_rules("mode.debug", "mode.release") -add_requires("lua 5.4") - target("foo") add_rules("module.shared") add_files("src/foo.c") - add_packages("lua") diff --git a/tests/projects/other/native_module/test.lua b/tests/projects/other/native_module/test.lua index 0c273586656..b5736207817 100644 --- a/tests/projects/other/native_module/test.lua +++ b/tests/projects/other/native_module/test.lua @@ -1,5 +1,3 @@ function main(t) - if not xmake.luajit() then - t:build() - end + t:build() end diff --git a/tests/projects/other/native_module/xmake.lua b/tests/projects/other/native_module/xmake.lua index e403454bb5c..211e839d37f 100644 --- a/tests/projects/other/native_module/xmake.lua +++ b/tests/projects/other/native_module/xmake.lua @@ -6,11 +6,11 @@ target("test") set_kind("binary") add_files("src/*.cpp") on_config(function (target) - import("shared.foo") - import("binary.bar", {always_build = true}) - print("shared: 1 + 1 = %d", foo.add(1, 1)) - print("shared: 1 - 1 = %d", foo.sub(1, 1)) - print("binary: 1 + 1 = %s", bar.add(1, 1)) - print("binary: 1 - 1 = %s", bar.sub(1, 1)) + import("shared.foo", {always_build = true}) + import("binary.bar") + print("foo: 1 + 1 = %d", foo.add(1, 1)) + print("foo: 1 - 1 = %d", foo.sub(1, 1)) + print("bar: 1 + 1 = %s", bar.add(1, 1)) + print("bar: 1 - 1 = %s", bar.sub(1, 1)) end) diff --git a/tests/projects/other/native_module_cjson/.gitignore b/tests/projects/other/native_module_cjson/.gitignore new file mode 100644 index 00000000000..15210576129 --- /dev/null +++ b/tests/projects/other/native_module_cjson/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/projects/other/native_module_cjson/modules/lua/cjson/src/cjson.c b/tests/projects/other/native_module_cjson/modules/lua/cjson/src/cjson.c new file mode 100644 index 00000000000..d8c2e49efd6 --- /dev/null +++ b/tests/projects/other/native_module_cjson/modules/lua/cjson/src/cjson.c @@ -0,0 +1,7 @@ +#include + +int luaopen_cjson(lua_State* lua); + +int luaopen(cjson, lua_State* lua) { + return luaopen_cjson(lua); +} diff --git a/tests/projects/other/native_module_cjson/modules/lua/cjson/xmake.lua b/tests/projects/other/native_module_cjson/modules/lua/cjson/xmake.lua new file mode 100644 index 00000000000..4347465410e --- /dev/null +++ b/tests/projects/other/native_module_cjson/modules/lua/cjson/xmake.lua @@ -0,0 +1,19 @@ +add_rules("mode.debug", "mode.release") + +target("cjson") + add_rules("module.shared") + set_warnings("all") + if is_plat("windows") then + set_languages("c89") + end + add_files("src/*.c") + add_files("../../../../../../../core/src/lua-cjson/lua-cjson/*.c|fpconv.c") + -- Use internal strtod() / g_fmt() code for performance and disable multi-thread + add_defines("NDEBUG", "USE_INTERNAL_FPCONV") + add_defines("XM_CONFIG_API_HAVE_LUA_CJSON") + if is_plat("windows") then + -- Windows sprintf()/strtod() handle NaN/inf differently. Not supported. + add_defines("DISABLE_INVALID_NUMBERS") + add_defines("inline=__inline") + end + diff --git a/tests/projects/other/native_module_cjson/src/main.cpp b/tests/projects/other/native_module_cjson/src/main.cpp new file mode 100644 index 00000000000..7c775d288ce --- /dev/null +++ b/tests/projects/other/native_module_cjson/src/main.cpp @@ -0,0 +1,6 @@ +#include + +int main(int argc, char** argv) { + std::cout << "hello world!" << std::endl; + return 0; +} diff --git a/tests/projects/other/native_module_cjson/test.lua b/tests/projects/other/native_module_cjson/test.lua new file mode 100644 index 00000000000..b5736207817 --- /dev/null +++ b/tests/projects/other/native_module_cjson/test.lua @@ -0,0 +1,3 @@ +function main(t) + t:build() +end diff --git a/tests/projects/other/native_module_cjson/xmake.lua b/tests/projects/other/native_module_cjson/xmake.lua new file mode 100644 index 00000000000..8768aca3e13 --- /dev/null +++ b/tests/projects/other/native_module_cjson/xmake.lua @@ -0,0 +1,12 @@ +add_rules("mode.debug", "mode.release") + +add_moduledirs("modules") + +target("test") + set_kind("binary") + add_files("src/*.cpp") + on_config(function (target) + import("lua.cjson", {always_build = true}) + print(cjson.decode('{"foo": 1, "bar": [1, 2, 3]}')) + end) + diff --git a/xmake/core/sandbox/modules/import/core/sandbox/module.lua b/xmake/core/sandbox/modules/import/core/sandbox/module.lua index b1fc0084e06..bddfb152410 100644 --- a/xmake/core/sandbox/modules/import/core/sandbox/module.lua +++ b/xmake/core/sandbox/modules/import/core/sandbox/module.lua @@ -305,21 +305,26 @@ function core_sandbox_module._load_from_shared(module_fullpath, opt) end libraryfiles = {} end - local script local module if #libraryfiles == 0 then libraryfiles = os.files(path.join(moduleinfo.buildir, "*module_*")) end if #libraryfiles > 0 then - local errors + local script, errors1, errors2 for _, libraryfile in ipairs(libraryfiles) do local modulename = path.basename(libraryfile):match("module_(.+)") - script, errors = package.loadlib(libraryfile, "luaopen_" .. modulename) - if not script then - return nil, errors - end - module = script() - if module then + if modulename then + if package.loadxmi then + script, errors1 = package.loadxmi(libraryfile, "xmiopen_" .. modulename) + end + if not script then + script, errors2 = package.loadlib(libraryfile, "luaopen_" .. modulename) + end + if script then + module = script() + else + return nil, errors1 or errors2 or string.format("xmiopen_%s and luaopen_%s not found!", modulename, modulename) + end break end end diff --git a/xmake/rules/module/xmake.lua b/xmake/rules/module/xmake.lua index f70c71d7e23..0515bd842b5 100644 --- a/xmake/rules/module/xmake.lua +++ b/xmake/rules/module/xmake.lua @@ -29,11 +29,15 @@ rule("module.binary") rule("module.shared") add_deps("utils.symbols.export_all") on_load(function (target) - assert(not xmake.luajit(), "rule(module.shared) only support for lua54 runtime!") import("core.project.config") target:set("kind", "shared") target:set("basename", "module_" .. target:name()) target:set("targetdir", config.buildir()) target:set("strip", "none") + target:add("includedirs", path.join(os.programdir(), "scripts", "module")) + target:add("includedirs", path.join(os.programdir(), "scripts", "module", "luawrap")) + if xmake.luajit() then + target:add("defines", "XMI_USE_LUAJIT") + end end) diff --git a/xmake/scripts/module/luawrap/lauxlib.h b/xmake/scripts/module/luawrap/lauxlib.h new file mode 100644 index 00000000000..58f7c8b4029 --- /dev/null +++ b/xmake/scripts/module/luawrap/lauxlib.h @@ -0,0 +1,31 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file luaxlib.h + * + */ +#ifndef XMI_LUAXLIB_H +#define XMI_LUAXLIB_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "xmi.h" + +#endif + + diff --git a/xmake/scripts/module/luawrap/lua.h b/xmake/scripts/module/luawrap/lua.h new file mode 100644 index 00000000000..92d799d5762 --- /dev/null +++ b/xmake/scripts/module/luawrap/lua.h @@ -0,0 +1,31 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file lua.h + * + */ +#ifndef XMI_LUA_H +#define XMI_LUA_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "xmi.h" + +#endif + + diff --git a/xmake/scripts/module/luawrap/luaconf.h b/xmake/scripts/module/luawrap/luaconf.h new file mode 100644 index 00000000000..e64d2ee3927 --- /dev/null +++ b/xmake/scripts/module/luawrap/luaconf.h @@ -0,0 +1,790 @@ +/* +** $Id: luaconf.h $ +** Configuration file for Lua +** See Copyright Notice in lua.h +*/ + + +#ifndef luaconf_h +#define luaconf_h + +#include +#include + + +/* +** =================================================================== +** General Configuration File for Lua +** +** Some definitions here can be changed externally, through the compiler +** (e.g., with '-D' options): They are commented out or protected +** by '#if !defined' guards. However, several other definitions +** should be changed directly here, either because they affect the +** Lua ABI (by making the changes here, you ensure that all software +** connected to Lua, such as C libraries, will be compiled with the same +** configuration); or because they are seldom changed. +** +** Search for "@@" to find all configurable definitions. +** =================================================================== +*/ + + +/* +** {==================================================================== +** System Configuration: macros to adapt (if needed) Lua to some +** particular platform, for instance restricting it to C89. +** ===================================================================== +*/ + +/* +@@ LUA_USE_C89 controls the use of non-ISO-C89 features. +** Define it if you want Lua to avoid the use of a few C99 features +** or Windows-specific features on Windows. +*/ +/* #define LUA_USE_C89 */ + + +/* +** By default, Lua on Windows use (some) specific Windows features +*/ +#if !defined(LUA_USE_C89) && defined(_WIN32) && !defined(_WIN32_WCE) +#define LUA_USE_WINDOWS /* enable goodies for regular Windows */ +#endif + + +#if defined(LUA_USE_WINDOWS) +#define LUA_DL_DLL /* enable support for DLL */ +#define LUA_USE_C89 /* broadly, Windows is C89 */ +#endif + + +#if defined(LUA_USE_LINUX) +#define LUA_USE_POSIX +#define LUA_USE_DLOPEN /* needs an extra library: -ldl */ +#endif + + +#if defined(LUA_USE_MACOSX) +#define LUA_USE_POSIX +#define LUA_USE_DLOPEN /* MacOS does not need -ldl */ +#endif + + +/* +@@ LUAI_IS32INT is true iff 'int' has (at least) 32 bits. +*/ +#define LUAI_IS32INT ((UINT_MAX >> 30) >= 3) + +/* }================================================================== */ + + + +/* +** {================================================================== +** Configuration for Number types. These options should not be +** set externally, because any other code connected to Lua must +** use the same configuration. +** =================================================================== +*/ + +/* +@@ LUA_INT_TYPE defines the type for Lua integers. +@@ LUA_FLOAT_TYPE defines the type for Lua floats. +** Lua should work fine with any mix of these options supported +** by your C compiler. The usual configurations are 64-bit integers +** and 'double' (the default), 32-bit integers and 'float' (for +** restricted platforms), and 'long'/'double' (for C compilers not +** compliant with C99, which may not have support for 'long long'). +*/ + +/* predefined options for LUA_INT_TYPE */ +#define LUA_INT_INT 1 +#define LUA_INT_LONG 2 +#define LUA_INT_LONGLONG 3 + +/* predefined options for LUA_FLOAT_TYPE */ +#define LUA_FLOAT_FLOAT 1 +#define LUA_FLOAT_DOUBLE 2 +#define LUA_FLOAT_LONGDOUBLE 3 + + +/* Default configuration ('long long' and 'double', for 64-bit Lua) */ +#define LUA_INT_DEFAULT LUA_INT_LONGLONG +#define LUA_FLOAT_DEFAULT LUA_FLOAT_DOUBLE + + +/* +@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. +*/ +#define LUA_32BITS 0 + + +/* +@@ LUA_C89_NUMBERS ensures that Lua uses the largest types available for +** C89 ('long' and 'double'); Windows always has '__int64', so it does +** not need to use this case. +*/ +#if defined(LUA_USE_C89) && !defined(LUA_USE_WINDOWS) +#define LUA_C89_NUMBERS 1 +#else +#define LUA_C89_NUMBERS 0 +#endif + + +#if LUA_32BITS /* { */ +/* +** 32-bit integers and 'float' +*/ +#if LUAI_IS32INT /* use 'int' if big enough */ +#define LUA_INT_TYPE LUA_INT_INT +#else /* otherwise use 'long' */ +#define LUA_INT_TYPE LUA_INT_LONG +#endif +#define LUA_FLOAT_TYPE LUA_FLOAT_FLOAT + +#elif LUA_C89_NUMBERS /* }{ */ +/* +** largest types available for C89 ('long' and 'double') +*/ +#define LUA_INT_TYPE LUA_INT_LONG +#define LUA_FLOAT_TYPE LUA_FLOAT_DOUBLE + +#else /* }{ */ +/* use defaults */ + +#define LUA_INT_TYPE LUA_INT_DEFAULT +#define LUA_FLOAT_TYPE LUA_FLOAT_DEFAULT + +#endif /* } */ + + +/* }================================================================== */ + + + +/* +** {================================================================== +** Configuration for Paths. +** =================================================================== +*/ + +/* +** LUA_PATH_SEP is the character that separates templates in a path. +** LUA_PATH_MARK is the string that marks the substitution points in a +** template. +** LUA_EXEC_DIR in a Windows path is replaced by the executable's +** directory. +*/ +#define LUA_PATH_SEP ";" +#define LUA_PATH_MARK "?" +#define LUA_EXEC_DIR "!" + + +/* +@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for +** Lua libraries. +@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for +** C libraries. +** CHANGE them if your machine has a non-conventional directory +** hierarchy or if you want to install your libraries in +** non-conventional directories. +*/ + +#define LUA_VDIR LUA_VERSION_MAJOR "." LUA_VERSION_MINOR +#if defined(_WIN32) /* { */ +/* +** In Windows, any exclamation mark ('!') in the path is replaced by the +** path of the directory of the executable file of the current process. +*/ +#define LUA_LDIR "!\\lua\\" +#define LUA_CDIR "!\\" +#define LUA_SHRDIR "!\\..\\share\\lua\\" LUA_VDIR "\\" + +#if !defined(LUA_PATH_DEFAULT) +#define LUA_PATH_DEFAULT \ + LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \ + LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua;" \ + LUA_SHRDIR"?.lua;" LUA_SHRDIR"?\\init.lua;" \ + ".\\?.lua;" ".\\?\\init.lua" +#endif + +#if !defined(LUA_CPATH_DEFAULT) +#define LUA_CPATH_DEFAULT \ + LUA_CDIR"?.dll;" \ + LUA_CDIR"..\\lib\\lua\\" LUA_VDIR "\\?.dll;" \ + LUA_CDIR"loadall.dll;" ".\\?.dll" +#endif + +#else /* }{ */ + +#define LUA_ROOT "/usr/local/" +#define LUA_LDIR LUA_ROOT "share/lua/" LUA_VDIR "/" +#define LUA_CDIR LUA_ROOT "lib/lua/" LUA_VDIR "/" + +#if !defined(LUA_PATH_DEFAULT) +#define LUA_PATH_DEFAULT \ + LUA_LDIR"?.lua;" LUA_LDIR"?/init.lua;" \ + LUA_CDIR"?.lua;" LUA_CDIR"?/init.lua;" \ + "./?.lua;" "./?/init.lua" +#endif + +#if !defined(LUA_CPATH_DEFAULT) +#define LUA_CPATH_DEFAULT \ + LUA_CDIR"?.so;" LUA_CDIR"loadall.so;" "./?.so" +#endif + +#endif /* } */ + + +/* +@@ LUA_DIRSEP is the directory separator (for submodules). +** CHANGE it if your machine does not use "/" as the directory separator +** and is not Windows. (On Windows Lua automatically uses "\".) +*/ +#if !defined(LUA_DIRSEP) + +#if defined(_WIN32) +#define LUA_DIRSEP "\\" +#else +#define LUA_DIRSEP "/" +#endif + +#endif + +/* }================================================================== */ + + +/* +** {================================================================== +** Marks for exported symbols in the C code +** =================================================================== +*/ + +/* +@@ LUA_API is a mark for all core API functions. +@@ LUALIB_API is a mark for all auxiliary library functions. +@@ LUAMOD_API is a mark for all standard library opening functions. +** CHANGE them if you need to define those functions in some special way. +** For instance, if you want to create one Windows DLL with the core and +** the libraries, you may want to use the following definition (define +** LUA_BUILD_AS_DLL to get it). +*/ +#if defined(LUA_BUILD_AS_DLL) /* { */ + +#if defined(LUA_CORE) || defined(LUA_LIB) /* { */ +#define LUA_API __declspec(dllexport) +#else /* }{ */ +#define LUA_API __declspec(dllimport) +#endif /* } */ + +#else /* }{ */ + +#define LUA_API extern + +#endif /* } */ + + +/* +** More often than not the libs go together with the core. +*/ +#define LUALIB_API LUA_API +#define LUAMOD_API LUA_API + + +/* +@@ LUAI_FUNC is a mark for all extern functions that are not to be +** exported to outside modules. +@@ LUAI_DDEF and LUAI_DDEC are marks for all extern (const) variables, +** none of which to be exported to outside modules (LUAI_DDEF for +** definitions and LUAI_DDEC for declarations). +** CHANGE them if you need to mark them in some special way. Elf/gcc +** (versions 3.2 and later) mark them as "hidden" to optimize access +** when Lua is compiled as a shared library. Not all elf targets support +** this attribute. Unfortunately, gcc does not offer a way to check +** whether the target offers that support, and those without support +** give a warning about it. To avoid these warnings, change to the +** default definition. +*/ +#if defined(__GNUC__) && ((__GNUC__*100 + __GNUC_MINOR__) >= 302) && \ + defined(__ELF__) /* { */ +#define LUAI_FUNC __attribute__((visibility("internal"))) extern +#else /* }{ */ +#define LUAI_FUNC extern +#endif /* } */ + +#define LUAI_DDEC(dec) LUAI_FUNC dec +#define LUAI_DDEF /* empty */ + +/* }================================================================== */ + + +/* +** {================================================================== +** Compatibility with previous versions +** =================================================================== +*/ + +/* +@@ LUA_COMPAT_5_3 controls other macros for compatibility with Lua 5.3. +** You can define it to get all options, or change specific options +** to fit your specific needs. +*/ +#if defined(LUA_COMPAT_5_3) /* { */ + +/* +@@ LUA_COMPAT_MATHLIB controls the presence of several deprecated +** functions in the mathematical library. +** (These functions were already officially removed in 5.3; +** nevertheless they are still available here.) +*/ +#define LUA_COMPAT_MATHLIB + +/* +@@ LUA_COMPAT_APIINTCASTS controls the presence of macros for +** manipulating other integer types (lua_pushunsigned, lua_tounsigned, +** luaL_checkint, luaL_checklong, etc.) +** (These macros were also officially removed in 5.3, but they are still +** available here.) +*/ +#define LUA_COMPAT_APIINTCASTS + + +/* +@@ LUA_COMPAT_LT_LE controls the emulation of the '__le' metamethod +** using '__lt'. +*/ +#define LUA_COMPAT_LT_LE + + +/* +@@ The following macros supply trivial compatibility for some +** changes in the API. The macros themselves document how to +** change your code to avoid using them. +** (Once more, these macros were officially removed in 5.3, but they are +** still available here.) +*/ +#define lua_strlen(L,i) lua_rawlen(L, (i)) + +#define lua_objlen(L,i) lua_rawlen(L, (i)) + +#define lua_equal(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPEQ) +#define lua_lessthan(L,idx1,idx2) lua_compare(L,(idx1),(idx2),LUA_OPLT) + +#endif /* } */ + +/* }================================================================== */ + + + +/* +** {================================================================== +** Configuration for Numbers (low-level part). +** Change these definitions if no predefined LUA_FLOAT_* / LUA_INT_* +** satisfy your needs. +** =================================================================== +*/ + +/* +@@ LUAI_UACNUMBER is the result of a 'default argument promotion' +@@ over a floating number. +@@ l_floatatt(x) corrects float attribute 'x' to the proper float type +** by prefixing it with one of FLT/DBL/LDBL. +@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats. +@@ LUA_NUMBER_FMT is the format for writing floats. +@@ lua_number2str converts a float to a string. +@@ l_mathop allows the addition of an 'l' or 'f' to all math operations. +@@ l_floor takes the floor of a float. +@@ lua_str2number converts a decimal numeral to a number. +*/ + + +/* The following definitions are good for most cases here */ + +#define l_floor(x) (l_mathop(floor)(x)) + +#define lua_number2str(s,sz,n) \ + l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n)) + +/* +@@ lua_numbertointeger converts a float number with an integral value +** to an integer, or returns 0 if float is not within the range of +** a lua_Integer. (The range comparisons are tricky because of +** rounding. The tests here assume a two-complement representation, +** where MININTEGER always has an exact representation as a float; +** MAXINTEGER may not have one, and therefore its conversion to float +** may have an ill-defined value.) +*/ +#define lua_numbertointeger(n,p) \ + ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ + (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ + (*(p) = (LUA_INTEGER)(n), 1)) + + +/* now the variable definitions */ + +#if LUA_FLOAT_TYPE == LUA_FLOAT_FLOAT /* { single float */ + +#define LUA_NUMBER float + +#define l_floatatt(n) (FLT_##n) + +#define LUAI_UACNUMBER double + +#define LUA_NUMBER_FRMLEN "" +#define LUA_NUMBER_FMT "%.7g" + +#define l_mathop(op) op##f + +#define lua_str2number(s,p) strtof((s), (p)) + + +#elif LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE /* }{ long double */ + +#define LUA_NUMBER long double + +#define l_floatatt(n) (LDBL_##n) + +#define LUAI_UACNUMBER long double + +#define LUA_NUMBER_FRMLEN "L" +#define LUA_NUMBER_FMT "%.19Lg" + +#define l_mathop(op) op##l + +#define lua_str2number(s,p) strtold((s), (p)) + +#elif LUA_FLOAT_TYPE == LUA_FLOAT_DOUBLE /* }{ double */ + +#define LUA_NUMBER double + +#define l_floatatt(n) (DBL_##n) + +#define LUAI_UACNUMBER double + +#define LUA_NUMBER_FRMLEN "" +#define LUA_NUMBER_FMT "%.14g" + +#define l_mathop(op) op + +#define lua_str2number(s,p) strtod((s), (p)) + +#else /* }{ */ + +#error "numeric float type not defined" + +#endif /* } */ + + + +/* +@@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER. +@@ LUAI_UACINT is the result of a 'default argument promotion' +@@ over a LUA_INTEGER. +@@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers. +@@ LUA_INTEGER_FMT is the format for writing integers. +@@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. +@@ LUA_MININTEGER is the minimum value for a LUA_INTEGER. +@@ LUA_MAXUNSIGNED is the maximum value for a LUA_UNSIGNED. +@@ LUA_UNSIGNEDBITS is the number of bits in a LUA_UNSIGNED. +@@ lua_integer2str converts an integer to a string. +*/ + + +/* The following definitions are good for most cases here */ + +#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" + +#define LUAI_UACINT LUA_INTEGER + +#define lua_integer2str(s,sz,n) \ + l_sprintf((s), sz, LUA_INTEGER_FMT, (LUAI_UACINT)(n)) + +/* +** use LUAI_UACINT here to avoid problems with promotions (which +** can turn a comparison between unsigneds into a signed comparison) +*/ +#define LUA_UNSIGNED unsigned LUAI_UACINT + + +#define LUA_UNSIGNEDBITS (sizeof(LUA_UNSIGNED) * CHAR_BIT) + + +/* now the variable definitions */ + +#if LUA_INT_TYPE == LUA_INT_INT /* { int */ + +#define LUA_INTEGER int +#define LUA_INTEGER_FRMLEN "" + +#define LUA_MAXINTEGER INT_MAX +#define LUA_MININTEGER INT_MIN + +#define LUA_MAXUNSIGNED UINT_MAX + +#elif LUA_INT_TYPE == LUA_INT_LONG /* }{ long */ + +#define LUA_INTEGER long +#define LUA_INTEGER_FRMLEN "l" + +#define LUA_MAXINTEGER LONG_MAX +#define LUA_MININTEGER LONG_MIN + +#define LUA_MAXUNSIGNED ULONG_MAX + +#elif LUA_INT_TYPE == LUA_INT_LONGLONG /* }{ long long */ + +/* use presence of macro LLONG_MAX as proxy for C99 compliance */ +#if defined(LLONG_MAX) /* { */ +/* use ISO C99 stuff */ + +#define LUA_INTEGER long long +#define LUA_INTEGER_FRMLEN "ll" + +#define LUA_MAXINTEGER LLONG_MAX +#define LUA_MININTEGER LLONG_MIN + +#define LUA_MAXUNSIGNED ULLONG_MAX + +#elif defined(LUA_USE_WINDOWS) /* }{ */ +/* in Windows, can use specific Windows types */ + +#define LUA_INTEGER __int64 +#define LUA_INTEGER_FRMLEN "I64" + +#define LUA_MAXINTEGER _I64_MAX +#define LUA_MININTEGER _I64_MIN + +#define LUA_MAXUNSIGNED _UI64_MAX + +#else /* }{ */ + +#error "Compiler does not support 'long long'. Use option '-DLUA_32BITS' \ + or '-DLUA_C89_NUMBERS' (see file 'luaconf.h' for details)" + +#endif /* } */ + +#else /* }{ */ + +#error "numeric integer type not defined" + +#endif /* } */ + +/* }================================================================== */ + + +/* +** {================================================================== +** Dependencies with C99 and other C details +** =================================================================== +*/ + +/* +@@ l_sprintf is equivalent to 'snprintf' or 'sprintf' in C89. +** (All uses in Lua have only one format item.) +*/ +#if !defined(LUA_USE_C89) +#define l_sprintf(s,sz,f,i) snprintf(s,sz,f,i) +#else +#define l_sprintf(s,sz,f,i) ((void)(sz), sprintf(s,f,i)) +#endif + + +/* +@@ lua_strx2number converts a hexadecimal numeral to a number. +** In C99, 'strtod' does that conversion. Otherwise, you can +** leave 'lua_strx2number' undefined and Lua will provide its own +** implementation. +*/ +#if !defined(LUA_USE_C89) +#define lua_strx2number(s,p) lua_str2number(s,p) +#endif + + +/* +@@ lua_pointer2str converts a pointer to a readable string in a +** non-specified way. +*/ +#define lua_pointer2str(buff,sz,p) l_sprintf(buff,sz,"%p",p) + + +/* +@@ lua_number2strx converts a float to a hexadecimal numeral. +** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. +** Otherwise, you can leave 'lua_number2strx' undefined and Lua will +** provide its own implementation. +*/ +#if !defined(LUA_USE_C89) +#define lua_number2strx(L,b,sz,f,n) \ + ((void)L, l_sprintf(b,sz,f,(LUAI_UACNUMBER)(n))) +#endif + + +/* +** 'strtof' and 'opf' variants for math functions are not valid in +** C89. Otherwise, the macro 'HUGE_VALF' is a good proxy for testing the +** availability of these variants. ('math.h' is already included in +** all files that use these macros.) +*/ +#if defined(LUA_USE_C89) || (defined(HUGE_VAL) && !defined(HUGE_VALF)) +#undef l_mathop /* variants not available */ +#undef lua_str2number +#define l_mathop(op) (lua_Number)op /* no variant */ +#define lua_str2number(s,p) ((lua_Number)strtod((s), (p))) +#endif + + +/* +@@ LUA_KCONTEXT is the type of the context ('ctx') for continuation +** functions. It must be a numerical type; Lua will use 'intptr_t' if +** available, otherwise it will use 'ptrdiff_t' (the nearest thing to +** 'intptr_t' in C89) +*/ +#define LUA_KCONTEXT ptrdiff_t + +#if !defined(LUA_USE_C89) && defined(__STDC_VERSION__) && \ + __STDC_VERSION__ >= 199901L +#include +#if defined(INTPTR_MAX) /* even in C99 this type is optional */ +#undef LUA_KCONTEXT +#define LUA_KCONTEXT intptr_t +#endif +#endif + + +/* +@@ lua_getlocaledecpoint gets the locale "radix character" (decimal point). +** Change that if you do not want to use C locales. (Code using this +** macro must include the header 'locale.h'.) +*/ +#if !defined(lua_getlocaledecpoint) +#define lua_getlocaledecpoint() (localeconv()->decimal_point[0]) +#endif + + +/* +** macros to improve jump prediction, used mostly for error handling +** and debug facilities. (Some macros in the Lua API use these macros. +** Define LUA_NOBUILTIN if you do not want '__builtin_expect' in your +** code.) +*/ +#if !defined(luai_likely) + +#if defined(__GNUC__) && !defined(LUA_NOBUILTIN) +#define luai_likely(x) (__builtin_expect(((x) != 0), 1)) +#define luai_unlikely(x) (__builtin_expect(((x) != 0), 0)) +#else +#define luai_likely(x) (x) +#define luai_unlikely(x) (x) +#endif + +#endif + + +#if defined(LUA_CORE) || defined(LUA_LIB) +/* shorter names for Lua's own use */ +#define l_likely(x) luai_likely(x) +#define l_unlikely(x) luai_unlikely(x) +#endif + + + +/* }================================================================== */ + + +/* +** {================================================================== +** Language Variations +** ===================================================================== +*/ + +/* +@@ LUA_NOCVTN2S/LUA_NOCVTS2N control how Lua performs some +** coercions. Define LUA_NOCVTN2S to turn off automatic coercion from +** numbers to strings. Define LUA_NOCVTS2N to turn off automatic +** coercion from strings to numbers. +*/ +/* #define LUA_NOCVTN2S */ +/* #define LUA_NOCVTS2N */ + + +/* +@@ LUA_USE_APICHECK turns on several consistency checks on the C API. +** Define it as a help when debugging C code. +*/ +#if defined(LUA_USE_APICHECK) +#include +#define luai_apicheck(l,e) assert(e) +#endif + +/* }================================================================== */ + + +/* +** {================================================================== +** Macros that affect the API and must be stable (that is, must be the +** same when you compile Lua and when you compile code that links to +** Lua). +** ===================================================================== +*/ + +/* +@@ LUAI_MAXSTACK limits the size of the Lua stack. +** CHANGE it if you need a different limit. This limit is arbitrary; +** its only purpose is to stop Lua from consuming unlimited stack +** space (and to reserve some numbers for pseudo-indices). +** (It must fit into max(size_t)/32.) +*/ +#if LUAI_IS32INT +#define LUAI_MAXSTACK 1000000 +#else +#define LUAI_MAXSTACK 15000 +#endif + + +/* +@@ LUA_EXTRASPACE defines the size of a raw memory area associated with +** a Lua state with very fast access. +** CHANGE it if you need a different size. +*/ +#define LUA_EXTRASPACE (sizeof(void *)) + + +/* +@@ LUA_IDSIZE gives the maximum size for the description of the source +@@ of a function in debug information. +** CHANGE it if you want a different size. +*/ +#define LUA_IDSIZE 60 + + +/* +@@ LUAL_BUFFERSIZE is the buffer size used by the lauxlib buffer system. +*/ +#define LUAL_BUFFERSIZE ((int)(16 * sizeof(void*) * sizeof(lua_Number))) + + +/* +@@ LUAI_MAXALIGN defines fields that, when used in a union, ensure +** maximum alignment for the other items in that union. +*/ +#define LUAI_MAXALIGN lua_Number n; double u; void *s; lua_Integer i; long l + +/* }================================================================== */ + + + + + +/* =================================================================== */ + +/* +** Local configuration. You can use this space to add your redefinitions +** without modifying the main part of the file. +*/ + + + + + +#endif + diff --git a/xmake/scripts/module/xmi.h b/xmake/scripts/module/xmi.h new file mode 100644 index 00000000000..b1fd1572ca7 --- /dev/null +++ b/xmake/scripts/module/xmi.h @@ -0,0 +1,672 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file xmi.h + * + */ +#ifndef XMI_H +#define XMI_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include +#include +#include +#ifndef LUA_VERSION +# include "luawrap/luaconf.h" +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +#define XMI_LUA_MULTRET (-1) + +// thread status +#define XMI_LUA_OK 0 +#define XMI_LUA_YIELD 1 +#define XMI_LUA_ERRRUN 2 +#define XMI_LUA_ERRSYNTAX 3 +#define XMI_LUA_ERRMEM 4 +#define XMI_LUA_ERRERR 5 + +// basic types +#define XMI_LUA_TNONE (-1) + +#define XMI_LUA_TNIL 0 +#define XMI_LUA_TBOOLEAN 1 +#define XMI_LUA_TLIGHTUSERDATA 2 +#define XMI_LUA_TNUMBER 3 +#define XMI_LUA_TSTRING 4 +#define XMI_LUA_TTABLE 5 +#define XMI_LUA_TFUNCTION 6 +#define XMI_LUA_TUSERDATA 7 +#define XMI_LUA_TTHREAD 8 + +#define XMI_LUA_NUMTYPES 9 + +// pseudo-indices +#ifdef XMI_USE_LUAJIT +# define XMI_LUA_REGISTRYINDEX (-10000) +# define XMI_LUA_ENVIRONINDEX (-10001) +# define XMI_LUA_GLOBALSINDEX (-10002) +# define xmi_lua_upvalueindex(i) (XMI_LUA_GLOBALSINDEX - (i)) +#else +# define XMI_LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000) +# define xmi_lua_upvalueindex(i) (XMI_LUA_REGISTRYINDEX - (i)) +#endif + +// get macros +#ifdef XMI_USE_LUAJIT +# define xmi_lua_getglobal(lua, s) xmi_lua_getfield(lua, LUA_GLOBALSINDEX, (s)) +# define xmi_lua_newuserdata(lua, s) (g_lua_ops)->_lua_newuserdata(lua, s) +#else +# define xmi_lua_getglobal(lua, name) (g_lua_ops)->_lua_getglobal(lua, name) +# define xmi_lua_geti(lua, idx, n) (g_lua_ops)->_lua_geti(lua, idx, n) +# define xmi_lua_rawgetp(lua, idx, p) (g_lua_ops)->_lua_rawgetp(lua, idx, p) +# define xmi_lua_getiuservalue(lua, idx, n) (g_lua_ops)->_lua_getiuservalue(lua, idx, n) +# define xmi_lua_newuserdatauv(lua, sz, nuvalue) (g_lua_ops)->_lua_newuserdatauv(lua, sz, nuvalue) +# define xmi_lua_newuserdata(lua, s) xmi_lua_newuserdatauv(lua, s, 1) +#endif +#define xmi_lua_gettable(lua, idx) (g_lua_ops)->_lua_gettable(lua, idx) +#define xmi_lua_getfield(lua, idx, k) (g_lua_ops)->_lua_getfield(lua, idx, k) +#define xmi_lua_rawget(lua, idx) (g_lua_ops)->_lua_rawget(lua, idx) +#define xmi_lua_rawgeti(lua, idx, n) (g_lua_ops)->_lua_rawgeti(lua, idx, n) +#define xmi_lua_createtable(lua, narr, nrec) (g_lua_ops)->_lua_createtable(lua, narr, nrec) +#define xmi_lua_getmetatable(lua, objindex) (g_lua_ops)->_lua_getmetatable(lua, objindex) +#define xmi_lua_newtable(lua) xmi_lua_createtable(lua, 0, 0) +#define xmi_lua_pop(lua, n) xmi_lua_settop(lua, -(n)-1) +#define xmi_lua_getuservalue(lua, idx) xmi_lua_getiuservalue(lua, idx, 1) + +// set macros +#ifdef XMI_USE_LUAJIT +# define xmi_lua_setglobal(lua, s) xmi_lua_setfield(lua, LUA_GLOBALSINDEX, (s)) +#else +# define xmi_lua_setglobal(lua, name) (g_lua_ops)->_lua_setglobal(lua, name) +# define xmi_lua_seti(lua, idx, n) (g_lua_ops)->_lua_seti(lua, idx, n) +# define xmi_lua_rawsetp(lua, idx, p) (g_lua_ops)->_lua_rawsetp(lua, idx, p) +# define xmi_lua_setiuservalue(lua, idx, n) (g_lua_ops)->_lua_setiuservalue(lua, idx, n) +#endif +#define xmi_lua_settable(lua, idx) (g_lua_ops)->_lua_settable(lua, idx) +#define xmi_lua_setfield(lua, idx, k) (g_lua_ops)->_lua_setfield(lua, idx, k) +#define xmi_lua_rawset(lua, idx) (g_lua_ops)->_lua_rawset(lua, idx) +#define xmi_lua_rawseti(lua, idx, n) (g_lua_ops)->_lua_rawseti(lua, idx, n) +#define xmi_lua_setmetatable(lua, objidx) (g_lua_ops)->_lua_setmetatable(lua, objidx) +#define xmi_lua_setuservalue(lua, idx) xmi_lua_setiuservalue(lua, idx, 1) + +// access macros +#define xmi_lua_isnumber(lua, idx) (g_lua_ops)->_lua_isnumber(lua, idx) +#define xmi_lua_isstring(lua, idx) (g_lua_ops)->_lua_isstring(lua, idx) +#define xmi_lua_iscfunction(lua, idx) (g_lua_ops)->_lua_iscfunction(lua, idx) +#define xmi_lua_isuserdata(lua, idx) (g_lua_ops)->_lua_isuserdata(lua, idx) +#define xmi_lua_type(lua, idx) (g_lua_ops)->_lua_type(lua, idx) +#define xmi_lua_typename(lua, idx) (g_lua_ops)->_lua_typename(lua, idx) +#ifndef XMI_USE_LUAJIT +# define xmi_lua_isinteger(lua, idx) (g_lua_ops)->_lua_isinteger(lua, idx) +#endif + +#define xmi_lua_isfunction(lua, n) (xmi_lua_type(lua, (n)) == XMI_LUA_TFUNCTION) +#define xmi_lua_istable(lua, n) (xmi_lua_type(lua, (n)) == XMI_LUA_TTABLE) +#define xmi_lua_islightuserdata(lua, n) (xmi_lua_type(lua, (n)) == XMI_LUA_TLIGHTUSERDATA) +#define xmi_lua_isnil(lua, n) (xmi_lua_type(lua, (n)) == XMI_LUA_TNIL) +#define xmi_lua_isboolean(lua, n) (xmi_lua_type(lua, (n)) == XMI_LUA_TBOOLEAN) +#define xmi_lua_isthread(lua, n) (xmi_lua_type(lua, (n)) == XMI_LUA_TTHREAD) +#define xmi_lua_isnone(lua, n) (xmi_lua_type(lua, (n)) == XMI_LUA_TNONE) +#define xmi_lua_isnoneornil(lua, n) (xmi_lua_type(lua, (n)) <= 0) + +#define xmi_lua_tonumberx(lua, idx, isnum) (g_lua_ops)->_lua_tonumberx(lua, idx, isnum) +#define xmi_lua_tointegerx(lua, idx, isnum) (g_lua_ops)->_lua_tointegerx(lua, idx, isnum) +#define xmi_lua_toboolean(lua, idx) (g_lua_ops)->_lua_toboolean(lua, idx) +#define xmi_lua_tolstring(lua, idx, len) (g_lua_ops)->_lua_tolstring(lua, idx, len) +#define xmi_lua_rawlen(lua, idx) (g_lua_ops)->_lua_rawlen(lua, idx) +#define xmi_lua_tocfunction(lua, idx) (g_lua_ops)->_lua_tocfunction(lua, idx) +#define xmi_lua_touserdata(lua, idx) (g_lua_ops)->_lua_touserdata(lua, idx) +#define xmi_lua_tothread(lua, idx) (g_lua_ops)->_lua_tothread(lua, idx) +#define xmi_lua_topointer(lua, idx) (g_lua_ops)->_lua_topointer(lua, idx) +#define xmi_lua_tonumber(lua, idx) xmi_lua_tonumberx(lua, (idx), NULL) +#define xmi_lua_tostring(lua, idx) xmi_lua_tolstring(lua, (idx), NULL) +#define xmi_lua_tointeger(lua, idx) xmi_lua_tointegerx(lua, (idx), NULL) + +// push macros +#define xmi_lua_pushnil(lua) (g_lua_ops)->_lua_pushnil(lua) +#define xmi_lua_pushinteger(lua, n) (g_lua_ops)->_lua_pushinteger(lua, n) +#define xmi_lua_pushboolean(lua, b) (g_lua_ops)->_lua_pushboolean(lua, b) +#define xmi_lua_pushnumber(lua, n) (g_lua_ops)->_lua_pushnumber(lua, n) +#define xmi_lua_pushlstring(lua, s, len) (g_lua_ops)->_lua_pushlstring(lua, s, len) +#define xmi_lua_pushstring(lua, s) (g_lua_ops)->_lua_pushstring(lua, s) +#define xmi_lua_pushvfstring(lua, fmt, argp) (g_lua_ops)->_lua_pushvfstring(lua, fmt, argp) +#if defined(_MSC_VER) +# define xmi_lua_pushfstring(lua, fmt, ...) (g_lua_ops)->_lua_pushfstring(lua, fmt, __VA_ARGS__) +#else +# define xmi_lua_pushfstring(lua, fmt, arg ...) (g_lua_ops)->_lua_pushfstring(lua, fmt, ## arg) +#endif +#define xmi_lua_pushcclosure(lua, fn, n) (g_lua_ops)->_lua_pushcclosure(lua, fn, n) +#define xmi_lua_pushlightuserdata(lua, p) (g_lua_ops)->_lua_pushlightuserdata(lua, p) +#define xmi_lua_pushthread(lua) (g_lua_ops)->_lua_pushthread(lua) +#define xmi_lua_pushcfunction(lua, f) xmi_lua_pushcclosure(lua, (f), 0) +#define xmi_lua_pushliteral(lua, s) xmi_lua_pushstring(lua, "" s) + +// stack functions +#ifdef XMI_USE_LUAJIT +# define xmi_lua_insert(lua, idx) (g_lua_ops)->_lua_insert(lua, idx) +# define xmi_lua_remove(lua, idx) (g_lua_ops)->_lua_remove(lua, idx) +# define xmi_lua_replace(lua, idx) (g_lua_ops)->_lua_replace(lua, idx) +#else +# define xmi_lua_absindex(lua, idx) (g_lua_ops)->_lua_absindex(lua, idx) +# define xmi_lua_rotate(lua, idx, n) (g_lua_ops)->_lua_rotate(lua, idx, n) +# define xmi_lua_insert(lua, idx) xmi_lua_rotate(lua, (idx), 1) +# define xmi_lua_remove(lua, idx) (xmi_lua_rotate(lua, (idx), -1), xmi_lua_pop(lua, 1)) +# define xmi_lua_replace(lua, idx) (xmi_lua_copy(lua, -1, (idx)), xmi_lua_pop(lua, 1)) +#endif +#define xmi_lua_gettop(lua) (g_lua_ops)->_lua_gettop(lua) +#define xmi_lua_settop(lua, idx) (g_lua_ops)->_lua_settop(lua, idx) +#define xmi_lua_pushvalue(lua, idx) (g_lua_ops)->_lua_pushvalue(lua, idx) +#define xmi_lua_copy(lua, fromidx, toidx) (g_lua_ops)->_lua_copy(lua, fromidx, toidx) +#define xmi_lua_checkstack(lua, n) (g_lua_ops)->_lua_checkstack(lua, n) +#define xmi_lua_xmove(from, to, n) (g_lua_ops)->_lua_xmove(from, to, n) + +// miscellaneous functions +#define xmi_lua_error(lua) (g_lua_ops)->_lua_error(lua) +#define xmi_lua_next(lua, idx) (g_lua_ops)->_lua_next(lua, idx) +#define xmi_lua_concat(lua, n) (g_lua_ops)->_lua_concat(lua, n) +#define xmi_lua_getallocf(lua, ud) (g_lua_ops)->_lua_getallocf(lua, ud) +#define xmi_lua_setallocf(lua, f, ud) (g_lua_ops)->_lua_setallocf(lua, f, ud) +#ifndef XMI_USE_LUAJIT +# define xmi_lua_len(lua, idx) (g_lua_ops)->_lua_len(lua, idx) +# define xmi_lua_toclose(lua, idx) (g_lua_ops)->_lua_toclose(lua, idx) +# define xmi_lua_closeslot(lua, idx) (g_lua_ops)->_lua_closeslot(lua, idx) +# define xmi_lua_stringtonumber(lua, s) (g_lua_ops)->_lua_stringtonumber(lua, s) +#endif + +// 'load' and 'call' functions +#ifdef XMI_USE_LUAJIT +# define xmi_lua_call(lua, n, nr) (g_lua_ops)->_lua_call(lua, n, nr) +# define xmi_lua_pcall(lua, n, nr, ef) (g_lua_ops)->_lua_pcall(lua, n, nr, ef) +# define xmi_lua_load(lua, r, dt, ch) (g_lua_ops)->_lua_load(lua, r, dt, ch) +# define xmi_lua_dump(lua, w, d) (g_lua_ops)->_lua_dump(lua, r, d) +#else +# define xmi_lua_callk(lua, n, nr, ctx, k) (g_lua_ops)->_lua_callk(lua, n, nr, ctx, k) +# define xmi_lua_pcallk(lua, n, nr, ef, ctx, k) (g_lua_ops)->_lua_pcallk(lua, n, nr, ef, ctx, k) +# define xmi_lua_call(lua, n, r) xmi_lua_callk(lua, (n), (r), 0, NULL) +# define xmi_lua_pcall(lua, n, r, f) xmi_lua_pcallk(lua, (n), (r), (f), 0, NULL) +# define xmi_lua_load(lua, r, dt, ch, mode) (g_lua_ops)->_lua_load(lua, r, dt, ch, mode) +# define xmi_lua_dump(lua, w, d, strip) (g_lua_ops)->_lua_dump(lua, r, d, strip) +#endif + +// luaL macros +#ifndef XMI_USE_LUAJIT +# define xmi_luaL_tolstring(lua, idx, len) (g_lua_ops)->_luaL_tolstring(lua, idx, len) +# define xmi_luaL_typeerror(lua, arg, tname) (g_lua_ops)->_luaL_typeerror(lua, arg, tname) +#endif +#define xmi_luaL_getmetafield(lua, obj, e) (g_lua_ops)->_luaL_getmetafield(lua, obj, e) +#define xmi_luaL_callmeta(lua, obj, e) (g_lua_ops)->_luaL_callmeta(lua, obj, e) +#define xmi_luaL_argerror(lua, numarg, extramsg) (g_lua_ops)->_luaL_argerror(lua, numarg, extramsg) +#define xmi_luaL_checklstring(lua, arg, l) (g_lua_ops)->_luaL_checklstring(lua, arg, l) +#define xmi_luaL_optlstring(lua, arg, def, l) (g_lua_ops)->_luaL_optlstring(lua, arg, def, l) +#define xmi_luaL_checknumber(lua, arg) (g_lua_ops)->_luaL_checknumber(lua, arg) +#define xmi_luaL_optnumber(lua, arg, def) (g_lua_ops)->_luaL_optnumber(lua, arg, def) +#define xmi_luaL_checkinteger(lua, idx) (g_lua_ops)->_luaL_checkinteger(lua, idx) +#define xmi_luaL_optinteger(lua, arg, def) (g_lua_ops)->_luaL_optinteger(lua, arg, def) +#define xmi_luaL_checkstack(lua, sz, msg) (g_lua_ops)->_luaL_checkstack(lua, sz, msg) +#define xmi_luaL_checktype(lua, arg, t) (g_lua_ops)->_luaL_checktype(lua, arg, t) +#define xmi_luaL_checkany(lua, arg) (g_lua_ops)->_luaL_checkany(lua, arg) +#define xmi_luaL_newmetatable(lua, tname) (g_lua_ops)->_luaL_newmetatable(lua, tname) +#define xmi_luaL_setmetatable(lua, tname) (g_lua_ops)->_luaL_setmetatable(lua, tname) +#define xmi_luaL_testudata(lua, tname) (g_lua_ops)->_luaL_testudata(lua, tname) +#define xmi_luaL_checkudata(lua, tname) (g_lua_ops)->_luaL_checkudata(lua, tname) +#define xmi_luaL_where(lua, lvl) (g_lua_ops)->_luaL_where(lua, lvl) +#if defined(_MSC_VER) +# define xmi_luaL_error(lua, fmt, ...) (g_lua_ops)->_luaL_error(lua, fmt, __VA_ARGS__) +#else +# define xmi_luaL_error(lua, fmt, arg ...) (g_lua_ops)->_luaL_error(lua, fmt, ## arg) +#endif +#define xmi_luaL_checkoption(lua, arg, def, lst) (g_lua_ops)->_luaL_checkoption(lua, arg, def, lst) +#define xmi_luaL_fileresult(lua, stat, fname) (g_lua_ops)->_luaL_fileresult(lua, stat, fname) +#define xmi_luaL_execresult(lua, stat) (g_lua_ops)->_luaL_execresult(lua, stat) +#define xmi_luaL_setfuncs(lua, l, nup) (g_lua_ops)->_luaL_setfuncs(lua, l, nup) + +#define xmi_luaL_newlibtable(lua, l) xml_lua_createtable(lua, 0, sizeof(l)/sizeof((l)[0]) - 1) +#define xmi_luaL_newlib(lua, l) \ + (xmi_luaL_checkversion(lua), xmi_luaL_newlibtable(lua,l), xmi_luaL_setfuncs(lua,l,0)) +#define xmi_luaL_argcheck(lua, cond, arg, extramsg) \ + ((void)(luai_likely(cond) || xmi_luaL_argerror(lua, (arg), (extramsg)))) +#define xmi_luaL_argexpected(lua, cond, arg, tname) \ + ((void)(luai_likely(cond) || xmi_luaL_typeerror(lua, (arg), (tname)))) +#define xmi_luaL_checkstring(lua, n) (xmi_luaL_checklstring(lua, (n), NULL)) +#define xmi_luaL_optstring(lua, n, d) (xmi_luaL_optlstring(lua, (n), (d), NULL)) +#define xmi_luaL_typename(lua, i) xmi_lua_typename(lua, xmi_lua_type(lua,(i))) +#define xmi_luaL_dofile(lua, fn) \ + (xmi_luaL_loadfile(lua, fn) || xmi_lua_pcall(lua, 0, XMI_LUA_MULTRET, 0)) +#define xmi_luaL_dostring(lua, s) \ + (xmi_luaL_loadstring(lua, s) || xmi_lua_pcall(lua, 0, XMI_LUA_MULTRET, 0)) +#define xmi_luaL_getmetatable(lua, n) (xmi_lua_getfield(lua, XMI_LUA_REGISTRYINDEX, (n))) +#define xmi_luaL_opt(lua, f, n, d) (xmi_lua_isnoneornil(lua,(n)) ? (d) : f(lua,(n))) +#define xmi_luaL_loadbuffer(lua, s, sz, n) xmi_luaL_loadbufferx(lua, s, sz, n, NULL) +#define xmi_luaL_pushfail(lua) xmi_lua_pushnil(lua) + +/* we cannot redefine lua functions in loadxmi.c, + * because original lua.h has been included + */ +#ifndef XM_PREFIX_H + +// thread status +# define LUA_OK XMI_LUA_OK +# define LUA_YIELD XMI_LUA_YIELD +# define LUA_ERRRUN XMI_LUA_ERRRUN +# define LUA_ERRSYNTAX XMI_LUA_ERRSYNTAX +# define LUA_ERRMEM XMI_LUA_ERRMEM +# define LUA_ERRERR XMI_LUA_ERRERR + +// basic types +# define LUA_TNONE XMI_LUA_TNONE +# define LUA_TNIL XMI_LUA_TNIL +# define LUA_TBOOLEAN XMI_LUA_TBOOLEAN +# define LUA_TLIGHTUSERDATA XMI_LUA_TLIGHTUSERDATA +# define LUA_TNUMBER XMI_LUA_TNUMBER +# define LUA_TSTRING XMI_LUA_TSTRING +# define LUA_TTABLE XMI_LUA_TTABLE +# define LUA_TFUNCTION XMI_LUA_TFUNCTION +# define LUA_TUSERDATA XMI_LUA_TUSERDATA +# define LUA_TTHREAD XMI_LUA_TTHREAD +# define LUA_NUMTYPES XMI_LUA_NUMTYPES + +// get macros +# define lua_getglobal xmi_lua_getglobal +# define lua_gettable xmi_lua_gettable +# define lua_getfield xmi_lua_getfield +# define lua_geti xmi_lua_geti +# define lua_rawget xmi_lua_rawget +# define lua_rawgeti xmi_lua_rawgeti +# define lua_rawgetp xmi_lua_rawgetp +# define lua_createtable xmi_lua_createtable +# define lua_newuserdatauv xmi_lua_newuserdatauv +# define lua_getmetatable xmi_lua_getmetatable +# define lua_getiuservalue xmi_lua_getiuservalue +# define lua_upvalueindex xmi_lua_upvalueindex +# define lua_newtable xmi_lua_newtable +# define lua_pop xmi_lua_pop +# define lua_newuserdata xmi_lua_newuserdata +# define lua_getuservalue xmi_lua_getuservalue + +// set macros +# define lua_setglobal xmi_lua_setglobal +# define lua_settable xmi_lua_settable +# define lua_setfield xmi_lua_setfield +# define lua_seti xmi_lua_seti +# define lua_rawset xmi_lua_rawset +# define lua_rawseti xmi_lua_rawseti +# define lua_rawsetp xmi_lua_rawsetp +# define lua_setmetatable xmi_lua_setmetatable +# define lua_setiuservalue xmi_lua_setiuservalue +# define lua_setuservalue xmi_lua_setuservalue + +// access macros +# define lua_isnumber xmi_lua_isnumber +# define lua_isstring xmi_lua_isstring +# define lua_iscfunction xmi_lua_iscfunction +# define lua_isuserdata xmi_lua_isuserdata +# define lua_type xmi_lua_type +# define lua_typename xmi_lua_typename +# ifndef XMI_USE_LUAJIT +# define lua_isinteger xmi_lua_isinteger +# endif + +# define lua_isfunction xmi_lua_isfunction +# define lua_istable xmi_lua_istable +# define lua_islightuserdata xmi_lua_islightuserdata +# define lua_isnil xmi_lua_isnil +# define lua_isboolean xmi_lua_isboolean +# define lua_isthread xmi_lua_isthread +# define lua_isnone xmi_lua_isnone +# define lua_isnoneornil xmi_lua_isnoneornil + +# define lua_tonumberx xmi_lua_tonumberx +# define lua_tointegerx xmi_lua_tointegerx +# define lua_toboolean xmi_lua_toboolean +# define lua_tolstring xmi_lua_tolstring +# define lua_rawlen xmi_lua_rawlen +# define lua_tocfunction xmi_lua_tocfunction +# define lua_touserdata xmi_lua_touserdata +# define lua_tothread xmi_lua_tothread +# define lua_topointer xmi_lua_topointer +# define lua_tostring xmi_lua_tostring +# define lua_tonumber xmi_lua_tonumber +# define lua_tointeger xmi_lua_tointeger + +// push macros +# define lua_pushnil xmi_lua_pushnil +# define lua_pushinteger xmi_lua_pushinteger +# define lua_pushboolean xmi_lua_pushboolean +# define lua_pushnumber xmi_lua_pushnumber +# define lua_pushlstring xmi_lua_pushlstring +# define lua_pushstring xmi_lua_pushstring +# define lua_pushvfstring xmi_lua_pushvfstring +# define lua_pushfstring xmi_lua_pushfstring +# define lua_pushcclosure xmi_lua_pushcclosure +# define lua_pushlightuserdata xmi_lua_pushlightuserdata +# define lua_pushthread xmi_lua_pushthread +# define lua_pushcfunction xmi_lua_pushcfunction +# define lua_pushliteral xmi_lua_pushliteral + +// stack functions +# define lua_absindex xmi_lua_absindex +# define lua_gettop xmi_lua_gettop +# define lua_settop xmi_lua_settop +# define lua_pushvalue xmi_lua_pushvalue +# define lua_rotate xmi_lua_rotate +# define lua_copy xmi_lua_copy +# define lua_checkstack xmi_lua_checkstack +# define lua_xmove xmi_lua_xmove +# define lua_insert xmi_lua_insert +# define lua_remove xmi_lua_remove +# define lua_replace xmi_lua_replace + +// miscellaneous functions +# define lua_error xmi_lua_error +# define lua_next xmi_lua_next +# define lua_concat xmi_lua_concat +# define lua_len xmi_lua_len +# define lua_stringtonumber xmi_lua_stringtonumber +# define lua_getallocf xmi_lua_getallocf +# define lua_setallocf xmi_lua_setallocf +# define lua_toclose xmi_lua_toclose +# define lua_closeslot xmi_lua_closeslot + +// 'load' and 'call' functions +# ifndef XMI_USE_LUAJIT +# define lua_callk xmi_lua_callk +# define lua_pcallk xmi_lua_pcallk +# endif +# define lua_call xmi_lua_call +# define lua_pcall xmi_lua_pcall +# define lua_load xmi_lua_load +# define lua_dump xmi_lua_dump + +// luaL macros +# define luaL_getmetafield xmi_luaL_getmetafield +# define luaL_callmeta xmi_luaL_callmeta +# define luaL_tolstring xmi_luaL_tolstring +# define luaL_argerror xmi_luaL_argerror +# define luaL_typeerror xmi_luaL_typeerror +# define luaL_checklstring xmi_luaL_checklstring +# define luaL_optlstring xmi_luaL_optlstring +# define luaL_checknumber xmi_luaL_checknumber +# define luaL_optnumber xmi_luaL_optnumber +# define luaL_checkinteger xmi_luaL_checkinteger +# define luaL_optinteger xmi_luaL_optinteger +# define luaL_checkstack xmi_luaL_checkstack +# define luaL_checktype xmi_luaL_checktype +# define luaL_checkany xmi_luaL_checkany +# define luaL_newmetatable xmi_luaL_newmetatable +# define luaL_setmetatable xmi_luaL_setmetatable +# define luaL_testudata xmi_luaL_testudata +# define luaL_checkudata xmi_luaL_checkudata +# define luaL_where xmi_luaL_where +# define luaL_error xmi_luaL_error +# define luaL_checkoption xmi_luaL_checkoption +# define luaL_fileresult xmi_luaL_fileresult +# define luaL_execresult xmi_luaL_execresult +# define luaL_setfuncs xmi_luaL_setfuncs + +# define luaL_newlibtable xmi_luaL_newlibtable +# define luaL_newlib xmi_luaL_newlib +# define luaL_argcheck xmi_luaL_argcheck +# define luaL_argexpected xmi_luaL_argexpected +# define luaL_checkstring xmi_luaL_checkstring +# define luaL_optstring xmi_luaL_optstring +# define luaL_typename xmi_luaL_typename +# define luaL_dofile xmi_luaL_dofile +# define luaL_dostring xmi_luaL_dostring +# define luaL_getmetatable xmi_luaL_getmetatable +# define luaL_opt xmi_luaL_opt +# define luaL_loadbuffer xmi_luaL_loadbuffer +# define luaL_pushfail xmi_luaL_pushfail + +// types +# define luaL_Reg xmi_luaL_Reg +# define lua_State xmi_lua_State +# define lua_Number xmi_lua_Number +# define lua_Integer xmi_lua_Integer +# define lua_Unsigned xmi_lua_Unsigned +# define lua_KContext xmi_lua_KContext +# define lua_CFunction xmi_lua_CFunction +# define lua_KFunction xmi_lua_KFunction +# define lua_Reader xmi_lua_Reader +# define lua_Writer xmi_lua_Writer +# define lua_Alloc xmi_lua_Alloc +# define lua_WarnFunction xmi_lua_WarnFunction +#endif + +// extern c +#ifdef __cplusplus +# define xmi_extern_c_enter extern "C" { +# define xmi_extern_c_leave } +#else +# define xmi_extern_c_enter +# define xmi_extern_c_leave +#endif + +// define lua module entry function +#define luaopen(name, lua) \ +__dummy = 1; \ +xmi_lua_ops_t* g_lua_ops = NULL; \ +xmi_extern_c_enter \ +int xmiopen_##name(lua); \ +xmi_extern_c_leave \ +int xmisetup(xmi_lua_ops_t* ops) { \ + g_lua_ops = ops; \ + return __dummy; \ +} \ +int xmiopen_##name(lua) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ +typedef LUA_NUMBER xmi_lua_Number; +typedef LUA_INTEGER xmi_lua_Integer; +#ifndef XMI_USE_LUAJIT +typedef LUA_UNSIGNED xmi_lua_Unsigned; +typedef LUA_KCONTEXT xmi_lua_KContext; +#endif + +typedef struct xmi_lua_State_ { + int dummy; +}xmi_lua_State; + +typedef int (*xmi_lua_CFunction)(lua_State* lua); +#ifndef XMI_USE_LUAJIT +typedef int (*xmi_lua_KFunction)(lua_State* lua, int status, lua_KContext ctx); +#endif +typedef const char* (*xmi_lua_Reader)(lua_State* lua, void* ud, size_t* sz); +typedef int (*xmi_lua_Writer)(lua_State* lua, const void* p, size_t sz, void* ud); +typedef void* (*xmi_lua_Alloc)(void* ud, void* ptr, size_t osize, size_t nsize); +typedef void (*xmi_lua_WarnFunction)(void* ud, const char* msg, int tocont); + +typedef struct xmi_luaL_Reg_ { + char const* name; + xmi_lua_CFunction func; +}xmi_luaL_Reg; + +typedef struct xmi_lua_ops_t_ { + + // get functions +#ifdef XMI_USE_LUAJIT + void* (*_lua_newuserdata)(lua_State* lua, size_t sz); + void (*_lua_gettable)(lua_State* lua, int idx); + void (*_lua_getfield)(lua_State* lua, int idx, const char* k); + void (*_lua_rawgeti)(lua_State* lua, int idx, int n); + void (*_lua_rawget)(lua_State* lua, int idx); +#else + int (*_lua_getglobal)(lua_State* lua, const char* name); + int (*_lua_geti)(lua_State* lua, int idx, lua_Integer n); + int (*_lua_rawgetp)(lua_State* lua, int idx, const void* p); + int (*_lua_getiuservalue)(lua_State* lua, int idx, int n); + void* (*_lua_newuserdatauv)(lua_State* lua, size_t sz, int nuvalue); + int (*_lua_gettable)(lua_State* lua, int idx); + int (*_lua_getfield)(lua_State* lua, int idx, const char* k); + int (*_lua_rawgeti)(lua_State* lua, int idx, lua_Integer n); + int (*_lua_rawget)(lua_State* lua, int idx); +#endif + void (*_lua_createtable)(lua_State* lua, int narr, int nrec); + int (*_lua_getmetatable)(lua_State* lua, int objindex); + + // set functions +#ifdef XMI_USE_LUAJIT + void (*_lua_rawseti)(lua_State* lua, int idx, int n); +#else + void (*_lua_setglobal)(lua_State* lua, const char* name); + void (*_lua_seti)(lua_State* lua, int idx, lua_Integer n); + void (*_lua_rawsetp)(lua_State* lua, int idx, const void* p); + int (*_lua_setiuservalue)(lua_State* lua, int idx, int n); + void (*_lua_rawseti)(lua_State* lua, int idx, lua_Integer n); +#endif + void (*_lua_settable)(lua_State* lua, int idx); + void (*_lua_setfield)(lua_State* lua, int idx, const char* k); + void (*_lua_rawset)(lua_State* lua, int idx); + int (*_lua_setmetatable)(lua_State* lua, int objindex); + + // access functions + int (*_lua_isnumber)(lua_State* lua, int idx); + int (*_lua_isstring)(lua_State* lua, int idx); + int (*_lua_iscfunction)(lua_State* lua, int idx); + int (*_lua_isuserdata)(lua_State* lua, int idx); + int (*_lua_type)(lua_State* lua, int idx); + const char* (*_lua_typename)(lua_State* lua, int tp); +#ifndef XMI_USE_LUAJIT + int (*_lua_isinteger)(lua_State* lua, int idx); +#endif + + lua_Number (*_lua_tonumberx)(lua_State* lua, int idx, int* isnum); + lua_Integer (*_lua_tointegerx)(lua_State* lua, int idx, int* isnum); + int (*_lua_toboolean)(lua_State* lua, int idx); + const char* (*_lua_tolstring)(lua_State* lua, int idx, size_t* len); + lua_CFunction (*_lua_tocfunction)(lua_State* lua, int idx); + void* (*_lua_touserdata)(lua_State* lua, int idx); + lua_State* (*_lua_tothread)(lua_State* lua, int idx); + const void* (*_lua_topointer)(lua_State* lua, int idx); +#ifndef XMI_USE_LUAJIT + lua_Unsigned (*_lua_rawlen)(lua_State* lua, int idx); +#endif + + // push functions + void (*_lua_pushnil)(lua_State* lua); + void (*_lua_pushinteger)(lua_State* lua, lua_Integer n); + void (*_lua_pushboolean)(lua_State* lua, int b); + void (*_lua_pushnumber)(lua_State* lua, lua_Number n); +#ifdef XMI_USE_LUAJIT + void (*_lua_pushlstring)(lua_State* lua, const char* s, size_t len); + void (*_lua_pushstring)(lua_State* lua, const char* s); +#else + const char* (*_lua_pushlstring)(lua_State* lua, const char* s, size_t len); + const char* (*_lua_pushstring)(lua_State* lua, const char* s); +#endif + const char* (*_lua_pushvfstring)(lua_State* lua, const char* fmt, va_list argp); + const char* (*_lua_pushfstring)(lua_State* lua, const char* fmt, ...); + void (*_lua_pushcclosure)(lua_State* lua, lua_CFunction fn, int n); + void (*_lua_pushlightuserdata)(lua_State* lua, void* p); + int (*_lua_pushthread)(lua_State* lua); + + // stack functions +#ifdef XMI_USE_LUAJIT + void (*_lua_insert)(lua_State* lua, int idx); + void (*_lua_remove)(lua_State* lua, int idx); + void (*_lua_replace)(lua_State* lua, int idx); +#else + int (*_lua_absindex)(lua_State* lua, int idx); + void (*_lua_rotate)(lua_State* lua, int idx, int n); +#endif + int (*_lua_gettop)(lua_State* lua); + void (*_lua_settop)(lua_State* lua, int idx); + void (*_lua_pushvalue)(lua_State* lua, int idx); + void (*_lua_copy)(lua_State* lua, int fromidx, int toidx); + int (*_lua_checkstack)(lua_State* lua, int n); + void (*_lua_xmove)(lua_State* from, lua_State* to, int n); + + // miscellaneous functions + int (*_lua_error)(lua_State* lua); + int (*_lua_next)(lua_State* lua, int idx); + void (*_lua_concat)(lua_State* lua, int n); + lua_Alloc (*_lua_getallocf)(lua_State* lua, void** ud); + void (*_lua_setallocf)(lua_State* lua, lua_Alloc f, void* ud); +#ifndef XMI_USE_LUAJIT + void (*_lua_len)(lua_State* lua, int idx); + void (*_lua_toclose)(lua_State* lua, int idx); + void (*_lua_closeslot)(lua_State* lua, int idx); + size_t (*_lua_stringtonumber)(lua_State* lua, const char* s); +#endif + + // 'load' and 'call' functions +#ifdef XMI_USE_LUAJIT + void (*_lua_call)(lua_State* lua, int nargs, int nresults); + int (*_lua_pcall)(lua_State* lua, int nargs, int nresults, int errfunc); + int (*_lua_load)(lua_State* lua, lua_Reader reader, void* dt, const char* chunkname); + int (*_lua_dump)(lua_State* lua, lua_Writer writer, void* data); +#else + void (*_lua_callk)(lua_State* lua, int nargs, int nresults, lua_KContext ctx, lua_KFunction k); + int (*_lua_pcallk)(lua_State* lua, int nargs, int nresults, int errfunc, lua_KContext ctx, lua_KFunction k); + int (*_lua_load)(lua_State* lua, lua_Reader reader, void* dt, const char* chunkname, const char* mode); + int (*_lua_dump)(lua_State* lua, lua_Writer writer, void* data, int strip); +#endif + + // luaL functions +#ifndef XMI_USE_LUAJIT + const char* (*_luaL_tolstring)(lua_State* lua, int idx, size_t* len); + int (*_luaL_typeerror)(lua_State* lua, int arg, const char* tname); +#endif + int (*_luaL_getmetafield)(lua_State* lua, int obj, const char* e); + int (*_luaL_callmeta)(lua_State* lua, int obj, const char* e); + int (*_luaL_argerror)(lua_State* lua, int numarg, const char* extramsg); + const char* (*_luaL_checklstring)(lua_State* lua, int arg, size_t* l); + const char* (*_luaL_optlstring)(lua_State* lua, int arg, const char* def, size_t* l); + lua_Number (*_luaL_checknumber)(lua_State* lua, int arg); + lua_Number (*_luaL_optnumber)(lua_State* lua, int arg, lua_Number def); + lua_Integer (*_luaL_checkinteger)(lua_State* lua, int idx); + lua_Integer (*_luaL_optinteger)(lua_State* lua, int arg, lua_Integer def); + void (*_luaL_checkstack)(lua_State* lua, int sz, const char* msg); + void (*_luaL_checktype)(lua_State* lua, int arg, int t); + void (*_luaL_checkany)(lua_State* lua, int arg); + int (*_luaL_newmetatable)(lua_State* lua, const char* tname); + void (*_luaL_setmetatable)(lua_State* lua, const char* tname); + void* (*_luaL_testudata)(lua_State* lua, int ud, const char* tname); + void* (*_luaL_checkudata)(lua_State* lua, int ud, const char* tname); + void (*_luaL_where)(lua_State* lua, int lvl); + int (*_luaL_error)(lua_State* lua, const char* fmt, ...); + int (*_luaL_checkoption)(lua_State* lua, int arg, const char* def, const char* const lst[]); + int (*_luaL_fileresult)(lua_State* lua, int stat, const char* fname); + int (*_luaL_execresult)(lua_State* lua, int stat); + void (*_luaL_setfuncs)(lua_State* lua, const luaL_Reg* l, int nup); + +}xmi_lua_ops_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * globals + */ +extern xmi_lua_ops_t* g_lua_ops; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ +xmi_extern_c_enter + +// setup lua interfaces +int xmisetup(xmi_lua_ops_t* ops); + +xmi_extern_c_leave +#endif + + diff --git a/xmake/templates/c++/module.shared/project/modules/shared/foo/src/foo.cpp b/xmake/templates/c++/module.shared/project/modules/shared/foo/src/foo.cpp index b6eb451ba7a..a98baca027c 100644 --- a/xmake/templates/c++/module.shared/project/modules/shared/foo/src/foo.cpp +++ b/xmake/templates/c++/module.shared/project/modules/shared/foo/src/foo.cpp @@ -1,8 +1,4 @@ -#include -extern "C" { -#include -#include -} +#include static int add(lua_State* lua) { int a = lua_tointeger(lua, 1); @@ -18,16 +14,14 @@ static int sub(lua_State* lua) { return 1; } -static const luaL_Reg g_funcs[] = { - {"add", add}, - {"sub", sub}, - {NULL, NULL} -}; - -extern "C" { - int luaopen_foo(lua_State* lua) { - lua_newtable(lua); - luaL_setfuncs(lua, g_funcs, 0); - return 1; - } +int luaopen(foo, lua_State* lua) { + static const luaL_Reg funcs[] = { + {"add", add}, + {"sub", sub}, + {NULL, NULL} + }; + lua_newtable(lua); + luaL_setfuncs(lua, funcs, 0); + return 1; } + diff --git a/xmake/templates/c++/module.shared/project/modules/shared/foo/xmake.lua b/xmake/templates/c++/module.shared/project/modules/shared/foo/xmake.lua index deef51363f6..9b3423d41d2 100644 --- a/xmake/templates/c++/module.shared/project/modules/shared/foo/xmake.lua +++ b/xmake/templates/c++/module.shared/project/modules/shared/foo/xmake.lua @@ -1,9 +1,6 @@ add_rules("mode.debug", "mode.release") -add_requires("lua 5.4") - target("foo") add_rules("module.shared") add_files("src/foo.cpp") - add_packages("lua") diff --git a/xmake/templates/c/module.shared/project/modules/shared/foo/src/foo.c b/xmake/templates/c/module.shared/project/modules/shared/foo/src/foo.c index 3024f9380f0..a98baca027c 100644 --- a/xmake/templates/c/module.shared/project/modules/shared/foo/src/foo.c +++ b/xmake/templates/c/module.shared/project/modules/shared/foo/src/foo.c @@ -1,6 +1,4 @@ -#include -#include -#include +#include static int add(lua_State* lua) { int a = lua_tointeger(lua, 1); @@ -16,14 +14,14 @@ static int sub(lua_State* lua) { return 1; } -static const luaL_Reg g_funcs[] = { - {"add", add}, - {"sub", sub}, - {NULL, NULL} -}; - -int luaopen_foo(lua_State* lua) { +int luaopen(foo, lua_State* lua) { + static const luaL_Reg funcs[] = { + {"add", add}, + {"sub", sub}, + {NULL, NULL} + }; lua_newtable(lua); - luaL_setfuncs(lua, g_funcs, 0); + luaL_setfuncs(lua, funcs, 0); return 1; } + diff --git a/xmake/templates/c/module.shared/project/modules/shared/foo/xmake.lua b/xmake/templates/c/module.shared/project/modules/shared/foo/xmake.lua index ad4590ffab0..035573d9f5e 100644 --- a/xmake/templates/c/module.shared/project/modules/shared/foo/xmake.lua +++ b/xmake/templates/c/module.shared/project/modules/shared/foo/xmake.lua @@ -1,9 +1,6 @@ add_rules("mode.debug", "mode.release") -add_requires("lua 5.4") - target("foo") add_rules("module.shared") add_files("src/foo.c") - add_packages("lua")