diff --git a/examples/spawn.c b/examples/spawn.c index ed1f364..67cfb9c 100644 --- a/examples/spawn.c +++ b/examples/spawn.c @@ -18,7 +18,10 @@ int main() { local inherited_fds, err = ckb.inherited_fds(); \ local n, err = ckb.write(inherited_fds[1], m); \ ckb.close(inherited_fds[1]); \ - assert(n == 10);", + assert(n == 10);\ + local pid = ckb.process_id(); \ + assert(pid == 1); \ + ", "hello", "world", }; diff --git a/lua-loader/lua-ckb.c b/lua-loader/lua-ckb.c index f1e6f12..4cd53a1 100644 --- a/lua-loader/lua-ckb.c +++ b/lua-loader/lua-ckb.c @@ -864,11 +864,49 @@ int lua_ckb_load_header_by_field(lua_State *L) { // int ckb_wait(uint64_t pid, int8_t* exit_code); -// uint64_t ckb_process_id(); +int lua_ckb_process_id(lua_State *L) { + uint64_t pid = ckb_process_id(); + lua_pushinteger(L, pid); + return 1; +} -// int ckb_pipe(uint64_t fds[2]); +int lua_ckb_pipe(lua_State *L) { + uint64_t fds[2]; + int err = ckb_pipe(fds); + if (err != 0) { + lua_pushnil(L); + lua_pushinteger(L, err); + return 2; + } + lua_newtable(L); + lua_pushinteger(L, fds[0]); + lua_rawseti(L, -2, 1); + lua_pushinteger(L, fds[1]); + lua_rawseti(L, -2, 2); + lua_pushnil(L); + return 2; +} -// int ckb_read(uint64_t fd, void* buffer, size_t* length); +int lua_ckb_read(lua_State *L) { + FIELD fields[] = { + {"fd", UINT64}, + {"size", UINT64}, + }; + GET_FIELDS_WITH_CHECK(L, fields, 2, 2); + uint64_t fd = fields[0].arg.integer; + uint64_t size = fields[1].arg.integer; + + uint8_t *buffer = (uint8_t *)malloc(size); + int ret = ckb_read(fd, buffer, &size); + if (ret != 0) { + lua_pushnil(L); + lua_pushinteger(L, ret); + return 2; + } + lua_pushlstring(L, (char *)buffer, size); + lua_pushnil(L); + return 2; +} int lua_ckb_write(lua_State *L) { FIELD fields[] = { @@ -917,7 +955,7 @@ int lua_ckb_close(lua_State *L) { uint64_t fd = fields[0].arg.integer; err = ckb_close(fd); lua_pushinteger(L, err); - return 2; + return 1; } int lua_ckb_load_block_extension(lua_State *L) { @@ -951,6 +989,9 @@ static const luaL_Reg ckb_syscall[] = { {"load_input_by_field", lua_ckb_load_input_by_field}, {"load_header_by_field", lua_ckb_load_header_by_field}, + {"process_id", lua_ckb_process_id}, + {"pipe", lua_ckb_pipe}, + {"read", lua_ckb_read}, {"write", lua_ckb_write}, {"inherited_fds", lua_ckb_inherited_fds}, {"close", lua_ckb_close},