Skip to content

Commit

Permalink
Staged
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Oct 8, 2024
1 parent 5548dec commit ea74add
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
5 changes: 4 additions & 1 deletion examples/spawn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};
Expand Down
49 changes: 45 additions & 4 deletions lua-loader/lua-ckb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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},
Expand Down

0 comments on commit ea74add

Please sign in to comment.