From 817353b79fc9145c18992facd920ec8f9497000b Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 3 Apr 2024 23:34:46 +0800 Subject: add zoo module --- tests/projects/other/native_module/xmake.lua | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'tests/projects/other/native_module/xmake.lua') diff --git a/tests/projects/other/native_module/xmake.lua b/tests/projects/other/native_module/xmake.lua index e403454bb..3148fc0f2 100644 --- a/tests/projects/other/native_module/xmake.lua +++ b/tests/projects/other/native_module/xmake.lua @@ -7,10 +7,13 @@ target("test") add_files("src/*.cpp") on_config(function (target) import("shared.foo") + import("shared.zoo") 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)) + print("foo: 1 + 1 = %d", foo.add(1, 1)) + print("foo: 1 - 1 = %d", foo.sub(1, 1)) + print("zoo: 1 + 1 = %d", zoo.add(1, 1)) + print("zoo: 1 - 1 = %d", zoo.sub(1, 1)) + print("bar: 1 + 1 = %s", bar.add(1, 1)) + print("bar: 1 - 1 = %s", bar.sub(1, 1)) end) -- cgit v1.3.1 From d08d95ac8fc9e1f316ff180aaedd61e21c384e24 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 3 Apr 2024 23:42:56 +0800 Subject: improve xmi.h --- .../native_module/modules/shared/foo/src/foo.c | 13 +++---- .../native_module/modules/shared/zoo/src/zoo.c | 32 ++++++++++++--- tests/projects/other/native_module/xmake.lua | 2 +- xmake/rules/module/xmake.lua | 1 + xmake/scripts/module/xmi.h | 45 ++++++++++++++++++++++ .../project/modules/shared/foo/src/foo.cpp | 13 +++---- .../project/modules/shared/foo/src/foo.c | 13 +++---- 7 files changed, 91 insertions(+), 28 deletions(-) create mode 100644 xmake/scripts/module/xmi.h (limited to 'tests/projects/other/native_module/xmake.lua') 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 3024f9380..c8ac1c81c 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 @@ -16,14 +16,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) { + 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/zoo/src/zoo.c b/tests/projects/other/native_module/modules/shared/zoo/src/zoo.c index b9bf90ec5..a93fb0dab 100644 --- a/tests/projects/other/native_module/modules/shared/zoo/src/zoo.c +++ b/tests/projects/other/native_module/modules/shared/zoo/src/zoo.c @@ -1,12 +1,32 @@ -#include +#include -int add(int a, int b) { - return a + b; +static int add(lua_State* lua) { +#if 0 + int a = lua_tointeger(lua, 1); + int b = lua_tointeger(lua, 2); + lua_pushinteger(lua, a + b); +#endif + return 1; } -int sub(int a, int b) { - return a + b; +static int sub(lua_State* lua) { +#if 0 + int a = lua_tointeger(lua, 1); + int b = lua_tointeger(lua, 2); + lua_pushinteger(lua, a - b); +#endif + return 1; } -void module_zoo() { +int xmi_luaopen_foo(lua_State* lua) { + static const luaL_Reg funcs[] = { + {"add", add}, + {"sub", sub}, + {NULL, NULL} + }; +#if 0 + lua_newtable(lua); + luaL_setfuncs(lua, funcs, 0); +#endif + return 1; } diff --git a/tests/projects/other/native_module/xmake.lua b/tests/projects/other/native_module/xmake.lua index 3148fc0f2..3f895bb12 100644 --- a/tests/projects/other/native_module/xmake.lua +++ b/tests/projects/other/native_module/xmake.lua @@ -7,7 +7,7 @@ target("test") add_files("src/*.cpp") on_config(function (target) import("shared.foo") - import("shared.zoo") + import("shared.zoo", {always_build = true}) import("binary.bar", {always_build = true}) print("foo: 1 + 1 = %d", foo.add(1, 1)) print("foo: 1 - 1 = %d", foo.sub(1, 1)) diff --git a/xmake/rules/module/xmake.lua b/xmake/rules/module/xmake.lua index f70c71d7e..2b1b75262 100644 --- a/xmake/rules/module/xmake.lua +++ b/xmake/rules/module/xmake.lua @@ -35,5 +35,6 @@ rule("module.shared") target:set("basename", "module_" .. target:name()) target:set("targetdir", config.buildir()) target:set("strip", "none") + target:add("sysincludedirs", path.join(os.programdir(), "scripts", "module")) end) diff --git a/xmake/scripts/module/xmi.h b/xmake/scripts/module/xmi.h new file mode 100644 index 000000000..598d23ef3 --- /dev/null +++ b/xmake/scripts/module/xmi.h @@ -0,0 +1,45 @@ +/*!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 + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +struct lua_State_; +typedef struct luaL_Reg_ { + char const* name; + int (*func)(struct lua_State_* lua); +}luaL_Reg; + +typedef struct lua_State_ { + void* ctx; +}lua_State; + +#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 b6eb451ba..579c6646f 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 @@ -18,16 +18,15 @@ 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) { + 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/src/foo.c b/xmake/templates/c/module.shared/project/modules/shared/foo/src/foo.c index 3024f9380..c8ac1c81c 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 @@ -16,14 +16,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) { + 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; } -- cgit v1.3.1 From 257bbb3fa4a5c2f47f4a9481ea5a6899aac27a2c Mon Sep 17 00:00:00 2001 From: ruki Date: Thu, 4 Apr 2024 00:52:36 +0800 Subject: replace foo tests --- .../native_module/modules/shared/foo/src/foo.c | 6 ++--- .../native_module/modules/shared/foo/xmake.lua | 3 --- .../native_module/modules/shared/zoo/src/zoo.c | 26 --------------------- .../native_module/modules/shared/zoo/xmake.lua | 6 ----- tests/projects/other/native_module/xmake.lua | 7 ++---- xmake/rules/module/xmake.lua | 3 +-- xmake/scripts/module/xmi.h | 22 +++++++++++------- .../project/modules/shared/foo/src/foo.cpp | 27 +++++++++------------- .../project/modules/shared/foo/src/foo.c | 7 +++--- 9 files changed, 33 insertions(+), 74 deletions(-) delete mode 100644 tests/projects/other/native_module/modules/shared/zoo/src/zoo.c delete mode 100644 tests/projects/other/native_module/modules/shared/zoo/xmake.lua (limited to 'tests/projects/other/native_module/xmake.lua') 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 c8ac1c81c..0555b071f 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,7 +14,7 @@ static int sub(lua_State* lua) { return 1; } -int luaopen_foo(lua_State* lua) { +int luaopen(foo, lua_State* lua) { static const luaL_Reg funcs[] = { {"add", add}, {"sub", sub}, 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 ad4590ffa..035573d9f 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/modules/shared/zoo/src/zoo.c b/tests/projects/other/native_module/modules/shared/zoo/src/zoo.c deleted file mode 100644 index b297ee302..000000000 --- a/tests/projects/other/native_module/modules/shared/zoo/src/zoo.c +++ /dev/null @@ -1,26 +0,0 @@ -#include - -static int add(lua_State* lua) { - int a = lua_tointeger(lua, 1); - int b = lua_tointeger(lua, 2); - lua_pushinteger(lua, a + b); - return 1; -} - -static int sub(lua_State* lua) { - int a = lua_tointeger(lua, 1); - int b = lua_tointeger(lua, 2); - lua_pushinteger(lua, a - b); - return 1; -} - -int luaopen(zoo, 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/tests/projects/other/native_module/modules/shared/zoo/xmake.lua b/tests/projects/other/native_module/modules/shared/zoo/xmake.lua deleted file mode 100644 index a74c73637..000000000 --- a/tests/projects/other/native_module/modules/shared/zoo/xmake.lua +++ /dev/null @@ -1,6 +0,0 @@ -add_rules("mode.debug", "mode.release") - -target("zoo") - add_rules("module.shared") - add_files("src/zoo.c") - diff --git a/tests/projects/other/native_module/xmake.lua b/tests/projects/other/native_module/xmake.lua index 3f895bb12..211e839d3 100644 --- a/tests/projects/other/native_module/xmake.lua +++ b/tests/projects/other/native_module/xmake.lua @@ -6,13 +6,10 @@ target("test") set_kind("binary") add_files("src/*.cpp") on_config(function (target) - import("shared.foo") - import("shared.zoo", {always_build = true}) - import("binary.bar", {always_build = true}) + 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("zoo: 1 + 1 = %d", zoo.add(1, 1)) - print("zoo: 1 - 1 = %d", zoo.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/xmake/rules/module/xmake.lua b/xmake/rules/module/xmake.lua index 2b1b75262..1667c5c2a 100644 --- a/xmake/rules/module/xmake.lua +++ b/xmake/rules/module/xmake.lua @@ -29,12 +29,11 @@ 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("sysincludedirs", path.join(os.programdir(), "scripts", "module")) + target:add("includedirs", path.join(os.programdir(), "scripts", "module")) end) diff --git a/xmake/scripts/module/xmi.h b/xmake/scripts/module/xmi.h index 2f828e31d..cfddb3aee 100644 --- a/xmake/scripts/module/xmi.h +++ b/xmake/scripts/module/xmi.h @@ -61,10 +61,22 @@ # define lua_Integer xmi_lua_Integer #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; \ @@ -100,18 +112,12 @@ extern xmi_lua_ops_t* g_lua_ops; /* ////////////////////////////////////////////////////////////////////////////////////// * interfaces */ - -#ifdef __cplusplus -extern "C" { -#endif +xmi_extern_c_enter // setup lua interfaces int xmisetup(xmi_lua_ops_t* ops); -#ifdef __cplusplus -} -#endif - +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 579c6646f..a98baca02 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,15 +14,14 @@ static int sub(lua_State* lua) { return 1; } -extern "C" { - 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; - } +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/src/foo.c b/xmake/templates/c/module.shared/project/modules/shared/foo/src/foo.c index c8ac1c81c..a98baca02 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,7 +14,7 @@ static int sub(lua_State* lua) { return 1; } -int luaopen_foo(lua_State* lua) { +int luaopen(foo, lua_State* lua) { static const luaL_Reg funcs[] = { {"add", add}, {"sub", sub}, @@ -26,3 +24,4 @@ int luaopen_foo(lua_State* lua) { luaL_setfuncs(lua, funcs, 0); return 1; } + -- cgit v1.3.1