11cb0ef41Sopenharmony_ci#include "env-inl.h"
21cb0ef41Sopenharmony_ci#include "base_object-inl.h"
31cb0ef41Sopenharmony_ci#include "debug_utils-inl.h"
41cb0ef41Sopenharmony_ci#include "memory_tracker-inl.h"
51cb0ef41Sopenharmony_ci#include "node_mem-inl.h"
61cb0ef41Sopenharmony_ci#include "util-inl.h"
71cb0ef41Sopenharmony_ci#include "node.h"
81cb0ef41Sopenharmony_ci#include "node_errors.h"
91cb0ef41Sopenharmony_ci#include "uv.h"
101cb0ef41Sopenharmony_ci#include "uvwasi.h"
111cb0ef41Sopenharmony_ci#include "node_wasi.h"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace node {
141cb0ef41Sopenharmony_cinamespace wasi {
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_citemplate <typename... Args>
171cb0ef41Sopenharmony_ciinline void Debug(WASI* wasi, Args&&... args) {
181cb0ef41Sopenharmony_ci  Debug(wasi->env(), DebugCategory::WASI, std::forward<Args>(args)...);
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci#define ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(ptr, obj)                          \
221cb0ef41Sopenharmony_ci  do {                                                                         \
231cb0ef41Sopenharmony_ci    ASSIGN_OR_RETURN_UNWRAP(ptr, obj);                                         \
241cb0ef41Sopenharmony_ci    if ((*(ptr))->memory_.IsEmpty()) {                                         \
251cb0ef41Sopenharmony_ci      THROW_ERR_WASI_NOT_STARTED(Environment::GetCurrent(args));               \
261cb0ef41Sopenharmony_ci      return;                                                                  \
271cb0ef41Sopenharmony_ci    }                                                                          \
281cb0ef41Sopenharmony_ci  } while (0)
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci#define RETURN_IF_BAD_ARG_COUNT(args, expected)                               \
311cb0ef41Sopenharmony_ci  do {                                                                        \
321cb0ef41Sopenharmony_ci    if ((args).Length() != (expected)) {                                      \
331cb0ef41Sopenharmony_ci      (args).GetReturnValue().Set(UVWASI_EINVAL);                             \
341cb0ef41Sopenharmony_ci      return;                                                                 \
351cb0ef41Sopenharmony_ci    }                                                                         \
361cb0ef41Sopenharmony_ci  } while (0)
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci#define CHECK_TO_TYPE_OR_RETURN(args, input, type, result)                    \
391cb0ef41Sopenharmony_ci  do {                                                                        \
401cb0ef41Sopenharmony_ci    if (!(input)->Is##type()) {                                               \
411cb0ef41Sopenharmony_ci      (args).GetReturnValue().Set(UVWASI_EINVAL);                             \
421cb0ef41Sopenharmony_ci      return;                                                                 \
431cb0ef41Sopenharmony_ci    }                                                                         \
441cb0ef41Sopenharmony_ci    (result) = (input).As<type>()->Value();                                   \
451cb0ef41Sopenharmony_ci  } while (0)
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci#define UNWRAP_BIGINT_OR_RETURN(args, input, type, result)                    \
481cb0ef41Sopenharmony_ci  do {                                                                        \
491cb0ef41Sopenharmony_ci    if (!(input)->IsBigInt()) {                                               \
501cb0ef41Sopenharmony_ci      (args).GetReturnValue().Set(UVWASI_EINVAL);                             \
511cb0ef41Sopenharmony_ci      return;                                                                 \
521cb0ef41Sopenharmony_ci    }                                                                         \
531cb0ef41Sopenharmony_ci    Local<BigInt> js_value = (input).As<BigInt>();                            \
541cb0ef41Sopenharmony_ci    bool lossless;                                                            \
551cb0ef41Sopenharmony_ci    (result) = js_value->type ## Value(&lossless);                            \
561cb0ef41Sopenharmony_ci  } while (0)
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci#define GET_BACKING_STORE_OR_RETURN(wasi, args, mem_ptr, mem_size)            \
591cb0ef41Sopenharmony_ci  do {                                                                        \
601cb0ef41Sopenharmony_ci    uvwasi_errno_t err = (wasi)->backingStore((mem_ptr), (mem_size));         \
611cb0ef41Sopenharmony_ci    if (err != UVWASI_ESUCCESS) {                                             \
621cb0ef41Sopenharmony_ci      (args).GetReturnValue().Set(err);                                       \
631cb0ef41Sopenharmony_ci      return;                                                                 \
641cb0ef41Sopenharmony_ci    }                                                                         \
651cb0ef41Sopenharmony_ci  } while (0)
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci#define CHECK_BOUNDS_OR_RETURN(args, mem_size, offset, buf_size)              \
681cb0ef41Sopenharmony_ci  do {                                                                        \
691cb0ef41Sopenharmony_ci    if (!uvwasi_serdes_check_bounds((offset), (mem_size), (buf_size))) {      \
701cb0ef41Sopenharmony_ci      (args).GetReturnValue().Set(UVWASI_EOVERFLOW);                          \
711cb0ef41Sopenharmony_ci      return;                                                                 \
721cb0ef41Sopenharmony_ci    }                                                                         \
731cb0ef41Sopenharmony_ci  } while (0)
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ciusing v8::Array;
761cb0ef41Sopenharmony_ciusing v8::BigInt;
771cb0ef41Sopenharmony_ciusing v8::Context;
781cb0ef41Sopenharmony_ciusing v8::Exception;
791cb0ef41Sopenharmony_ciusing v8::FunctionCallbackInfo;
801cb0ef41Sopenharmony_ciusing v8::FunctionTemplate;
811cb0ef41Sopenharmony_ciusing v8::Integer;
821cb0ef41Sopenharmony_ciusing v8::Isolate;
831cb0ef41Sopenharmony_ciusing v8::Local;
841cb0ef41Sopenharmony_ciusing v8::MaybeLocal;
851cb0ef41Sopenharmony_ciusing v8::Object;
861cb0ef41Sopenharmony_ciusing v8::String;
871cb0ef41Sopenharmony_ciusing v8::Uint32;
881cb0ef41Sopenharmony_ciusing v8::Value;
891cb0ef41Sopenharmony_ciusing v8::WasmMemoryObject;
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_cistatic MaybeLocal<Value> WASIException(Local<Context> context,
921cb0ef41Sopenharmony_ci                                       int errorno,
931cb0ef41Sopenharmony_ci                                       const char* syscall) {
941cb0ef41Sopenharmony_ci  Isolate* isolate = context->GetIsolate();
951cb0ef41Sopenharmony_ci  Environment* env = Environment::GetCurrent(context);
961cb0ef41Sopenharmony_ci  CHECK_NOT_NULL(env);
971cb0ef41Sopenharmony_ci  const char* err_name = uvwasi_embedder_err_code_to_string(errorno);
981cb0ef41Sopenharmony_ci  Local<String> js_code = OneByteString(isolate, err_name);
991cb0ef41Sopenharmony_ci  Local<String> js_syscall = OneByteString(isolate, syscall);
1001cb0ef41Sopenharmony_ci  Local<String> js_msg = js_code;
1011cb0ef41Sopenharmony_ci  js_msg =
1021cb0ef41Sopenharmony_ci      String::Concat(isolate, js_msg, FIXED_ONE_BYTE_STRING(isolate, ", "));
1031cb0ef41Sopenharmony_ci  js_msg = String::Concat(isolate, js_msg, js_syscall);
1041cb0ef41Sopenharmony_ci  Local<Object> e;
1051cb0ef41Sopenharmony_ci  if (!Exception::Error(js_msg)->ToObject(context).ToLocal(&e))
1061cb0ef41Sopenharmony_ci    return MaybeLocal<Value>();
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci  if (e->Set(context,
1091cb0ef41Sopenharmony_ci             env->errno_string(),
1101cb0ef41Sopenharmony_ci             Integer::New(isolate, errorno)).IsNothing() ||
1111cb0ef41Sopenharmony_ci      e->Set(context, env->code_string(), js_code).IsNothing() ||
1121cb0ef41Sopenharmony_ci      e->Set(context, env->syscall_string(), js_syscall).IsNothing()) {
1131cb0ef41Sopenharmony_ci    return MaybeLocal<Value>();
1141cb0ef41Sopenharmony_ci  }
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_ci  return e;
1171cb0ef41Sopenharmony_ci}
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ciWASI::WASI(Environment* env,
1211cb0ef41Sopenharmony_ci           Local<Object> object,
1221cb0ef41Sopenharmony_ci           uvwasi_options_t* options) : BaseObject(env, object) {
1231cb0ef41Sopenharmony_ci  MakeWeak();
1241cb0ef41Sopenharmony_ci  alloc_info_ = MakeAllocator();
1251cb0ef41Sopenharmony_ci  options->allocator = &alloc_info_;
1261cb0ef41Sopenharmony_ci  int err = uvwasi_init(&uvw_, options);
1271cb0ef41Sopenharmony_ci  if (err != UVWASI_ESUCCESS) {
1281cb0ef41Sopenharmony_ci    Local<Value> exception;
1291cb0ef41Sopenharmony_ci    if (!WASIException(env->context(), err, "uvwasi_init").ToLocal(&exception))
1301cb0ef41Sopenharmony_ci      return;
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci    env->isolate()->ThrowException(exception);
1331cb0ef41Sopenharmony_ci  }
1341cb0ef41Sopenharmony_ci}
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ciWASI::~WASI() {
1381cb0ef41Sopenharmony_ci  uvwasi_destroy(&uvw_);
1391cb0ef41Sopenharmony_ci  CHECK_EQ(current_uvwasi_memory_, 0);
1401cb0ef41Sopenharmony_ci}
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_civoid WASI::MemoryInfo(MemoryTracker* tracker) const {
1431cb0ef41Sopenharmony_ci  tracker->TrackField("memory", memory_);
1441cb0ef41Sopenharmony_ci  tracker->TrackFieldWithSize("uvwasi_memory", current_uvwasi_memory_);
1451cb0ef41Sopenharmony_ci}
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_civoid WASI::CheckAllocatedSize(size_t previous_size) const {
1481cb0ef41Sopenharmony_ci  CHECK_GE(current_uvwasi_memory_, previous_size);
1491cb0ef41Sopenharmony_ci}
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_civoid WASI::IncreaseAllocatedSize(size_t size) {
1521cb0ef41Sopenharmony_ci  current_uvwasi_memory_ += size;
1531cb0ef41Sopenharmony_ci}
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_civoid WASI::DecreaseAllocatedSize(size_t size) {
1561cb0ef41Sopenharmony_ci  current_uvwasi_memory_ -= size;
1571cb0ef41Sopenharmony_ci}
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_civoid WASI::New(const FunctionCallbackInfo<Value>& args) {
1601cb0ef41Sopenharmony_ci  CHECK(args.IsConstructCall());
1611cb0ef41Sopenharmony_ci  CHECK_EQ(args.Length(), 4);
1621cb0ef41Sopenharmony_ci  CHECK(args[0]->IsArray());
1631cb0ef41Sopenharmony_ci  CHECK(args[1]->IsArray());
1641cb0ef41Sopenharmony_ci  CHECK(args[2]->IsArray());
1651cb0ef41Sopenharmony_ci  CHECK(args[3]->IsArray());
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci  Environment* env = Environment::GetCurrent(args);
1681cb0ef41Sopenharmony_ci  Local<Context> context = env->context();
1691cb0ef41Sopenharmony_ci  Local<Array> argv = args[0].As<Array>();
1701cb0ef41Sopenharmony_ci  const uint32_t argc = argv->Length();
1711cb0ef41Sopenharmony_ci  uvwasi_options_t options;
1721cb0ef41Sopenharmony_ci
1731cb0ef41Sopenharmony_ci  uvwasi_options_init(&options);
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci  Local<Array> stdio = args[3].As<Array>();
1761cb0ef41Sopenharmony_ci  CHECK_EQ(stdio->Length(), 3);
1771cb0ef41Sopenharmony_ci  options.in = stdio->Get(context, 0).ToLocalChecked()->
1781cb0ef41Sopenharmony_ci    Int32Value(context).FromJust();
1791cb0ef41Sopenharmony_ci  options.out = stdio->Get(context, 1).ToLocalChecked()->
1801cb0ef41Sopenharmony_ci    Int32Value(context).FromJust();
1811cb0ef41Sopenharmony_ci  options.err = stdio->Get(context, 2).ToLocalChecked()->
1821cb0ef41Sopenharmony_ci    Int32Value(context).FromJust();
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci  options.fd_table_size = 3;
1851cb0ef41Sopenharmony_ci  options.argc = argc;
1861cb0ef41Sopenharmony_ci  options.argv =
1871cb0ef41Sopenharmony_ci    const_cast<const char**>(argc == 0 ? nullptr : new char*[argc]);
1881cb0ef41Sopenharmony_ci
1891cb0ef41Sopenharmony_ci  for (uint32_t i = 0; i < argc; i++) {
1901cb0ef41Sopenharmony_ci    auto arg = argv->Get(context, i).ToLocalChecked();
1911cb0ef41Sopenharmony_ci    CHECK(arg->IsString());
1921cb0ef41Sopenharmony_ci    node::Utf8Value str(env->isolate(), arg);
1931cb0ef41Sopenharmony_ci    options.argv[i] = strdup(*str);
1941cb0ef41Sopenharmony_ci    CHECK_NOT_NULL(options.argv[i]);
1951cb0ef41Sopenharmony_ci  }
1961cb0ef41Sopenharmony_ci
1971cb0ef41Sopenharmony_ci  Local<Array> env_pairs = args[1].As<Array>();
1981cb0ef41Sopenharmony_ci  const uint32_t envc = env_pairs->Length();
1991cb0ef41Sopenharmony_ci  options.envp = const_cast<const char**>(new char*[envc + 1]);
2001cb0ef41Sopenharmony_ci  for (uint32_t i = 0; i < envc; i++) {
2011cb0ef41Sopenharmony_ci    auto pair = env_pairs->Get(context, i).ToLocalChecked();
2021cb0ef41Sopenharmony_ci    CHECK(pair->IsString());
2031cb0ef41Sopenharmony_ci    node::Utf8Value str(env->isolate(), pair);
2041cb0ef41Sopenharmony_ci    options.envp[i] = strdup(*str);
2051cb0ef41Sopenharmony_ci    CHECK_NOT_NULL(options.envp[i]);
2061cb0ef41Sopenharmony_ci  }
2071cb0ef41Sopenharmony_ci  options.envp[envc] = nullptr;
2081cb0ef41Sopenharmony_ci
2091cb0ef41Sopenharmony_ci  Local<Array> preopens = args[2].As<Array>();
2101cb0ef41Sopenharmony_ci  CHECK_EQ(preopens->Length() % 2, 0);
2111cb0ef41Sopenharmony_ci  options.preopenc = preopens->Length() / 2;
2121cb0ef41Sopenharmony_ci  options.preopens = Calloc<uvwasi_preopen_t>(options.preopenc);
2131cb0ef41Sopenharmony_ci  int index = 0;
2141cb0ef41Sopenharmony_ci  for (uint32_t i = 0; i < preopens->Length(); i += 2) {
2151cb0ef41Sopenharmony_ci    auto mapped = preopens->Get(context, i).ToLocalChecked();
2161cb0ef41Sopenharmony_ci    auto real = preopens->Get(context, i + 1).ToLocalChecked();
2171cb0ef41Sopenharmony_ci    CHECK(mapped->IsString());
2181cb0ef41Sopenharmony_ci    CHECK(real->IsString());
2191cb0ef41Sopenharmony_ci    node::Utf8Value mapped_path(env->isolate(), mapped);
2201cb0ef41Sopenharmony_ci    node::Utf8Value real_path(env->isolate(), real);
2211cb0ef41Sopenharmony_ci    options.preopens[index].mapped_path = strdup(*mapped_path);
2221cb0ef41Sopenharmony_ci    CHECK_NOT_NULL(options.preopens[index].mapped_path);
2231cb0ef41Sopenharmony_ci    options.preopens[index].real_path = strdup(*real_path);
2241cb0ef41Sopenharmony_ci    CHECK_NOT_NULL(options.preopens[index].real_path);
2251cb0ef41Sopenharmony_ci    index++;
2261cb0ef41Sopenharmony_ci  }
2271cb0ef41Sopenharmony_ci
2281cb0ef41Sopenharmony_ci  new WASI(env, args.This(), &options);
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_ci  if (options.argv != nullptr) {
2311cb0ef41Sopenharmony_ci    for (uint32_t i = 0; i < argc; i++)
2321cb0ef41Sopenharmony_ci      free(const_cast<char*>(options.argv[i]));
2331cb0ef41Sopenharmony_ci    delete[] options.argv;
2341cb0ef41Sopenharmony_ci  }
2351cb0ef41Sopenharmony_ci
2361cb0ef41Sopenharmony_ci  for (uint32_t i = 0; options.envp[i]; i++)
2371cb0ef41Sopenharmony_ci    free(const_cast<char*>(options.envp[i]));
2381cb0ef41Sopenharmony_ci  delete[] options.envp;
2391cb0ef41Sopenharmony_ci
2401cb0ef41Sopenharmony_ci  if (options.preopens != nullptr) {
2411cb0ef41Sopenharmony_ci    for (uint32_t i = 0; i < options.preopenc; i++) {
2421cb0ef41Sopenharmony_ci      free(const_cast<char*>(options.preopens[i].mapped_path));
2431cb0ef41Sopenharmony_ci      free(const_cast<char*>(options.preopens[i].real_path));
2441cb0ef41Sopenharmony_ci    }
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_ci    free(options.preopens);
2471cb0ef41Sopenharmony_ci  }
2481cb0ef41Sopenharmony_ci}
2491cb0ef41Sopenharmony_ci
2501cb0ef41Sopenharmony_ci
2511cb0ef41Sopenharmony_civoid WASI::ArgsGet(const FunctionCallbackInfo<Value>& args) {
2521cb0ef41Sopenharmony_ci  WASI* wasi;
2531cb0ef41Sopenharmony_ci  uint32_t argv_offset;
2541cb0ef41Sopenharmony_ci  uint32_t argv_buf_offset;
2551cb0ef41Sopenharmony_ci  char* memory;
2561cb0ef41Sopenharmony_ci  size_t mem_size;
2571cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 2);
2581cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, argv_offset);
2591cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, argv_buf_offset);
2601cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
2611cb0ef41Sopenharmony_ci  Debug(wasi, "args_get(%d, %d)\n", argv_offset, argv_buf_offset);
2621cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
2631cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
2641cb0ef41Sopenharmony_ci                         mem_size,
2651cb0ef41Sopenharmony_ci                         argv_buf_offset,
2661cb0ef41Sopenharmony_ci                         wasi->uvw_.argv_buf_size);
2671cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
2681cb0ef41Sopenharmony_ci                         mem_size,
2691cb0ef41Sopenharmony_ci                         argv_offset,
2701cb0ef41Sopenharmony_ci                         wasi->uvw_.argc * UVWASI_SERDES_SIZE_uint32_t);
2711cb0ef41Sopenharmony_ci  std::vector<char*> argv(wasi->uvw_.argc);
2721cb0ef41Sopenharmony_ci  char* argv_buf = &memory[argv_buf_offset];
2731cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_args_get(&wasi->uvw_, argv.data(), argv_buf);
2741cb0ef41Sopenharmony_ci
2751cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS) {
2761cb0ef41Sopenharmony_ci    for (size_t i = 0; i < wasi->uvw_.argc; i++) {
2771cb0ef41Sopenharmony_ci      uint32_t offset =
2781cb0ef41Sopenharmony_ci          static_cast<uint32_t>(argv_buf_offset + (argv[i] - argv[0]));
2791cb0ef41Sopenharmony_ci      uvwasi_serdes_write_uint32_t(memory,
2801cb0ef41Sopenharmony_ci                                   argv_offset +
2811cb0ef41Sopenharmony_ci                                   (i * UVWASI_SERDES_SIZE_uint32_t),
2821cb0ef41Sopenharmony_ci                                   offset);
2831cb0ef41Sopenharmony_ci    }
2841cb0ef41Sopenharmony_ci  }
2851cb0ef41Sopenharmony_ci
2861cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
2871cb0ef41Sopenharmony_ci}
2881cb0ef41Sopenharmony_ci
2891cb0ef41Sopenharmony_ci
2901cb0ef41Sopenharmony_civoid WASI::ArgsSizesGet(const FunctionCallbackInfo<Value>& args) {
2911cb0ef41Sopenharmony_ci  WASI* wasi;
2921cb0ef41Sopenharmony_ci  uint32_t argc_offset;
2931cb0ef41Sopenharmony_ci  uint32_t argv_buf_offset;
2941cb0ef41Sopenharmony_ci  char* memory;
2951cb0ef41Sopenharmony_ci  size_t mem_size;
2961cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 2);
2971cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, argc_offset);
2981cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, argv_buf_offset);
2991cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
3001cb0ef41Sopenharmony_ci  Debug(wasi, "args_sizes_get(%d, %d)\n", argc_offset, argv_buf_offset);
3011cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
3021cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
3031cb0ef41Sopenharmony_ci                         mem_size,
3041cb0ef41Sopenharmony_ci                         argc_offset,
3051cb0ef41Sopenharmony_ci                         UVWASI_SERDES_SIZE_size_t);
3061cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
3071cb0ef41Sopenharmony_ci                         mem_size,
3081cb0ef41Sopenharmony_ci                         argv_buf_offset,
3091cb0ef41Sopenharmony_ci                         UVWASI_SERDES_SIZE_size_t);
3101cb0ef41Sopenharmony_ci  uvwasi_size_t argc;
3111cb0ef41Sopenharmony_ci  uvwasi_size_t argv_buf_size;
3121cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_args_sizes_get(&wasi->uvw_,
3131cb0ef41Sopenharmony_ci                                             &argc,
3141cb0ef41Sopenharmony_ci                                             &argv_buf_size);
3151cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS) {
3161cb0ef41Sopenharmony_ci    uvwasi_serdes_write_size_t(memory, argc_offset, argc);
3171cb0ef41Sopenharmony_ci    uvwasi_serdes_write_size_t(memory, argv_buf_offset, argv_buf_size);
3181cb0ef41Sopenharmony_ci  }
3191cb0ef41Sopenharmony_ci
3201cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
3211cb0ef41Sopenharmony_ci}
3221cb0ef41Sopenharmony_ci
3231cb0ef41Sopenharmony_ci
3241cb0ef41Sopenharmony_civoid WASI::ClockResGet(const FunctionCallbackInfo<Value>& args) {
3251cb0ef41Sopenharmony_ci  WASI* wasi;
3261cb0ef41Sopenharmony_ci  uint32_t clock_id;
3271cb0ef41Sopenharmony_ci  uint32_t resolution_ptr;
3281cb0ef41Sopenharmony_ci  char* memory;
3291cb0ef41Sopenharmony_ci  size_t mem_size;
3301cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 2);
3311cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, clock_id);
3321cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, resolution_ptr);
3331cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
3341cb0ef41Sopenharmony_ci  Debug(wasi, "clock_res_get(%d, %d)\n", clock_id, resolution_ptr);
3351cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
3361cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
3371cb0ef41Sopenharmony_ci                         mem_size,
3381cb0ef41Sopenharmony_ci                         resolution_ptr,
3391cb0ef41Sopenharmony_ci                         UVWASI_SERDES_SIZE_timestamp_t);
3401cb0ef41Sopenharmony_ci  uvwasi_timestamp_t resolution;
3411cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_clock_res_get(&wasi->uvw_,
3421cb0ef41Sopenharmony_ci                                            clock_id,
3431cb0ef41Sopenharmony_ci                                            &resolution);
3441cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS)
3451cb0ef41Sopenharmony_ci    uvwasi_serdes_write_timestamp_t(memory, resolution_ptr, resolution);
3461cb0ef41Sopenharmony_ci
3471cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
3481cb0ef41Sopenharmony_ci}
3491cb0ef41Sopenharmony_ci
3501cb0ef41Sopenharmony_ci
3511cb0ef41Sopenharmony_civoid WASI::ClockTimeGet(const FunctionCallbackInfo<Value>& args) {
3521cb0ef41Sopenharmony_ci  WASI* wasi;
3531cb0ef41Sopenharmony_ci  uint32_t clock_id;
3541cb0ef41Sopenharmony_ci  uint64_t precision;
3551cb0ef41Sopenharmony_ci  uint32_t time_ptr;
3561cb0ef41Sopenharmony_ci  char* memory;
3571cb0ef41Sopenharmony_ci  size_t mem_size;
3581cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 3);
3591cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, clock_id);
3601cb0ef41Sopenharmony_ci  UNWRAP_BIGINT_OR_RETURN(args, args[1], Uint64, precision);
3611cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, time_ptr);
3621cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
3631cb0ef41Sopenharmony_ci  Debug(wasi, "clock_time_get(%d, %d, %d)\n", clock_id, precision, time_ptr);
3641cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
3651cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
3661cb0ef41Sopenharmony_ci                         mem_size,
3671cb0ef41Sopenharmony_ci                         time_ptr,
3681cb0ef41Sopenharmony_ci                         UVWASI_SERDES_SIZE_timestamp_t);
3691cb0ef41Sopenharmony_ci  uvwasi_timestamp_t time;
3701cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_clock_time_get(&wasi->uvw_,
3711cb0ef41Sopenharmony_ci                                             clock_id,
3721cb0ef41Sopenharmony_ci                                             precision,
3731cb0ef41Sopenharmony_ci                                             &time);
3741cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS)
3751cb0ef41Sopenharmony_ci    uvwasi_serdes_write_timestamp_t(memory, time_ptr, time);
3761cb0ef41Sopenharmony_ci
3771cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
3781cb0ef41Sopenharmony_ci}
3791cb0ef41Sopenharmony_ci
3801cb0ef41Sopenharmony_ci
3811cb0ef41Sopenharmony_civoid WASI::EnvironGet(const FunctionCallbackInfo<Value>& args) {
3821cb0ef41Sopenharmony_ci  WASI* wasi;
3831cb0ef41Sopenharmony_ci  uint32_t environ_offset;
3841cb0ef41Sopenharmony_ci  uint32_t environ_buf_offset;
3851cb0ef41Sopenharmony_ci  char* memory;
3861cb0ef41Sopenharmony_ci  size_t mem_size;
3871cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 2);
3881cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, environ_offset);
3891cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, environ_buf_offset);
3901cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
3911cb0ef41Sopenharmony_ci  Debug(wasi, "environ_get(%d, %d)\n", environ_offset, environ_buf_offset);
3921cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
3931cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
3941cb0ef41Sopenharmony_ci                         mem_size,
3951cb0ef41Sopenharmony_ci                         environ_buf_offset,
3961cb0ef41Sopenharmony_ci                         wasi->uvw_.env_buf_size);
3971cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
3981cb0ef41Sopenharmony_ci                         mem_size,
3991cb0ef41Sopenharmony_ci                         environ_offset,
4001cb0ef41Sopenharmony_ci                         wasi->uvw_.envc * UVWASI_SERDES_SIZE_uint32_t);
4011cb0ef41Sopenharmony_ci  std::vector<char*> environment(wasi->uvw_.envc);
4021cb0ef41Sopenharmony_ci  char* environ_buf = &memory[environ_buf_offset];
4031cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_environ_get(&wasi->uvw_,
4041cb0ef41Sopenharmony_ci                                          environment.data(),
4051cb0ef41Sopenharmony_ci                                          environ_buf);
4061cb0ef41Sopenharmony_ci
4071cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS) {
4081cb0ef41Sopenharmony_ci    for (size_t i = 0; i < wasi->uvw_.envc; i++) {
4091cb0ef41Sopenharmony_ci      uint32_t offset = static_cast<uint32_t>(
4101cb0ef41Sopenharmony_ci          environ_buf_offset + (environment[i] - environment[0]));
4111cb0ef41Sopenharmony_ci
4121cb0ef41Sopenharmony_ci      uvwasi_serdes_write_uint32_t(memory,
4131cb0ef41Sopenharmony_ci                                   environ_offset +
4141cb0ef41Sopenharmony_ci                                   (i * UVWASI_SERDES_SIZE_uint32_t),
4151cb0ef41Sopenharmony_ci                                   offset);
4161cb0ef41Sopenharmony_ci    }
4171cb0ef41Sopenharmony_ci  }
4181cb0ef41Sopenharmony_ci
4191cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
4201cb0ef41Sopenharmony_ci}
4211cb0ef41Sopenharmony_ci
4221cb0ef41Sopenharmony_ci
4231cb0ef41Sopenharmony_civoid WASI::EnvironSizesGet(const FunctionCallbackInfo<Value>& args) {
4241cb0ef41Sopenharmony_ci  WASI* wasi;
4251cb0ef41Sopenharmony_ci  uint32_t envc_offset;
4261cb0ef41Sopenharmony_ci  uint32_t env_buf_offset;
4271cb0ef41Sopenharmony_ci  char* memory;
4281cb0ef41Sopenharmony_ci  size_t mem_size;
4291cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 2);
4301cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, envc_offset);
4311cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, env_buf_offset);
4321cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
4331cb0ef41Sopenharmony_ci  Debug(wasi, "environ_sizes_get(%d, %d)\n", envc_offset, env_buf_offset);
4341cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
4351cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
4361cb0ef41Sopenharmony_ci                         mem_size,
4371cb0ef41Sopenharmony_ci                         envc_offset,
4381cb0ef41Sopenharmony_ci                         UVWASI_SERDES_SIZE_size_t);
4391cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
4401cb0ef41Sopenharmony_ci                         mem_size,
4411cb0ef41Sopenharmony_ci                         env_buf_offset,
4421cb0ef41Sopenharmony_ci                         UVWASI_SERDES_SIZE_size_t);
4431cb0ef41Sopenharmony_ci  uvwasi_size_t envc;
4441cb0ef41Sopenharmony_ci  uvwasi_size_t env_buf_size;
4451cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_environ_sizes_get(&wasi->uvw_,
4461cb0ef41Sopenharmony_ci                                                &envc,
4471cb0ef41Sopenharmony_ci                                                &env_buf_size);
4481cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS) {
4491cb0ef41Sopenharmony_ci    uvwasi_serdes_write_size_t(memory, envc_offset, envc);
4501cb0ef41Sopenharmony_ci    uvwasi_serdes_write_size_t(memory, env_buf_offset, env_buf_size);
4511cb0ef41Sopenharmony_ci  }
4521cb0ef41Sopenharmony_ci
4531cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
4541cb0ef41Sopenharmony_ci}
4551cb0ef41Sopenharmony_ci
4561cb0ef41Sopenharmony_ci
4571cb0ef41Sopenharmony_civoid WASI::FdAdvise(const FunctionCallbackInfo<Value>& args) {
4581cb0ef41Sopenharmony_ci  WASI* wasi;
4591cb0ef41Sopenharmony_ci  uint32_t fd;
4601cb0ef41Sopenharmony_ci  uint64_t offset;
4611cb0ef41Sopenharmony_ci  uint64_t len;
4621cb0ef41Sopenharmony_ci  uint8_t advice;
4631cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 4);
4641cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
4651cb0ef41Sopenharmony_ci  UNWRAP_BIGINT_OR_RETURN(args, args[1], Uint64, offset);
4661cb0ef41Sopenharmony_ci  UNWRAP_BIGINT_OR_RETURN(args, args[2], Uint64, len);
4671cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, advice);
4681cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
4691cb0ef41Sopenharmony_ci  Debug(wasi, "fd_advise(%d, %d, %d, %d)\n", fd, offset, len, advice);
4701cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_fd_advise(&wasi->uvw_, fd, offset, len, advice);
4711cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
4721cb0ef41Sopenharmony_ci}
4731cb0ef41Sopenharmony_ci
4741cb0ef41Sopenharmony_ci
4751cb0ef41Sopenharmony_civoid WASI::FdAllocate(const FunctionCallbackInfo<Value>& args) {
4761cb0ef41Sopenharmony_ci  WASI* wasi;
4771cb0ef41Sopenharmony_ci  uint32_t fd;
4781cb0ef41Sopenharmony_ci  uint64_t offset;
4791cb0ef41Sopenharmony_ci  uint64_t len;
4801cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 3);
4811cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
4821cb0ef41Sopenharmony_ci  UNWRAP_BIGINT_OR_RETURN(args, args[1], Uint64, offset);
4831cb0ef41Sopenharmony_ci  UNWRAP_BIGINT_OR_RETURN(args, args[2], Uint64, len);
4841cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
4851cb0ef41Sopenharmony_ci  Debug(wasi, "fd_allocate(%d, %d, %d)\n", fd, offset, len);
4861cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_fd_allocate(&wasi->uvw_, fd, offset, len);
4871cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
4881cb0ef41Sopenharmony_ci}
4891cb0ef41Sopenharmony_ci
4901cb0ef41Sopenharmony_ci
4911cb0ef41Sopenharmony_civoid WASI::FdClose(const FunctionCallbackInfo<Value>& args) {
4921cb0ef41Sopenharmony_ci  WASI* wasi;
4931cb0ef41Sopenharmony_ci  uint32_t fd;
4941cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 1);
4951cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
4961cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
4971cb0ef41Sopenharmony_ci  Debug(wasi, "fd_close(%d)\n", fd);
4981cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_fd_close(&wasi->uvw_, fd);
4991cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
5001cb0ef41Sopenharmony_ci}
5011cb0ef41Sopenharmony_ci
5021cb0ef41Sopenharmony_ci
5031cb0ef41Sopenharmony_civoid WASI::FdDatasync(const FunctionCallbackInfo<Value>& args) {
5041cb0ef41Sopenharmony_ci  WASI* wasi;
5051cb0ef41Sopenharmony_ci  uint32_t fd;
5061cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 1);
5071cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
5081cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
5091cb0ef41Sopenharmony_ci  Debug(wasi, "fd_datasync(%d)\n", fd);
5101cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_fd_datasync(&wasi->uvw_, fd);
5111cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
5121cb0ef41Sopenharmony_ci}
5131cb0ef41Sopenharmony_ci
5141cb0ef41Sopenharmony_ci
5151cb0ef41Sopenharmony_civoid WASI::FdFdstatGet(const FunctionCallbackInfo<Value>& args) {
5161cb0ef41Sopenharmony_ci  WASI* wasi;
5171cb0ef41Sopenharmony_ci  uint32_t fd;
5181cb0ef41Sopenharmony_ci  uint32_t buf;
5191cb0ef41Sopenharmony_ci  char* memory;
5201cb0ef41Sopenharmony_ci  size_t mem_size;
5211cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 2);
5221cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
5231cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, buf);
5241cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
5251cb0ef41Sopenharmony_ci  Debug(wasi, "fd_fdstat_get(%d, %d)\n", fd, buf);
5261cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
5271cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, buf, UVWASI_SERDES_SIZE_fdstat_t);
5281cb0ef41Sopenharmony_ci  uvwasi_fdstat_t stats;
5291cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_fd_fdstat_get(&wasi->uvw_, fd, &stats);
5301cb0ef41Sopenharmony_ci
5311cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS)
5321cb0ef41Sopenharmony_ci    uvwasi_serdes_write_fdstat_t(memory, buf, &stats);
5331cb0ef41Sopenharmony_ci
5341cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
5351cb0ef41Sopenharmony_ci}
5361cb0ef41Sopenharmony_ci
5371cb0ef41Sopenharmony_ci
5381cb0ef41Sopenharmony_civoid WASI::FdFdstatSetFlags(const FunctionCallbackInfo<Value>& args) {
5391cb0ef41Sopenharmony_ci  WASI* wasi;
5401cb0ef41Sopenharmony_ci  uint32_t fd;
5411cb0ef41Sopenharmony_ci  uint16_t flags;
5421cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 2);
5431cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
5441cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, flags);
5451cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
5461cb0ef41Sopenharmony_ci  Debug(wasi, "fd_fdstat_set_flags(%d, %d)\n", fd, flags);
5471cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_fd_fdstat_set_flags(&wasi->uvw_, fd, flags);
5481cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
5491cb0ef41Sopenharmony_ci}
5501cb0ef41Sopenharmony_ci
5511cb0ef41Sopenharmony_ci
5521cb0ef41Sopenharmony_civoid WASI::FdFdstatSetRights(const FunctionCallbackInfo<Value>& args) {
5531cb0ef41Sopenharmony_ci  WASI* wasi;
5541cb0ef41Sopenharmony_ci  uint32_t fd;
5551cb0ef41Sopenharmony_ci  uint64_t fs_rights_base;
5561cb0ef41Sopenharmony_ci  uint64_t fs_rights_inheriting;
5571cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 3);
5581cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
5591cb0ef41Sopenharmony_ci  UNWRAP_BIGINT_OR_RETURN(args, args[1], Uint64, fs_rights_base);
5601cb0ef41Sopenharmony_ci  UNWRAP_BIGINT_OR_RETURN(args, args[2], Uint64, fs_rights_inheriting);
5611cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
5621cb0ef41Sopenharmony_ci  Debug(wasi,
5631cb0ef41Sopenharmony_ci        "fd_fdstat_set_rights(%d, %d, %d)\n",
5641cb0ef41Sopenharmony_ci        fd,
5651cb0ef41Sopenharmony_ci        fs_rights_base,
5661cb0ef41Sopenharmony_ci        fs_rights_inheriting);
5671cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_fd_fdstat_set_rights(&wasi->uvw_,
5681cb0ef41Sopenharmony_ci                                                   fd,
5691cb0ef41Sopenharmony_ci                                                   fs_rights_base,
5701cb0ef41Sopenharmony_ci                                                   fs_rights_inheriting);
5711cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
5721cb0ef41Sopenharmony_ci}
5731cb0ef41Sopenharmony_ci
5741cb0ef41Sopenharmony_ci
5751cb0ef41Sopenharmony_civoid WASI::FdFilestatGet(const FunctionCallbackInfo<Value>& args) {
5761cb0ef41Sopenharmony_ci  WASI* wasi;
5771cb0ef41Sopenharmony_ci  uint32_t fd;
5781cb0ef41Sopenharmony_ci  uint32_t buf;
5791cb0ef41Sopenharmony_ci  char* memory;
5801cb0ef41Sopenharmony_ci  size_t mem_size;
5811cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 2);
5821cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
5831cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, buf);
5841cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
5851cb0ef41Sopenharmony_ci  Debug(wasi, "fd_filestat_get(%d, %d)\n", fd, buf);
5861cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
5871cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, buf, UVWASI_SERDES_SIZE_filestat_t);
5881cb0ef41Sopenharmony_ci  uvwasi_filestat_t stats;
5891cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_fd_filestat_get(&wasi->uvw_, fd, &stats);
5901cb0ef41Sopenharmony_ci
5911cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS)
5921cb0ef41Sopenharmony_ci    uvwasi_serdes_write_filestat_t(memory, buf, &stats);
5931cb0ef41Sopenharmony_ci
5941cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
5951cb0ef41Sopenharmony_ci}
5961cb0ef41Sopenharmony_ci
5971cb0ef41Sopenharmony_ci
5981cb0ef41Sopenharmony_civoid WASI::FdFilestatSetSize(const FunctionCallbackInfo<Value>& args) {
5991cb0ef41Sopenharmony_ci  WASI* wasi;
6001cb0ef41Sopenharmony_ci  uint32_t fd;
6011cb0ef41Sopenharmony_ci  uint64_t st_size;
6021cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 2);
6031cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
6041cb0ef41Sopenharmony_ci  UNWRAP_BIGINT_OR_RETURN(args, args[1], Uint64, st_size);
6051cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
6061cb0ef41Sopenharmony_ci  Debug(wasi, "fd_filestat_set_size(%d, %d)\n", fd, st_size);
6071cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_fd_filestat_set_size(&wasi->uvw_, fd, st_size);
6081cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
6091cb0ef41Sopenharmony_ci}
6101cb0ef41Sopenharmony_ci
6111cb0ef41Sopenharmony_ci
6121cb0ef41Sopenharmony_civoid WASI::FdFilestatSetTimes(const FunctionCallbackInfo<Value>& args) {
6131cb0ef41Sopenharmony_ci  WASI* wasi;
6141cb0ef41Sopenharmony_ci  uint32_t fd;
6151cb0ef41Sopenharmony_ci  uint64_t st_atim;
6161cb0ef41Sopenharmony_ci  uint64_t st_mtim;
6171cb0ef41Sopenharmony_ci  uint16_t fst_flags;
6181cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 4);
6191cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
6201cb0ef41Sopenharmony_ci  UNWRAP_BIGINT_OR_RETURN(args, args[1], Uint64, st_atim);
6211cb0ef41Sopenharmony_ci  UNWRAP_BIGINT_OR_RETURN(args, args[2], Uint64, st_mtim);
6221cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, fst_flags);
6231cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
6241cb0ef41Sopenharmony_ci  Debug(wasi,
6251cb0ef41Sopenharmony_ci        "fd_filestat_set_times(%d, %d, %d, %d)\n",
6261cb0ef41Sopenharmony_ci        fd,
6271cb0ef41Sopenharmony_ci        st_atim,
6281cb0ef41Sopenharmony_ci        st_mtim,
6291cb0ef41Sopenharmony_ci        fst_flags);
6301cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_fd_filestat_set_times(&wasi->uvw_,
6311cb0ef41Sopenharmony_ci                                                    fd,
6321cb0ef41Sopenharmony_ci                                                    st_atim,
6331cb0ef41Sopenharmony_ci                                                    st_mtim,
6341cb0ef41Sopenharmony_ci                                                    fst_flags);
6351cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
6361cb0ef41Sopenharmony_ci}
6371cb0ef41Sopenharmony_ci
6381cb0ef41Sopenharmony_ci
6391cb0ef41Sopenharmony_civoid WASI::FdPread(const FunctionCallbackInfo<Value>& args) {
6401cb0ef41Sopenharmony_ci  WASI* wasi;
6411cb0ef41Sopenharmony_ci  uint32_t fd;
6421cb0ef41Sopenharmony_ci  uint32_t iovs_ptr;
6431cb0ef41Sopenharmony_ci  uint32_t iovs_len;
6441cb0ef41Sopenharmony_ci  uint64_t offset;
6451cb0ef41Sopenharmony_ci  uint32_t nread_ptr;
6461cb0ef41Sopenharmony_ci  char* memory;
6471cb0ef41Sopenharmony_ci  size_t mem_size;
6481cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 5);
6491cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
6501cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, iovs_ptr);
6511cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, iovs_len);
6521cb0ef41Sopenharmony_ci  UNWRAP_BIGINT_OR_RETURN(args, args[3], Uint64, offset);
6531cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, nread_ptr);
6541cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
6551cb0ef41Sopenharmony_ci  Debug(wasi,
6561cb0ef41Sopenharmony_ci        "uvwasi_fd_pread(%d, %d, %d, %d, %d)\n",
6571cb0ef41Sopenharmony_ci        fd,
6581cb0ef41Sopenharmony_ci        iovs_ptr,
6591cb0ef41Sopenharmony_ci        iovs_len,
6601cb0ef41Sopenharmony_ci        offset,
6611cb0ef41Sopenharmony_ci        nread_ptr);
6621cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
6631cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
6641cb0ef41Sopenharmony_ci                         mem_size,
6651cb0ef41Sopenharmony_ci                         iovs_ptr,
6661cb0ef41Sopenharmony_ci                         iovs_len * UVWASI_SERDES_SIZE_iovec_t);
6671cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, nread_ptr, UVWASI_SERDES_SIZE_size_t);
6681cb0ef41Sopenharmony_ci  std::vector<uvwasi_iovec_t> iovs(iovs_len);
6691cb0ef41Sopenharmony_ci  uvwasi_errno_t err;
6701cb0ef41Sopenharmony_ci
6711cb0ef41Sopenharmony_ci  err = uvwasi_serdes_readv_iovec_t(memory,
6721cb0ef41Sopenharmony_ci                                    mem_size,
6731cb0ef41Sopenharmony_ci                                    iovs_ptr,
6741cb0ef41Sopenharmony_ci                                    iovs.data(),
6751cb0ef41Sopenharmony_ci                                    iovs_len);
6761cb0ef41Sopenharmony_ci  if (err != UVWASI_ESUCCESS) {
6771cb0ef41Sopenharmony_ci    args.GetReturnValue().Set(err);
6781cb0ef41Sopenharmony_ci    return;
6791cb0ef41Sopenharmony_ci  }
6801cb0ef41Sopenharmony_ci
6811cb0ef41Sopenharmony_ci  uvwasi_size_t nread;
6821cb0ef41Sopenharmony_ci  err = uvwasi_fd_pread(&wasi->uvw_, fd, iovs.data(), iovs_len, offset, &nread);
6831cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS)
6841cb0ef41Sopenharmony_ci    uvwasi_serdes_write_size_t(memory, nread_ptr, nread);
6851cb0ef41Sopenharmony_ci
6861cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
6871cb0ef41Sopenharmony_ci}
6881cb0ef41Sopenharmony_ci
6891cb0ef41Sopenharmony_ci
6901cb0ef41Sopenharmony_civoid WASI::FdPrestatGet(const FunctionCallbackInfo<Value>& args) {
6911cb0ef41Sopenharmony_ci  WASI* wasi;
6921cb0ef41Sopenharmony_ci  uint32_t fd;
6931cb0ef41Sopenharmony_ci  uint32_t buf;
6941cb0ef41Sopenharmony_ci  char* memory;
6951cb0ef41Sopenharmony_ci  size_t mem_size;
6961cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 2);
6971cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
6981cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, buf);
6991cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
7001cb0ef41Sopenharmony_ci  Debug(wasi, "fd_prestat_get(%d, %d)\n", fd, buf);
7011cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
7021cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, buf, UVWASI_SERDES_SIZE_prestat_t);
7031cb0ef41Sopenharmony_ci  uvwasi_prestat_t prestat;
7041cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_fd_prestat_get(&wasi->uvw_, fd, &prestat);
7051cb0ef41Sopenharmony_ci
7061cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS)
7071cb0ef41Sopenharmony_ci    uvwasi_serdes_write_prestat_t(memory, buf, &prestat);
7081cb0ef41Sopenharmony_ci
7091cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
7101cb0ef41Sopenharmony_ci}
7111cb0ef41Sopenharmony_ci
7121cb0ef41Sopenharmony_ci
7131cb0ef41Sopenharmony_civoid WASI::FdPrestatDirName(const FunctionCallbackInfo<Value>& args) {
7141cb0ef41Sopenharmony_ci  WASI* wasi;
7151cb0ef41Sopenharmony_ci  uint32_t fd;
7161cb0ef41Sopenharmony_ci  uint32_t path_ptr;
7171cb0ef41Sopenharmony_ci  uint32_t path_len;
7181cb0ef41Sopenharmony_ci  char* memory;
7191cb0ef41Sopenharmony_ci  size_t mem_size;
7201cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 3);
7211cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
7221cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, path_ptr);
7231cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, path_len);
7241cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
7251cb0ef41Sopenharmony_ci  Debug(wasi, "fd_prestat_dir_name(%d, %d, %d)\n", fd, path_ptr, path_len);
7261cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
7271cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, path_ptr, path_len);
7281cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_fd_prestat_dir_name(&wasi->uvw_,
7291cb0ef41Sopenharmony_ci                                                  fd,
7301cb0ef41Sopenharmony_ci                                                  &memory[path_ptr],
7311cb0ef41Sopenharmony_ci                                                  path_len);
7321cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
7331cb0ef41Sopenharmony_ci}
7341cb0ef41Sopenharmony_ci
7351cb0ef41Sopenharmony_ci
7361cb0ef41Sopenharmony_civoid WASI::FdPwrite(const FunctionCallbackInfo<Value>& args) {
7371cb0ef41Sopenharmony_ci  WASI* wasi;
7381cb0ef41Sopenharmony_ci  uint32_t fd;
7391cb0ef41Sopenharmony_ci  uint32_t iovs_ptr;
7401cb0ef41Sopenharmony_ci  uint32_t iovs_len;
7411cb0ef41Sopenharmony_ci  uint64_t offset;
7421cb0ef41Sopenharmony_ci  uint32_t nwritten_ptr;
7431cb0ef41Sopenharmony_ci  char* memory;
7441cb0ef41Sopenharmony_ci  size_t mem_size;
7451cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 5);
7461cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
7471cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, iovs_ptr);
7481cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, iovs_len);
7491cb0ef41Sopenharmony_ci  UNWRAP_BIGINT_OR_RETURN(args, args[3], Uint64, offset);
7501cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, nwritten_ptr);
7511cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
7521cb0ef41Sopenharmony_ci  Debug(wasi,
7531cb0ef41Sopenharmony_ci        "uvwasi_fd_pwrite(%d, %d, %d, %d, %d)\n",
7541cb0ef41Sopenharmony_ci        fd,
7551cb0ef41Sopenharmony_ci        iovs_ptr,
7561cb0ef41Sopenharmony_ci        iovs_len,
7571cb0ef41Sopenharmony_ci        offset,
7581cb0ef41Sopenharmony_ci        nwritten_ptr);
7591cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
7601cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
7611cb0ef41Sopenharmony_ci                         mem_size,
7621cb0ef41Sopenharmony_ci                         iovs_ptr,
7631cb0ef41Sopenharmony_ci                         iovs_len * UVWASI_SERDES_SIZE_ciovec_t);
7641cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
7651cb0ef41Sopenharmony_ci                         mem_size,
7661cb0ef41Sopenharmony_ci                         nwritten_ptr,
7671cb0ef41Sopenharmony_ci                         UVWASI_SERDES_SIZE_size_t);
7681cb0ef41Sopenharmony_ci  std::vector<uvwasi_ciovec_t> iovs(iovs_len);
7691cb0ef41Sopenharmony_ci  uvwasi_errno_t err;
7701cb0ef41Sopenharmony_ci
7711cb0ef41Sopenharmony_ci  err = uvwasi_serdes_readv_ciovec_t(memory,
7721cb0ef41Sopenharmony_ci                                     mem_size,
7731cb0ef41Sopenharmony_ci                                     iovs_ptr,
7741cb0ef41Sopenharmony_ci                                     iovs.data(),
7751cb0ef41Sopenharmony_ci                                     iovs_len);
7761cb0ef41Sopenharmony_ci  if (err != UVWASI_ESUCCESS) {
7771cb0ef41Sopenharmony_ci    args.GetReturnValue().Set(err);
7781cb0ef41Sopenharmony_ci    return;
7791cb0ef41Sopenharmony_ci  }
7801cb0ef41Sopenharmony_ci
7811cb0ef41Sopenharmony_ci  uvwasi_size_t nwritten;
7821cb0ef41Sopenharmony_ci  err = uvwasi_fd_pwrite(&wasi->uvw_,
7831cb0ef41Sopenharmony_ci                         fd,
7841cb0ef41Sopenharmony_ci                         iovs.data(),
7851cb0ef41Sopenharmony_ci                         iovs_len,
7861cb0ef41Sopenharmony_ci                         offset,
7871cb0ef41Sopenharmony_ci                         &nwritten);
7881cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS)
7891cb0ef41Sopenharmony_ci    uvwasi_serdes_write_size_t(memory, nwritten_ptr, nwritten);
7901cb0ef41Sopenharmony_ci
7911cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
7921cb0ef41Sopenharmony_ci}
7931cb0ef41Sopenharmony_ci
7941cb0ef41Sopenharmony_ci
7951cb0ef41Sopenharmony_civoid WASI::FdRead(const FunctionCallbackInfo<Value>& args) {
7961cb0ef41Sopenharmony_ci  WASI* wasi;
7971cb0ef41Sopenharmony_ci  uint32_t fd;
7981cb0ef41Sopenharmony_ci  uint32_t iovs_ptr;
7991cb0ef41Sopenharmony_ci  uint32_t iovs_len;
8001cb0ef41Sopenharmony_ci  uint32_t nread_ptr;
8011cb0ef41Sopenharmony_ci  char* memory;
8021cb0ef41Sopenharmony_ci  size_t mem_size;
8031cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 4);
8041cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
8051cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, iovs_ptr);
8061cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, iovs_len);
8071cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, nread_ptr);
8081cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
8091cb0ef41Sopenharmony_ci  Debug(wasi, "fd_read(%d, %d, %d, %d)\n", fd, iovs_ptr, iovs_len, nread_ptr);
8101cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
8111cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
8121cb0ef41Sopenharmony_ci                         mem_size,
8131cb0ef41Sopenharmony_ci                         iovs_ptr,
8141cb0ef41Sopenharmony_ci                         iovs_len * UVWASI_SERDES_SIZE_iovec_t);
8151cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, nread_ptr, UVWASI_SERDES_SIZE_size_t);
8161cb0ef41Sopenharmony_ci  std::vector<uvwasi_iovec_t> iovs(iovs_len);
8171cb0ef41Sopenharmony_ci  uvwasi_errno_t err;
8181cb0ef41Sopenharmony_ci
8191cb0ef41Sopenharmony_ci  err = uvwasi_serdes_readv_iovec_t(memory,
8201cb0ef41Sopenharmony_ci                                    mem_size,
8211cb0ef41Sopenharmony_ci                                    iovs_ptr,
8221cb0ef41Sopenharmony_ci                                    iovs.data(),
8231cb0ef41Sopenharmony_ci                                    iovs_len);
8241cb0ef41Sopenharmony_ci  if (err != UVWASI_ESUCCESS) {
8251cb0ef41Sopenharmony_ci    args.GetReturnValue().Set(err);
8261cb0ef41Sopenharmony_ci    return;
8271cb0ef41Sopenharmony_ci  }
8281cb0ef41Sopenharmony_ci
8291cb0ef41Sopenharmony_ci  uvwasi_size_t nread;
8301cb0ef41Sopenharmony_ci  err = uvwasi_fd_read(&wasi->uvw_, fd, iovs.data(), iovs_len, &nread);
8311cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS)
8321cb0ef41Sopenharmony_ci    uvwasi_serdes_write_size_t(memory, nread_ptr, nread);
8331cb0ef41Sopenharmony_ci
8341cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
8351cb0ef41Sopenharmony_ci}
8361cb0ef41Sopenharmony_ci
8371cb0ef41Sopenharmony_ci
8381cb0ef41Sopenharmony_civoid WASI::FdReaddir(const FunctionCallbackInfo<Value>& args) {
8391cb0ef41Sopenharmony_ci  WASI* wasi;
8401cb0ef41Sopenharmony_ci  uint32_t fd;
8411cb0ef41Sopenharmony_ci  uint32_t buf_ptr;
8421cb0ef41Sopenharmony_ci  uint32_t buf_len;
8431cb0ef41Sopenharmony_ci  uint64_t cookie;
8441cb0ef41Sopenharmony_ci  uint32_t bufused_ptr;
8451cb0ef41Sopenharmony_ci  char* memory;
8461cb0ef41Sopenharmony_ci  size_t mem_size;
8471cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 5);
8481cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
8491cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, buf_ptr);
8501cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, buf_len);
8511cb0ef41Sopenharmony_ci  UNWRAP_BIGINT_OR_RETURN(args, args[3], Uint64, cookie);
8521cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, bufused_ptr);
8531cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
8541cb0ef41Sopenharmony_ci  Debug(wasi,
8551cb0ef41Sopenharmony_ci        "uvwasi_fd_readdir(%d, %d, %d, %d, %d)\n",
8561cb0ef41Sopenharmony_ci        fd,
8571cb0ef41Sopenharmony_ci        buf_ptr,
8581cb0ef41Sopenharmony_ci        buf_len,
8591cb0ef41Sopenharmony_ci        cookie,
8601cb0ef41Sopenharmony_ci        bufused_ptr);
8611cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
8621cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, buf_ptr, buf_len);
8631cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
8641cb0ef41Sopenharmony_ci                         mem_size,
8651cb0ef41Sopenharmony_ci                         bufused_ptr,
8661cb0ef41Sopenharmony_ci                         UVWASI_SERDES_SIZE_size_t);
8671cb0ef41Sopenharmony_ci  uvwasi_size_t bufused;
8681cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_fd_readdir(&wasi->uvw_,
8691cb0ef41Sopenharmony_ci                                         fd,
8701cb0ef41Sopenharmony_ci                                         &memory[buf_ptr],
8711cb0ef41Sopenharmony_ci                                         buf_len,
8721cb0ef41Sopenharmony_ci                                         cookie,
8731cb0ef41Sopenharmony_ci                                         &bufused);
8741cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS)
8751cb0ef41Sopenharmony_ci    uvwasi_serdes_write_size_t(memory, bufused_ptr, bufused);
8761cb0ef41Sopenharmony_ci
8771cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
8781cb0ef41Sopenharmony_ci}
8791cb0ef41Sopenharmony_ci
8801cb0ef41Sopenharmony_ci
8811cb0ef41Sopenharmony_civoid WASI::FdRenumber(const FunctionCallbackInfo<Value>& args) {
8821cb0ef41Sopenharmony_ci  WASI* wasi;
8831cb0ef41Sopenharmony_ci  uint32_t from;
8841cb0ef41Sopenharmony_ci  uint32_t to;
8851cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 2);
8861cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, from);
8871cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, to);
8881cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
8891cb0ef41Sopenharmony_ci  Debug(wasi, "fd_renumber(%d, %d)\n", from, to);
8901cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_fd_renumber(&wasi->uvw_, from, to);
8911cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
8921cb0ef41Sopenharmony_ci}
8931cb0ef41Sopenharmony_ci
8941cb0ef41Sopenharmony_ci
8951cb0ef41Sopenharmony_civoid WASI::FdSeek(const FunctionCallbackInfo<Value>& args) {
8961cb0ef41Sopenharmony_ci  WASI* wasi;
8971cb0ef41Sopenharmony_ci  uint32_t fd;
8981cb0ef41Sopenharmony_ci  int64_t offset;
8991cb0ef41Sopenharmony_ci  uint8_t whence;
9001cb0ef41Sopenharmony_ci  uint32_t newoffset_ptr;
9011cb0ef41Sopenharmony_ci  char* memory;
9021cb0ef41Sopenharmony_ci  size_t mem_size;
9031cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 4);
9041cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
9051cb0ef41Sopenharmony_ci  UNWRAP_BIGINT_OR_RETURN(args, args[1], Int64, offset);
9061cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, whence);
9071cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, newoffset_ptr);
9081cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
9091cb0ef41Sopenharmony_ci  Debug(wasi, "fd_seek(%d, %d, %d, %d)\n", fd, offset, whence, newoffset_ptr);
9101cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
9111cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
9121cb0ef41Sopenharmony_ci                         mem_size,
9131cb0ef41Sopenharmony_ci                         newoffset_ptr,
9141cb0ef41Sopenharmony_ci                         UVWASI_SERDES_SIZE_filesize_t);
9151cb0ef41Sopenharmony_ci  uvwasi_filesize_t newoffset;
9161cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_fd_seek(&wasi->uvw_,
9171cb0ef41Sopenharmony_ci                                      fd,
9181cb0ef41Sopenharmony_ci                                      offset,
9191cb0ef41Sopenharmony_ci                                      whence,
9201cb0ef41Sopenharmony_ci                                      &newoffset);
9211cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS)
9221cb0ef41Sopenharmony_ci    uvwasi_serdes_write_filesize_t(memory, newoffset_ptr, newoffset);
9231cb0ef41Sopenharmony_ci
9241cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
9251cb0ef41Sopenharmony_ci}
9261cb0ef41Sopenharmony_ci
9271cb0ef41Sopenharmony_ci
9281cb0ef41Sopenharmony_civoid WASI::FdSync(const FunctionCallbackInfo<Value>& args) {
9291cb0ef41Sopenharmony_ci  WASI* wasi;
9301cb0ef41Sopenharmony_ci  uint32_t fd;
9311cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 1);
9321cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
9331cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
9341cb0ef41Sopenharmony_ci  Debug(wasi, "fd_sync(%d)\n", fd);
9351cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_fd_sync(&wasi->uvw_, fd);
9361cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
9371cb0ef41Sopenharmony_ci}
9381cb0ef41Sopenharmony_ci
9391cb0ef41Sopenharmony_ci
9401cb0ef41Sopenharmony_civoid WASI::FdTell(const FunctionCallbackInfo<Value>& args) {
9411cb0ef41Sopenharmony_ci  WASI* wasi;
9421cb0ef41Sopenharmony_ci  uint32_t fd;
9431cb0ef41Sopenharmony_ci  uint32_t offset_ptr;
9441cb0ef41Sopenharmony_ci  char* memory;
9451cb0ef41Sopenharmony_ci  size_t mem_size;
9461cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 2);
9471cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
9481cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, offset_ptr);
9491cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
9501cb0ef41Sopenharmony_ci  Debug(wasi, "fd_tell(%d, %d)\n", fd, offset_ptr);
9511cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
9521cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
9531cb0ef41Sopenharmony_ci                         mem_size,
9541cb0ef41Sopenharmony_ci                         offset_ptr,
9551cb0ef41Sopenharmony_ci                         UVWASI_SERDES_SIZE_filesize_t);
9561cb0ef41Sopenharmony_ci  uvwasi_filesize_t offset;
9571cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_fd_tell(&wasi->uvw_, fd, &offset);
9581cb0ef41Sopenharmony_ci
9591cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS)
9601cb0ef41Sopenharmony_ci    uvwasi_serdes_write_filesize_t(memory, offset_ptr, offset);
9611cb0ef41Sopenharmony_ci
9621cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
9631cb0ef41Sopenharmony_ci}
9641cb0ef41Sopenharmony_ci
9651cb0ef41Sopenharmony_ci
9661cb0ef41Sopenharmony_civoid WASI::FdWrite(const FunctionCallbackInfo<Value>& args) {
9671cb0ef41Sopenharmony_ci  WASI* wasi;
9681cb0ef41Sopenharmony_ci  uint32_t fd;
9691cb0ef41Sopenharmony_ci  uint32_t iovs_ptr;
9701cb0ef41Sopenharmony_ci  uint32_t iovs_len;
9711cb0ef41Sopenharmony_ci  uint32_t nwritten_ptr;
9721cb0ef41Sopenharmony_ci  char* memory;
9731cb0ef41Sopenharmony_ci  size_t mem_size;
9741cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 4);
9751cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
9761cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, iovs_ptr);
9771cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, iovs_len);
9781cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, nwritten_ptr);
9791cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
9801cb0ef41Sopenharmony_ci  Debug(wasi,
9811cb0ef41Sopenharmony_ci        "fd_write(%d, %d, %d, %d)\n",
9821cb0ef41Sopenharmony_ci        fd,
9831cb0ef41Sopenharmony_ci        iovs_ptr,
9841cb0ef41Sopenharmony_ci        iovs_len,
9851cb0ef41Sopenharmony_ci        nwritten_ptr);
9861cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
9871cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
9881cb0ef41Sopenharmony_ci                         mem_size,
9891cb0ef41Sopenharmony_ci                         iovs_ptr,
9901cb0ef41Sopenharmony_ci                         iovs_len * UVWASI_SERDES_SIZE_ciovec_t);
9911cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
9921cb0ef41Sopenharmony_ci                         mem_size,
9931cb0ef41Sopenharmony_ci                         nwritten_ptr,
9941cb0ef41Sopenharmony_ci                         UVWASI_SERDES_SIZE_size_t);
9951cb0ef41Sopenharmony_ci  std::vector<uvwasi_ciovec_t> iovs(iovs_len);
9961cb0ef41Sopenharmony_ci  uvwasi_errno_t err;
9971cb0ef41Sopenharmony_ci
9981cb0ef41Sopenharmony_ci  err = uvwasi_serdes_readv_ciovec_t(memory,
9991cb0ef41Sopenharmony_ci                                     mem_size,
10001cb0ef41Sopenharmony_ci                                     iovs_ptr,
10011cb0ef41Sopenharmony_ci                                     iovs.data(),
10021cb0ef41Sopenharmony_ci                                     iovs_len);
10031cb0ef41Sopenharmony_ci  if (err != UVWASI_ESUCCESS) {
10041cb0ef41Sopenharmony_ci    args.GetReturnValue().Set(err);
10051cb0ef41Sopenharmony_ci    return;
10061cb0ef41Sopenharmony_ci  }
10071cb0ef41Sopenharmony_ci
10081cb0ef41Sopenharmony_ci  uvwasi_size_t nwritten;
10091cb0ef41Sopenharmony_ci  err = uvwasi_fd_write(&wasi->uvw_, fd, iovs.data(), iovs_len, &nwritten);
10101cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS)
10111cb0ef41Sopenharmony_ci    uvwasi_serdes_write_size_t(memory, nwritten_ptr, nwritten);
10121cb0ef41Sopenharmony_ci
10131cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
10141cb0ef41Sopenharmony_ci}
10151cb0ef41Sopenharmony_ci
10161cb0ef41Sopenharmony_ci
10171cb0ef41Sopenharmony_civoid WASI::PathCreateDirectory(const FunctionCallbackInfo<Value>& args) {
10181cb0ef41Sopenharmony_ci  WASI* wasi;
10191cb0ef41Sopenharmony_ci  uint32_t fd;
10201cb0ef41Sopenharmony_ci  uint32_t path_ptr;
10211cb0ef41Sopenharmony_ci  uint32_t path_len;
10221cb0ef41Sopenharmony_ci  char* memory;
10231cb0ef41Sopenharmony_ci  size_t mem_size;
10241cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 3);
10251cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
10261cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, path_ptr);
10271cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, path_len);
10281cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
10291cb0ef41Sopenharmony_ci  Debug(wasi, "path_create_directory(%d, %d, %d)\n", fd, path_ptr, path_len);
10301cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
10311cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, path_ptr, path_len);
10321cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_path_create_directory(&wasi->uvw_,
10331cb0ef41Sopenharmony_ci                                                    fd,
10341cb0ef41Sopenharmony_ci                                                    &memory[path_ptr],
10351cb0ef41Sopenharmony_ci                                                    path_len);
10361cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
10371cb0ef41Sopenharmony_ci}
10381cb0ef41Sopenharmony_ci
10391cb0ef41Sopenharmony_ci
10401cb0ef41Sopenharmony_civoid WASI::PathFilestatGet(const FunctionCallbackInfo<Value>& args) {
10411cb0ef41Sopenharmony_ci  WASI* wasi;
10421cb0ef41Sopenharmony_ci  uint32_t fd;
10431cb0ef41Sopenharmony_ci  uint32_t flags;
10441cb0ef41Sopenharmony_ci  uint32_t path_ptr;
10451cb0ef41Sopenharmony_ci  uint32_t path_len;
10461cb0ef41Sopenharmony_ci  uint32_t buf_ptr;
10471cb0ef41Sopenharmony_ci  char* memory;
10481cb0ef41Sopenharmony_ci  size_t mem_size;
10491cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 5);
10501cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
10511cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, flags);
10521cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, path_ptr);
10531cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, path_len);
10541cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, buf_ptr);
10551cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
10561cb0ef41Sopenharmony_ci  Debug(wasi,
10571cb0ef41Sopenharmony_ci        "path_filestat_get(%d, %d, %d)\n",
10581cb0ef41Sopenharmony_ci        fd,
10591cb0ef41Sopenharmony_ci        path_ptr,
10601cb0ef41Sopenharmony_ci        path_len);
10611cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
10621cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, path_ptr, path_len);
10631cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
10641cb0ef41Sopenharmony_ci                         mem_size,
10651cb0ef41Sopenharmony_ci                         buf_ptr,
10661cb0ef41Sopenharmony_ci                         UVWASI_SERDES_SIZE_filestat_t);
10671cb0ef41Sopenharmony_ci  uvwasi_filestat_t stats;
10681cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_path_filestat_get(&wasi->uvw_,
10691cb0ef41Sopenharmony_ci                                                fd,
10701cb0ef41Sopenharmony_ci                                                flags,
10711cb0ef41Sopenharmony_ci                                                &memory[path_ptr],
10721cb0ef41Sopenharmony_ci                                                path_len,
10731cb0ef41Sopenharmony_ci                                                &stats);
10741cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS)
10751cb0ef41Sopenharmony_ci    uvwasi_serdes_write_filestat_t(memory, buf_ptr, &stats);
10761cb0ef41Sopenharmony_ci
10771cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
10781cb0ef41Sopenharmony_ci}
10791cb0ef41Sopenharmony_ci
10801cb0ef41Sopenharmony_ci
10811cb0ef41Sopenharmony_civoid WASI::PathFilestatSetTimes(const FunctionCallbackInfo<Value>& args) {
10821cb0ef41Sopenharmony_ci  WASI* wasi;
10831cb0ef41Sopenharmony_ci  uint32_t fd;
10841cb0ef41Sopenharmony_ci  uint32_t flags;
10851cb0ef41Sopenharmony_ci  uint32_t path_ptr;
10861cb0ef41Sopenharmony_ci  uint32_t path_len;
10871cb0ef41Sopenharmony_ci  uint64_t st_atim;
10881cb0ef41Sopenharmony_ci  uint64_t st_mtim;
10891cb0ef41Sopenharmony_ci  uint16_t fst_flags;
10901cb0ef41Sopenharmony_ci  char* memory;
10911cb0ef41Sopenharmony_ci  size_t mem_size;
10921cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 7);
10931cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
10941cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, flags);
10951cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, path_ptr);
10961cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, path_len);
10971cb0ef41Sopenharmony_ci  UNWRAP_BIGINT_OR_RETURN(args, args[4], Uint64, st_atim);
10981cb0ef41Sopenharmony_ci  UNWRAP_BIGINT_OR_RETURN(args, args[5], Uint64, st_mtim);
10991cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[6], Uint32, fst_flags);
11001cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
11011cb0ef41Sopenharmony_ci  Debug(wasi,
11021cb0ef41Sopenharmony_ci        "path_filestat_set_times(%d, %d, %d, %d, %d, %d, %d)\n",
11031cb0ef41Sopenharmony_ci        fd,
11041cb0ef41Sopenharmony_ci        flags,
11051cb0ef41Sopenharmony_ci        path_ptr,
11061cb0ef41Sopenharmony_ci        path_len,
11071cb0ef41Sopenharmony_ci        st_atim,
11081cb0ef41Sopenharmony_ci        st_mtim,
11091cb0ef41Sopenharmony_ci        fst_flags);
11101cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
11111cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, path_ptr, path_len);
11121cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_path_filestat_set_times(&wasi->uvw_,
11131cb0ef41Sopenharmony_ci                                                      fd,
11141cb0ef41Sopenharmony_ci                                                      flags,
11151cb0ef41Sopenharmony_ci                                                      &memory[path_ptr],
11161cb0ef41Sopenharmony_ci                                                      path_len,
11171cb0ef41Sopenharmony_ci                                                      st_atim,
11181cb0ef41Sopenharmony_ci                                                      st_mtim,
11191cb0ef41Sopenharmony_ci                                                      fst_flags);
11201cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
11211cb0ef41Sopenharmony_ci}
11221cb0ef41Sopenharmony_ci
11231cb0ef41Sopenharmony_ci
11241cb0ef41Sopenharmony_civoid WASI::PathLink(const FunctionCallbackInfo<Value>& args) {
11251cb0ef41Sopenharmony_ci  WASI* wasi;
11261cb0ef41Sopenharmony_ci  uint32_t old_fd;
11271cb0ef41Sopenharmony_ci  uint32_t old_flags;
11281cb0ef41Sopenharmony_ci  uint32_t old_path_ptr;
11291cb0ef41Sopenharmony_ci  uint32_t old_path_len;
11301cb0ef41Sopenharmony_ci  uint32_t new_fd;
11311cb0ef41Sopenharmony_ci  uint32_t new_path_ptr;
11321cb0ef41Sopenharmony_ci  uint32_t new_path_len;
11331cb0ef41Sopenharmony_ci  char* memory;
11341cb0ef41Sopenharmony_ci  size_t mem_size;
11351cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 7);
11361cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, old_fd);
11371cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, old_flags);
11381cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, old_path_ptr);
11391cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, old_path_len);
11401cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, new_fd);
11411cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[5], Uint32, new_path_ptr);
11421cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[6], Uint32, new_path_len);
11431cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
11441cb0ef41Sopenharmony_ci  Debug(wasi,
11451cb0ef41Sopenharmony_ci        "path_link(%d, %d, %d, %d, %d, %d, %d)\n",
11461cb0ef41Sopenharmony_ci        old_fd,
11471cb0ef41Sopenharmony_ci        old_flags,
11481cb0ef41Sopenharmony_ci        old_path_ptr,
11491cb0ef41Sopenharmony_ci        old_path_len,
11501cb0ef41Sopenharmony_ci        new_fd,
11511cb0ef41Sopenharmony_ci        new_path_ptr,
11521cb0ef41Sopenharmony_ci        new_path_len);
11531cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
11541cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, old_path_ptr, old_path_len);
11551cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, new_path_ptr, new_path_len);
11561cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_path_link(&wasi->uvw_,
11571cb0ef41Sopenharmony_ci                                        old_fd,
11581cb0ef41Sopenharmony_ci                                        old_flags,
11591cb0ef41Sopenharmony_ci                                        &memory[old_path_ptr],
11601cb0ef41Sopenharmony_ci                                        old_path_len,
11611cb0ef41Sopenharmony_ci                                        new_fd,
11621cb0ef41Sopenharmony_ci                                        &memory[new_path_ptr],
11631cb0ef41Sopenharmony_ci                                        new_path_len);
11641cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
11651cb0ef41Sopenharmony_ci}
11661cb0ef41Sopenharmony_ci
11671cb0ef41Sopenharmony_ci
11681cb0ef41Sopenharmony_civoid WASI::PathOpen(const FunctionCallbackInfo<Value>& args) {
11691cb0ef41Sopenharmony_ci  WASI* wasi;
11701cb0ef41Sopenharmony_ci  uint32_t dirfd;
11711cb0ef41Sopenharmony_ci  uint32_t dirflags;
11721cb0ef41Sopenharmony_ci  uint32_t path_ptr;
11731cb0ef41Sopenharmony_ci  uint32_t path_len;
11741cb0ef41Sopenharmony_ci  uint32_t o_flags;
11751cb0ef41Sopenharmony_ci  uint64_t fs_rights_base;
11761cb0ef41Sopenharmony_ci  uint64_t fs_rights_inheriting;
11771cb0ef41Sopenharmony_ci  uint32_t fs_flags;
11781cb0ef41Sopenharmony_ci  uint32_t fd_ptr;
11791cb0ef41Sopenharmony_ci  char* memory;
11801cb0ef41Sopenharmony_ci  size_t mem_size;
11811cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 9);
11821cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, dirfd);
11831cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, dirflags);
11841cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, path_ptr);
11851cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, path_len);
11861cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, o_flags);
11871cb0ef41Sopenharmony_ci  UNWRAP_BIGINT_OR_RETURN(args, args[5], Uint64, fs_rights_base);
11881cb0ef41Sopenharmony_ci  UNWRAP_BIGINT_OR_RETURN(args, args[6], Uint64, fs_rights_inheriting);
11891cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[7], Uint32, fs_flags);
11901cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[8], Uint32, fd_ptr);
11911cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
11921cb0ef41Sopenharmony_ci  Debug(wasi,
11931cb0ef41Sopenharmony_ci        "path_open(%d, %d, %d, %d, %d, %d, %d, %d, %d)\n",
11941cb0ef41Sopenharmony_ci        dirfd,
11951cb0ef41Sopenharmony_ci        dirflags,
11961cb0ef41Sopenharmony_ci        path_ptr,
11971cb0ef41Sopenharmony_ci        path_len,
11981cb0ef41Sopenharmony_ci        o_flags,
11991cb0ef41Sopenharmony_ci        fs_rights_base,
12001cb0ef41Sopenharmony_ci        fs_rights_inheriting,
12011cb0ef41Sopenharmony_ci        fs_flags,
12021cb0ef41Sopenharmony_ci        fd_ptr);
12031cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
12041cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, path_ptr, path_len);
12051cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, fd_ptr, UVWASI_SERDES_SIZE_fd_t);
12061cb0ef41Sopenharmony_ci  uvwasi_fd_t fd;
12071cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_path_open(&wasi->uvw_,
12081cb0ef41Sopenharmony_ci                                        dirfd,
12091cb0ef41Sopenharmony_ci                                        dirflags,
12101cb0ef41Sopenharmony_ci                                        &memory[path_ptr],
12111cb0ef41Sopenharmony_ci                                        path_len,
12121cb0ef41Sopenharmony_ci                                        static_cast<uvwasi_oflags_t>(o_flags),
12131cb0ef41Sopenharmony_ci                                        fs_rights_base,
12141cb0ef41Sopenharmony_ci                                        fs_rights_inheriting,
12151cb0ef41Sopenharmony_ci                                        static_cast<uvwasi_fdflags_t>(fs_flags),
12161cb0ef41Sopenharmony_ci                                        &fd);
12171cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS)
12181cb0ef41Sopenharmony_ci    uvwasi_serdes_write_size_t(memory, fd_ptr, fd);
12191cb0ef41Sopenharmony_ci
12201cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
12211cb0ef41Sopenharmony_ci}
12221cb0ef41Sopenharmony_ci
12231cb0ef41Sopenharmony_ci
12241cb0ef41Sopenharmony_civoid WASI::PathReadlink(const FunctionCallbackInfo<Value>& args) {
12251cb0ef41Sopenharmony_ci  WASI* wasi;
12261cb0ef41Sopenharmony_ci  uint32_t fd;
12271cb0ef41Sopenharmony_ci  uint32_t path_ptr;
12281cb0ef41Sopenharmony_ci  uint32_t path_len;
12291cb0ef41Sopenharmony_ci  uint32_t buf_ptr;
12301cb0ef41Sopenharmony_ci  uint32_t buf_len;
12311cb0ef41Sopenharmony_ci  uint32_t bufused_ptr;
12321cb0ef41Sopenharmony_ci  char* memory;
12331cb0ef41Sopenharmony_ci  size_t mem_size;
12341cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 6);
12351cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
12361cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, path_ptr);
12371cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, path_len);
12381cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, buf_ptr);
12391cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, buf_len);
12401cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[5], Uint32, bufused_ptr);
12411cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
12421cb0ef41Sopenharmony_ci  Debug(wasi,
12431cb0ef41Sopenharmony_ci        "path_readlink(%d, %d, %d, %d, %d, %d)\n",
12441cb0ef41Sopenharmony_ci        fd,
12451cb0ef41Sopenharmony_ci        path_ptr,
12461cb0ef41Sopenharmony_ci        path_len,
12471cb0ef41Sopenharmony_ci        buf_ptr,
12481cb0ef41Sopenharmony_ci        buf_len,
12491cb0ef41Sopenharmony_ci        bufused_ptr);
12501cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
12511cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, path_ptr, path_len);
12521cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, buf_ptr, buf_len);
12531cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
12541cb0ef41Sopenharmony_ci                         mem_size,
12551cb0ef41Sopenharmony_ci                         bufused_ptr,
12561cb0ef41Sopenharmony_ci                         UVWASI_SERDES_SIZE_size_t);
12571cb0ef41Sopenharmony_ci  uvwasi_size_t bufused;
12581cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_path_readlink(&wasi->uvw_,
12591cb0ef41Sopenharmony_ci                                            fd,
12601cb0ef41Sopenharmony_ci                                            &memory[path_ptr],
12611cb0ef41Sopenharmony_ci                                            path_len,
12621cb0ef41Sopenharmony_ci                                            &memory[buf_ptr],
12631cb0ef41Sopenharmony_ci                                            buf_len,
12641cb0ef41Sopenharmony_ci                                            &bufused);
12651cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS)
12661cb0ef41Sopenharmony_ci    uvwasi_serdes_write_size_t(memory, bufused_ptr, bufused);
12671cb0ef41Sopenharmony_ci
12681cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
12691cb0ef41Sopenharmony_ci}
12701cb0ef41Sopenharmony_ci
12711cb0ef41Sopenharmony_ci
12721cb0ef41Sopenharmony_civoid WASI::PathRemoveDirectory(const FunctionCallbackInfo<Value>& args) {
12731cb0ef41Sopenharmony_ci  WASI* wasi;
12741cb0ef41Sopenharmony_ci  uint32_t fd;
12751cb0ef41Sopenharmony_ci  uint32_t path_ptr;
12761cb0ef41Sopenharmony_ci  uint32_t path_len;
12771cb0ef41Sopenharmony_ci  char* memory;
12781cb0ef41Sopenharmony_ci  size_t mem_size;
12791cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 3);
12801cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
12811cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, path_ptr);
12821cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, path_len);
12831cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
12841cb0ef41Sopenharmony_ci  Debug(wasi, "path_remove_directory(%d, %d, %d)\n", fd, path_ptr, path_len);
12851cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
12861cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, path_ptr, path_len);
12871cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_path_remove_directory(&wasi->uvw_,
12881cb0ef41Sopenharmony_ci                                                    fd,
12891cb0ef41Sopenharmony_ci                                                    &memory[path_ptr],
12901cb0ef41Sopenharmony_ci                                                    path_len);
12911cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
12921cb0ef41Sopenharmony_ci}
12931cb0ef41Sopenharmony_ci
12941cb0ef41Sopenharmony_ci
12951cb0ef41Sopenharmony_civoid WASI::PathRename(const FunctionCallbackInfo<Value>& args) {
12961cb0ef41Sopenharmony_ci  WASI* wasi;
12971cb0ef41Sopenharmony_ci  uint32_t old_fd;
12981cb0ef41Sopenharmony_ci  uint32_t old_path_ptr;
12991cb0ef41Sopenharmony_ci  uint32_t old_path_len;
13001cb0ef41Sopenharmony_ci  uint32_t new_fd;
13011cb0ef41Sopenharmony_ci  uint32_t new_path_ptr;
13021cb0ef41Sopenharmony_ci  uint32_t new_path_len;
13031cb0ef41Sopenharmony_ci  char* memory;
13041cb0ef41Sopenharmony_ci  size_t mem_size;
13051cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 6);
13061cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, old_fd);
13071cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, old_path_ptr);
13081cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, old_path_len);
13091cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, new_fd);
13101cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, new_path_ptr);
13111cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[5], Uint32, new_path_len);
13121cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
13131cb0ef41Sopenharmony_ci  Debug(wasi,
13141cb0ef41Sopenharmony_ci        "path_rename(%d, %d, %d, %d, %d, %d)\n",
13151cb0ef41Sopenharmony_ci        old_fd,
13161cb0ef41Sopenharmony_ci        old_path_ptr,
13171cb0ef41Sopenharmony_ci        old_path_len,
13181cb0ef41Sopenharmony_ci        new_fd,
13191cb0ef41Sopenharmony_ci        new_path_ptr,
13201cb0ef41Sopenharmony_ci        new_path_len);
13211cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
13221cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, old_path_ptr, old_path_len);
13231cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, new_path_ptr, new_path_len);
13241cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_path_rename(&wasi->uvw_,
13251cb0ef41Sopenharmony_ci                                          old_fd,
13261cb0ef41Sopenharmony_ci                                          &memory[old_path_ptr],
13271cb0ef41Sopenharmony_ci                                          old_path_len,
13281cb0ef41Sopenharmony_ci                                          new_fd,
13291cb0ef41Sopenharmony_ci                                          &memory[new_path_ptr],
13301cb0ef41Sopenharmony_ci                                          new_path_len);
13311cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
13321cb0ef41Sopenharmony_ci}
13331cb0ef41Sopenharmony_ci
13341cb0ef41Sopenharmony_ci
13351cb0ef41Sopenharmony_civoid WASI::PathSymlink(const FunctionCallbackInfo<Value>& args) {
13361cb0ef41Sopenharmony_ci  WASI* wasi;
13371cb0ef41Sopenharmony_ci  uint32_t old_path_ptr;
13381cb0ef41Sopenharmony_ci  uint32_t old_path_len;
13391cb0ef41Sopenharmony_ci  uint32_t fd;
13401cb0ef41Sopenharmony_ci  uint32_t new_path_ptr;
13411cb0ef41Sopenharmony_ci  uint32_t new_path_len;
13421cb0ef41Sopenharmony_ci  char* memory;
13431cb0ef41Sopenharmony_ci  size_t mem_size;
13441cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 5);
13451cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, old_path_ptr);
13461cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, old_path_len);
13471cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, fd);
13481cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, new_path_ptr);
13491cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, new_path_len);
13501cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
13511cb0ef41Sopenharmony_ci  Debug(wasi,
13521cb0ef41Sopenharmony_ci        "path_symlink(%d, %d, %d, %d, %d)\n",
13531cb0ef41Sopenharmony_ci        old_path_ptr,
13541cb0ef41Sopenharmony_ci        old_path_len,
13551cb0ef41Sopenharmony_ci        fd,
13561cb0ef41Sopenharmony_ci        new_path_ptr,
13571cb0ef41Sopenharmony_ci        new_path_len);
13581cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
13591cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, old_path_ptr, old_path_len);
13601cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, new_path_ptr, new_path_len);
13611cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_path_symlink(&wasi->uvw_,
13621cb0ef41Sopenharmony_ci                                           &memory[old_path_ptr],
13631cb0ef41Sopenharmony_ci                                           old_path_len,
13641cb0ef41Sopenharmony_ci                                           fd,
13651cb0ef41Sopenharmony_ci                                           &memory[new_path_ptr],
13661cb0ef41Sopenharmony_ci                                           new_path_len);
13671cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
13681cb0ef41Sopenharmony_ci}
13691cb0ef41Sopenharmony_ci
13701cb0ef41Sopenharmony_ci
13711cb0ef41Sopenharmony_civoid WASI::PathUnlinkFile(const FunctionCallbackInfo<Value>& args) {
13721cb0ef41Sopenharmony_ci  WASI* wasi;
13731cb0ef41Sopenharmony_ci  uint32_t fd;
13741cb0ef41Sopenharmony_ci  uint32_t path_ptr;
13751cb0ef41Sopenharmony_ci  uint32_t path_len;
13761cb0ef41Sopenharmony_ci  char* memory;
13771cb0ef41Sopenharmony_ci  size_t mem_size;
13781cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 3);
13791cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, fd);
13801cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, path_ptr);
13811cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, path_len);
13821cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
13831cb0ef41Sopenharmony_ci  Debug(wasi, "path_unlink_file(%d, %d, %d)\n", fd, path_ptr, path_len);
13841cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
13851cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, path_ptr, path_len);
13861cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_path_unlink_file(&wasi->uvw_,
13871cb0ef41Sopenharmony_ci                                               fd,
13881cb0ef41Sopenharmony_ci                                               &memory[path_ptr],
13891cb0ef41Sopenharmony_ci                                               path_len);
13901cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
13911cb0ef41Sopenharmony_ci}
13921cb0ef41Sopenharmony_ci
13931cb0ef41Sopenharmony_ci
13941cb0ef41Sopenharmony_civoid WASI::PollOneoff(const FunctionCallbackInfo<Value>& args) {
13951cb0ef41Sopenharmony_ci  WASI* wasi;
13961cb0ef41Sopenharmony_ci  uint32_t in_ptr;
13971cb0ef41Sopenharmony_ci  uint32_t out_ptr;
13981cb0ef41Sopenharmony_ci  uint32_t nsubscriptions;
13991cb0ef41Sopenharmony_ci  uint32_t nevents_ptr;
14001cb0ef41Sopenharmony_ci  char* memory;
14011cb0ef41Sopenharmony_ci  size_t mem_size;
14021cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 4);
14031cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, in_ptr);
14041cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, out_ptr);
14051cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, nsubscriptions);
14061cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, nevents_ptr);
14071cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
14081cb0ef41Sopenharmony_ci  Debug(wasi,
14091cb0ef41Sopenharmony_ci        "poll_oneoff(%d, %d, %d, %d)\n",
14101cb0ef41Sopenharmony_ci        in_ptr,
14111cb0ef41Sopenharmony_ci        out_ptr,
14121cb0ef41Sopenharmony_ci        nsubscriptions,
14131cb0ef41Sopenharmony_ci        nevents_ptr);
14141cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
14151cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
14161cb0ef41Sopenharmony_ci                         mem_size,
14171cb0ef41Sopenharmony_ci                         in_ptr,
14181cb0ef41Sopenharmony_ci                         nsubscriptions * UVWASI_SERDES_SIZE_subscription_t);
14191cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
14201cb0ef41Sopenharmony_ci                         mem_size,
14211cb0ef41Sopenharmony_ci                         out_ptr,
14221cb0ef41Sopenharmony_ci                         nsubscriptions * UVWASI_SERDES_SIZE_event_t);
14231cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
14241cb0ef41Sopenharmony_ci                         mem_size,
14251cb0ef41Sopenharmony_ci                         nevents_ptr,
14261cb0ef41Sopenharmony_ci                         UVWASI_SERDES_SIZE_size_t);
14271cb0ef41Sopenharmony_ci  std::vector<uvwasi_subscription_t> in(nsubscriptions);
14281cb0ef41Sopenharmony_ci  std::vector<uvwasi_event_t> out(nsubscriptions);
14291cb0ef41Sopenharmony_ci
14301cb0ef41Sopenharmony_ci  for (uint32_t i = 0; i < nsubscriptions; ++i) {
14311cb0ef41Sopenharmony_ci    uvwasi_serdes_read_subscription_t(memory, in_ptr, &in[i]);
14321cb0ef41Sopenharmony_ci    in_ptr += UVWASI_SERDES_SIZE_subscription_t;
14331cb0ef41Sopenharmony_ci  }
14341cb0ef41Sopenharmony_ci
14351cb0ef41Sopenharmony_ci  uvwasi_size_t nevents;
14361cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_poll_oneoff(&wasi->uvw_,
14371cb0ef41Sopenharmony_ci                                          in.data(),
14381cb0ef41Sopenharmony_ci                                          out.data(),
14391cb0ef41Sopenharmony_ci                                          nsubscriptions,
14401cb0ef41Sopenharmony_ci                                          &nevents);
14411cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS) {
14421cb0ef41Sopenharmony_ci    uvwasi_serdes_write_size_t(memory, nevents_ptr, nevents);
14431cb0ef41Sopenharmony_ci
14441cb0ef41Sopenharmony_ci    for (uint32_t i = 0; i < nsubscriptions; ++i) {
14451cb0ef41Sopenharmony_ci      uvwasi_serdes_write_event_t(memory, out_ptr, &out[i]);
14461cb0ef41Sopenharmony_ci      out_ptr += UVWASI_SERDES_SIZE_event_t;
14471cb0ef41Sopenharmony_ci    }
14481cb0ef41Sopenharmony_ci  }
14491cb0ef41Sopenharmony_ci
14501cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
14511cb0ef41Sopenharmony_ci}
14521cb0ef41Sopenharmony_ci
14531cb0ef41Sopenharmony_ci
14541cb0ef41Sopenharmony_civoid WASI::ProcExit(const FunctionCallbackInfo<Value>& args) {
14551cb0ef41Sopenharmony_ci  WASI* wasi;
14561cb0ef41Sopenharmony_ci  uint32_t code;
14571cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 1);
14581cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, code);
14591cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
14601cb0ef41Sopenharmony_ci  Debug(wasi, "proc_exit(%d)\n", code);
14611cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(uvwasi_proc_exit(&wasi->uvw_, code));
14621cb0ef41Sopenharmony_ci}
14631cb0ef41Sopenharmony_ci
14641cb0ef41Sopenharmony_ci
14651cb0ef41Sopenharmony_civoid WASI::ProcRaise(const FunctionCallbackInfo<Value>& args) {
14661cb0ef41Sopenharmony_ci  WASI* wasi;
14671cb0ef41Sopenharmony_ci  uint32_t sig;
14681cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 1);
14691cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, sig);
14701cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
14711cb0ef41Sopenharmony_ci  Debug(wasi, "proc_raise(%d)\n", sig);
14721cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_proc_raise(&wasi->uvw_, sig);
14731cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
14741cb0ef41Sopenharmony_ci}
14751cb0ef41Sopenharmony_ci
14761cb0ef41Sopenharmony_ci
14771cb0ef41Sopenharmony_civoid WASI::RandomGet(const FunctionCallbackInfo<Value>& args) {
14781cb0ef41Sopenharmony_ci  WASI* wasi;
14791cb0ef41Sopenharmony_ci  uint32_t buf_ptr;
14801cb0ef41Sopenharmony_ci  uint32_t buf_len;
14811cb0ef41Sopenharmony_ci  char* memory;
14821cb0ef41Sopenharmony_ci  size_t mem_size;
14831cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 2);
14841cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, buf_ptr);
14851cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, buf_len);
14861cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
14871cb0ef41Sopenharmony_ci  Debug(wasi, "random_get(%d, %d)\n", buf_ptr, buf_len);
14881cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
14891cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, buf_ptr, buf_len);
14901cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_random_get(&wasi->uvw_,
14911cb0ef41Sopenharmony_ci                                         &memory[buf_ptr],
14921cb0ef41Sopenharmony_ci                                         buf_len);
14931cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
14941cb0ef41Sopenharmony_ci}
14951cb0ef41Sopenharmony_ci
14961cb0ef41Sopenharmony_ci
14971cb0ef41Sopenharmony_civoid WASI::SchedYield(const FunctionCallbackInfo<Value>& args) {
14981cb0ef41Sopenharmony_ci  WASI* wasi;
14991cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 0);
15001cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
15011cb0ef41Sopenharmony_ci  Debug(wasi, "sched_yield()\n");
15021cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_sched_yield(&wasi->uvw_);
15031cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
15041cb0ef41Sopenharmony_ci}
15051cb0ef41Sopenharmony_ci
15061cb0ef41Sopenharmony_civoid WASI::SockAccept(const FunctionCallbackInfo<Value>& args) {
15071cb0ef41Sopenharmony_ci  WASI* wasi;
15081cb0ef41Sopenharmony_ci  uint32_t sock;
15091cb0ef41Sopenharmony_ci  uint32_t flags;
15101cb0ef41Sopenharmony_ci  uint32_t fd_ptr;
15111cb0ef41Sopenharmony_ci  char* memory;
15121cb0ef41Sopenharmony_ci  size_t mem_size;
15131cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 3);
15141cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, sock);
15151cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, flags);
15161cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, fd_ptr);
15171cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
15181cb0ef41Sopenharmony_ci  Debug(wasi,
15191cb0ef41Sopenharmony_ci        "sock_accept(%d, %d, %d)\n",
15201cb0ef41Sopenharmony_ci        sock,
15211cb0ef41Sopenharmony_ci        flags,
15221cb0ef41Sopenharmony_ci        fd_ptr);
15231cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
15241cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, fd_ptr, UVWASI_SERDES_SIZE_fd_t);
15251cb0ef41Sopenharmony_ci
15261cb0ef41Sopenharmony_ci  uvwasi_fd_t fd;
15271cb0ef41Sopenharmony_ci  uvwasi_errno_t err =  uvwasi_sock_accept(&wasi->uvw_,
15281cb0ef41Sopenharmony_ci                                           sock,
15291cb0ef41Sopenharmony_ci                                           flags,
15301cb0ef41Sopenharmony_ci                                           &fd);
15311cb0ef41Sopenharmony_ci
15321cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS)
15331cb0ef41Sopenharmony_ci    uvwasi_serdes_write_size_t(memory, fd_ptr, fd);
15341cb0ef41Sopenharmony_ci
15351cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
15361cb0ef41Sopenharmony_ci}
15371cb0ef41Sopenharmony_ci
15381cb0ef41Sopenharmony_civoid WASI::SockRecv(const FunctionCallbackInfo<Value>& args) {
15391cb0ef41Sopenharmony_ci  WASI* wasi;
15401cb0ef41Sopenharmony_ci  uint32_t sock;
15411cb0ef41Sopenharmony_ci  uint32_t ri_data_ptr;
15421cb0ef41Sopenharmony_ci  uint32_t ri_data_len;
15431cb0ef41Sopenharmony_ci  uint16_t ri_flags;
15441cb0ef41Sopenharmony_ci  uint32_t ro_datalen_ptr;
15451cb0ef41Sopenharmony_ci  uint16_t ro_flags_ptr;
15461cb0ef41Sopenharmony_ci  char* memory;
15471cb0ef41Sopenharmony_ci  size_t mem_size;
15481cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 6);
15491cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, sock);
15501cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, ri_data_ptr);
15511cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, ri_data_len);
15521cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, ri_flags);
15531cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, ro_datalen_ptr);
15541cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[5], Uint32, ro_flags_ptr);
15551cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
15561cb0ef41Sopenharmony_ci  Debug(wasi,
15571cb0ef41Sopenharmony_ci        "sock_recv(%d, %d, %d, %d, %d, %d)\n",
15581cb0ef41Sopenharmony_ci        sock,
15591cb0ef41Sopenharmony_ci        ri_data_ptr,
15601cb0ef41Sopenharmony_ci        ri_data_len,
15611cb0ef41Sopenharmony_ci        ri_flags,
15621cb0ef41Sopenharmony_ci        ro_datalen_ptr,
15631cb0ef41Sopenharmony_ci        ro_flags_ptr);
15641cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
15651cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
15661cb0ef41Sopenharmony_ci                         mem_size,
15671cb0ef41Sopenharmony_ci                         ri_data_ptr,
15681cb0ef41Sopenharmony_ci                         ri_data_len * UVWASI_SERDES_SIZE_iovec_t);
15691cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, ro_datalen_ptr, 4);
15701cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args, mem_size, ro_flags_ptr, 4);
15711cb0ef41Sopenharmony_ci  std::vector<uvwasi_iovec_t> ri_data(ri_data_len);
15721cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_serdes_readv_iovec_t(memory,
15731cb0ef41Sopenharmony_ci                                                   mem_size,
15741cb0ef41Sopenharmony_ci                                                   ri_data_ptr,
15751cb0ef41Sopenharmony_ci                                                   ri_data.data(),
15761cb0ef41Sopenharmony_ci                                                   ri_data_len);
15771cb0ef41Sopenharmony_ci  if (err != UVWASI_ESUCCESS) {
15781cb0ef41Sopenharmony_ci    args.GetReturnValue().Set(err);
15791cb0ef41Sopenharmony_ci    return;
15801cb0ef41Sopenharmony_ci  }
15811cb0ef41Sopenharmony_ci
15821cb0ef41Sopenharmony_ci  uvwasi_size_t ro_datalen;
15831cb0ef41Sopenharmony_ci  uvwasi_roflags_t ro_flags;
15841cb0ef41Sopenharmony_ci  err = uvwasi_sock_recv(&wasi->uvw_,
15851cb0ef41Sopenharmony_ci                         sock,
15861cb0ef41Sopenharmony_ci                         ri_data.data(),
15871cb0ef41Sopenharmony_ci                         ri_data_len,
15881cb0ef41Sopenharmony_ci                         ri_flags,
15891cb0ef41Sopenharmony_ci                         &ro_datalen,
15901cb0ef41Sopenharmony_ci                         &ro_flags);
15911cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS) {
15921cb0ef41Sopenharmony_ci    uvwasi_serdes_write_size_t(memory, ro_datalen_ptr, ro_datalen);
15931cb0ef41Sopenharmony_ci    uvwasi_serdes_write_roflags_t(memory, ro_flags_ptr, ro_flags);
15941cb0ef41Sopenharmony_ci  }
15951cb0ef41Sopenharmony_ci
15961cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
15971cb0ef41Sopenharmony_ci}
15981cb0ef41Sopenharmony_ci
15991cb0ef41Sopenharmony_ci
16001cb0ef41Sopenharmony_civoid WASI::SockSend(const FunctionCallbackInfo<Value>& args) {
16011cb0ef41Sopenharmony_ci  WASI* wasi;
16021cb0ef41Sopenharmony_ci  uint32_t sock;
16031cb0ef41Sopenharmony_ci  uint32_t si_data_ptr;
16041cb0ef41Sopenharmony_ci  uint32_t si_data_len;
16051cb0ef41Sopenharmony_ci  uint16_t si_flags;
16061cb0ef41Sopenharmony_ci  uint32_t so_datalen_ptr;
16071cb0ef41Sopenharmony_ci  char* memory;
16081cb0ef41Sopenharmony_ci  size_t mem_size;
16091cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 5);
16101cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, sock);
16111cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, si_data_ptr);
16121cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[2], Uint32, si_data_len);
16131cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[3], Uint32, si_flags);
16141cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[4], Uint32, so_datalen_ptr);
16151cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
16161cb0ef41Sopenharmony_ci  Debug(wasi,
16171cb0ef41Sopenharmony_ci        "sock_send(%d, %d, %d, %d, %d)\n",
16181cb0ef41Sopenharmony_ci        sock,
16191cb0ef41Sopenharmony_ci        si_data_ptr,
16201cb0ef41Sopenharmony_ci        si_data_len,
16211cb0ef41Sopenharmony_ci        si_flags,
16221cb0ef41Sopenharmony_ci        so_datalen_ptr);
16231cb0ef41Sopenharmony_ci  GET_BACKING_STORE_OR_RETURN(wasi, args, &memory, &mem_size);
16241cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
16251cb0ef41Sopenharmony_ci                         mem_size,
16261cb0ef41Sopenharmony_ci                         si_data_ptr,
16271cb0ef41Sopenharmony_ci                         si_data_len * UVWASI_SERDES_SIZE_ciovec_t);
16281cb0ef41Sopenharmony_ci  CHECK_BOUNDS_OR_RETURN(args,
16291cb0ef41Sopenharmony_ci                         mem_size,
16301cb0ef41Sopenharmony_ci                         so_datalen_ptr,
16311cb0ef41Sopenharmony_ci                         UVWASI_SERDES_SIZE_size_t);
16321cb0ef41Sopenharmony_ci  std::vector<uvwasi_ciovec_t> si_data(si_data_len);
16331cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_serdes_readv_ciovec_t(memory,
16341cb0ef41Sopenharmony_ci                                                    mem_size,
16351cb0ef41Sopenharmony_ci                                                    si_data_ptr,
16361cb0ef41Sopenharmony_ci                                                    si_data.data(),
16371cb0ef41Sopenharmony_ci                                                    si_data_len);
16381cb0ef41Sopenharmony_ci  if (err != UVWASI_ESUCCESS) {
16391cb0ef41Sopenharmony_ci    args.GetReturnValue().Set(err);
16401cb0ef41Sopenharmony_ci    return;
16411cb0ef41Sopenharmony_ci  }
16421cb0ef41Sopenharmony_ci
16431cb0ef41Sopenharmony_ci  uvwasi_size_t so_datalen;
16441cb0ef41Sopenharmony_ci  err = uvwasi_sock_send(&wasi->uvw_,
16451cb0ef41Sopenharmony_ci                         sock,
16461cb0ef41Sopenharmony_ci                         si_data.data(),
16471cb0ef41Sopenharmony_ci                         si_data_len,
16481cb0ef41Sopenharmony_ci                         si_flags,
16491cb0ef41Sopenharmony_ci                         &so_datalen);
16501cb0ef41Sopenharmony_ci  if (err == UVWASI_ESUCCESS)
16511cb0ef41Sopenharmony_ci    uvwasi_serdes_write_size_t(memory, so_datalen_ptr, so_datalen);
16521cb0ef41Sopenharmony_ci
16531cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
16541cb0ef41Sopenharmony_ci}
16551cb0ef41Sopenharmony_ci
16561cb0ef41Sopenharmony_ci
16571cb0ef41Sopenharmony_civoid WASI::SockShutdown(const FunctionCallbackInfo<Value>& args) {
16581cb0ef41Sopenharmony_ci  WASI* wasi;
16591cb0ef41Sopenharmony_ci  uint32_t sock;
16601cb0ef41Sopenharmony_ci  uint8_t how;
16611cb0ef41Sopenharmony_ci  RETURN_IF_BAD_ARG_COUNT(args, 2);
16621cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[0], Uint32, sock);
16631cb0ef41Sopenharmony_ci  CHECK_TO_TYPE_OR_RETURN(args, args[1], Uint32, how);
16641cb0ef41Sopenharmony_ci  ASSIGN_INITIALIZED_OR_RETURN_UNWRAP(&wasi, args.This());
16651cb0ef41Sopenharmony_ci  Debug(wasi, "sock_shutdown(%d, %d)\n", sock, how);
16661cb0ef41Sopenharmony_ci  uvwasi_errno_t err = uvwasi_sock_shutdown(&wasi->uvw_, sock, how);
16671cb0ef41Sopenharmony_ci  args.GetReturnValue().Set(err);
16681cb0ef41Sopenharmony_ci}
16691cb0ef41Sopenharmony_ci
16701cb0ef41Sopenharmony_ci
16711cb0ef41Sopenharmony_civoid WASI::_SetMemory(const FunctionCallbackInfo<Value>& args) {
16721cb0ef41Sopenharmony_ci  WASI* wasi;
16731cb0ef41Sopenharmony_ci  ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
16741cb0ef41Sopenharmony_ci  CHECK_EQ(args.Length(), 1);
16751cb0ef41Sopenharmony_ci  if (!args[0]->IsWasmMemoryObject()) {
16761cb0ef41Sopenharmony_ci    return node::THROW_ERR_INVALID_ARG_TYPE(
16771cb0ef41Sopenharmony_ci        wasi->env(),
16781cb0ef41Sopenharmony_ci        "\"instance.exports.memory\" property must be a WebAssembly.Memory "
16791cb0ef41Sopenharmony_ci        "object");
16801cb0ef41Sopenharmony_ci  }
16811cb0ef41Sopenharmony_ci  wasi->memory_.Reset(wasi->env()->isolate(), args[0].As<WasmMemoryObject>());
16821cb0ef41Sopenharmony_ci}
16831cb0ef41Sopenharmony_ci
16841cb0ef41Sopenharmony_ci
16851cb0ef41Sopenharmony_ciuvwasi_errno_t WASI::backingStore(char** store, size_t* byte_length) {
16861cb0ef41Sopenharmony_ci  Local<WasmMemoryObject> memory = PersistentToLocal::Strong(this->memory_);
16871cb0ef41Sopenharmony_ci  Local<v8::ArrayBuffer> ab = memory->Buffer();
16881cb0ef41Sopenharmony_ci  *byte_length = ab->ByteLength();
16891cb0ef41Sopenharmony_ci  *store = static_cast<char*>(ab->Data());
16901cb0ef41Sopenharmony_ci  CHECK_NOT_NULL(*store);
16911cb0ef41Sopenharmony_ci  return UVWASI_ESUCCESS;
16921cb0ef41Sopenharmony_ci}
16931cb0ef41Sopenharmony_ci
16941cb0ef41Sopenharmony_ci
16951cb0ef41Sopenharmony_cistatic void Initialize(Local<Object> target,
16961cb0ef41Sopenharmony_ci                       Local<Value> unused,
16971cb0ef41Sopenharmony_ci                       Local<Context> context,
16981cb0ef41Sopenharmony_ci                       void* priv) {
16991cb0ef41Sopenharmony_ci  Environment* env = Environment::GetCurrent(context);
17001cb0ef41Sopenharmony_ci  Isolate* isolate = env->isolate();
17011cb0ef41Sopenharmony_ci
17021cb0ef41Sopenharmony_ci  Local<FunctionTemplate> tmpl = NewFunctionTemplate(isolate, WASI::New);
17031cb0ef41Sopenharmony_ci  tmpl->InstanceTemplate()->SetInternalFieldCount(WASI::kInternalFieldCount);
17041cb0ef41Sopenharmony_ci  tmpl->Inherit(BaseObject::GetConstructorTemplate(env));
17051cb0ef41Sopenharmony_ci
17061cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "args_get", WASI::ArgsGet);
17071cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "args_sizes_get", WASI::ArgsSizesGet);
17081cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "clock_res_get", WASI::ClockResGet);
17091cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "clock_time_get", WASI::ClockTimeGet);
17101cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "environ_get", WASI::EnvironGet);
17111cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "environ_sizes_get", WASI::EnvironSizesGet);
17121cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "fd_advise", WASI::FdAdvise);
17131cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "fd_allocate", WASI::FdAllocate);
17141cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "fd_close", WASI::FdClose);
17151cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "fd_datasync", WASI::FdDatasync);
17161cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "fd_fdstat_get", WASI::FdFdstatGet);
17171cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "fd_fdstat_set_flags", WASI::FdFdstatSetFlags);
17181cb0ef41Sopenharmony_ci  SetProtoMethod(
17191cb0ef41Sopenharmony_ci      isolate, tmpl, "fd_fdstat_set_rights", WASI::FdFdstatSetRights);
17201cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "fd_filestat_get", WASI::FdFilestatGet);
17211cb0ef41Sopenharmony_ci  SetProtoMethod(
17221cb0ef41Sopenharmony_ci      isolate, tmpl, "fd_filestat_set_size", WASI::FdFilestatSetSize);
17231cb0ef41Sopenharmony_ci  SetProtoMethod(
17241cb0ef41Sopenharmony_ci      isolate, tmpl, "fd_filestat_set_times", WASI::FdFilestatSetTimes);
17251cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "fd_pread", WASI::FdPread);
17261cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "fd_prestat_get", WASI::FdPrestatGet);
17271cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "fd_prestat_dir_name", WASI::FdPrestatDirName);
17281cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "fd_pwrite", WASI::FdPwrite);
17291cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "fd_read", WASI::FdRead);
17301cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "fd_readdir", WASI::FdReaddir);
17311cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "fd_renumber", WASI::FdRenumber);
17321cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "fd_seek", WASI::FdSeek);
17331cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "fd_sync", WASI::FdSync);
17341cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "fd_tell", WASI::FdTell);
17351cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "fd_write", WASI::FdWrite);
17361cb0ef41Sopenharmony_ci  SetProtoMethod(
17371cb0ef41Sopenharmony_ci      isolate, tmpl, "path_create_directory", WASI::PathCreateDirectory);
17381cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "path_filestat_get", WASI::PathFilestatGet);
17391cb0ef41Sopenharmony_ci  SetProtoMethod(
17401cb0ef41Sopenharmony_ci      isolate, tmpl, "path_filestat_set_times", WASI::PathFilestatSetTimes);
17411cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "path_link", WASI::PathLink);
17421cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "path_open", WASI::PathOpen);
17431cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "path_readlink", WASI::PathReadlink);
17441cb0ef41Sopenharmony_ci  SetProtoMethod(
17451cb0ef41Sopenharmony_ci      isolate, tmpl, "path_remove_directory", WASI::PathRemoveDirectory);
17461cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "path_rename", WASI::PathRename);
17471cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "path_symlink", WASI::PathSymlink);
17481cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "path_unlink_file", WASI::PathUnlinkFile);
17491cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "poll_oneoff", WASI::PollOneoff);
17501cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "proc_exit", WASI::ProcExit);
17511cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "proc_raise", WASI::ProcRaise);
17521cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "random_get", WASI::RandomGet);
17531cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "sched_yield", WASI::SchedYield);
17541cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "sock_accept", WASI::SockAccept);
17551cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "sock_recv", WASI::SockRecv);
17561cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "sock_send", WASI::SockSend);
17571cb0ef41Sopenharmony_ci  SetProtoMethod(isolate, tmpl, "sock_shutdown", WASI::SockShutdown);
17581cb0ef41Sopenharmony_ci
17591cb0ef41Sopenharmony_ci  SetInstanceMethod(isolate, tmpl, "_setMemory", WASI::_SetMemory);
17601cb0ef41Sopenharmony_ci
17611cb0ef41Sopenharmony_ci  SetConstructorFunction(context, target, "WASI", tmpl);
17621cb0ef41Sopenharmony_ci}
17631cb0ef41Sopenharmony_ci
17641cb0ef41Sopenharmony_ci
17651cb0ef41Sopenharmony_ci}  // namespace wasi
17661cb0ef41Sopenharmony_ci}  // namespace node
17671cb0ef41Sopenharmony_ci
17681cb0ef41Sopenharmony_ciNODE_BINDING_CONTEXT_AWARE_INTERNAL(wasi, node::wasi::Initialize)
1769