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:
+36
-27
@@ -13,6 +13,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "jerryscript-mbed-drivers/setInterval-js.h"
|
||||
|
||||
#include "jerryscript-mbed-event-loop/EventLoop.h"
|
||||
|
||||
/**
|
||||
@@ -23,26 +24,33 @@
|
||||
* @param function Function to call
|
||||
* @param interval Time between function calls, in ms.
|
||||
*/
|
||||
DECLARE_GLOBAL_FUNCTION(setInterval) {
|
||||
CHECK_ARGUMENT_COUNT(global, setInterval, (args_count == 2));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(global, setInterval, 0, function);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(global, setInterval, 1, number);
|
||||
DECLARE_GLOBAL_FUNCTION (setInterval)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (global, setInterval, (args_count == 2));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (global, setInterval, 0, function);
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (global, setInterval, 1, number);
|
||||
|
||||
int interval = int(jerry_get_number_value(args[1]));
|
||||
int interval = int (jerry_value_as_number (args[1]));
|
||||
|
||||
int id = mbed::js::EventLoop::getInstance().getQueue().call_every(interval, jerry_call_function, args[0], jerry_create_null(), (jerry_value_t*)NULL, 0);
|
||||
int id = mbed::js::EventLoop::getInstance ().getQueue ().call_every (interval,
|
||||
jerry_call,
|
||||
args[0],
|
||||
jerry_null (),
|
||||
(jerry_value_t*) NULL,
|
||||
0);
|
||||
|
||||
jerry_value_t result = jerry_set_property_by_index(call_info_p->function, id, args[0]);
|
||||
jerry_value_t result = jerry_object_set_index (call_info_p->function, id, args[0]);
|
||||
|
||||
if (jerry_value_is_error(result)) {
|
||||
jerry_release_value(result);
|
||||
mbed::js::EventLoop::getInstance().getQueue().cancel(id);
|
||||
if (jerry_value_is_exception (result))
|
||||
{
|
||||
jerry_value_free (result);
|
||||
mbed::js::EventLoop::getInstance ().getQueue ().cancel (id);
|
||||
|
||||
return jerry_create_error(JERRY_ERROR_TYPE, (const jerry_char_t *) "Failed to run setInterval");
|
||||
}
|
||||
return jerry_throw_sz (JERRY_ERROR_TYPE, "Failed to run setInterval");
|
||||
}
|
||||
|
||||
jerry_release_value(result);
|
||||
return jerry_create_number(id);
|
||||
jerry_value_free (result);
|
||||
return jerry_number (id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,22 +60,23 @@ DECLARE_GLOBAL_FUNCTION(setInterval) {
|
||||
*
|
||||
* @param id ID of the timeout event, returned by setInterval.
|
||||
*/
|
||||
DECLARE_GLOBAL_FUNCTION(clearInterval) {
|
||||
CHECK_ARGUMENT_COUNT(global, clearInterval, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS(global, clearInterval, 0, number);
|
||||
DECLARE_GLOBAL_FUNCTION (clearInterval)
|
||||
{
|
||||
CHECK_ARGUMENT_COUNT (global, clearInterval, (args_count == 1));
|
||||
CHECK_ARGUMENT_TYPE_ALWAYS (global, clearInterval, 0, number);
|
||||
|
||||
int id = int(jerry_get_number_value(args[0]));
|
||||
int id = int (jerry_value_as_number (args[0]));
|
||||
|
||||
mbed::js::EventLoop::getInstance().getQueue().cancel(id);
|
||||
mbed::js::EventLoop::getInstance ().getQueue ().cancel (id);
|
||||
|
||||
jerry_value_t global_obj = jerry_get_global_object();
|
||||
jerry_value_t prop_name = jerry_create_string((const jerry_char_t*)"setInterval");
|
||||
jerry_value_t func_obj = jerry_get_property(global_obj, prop_name);
|
||||
jerry_release_value(prop_name);
|
||||
jerry_value_t global_obj = jerry_current_realm ();
|
||||
jerry_value_t prop_name = jerry_string_sz ("setInterval");
|
||||
jerry_value_t func_obj = jerry_object_get (global_obj, prop_name);
|
||||
jerry_value_free (prop_name);
|
||||
|
||||
jerry_delete_property_by_index(func_obj, id);
|
||||
jerry_release_value(func_obj);
|
||||
jerry_release_value(global_obj);
|
||||
jerry_object_delete_index (func_obj, id);
|
||||
jerry_value_free (func_obj);
|
||||
jerry_value_free (global_obj);
|
||||
|
||||
return jerry_create_undefined();
|
||||
return jerry_undefined ();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user