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,10 +12,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "jerryscript-mbed-util/logging.h"
#include "jerryscript-mbed-event-loop/EventLoop.h"
#include "jerryscript-mbed-library-registry/wrap_tools.h"
#include "jerryscript-mbed-util/logging.h"
#include "mbed.h"
/**
@@ -23,13 +22,15 @@
*
* Called if/when the InterruptIn object is GC'ed.
*/
void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(InterruptIn) (void *void_ptr, jerry_object_native_info_t *info_p) {
(void) info_p;
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
void
NAME_FOR_CLASS_NATIVE_DESTRUCTOR (InterruptIn) (void *void_ptr, jerry_object_native_info_t *info_p)
{
(void) info_p;
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
native_ptr->rise(0);
native_ptr->fall(0);
delete native_ptr;
native_ptr->rise (0);
native_ptr->fall (0);
delete native_ptr;
}
/**
@@ -37,9 +38,8 @@ void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(InterruptIn) (void *void_ptr, jerry_object
*
* Set InterruptIn#destructor as the free callback.
*/
static const jerry_object_native_info_t native_obj_type_info = {
.free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(InterruptIn)
};
static const jerry_object_native_info_t native_obj_type_info = { .free_cb =
NAME_FOR_CLASS_NATIVE_DESTRUCTOR (InterruptIn) };
/**
* InterruptIn#rise (native JavaScript method)
@@ -48,62 +48,63 @@ static const jerry_object_native_info_t native_obj_type_info = {
*
* @param cb Callback function, or null to detach previously attached callback.
*/
DECLARE_CLASS_FUNCTION(InterruptIn, rise) {
CHECK_ARGUMENT_COUNT(InterruptIn, rise, (args_count == 1));
DECLARE_CLASS_FUNCTION (InterruptIn, rise)
{
CHECK_ARGUMENT_COUNT (InterruptIn, rise, (args_count == 1));
// Detach the rise callback when InterruptIn::rise(null) is called
if (jerry_value_is_null(args[0])) {
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Detach the rise callback when InterruptIn::rise(null) is called
if (jerry_value_is_null (args[0]))
{
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
}
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
jerry_value_t property_name = jerry_create_string((const jerry_char_t*)"cb_rise");
jerry_value_t cb_func = jerry_get_property(call_info_p->this_value, property_name);
jerry_release_value(property_name);
// Only drop the callback if it exists
if (jerry_value_is_function(cb_func)) {
// Ensure that the EventLoop frees memory used by the callback.
mbed::js::EventLoop::getInstance().dropCallback(cb_func);
}
jerry_release_value(cb_func);
native_ptr->rise(0);
return jerry_create_undefined();
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
}
// Assuming we actually have a callback now...
CHECK_ARGUMENT_TYPE_ALWAYS(InterruptIn, rise, 0, function);
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
jerry_value_t property_name = jerry_string_sz ("cb_rise");
jerry_value_t cb_func = jerry_object_get (call_info_p->this_value, property_name);
jerry_value_free (property_name);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
// Only drop the callback if it exists
if (jerry_value_is_function (cb_func))
{
// Ensure that the EventLoop frees memory used by the callback.
mbed::js::EventLoop::getInstance ().dropCallback (cb_func);
}
jerry_value_free (cb_func);
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
native_ptr->rise (0);
jerry_value_t f = args[0];
return jerry_undefined ();
}
// Pass the function to EventLoop.
mbed::Callback<void()> cb = mbed::js::EventLoop::getInstance().wrapFunction(f);
native_ptr->rise(cb);
// Assuming we actually have a callback now...
CHECK_ARGUMENT_TYPE_ALWAYS (InterruptIn, rise, 0, function);
// Keep track of our callback internally.
jerry_value_t property_name = jerry_create_string((const jerry_char_t*)"cb_rise");
jerry_release_value(jerry_set_property(call_info_p->this_value, property_name, f));
jerry_release_value(property_name);
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
return jerry_create_undefined();
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
}
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
jerry_value_t f = args[0];
// Pass the function to EventLoop.
mbed::Callback<void ()> cb = mbed::js::EventLoop::getInstance ().wrapFunction (f);
native_ptr->rise (cb);
// Keep track of our callback internally.
jerry_value_t property_name = jerry_string_sz ("cb_rise");
jerry_value_free (jerry_object_set (call_info_p->this_value, property_name, f));
jerry_value_free (property_name);
return jerry_undefined ();
}
/**
@@ -113,62 +114,63 @@ DECLARE_CLASS_FUNCTION(InterruptIn, rise) {
*
* @param cb Callback function, or null to detach previously attached callback.
*/
DECLARE_CLASS_FUNCTION(InterruptIn, fall) {
CHECK_ARGUMENT_COUNT(InterruptIn, fall, (args_count == 1));
DECLARE_CLASS_FUNCTION (InterruptIn, fall)
{
CHECK_ARGUMENT_COUNT (InterruptIn, fall, (args_count == 1));
// Detach the fall callback when InterruptIn::fall(null) is called
if (jerry_value_is_null(args[0])) {
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
// Detach the fall callback when InterruptIn::fall(null) is called
if (jerry_value_is_null (args[0]))
{
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
}
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
jerry_value_t property_name = jerry_create_string((const jerry_char_t*)"cb_fall");
jerry_value_t cb_func = jerry_get_property(call_info_p->this_value, property_name);
jerry_release_value(property_name);
// Only drop the callback if it exists
if (jerry_value_is_function(cb_func)) {
// Ensure that the EventLoop frees memory used by the callback.
mbed::js::EventLoop::getInstance().dropCallback(cb_func);
}
jerry_release_value(cb_func);
native_ptr->fall(0);
return jerry_create_undefined();
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
}
// Assuming we actually have a callback now...
CHECK_ARGUMENT_TYPE_ALWAYS(InterruptIn, fall, 0, function);
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
jerry_value_t property_name = jerry_string_sz ("cb_fall");
jerry_value_t cb_func = jerry_object_get (call_info_p->this_value, property_name);
jerry_value_free (property_name);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
// Only drop the callback if it exists
if (jerry_value_is_function (cb_func))
{
// Ensure that the EventLoop frees memory used by the callback.
mbed::js::EventLoop::getInstance ().dropCallback (cb_func);
}
jerry_value_free (cb_func);
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
native_ptr->fall (0);
jerry_value_t f = args[0];
return jerry_undefined ();
}
// Pass the function to EventLoop.
mbed::Callback<void()> cb = mbed::js::EventLoop::getInstance().wrapFunction(f);
native_ptr->fall(cb);
// Assuming we actually have a callback now...
CHECK_ARGUMENT_TYPE_ALWAYS (InterruptIn, fall, 0, function);
// Keep track of our callback internally.
jerry_value_t property_name = jerry_create_string((const jerry_char_t*)"cb_fall");
jerry_release_value(jerry_set_property(call_info_p->this_value, property_name, f));
jerry_release_value(property_name);
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
return jerry_create_undefined();
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
}
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
jerry_value_t f = args[0];
// Pass the function to EventLoop.
mbed::Callback<void ()> cb = mbed::js::EventLoop::getInstance ().wrapFunction (f);
native_ptr->fall (cb);
// Keep track of our callback internally.
jerry_value_t property_name = jerry_string_sz ("cb_fall");
jerry_value_free (jerry_object_set (call_info_p->this_value, property_name, f));
jerry_value_free (property_name);
return jerry_undefined ();
}
/**
@@ -178,24 +180,24 @@ DECLARE_CLASS_FUNCTION(InterruptIn, fall) {
*
* @param mode PullUp, PullDown, PullNone
*/
DECLARE_CLASS_FUNCTION(InterruptIn, mode) {
CHECK_ARGUMENT_COUNT(InterruptIn, mode, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(InterruptIn, mode, 0, number);
DECLARE_CLASS_FUNCTION (InterruptIn, mode)
{
CHECK_ARGUMENT_COUNT (InterruptIn, mode, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS (InterruptIn, mode, 0, number);
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
}
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
int pull = jerry_get_number_value(args[0]);
native_ptr->mode((PinMode)pull);
int pull = jerry_value_as_number (args[0]);
native_ptr->mode ((PinMode) pull);
return jerry_create_undefined();
return jerry_undefined ();
}
/**
@@ -203,21 +205,21 @@ DECLARE_CLASS_FUNCTION(InterruptIn, mode) {
*
* Disable IRQ. See InterruptIn.h in mbed-os sources for more details.
*/
DECLARE_CLASS_FUNCTION(InterruptIn, disable_irq) {
CHECK_ARGUMENT_COUNT(InterruptIn, disable_irq, (args_count == 0));
DECLARE_CLASS_FUNCTION (InterruptIn, disable_irq)
{
CHECK_ARGUMENT_COUNT (InterruptIn, disable_irq, (args_count == 0));
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
}
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
native_ptr->disable_irq();
return jerry_create_undefined();
native_ptr->disable_irq ();
return jerry_undefined ();
}
/**
@@ -225,21 +227,21 @@ DECLARE_CLASS_FUNCTION(InterruptIn, disable_irq) {
*
* Enable IRQ. See InterruptIn.h in mbed-os sources for more details.
*/
DECLARE_CLASS_FUNCTION(InterruptIn, enable_irq) {
CHECK_ARGUMENT_COUNT(InterruptIn, enable_irq, (args_count == 0));
DECLARE_CLASS_FUNCTION (InterruptIn, enable_irq)
{
CHECK_ARGUMENT_COUNT (InterruptIn, enable_irq, (args_count == 0));
void *void_ptr;
bool has_ptr = jerry_get_object_native_pointer(call_info_p->this_value, &void_ptr, &native_obj_type_info);
void *void_ptr = jerry_object_get_native_ptr (call_info_p->this_value, &native_obj_type_info);
if (!has_ptr) {
return jerry_create_error(JERRY_ERROR_TYPE,
(const jerry_char_t *) "Failed to get native InterruptIn pointer");
}
if (void_ptr == NULL)
{
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to get native InterruptIn pointer");
}
InterruptIn *native_ptr = static_cast<InterruptIn*>(void_ptr);
InterruptIn *native_ptr = static_cast<InterruptIn *> (void_ptr);
native_ptr->enable_irq();
return jerry_create_undefined();
native_ptr->enable_irq ();
return jerry_undefined ();
}
/**
@@ -249,21 +251,22 @@ DECLARE_CLASS_FUNCTION(InterruptIn, enable_irq) {
*
* @returns JavaScript object wrapping InterruptIn native object
*/
DECLARE_CLASS_CONSTRUCTOR(InterruptIn) {
CHECK_ARGUMENT_COUNT(InterruptIn, __constructor, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(InterruptIn, __constructor, 0, number);
int pin = jerry_get_number_value(args[0]);
DECLARE_CLASS_CONSTRUCTOR (InterruptIn)
{
CHECK_ARGUMENT_COUNT (InterruptIn, __constructor, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS (InterruptIn, __constructor, 0, number);
int pin = jerry_value_as_number (args[0]);
InterruptIn *native_ptr = new InterruptIn((PinName)pin);
jerry_value_t js_object = jerry_create_object();
InterruptIn *native_ptr = new InterruptIn ((PinName) pin);
jerry_value_t js_object = jerry_object ();
jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
jerry_object_set_native_ptr (js_object, &native_obj_type_info, native_ptr);
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, rise);
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, fall);
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, mode);
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, enable_irq);
ATTACH_CLASS_FUNCTION(js_object, InterruptIn, disable_irq);
ATTACH_CLASS_FUNCTION (js_object, InterruptIn, rise);
ATTACH_CLASS_FUNCTION (js_object, InterruptIn, fall);
ATTACH_CLASS_FUNCTION (js_object, InterruptIn, mode);
ATTACH_CLASS_FUNCTION (js_object, InterruptIn, enable_irq);
ATTACH_CLASS_FUNCTION (js_object, InterruptIn, disable_irq);
return js_object;
return js_object;
}