Ejemplo n.º 1
0
iotjs_bufferwrap_t* iotjs_bufferwrap_from_jbuiltin(
    const iotjs_jval_t* jbuiltin) {
  IOTJS_ASSERT(iotjs_jval_is_object(jbuiltin));
  iotjs_bufferwrap_t* buffer =
      (iotjs_bufferwrap_t*)iotjs_jval_get_object_native_handle(jbuiltin);
  IOTJS_ASSERT(buffer != NULL);
  return buffer;
}
Ejemplo n.º 2
0
iotjs_bufferwrap_t* iotjs_bufferwrap_from_jbuffer(const iotjs_jval_t* jbuffer) {
  IOTJS_ASSERT(iotjs_jval_is_object(jbuffer));
  iotjs_jval_t jbuiltin =
      iotjs_jval_get_property(jbuffer, IOTJS_MAGIC_STRING__BUILTIN);
  iotjs_bufferwrap_t* buffer = iotjs_bufferwrap_from_jbuiltin(&jbuiltin);
  iotjs_jval_destroy(&jbuiltin);
  return buffer;
}
Ejemplo n.º 3
0
uintptr_t iotjs_jval_get_object_native_handle(const iotjs_jval_t* jobj) {
  const IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jval_t, jobj);
  IOTJS_ASSERT(iotjs_jval_is_object(jobj));

  uintptr_t ptr;
  jerry_get_object_native_handle(_this->value, &ptr);
  return ptr;
}
Ejemplo n.º 4
0
void iotjs_jval_set_object_native_handle(const iotjs_jval_t* jobj,
                                         uintptr_t ptr,
                                         JFreeHandlerType free_handler) {
  const IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jval_t, jobj);
  IOTJS_ASSERT(iotjs_jval_is_object(jobj));

  jerry_set_object_native_handle(_this->value, ptr, free_handler);
}
Ejemplo n.º 5
0
void iotjs_jval_set_method(const iotjs_jval_t* jobj, const char* name,
                           JHandlerType handler) {
  const IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jval_t, jobj);
  IOTJS_ASSERT(iotjs_jval_is_object(jobj));

  iotjs_jval_t jfunc = iotjs_jval_create_function(handler);
  iotjs_jval_set_property_jval(jobj, name, &jfunc);
  iotjs_jval_destroy(&jfunc);
}
Ejemplo n.º 6
0
void iotjs_jval_set_property_by_index(const iotjs_jval_t* jarr, uint32_t idx,
                                      const iotjs_jval_t* jval) {
  const IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jval_t, jarr);
  IOTJS_ASSERT(iotjs_jval_is_object(jarr));

  jerry_value_t value = iotjs_jval_as_raw(jval);
  jerry_value_t ret_val = jerry_set_property_by_index(_this->value, idx, value);
  IOTJS_ASSERT(!jerry_value_has_error_flag(ret_val));
  jerry_release_value(ret_val);
}
Ejemplo n.º 7
0
void iotjs_jval_set_property_jval(const iotjs_jval_t* jobj, const char* name,
                                  const iotjs_jval_t* val) {
  const IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jval_t, jobj);
  IOTJS_ASSERT(iotjs_jval_is_object(jobj));

  jerry_value_t prop_name = jerry_create_string((const jerry_char_t*)(name));
  jerry_value_t value = iotjs_jval_as_raw(val);
  jerry_value_t ret_val = jerry_set_property(_this->value, prop_name, value);
  jerry_release_value(prop_name);

  IOTJS_ASSERT(!jerry_value_has_error_flag(ret_val));
  jerry_release_value(ret_val);
}
Ejemplo n.º 8
0
iotjs_jval_t iotjs_jval_get_property_by_index(const iotjs_jval_t* jarr,
                                              uint32_t idx) {
  const IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jval_t, jarr);
  IOTJS_ASSERT(iotjs_jval_is_object(jarr));

  jerry_value_t res = jerry_get_property_by_index(_this->value, idx);

  if (jerry_value_has_error_flag(res)) {
    jerry_release_value(res);
    return iotjs_jval_create_copied(iotjs_jval_get_undefined());
  }

  return iotjs_jval_create_raw(res);
}
Ejemplo n.º 9
0
void iotjs_binding_initialize() {
  jundefined = iotjs_jval_create_raw(jerry_create_undefined());
  jnull = iotjs_jval_create_raw(jerry_create_null());
  jtrue = iotjs_jval_create_raw(jerry_create_boolean(true));
  jfalse = iotjs_jval_create_raw(jerry_create_boolean(false));
  jglobal = iotjs_jval_create_raw(jerry_get_global_object());

  IOTJS_ASSERT(iotjs_jval_is_object(&jglobal));

  iotjs_jargs_initialize_empty(&jargs_empty);

#ifdef NDEBUG
  assert(sizeof(iotjs_jval_t) == sizeof(jerry_value_t));
#endif
}
Ejemplo n.º 10
0
iotjs_jval_t iotjs_jval_get_property(const iotjs_jval_t* jobj,
                                     const char* name) {
  const IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jval_t, jobj);
  IOTJS_ASSERT(iotjs_jval_is_object(jobj));

  jerry_value_t prop_name = jerry_create_string((const jerry_char_t*)(name));
  jerry_value_t res = jerry_get_property(_this->value, prop_name);
  jerry_release_value(prop_name);

  if (jerry_value_has_error_flag(res)) {
    jerry_release_value(res);
    return iotjs_jval_create_copied(iotjs_jval_get_undefined());
  }

  return iotjs_jval_create_raw(res);
}
Ejemplo n.º 11
0
iotjs_jval_t iotjs_bufferwrap_create_buffer(size_t len) {
  iotjs_jval_t* jglobal = iotjs_jval_get_global_object();

  iotjs_jval_t jbuffer =
      iotjs_jval_get_property(jglobal, IOTJS_MAGIC_STRING_BUFFER);
  IOTJS_ASSERT(iotjs_jval_is_function(&jbuffer));

  iotjs_jargs_t jargs = iotjs_jargs_create(1);
  iotjs_jargs_append_number(&jargs, len);

  iotjs_jval_t jres =
      iotjs_jhelper_call_ok(&jbuffer, iotjs_jval_get_undefined(), &jargs);
  IOTJS_ASSERT(iotjs_jval_is_object(&jres));

  iotjs_jargs_destroy(&jargs);
  iotjs_jval_destroy(&jbuffer);

  return jres;
}
Ejemplo n.º 12
0
iotjs_jval_t iotjs_jhelper_call(const iotjs_jval_t* jfunc,
                                const iotjs_jval_t* jthis,
                                const iotjs_jargs_t* jargs, bool* throws) {
  IOTJS_ASSERT(iotjs_jval_is_object(jfunc));

  jerry_value_t* jargv_ = NULL;
  jerry_length_t jargc_ = iotjs_jargs_length(jargs);

#ifdef NDEBUG
  jargv_ = (jerry_value_t*)jargs->unsafe.argv;
#else
  if (jargc_ > 0) {
    unsigned buffer_size = sizeof(iotjs_jval_t) * jargc_;
    jargv_ = (jerry_value_t*)iotjs_buffer_allocate(buffer_size);
    for (unsigned i = 0; i < jargc_; ++i) {
      jargv_[i] = iotjs_jval_as_raw(iotjs_jargs_get(jargs, i));
    }
  }
#endif

  jerry_value_t jfunc_ = iotjs_jval_as_raw(jfunc);
  jerry_value_t jthis_ = iotjs_jval_as_raw(jthis);
  jerry_value_t res = jerry_call_function(jfunc_, jthis_, jargv_, jargc_);

#ifndef NDEBUG
  if (jargv_) {
    iotjs_buffer_release((char*)jargv_);
  }
#endif

  *throws = jerry_value_has_error_flag(res);

  jerry_value_clear_error_flag(&res);

  return iotjs_jval_create_raw(res);
}
Ejemplo n.º 13
0
const iotjs_jval_t* iotjs_jval_as_object(const iotjs_jval_t* jval) {
  const IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jval_t, jval);
  IOTJS_ASSERT(iotjs_jval_is_object(jval));
  return jval;
}