• 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 #ifndef _JERRYSCRIPT_MBED_EVENT_LOOP_EVENT_LOOP_H
16 #define _JERRYSCRIPT_MBED_EVENT_LOOP_EVENT_LOOP_H
17 
18 #include <vector>
19 
20 #include "jerry-core/include/jerryscript.h"
21 
22 #include "Callback.h"
23 #include "mbed_assert.h"
24 
25 #include "events/EventQueue.h"
26 
27 #include "jerryscript-mbed-util/logging.h"
28 #include "jerryscript-mbed-event-loop/BoundCallback.h"
29 
30 extern "C" void exit(int return_code);
31 
32 namespace mbed {
33 namespace js {
34 
35 static const uint32_t EVENT_INTERVAL_MS = 1;
36 
37 class EventLoop {
38  private:
39     static EventLoop instance;
40 
41  public:
getInstance()42     static EventLoop& getInstance() {
43         return instance;
44     }
45 
go()46     void go() {
47         while (true) {
48             queue.dispatch();
49         }
50     }
51 
wrapFunction(jerry_value_t f)52     Callback<void()> wrapFunction(jerry_value_t f) {
53         MBED_ASSERT(jerry_value_is_function(f));
54 
55         // we need to return a callback that'll schedule this
56         Callback<void(uint32_t)> cb_raw(this, &EventLoop::callback);
57         BoundCallback<void(uint32_t)> *cb = new BoundCallback<void(uint32_t)>(cb_raw, f);
58 
59         bound_callbacks.push_back(std::make_pair(f, cb));
60 
61         return *cb;
62     }
63 
dropCallback(jerry_value_t f)64     void dropCallback(jerry_value_t f) {
65         for (std::vector<std::pair<jerry_value_t, BoundCallback<void(uint32_t)>*> >::iterator it = bound_callbacks.begin(); it != bound_callbacks.end(); it++) {
66             std::pair<jerry_value_t, BoundCallback<void(uint32_t)>*> element = *it;
67 
68             if (element.first == f) {
69                 delete element.second;
70                 break;
71             }
72         }
73     }
74 
callback(jerry_value_t f)75     void callback(jerry_value_t f) {
76         queue.call(jerry_call_function, f, jerry_create_null(), (const jerry_value_t*)NULL, 0);
77     }
78 
nativeCallback(Callback<void ()> cb)79     void nativeCallback(Callback<void()> cb) {
80         queue.call(cb);
81     }
82 
getQueue()83     events::EventQueue& getQueue() {
84         return queue;
85     }
86 
87  private:
EventLoop()88     EventLoop() {}
89 
90     std::vector<std::pair<jerry_value_t, BoundCallback<void(uint32_t)>*> > bound_callbacks;
91     events::EventQueue queue;
92 };
93 
94 void event_loop();
95 
96 }  // namespace js
97 }  // namespace mbed
98 
99 #endif  // _JERRYSCRIPT_MBED_EVENT_LOOP_EVENT_LOOP_H
100