Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve native shared module to remove lua package dependence #4932

Merged
merged 41 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
817353b
add zoo module
waruqi Apr 3, 2024
d08d95a
improve xmi.h
waruqi Apr 3, 2024
70748de
improve loadxmi
waruqi Apr 3, 2024
953bb53
fix loadxmi
waruqi Apr 3, 2024
f716446
use xmi.h
waruqi Apr 3, 2024
fee2d1b
improve xmiopen
waruqi Apr 3, 2024
b0bc1ce
fix xmiopen
waruqi Apr 3, 2024
c04f30a
fix init luaops
waruqi Apr 3, 2024
257bbb3
replace foo tests
waruqi Apr 3, 2024
89554d9
remove lua deps
waruqi Apr 3, 2024
5c56dcd
add cjson test
waruqi Apr 3, 2024
f56b7ac
add luaL_error
waruqi Apr 4, 2024
6142965
fix xmi.h
waruqi Apr 4, 2024
110401c
update xmi.h
waruqi Apr 4, 2024
3fa5c97
add more apis
waruqi Apr 4, 2024
68e8592
fix lua include
waruqi Apr 4, 2024
a23a38d
improve loadxmi
waruqi Apr 4, 2024
e86ea3e
add more helper lua funcs
waruqi Apr 4, 2024
57c6672
Update module.lua
waruqi Apr 4, 2024
1b5a2aa
add more apis
waruqi Apr 4, 2024
6f4a3aa
add lua_isxxx
waruqi Apr 4, 2024
7bcdab3
add toboolean
waruqi Apr 4, 2024
b8d9d41
add lua_pushxxx
waruqi Apr 4, 2024
5878db8
add get functions
waruqi Apr 4, 2024
160bfb9
add more macros
waruqi Apr 4, 2024
0dd39dc
fix install
waruqi Apr 4, 2024
18f0fd5
add stack functions
waruqi Apr 4, 2024
764c5c3
add set functions
waruqi Apr 4, 2024
4b91348
add more access functions
waruqi Apr 4, 2024
2bd085d
add miscellaneous functions
waruqi Apr 4, 2024
2fe23b1
add basic types
waruqi Apr 4, 2024
1aeccb9
add more luaL functions
waruqi Apr 5, 2024
05f7602
add call functions
waruqi Apr 5, 2024
5bbb027
add thread status
waruqi Apr 5, 2024
1f4516a
add more stack function
waruqi Apr 5, 2024
448b9bb
modify funcstion for luajit
waruqi Apr 5, 2024
aec0f76
update xmi.h
waruqi Apr 5, 2024
bcce486
support for luajit native module
waruqi Apr 5, 2024
ddd7ea7
fix tolstring
waruqi Apr 5, 2024
df7a9c2
format code
waruqi Apr 5, 2024
5297e23
fix compile error
waruqi Apr 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/src/demo/xmake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
13 changes: 13 additions & 0 deletions core/src/xmake/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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");
Expand Down
228 changes: 228 additions & 0 deletions core/src/xmake/package/loadxmi.c
Original file line number Diff line number Diff line change
@@ -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;
}
32 changes: 32 additions & 0 deletions core/src/xmake/package/prefix.h
Original file line number Diff line number Diff line change
@@ -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


1 change: 1 addition & 0 deletions core/src/xmake/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Expand Down
2 changes: 2 additions & 0 deletions core/src/xmake/xmake.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
19 changes: 8 additions & 11 deletions tests/projects/other/native_module/modules/shared/foo/src/foo.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include <stdlib.h>
#include <lua.h>
#include <lauxlib.h>
#include <xmi.h>

static int add(lua_State* lua) {
int a = lua_tointeger(lua, 1);
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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")

4 changes: 1 addition & 3 deletions tests/projects/other/native_module/test.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
function main(t)
if not xmake.luajit() then
t:build()
end
t:build()
end
12 changes: 6 additions & 6 deletions tests/projects/other/native_module/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Loading
Loading