diff options
| author | ruki <[email protected]> | 2021-09-19 23:48:25 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-09-19 23:48:25 +0800 |
| commit | 33b05eff154f9158a68b0808f4be0f6c88aa6361 (patch) | |
| tree | e1237eaf2a55ded0f55823562a712fd4cac0c1a8 /core/src | |
| parent | 974ae1de229ee3077c868da14adebc11ddac42ac (diff) | |
add libc module
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/engine.c | 13 | ||||
| -rw-r--r-- | core/src/xmake/libc/malloc.c | 52 | ||||
| -rw-r--r-- | core/src/xmake/libc/prefix.h | 44 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 3 | ||||
| -rw-r--r-- | core/src/xmake/string/endswith.c | 1 |
5 files changed, 111 insertions, 2 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 69aa1e452..0c52f6cf0 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -221,6 +221,9 @@ tb_int_t xm_semver_compare(lua_State* lua); tb_int_t xm_semver_satisfies(lua_State* lua); tb_int_t xm_semver_select(lua_State* lua); +// the libc functions +tb_int_t xm_libc_malloc(lua_State* lua); + #ifdef XM_CONFIG_API_HAVE_CURSES // register curses __tb_extern_c_enter__ @@ -412,6 +415,13 @@ static luaL_Reg const g_semver_functions[] = , { tb_null, tb_null } }; +// the libc functions +static luaL_Reg const g_libc_functions[] = +{ + { "malloc", xm_libc_malloc } +, { tb_null, tb_null } +}; + /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ @@ -854,6 +864,9 @@ xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_c // bind semver functions luaL_register(engine->lua, "semver", g_semver_functions); + // bind libc functions + luaL_register(engine->lua, "libc", g_libc_functions); + #ifdef XM_CONFIG_API_HAVE_CURSES // bind curses xm_curses_register(engine->lua); diff --git a/core/src/xmake/libc/malloc.c b/core/src/xmake/libc/malloc.c new file mode 100644 index 000000000..0ee81e68f --- /dev/null +++ b/core/src/xmake/libc/malloc.c @@ -0,0 +1,52 @@ +/*!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 malloc.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "malloc" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_libc_malloc(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // check arguments? + if (!lua_isnumber(lua, 1)) + xm_libc_return_error(lua, "malloc(invalid size)!"); + + // do malloc + tb_pointer_t data = tb_null; + tb_int_t size = (tb_int_t)lua_tointeger(lua, 1); + if (size > 0) data = tb_malloc(size); + xm_lua_pushpointer(lua, data); + return 1; +} + diff --git a/core/src/xmake/libc/prefix.h b/core/src/xmake/libc/prefix.h new file mode 100644 index 000000000..a220690d6 --- /dev/null +++ b/core/src/xmake/libc/prefix.h @@ -0,0 +1,44 @@ +/*!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_LIBC_PREFIX_H +#define XM_LIBC_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ + +// return libc error +#define xm_libc_return_error(lua, error) \ + do \ + { \ + lua_pushnil(lua); \ + lua_pushliteral(lua, error); \ + return 2; \ + } while (0) + +#endif + + diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 20bc49d82..a25bd0936 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -115,7 +115,8 @@ xmake_C_FILES += \ readline/readline \ readline/history_list \ readline/add_history \ - readline/clear_history + readline/clear_history \ + libc/malloc iswin = ifeq ($(PLAT),windows) diff --git a/core/src/xmake/string/endswith.c b/core/src/xmake/string/endswith.c index df574b657..8f64dbda6 100644 --- a/core/src/xmake/string/endswith.c +++ b/core/src/xmake/string/endswith.c @@ -48,7 +48,6 @@ tb_int_t xm_string_endswith(lua_State* lua) // string:endswith(suffix)? lua_pushboolean(lua, string_size >= suffix_size && !tb_strcmp(string + string_size - suffix_size, suffix)); - // ok return 1; } |
