summaryrefslogtreecommitdiff
path: root/core/src/xmake/libc
diff options
context:
space:
mode:
authorruki <[email protected]>2021-09-21 00:52:15 +0800
committerruki <[email protected]>2021-09-21 00:52:15 +0800
commitbce8f65cb9f619dfa30d7b4b1fa2da9fa56b58e1 (patch)
treee05136ba9eaa706a2d2cabab6732a9410b05e297 /core/src/xmake/libc
parent4181bf780192cbf4be257fb7b658cfb390684a12 (diff)
improve bytes
Diffstat (limited to 'core/src/xmake/libc')
-rw-r--r--core/src/xmake/libc/byteof.c6
-rw-r--r--core/src/xmake/libc/dataptr.c23
-rw-r--r--core/src/xmake/libc/diffptr.c57
-rw-r--r--core/src/xmake/libc/free.c6
-rw-r--r--core/src/xmake/libc/malloc.c8
-rw-r--r--core/src/xmake/libc/memcpy.c8
-rw-r--r--core/src/xmake/libc/memset.c6
-rw-r--r--core/src/xmake/libc/ptraddr.c55
-rw-r--r--core/src/xmake/libc/setbyte.c6
-rw-r--r--core/src/xmake/libc/strndup.c4
10 files changed, 24 insertions, 155 deletions
diff --git a/core/src/xmake/libc/byteof.c b/core/src/xmake/libc/byteof.c
index 9d0031559..b6d757c81 100644
--- a/core/src/xmake/libc/byteof.c
+++ b/core/src/xmake/libc/byteof.c
@@ -40,8 +40,8 @@ tb_int_t xm_libc_byteof(lua_State* lua)
// get data
tb_pointer_t data = tb_null;
- if (xm_lua_ispointer(lua, 1))
- data = (tb_pointer_t)xm_lua_topointer(lua, 1);
+ if (lua_isnumber(lua, 1))
+ data = (tb_pointer_t)(tb_size_t)lua_tointeger(lua, 1);
else if (lua_isstring(lua, 1))
data = (tb_pointer_t)luaL_checkstring(lua, 1);
else xm_libc_return_error(lua, "libc.byteof(invalid data)!");
@@ -49,7 +49,7 @@ tb_int_t xm_libc_byteof(lua_State* lua)
// get offset
tb_int_t offset = 0;
if (lua_isnumber(lua, 2))
- offset = (tb_int_t)lua_tonumber(lua, 2);
+ offset = (tb_int_t)lua_tointeger(lua, 2);
else xm_libc_return_error(lua, "libc.byteof(invalid offset)!");
lua_pushinteger(lua, ((tb_byte_t const*)data)[offset]);
return 1;
diff --git a/core/src/xmake/libc/dataptr.c b/core/src/xmake/libc/dataptr.c
index a15fc108b..d8d6c6f0d 100644
--- a/core/src/xmake/libc/dataptr.c
+++ b/core/src/xmake/libc/dataptr.c
@@ -38,18 +38,15 @@ tb_int_t xm_libc_dataptr(lua_State* lua)
// check
tb_assert_and_check_return_val(lua, 0);
- if (xm_lua_ispointer(lua, 1))
- {
- tb_pointer_t data = (tb_pointer_t)xm_lua_topointer(lua, 1);
- xm_lua_pushpointer(lua, data);
- return 1;
- }
- else if (lua_isstring(lua, 1))
- {
- tb_char_t const* cstr = luaL_checkstring(lua, 1);
- xm_lua_pushpointer(lua, (tb_pointer_t)cstr);
- return 1;
- }
- xm_libc_return_error(lua, "libc.dataptr(invalid data)!");
+ tb_pointer_t data = tb_null;
+ if (lua_isstring(lua, 1))
+ data = (tb_pointer_t)luaL_checkstring(lua, 1);
+ else if (lua_isnumber(lua, 1))
+ data = (tb_pointer_t)(tb_size_t)lua_tointeger(lua, 1);
+ else if (xm_lua_ispointer(lua, 1))
+ data = (tb_pointer_t)xm_lua_topointer(lua, 1);
+ else xm_libc_return_error(lua, "libc.dataptr(invalid data)!");
+ lua_pushinteger(lua, (lua_Integer)data);
+ return 1;
}
diff --git a/core/src/xmake/libc/diffptr.c b/core/src/xmake/libc/diffptr.c
deleted file mode 100644
index 213c843e7..000000000
--- a/core/src/xmake/libc/diffptr.c
+++ /dev/null
@@ -1,57 +0,0 @@
-/*!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 f2edd3598..4fa549975 100644
--- a/core/src/xmake/libc/free.c
+++ b/core/src/xmake/libc/free.c
@@ -38,12 +38,8 @@ tb_int_t xm_libc_free(lua_State* lua)
// check
tb_assert_and_check_return_val(lua, 0);
- // check arguments?
- if (!xm_lua_ispointer(lua, 1))
- xm_libc_return_error(lua, "libc.free(invalid data)!");
-
// do free
- tb_pointer_t data = (tb_pointer_t)xm_lua_topointer(lua, 1);
+ tb_pointer_t data = (tb_pointer_t)(tb_size_t)luaL_checkinteger(lua, 1);
if (data) tb_free(data);
return 0;
}
diff --git a/core/src/xmake/libc/malloc.c b/core/src/xmake/libc/malloc.c
index bb4b4271f..ae13547a3 100644
--- a/core/src/xmake/libc/malloc.c
+++ b/core/src/xmake/libc/malloc.c
@@ -38,15 +38,11 @@ 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, "libc.malloc(invalid size)!");
-
// do malloc
tb_pointer_t data = tb_null;
- tb_int_t size = (tb_int_t)lua_tointeger(lua, 1);
+ tb_long_t size = (tb_long_t)luaL_checkinteger(lua, 1);
if (size > 0) data = tb_malloc(size);
- xm_lua_pushpointer(lua, data);
+ lua_pushinteger(lua, (lua_Integer)data);
return 1;
}
diff --git a/core/src/xmake/libc/memcpy.c b/core/src/xmake/libc/memcpy.c
index e2c9af4c3..3f8d3b36e 100644
--- a/core/src/xmake/libc/memcpy.c
+++ b/core/src/xmake/libc/memcpy.c
@@ -38,13 +38,9 @@ tb_int_t xm_libc_memcpy(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, "memcpy(invalid args)!");
-
// do memcpy
- tb_pointer_t dst = (tb_pointer_t)xm_lua_topointer(lua, 1);
- tb_pointer_t src = (tb_pointer_t)xm_lua_topointer(lua, 2);
+ tb_pointer_t dst = (tb_pointer_t)(tb_size_t)luaL_checkinteger(lua, 1);
+ tb_pointer_t src = (tb_pointer_t)(tb_size_t)luaL_checkinteger(lua, 2);
tb_int_t size = (tb_int_t)lua_tointeger(lua, 3);
if (dst && src && size > 0)
tb_memcpy(dst, src, size);
diff --git a/core/src/xmake/libc/memset.c b/core/src/xmake/libc/memset.c
index c3171bace..5aef08105 100644
--- a/core/src/xmake/libc/memset.c
+++ b/core/src/xmake/libc/memset.c
@@ -38,12 +38,8 @@ tb_int_t xm_libc_memset(lua_State* lua)
// check
tb_assert_and_check_return_val(lua, 0);
- // check arguments?
- if (!xm_lua_ispointer(lua, 1) || !lua_isnumber(lua, 2) || !lua_isnumber(lua, 3))
- xm_libc_return_error(lua, "memset(invalid args)!");
-
// do memset
- tb_pointer_t data = (tb_pointer_t)xm_lua_topointer(lua, 1);
+ tb_pointer_t data = (tb_pointer_t)(tb_size_t)luaL_checkinteger(lua, 1);
tb_char_t ch = (tb_char_t)lua_tointeger(lua, 2);
tb_int_t size = (tb_int_t)lua_tointeger(lua, 3);
if (data && size > 0)
diff --git a/core/src/xmake/libc/ptraddr.c b/core/src/xmake/libc/ptraddr.c
deleted file mode 100644
index 6c9645cc2..000000000
--- a/core/src/xmake/libc/ptraddr.c
+++ /dev/null
@@ -1,55 +0,0 @@
-/*!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 ptraddr.c
- *
- */
-
-/* //////////////////////////////////////////////////////////////////////////////////////
- * trace
- */
-#define TB_TRACE_MODULE_NAME "ptraddr"
-#define TB_TRACE_MODULE_DEBUG (0)
-
-/* //////////////////////////////////////////////////////////////////////////////////////
- * includes
- */
-#include "prefix.h"
-
-/* //////////////////////////////////////////////////////////////////////////////////////
- * implementation
- */
-tb_int_t xm_libc_ptraddr(lua_State* lua)
-{
- // check
- tb_assert_and_check_return_val(lua, 0);
-
- if (xm_lua_ispointer(lua, 1))
- {
- tb_pointer_t data = (tb_pointer_t)xm_lua_topointer(lua, 1);
- lua_pushnumber(lua, (lua_Number)(tb_hize_t)data);
- return 1;
- }
- else if (lua_isstring(lua, 1))
- {
- tb_char_t const* cstr = luaL_checkstring(lua, 1);
- lua_pushnumber(lua, (lua_Number)(tb_hize_t)cstr);
- return 1;
- }
- xm_libc_return_error(lua, "libc.ptraddr(invalid data)!");
-}
-
diff --git a/core/src/xmake/libc/setbyte.c b/core/src/xmake/libc/setbyte.c
index 298e056a6..5027411a8 100644
--- a/core/src/xmake/libc/setbyte.c
+++ b/core/src/xmake/libc/setbyte.c
@@ -40,8 +40,8 @@ tb_int_t xm_libc_setbyte(lua_State* lua)
// get data
tb_pointer_t data = tb_null;
- if (xm_lua_ispointer(lua, 1))
- data = (tb_pointer_t)xm_lua_topointer(lua, 1);
+ if (lua_isnumber(lua, 1))
+ data = (tb_pointer_t)(tb_size_t)lua_tointeger(lua, 1);
else if (lua_isstring(lua, 1))
data = (tb_pointer_t)luaL_checkstring(lua, 1);
else xm_libc_return_error(lua, "libc.setbyte(invalid data)!");
@@ -49,7 +49,7 @@ tb_int_t xm_libc_setbyte(lua_State* lua)
// get offset
tb_int_t offset = 0;
if (lua_isnumber(lua, 2))
- offset = (tb_int_t)lua_tonumber(lua, 2);
+ offset = (tb_int_t)lua_tointeger(lua, 2);
else xm_libc_return_error(lua, "libc.setbyte(invalid offset)!");
// set byte
diff --git a/core/src/xmake/libc/strndup.c b/core/src/xmake/libc/strndup.c
index f2a3b8359..b56a76714 100644
--- a/core/src/xmake/libc/strndup.c
+++ b/core/src/xmake/libc/strndup.c
@@ -40,8 +40,8 @@ tb_int_t xm_libc_strndup(lua_State* lua)
// 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);
+ if (lua_isnumber(lua, 1))
+ s = (tb_char_t const*)(tb_size_t)lua_tointeger(lua, 1);
else if (lua_isstring(lua, 2))
s = lua_tostring(lua, 2);
else xm_libc_return_error(lua, "libc.strndup(invalid args)!");