diff options
| author | ruki <[email protected]> | 2024-04-05 16:21:00 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-04-05 16:21:00 +0800 |
| commit | 982a307fa363ae022554df2afaa3f922c5bb0986 (patch) | |
| tree | 279590bb6068b94eeade11693217b11d21e0db35 /core/src | |
| parent | 282e468d6cf18473bac127d260fe802e7bb64392 (diff) | |
| parent | 5297e2329198a683400870382699af2f37d25ec2 (diff) | |
Merge pull request #4932 from xmake-io/native
Improve native shared module to remove lua package dependence
Diffstat (limited to 'core/src')
| -rwxr-xr-x | core/src/demo/xmake.sh | 1 | ||||
| -rw-r--r-- | core/src/xmake/engine.c | 13 | ||||
| -rw-r--r-- | core/src/xmake/package/loadxmi.c | 228 | ||||
| -rw-r--r-- | core/src/xmake/package/prefix.h | 32 | ||||
| -rw-r--r-- | core/src/xmake/xmake.lua | 1 | ||||
| -rwxr-xr-x | core/src/xmake/xmake.sh | 2 |
6 files changed, 277 insertions, 0 deletions
diff --git a/core/src/demo/xmake.sh b/core/src/demo/xmake.sh index a6342ce4e..05b1047ca 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 827d46d2e..b63dcf42b 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 000000000..43915aff3 --- /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 000000000..e710e526c --- /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 03e733c9a..15702c808 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 a813cf078..62af5b2fa 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" |
