open(path_ptr, path_len, flags:i32, mode:i32) -> i32
| 84 | |
| 85 | // open(path_ptr, path_len, flags:i32, mode:i32) -> i32 |
| 86 | static std::vector<Value> fs_open(Stack& stack) { |
| 87 | Frame& frame = stack.frames.top(); |
| 88 | i64_t path_ptr = get_ptr(frame.locals[0]); |
| 89 | i64_t path_len = get_ptr(frame.locals[1]); |
| 90 | i32_t flags = std::get<i32_t>(frame.locals[2]); |
| 91 | i32_t mode = std::get<i32_t>(frame.locals[3]); |
| 92 | try { |
| 93 | std::string path = mem_string(stack, path_ptr, path_len); |
| 94 | int fd = sys_open(path.c_str(), flags, (int)mode); |
| 95 | if(fd < 0) return {Value(err_code())}; |
| 96 | return {Value(fd_table.alloc(fd))}; |
| 97 | } catch(...) { |
| 98 | return {Value(i32_t(-EFAULT))}; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // close(fd:i32) -> i32 |
| 103 | static std::vector<Value> fs_close(Stack& stack) { |