summaryrefslogtreecommitdiff
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
parent4181bf780192cbf4be257fb7b658cfb390684a12 (diff)
improve bytes
-rw-r--r--core/src/xmake/engine.c4
-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
-rw-r--r--core/src/xmake/makefile2
-rw-r--r--xmake/core/base/bytes.lua10
-rw-r--r--xmake/core/base/libc.lua14
14 files changed, 31 insertions, 178 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index e7c4c9644..e6ecec020 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -228,8 +228,6 @@ tb_int_t xm_libc_memcpy(lua_State* lua);
tb_int_t xm_libc_memset(lua_State* lua);
tb_int_t xm_libc_strndup(lua_State* lua);
tb_int_t xm_libc_dataptr(lua_State* lua);
-tb_int_t xm_libc_ptraddr(lua_State* lua);
-tb_int_t xm_libc_diffptr(lua_State* lua);
tb_int_t xm_libc_byteof(lua_State* lua);
tb_int_t xm_libc_setbyte(lua_State* lua);
@@ -433,8 +431,6 @@ static luaL_Reg const g_libc_functions[] =
, { "memset", xm_libc_memset }
, { "strndup", xm_libc_strndup }
, { "dataptr", xm_libc_dataptr }
-, { "ptraddr", xm_libc_ptraddr }
-, { "diffptr", xm_libc_diffptr }
, { "byteof", xm_libc_byteof }
, { "setbyte", xm_libc_setbyte }
, { tb_null, tb_null }
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)!");
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index 5d3236c2d..b29e8cc20 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -121,8 +121,6 @@ xmake_C_FILES += \
libc/memset \
libc/memcpy \
libc/dataptr \
- libc/ptraddr \
- libc/diffptr \
libc/byteof \
libc/setbyte \
libc/strndup
diff --git a/xmake/core/base/bytes.lua b/xmake/core/base/bytes.lua
index 300d2a073..9f7b6ca4f 100644
--- a/xmake/core/base/bytes.lua
+++ b/xmake/core/base/bytes.lua
@@ -96,7 +96,7 @@ function _instance.new(...)
os.raise("incorrect bounds(%d-%d) for bytes(...)!", start, last)
end
instance._SIZE = last - start + 1
- instance._CDATA = libc.diffptr(b:cdata(), -1 + start)
+ instance._CDATA = b:cdata() -1 + start
instance._REF = b -- keep lua ref for GC
instance._MANAGED = false
instance._READONLY = b:readonly()
@@ -109,7 +109,7 @@ function _instance.new(...)
instance._CDATA = libc.malloc(instance._SIZE, {gc = true})
local offset = 0
for _, b in ipairs(args) do
- libc.memcpy(libc.diffptr(instance._CDATA, offset), b:cdata(), b:size())
+ libc.memcpy(instance._CDATA + offset, b:cdata(), b:size())
offset = offset + b:size()
end
instance._MANAGED = true
@@ -124,7 +124,7 @@ function _instance.new(...)
instance._CDATA = libc.malloc(instance._SIZE, {gc = true})
local offset = 0
for _, b in ipairs(args) do
- libc.memcpy(libc.diffptr(instance._CDATA, offset), b._CDATA, b:size())
+ libc.memcpy(instance._CDATA + offset, b._CDATA, b:size())
offset = offset + b:size()
end
instance._MANAGED = true
@@ -138,7 +138,7 @@ function _instance.new(...)
os.raise("incorrect bounds(%d-%d)!", start, last)
end
instance._SIZE = last - start + 1
- instance._CDATA = libc.diffptr(b:cdata(), -1 + start)
+ instance._CDATA = b:cdata() -1 + start
instance._REF = b -- keep lua ref for GC
instance._MANAGED = false
instance._READONLY = b:readonly()
@@ -324,7 +324,7 @@ end
-- convert bytes to string
function _instance:str(i, j)
local offset = i and i - 1 or 0
- return libc.strndup(libc.diffptr(self:cdata(), offset), (j or self:size()) - offset)
+ return libc.strndup(self:cdata() + offset, (j or self:size()) - offset)
end
-- get uint8 value
diff --git a/xmake/core/base/libc.lua b/xmake/core/base/libc.lua
index 6e69dd203..9aecb8af5 100644
--- a/xmake/core/base/libc.lua
+++ b/xmake/core/base/libc.lua
@@ -28,8 +28,6 @@ libc._memcpy = libc._memcpy or libc.memcpy
libc._memset = libc._memset or libc.memset
libc._strndup = libc._strndup or libc.strndup
libc._dataptr = libc._dataptr or libc.dataptr
-libc._ptraddr = libc._ptraddr or libc.ptraddr
-libc._diffptr = libc._diffptr or libc.diffptr
libc._byteof = libc._byteof or libc.byteof
libc._setbyte = libc._setbyte or libc.setbyte
@@ -112,14 +110,6 @@ function libc.setbyte(data, offset, value)
end
end
-function libc.diffptr(data, offset)
- if ffi then
- return data + offset
- else
- return libc._diffptr(data, offset)
- end
-end
-
function libc.dataptr(data, opt)
if ffi then
if opt and opt.gc then
@@ -128,7 +118,7 @@ function libc.dataptr(data, opt)
return ffi.cast("unsigned char*", data)
end
else
- return libc._dataptr(data)
+ return type(data) == "number" and data or libc._dataptr(data)
end
end
@@ -136,7 +126,7 @@ function libc.ptraddr(data)
if ffi then
return tonumber(ffi.cast('unsigned long long', data))
else
- return libc._ptraddr(data)
+ return data
end
end