Migration guide

This guide intends to describe the major changes between the JerryScript 1.0 and 2.0 versions. In addtion it is designed to provide a guide on how to modify the 1.0 version code to a 2.0 compliant code.

During the development it was important to minimize the changes in the API functions and types. Each API method removal or chang is described below providing a before and after code example. For more information on the current API methods please check the API reference document.

Short list of removed/renamed headers, types, functions, and macros

Removed legacy headers

  • jerry-internal.h

Renamed headers

  • jerry-api.h to jerryscript.h
  • jerry-port.h to jerryscript-port.h

Removed API types

  • jerry_char_ptr_t usage replaced with jerry_char_t *
  • jerry_object_free_callback_t replaced by jerry_object_native_free_callback_t

Removed API methods

  • jerry_get_memory_limits
  • jerry_get_object_native_handle replaced by jerry_get_object_native_pointer
  • jerry_set_object_native_handle replaced by jerry_set_object_native_pointer
  • jerry_value_set_abort_flag replaced by jerry_create_abort_from_value
  • jerry_value_has_abort_flag replaced by jerry_value_is_abort
  • jerry_value_set_error_flag replaced by jerry_create_error_from_value
  • jerry_value_has_error_flag replaced by jerry_value_is_error
  • jerry_value_clear_error_flag replaced by jerry_get_value_from_error
  • jerry_get_value_without_error_flag replaced by jerry_get_value_from_error
  • jerry_parse_and_save_snapshot replaced by jerry_generate_snapshot
  • jerry_parse_and_save_function_snapshot replaced by jerry_generate_function_snapshot

Removed unused configuration macros

  • CONFIG_MEM_DATA_LIMIT_MINUS_HEAP_SIZE
  • CONFIG_MEM_STACK_LIMIT
  • CONFIG_VM_STACK_FRAME_INLINED_VALUES_NUMBER
  • CONFIG_ECMA_GLOBAL_ENVIRONMENT_DECLARATIVE
  • All CONFIG_.. macros have been renamed to use the JERRY_ prefix format.

Modified API functions

Error manipulating functions

The most important changes in the API are releated to error handling and manipulation.

jerry_value_set_abort_flag

This function was replaced with jerry_create_abort_from_value. Take note of the second argument of the new jerry_create_abort_from_value function which controls if the first argument should be usable after the call or not.

Before

  1. {
  2. jerry_value_t value;
  3. // create or acquire value
  4. // ...
  5. jerry_value_set_abort_flag (&value);
  6. jerry_release_value (value);
  7. }

After

  1. {
  2. jerry_value_t value;
  3. // create or acquire value
  4. // ...
  5. jerry_value_t abort = jerry_create_abort_from_value (value, true);
  6. // using the 'value' variable after release is invalid
  7. jerry_release_value (abort);
  8. }
  • OR
  1. {
  2. jerry_value_t value;
  3. ... // create or acquire value
  4. jerry_value_t abort = jerry_create_abort_from_value (value, false);
  5. // both 'abort' and 'value' can be used and must be released when they are no longer needed
  6. jerry_release_value (abort);
  7. jerry_release_value (value);
  8. }

jerry_value_has_abort_flag

This function was renamed to jerry_value_is_abort.

Before

  1. {
  2. jerry_value_t value;
  3. // create or acquire value
  4. // ...
  5. if (jerry_value_has_abort_flag (value))
  6. {
  7. // ...
  8. }
  9. jerry_release_value (value);
  10. }

After

  1. {
  2. jerry_value_t value;
  3. // create or acquire value
  4. // ...
  5. if (jerry_value_is_abort (value))
  6. {
  7. // ...
  8. }
  9. jerry_release_value (value);
  10. }

jerry_value_set_error_flag

This function was replaced with jerry_create_error_from_value. Take note of the second argument of the new jerry_create_error_from_value function which controls if the first argument should be usable after the call or not.

Before

  1. {
  2. jerry_value_t value;
  3. // create or acquire value
  4. // ...
  5. jerry_value_set_error_flag (&value);
  6. jerry_release_value (value);
  7. }

After

  1. {
  2. jerry_value_t value;
  3. // create or acquire value
  4. // ...
  5. jerry_value_t error = jerry_create_error_from_value (value, true);
  6. // using the 'value' variable after release is invalid
  7. jerry_release_value (error);
  8. }
  • OR
  1. {
  2. jerry_value_t value;
  3. // create or acquire value
  4. // ...
  5. jerry_value_t error = jerry_create_error_from_value (value, false);
  6. // both 'error' and 'value' can be used and must be released when they are no longer needed
  7. jerry_release_value (error);
  8. jerry_release_value (value);
  9. }

jerry_value_has_error_flag

This function was renamed to jerry_value_is_error.

Before

  1. {
  2. jerry_value_t value;
  3. // create or acquire value
  4. // ...
  5. if (jerry_value_has_error_flag (value))
  6. {
  7. // ...
  8. }
  9. jerry_release_value (value);
  10. }

After

  1. {
  2. jerry_value_t value;
  3. // create or acquire value
  4. // ...
  5. if (jerry_value_is_error (value))
  6. {
  7. // ...
  8. }
  9. jerry_release_value (value);
  10. }

jerry_value_clear_error_flag AND jerry_get_value_without_error_flag

These functions were merged into jerry_get_value_from_error. Please note the second argument of the new function which controls if the first argument passed should be released or not.

Before

  1. {
  2. jerry_value_t value;
  3. // create or acquire value
  4. // ...
  5. jerry_value_set_error_flag (&value);
  6. jerry_value_clear_error_flag (&value);
  7. // or
  8. jerry_value_t real_value = jerry_get_value_without_error_flag (value);
  9. jerry_release_value (value);
  10. jerry_release_value (real_value);
  11. }

After

  1. {
  2. jerry_value_t value;
  3. // create or acquire value
  4. // ...
  5. jerry_value_t error = jerry_create_error_from_value (value, true);
  6. jerry_value_t real_value = jerry_get_value_from_error (error, true);
  7. jerry_release_value (real_value);
  8. }

Other functions changed

jerry_register_magic_strings

In case of the jerry_register_magic_strings function the change is that the first argument’s base type jerry_char_ptr_t was changed to jerry_char_t*. For more details see: jerry_register_magic_strings.

In the following code parts please take note of the type used for the magic_string_items array.

Before

  1. {
  2. // must be static, because 'jerry_register_magic_strings' does not copy
  3. // the items must be sorted by size at first, then lexicographically
  4. static const jerry_char_ptr_t magic_string_items[] = {
  5. (const jerry_char_ptr_t) "magicstring1",
  6. (const jerry_char_ptr_t) "magicstring2",
  7. (const jerry_char_ptr_t) "magicstring3"
  8. };
  9. uint32_t num_magic_string_items = (uint32_t) (sizeof (magic_string_items) / sizeof (jerry_char_ptr_t));
  10. // must be static, because 'jerry_register_magic_strings' does not copy
  11. static const jerry_length_t magic_string_lengths[] = { 12, 12, 12 };
  12. jerry_register_magic_strings (magic_string_items, num_magic_string_items, magic_string_lengths);
  13. }

After

  1. {
  2. // must be static, because 'jerry_register_magic_strings' does not copy
  3. // the items must be sorted by size at first, then lexicographically
  4. static const jerry_char_t *magic_string_items[] = {
  5. (const jerry_char_t *) "magicstring1",
  6. (const jerry_char_t *) "magicstring2",
  7. (const jerry_char_t *) "magicstring3"
  8. };
  9. uint32_t num_magic_string_items = (uint32_t) (sizeof (magic_string_items) / sizeof (jerry_char_t *));
  10. // must be static, because 'jerry_register_magic_strings' does not copy
  11. static const jerry_length_t magic_string_lengths[] = { 12, 12, 12 };
  12. jerry_register_magic_strings (magic_string_items, num_magic_string_items, magic_string_lengths);
  13. }

Snapshot generating API

jerry_parse_and_save_snapshot

This function was replaced with jerry_generate_snapshot. The function returns an error object if there was any problem during snapshot generation and if there was no problem the return value is a number value containing the snapshot size in bytes.

Before

  1. {
  2. static uint32_t global_mode_snapshot_buffer[256];
  3. const jerry_char_t *code_to_snapshot_p = (const jerry_char_t *) "(function () { return 'string from snapshot'; }) ();";
  4. size_t global_mode_snapshot_size =
  5. jerry_parse_and_save_snapshot (code_to_snapshot_p,
  6. strlen ((const char *) code_to_snapshot_p),
  7. true,
  8. false,
  9. global_mode_snapshot_buffer,
  10. sizeof (global_mode_snapshot_buffer) / sizeof (uint32_t));
  11. // use "global_mode_snapshot_buffer"
  12. }

After

  1. {
  2. static uint32_t global_mode_snapshot_buffer[256];
  3. const jerry_char_t *code_to_snapshot_p = (const jerry_char_t *) "(function () { return 'string from snapshot'; }) ();";
  4. jerry_value_t generate_result;
  5. generate_result = jerry_generate_snapshot (NULL,
  6. 0,
  7. code_to_snapshot_p,
  8. strlen ((const char *) code_to_snapshot_p),
  9. global_mode_snapshot_buffer,
  10. sizeof (global_mode_snapshot_buffer) / sizeof (uint32_t));
  11. if (jerry_value_is_error (generate_result))
  12. {
  13. // There was a problem during snapshot generation, for example there is a SyntaxError.
  14. // Use the "generate_result" to check the error.
  15. }
  16. else
  17. {
  18. size_t snapshot_size = (size_t) jerry_get_number_value (generate_result);
  19. // use "global_mode_snapshot_buffer"
  20. }
  21. jerry_release_value (generate_result);
  22. }

jerry_parse_and_save_function_snapshot

This function was replaced with jerry_generate_function_snapshot. The function returns an error object if there was any problem during snapshot generation and if there was no problem the return value is a number value containing the snapshot size in bytes.

Before

  1. {
  2. static uint32_t func_snapshot_buffer[1024];
  3. const jerry_char_t *args_p = (const jerry_char_t *) "a, b";
  4. const jerry_char_t *src_p = (const jerry_char_t *) "return a + b;";
  5. size_t func_snapshot_size =
  6. jerry_parse_and_save_function_snapshot (src_p,
  7. strlen ((const char *) src_p),
  8. args_p,
  9. strlen ((const char *) args_p),
  10. false,
  11. func_snapshot_buffer,
  12. sizeof (func_snapshot_buffer) / sizeof (uint32_t));
  13. // check "function_snapshot_size" and use "func_snapshot_buffer"
  14. }

After

  1. {
  2. static uint32_t func_snapshot_buffer[1024];
  3. const jerry_char_t *args_p = (const jerry_char_t *) "a, b";
  4. const jerry_char_t *src_p = (const jerry_char_t *) "return a + b;";
  5. jerry_value_t generate_result;
  6. generate_result = jerry_generate_function_snapshot (NULL,
  7. 0,
  8. src_p,
  9. strlen ((const char *) src_p),
  10. args_p,
  11. strlen ((const char *) args_p),
  12. 0,
  13. func_snapshot_buffer,
  14. sizeof (func_snapshot_buffer) / sizeof (uint32_t));
  15. if (jerry_value_is_error (generate_result))
  16. {
  17. // There was a problem during snapshot generation, for example there is a SyntaxError.
  18. // Use the "generate_result" to check the error.
  19. }
  20. else
  21. {
  22. size_t snapshot_size = (size_t) jerry_get_number_value (generate_result);
  23. // use "func_snapshot_buffer"
  24. }
  25. jerry_release_value (generate_result)
  26. }

Garbage collection

jerry_gc

The jerry_gc function was modified to handle an argument which represents the pressure for the garbage collector. For more information checkout the jerry_gc_mode_t reference.

Before

  1. {
  2. jerry_gc ();
  3. }

After

  1. {
  2. jerry_gc (JERRY_GC_PRESSURE_LOW);
  3. }

jerry_eval

The third argument of jerry_eval has been changed from bool to jerry_parse_opts_t.

Before

  1. const jerry_char_t *str_to_eval = (const jerry_char_t *) "1 + 1";
  2. jerry_value_t ret_val = jerry_eval (str_to_eval,
  3. strlen ((const char *) str_to_eval),
  4. false);

After

  1. const jerry_char_t *str_to_eval = (const jerry_char_t *) "1 + 1";
  2. jerry_value_t ret_val = jerry_eval (str_to_eval,
  3. strlen ((const char *) str_to_eval),
  4. JERRY_PARSE_NO_OPTS);

Port API

jerry_port_get_time_zone

The port API of handling timezones has been changed. The previous interface did not allow timezones to be handled correctly, even if the host system was up to the task. Check the related issue for more details.

The new port API function name is [jerry_port_get_local_time_zone_adjustment](05.PORT-API.md#date-1].

Below is the default implementations for both versions:

Before

  1. bool jerry_port_get_time_zone (jerry_time_zone_t *tz_p)
  2. {
  3. struct timeval tv;
  4. struct timezone tz;
  5. /* gettimeofday may not fill tz, so zero-initializing */
  6. tz.tz_minuteswest = 0;
  7. tz.tz_dsttime = 0;
  8. if (gettimeofday (&tv, &tz) != 0)
  9. {
  10. return false;
  11. }
  12. tz_p->offset = tz.tz_minuteswest;
  13. tz_p->daylight_saving_time = tz.tz_dsttime > 0 ? 1 : 0;
  14. return true;
  15. } /* jerry_port_get_time_zone */

After

  1. double jerry_port_get_local_time_zone_adjustment (double unix_ms,
  2. bool is_utc)
  3. {
  4. struct tm tm;
  5. time_t now = (time_t) (unix_ms / 1000);
  6. localtime_r (&now, &tm);
  7. if (!is_utc)
  8. {
  9. now -= tm.tm_gmtoff;
  10. localtime_r (&now, &tm);
  11. }
  12. return ((double) tm.tm_gmtoff) * 1000;
  13. } /* jerry_port_get_local_time_zone_adjustment */

Native pointers

The assignment of native pointers (previously called handles) have been changed since v1.0. In the previous version only one native pointer could be assigned to a jerry_value_t. Now it is allowed to register multiple native infos, which can be accessed with the corresponding jerry_object_native_info_t. The old functions were removed and replaced by new ones.

Before

  1. struct
  2. {
  3. int data;
  4. } my_info;
  5. static void
  6. handler_construct_freecb (uintptr_t native_p)
  7. {
  8. // Invoked when the JS object is released and the
  9. // native data should be freed.
  10. struct my_info *info = (struct my_info *) native_p;
  11. free (info);
  12. }
  13. void
  14. demo (void)
  15. {
  16. jerry_value_t this_val;
  17. // create or acquire this_val
  18. // ...
  19. struct my_info *info = (struct my_info *) malloc (sizeof (struct my_info));
  20. info->data = 11;
  21. // setting the native handle
  22. jerry_set_object_native_handle (this_val,
  23. (uintptr_t) info,
  24. handler_construct_freecb);
  25. // ...
  26. // reading back the native handle
  27. uintptr_t ptr = (uintptr_t) NULL;
  28. bool is_ok = jerry_get_object_native_handle (this_val, &ptr);
  29. if (is_ok)
  30. {
  31. struct my_info *obj_info = (struct my_info *) ptr;
  32. // use "obj_info"
  33. }
  34. }

After

  1. struct
  2. {
  3. int data;
  4. } my_info;
  5. static void
  6. handler_construct_freecb (void *native_p)
  7. {
  8. // Invoked when the JS object is released and the
  9. // native data should be freed.
  10. struct my_info *info = (struct my_info *) native_p;
  11. free (info);
  12. }
  13. static const jerry_object_native_info_t my_info_type_info =
  14. {
  15. .free_cb = handler_construct_freecb
  16. };
  17. void
  18. demo (void)
  19. {
  20. jerry_value_t this_val;
  21. // create or acquire this_val
  22. // ...
  23. struct my_info *info = (struct my_info *) malloc (sizeof (struct my_info));
  24. info->data = 11;
  25. // setting the native handle
  26. jerry_set_object_native_pointer (this_val,
  27. info,
  28. &my_info_type_info);
  29. // ...
  30. // reading back the native handle pointed by the "my_info_type_info" variable
  31. void *ptr = NULL;
  32. bool has_p = jerry_get_object_native_pointer (this_val, &ptr, &my_info_type_info);
  33. if (has_p)
  34. {
  35. struct my_info *obj_info = (struct my_info *) ptr;
  36. // use "obj_info"
  37. }
  38. }

New API functions

In this section the new API functions are listed.

Built-in objects

ArrayBuffer

DataView

JSON

Number

Promise

RegExp

String

Symbol

TypedArray

Instances and memory management

JerryScript instances

Memory management

Operations with JavaScript values

Binary operations

Error manipulating

Native pointers

Property

Debugger

Other

Port API functions