argv(idx:i32, buf_ptr, buf_len) -> i32
| 45 | |
| 46 | // argv(idx:i32, buf_ptr, buf_len) -> i32 |
| 47 | static std::vector<Value> proc_argv(Stack& stack) { |
| 48 | Frame& frame = stack.frames.top(); |
| 49 | i32_t idx = std::get<i32_t>(frame.locals[0]); |
| 50 | i64_t buf_ptr = get_ptr(frame.locals[1]); |
| 51 | i64_t buf_len = get_ptr(frame.locals[2]); |
| 52 | if(idx < 0 || (size_t)idx >= wasmvm_args.size()) { |
| 53 | return {Value(i32_t(-EINVAL))}; |
| 54 | } |
| 55 | const std::string& arg = wasmvm_args[(size_t)idx]; |
| 56 | i64_t copy_len = std::min((i64_t)arg.size(), buf_len); |
| 57 | try { |
| 58 | mem_write(stack, buf_ptr, arg.data(), (size_t)copy_len); |
| 59 | if(copy_len < buf_len) { |
| 60 | byte_t nul = std::byte{0}; |
| 61 | mem_write(stack, buf_ptr + copy_len, &nul, 1); |
| 62 | } |
| 63 | return {Value(i32_t(copy_len))}; |
| 64 | } catch(...) { |
| 65 | return {Value(i32_t(-EFAULT))}; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // getenv(name_ptr, name_len, buf_ptr, buf_len) -> i32 |
| 70 | static std::vector<Value> proc_getenv(Stack& stack) { |