Ejemplo n.º 1
0
static uint8_t spi_transfer_helper(jerry_value_t jtx_buf, iotjs_spi_t* spi) {
  uint8_t result = kSpiOpTransferBuffer;
  if (jerry_value_is_array(jtx_buf)) {
    spi->buf_len = spi_get_array_data(&spi->tx_buf_data, jtx_buf);
    result = kSpiOpTransferArray;
  } else if (jerry_value_is_object(jtx_buf)) {
    iotjs_bufferwrap_t* tx_buf = iotjs_bufferwrap_from_jbuffer(jtx_buf);

    spi->tx_buf_data = tx_buf->buffer;
    spi->buf_len = iotjs_bufferwrap_length(tx_buf);
  }

  IOTJS_ASSERT(spi->buf_len > 0);
  spi->rx_buf_data = iotjs_buffer_allocate(spi->buf_len);
  IOTJS_ASSERT(spi->tx_buf_data != NULL && spi->rx_buf_data != NULL);

  return result;
}
Ejemplo n.º 2
0
/**
 * Validate the JS arguments and assign them to the native arguments.
 *
 * @return jerry undefined: all validators passed,
 *         jerry error: a validator failed.
 */
jerry_value_t
jerryx_arg_transform_args (const jerry_value_t *js_arg_p, /**< points to the array with JS arguments */
                           const jerry_length_t js_arg_cnt, /**< the count of the `js_arg_p` array */
                           const jerryx_arg_t *c_arg_p, /**< points to the array of validation/transformation steps */
                           jerry_length_t c_arg_cnt) /**< the count of the `c_arg_p` array */
{
  jerry_value_t ret = jerry_create_undefined ();

  jerryx_arg_js_iterator_t iterator =
  {
    .js_arg_p = js_arg_p,
    .js_arg_cnt = js_arg_cnt,
    .js_arg_idx = 0
  };

  for (; c_arg_cnt != 0 && !jerry_value_is_error (ret); c_arg_cnt--, c_arg_p++)
  {
    ret = c_arg_p->func (&iterator, c_arg_p);
  }

  return ret;
} /* jerryx_arg_transform_args */

/**
 * Validate the this value and the JS arguments,
 * and assign them to the native arguments.
 * This function is useful to perform input validation inside external
 * function handlers (see jerry_external_handler_t).
 * @note this_val is processed as the first value, before the array of arguments.
 *
 * @return jerry undefined: all validators passed,
 *         jerry error: a validator failed.
 */
jerry_value_t
jerryx_arg_transform_this_and_args (const jerry_value_t this_val, /**< the this_val for the external function */
                                    const jerry_value_t *js_arg_p, /**< points to the array with JS arguments */
                                    const jerry_length_t js_arg_cnt, /**< the count of the `js_arg_p` array */
                                    const jerryx_arg_t *c_arg_p, /**< points to the array of transformation steps */
                                    jerry_length_t c_arg_cnt) /**< the count of the `c_arg_p` array */
{
  if (c_arg_cnt == 0)
  {
    return jerry_create_undefined ();
  }

  jerryx_arg_js_iterator_t iterator =
  {
    .js_arg_p = &this_val,
    .js_arg_cnt = 1,
    .js_arg_idx = 0
  };

  jerry_value_t ret = c_arg_p->func (&iterator, c_arg_p);

  if (jerry_value_is_error (ret))
  {
    jerry_release_value (ret);

    return jerry_create_error (JERRY_ERROR_TYPE, (jerry_char_t *) "'this' validation failed.");
  }

  return jerryx_arg_transform_args (js_arg_p, js_arg_cnt, c_arg_p + 1, c_arg_cnt - 1);
} /* jerryx_arg_transform_this_and_args */

/**
 * Validate the `obj_val`'s properties,
 * and assign them to the native arguments.
 *
 * @return jerry undefined: all validators passed,
 *         jerry error: a validator failed.
 */
jerry_value_t
jerryx_arg_transform_object_properties (const jerry_value_t obj_val,/**< the JS object */
                                        const jerry_char_t **name_p, /**< property name list of the JS object */
                                        const jerry_length_t name_cnt, /**< count of the name list */
                                        const jerryx_arg_t *c_arg_p, /**< points to the array of transformation steps */
                                        jerry_length_t c_arg_cnt) /**< the count of the `c_arg_p` array */
{
  if (!jerry_value_is_object (obj_val))
  {
    return jerry_create_error (JERRY_ERROR_TYPE, (jerry_char_t *) "Not an object.");
  }

  jerry_value_t prop[name_cnt];

  for (jerry_length_t i = 0; i < name_cnt; i++, name_p++)
  {
    const jerry_value_t name_str = jerry_create_string (*name_p);
    prop[i] = jerry_get_property (obj_val, name_str);
    jerry_release_value (name_str);

    if (jerry_value_is_error (prop[i]))
    {
      for (jerry_length_t j = 0; j < i; j++)
      {
        jerry_release_value (prop[j]);
      }

      return prop[i];
    }
  }

  const jerry_value_t ret = jerryx_arg_transform_args (prop, name_cnt, c_arg_p, c_arg_cnt);

  for (jerry_length_t i = 0; i < name_cnt; i++)
  {
    jerry_release_value (prop[i]);
  }

  return ret;
} /* jerryx_arg_transform_object_properties */

/**
 * Validate the items in the JS array and assign them to the native arguments.
 *
 * @return jerry undefined: all validators passed,
 *         jerry error: a validator failed.
 */
jerry_value_t
jerryx_arg_transform_array (const jerry_value_t array_val, /**< points to the JS array */
                            const jerryx_arg_t *c_arg_p, /**< points to the array of validation/transformation steps */
                            jerry_length_t c_arg_cnt) /**< the count of the `c_arg_p` array */
{
  if (!jerry_value_is_array (array_val))
  {
    return jerry_create_error (JERRY_ERROR_TYPE, (jerry_char_t *) "Not an array.");
  }

  jerry_value_t arr[c_arg_cnt];

  for (jerry_length_t i = 0; i < c_arg_cnt; i++)
  {
    arr[i] = jerry_get_property_by_index (array_val, i);

    if (jerry_value_is_error (arr[i]))
    {
      for (jerry_length_t j = 0; j < i; j++)
      {
        jerry_release_value (arr[j]);
      }

      return arr[i];
    }
  }

  const jerry_value_t ret = jerryx_arg_transform_args (arr, c_arg_cnt, c_arg_p, c_arg_cnt);

  for (jerry_length_t i = 0; i < c_arg_cnt; i++)
  {
    jerry_release_value (arr[i]);
  }

  return ret;
} /* jerryx_arg_transform_array */
Ejemplo n.º 3
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 */