Example #1
0
static bool
foreach (const jerry_value_t name, /**< field name */
         const jerry_value_t value, /**< field value */
         void *user_data) /**< user data */
{
  char str_buf_p[128];
  jerry_size_t sz = jerry_string_to_char_buffer (name, (jerry_char_t *) str_buf_p, 128);
  str_buf_p[sz] = '\0';

  JERRY_ASSERT (!strncmp ((const char *) user_data, "user_data", 9));
  JERRY_ASSERT (sz > 0);

  if (!strncmp (str_buf_p, "alpha", (size_t) sz))
  {
    JERRY_ASSERT (jerry_value_is_number (value));
    JERRY_ASSERT (jerry_get_number_value (value) == 32.0);
    return true;
  }
  else if (!strncmp (str_buf_p, "bravo", (size_t) sz))
  {
    JERRY_ASSERT (jerry_value_is_boolean (value));
    JERRY_ASSERT (jerry_get_boolean_value (value) == false);
    return true;
  }
  else if (!strncmp (str_buf_p, "charlie", (size_t) sz))
  {
    JERRY_ASSERT (jerry_value_is_object (value));
    return true;
  }
  else if (!strncmp (str_buf_p, "delta", (size_t) sz))
  {
    JERRY_ASSERT (jerry_value_is_number (value));
    JERRY_ASSERT (jerry_get_number_value (value) == 123.45);
    return true;
  }
  else if (!strncmp (str_buf_p, "echo", (size_t) sz))
  {
    JERRY_ASSERT (jerry_value_is_string (value));
    jerry_size_t echo_sz = jerry_string_to_char_buffer (value,
                                                        (jerry_char_t *) str_buf_p,
                                                        128);
    str_buf_p[echo_sz] = '\0';
    JERRY_ASSERT (!strncmp (str_buf_p, "foobar", (size_t) echo_sz));
    return true;
  }

  JERRY_ASSERT (false);
  return false;


} /* foreach */
Example #2
0
int
main (void)
{
  TEST_INIT ();

  bool is_ok;
  jerry_size_t sz;
  jerry_value_t val_t, val_foo, val_bar, val_A, val_A_prototype, val_a, val_a_foo, val_value_field, val_p, val_np;
  jerry_value_t val_call_external;
  jerry_value_t global_obj_val, obj_val;
  jerry_value_t external_func_val, external_construct_val;
  jerry_value_t throw_test_handler_val;
  jerry_value_t parsed_code_val, proto_val, prim_val;
  jerry_value_t res, args[2];
  char buffer[32];

  is_ok = test_run_simple ("print ('Hello, World!');");
  JERRY_ASSERT (is_ok);

  is_ok = test_run_simple ("throw 'Hello World';");
  JERRY_ASSERT (!is_ok);

  jerry_init (JERRY_INIT_EMPTY);

  parsed_code_val = jerry_parse ((jerry_char_t *) test_source, strlen (test_source), false);
  JERRY_ASSERT (!jerry_value_has_error_flag (parsed_code_val));

  res = jerry_run (parsed_code_val);
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  jerry_release_value (res);
  jerry_release_value (parsed_code_val);

  global_obj_val = jerry_get_global_object ();

  // Test corner case for jerry_string_to_char_buffer
  args[0] = jerry_create_string ((jerry_char_t *) "");
  sz = jerry_get_string_size (args[0]);
  JERRY_ASSERT (sz == 0);
  jerry_release_value (args[0]);

  // Get global.boo (non-existing field)
  val_t = get_property (global_obj_val, "boo");
  JERRY_ASSERT (!jerry_value_has_error_flag (val_t));
  JERRY_ASSERT (jerry_value_is_undefined (val_t));

  // Get global.t
  val_t = get_property (global_obj_val, "t");
  JERRY_ASSERT (!jerry_value_has_error_flag (val_t));
  JERRY_ASSERT (jerry_value_is_number (val_t)
                && jerry_get_number_value (val_t) == 1.0);
  jerry_release_value (val_t);

  // Get global.foo
  val_foo = get_property (global_obj_val, "foo");
  JERRY_ASSERT (!jerry_value_has_error_flag (val_foo));
  JERRY_ASSERT (jerry_value_is_object (val_foo));

  // Call foo (4, 2)
  args[0] = jerry_create_number (4);
  args[1] = jerry_create_number (2);
  res = jerry_call_function (val_foo, jerry_create_undefined (), args, 2);
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  JERRY_ASSERT (jerry_value_is_number (res)
                && jerry_get_number_value (res) == 1.0);
  jerry_release_value (res);

  // Get global.bar
  val_bar = get_property (global_obj_val, "bar");
  JERRY_ASSERT (!jerry_value_has_error_flag (val_bar));
  JERRY_ASSERT (jerry_value_is_object (val_bar));

  // Call bar (4, 2)
  res = jerry_call_function (val_bar, jerry_create_undefined (), args, 2);
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  JERRY_ASSERT (jerry_value_is_number (res)
                && jerry_get_number_value (res) == 5.0);
  jerry_release_value (res);
  jerry_release_value (val_bar);

  // Set global.t = "abcd"
  jerry_release_value (args[0]);
  args[0] = jerry_create_string ((jerry_char_t *) "abcd");
  res = set_property (global_obj_val, "t", args[0]);
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  JERRY_ASSERT (jerry_get_boolean_value (res));
  jerry_release_value (res);

  // Call foo (4, 2)
  res = jerry_call_function (val_foo, jerry_create_undefined (), args, 2);
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  JERRY_ASSERT (jerry_value_is_string (res));
  sz = jerry_get_string_size (res);
  JERRY_ASSERT (sz == 4);
  sz = jerry_string_to_char_buffer (res, (jerry_char_t *) buffer, sz);
  JERRY_ASSERT (sz == 4);
  jerry_release_value (res);
  JERRY_ASSERT (!strncmp (buffer, "abcd", (size_t) sz));
  jerry_release_value (args[0]);
  jerry_release_value (args[1]);

  // Get global.A
  val_A = get_property (global_obj_val, "A");
  JERRY_ASSERT (!jerry_value_has_error_flag (val_A));
  JERRY_ASSERT (jerry_value_is_object (val_A));

  // Get A.prototype
  is_ok = jerry_value_is_constructor (val_A);
  JERRY_ASSERT (is_ok);
  val_A_prototype = get_property (val_A, "prototype");
  JERRY_ASSERT (!jerry_value_has_error_flag (val_A_prototype));
  JERRY_ASSERT (jerry_value_is_object (val_A_prototype));
  jerry_release_value (val_A);

  // Set A.prototype.foo = global.foo
  res = set_property (val_A_prototype, "foo", val_foo);
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  JERRY_ASSERT (jerry_get_boolean_value (res));
  jerry_release_value (res);
  jerry_release_value (val_A_prototype);
  jerry_release_value (val_foo);

  // Get global.a
  val_a = get_property (global_obj_val, "a");
  JERRY_ASSERT (!jerry_value_has_error_flag (val_a));
  JERRY_ASSERT (jerry_value_is_object (val_a));

  // Get a.t
  res = get_property (val_a, "t");
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  JERRY_ASSERT (jerry_value_is_number (res)
                && jerry_get_number_value (res) == 12.0);
  jerry_release_value (res);

  // foreach properties
  val_p = get_property (global_obj_val, "p");
  is_ok = jerry_foreach_object_property (val_p, foreach, (void *) "user_data");
  JERRY_ASSERT (is_ok);

  // break foreach at third element
  int count = 0;
  is_ok = jerry_foreach_object_property (val_p, foreach_subset, &count);
  JERRY_ASSERT (is_ok);
  JERRY_ASSERT (count == 3);
  jerry_release_value (val_p);

  // foreach with throw test
  val_np = get_property (global_obj_val, "np");
  is_ok = !jerry_foreach_object_property (val_np, foreach_exception, NULL);
  JERRY_ASSERT (is_ok);
  jerry_release_value (val_np);

  // Get a.foo
  val_a_foo = get_property (val_a, "foo");
  JERRY_ASSERT (!jerry_value_has_error_flag (val_a_foo));
  JERRY_ASSERT (jerry_value_is_object (val_a_foo));

  // Call a.foo ()
  res = jerry_call_function (val_a_foo, val_a, NULL, 0);
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  JERRY_ASSERT (jerry_value_is_number (res)
                && jerry_get_number_value (res) == 12.0);
  jerry_release_value (res);
  jerry_release_value (val_a_foo);

  jerry_release_value (val_a);

  // Create native handler bound function object and set it to 'external' variable
  external_func_val = jerry_create_external_function (handler);
  JERRY_ASSERT (jerry_value_is_function (external_func_val)
                && jerry_value_is_constructor (external_func_val));

  res = set_property (global_obj_val, "external", external_func_val);
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  JERRY_ASSERT (jerry_get_boolean_value (res));
  jerry_release_value (external_func_val);

  // Call 'call_external' function that should call external function created above
  val_call_external = get_property (global_obj_val, "call_external");
  JERRY_ASSERT (!jerry_value_has_error_flag (val_call_external));
  JERRY_ASSERT (jerry_value_is_object (val_call_external));
  res = jerry_call_function (val_call_external, global_obj_val, NULL, 0);
  jerry_release_value (val_call_external);
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  JERRY_ASSERT (jerry_value_is_string (res));
  sz = jerry_get_string_size (res);
  JERRY_ASSERT (sz == 19);
  sz = jerry_string_to_char_buffer (res, (jerry_char_t *) buffer, sz);
  JERRY_ASSERT (sz == 19);
  jerry_release_value (res);
  JERRY_ASSERT (!strncmp (buffer, "string from handler", (size_t) sz));

  // Create native handler bound function object and set it to 'external_construct' variable
  external_construct_val = jerry_create_external_function (handler_construct);
  JERRY_ASSERT (jerry_value_is_function (external_construct_val)
                && jerry_value_is_constructor (external_construct_val));

  res = set_property (global_obj_val, "external_construct", external_construct_val);
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  JERRY_ASSERT (jerry_get_boolean_value (res));
  jerry_release_value (res);

  // Call external function created above, as constructor
  args[0] = jerry_create_boolean (true);
  res = jerry_construct_object (external_construct_val, args, 1);
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  JERRY_ASSERT (jerry_value_is_object (res));
  val_value_field = get_property (res, "value_field");

  // Get 'value_field' of constructed object
  JERRY_ASSERT (!jerry_value_has_error_flag (val_value_field));
  JERRY_ASSERT (jerry_value_is_boolean (val_value_field)
                && jerry_get_boolean_value (val_value_field));
  jerry_release_value (val_value_field);
  jerry_release_value (external_construct_val);

  uintptr_t ptr;
  is_ok = jerry_get_object_native_handle (res, &ptr);
  JERRY_ASSERT (is_ok
                && ptr == (uintptr_t) 0x0012345678abcdefull);

  jerry_release_value (res);

  // Test: Throwing exception from native handler.
  throw_test_handler_val = jerry_create_external_function (handler_throw_test);
  JERRY_ASSERT (jerry_value_is_function (throw_test_handler_val));

  res = set_property (global_obj_val, "throw_test", throw_test_handler_val);
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  JERRY_ASSERT (jerry_get_boolean_value (res));
  jerry_release_value (res);
  jerry_release_value (throw_test_handler_val);

  val_t = get_property (global_obj_val, "call_throw_test");
  JERRY_ASSERT (!jerry_value_has_error_flag (val_t));
  JERRY_ASSERT (jerry_value_is_object (val_t));

  res = jerry_call_function (val_t, global_obj_val, NULL, 0);
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  jerry_release_value (val_t);
  jerry_release_value (res);

  // Test: Unhandled exception in called function
  val_t = get_property (global_obj_val, "throw_reference_error");
  JERRY_ASSERT (!jerry_value_has_error_flag (val_t));
  JERRY_ASSERT (jerry_value_is_object (val_t));

  res = jerry_call_function (val_t, global_obj_val, NULL, 0);

  JERRY_ASSERT (jerry_value_has_error_flag (res));
  jerry_release_value (val_t);

  // 'res' should contain exception object
  JERRY_ASSERT (jerry_value_is_object (res));
  jerry_release_value (res);

  // Test: Call of non-function
  obj_val = jerry_create_object ();
  res = jerry_call_function (obj_val, global_obj_val, NULL, 0);
  JERRY_ASSERT (jerry_value_has_error_flag (res));

  // 'res' should contain exception object
  JERRY_ASSERT (jerry_value_is_object (res));
  jerry_release_value (res);

  jerry_release_value (obj_val);

  // Test: Unhandled exception in function called, as constructor
  val_t = get_property (global_obj_val, "throw_reference_error");
  JERRY_ASSERT (!jerry_value_has_error_flag (val_t));
  JERRY_ASSERT (jerry_value_is_object (val_t));

  res = jerry_construct_object (val_t, NULL, 0);
  JERRY_ASSERT (jerry_value_has_error_flag (res));
  jerry_release_value (val_t);

  // 'res' should contain exception object
  JERRY_ASSERT (jerry_value_is_object (res));
  jerry_release_value (res);

  // Test: Call of non-function as constructor
  obj_val = jerry_create_object ();
  res = jerry_construct_object (obj_val, NULL, 0);
  JERRY_ASSERT (jerry_value_has_error_flag (res));

  // 'res' should contain exception object
  JERRY_ASSERT (jerry_value_is_object (res));
  jerry_release_value (res);

  jerry_release_value (obj_val);

  // Test: Array Object API
  jerry_value_t array_obj_val = jerry_create_array (10);
  JERRY_ASSERT (jerry_value_is_array (array_obj_val));
  JERRY_ASSERT (jerry_get_array_length (array_obj_val) == 10);

  jerry_value_t v_in = jerry_create_number (10.5);
  jerry_set_property_by_index (array_obj_val, 5, v_in);
  jerry_value_t v_out = jerry_get_property_by_index (array_obj_val, 5);

  JERRY_ASSERT (jerry_value_is_number (v_out)
                && jerry_get_number_value (v_out) == 10.5);

  jerry_release_value (v_in);
  jerry_release_value (v_out);
  jerry_release_value (array_obj_val);

  // Test: init property descriptor
  jerry_property_descriptor_t prop_desc;
  jerry_init_property_descriptor_fields (&prop_desc);
  JERRY_ASSERT (prop_desc.is_value_defined == false);
  JERRY_ASSERT (jerry_value_is_undefined (prop_desc.value));
  JERRY_ASSERT (prop_desc.is_writable_defined == false);
  JERRY_ASSERT (prop_desc.is_writable == false);
  JERRY_ASSERT (prop_desc.is_enumerable_defined == false);
  JERRY_ASSERT (prop_desc.is_enumerable == false);
  JERRY_ASSERT (prop_desc.is_configurable_defined == false);
  JERRY_ASSERT (prop_desc.is_configurable == false);
  JERRY_ASSERT (prop_desc.is_get_defined == false);
  JERRY_ASSERT (jerry_value_is_undefined (prop_desc.getter));
  JERRY_ASSERT (prop_desc.is_set_defined == false);
  JERRY_ASSERT (jerry_value_is_undefined (prop_desc.setter));

  // Test: define own properties
  jerry_value_t prop_name = jerry_create_string ((const jerry_char_t *) "my_defined_property");
  prop_desc.is_value_defined = true;
  prop_desc.value = jerry_acquire_value (prop_name);
  res = jerry_define_own_property (global_obj_val, prop_name, &prop_desc);
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  JERRY_ASSERT (jerry_value_is_boolean (res));
  JERRY_ASSERT (jerry_get_boolean_value (res));
  jerry_release_value (res);
  jerry_free_property_descriptor_fields (&prop_desc);

  // Test: get own property descriptor
  is_ok = jerry_get_own_property_descriptor (global_obj_val, prop_name, &prop_desc);
  JERRY_ASSERT (is_ok);
  JERRY_ASSERT (prop_desc.is_value_defined == true);
  JERRY_ASSERT (jerry_value_is_string (prop_desc.value));
  JERRY_ASSERT (prop_desc.is_writable == false);
  JERRY_ASSERT (prop_desc.is_enumerable == false);
  JERRY_ASSERT (prop_desc.is_configurable == false);
  JERRY_ASSERT (prop_desc.is_get_defined == false);
  JERRY_ASSERT (jerry_value_is_undefined (prop_desc.getter));
  JERRY_ASSERT (prop_desc.is_set_defined == false);
  JERRY_ASSERT (jerry_value_is_undefined (prop_desc.setter));
  jerry_release_value (prop_name);
  jerry_free_property_descriptor_fields (&prop_desc);

  // Test: object keys
  res = jerry_get_object_keys (global_obj_val);
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  JERRY_ASSERT (jerry_value_is_array (res));
  jerry_release_value (res);

  // Test: jerry_value_to_primitive
  obj_val = jerry_eval ((jerry_char_t *) "new String ('hello')", 20, false);
  JERRY_ASSERT (!jerry_value_has_error_flag (obj_val));
  JERRY_ASSERT (jerry_value_is_object (obj_val));
  JERRY_ASSERT (!jerry_value_is_string (obj_val));
  prim_val = jerry_value_to_primitive (obj_val);
  JERRY_ASSERT (!jerry_value_has_error_flag (prim_val));
  JERRY_ASSERT (jerry_value_is_string (prim_val));
  jerry_release_value (prim_val);

  // Test: jerry_get_prototype
  proto_val = jerry_get_prototype (obj_val);
  JERRY_ASSERT (!jerry_value_has_error_flag (proto_val));
  JERRY_ASSERT (jerry_value_is_object (proto_val));
  jerry_release_value (obj_val);

  // Test: jerry_set_prototype
  obj_val = jerry_create_object ();
  res = jerry_set_prototype (obj_val, jerry_create_null ());
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  JERRY_ASSERT (jerry_value_is_boolean (res));
  JERRY_ASSERT (jerry_get_boolean_value (res));

  res = jerry_set_prototype (obj_val, jerry_create_object ());
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  JERRY_ASSERT (jerry_value_is_boolean (res));
  JERRY_ASSERT (jerry_get_boolean_value (res));
  proto_val = jerry_get_prototype (obj_val);
  JERRY_ASSERT (!jerry_value_has_error_flag (proto_val));
  JERRY_ASSERT (jerry_value_is_object (proto_val));
  jerry_release_value (proto_val);
  jerry_release_value (obj_val);

  // Test: eval
  const char *eval_code_src_p = "(function () { return 123; })";
  val_t = jerry_eval ((jerry_char_t *) eval_code_src_p, strlen (eval_code_src_p), true);
  JERRY_ASSERT (!jerry_value_has_error_flag (val_t));
  JERRY_ASSERT (jerry_value_is_object (val_t));
  JERRY_ASSERT (jerry_value_is_function (val_t));

  res = jerry_call_function (val_t, jerry_create_undefined (), NULL, 0);
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  JERRY_ASSERT (jerry_value_is_number (res)
                && jerry_get_number_value (res) == 123.0);
  jerry_release_value (res);

  jerry_release_value (val_t);

  // cleanup.
  jerry_release_value (global_obj_val);

  // TEST: run gc.
  jerry_gc ();

  jerry_cleanup ();

  JERRY_ASSERT (test_api_is_free_callback_was_called);

  // External Magic String
  jerry_init (JERRY_INIT_SHOW_OPCODES);

  uint32_t num_magic_string_items = (uint32_t) (sizeof (magic_string_items) / sizeof (jerry_char_ptr_t));
  jerry_register_magic_strings (magic_string_items,
                                num_magic_string_items,
                                magic_string_lengths);

  const char *ms_code_src_p = "var global = {}; var console = [1]; var process = 1;";
  parsed_code_val = jerry_parse ((jerry_char_t *) ms_code_src_p, strlen (ms_code_src_p), false);
  JERRY_ASSERT (!jerry_value_has_error_flag (parsed_code_val));

  res = jerry_run (parsed_code_val);
  JERRY_ASSERT (!jerry_value_has_error_flag (res));
  jerry_release_value (res);
  jerry_release_value (parsed_code_val);

  jerry_cleanup ();

  // Dump / execute snapshot
  // FIXME: support save/load snapshot for optimized parser
  if (false)
  {
    static uint8_t global_mode_snapshot_buffer[1024];
    static uint8_t eval_mode_snapshot_buffer[1024];

    const char *code_to_snapshot_p = "(function () { return 'string from snapshot'; }) ();";

    jerry_init (JERRY_INIT_SHOW_OPCODES);
    size_t global_mode_snapshot_size = jerry_parse_and_save_snapshot ((jerry_char_t *) code_to_snapshot_p,
                                                                      strlen (code_to_snapshot_p),
                                                                      true,
                                                                      false,
                                                                      global_mode_snapshot_buffer,
                                                                      sizeof (global_mode_snapshot_buffer));
    JERRY_ASSERT (global_mode_snapshot_size != 0);
    jerry_cleanup ();

    jerry_init (JERRY_INIT_SHOW_OPCODES);
    size_t eval_mode_snapshot_size = jerry_parse_and_save_snapshot ((jerry_char_t *) code_to_snapshot_p,
                                                                    strlen (code_to_snapshot_p),
                                                                    false,
                                                                    false,
                                                                    eval_mode_snapshot_buffer,
                                                                    sizeof (eval_mode_snapshot_buffer));
    JERRY_ASSERT (eval_mode_snapshot_size != 0);
    jerry_cleanup ();

    jerry_init (JERRY_INIT_SHOW_OPCODES);

    res = jerry_exec_snapshot (global_mode_snapshot_buffer,
                               global_mode_snapshot_size,
                               false);

    JERRY_ASSERT (!jerry_value_has_error_flag (res));
    JERRY_ASSERT (jerry_value_is_undefined (res));
    jerry_release_value (res);

    res = jerry_exec_snapshot (eval_mode_snapshot_buffer,
                               eval_mode_snapshot_size,
                               false);

    JERRY_ASSERT (!jerry_value_has_error_flag (res));
    JERRY_ASSERT (jerry_value_is_string (res));
    sz = jerry_get_string_size (res);
    JERRY_ASSERT (sz == 20);
    sz = jerry_string_to_char_buffer (res, (jerry_char_t *) buffer, sz);
    JERRY_ASSERT (sz == 20);
    jerry_release_value (res);
    JERRY_ASSERT (!strncmp (buffer, "string from snapshot", (size_t) sz));

    jerry_cleanup ();
  }

  return 0;
} /* main */
Example #3
0
static jerry_value_t gpio_set_configuration(iotjs_gpio_t* gpio,
                                            jerry_value_t jconfigurable) {
  jerry_value_t jpin =
      iotjs_jval_get_property(jconfigurable, IOTJS_MAGIC_STRING_PIN);
  gpio->pin = iotjs_jval_as_number(jpin);
  jerry_release_value(jpin);

  // Direction
  jerry_value_t jdirection =
      iotjs_jval_get_property(jconfigurable, IOTJS_MAGIC_STRING_DIRECTION);

  if (jerry_value_is_undefined(jdirection)) {
    gpio->direction = kGpioDirectionOut;
  } else {
    if (jerry_value_is_number(jdirection)) {
      gpio->direction = (GpioDirection)iotjs_jval_as_number(jdirection);
    } else {
      gpio->direction = __kGpioDirectionMax;
    }
    if (gpio->direction >= __kGpioDirectionMax) {
      return JS_CREATE_ERROR(
          TYPE, "Bad arguments - gpio.direction should be DIRECTION.IN or OUT");
    }
  }
  jerry_release_value(jdirection);

  // Mode
  jerry_value_t jmode =
      iotjs_jval_get_property(jconfigurable, IOTJS_MAGIC_STRING_MODE);

  if (jerry_value_is_undefined(jmode)) {
    gpio->mode = kGpioModeNone;
  } else {
    if (jerry_value_is_number(jmode)) {
      gpio->mode = (GpioMode)iotjs_jval_as_number(jmode);
    } else {
      gpio->mode = __kGpioModeMax;
    }
    if (gpio->mode >= __kGpioModeMax) {
      return JS_CREATE_ERROR(TYPE,
                             "Bad arguments - gpio.mode should be MODE.NONE, "
                             "PULLUP, PULLDOWN, FLOAT, PUSHPULL or OPENDRAIN");

    } else if (gpio->direction == kGpioDirectionIn &&
               gpio->mode != kGpioModeNone && gpio->mode != kGpioModePullup &&
               gpio->mode != kGpioModePulldown) {
      return JS_CREATE_ERROR(TYPE,
                             "Bad arguments - DIRECTION.IN only supports "
                             "MODE.NONE, PULLUP and PULLDOWN");

    } else if (gpio->direction == kGpioDirectionOut &&
               gpio->mode != kGpioModeNone && gpio->mode != kGpioModeFloat &&
               gpio->mode != kGpioModePushpull &&
               gpio->mode != kGpioModeOpendrain) {
      return JS_CREATE_ERROR(TYPE,
                             "Bad arguments - DIRECTION.OUT only supports "
                             "MODE.NONE, FLOAT, PUSHPULL and OPENDRAIN");
    }
  }
  jerry_release_value(jmode);

  // Edge
  jerry_value_t jedge =
      iotjs_jval_get_property(jconfigurable, IOTJS_MAGIC_STRING_EDGE);

  if (jerry_value_is_undefined(jedge)) {
    gpio->edge = kGpioEdgeNone;
  } else {
    if (jerry_value_is_number(jedge)) {
      gpio->edge = (GpioEdge)iotjs_jval_as_number(jedge);
    } else {
      gpio->edge = __kGpioEdgeMax;
    }
    if (gpio->edge >= __kGpioEdgeMax) {
      return JS_CREATE_ERROR(TYPE,
                             "Bad arguments - gpio.edge should be EDGE.NONE, "
                             "RISING, FALLING or BOTH");
    }
  }
  jerry_release_value(jedge);

  return jerry_create_undefined();
}
int
main (void)
{
  jerry_init (JERRY_INIT_EMPTY);

  if (!jerry_is_feature_enabled (JERRY_FEATURE_TYPEDARRAY))
  {
    jerry_port_log (JERRY_LOG_LEVEL_ERROR, "ArrayBuffer is disabled!\n");
    jerry_cleanup ();
    return 0;
  }

  jerry_value_t function_val = jerry_create_external_function (assert_handler);
  register_js_value ("assert", function_val);
  jerry_release_value (function_val);

  /* Test array buffer queries */
  {
    const char *eval_arraybuffer_src_p = "new ArrayBuffer (10)";
    jerry_value_t eval_arraybuffer = jerry_eval ((jerry_char_t *) eval_arraybuffer_src_p,
                                                 strlen (eval_arraybuffer_src_p),
                                                 true);
    TEST_ASSERT (!jerry_value_is_error (eval_arraybuffer));
    TEST_ASSERT (jerry_value_is_arraybuffer (eval_arraybuffer));
    TEST_ASSERT (jerry_get_arraybuffer_byte_length (eval_arraybuffer) == 10);
    jerry_release_value (eval_arraybuffer);
  }

  /* Test array buffer creation */
  {
    const uint32_t length = 15;
    jerry_value_t arraybuffer = jerry_create_arraybuffer (length);
    TEST_ASSERT (!jerry_value_is_error (arraybuffer));
    TEST_ASSERT (jerry_value_is_arraybuffer (arraybuffer));
    TEST_ASSERT (jerry_get_arraybuffer_byte_length (arraybuffer) == length);
    jerry_release_value (arraybuffer);
  }

  /* Test array buffer read operations */
  for (uint8_t i = 0; i < 15; i++)
  {
    test_read_with_offset (i);
  }

  /* Test zero length ArrayBuffer read */
  {
    const uint32_t length = 0;
    jerry_value_t arraybuffer = jerry_create_arraybuffer (length);
    TEST_ASSERT (!jerry_value_is_error (arraybuffer));
    TEST_ASSERT (jerry_value_is_arraybuffer (arraybuffer));
    TEST_ASSERT (jerry_get_arraybuffer_byte_length (arraybuffer) == length);

    uint8_t data[20];
    memset (data, 11, 20);

    jerry_length_t bytes_read = jerry_arraybuffer_read (arraybuffer, 0, data, 20);
    TEST_ASSERT (bytes_read == 0);

    for (int i = 0; i < 20; i++)
    {
      TEST_ASSERT (data[i] == 11);
    }

    jerry_release_value (arraybuffer);
  }

  /* Test array buffer write operations */
  for (uint8_t i = 0; i < 15; i++)
  {
    test_write_with_offset (i);
  }

  /* Test zero length ArrayBuffer write */
  {
    const uint32_t length = 0;
    jerry_value_t arraybuffer = jerry_create_arraybuffer (length);
    TEST_ASSERT (!jerry_value_is_error (arraybuffer));
    TEST_ASSERT (jerry_value_is_arraybuffer (arraybuffer));
    TEST_ASSERT (jerry_get_arraybuffer_byte_length (arraybuffer) == length);

    uint8_t data[20];
    memset (data, 11, 20);

    jerry_length_t bytes_written = jerry_arraybuffer_write (arraybuffer, 0, data, 20);
    TEST_ASSERT (bytes_written == 0);

    jerry_release_value (arraybuffer);
  }

  /* Test ArrayBuffer with buffer allocated externally */
  {
    const uint32_t buffer_size = 15;
    const uint8_t base_value = 51;

    uint8_t buffer_p[buffer_size];
    memset (buffer_p, base_value, buffer_size);

    jerry_value_t arrayb = jerry_create_arraybuffer_external (buffer_size, buffer_p, test_free_cb);
    uint8_t new_value = 123;
    jerry_length_t copied = jerry_arraybuffer_write (arrayb, 0, &new_value, 1);
    TEST_ASSERT (copied == 1);
    TEST_ASSERT (buffer_p[0] == new_value);
    TEST_ASSERT (jerry_get_arraybuffer_byte_length (arrayb) == buffer_size);

    for (uint32_t i = 1; i < buffer_size; i++)
    {
      TEST_ASSERT (buffer_p[i] == base_value);
    }

    uint8_t test_buffer[buffer_size];
    jerry_length_t read = jerry_arraybuffer_read (arrayb, 0, test_buffer, buffer_size);
    TEST_ASSERT (read == buffer_size);
    TEST_ASSERT (test_buffer[0] == new_value);

    for (uint32_t i = 1; i < buffer_size; i++)
    {
      TEST_ASSERT (test_buffer[i] == base_value);
    }

    TEST_ASSERT (jerry_value_is_arraybuffer (arrayb));
    jerry_release_value (arrayb);
  }

  /* Test ArrayBuffer external memory map/unmap */
  {
    const uint32_t buffer_size = 20;
    uint8_t buffer_p[buffer_size];
    {
      jerry_value_t input_buffer = jerry_create_arraybuffer_external (buffer_size, buffer_p, NULL);
      register_js_value ("input_buffer", input_buffer);
      jerry_release_value (input_buffer);
    }

    const char *eval_arraybuffer_src_p = (
      "var array = new Uint8Array(input_buffer);"
      "for (var i = 0; i < array.length; i++)"
      "{"
      "  array[i] = i * 2;"
      "};"
      "array.buffer");
    jerry_value_t buffer = jerry_eval ((jerry_char_t *) eval_arraybuffer_src_p,
                                        strlen (eval_arraybuffer_src_p),
                                        true);

    TEST_ASSERT (!jerry_value_is_error (buffer));
    TEST_ASSERT (jerry_value_is_arraybuffer (buffer));
    TEST_ASSERT (jerry_get_arraybuffer_byte_length (buffer) == 20);

    uint8_t *const data = jerry_get_arraybuffer_pointer (buffer);

    /* test memory read */
    for (int i = 0; i < 20; i++)
    {
      TEST_ASSERT (data[i] == (uint8_t) (i * 2));
    }

    /* "upload" new data */
    double sum = 0;
    for (int i = 0; i < 20; i++)
    {
      data[i] = (uint8_t)(i * 3);
      sum += data[i];
    }

    jerry_release_value (buffer);

    const char *eval_test_arraybuffer_p = (
      "var sum = 0;"
      "for (var i = 0; i < array.length; i++)"
      "{"
      "  var expected = i * 3;"
      "  assert(array[i] == expected, 'Array at index ' + i + ' was: ' + array[i] + ' should be: ' + expected);"
      "  sum += array[i]"
      "};"
      "sum");
    jerry_value_t res = jerry_eval ((jerry_char_t *) eval_test_arraybuffer_p,
                                    strlen (eval_test_arraybuffer_p),
                                    true);
    TEST_ASSERT (jerry_value_is_number (res));
    TEST_ASSERT (jerry_get_number_value (res) == sum);
    jerry_release_value (res);

    jerry_release_value (buffer);
  }

  /* Test ArrayBuffer external with invalid arguments */
  {
    jerry_value_t input_buffer = jerry_create_arraybuffer_external (0, NULL, NULL);
    TEST_ASSERT (jerry_value_is_error (input_buffer));
    TEST_ASSERT (jerry_get_error_type (input_buffer) == JERRY_ERROR_RANGE);
    jerry_release_value (input_buffer);
  }

  jerry_cleanup ();

  TEST_ASSERT (callback_called == true);

  return 0;
} /* main */
Example #5
0
/* Default configuration:
 *{
 *  mode: spi.MODE[0],
 *  chipSelect: spi.CHIPSELECT.NONE,
 *  maxSpeed: 500000,
 *  bitsPerWord: 8,
 *  bitOrder: spi.BITORDER.MSB,
 *  loopback: false
 * }
 */
static jerry_value_t spi_set_configuration(iotjs_spi_t* spi,
                                           jerry_value_t joptions) {
  jerry_value_t jmode =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_MODE);
  if (jerry_value_is_undefined(jmode)) {
    spi->mode = kSpiMode_0;
  } else {
    if (jerry_value_is_number(jmode)) {
      spi->mode = (SpiMode)iotjs_jval_as_number(jmode);
    } else {
      spi->mode = __kSpiModeMax;
    }

    if (spi->mode >= __kSpiModeMax) {
      return JS_CREATE_ERROR(
          TYPE, "Bad arguments - mode should be MODE[0], [1], [2] or [3]");
    }
  }
  jerry_release_value(jmode);

  jerry_value_t jchip_select =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_CHIPSELECT);
  if (jerry_value_is_undefined(jchip_select)) {
    spi->chip_select = kSpiCsNone;
  } else {
    if (jerry_value_is_number(jchip_select)) {
      spi->chip_select = (SpiChipSelect)iotjs_jval_as_number(jchip_select);
    } else {
      spi->chip_select = __kSpiCsMax;
    }

    if (spi->chip_select >= __kSpiCsMax) {
      return JS_CREATE_ERROR(
          TYPE, "Bad arguments - chipSelect should be CHIPSELECT.NONE or HIGH");
    }
  }
  jerry_release_value(jchip_select);

  jerry_value_t jmax_speed =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_MAXSPEED);
  if (jerry_value_is_undefined(jmax_speed)) {
    spi->max_speed = 500000;
  } else {
    if (!jerry_value_is_number(jmax_speed)) {
      return JS_CREATE_ERROR(TYPE, "Bad arguments - maxSpeed should be Number");
    }
    spi->max_speed = iotjs_jval_as_number(jmax_speed);
  }
  jerry_release_value(jmax_speed);

  jerry_value_t jbits_per_word =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_BITSPERWORD);
  if (jerry_value_is_undefined(jbits_per_word)) {
    spi->bits_per_word = 8;
  } else {
    if (jerry_value_is_number(jbits_per_word)) {
      spi->bits_per_word = iotjs_jval_as_number(jbits_per_word);
    } else {
      spi->bits_per_word = 0;
    }

    if (spi->bits_per_word != 8 && spi->bits_per_word != 9) {
      return JS_CREATE_ERROR(TYPE,
                             "Bad arguments - bitsPerWord should be 8 or 9");
    }
  }
  jerry_release_value(jbits_per_word);

  jerry_value_t jbit_order =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_BITORDER);
  if (jerry_value_is_undefined(jbit_order)) {
    spi->bit_order = kSpiOrderMsb;
  } else {
    if (jerry_value_is_number(jbit_order)) {
      spi->bit_order = (SpiOrder)iotjs_jval_as_number(jbit_order);
    } else {
      spi->bit_order = __kSpiOrderMax;
    }

    if (spi->bit_order >= __kSpiOrderMax) {
      return JS_CREATE_ERROR(
          TYPE, "Bad arguments - bitOrder should be BITORDER.MSB or LSB");
    }
  }
  jerry_release_value(jbit_order);

  jerry_value_t jloopback =
      iotjs_jval_get_property(joptions, IOTJS_MAGIC_STRING_LOOPBACK);
  if (jerry_value_is_undefined(jloopback)) {
    spi->loopback = false;
  } else {
    if (!jerry_value_is_boolean(jloopback)) {
      return JS_CREATE_ERROR(TYPE,
                             "Bad arguments - loopback should be Boolean");
    }
    spi->loopback = iotjs_jval_as_boolean(jloopback);
  }
  jerry_release_value(jloopback);

  return jerry_create_undefined();
}