示例#1
0
napi_value New(napi_env env, napi_callback_info info) {
  size_t argc = 1;
  napi_value args[1];
  NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

  NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments");

  napi_valuetype valuetype0;
  NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0));

  NAPI_ASSERT(env, valuetype0 == napi_object,
    "Wrong type of arguments. Expects an array as first argument.");

  napi_value ret;
  NAPI_CALL(env, napi_create_array(env, &ret));

  uint32_t i, length;
  NAPI_CALL(env, napi_get_array_length(env, args[0], &length));

  for (i = 0; i < length; i++) {
    napi_value e;
    NAPI_CALL(env, napi_get_element(env, args[0], i, &e));
    NAPI_CALL(env, napi_set_element(env, ret, i, e));
  }

  return ret;
}
示例#2
0
文件: test_array.c 项目: watilde/node
void New(napi_env env, napi_callback_info info) {
  napi_status status;

  size_t argc;
  status = napi_get_cb_args_length(env, info, &argc);
  if (status != napi_ok) return;

  if (argc < 1) {
    napi_throw_type_error(env, "Wrong number of arguments");
    return;
  }

  napi_value args[1];
  status = napi_get_cb_args(env, info, args, 1);
  if (status != napi_ok) return;

  napi_valuetype valuetype;
  status = napi_typeof(env, args[0], &valuetype);
  if (status != napi_ok) return;

  if (valuetype != napi_object) {
    napi_throw_type_error(
        env, "Wrong type of argments. Expects an array as first argument.");
    return;
  }

  napi_value ret;
  status = napi_create_array(env, &ret);
  if (status != napi_ok) return;

  uint32_t i, length;
  status = napi_get_array_length(env, args[0], &length);
  if (status != napi_ok) return;

  for (i = 0; i < length; i++) {
    napi_value e;
    status = napi_get_element(env, args[0], i, &e);
    if (status != napi_ok) return;

    status = napi_set_element(env, ret, i, e);
    if (status != napi_ok) return;
  }

  status = napi_set_return_value(env, info, ret);
  if (status != napi_ok) return;
}