diff options
| author | ruki <[email protected]> | 2021-09-21 00:33:49 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-09-21 00:33:49 +0800 |
| commit | 6bd955fbacc03792d8b36dffa087967e9e056e31 (patch) | |
| tree | 91c05b8f4a7c047add5f7d61c323e47361a8aef7 /core/src/xmake/libc | |
| parent | 1e14751dcb53640e9592be678e3ebf6a46d611e1 (diff) | |
fix strndup
Diffstat (limited to 'core/src/xmake/libc')
| -rw-r--r-- | core/src/xmake/libc/dataptr.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/libc/diffptr.c | 57 | ||||
| -rw-r--r-- | core/src/xmake/libc/free.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/libc/malloc.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/libc/ptraddr.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/libc/strndup.c | 6 |
6 files changed, 62 insertions, 9 deletions
diff --git a/core/src/xmake/libc/dataptr.c b/core/src/xmake/libc/dataptr.c index 4f983e5e7..a15fc108b 100644 --- a/core/src/xmake/libc/dataptr.c +++ b/core/src/xmake/libc/dataptr.c @@ -50,6 +50,6 @@ tb_int_t xm_libc_dataptr(lua_State* lua) xm_lua_pushpointer(lua, (tb_pointer_t)cstr); return 1; } - xm_libc_return_error(lua, "dataptr(invalid data)!"); + xm_libc_return_error(lua, "libc.dataptr(invalid data)!"); } diff --git a/core/src/xmake/libc/diffptr.c b/core/src/xmake/libc/diffptr.c new file mode 100644 index 000000000..213c843e7 --- /dev/null +++ b/core/src/xmake/libc/diffptr.c @@ -0,0 +1,57 @@ +/*!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 diffptr.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "diffptr" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_libc_diffptr(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // get data + tb_pointer_t data = tb_null; + if (xm_lua_ispointer(lua, 1)) + data = (tb_pointer_t)xm_lua_topointer(lua, 1); + else if (lua_isstring(lua, 1)) + data = (tb_pointer_t)luaL_checkstring(lua, 1); + else xm_libc_return_error(lua, "libc.diffptr(invalid data)!"); + + // get offset + tb_int_t offset = 0; + if (lua_isnumber(lua, 2)) + offset = (tb_int_t)lua_tonumber(lua, 2); + else xm_libc_return_error(lua, "libc.diffptr(invalid offset)!"); + xm_lua_pushpointer(lua, data + offset); + return 1; +} + diff --git a/core/src/xmake/libc/free.c b/core/src/xmake/libc/free.c index b4d17f1d0..f2edd3598 100644 --- a/core/src/xmake/libc/free.c +++ b/core/src/xmake/libc/free.c @@ -40,7 +40,7 @@ tb_int_t xm_libc_free(lua_State* lua) // check arguments? if (!xm_lua_ispointer(lua, 1)) - xm_libc_return_error(lua, "free(invalid data)!"); + xm_libc_return_error(lua, "libc.free(invalid data)!"); // do free tb_pointer_t data = (tb_pointer_t)xm_lua_topointer(lua, 1); diff --git a/core/src/xmake/libc/malloc.c b/core/src/xmake/libc/malloc.c index 0ee81e68f..bb4b4271f 100644 --- a/core/src/xmake/libc/malloc.c +++ b/core/src/xmake/libc/malloc.c @@ -40,7 +40,7 @@ tb_int_t xm_libc_malloc(lua_State* lua) // check arguments? if (!lua_isnumber(lua, 1)) - xm_libc_return_error(lua, "malloc(invalid size)!"); + xm_libc_return_error(lua, "libc.malloc(invalid size)!"); // do malloc tb_pointer_t data = tb_null; diff --git a/core/src/xmake/libc/ptraddr.c b/core/src/xmake/libc/ptraddr.c index 347bf60bf..6c9645cc2 100644 --- a/core/src/xmake/libc/ptraddr.c +++ b/core/src/xmake/libc/ptraddr.c @@ -50,6 +50,6 @@ tb_int_t xm_libc_ptraddr(lua_State* lua) lua_pushnumber(lua, (lua_Number)(tb_hize_t)cstr); return 1; } - xm_libc_return_error(lua, "ptraddr(invalid data)!"); + xm_libc_return_error(lua, "libc.ptraddr(invalid data)!"); } diff --git a/core/src/xmake/libc/strndup.c b/core/src/xmake/libc/strndup.c index e0e1a1e02..f2a3b8359 100644 --- a/core/src/xmake/libc/strndup.c +++ b/core/src/xmake/libc/strndup.c @@ -38,17 +38,13 @@ tb_int_t xm_libc_strndup(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - // check arguments? - if (!xm_lua_ispointer(lua, 1) || !xm_lua_ispointer(lua, 2) || !lua_isnumber(lua, 3)) - xm_libc_return_error(lua, "strndup(invalid args)!"); - // do strndup tb_char_t const* s = tb_null; if (xm_lua_ispointer(lua, 1)) s = (tb_char_t const*)xm_lua_topointer(lua, 1); else if (lua_isstring(lua, 2)) s = lua_tostring(lua, 2); - else xm_libc_return_error(lua, "strndup(invalid args)!"); + else xm_libc_return_error(lua, "libc.strndup(invalid args)!"); tb_int_t n = (tb_int_t)lua_tointeger(lua, 2); if (s && n >= 0) lua_pushlstring(lua, s, n); |
