Example #1
0
JHANDLER_FUNCTION(Stat, handler) {
  int argc = handler.GetArgLength();

  if (argc < 1) {
    JHANDLER_THROW_RETURN(handler, TypeError, "path required");
  }
  if (!handler.GetArg(0)->IsString()) {
    JHANDLER_THROW_RETURN(handler, TypeError, "path must be a string");
  }

  Environment* env = Environment::GetEnv();

  String path = handler.GetArg(0)->GetString();

  if (argc > 1 && handler.GetArg(1)->IsFunction()) {
    FS_ASYNC(env, stat, handler.GetArg(1), path.data());
  } else {
    FS_SYNC(env, stat, path.data());
    uv_stat_t* s = &(req_wrap.data()->statbuf);
    JObject ret(MakeStatObject(s));
    handler.Return(ret);
  }

  return true;
}
Example #2
0
jerry_value_t fs_do_read_or_write(const jerry_value_t jfunc,
                                  const jerry_value_t jthis,
                                  const jerry_value_t jargv[],
                                  const jerry_length_t jargc,
                                  const iotjs_fs_op_t fs_op) {
  DJS_CHECK_THIS();
  DJS_CHECK_ARGS(5, number, object, number, number, number);
  DJS_CHECK_ARG_IF_EXIST(5, function);

  const iotjs_environment_t* env = iotjs_environment_get();

  int fd = JS_GET_ARG(0, number);
  const jerry_value_t jbuffer = JS_GET_ARG(1, object);
  size_t offset = JS_GET_ARG(2, number);
  size_t length = JS_GET_ARG(3, number);
  int position = JS_GET_ARG(4, number);
  const jerry_value_t jcallback = JS_GET_ARG_IF_EXIST(5, function);

  iotjs_bufferwrap_t* buffer_wrap = iotjs_bufferwrap_from_jbuffer(jbuffer);
  char* data = buffer_wrap->buffer;
  size_t data_length = iotjs_bufferwrap_length(buffer_wrap);
  JS_CHECK(data != NULL && data_length > 0);

  if (!IsWithinBounds(offset, length, data_length)) {
    return JS_CREATE_ERROR(RANGE, "length out of bound");
  }

  uv_buf_t uvbuf = uv_buf_init(data + offset, length);

  jerry_value_t ret_value;
  if (fs_op == IOTJS_FS_READ) {
    if (!jerry_value_is_null(jcallback)) {
      FS_ASYNC(env, read, jcallback, fd, &uvbuf, 1, position);
    } else {
      FS_SYNC(env, read, fd, &uvbuf, 1, position);
    }
  } else {
    if (!jerry_value_is_null(jcallback)) {
      FS_ASYNC(env, write, jcallback, fd, &uvbuf, 1, position);
    } else {
      FS_SYNC(env, write, fd, &uvbuf, 1, position);
    }
  }
  return ret_value;
}
Example #3
0
JHANDLER_FUNCTION(Close, handler) {
  IOTJS_ASSERT(handler.GetThis()->IsObject());
  IOTJS_ASSERT(handler.GetArgLength() >= 1);
  IOTJS_ASSERT(handler.GetArg(0)->IsNumber());

  Environment* env = Environment::GetEnv();

  int fd = handler.GetArg(0)->GetInt32();

  if (handler.GetArgLength() > 1 && handler.GetArg(1)->IsFunction()) {
    FS_ASYNC(env, close, handler.GetArg(1), fd);
  } else {
    FS_SYNC(env, close, fd);
  }

  return !handler.HasThrown();
}
Example #4
0
JHANDLER_FUNCTION(Write, handler) {
  IOTJS_ASSERT(handler.GetThis()->IsObject());
  IOTJS_ASSERT(handler.GetArgLength() >= 5);
  IOTJS_ASSERT(handler.GetArg(0)->IsNumber());
  IOTJS_ASSERT(handler.GetArg(1)->IsObject());
  IOTJS_ASSERT(handler.GetArg(2)->IsNumber());
  IOTJS_ASSERT(handler.GetArg(3)->IsNumber());
  IOTJS_ASSERT(handler.GetArg(4)->IsNumber());

  Environment* env = Environment::GetEnv();

  int fd = handler.GetArg(0)->GetInt32();
  int offset = handler.GetArg(2)->GetInt32();
  int length = handler.GetArg(3)->GetInt32();
  int position = handler.GetArg(4)->GetInt32();

  JObject* jbuffer = handler.GetArg(1);
  BufferWrap* buffer_wrap = BufferWrap::FromJBuffer(*jbuffer);
  char* data = buffer_wrap->buffer();
  int data_length = buffer_wrap->length();

  if (offset >= data_length) {
    JHANDLER_THROW_RETURN(handler, RangeError, "offset out of bound");
  }
  if (offset + length > data_length) {
    JHANDLER_THROW_RETURN(handler, RangeError, "length out of bound");
  }

  uv_buf_t uvbuf = uv_buf_init(reinterpret_cast<char*>(data + offset),
                               length);

  if (handler.GetArgLength() > 5 && handler.GetArg(5)->IsFunction()) {
    FS_ASYNC(env, write, handler.GetArg(5), fd, &uvbuf, 1, position);
  } else {
    FS_SYNC(env, write, fd, &uvbuf, 1, position);
    handler.Return(JVal::Number(err));
  }

  return !handler.HasThrown();
}
Example #5
0
JHANDLER_FUNCTION(Open, handler) {
  IOTJS_ASSERT(handler.GetThis()->IsObject());
  IOTJS_ASSERT(handler.GetArgLength() >= 3);
  IOTJS_ASSERT(handler.GetArg(0)->IsString());
  IOTJS_ASSERT(handler.GetArg(1)->IsNumber());
  IOTJS_ASSERT(handler.GetArg(2)->IsNumber());

  Environment* env = Environment::GetEnv();

  String path = handler.GetArg(0)->GetString();
  int flags = handler.GetArg(1)->GetInt32();
  int mode = handler.GetArg(2)->GetInt32();

  if (handler.GetArgLength() > 3 && handler.GetArg(3)->IsFunction()) {
    FS_ASYNC(env, open, handler.GetArg(3), path.data(), flags, mode);
  } else {
    FS_SYNC(env, open, path.data(), flags, mode);
    handler.Return(JVal::Number(err));
  }

  return !handler.HasThrown();
}