• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright JS Foundation and other contributors, http://js.foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "jerryscript-mbed-drivers/setInterval-js.h"
16 #include "jerryscript-mbed-event-loop/EventLoop.h"
17 
18 /**
19  * setInterval (native JavaScript function)
20  *
21  * Call a JavaScript function at fixed intervals.
22  *
23  * @param function Function to call
24  * @param interval Time between function calls, in ms.
25  */
DECLARE_GLOBAL_FUNCTION(setInterval)26 DECLARE_GLOBAL_FUNCTION(setInterval) {
27     CHECK_ARGUMENT_COUNT(global, setInterval, (args_count == 2));
28     CHECK_ARGUMENT_TYPE_ALWAYS(global, setInterval, 0, function);
29     CHECK_ARGUMENT_TYPE_ALWAYS(global, setInterval, 1, number);
30 
31     int interval = int(jerry_get_number_value(args[1]));
32 
33     int id = mbed::js::EventLoop::getInstance().getQueue().call_every(interval, jerry_call_function, args[0], jerry_create_null(), (jerry_value_t*)NULL, 0);
34 
35     jerry_value_t result = jerry_set_property_by_index(function_obj_p, id, args[0]);
36 
37     if (jerry_value_is_error(result)) {
38         jerry_release_value(result);
39         mbed::js::EventLoop::getInstance().getQueue().cancel(id);
40 
41         return jerry_create_error(JERRY_ERROR_TYPE, (const jerry_char_t *) "Failed to run setInterval");
42     }
43 
44     jerry_release_value(result);
45     return jerry_create_number(id);
46 }
47 
48 /**
49  * clearInterval (native JavaScript function)
50  *
51  * Cancel an event that was previously scheduled via setInterval.
52  *
53  * @param id ID of the timeout event, returned by setInterval.
54  */
DECLARE_GLOBAL_FUNCTION(clearInterval)55 DECLARE_GLOBAL_FUNCTION(clearInterval) {
56     CHECK_ARGUMENT_COUNT(global, clearInterval, (args_count == 1));
57     CHECK_ARGUMENT_TYPE_ALWAYS(global, clearInterval, 0, number);
58 
59     int id = int(jerry_get_number_value(args[0]));
60 
61     mbed::js::EventLoop::getInstance().getQueue().cancel(id);
62 
63     jerry_value_t global_obj = jerry_get_global_object();
64     jerry_value_t prop_name = jerry_create_string((const jerry_char_t*)"setInterval");
65     jerry_value_t func_obj = jerry_get_property(global_obj, prop_name);
66     jerry_release_value(prop_name);
67 
68     jerry_delete_property_by_index(func_obj, id);
69     jerry_release_value(func_obj);
70     jerry_release_value(global_obj);
71 
72     return jerry_create_undefined();
73 }
74