Rework the public API (#4829)
Related to #4186. Some notable changes: - The term 'Error' now strictly refers to native Error objects defined in the ECMA standard, which are ordinary objects. All other uses of 'error' or 'error reference' where the term refers to a thrown value is now called 'exception'. - Simplified the naming scheme of many String API functions. These functions will now also take an 'encoding' argument to specify the desired encoding in which to operate. - Removed the substring-copy-to-buffer functions. These functions behaved awkwardly, as they use character index to specify the start/end positions, and were mostly used incorrectly with byte offsets instead. The functionality can still be replicated with other functions if necessary. - String-to-buffer functions will no longer fail if the buffer is not sufficiently large, the string will instead be cropped. - Fixed the usage of the '_sz' prefix in many API functions. The term 'sz' means zero-terminated string in hungarian notation, this was used incorrectly in many cases. - Renamed most of the public API functions to have shorter, more on-point names, rather than the often too long descriptive names. Functions are now also grouped by the type of value they operate on, where this makes sense. JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
This commit is contained in:
@@ -12,7 +12,7 @@ Set a property on a target object with a given name.
|
||||
*Note*:
|
||||
- The property name must be a zero terminated UTF-8 string.
|
||||
- There should be no '\0' (NULL) character in the name excluding the string terminator.
|
||||
- Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
|
||||
- Returned value must be freed with [jerry_value_free](#jerry_value_free) when it
|
||||
is no longer needed.
|
||||
|
||||
|
||||
@@ -45,19 +45,19 @@ main (int argc, char **argv)
|
||||
{
|
||||
jerry_init (JERRY_INIT_EMPTY);
|
||||
|
||||
jerry_value_t global = jerry_get_global_object ();
|
||||
jerry_value_t global = jerry_current_realm ();
|
||||
|
||||
jerry_value_t value = jerry_create_number (3.3);
|
||||
jerry_value_t value = jerry_number (3.3);
|
||||
jerry_value_t result = jerryx_set_property_str (global, "value", value);
|
||||
if (jerry_value_is_error (result))
|
||||
if (jerry_value_is_exception (result))
|
||||
{
|
||||
/* The error type/reason can be extracted via the `jerry_get_value_from_error` method */
|
||||
/* The error type/reason can be extracted via the `jerry_exception_value` method */
|
||||
printf ("Error during property configuration\r\n");
|
||||
}
|
||||
|
||||
jerry_release_value (result);
|
||||
jerry_release_value (value);
|
||||
jerry_release_value (global);
|
||||
jerry_value_free (result);
|
||||
jerry_value_free (value);
|
||||
jerry_value_free (global);
|
||||
jerry_cleanup();
|
||||
|
||||
return 0;
|
||||
@@ -73,7 +73,7 @@ Get the value of a property from the specified object with the given name.
|
||||
*Notes*:
|
||||
- The property name must be a zero terminated UTF-8 string.
|
||||
- There should be no '\0' (NULL) character in the name excluding the string terminator.
|
||||
- Returned value must be freed with [jerry_release_value](#jerry_release_value) when it
|
||||
- Returned value must be freed with [jerry_value_free](#jerry_value_free) when it
|
||||
is no longer needed.
|
||||
|
||||
|
||||
@@ -104,14 +104,14 @@ main (int argc, char **argv)
|
||||
{
|
||||
jerry_init (JERRY_INIT_EMPTY);
|
||||
|
||||
jerry_value_t global = jerry_get_global_object ();
|
||||
jerry_value_t global = jerry_current_realm ();
|
||||
|
||||
jerry_value_t math_object = jerryx_get_property_str (global, "Math");
|
||||
|
||||
/* use math_object */
|
||||
|
||||
jerry_release_value (math_object);
|
||||
jerry_release_value (global);
|
||||
jerry_value_free (math_object);
|
||||
jerry_value_free (global);
|
||||
jerry_cleanup();
|
||||
|
||||
return 0;
|
||||
@@ -125,7 +125,7 @@ main (int argc, char **argv)
|
||||
Check if a property exists on an object.
|
||||
|
||||
*Notes*:
|
||||
- The operation performed is the same as what the `jerry_has_property` method.
|
||||
- The operation performed is the same as what the `jerry_object_has` method.
|
||||
- The property name must be a zero terminated UTF-8 string.
|
||||
- There should be no '\0' (NULL) character in the name excluding the string terminator.
|
||||
|
||||
@@ -157,11 +157,11 @@ main (int argc, char **argv)
|
||||
{
|
||||
jerry_init (JERRY_INIT_EMPTY);
|
||||
|
||||
jerry_value_t global = jerry_get_global_object ();
|
||||
jerry_value_t global = jerry_current_realm ();
|
||||
|
||||
bool have_math = jerryx_has_property_str (global, "Math");
|
||||
|
||||
jerry_release_value (global);
|
||||
jerry_value_free (global);
|
||||
jerry_cleanup();
|
||||
|
||||
return have_math ? 0 : 1;
|
||||
@@ -273,7 +273,7 @@ handler (const jerry_call_info_t *call_info_p,
|
||||
{
|
||||
printf ("native handler called!\n");
|
||||
|
||||
return jerry_create_boolean (true);
|
||||
return jerry_boolean (true);
|
||||
}
|
||||
|
||||
int
|
||||
@@ -283,24 +283,24 @@ main (int argc, char **argv)
|
||||
|
||||
jerryx_property_entry methods[] =
|
||||
{
|
||||
{ "demo", jerry_create_external_function (handler) },
|
||||
{ "demo", jerry_function_external (handler) },
|
||||
{ NULL, 0 },
|
||||
};
|
||||
|
||||
jerry_value_t global = jerry_get_global_object ();
|
||||
jerry_value_t global = jerry_current_realm ();
|
||||
jerryx_register_result reg = jerryx_set_properties (global, methods);
|
||||
/* if `reg.result` is undefined all methods are registered */
|
||||
if (jerry_value_is_error (reg.result))
|
||||
if (jerry_value_is_exception (reg.result))
|
||||
{
|
||||
printf ("Only registered %d properties\r\n", reg.registered);
|
||||
/* clean up not registered property values */
|
||||
jerryx_release_property_entry (methods, reg);
|
||||
|
||||
/* clean up the error */
|
||||
jerry_release_value (reg.result);
|
||||
jerry_value_free (reg.result);
|
||||
}
|
||||
|
||||
jerry_release_value (global);
|
||||
jerry_value_free (global);
|
||||
|
||||
jerry_cleanup();
|
||||
|
||||
@@ -314,8 +314,8 @@ To make property registration convenient, there are a set of macros to use
|
||||
when setting a property entry:
|
||||
|
||||
* `JERRYX_PROPERTY_NUMBER(NAME, NUMBER)` - creates a number entry.
|
||||
* `JERRYX_PROPERTY_STRING(NAME, STR)` - creates an UTF-8 string entry. This string must be zero terminated.
|
||||
* `JERRYX_PROPERTY_STRING_SZ(NAME, STR, SIZE)` - creates an UTF-8 string entry using only `SIZE` bytes from the string.
|
||||
* `JERRYX_PROPERTY_STRING(NAME, STR, SIZE)` - creates an UTF-8 string entry using `SIZE` bytes from the string.
|
||||
* `JERRYX_PROPERTY_STRING_SZ(NAME, STR)` - creates an ASCII string entry. This string must be zero terminated.
|
||||
* `JERRYX_PROPERTY_BOOLEAN(NAME, VALUE)` - creates a boolean entry.
|
||||
* `JERRYX_PROPERTY_FUNCTION(NAME, NATIVE)` - creates a native C function entry.
|
||||
* `JERRYX_PROPERTY_UNDEFINED(NAME)` - creates an undefined property entry.
|
||||
@@ -336,7 +336,7 @@ handler (const jerry_call_info_t *call_info_p,
|
||||
{
|
||||
printf ("native handler called!\n");
|
||||
|
||||
return jerry_create_boolean (true);
|
||||
return jerry_boolean (true);
|
||||
}
|
||||
|
||||
int
|
||||
@@ -357,20 +357,20 @@ main (int argc, char **argv)
|
||||
JERRYX_PROPERTY_LIST_END(),
|
||||
};
|
||||
|
||||
jerry_value_t global = jerry_get_global_object ();
|
||||
jerry_value_t global = jerry_current_realm ();
|
||||
jerryx_register_result reg = jerryx_set_properties (global, methods);
|
||||
/* if `reg.result` is undefined all methods are registered */
|
||||
if (jerry_value_is_error (reg.result))
|
||||
if (jerry_value_is_exception (reg.result))
|
||||
{
|
||||
printf ("Only registered %d properties\r\n", reg.registered);
|
||||
/* clean up not registered property values */
|
||||
jerryx_release_property_entry (methods, reg);
|
||||
|
||||
/* clean up the error */
|
||||
jerry_release_value (reg.result);
|
||||
jerry_value_free (reg.result);
|
||||
}
|
||||
|
||||
jerry_release_value (global);
|
||||
jerry_value_free (global);
|
||||
|
||||
jerry_cleanup();
|
||||
|
||||
@@ -551,14 +551,14 @@ jerryx_handler_print (const jerry_value_t func_obj_val, const jerry_value_t this
|
||||
|
||||
Register a JavaScript function in the global object.
|
||||
|
||||
*Note*: Returned value must be freed with `jerry_release_value`, when it is no
|
||||
*Note*: Returned value must be freed with `jerry_value_free`, when it is no
|
||||
longer needed.
|
||||
|
||||
**Prototype**
|
||||
|
||||
```c
|
||||
jerry_value_t
|
||||
jerryx_handler_register_global (const jerry_char_t *name_p,
|
||||
jerryx_handler_register_global (const char *name_p,
|
||||
jerry_external_handler_t handler_p);
|
||||
```
|
||||
|
||||
@@ -589,14 +589,14 @@ static const struct {
|
||||
static void
|
||||
register_common_functions (void)
|
||||
{
|
||||
jerry_value_t ret = jerry_create_undefined ();
|
||||
jerry_value_t ret = jerry_undefined ();
|
||||
|
||||
for (int i = 0; common_functions[i].name_p != NULL && !jerry_value_is_error (ret); i++)
|
||||
for (int i = 0; common_functions[i].name_p != NULL && !jerry_value_is_exception (ret); i++)
|
||||
{
|
||||
ret = jerryx_handler_register_global ((const jerry_char_t *) common_functions[i].name_p,
|
||||
ret = jerryx_handler_register_global (common_functions[i].name_p,
|
||||
common_functions[i].handler_p);
|
||||
}
|
||||
|
||||
jerry_release_value (ret);
|
||||
jerry_value_free (ret);
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user