target: mbedos5: Add return value to setTimeout/setInterval and implement clearTimeout/clearInterval (#1457)

In the mbedos5 target the setTimeout and setInterval functions are not on spec, as they return 'undefined' instead of an event ID. Also clearTimeout and clearInterval are not implemented, so scheduled events cannot be canceled. This patch changes the behavior of the set* functions, and implements clear* functions.

JerryScript-DCO-1.0-Signed-off-by: Jan Jongboom janjongboom@gmail.com
This commit is contained in:
Jan Jongboom 2016-11-29 22:43:03 +08:00 committed by László Langó
parent 0467239d03
commit 9c803672ff
5 changed files with 43 additions and 2 deletions

View File

@ -28,6 +28,8 @@ DECLARE_JS_WRAPPER_REGISTRATION (base) {
REGISTER_GLOBAL_FUNCTION(gc);
REGISTER_GLOBAL_FUNCTION(setInterval);
REGISTER_GLOBAL_FUNCTION(setTimeout);
REGISTER_GLOBAL_FUNCTION(clearInterval);
REGISTER_GLOBAL_FUNCTION(clearTimeout);
REGISTER_CLASS_CONSTRUCTOR(DigitalOut);
REGISTER_CLASS_CONSTRUCTOR(I2C);
REGISTER_CLASS_CONSTRUCTOR(InterruptIn);

View File

@ -18,5 +18,6 @@
#include "jerryscript-mbed-library-registry/wrap_tools.h"
DECLARE_GLOBAL_FUNCTION(setInterval);
DECLARE_GLOBAL_FUNCTION(clearInterval);
#endif // _JERRYSCRIPT_MBED_DRIVERS_SET_INTERVAL_H

View File

@ -18,5 +18,6 @@
#include "jerryscript-mbed-library-registry/wrap_tools.h"
DECLARE_GLOBAL_FUNCTION(setTimeout);
DECLARE_GLOBAL_FUNCTION(clearTimeout);
#endif // _JERRYSCRIPT_MBED_DRIVERS_SET_TIMEOUT_H

View File

@ -31,6 +31,25 @@ DECLARE_GLOBAL_FUNCTION(setInterval) {
jerry_acquire_value(args[0]);
int interval = int(jerry_get_number_value(args[1]));
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_function, args[0], jerry_create_null(), (jerry_value_t*)NULL, 0);
return jerry_create_number(id);
}
/**
* clearInterval (native JavaScript function)
*
* Cancel an event that was previously scheduled via 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);
int id = int(jerry_get_number_value(args[0]));
mbed::js::EventLoop::getInstance().getQueue().cancel(id);
return jerry_create_undefined();
}

View File

@ -31,7 +31,25 @@ DECLARE_GLOBAL_FUNCTION(setTimeout) {
jerry_acquire_value(args[0]);
int interval = int(jerry_get_number_value(args[1]));
mbed::js::EventLoop::getInstance().getQueue().call_in(interval, jerry_call_function, args[0], jerry_create_null(), (jerry_value_t*)NULL, 0);
int id = mbed::js::EventLoop::getInstance().getQueue().call_in(interval, jerry_call_function, args[0], jerry_create_null(), (jerry_value_t*)NULL, 0);
return jerry_create_number(id);
}
/**
* clearTimeout (native JavaScript function)
*
* Cancel an event that was previously scheduled via setTimeout.
*
* @param id ID of the timeout event, returned by setTimeout.
*/
DECLARE_GLOBAL_FUNCTION(clearTimeout) {
CHECK_ARGUMENT_COUNT(global, clearTimeout, (args_count == 1));
CHECK_ARGUMENT_TYPE_ALWAYS(global, clearTimeout, 0, number);
int id = int(jerry_get_number_value(args[0]));
mbed::js::EventLoop::getInstance().getQueue().cancel(id);
return jerry_create_undefined();
}