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:
Dániel Bátyai
2021-12-06 10:20:09 +01:00
committed by GitHub
parent 81d2319144
commit 9860d66a56
180 changed files with 10738 additions and 11025 deletions
@@ -12,66 +12,73 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdlib.h>
#include <stdio.h>
#include "jerryscript-mbed-library-registry/wrap_tools.h"
bool jsmbed_wrap_register_global_function(const char* name, jerry_external_handler_t handler) {
jerry_value_t global_object_val = jerry_get_global_object();
jerry_value_t reg_function = jerry_create_external_function(handler);
#include <stdio.h>
#include <stdlib.h>
bool is_ok = true;
bool
jsmbed_wrap_register_global_function (const char* name, jerry_external_handler_t handler)
{
jerry_value_t global_object_val = jerry_current_realm ();
jerry_value_t reg_function = jerry_function_external (handler);
if (!(jerry_value_is_function(reg_function)
&& jerry_value_is_constructor(reg_function))) {
is_ok = false;
LOG_PRINT_ALWAYS("Error: jerry_create_external_function failed!\r\n");
jerry_release_value(global_object_val);
jerry_release_value(reg_function);
return is_ok;
}
if (jerry_value_is_error(reg_function)) {
is_ok = false;
LOG_PRINT_ALWAYS("Error: jerry_create_external_function has error flag! \r\n");
jerry_release_value(global_object_val);
jerry_release_value(reg_function);
return is_ok;
}
jerry_value_t jerry_name = jerry_create_string((jerry_char_t *) name);
jerry_value_t set_result = jerry_set_property(global_object_val, jerry_name, reg_function);
if (jerry_value_is_error(set_result)) {
is_ok = false;
LOG_PRINT_ALWAYS("Error: jerry_create_external_function failed: [%s]\r\n", name);
}
jerry_release_value(jerry_name);
jerry_release_value(global_object_val);
jerry_release_value(reg_function);
jerry_release_value(set_result);
bool is_ok = true;
if (!(jerry_value_is_function (reg_function) && jerry_value_is_constructor (reg_function)))
{
is_ok = false;
LOG_PRINT_ALWAYS ("Error: jerry_function_external failed!\r\n");
jerry_value_free (global_object_val);
jerry_value_free (reg_function);
return is_ok;
}
if (jerry_value_is_exception (reg_function))
{
is_ok = false;
LOG_PRINT_ALWAYS ("Error: jerry_function_external has error flag! \r\n");
jerry_value_free (global_object_val);
jerry_value_free (reg_function);
return is_ok;
}
jerry_value_t jerry_name = jerry_string_sz (name);
jerry_value_t set_result = jerry_object_set (global_object_val, jerry_name, reg_function);
if (jerry_value_is_exception (set_result))
{
is_ok = false;
LOG_PRINT_ALWAYS ("Error: jerry_function_external failed: [%s]\r\n", name);
}
jerry_value_free (jerry_name);
jerry_value_free (global_object_val);
jerry_value_free (reg_function);
jerry_value_free (set_result);
return is_ok;
}
bool jsmbed_wrap_register_class_constructor(const char* name, jerry_external_handler_t handler) {
// Register class constructor as a global function
return jsmbed_wrap_register_global_function(name, handler);
bool
jsmbed_wrap_register_class_constructor (const char* name, jerry_external_handler_t handler)
{
// Register class constructor as a global function
return jsmbed_wrap_register_global_function (name, handler);
}
bool jsmbed_wrap_register_class_function(jerry_value_t this_obj, const char* name, jerry_external_handler_t handler) {
jerry_value_t property_name = jerry_create_string(reinterpret_cast<const jerry_char_t *>(name));
jerry_value_t handler_obj = jerry_create_external_function(handler);
bool
jsmbed_wrap_register_class_function (jerry_value_t this_obj, const char* name, jerry_external_handler_t handler)
{
jerry_value_t property_name = jerry_string_sz (name);
jerry_value_t handler_obj = jerry_function_external (handler);
jerry_release_value(jerry_set_property(this_obj, property_name, handler_obj));
jerry_value_free (jerry_object_set (this_obj, property_name, handler_obj));
jerry_release_value(handler_obj);
jerry_release_value(property_name);
jerry_value_free (handler_obj);
jerry_value_free (property_name);
// TODO: check for errors, and return false in the case of errors
return true;
// TODO: check for errors, and return false in the case of errors
return true;
}