diff options
| author | ruki <[email protected]> | 2020-08-04 22:45:20 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-08-04 22:45:20 +0800 |
| commit | 81c072eaa6eadde5f6427e0109226a7fa04691b0 (patch) | |
| tree | 7243963c4614209145b441144093889fface5102 /core/src/xmake | |
| parent | 8b08fbb9f5f0f54dd1232f4bf387932116dde47f (diff) | |
improve pushpointer
Diffstat (limited to 'core/src/xmake')
30 files changed, 145 insertions, 72 deletions
diff --git a/core/src/xmake/io/pipe_close.c b/core/src/xmake/io/pipe_close.c index f0bb4113e..94b5f2437 100644 --- a/core/src/xmake/io/pipe_close.c +++ b/core/src/xmake/io/pipe_close.c @@ -40,12 +40,12 @@ tb_int_t xm_io_pipe_close(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - // is user data? - if (!lua_isuserdata(lua, 1)) + // check pipe? + if (!xm_lua_ispointer(lua, 1)) return 0; // get the pipe file - tb_pipe_file_ref_t pipefile = (tb_pipe_file_ref_t)lua_touserdata(lua, 1); + tb_pipe_file_ref_t pipefile = (tb_pipe_file_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(pipefile, 0); // exit pipe file diff --git a/core/src/xmake/io/pipe_connect.c b/core/src/xmake/io/pipe_connect.c index 3c0fc58b5..b9a9a9c8e 100644 --- a/core/src/xmake/io/pipe_connect.c +++ b/core/src/xmake/io/pipe_connect.c @@ -41,7 +41,7 @@ tb_int_t xm_io_pipe_connect(lua_State* lua) tb_assert_and_check_return_val(lua, 0); // check pipe - if (!lua_isuserdata(lua, 1)) + if (!xm_lua_ispointer(lua, 1)) { lua_pushnumber(lua, -1); lua_pushliteral(lua, "invalid pipe!"); @@ -49,7 +49,7 @@ tb_int_t xm_io_pipe_connect(lua_State* lua) } // get pipe file - tb_pipe_file_ref_t pipefile = (tb_pipe_file_ref_t)lua_touserdata(lua, 1); + tb_pipe_file_ref_t pipefile = (tb_pipe_file_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(pipefile, 0); // connect pipe diff --git a/core/src/xmake/io/pipe_open.c b/core/src/xmake/io/pipe_open.c index 8a5c55c85..970ebee58 100644 --- a/core/src/xmake/io/pipe_open.c +++ b/core/src/xmake/io/pipe_open.c @@ -56,7 +56,7 @@ tb_int_t xm_io_pipe_open(lua_State* lua) // open pipe file tb_pipe_file_ref_t pipefile = tb_pipe_file_init(name, mode, buffsize); - if (pipefile) lua_pushlightuserdata(lua, (tb_pointer_t)pipefile); + if (pipefile) xm_lua_pushpointer(lua, (tb_pointer_t)pipefile); else lua_pushnil(lua); return 1; } diff --git a/core/src/xmake/io/pipe_openpair.c b/core/src/xmake/io/pipe_openpair.c index a1f42cc97..6aee0044f 100644 --- a/core/src/xmake/io/pipe_openpair.c +++ b/core/src/xmake/io/pipe_openpair.c @@ -49,8 +49,8 @@ tb_int_t xm_io_pipe_openpair(lua_State* lua) tb_pipe_file_ref_t pipefile[2]; if (tb_pipe_file_init_pair(pipefile, buffsize)) { - lua_pushlightuserdata(lua, (tb_pointer_t)pipefile[0]); - lua_pushlightuserdata(lua, (tb_pointer_t)pipefile[1]); + xm_lua_pushpointer(lua, (tb_pointer_t)pipefile[0]); + xm_lua_pushpointer(lua, (tb_pointer_t)pipefile[1]); } else { diff --git a/core/src/xmake/io/pipe_read.c b/core/src/xmake/io/pipe_read.c index 9e14a8c48..35e79f053 100644 --- a/core/src/xmake/io/pipe_read.c +++ b/core/src/xmake/io/pipe_read.c @@ -41,7 +41,7 @@ tb_int_t xm_io_pipe_read(lua_State* lua) tb_assert_and_check_return_val(lua, 0); // check pipe file - if (!lua_isuserdata(lua, 1)) + if (!xm_lua_ispointer(lua, 1)) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid pipe file!"); @@ -49,7 +49,7 @@ tb_int_t xm_io_pipe_read(lua_State* lua) } // get pipe file - tb_pipe_file_ref_t pipefile = (tb_pipe_file_ref_t)lua_touserdata(lua, 1); + tb_pipe_file_ref_t pipefile = (tb_pipe_file_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(pipefile, 0); // get data diff --git a/core/src/xmake/io/pipe_wait.c b/core/src/xmake/io/pipe_wait.c index ba4d68a7b..a92fc50c6 100644 --- a/core/src/xmake/io/pipe_wait.c +++ b/core/src/xmake/io/pipe_wait.c @@ -40,12 +40,12 @@ tb_int_t xm_io_pipe_wait(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - // is user data? - if (!lua_isuserdata(lua, 1)) + // check pipe? + if (!xm_lua_ispointer(lua, 1)) return 0; // get pipe file - tb_pipe_file_ref_t pipefile = (tb_pipe_file_ref_t)lua_touserdata(lua, 1); + tb_pipe_file_ref_t pipefile = (tb_pipe_file_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(pipefile, 0); // get events diff --git a/core/src/xmake/io/pipe_write.c b/core/src/xmake/io/pipe_write.c index 368230955..6830da81a 100644 --- a/core/src/xmake/io/pipe_write.c +++ b/core/src/xmake/io/pipe_write.c @@ -40,8 +40,8 @@ tb_int_t xm_io_pipe_write(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - // check pipe file - if (!lua_isuserdata(lua, 1)) + // check pipe + if (!xm_lua_ispointer(lua, 1)) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid pipe file!"); @@ -49,7 +49,7 @@ tb_int_t xm_io_pipe_write(lua_State* lua) } // get pipe file - tb_pipe_file_ref_t pipefile = (tb_pipe_file_ref_t)lua_touserdata(lua, 1); + tb_pipe_file_ref_t pipefile = (tb_pipe_file_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(pipefile, 0); // get data diff --git a/core/src/xmake/io/poller_insert.c b/core/src/xmake/io/poller_insert.c index 8de62ce3a..c0719e520 100644 --- a/core/src/xmake/io/poller_insert.c +++ b/core/src/xmake/io/poller_insert.c @@ -41,8 +41,8 @@ tb_int_t xm_io_poller_insert(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - // is user data? - if (!lua_isuserdata(lua, 2)) + // is pointer? + if (!xm_lua_ispointer(lua, 2)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "invalid poller object!"); @@ -53,7 +53,8 @@ tb_int_t xm_io_poller_insert(lua_State* lua) tb_uint8_t otype = (tb_uint8_t)luaL_checknumber(lua, 1); // get cdata - tb_pointer_t cdata = (tb_pointer_t)lua_touserdata(lua, 2); + tb_char_t const* cdata_str = tb_null; + tb_pointer_t cdata = (tb_pointer_t)xm_lua_topointer2(lua, 2, &cdata_str); tb_check_return_val(cdata, 0); // get events @@ -63,7 +64,7 @@ tb_int_t xm_io_poller_insert(lua_State* lua) tb_poller_object_t object; object.type = otype; object.ref.ptr = cdata; - lua_pushboolean(lua, tb_poller_insert(xm_io_poller(), &object, events, tb_null)); + lua_pushboolean(lua, tb_poller_insert(xm_io_poller(), &object, events, cdata_str)); return 1; } diff --git a/core/src/xmake/io/poller_modify.c b/core/src/xmake/io/poller_modify.c index 49473bf8b..764d5f6ce 100644 --- a/core/src/xmake/io/poller_modify.c +++ b/core/src/xmake/io/poller_modify.c @@ -41,8 +41,8 @@ tb_int_t xm_io_poller_modify(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - // is user data? - if (!lua_isuserdata(lua, 2)) + // is pointer? + if (!xm_lua_ispointer(lua, 2)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "invalid poller object!"); @@ -53,7 +53,8 @@ tb_int_t xm_io_poller_modify(lua_State* lua) tb_uint8_t otype = (tb_uint8_t)luaL_checknumber(lua, 1); // get cdata - tb_pointer_t cdata = (tb_pointer_t)lua_touserdata(lua, 2); + tb_char_t const* cdata_str = tb_null; + tb_pointer_t cdata = (tb_pointer_t)xm_lua_topointer2(lua, 2, &cdata_str); tb_check_return_val(cdata, 0); // get events @@ -63,7 +64,7 @@ tb_int_t xm_io_poller_modify(lua_State* lua) tb_poller_object_t object; object.type = otype; object.ref.ptr = cdata; - lua_pushboolean(lua, tb_poller_modify(xm_io_poller(), &object, events, tb_null)); + lua_pushboolean(lua, tb_poller_modify(xm_io_poller(), &object, events, cdata_str)); return 1; } diff --git a/core/src/xmake/io/poller_remove.c b/core/src/xmake/io/poller_remove.c index 848ab50f9..cb2fb45a8 100644 --- a/core/src/xmake/io/poller_remove.c +++ b/core/src/xmake/io/poller_remove.c @@ -41,8 +41,8 @@ tb_int_t xm_io_poller_remove(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - // is user data? - if (!lua_isuserdata(lua, 2)) + // is pointer? + if (!xm_lua_ispointer(lua, 2)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "invalid poller object!"); @@ -53,7 +53,7 @@ tb_int_t xm_io_poller_remove(lua_State* lua) tb_uint8_t otype = (tb_uint8_t)luaL_checknumber(lua, 1); // get cdata - tb_pointer_t cdata = (tb_pointer_t)lua_touserdata(lua, 2); + tb_pointer_t cdata = (tb_pointer_t)xm_lua_topointer(lua, 2); tb_check_return_val(cdata, 0); // remove events from poller diff --git a/core/src/xmake/io/poller_wait.c b/core/src/xmake/io/poller_wait.c index 4d531d68a..59fd8c8d8 100644 --- a/core/src/xmake/io/poller_wait.c +++ b/core/src/xmake/io/poller_wait.c @@ -51,7 +51,8 @@ static tb_void_t xm_io_poller_event(tb_poller_ref_t poller, tb_poller_object_ref lua_newtable(g_lua); lua_pushinteger(g_lua, (tb_int_t)object->type); lua_rawseti(g_lua, -2, 1); - lua_pushlightuserdata(g_lua, object->ref.ptr); + if (priv) lua_pushstring(g_lua, (tb_char_t const*)priv); + else lua_pushlightuserdata(g_lua, object->ref.ptr); lua_rawseti(g_lua, -2, 2); lua_pushinteger(g_lua, (tb_int_t)events); lua_rawseti(g_lua, -2, 3); diff --git a/core/src/xmake/io/socket_accept.c b/core/src/xmake/io/socket_accept.c index 5bd30c3be..53fa5627d 100644 --- a/core/src/xmake/io/socket_accept.c +++ b/core/src/xmake/io/socket_accept.c @@ -40,17 +40,17 @@ tb_int_t xm_io_socket_accept(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - // is user data? - if (!lua_isuserdata(lua, 1)) + // is pointer? + if (!xm_lua_ispointer(lua, 1)) return 0; // get socket - tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); + tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(sock, 0); // accept socket tb_socket_ref_t client = tb_socket_accept(sock, tb_null); - if (client) lua_pushlightuserdata(lua, (tb_pointer_t)client); + if (client) xm_lua_pushpointer(lua, (tb_pointer_t)client); else lua_pushnil(lua); return 1; } diff --git a/core/src/xmake/io/socket_bind.c b/core/src/xmake/io/socket_bind.c index dd3248084..41bffb8a3 100644 --- a/core/src/xmake/io/socket_bind.c +++ b/core/src/xmake/io/socket_bind.c @@ -41,7 +41,7 @@ tb_int_t xm_io_socket_bind(lua_State* lua) tb_assert_and_check_return_val(lua, 0); // check socket - if (!lua_isuserdata(lua, 1)) + if (!xm_lua_ispointer(lua, 1)) { lua_pushboolean(lua, tb_false); lua_pushliteral(lua, "invalid socket!"); @@ -49,7 +49,7 @@ tb_int_t xm_io_socket_bind(lua_State* lua) } // get socket - tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); + tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(sock, 0); // get address diff --git a/core/src/xmake/io/socket_close.c b/core/src/xmake/io/socket_close.c index 0966ea78d..a8eea4994 100644 --- a/core/src/xmake/io/socket_close.c +++ b/core/src/xmake/io/socket_close.c @@ -40,12 +40,12 @@ tb_int_t xm_io_socket_close(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - // is user data? - if (!lua_isuserdata(lua, 1)) + // is pointer? + if (!xm_lua_ispointer(lua, 1)) return 0; // get socket - tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); + tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(sock, 0); // exit socket diff --git a/core/src/xmake/io/socket_connect.c b/core/src/xmake/io/socket_connect.c index d7a8faa4d..f1a5acf86 100644 --- a/core/src/xmake/io/socket_connect.c +++ b/core/src/xmake/io/socket_connect.c @@ -41,7 +41,7 @@ tb_int_t xm_io_socket_connect(lua_State* lua) tb_assert_and_check_return_val(lua, 0); // check socket - if (!lua_isuserdata(lua, 1)) + if (!xm_lua_ispointer(lua, 1)) { lua_pushnumber(lua, -1); lua_pushliteral(lua, "invalid socket!"); @@ -49,7 +49,7 @@ tb_int_t xm_io_socket_connect(lua_State* lua) } // get socket - tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); + tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(sock, 0); // get address diff --git a/core/src/xmake/io/socket_listen.c b/core/src/xmake/io/socket_listen.c index 523dfefe4..3263080ab 100644 --- a/core/src/xmake/io/socket_listen.c +++ b/core/src/xmake/io/socket_listen.c @@ -40,12 +40,12 @@ tb_int_t xm_io_socket_listen(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - // is user data? - if (!lua_isuserdata(lua, 1)) + // is pointer? + if (!xm_lua_ispointer(lua, 1)) return 0; // get socket - tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); + tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(sock, 0); // get backlog diff --git a/core/src/xmake/io/socket_open.c b/core/src/xmake/io/socket_open.c index 48eb4ca43..fffbaea6e 100644 --- a/core/src/xmake/io/socket_open.c +++ b/core/src/xmake/io/socket_open.c @@ -64,7 +64,7 @@ tb_int_t xm_io_socket_open(lua_State* lua) // init socket tb_socket_ref_t sock = tb_socket_init(socktype, family); - if (sock) lua_pushlightuserdata(lua, (tb_pointer_t)sock); + if (sock) xm_lua_pushpointer(lua, (tb_pointer_t)sock); else lua_pushnil(lua); return 1; } diff --git a/core/src/xmake/io/socket_rawfd.c b/core/src/xmake/io/socket_rawfd.c index bcc973b26..23270ab6f 100644 --- a/core/src/xmake/io/socket_rawfd.c +++ b/core/src/xmake/io/socket_rawfd.c @@ -48,12 +48,12 @@ tb_int_t xm_io_socket_rawfd(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - // is user data? - if (!lua_isuserdata(lua, 1)) + // is pointer? + if (!xm_lua_ispointer(lua, 1)) xm_io_return_error(lua, "get rawfd for invalid sock!"); // get socket - tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); + tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(sock, 0); // return result diff --git a/core/src/xmake/io/socket_recv.c b/core/src/xmake/io/socket_recv.c index 8e09f14cb..97251dcbd 100644 --- a/core/src/xmake/io/socket_recv.c +++ b/core/src/xmake/io/socket_recv.c @@ -41,7 +41,7 @@ tb_int_t xm_io_socket_recv(lua_State* lua) tb_assert_and_check_return_val(lua, 0); // check socket - if (!lua_isuserdata(lua, 1)) + if (!xm_lua_ispointer(lua, 1)) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid socket!"); @@ -49,7 +49,7 @@ tb_int_t xm_io_socket_recv(lua_State* lua) } // get socket - tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); + tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(sock, 0); // get data diff --git a/core/src/xmake/io/socket_recvfrom.c b/core/src/xmake/io/socket_recvfrom.c index 0bda81042..fa476fc23 100644 --- a/core/src/xmake/io/socket_recvfrom.c +++ b/core/src/xmake/io/socket_recvfrom.c @@ -41,7 +41,7 @@ tb_int_t xm_io_socket_recvfrom(lua_State* lua) tb_assert_and_check_return_val(lua, 0); // check socket - if (!lua_isuserdata(lua, 1)) + if (!xm_lua_ispointer(lua, 1)) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid socket!"); @@ -49,7 +49,7 @@ tb_int_t xm_io_socket_recvfrom(lua_State* lua) } // get socket - tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); + tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(sock, 0); // get data diff --git a/core/src/xmake/io/socket_send.c b/core/src/xmake/io/socket_send.c index 81a24bd4b..696a4a4c1 100644 --- a/core/src/xmake/io/socket_send.c +++ b/core/src/xmake/io/socket_send.c @@ -41,7 +41,7 @@ tb_int_t xm_io_socket_send(lua_State* lua) tb_assert_and_check_return_val(lua, 0); // check socket - if (!lua_isuserdata(lua, 1)) + if (!xm_lua_ispointer(lua, 1)) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid socket!"); @@ -49,7 +49,7 @@ tb_int_t xm_io_socket_send(lua_State* lua) } // get socket - tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); + tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(sock, 0); // get data diff --git a/core/src/xmake/io/socket_sendfile.c b/core/src/xmake/io/socket_sendfile.c index f14c480d7..67dee4e6a 100644 --- a/core/src/xmake/io/socket_sendfile.c +++ b/core/src/xmake/io/socket_sendfile.c @@ -41,7 +41,7 @@ tb_int_t xm_io_socket_sendfile(lua_State* lua) tb_assert_and_check_return_val(lua, 0); // check socket - if (!lua_isuserdata(lua, 1)) + if (!xm_lua_ispointer(lua, 1)) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid socket!"); @@ -57,7 +57,7 @@ tb_int_t xm_io_socket_sendfile(lua_State* lua) } // get socket - tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); + tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(sock, 0); // get file diff --git a/core/src/xmake/io/socket_sendto.c b/core/src/xmake/io/socket_sendto.c index 03349cc4a..13c563907 100644 --- a/core/src/xmake/io/socket_sendto.c +++ b/core/src/xmake/io/socket_sendto.c @@ -41,7 +41,7 @@ tb_int_t xm_io_socket_sendto(lua_State* lua) tb_assert_and_check_return_val(lua, 0); // check socket - if (!lua_isuserdata(lua, 1)) + if (!xm_lua_ispointer(lua, 1)) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid socket!"); @@ -49,7 +49,7 @@ tb_int_t xm_io_socket_sendto(lua_State* lua) } // get socket - tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); + tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(sock, 0); // get data diff --git a/core/src/xmake/io/socket_wait.c b/core/src/xmake/io/socket_wait.c index 4a5772fd9..7a1e4fade 100644 --- a/core/src/xmake/io/socket_wait.c +++ b/core/src/xmake/io/socket_wait.c @@ -40,12 +40,12 @@ tb_int_t xm_io_socket_wait(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - // is user data? - if (!lua_isuserdata(lua, 1)) + // check socket? + if (!xm_lua_ispointer(lua, 1)) return 0; // get socket - tb_socket_ref_t sock = (tb_socket_ref_t)lua_touserdata(lua, 1); + tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(sock, 0); // get events diff --git a/core/src/xmake/prefix.h b/core/src/xmake/prefix.h index b63bf6679..1b3d519ea 100644 --- a/core/src/xmake/prefix.h +++ b/core/src/xmake/prefix.h @@ -29,6 +29,78 @@ #include "lualib.h" #include "lauxlib.h" +/* ////////////////////////////////////////////////////////////////////////////////////// + * private interfaces + */ + +#if TB_CPU_BIT64 +/* we use this interface instead of lua_pushlightuserdata() to fix bad light userdata pointer bug + * + * @see https://github.com/xmake-io/xmake/issues/914 + * + * @note we cannot lua_newuserdata() because we need pass this pointer to lua code in poller_wait()/event_callback, but lua_pushuserdata does not exists + */ +static __tb_inline__ tb_void_t xm_lua_pushpointer(lua_State* lua, tb_pointer_t ptr) +{ + tb_uint64_t ptrval = (tb_uint64_t)ptr; + tb_trace_i("ptr: %p %llx", ptrval >> 47); + if (0)//(ptrval >> 47) == 0) + lua_pushlightuserdata(lua, ptr); + else + { + tb_char_t str[64]; + tb_long_t len = tb_snprintf(str, sizeof(str), "%p", ptr); + lua_pushlstring(lua, str, len); + tb_trace_i("push ptr: %p, str: %s", ptr, str); + } +} +static __tb_inline__ tb_bool_t xm_lua_ispointer(lua_State* lua, tb_int_t idx) +{ + return lua_isuserdata(lua, idx) || lua_isstring(lua, idx); +} +static __tb_inline__ tb_pointer_t xm_lua_topointer2(lua_State* lua, tb_int_t idx, tb_char_t const** pstr) +{ + tb_pointer_t ptr = tb_null; + if (lua_isuserdata(lua, idx)) + { + ptr = lua_touserdata(lua, idx); + if (pstr) *pstr = tb_null; + } + else + { + size_t len = 0; + tb_char_t const* str = luaL_checklstring(lua, idx, &len); + if (str && len > 2 && str[0] == '0' && str[1] == 'x') + ptr = (tb_pointer_t)tb_s16tou64(str); + if (pstr) *pstr = str; + tb_trace_i("to ptr: %p, str: %s", ptr, str); + } + return ptr; +} +static __tb_inline__ tb_pointer_t xm_lua_topointer(lua_State* lua, tb_int_t idx) +{ + return xm_lua_topointer2(lua, idx, tb_null); +} +#else +static __tb_inline__ tb_void_t xm_lua_pushpointer(lua_State* lua, tb_pointer_t ptr) +{ + lua_pushlightuserdata(lua, ptr); +} +static __tb_inline__ tb_bool_t xm_lua_ispointer(lua_State* lua, tb_int_t idx) +{ + return lua_isuserdata(lua, idx); +} +static __tb_inline__ tb_pointer_t xm_lua_topointer2(lua_State* lua, tb_int_t idx, tb_char_t const** pstr) +{ + if (pstr) *pstr = tb_null; + return lua_touserdata(lua, idx); +} +static __tb_inline__ tb_pointer_t xm_lua_topointer(lua_State* lua, tb_int_t idx) +{ + return lua_touserdata(lua, idx); +} +#endif + #endif diff --git a/core/src/xmake/process/close.c b/core/src/xmake/process/close.c index bcbc6dfd3..a84964418 100644 --- a/core/src/xmake/process/close.c +++ b/core/src/xmake/process/close.c @@ -40,12 +40,12 @@ tb_int_t xm_process_close(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - // is user data? - if (!lua_isuserdata(lua, 1)) + // is pointer? + if (!xm_lua_ispointer(lua, 1)) return 0; // get the process - tb_process_ref_t process = (tb_process_ref_t)lua_touserdata(lua, 1); + tb_process_ref_t process = (tb_process_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(process, 0); // exit process @@ -53,7 +53,5 @@ tb_int_t xm_process_close(lua_State* lua) // save result: ok lua_pushboolean(lua, tb_true); - - // ok return 1; } diff --git a/core/src/xmake/process/kill.c b/core/src/xmake/process/kill.c index 1a695dc27..754d2d2aa 100644 --- a/core/src/xmake/process/kill.c +++ b/core/src/xmake/process/kill.c @@ -40,12 +40,12 @@ tb_int_t xm_process_kill(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - // is user data? - if (!lua_isuserdata(lua, 1)) + // is pointer? + if (!xm_lua_ispointer(lua, 1)) return 0; // get the process - tb_process_ref_t process = (tb_process_ref_t)lua_touserdata(lua, 1); + tb_process_ref_t process = (tb_process_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(process, 0); // kill process diff --git a/core/src/xmake/process/open.c b/core/src/xmake/process/open.c index d027887b7..4a143b901 100644 --- a/core/src/xmake/process/open.c +++ b/core/src/xmake/process/open.c @@ -217,7 +217,7 @@ tb_int_t xm_process_open(lua_State* lua) // init process tb_process_ref_t process = (tb_process_ref_t)tb_process_init_cmd(command, &attr); - if (process) lua_pushlightuserdata(lua, (tb_pointer_t)process); + if (process) xm_lua_pushpointer(lua, (tb_pointer_t)process); else lua_pushnil(lua); return 1; } diff --git a/core/src/xmake/process/openv.c b/core/src/xmake/process/openv.c index c81e98d88..062a7e343 100644 --- a/core/src/xmake/process/openv.c +++ b/core/src/xmake/process/openv.c @@ -259,7 +259,7 @@ tb_int_t xm_process_openv(lua_State* lua) // init process tb_process_ref_t process = (tb_process_ref_t)tb_process_init(shellname, argv, &attr); - if (process) lua_pushlightuserdata(lua, (tb_pointer_t)process); + if (process) xm_lua_pushpointer(lua, (tb_pointer_t)process); else lua_pushnil(lua); // exit argv diff --git a/core/src/xmake/process/wait.c b/core/src/xmake/process/wait.c index 0f85f6e2d..bec6f379f 100644 --- a/core/src/xmake/process/wait.c +++ b/core/src/xmake/process/wait.c @@ -40,8 +40,8 @@ tb_int_t xm_process_wait(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - // is user data? - if (!lua_isuserdata(lua, 1)) + // is pointer? + if (!xm_lua_ispointer(lua, 1)) { // error lua_pushfstring(lua, "invalid argument type(%s) for process.wait", luaL_typename(lua, 1)); @@ -50,7 +50,7 @@ tb_int_t xm_process_wait(lua_State* lua) } // get the process - tb_process_ref_t process = (tb_process_ref_t)lua_touserdata(lua, 1); + tb_process_ref_t process = (tb_process_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(process, 0); // get the timeout |
