jerryscript/src/include/Jerryscript/Function.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  * Function.h
8  *
9  * @author: Dec 2021 - Mikee47 <mike@sillyhouse.net>
10  *
11  */
12 
13 #pragma once
14 
15 #include "Types.h"
16 
17 namespace JS
18 {
19 using namespace Jerryscript;
20 }
21 
22 // get number of arguments with JS_NARG
23 // https://newbedev.com/overloading-macro-on-number-of-arguments
24 // https://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html
25 #define JS_NARG(...) JS_NARG_I(_0 __VA_OPT__(, ) __VA_ARGS__, JS_RSEQ_N)
26 #define JS_NARG_I(...) JS_ARG_N(__VA_ARGS__)
27 #define JS_ARG_N(_0, _1, _2, _3, _4, _5, _6, _7, _8, N, ...) N
28 #define JS_RSEQ_N 8, 7, 6, 5, 4, 3, 2, 1, 0
29 static_assert(JS_NARG() == 0 && JS_NARG(!, !) == 2, "JS_NARG broken");
30 
31 #define JS_CONCAT_(x, y) x##y
32 #define JS_CONCAT(x, y) JS_CONCAT_(x, y)
33 
34 #define JS_ARGS_0
35 #define JS_ARGS_1 JS_ARGS_0, &args[0]
36 #define JS_ARGS_2 JS_ARGS_1, &args[1]
37 #define JS_ARGS_3 JS_ARGS_2, &args[2]
38 #define JS_ARGS_4 JS_ARGS_3, &args[3]
39 #define JS_ARGS_5 JS_ARGS_4, &args[4]
40 #define JS_ARGS_6 JS_ARGS_5, &args[5]
41 #define JS_ARGS_7 JS_ARGS_6, &args[6]
42 #define JS_ARGS_8 JS_ARGS_7, &args[7]
43 
72 #define JS_DEFINE_FUNCTION(func, ...) \
73  JS::Value js_##func(const JS::CallInfo& callInfo, ##__VA_ARGS__); \
74  jerry_value_t func(const jerry_call_info_t* call_info_p, const jerry_value_t args[], \
75  const jerry_length_t args_count) \
76  { \
77  using Function = JS::Value (*)(const jerry_call_info_t*, ...); \
78  if(JS_NARG(__VA_ARGS__) != args_count) { \
79  return JS::create_arg_count_error(__FUNCTION__); \
80  } \
81  auto res = Function(js_##func)(call_info_p JS_CONCAT(JS_ARGS_, JS_NARG(__VA_ARGS__))); \
82  return res.release(); \
83  } \
84  JS::Value js_##func(const Jerryscript::CallInfo& callInfo, ##__VA_ARGS__)
85 
103 #define JS_DEFINE_FUNCTION_VAR(func) \
104  JS::Value js_##func(const JS::CallInfo& callInfo, JS::Value[], unsigned); \
105  jerry_value_t func(const jerry_call_info_t* call_info_p, const jerry_value_t args[], \
106  const jerry_length_t args_count) \
107  { \
108  using Function = JS::Value (*)(const jerry_call_info_t*, JS::Value[], unsigned); \
109  auto res = Function(js_##func)(call_info_p, (JS::Value*)args, args_count); \
110  return res.release(); \
111  } \
112  JS::Value js_##func(const JS::CallInfo& callInfo, JS::Value args[], unsigned argCount)
113 
120 #define JS_DECLARE_FUNCTION(func) \
121  jerry_value_t func(const jerry_call_info_t*, const jerry_value_t[], const jerry_length_t);
122 
126 namespace Jerryscript
127 {
131 struct CallInfo {
132  Value function;
135 };
136 
137 // Internal helper function used by JS_DEFINE_FUNCTION
138 jerry_value_t create_arg_count_error(const char* functionName);
139 
140 } // namespace Jerryscript
Represents a Jerryscript value.
Definition: Libraries/jerryscript/src/include/Jerryscript/Types.h:153
#define JS_NARG(...)
Definition: jerryscript/src/include/Jerryscript/Function.h:25
Definition: jerryscript/src/include/Jerryscript/Function.h:18
Definition: Libraries/jerryscript/src/include/Jerryscript/Context.h:124
jerry_value_t create_arg_count_error(const char *functionName)
Maps directly onto jerry_call_info_t structure.
Definition: jerryscript/src/include/Jerryscript/Function.h:131
Value this_value
Definition: jerryscript/src/include/Jerryscript/Function.h:133
Value new_target
Definition: jerryscript/src/include/Jerryscript/Function.h:134