Libraries/jerryscript/src/include/Jerryscript/Context.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * Context.h - Support for external contexts
8  *
9  * @author: Dec 2021 - Mikee47 <mike@sillyhouse.net>
10  *
11  */
12 
13 #pragma once
14 
15 #include "Function.h"
16 #include <Data/LinkedObjectList.h>
17 #include <Platform/System.h>
18 #include <memory>
19 
59 #define JS_DEFINE_METHOD(method, ...) \
60  static jerry_value_t method(const jerry_call_info_t* call_info_p, const jerry_value_t args[], \
61  const jerry_length_t args_count) \
62  { \
63  using Method = JS::Value (ContextClass::*)(const jerry_call_info_t*, ...); \
64  if(JS_NARG(__VA_ARGS__) != args_count) { \
65  return JS::create_arg_count_error(__FUNCTION__); \
66  } \
67  auto& ctx = getCurrent(); \
68  auto m = reinterpret_cast<Method>(&ContextClass::js_##method); \
69  auto res = (ctx.*m)(call_info_p JS_CONCAT(JS_ARGS_, JS_NARG(__VA_ARGS__))); \
70  return res.release(); \
71  } \
72  Jerryscript::Value js_##method(const JS::CallInfo& callInfo, ##__VA_ARGS__)
73 
108 #define JS_DEFINE_METHOD_VAR(method) \
109  static jerry_value_t method(const jerry_call_info_t* call_info_p, const jerry_value_t args[], \
110  const jerry_length_t args_count) \
111  { \
112  using Method = JS::Value (ContextClass::*)(const jerry_call_info_t*, JS::Value[], unsigned); \
113  auto& ctx = getCurrent(); \
114  auto m = reinterpret_cast<Method>(&ContextClass::js_##method); \
115  auto res = (ctx.*m)(call_info_p, (JS::Value*)args, args_count); \
116  return res.release(); \
117  } \
118  Jerryscript::Value js_##method(const Jerryscript::CallInfo& callInfo, Jerryscript::Value args[], unsigned argCount)
119 
123 namespace Jerryscript
124 {
131 class Context
132 {
133 public:
138 
143  Context(size_t heapSize);
144 
146  {
147  if(current == this) {
148  current = nullptr;
149  }
150  }
151 
152  explicit operator bool() const
153  {
154  return bool(context);
155  }
156 
162  void select()
163  {
164  current = this;
165  }
166 
167  operator jerry_context_t*() const
168  {
169  return reinterpret_cast<jerry_context_t*>(context.get());
170  }
171 
172  static Context& getCurrent()
173  {
174  assert(current != nullptr);
175  return *current;
176  }
177 
178 private:
179  static void* alloc(size_t size, void* param);
180 
181  std::unique_ptr<uint8_t[]> context;
182  static Context* current;
183 };
184 
188 template <class ClassType> class ContextTemplate : public LinkedObjectTemplate<ClassType>, public Context
189 {
190 public:
191  using ContextClass = ClassType;
192 
193  using Context::Context;
194 
195  static ClassType& getCurrent()
196  {
197  return static_cast<ClassType&>(Context::getCurrent());
198  }
199 };
200 
204 template <class ClassType> class ContextList : public OwnedLinkedObjectListTemplate<ClassType>
205 {
206 public:
207  using Callback = Delegate<void(ClassType& ctx)>;
208 
236  void foreach(Callback callback)
237  {
238  runNext(this->head(), callback);
239  }
240 
241 private:
242  static void runNext(ClassType* ctx, Callback callback)
243  {
244  if(ctx == nullptr) {
245  return;
246  }
247  System.queueCallback([ctx, callback]() {
248  callback(*ctx);
249  runNext(ctx->getNext(), callback);
250  });
251  }
252 };
253 
254 } // namespace Jerryscript
Definition: Delegate.h:20
Manages a list of contexts.
Definition: Libraries/jerryscript/src/include/Jerryscript/Context.h:205
Delegate< void(ClassType &ctx)> Callback
Definition: Libraries/jerryscript/src/include/Jerryscript/Context.h:207
Implement a custom Context class.
Definition: Libraries/jerryscript/src/include/Jerryscript/Context.h:189
static ClassType & getCurrent()
Definition: Libraries/jerryscript/src/include/Jerryscript/Context.h:195
ClassType ContextClass
Definition: Libraries/jerryscript/src/include/Jerryscript/Context.h:191
Jerryscript external context.
Definition: Libraries/jerryscript/src/include/Jerryscript/Context.h:132
Context()
Create a context using the default JERRY_GLOBAL_HEAP_SIZE setting.
~Context()
Definition: Libraries/jerryscript/src/include/Jerryscript/Context.h:145
void select()
Make this the current context.
Definition: Libraries/jerryscript/src/include/Jerryscript/Context.h:162
static Context & getCurrent()
Definition: Libraries/jerryscript/src/include/Jerryscript/Context.h:172
Context(size_t heapSize)
Create a context with custom heap size.
ObjectType * head()
Definition: LinkedObjectList.h:104
Base class template for linked items with type casting.
Definition: LinkedObject.h:62
Class template for singly-linked list of objects.
Definition: LinkedObjectList.h:175
static bool queueCallback(TaskCallback32 callback, uint32_t param=0)
Queue a deferred callback.
SystemClass System
Global instance of system object.
Definition: Libraries/jerryscript/src/include/Jerryscript/Context.h:124