IncludeJS 0.0.1
Build your own JavaScript runtime
 
Loading...
Searching...
No Matches
engine_function.h
1#ifndef INCLUDEJS_ENGINE_FUNCTION_H_
2#define INCLUDEJS_ENGINE_FUNCTION_H_
3
4#include "engine_export.h"
5
6#include <includejs/engine_context.h>
7#include <includejs/engine_value.h>
8
9#if defined(INCLUDEJS_ENGINE_JAVASCRIPT_CORE)
10#include <exception> // std::exception
11#include <vector> // std::vector
12#endif
13
14#ifdef DOXYGEN
16#define INCLUDEJS_ARGS
17#else
18#if __cplusplus >= 202002L
19#include <span> // std::span
20#define INCLUDEJS_ARGS std::span<includejs::Value>
21#else
22#define INCLUDEJS_ARGS const std::vector<includejs::Value> &
23#endif
24#endif
25
26#if defined(INCLUDEJS_ENGINE_JAVASCRIPT_CORE)
27namespace includejs {
28// This is a opaque function signature that can be force-casted into
29// JSObjectCallAsFunctionCallback
31using Function = const void *(*)(const void *, const void *, const void *,
32 const size_t, const void *[], const void **);
33} // namespace includejs
34#endif
35
36#if defined(INCLUDEJS_ENGINE_JAVASCRIPT_CORE)
37#define _INCLUDEJS_EXPOSE_FUNCTION_INTERNAL(function, call_as) \
38 static const void *function(const void *context, const void *, const void *, \
39 const size_t argc, const void *raw_arguments[], \
40 const void **exception) { \
41 std::vector<::includejs::Value> arguments; \
42 arguments.reserve(argc); \
43 for (std::size_t index = 0; index < argc; index++) { \
44 arguments.emplace_back(context, raw_arguments[index]); \
45 } \
46 try { \
47 return call_as({context}, arguments).native(); \
48 } catch (const std::exception &error) { \
49 const ::includejs::Context ignition_context{context}; \
50 *exception = ignition_context.make_error(error.what()).native(); \
51 return ignition_context.make_undefined().native(); \
52 } \
53 }
54#endif
55
56#ifdef DOXYGEN
58#define INCLUDEJS_EXPOSE_FUNCTION(function)
60#define INCLUDEJS_EXPOSE_TEMPLATE_FUNCTION(function)
61#else
62#define INCLUDEJS_EXPOSE_FUNCTION(function) \
63 _INCLUDEJS_EXPOSE_FUNCTION_INTERNAL(function, function)
64#define INCLUDEJS_EXPOSE_TEMPLATE_FUNCTION(function) \
65 template <typename... Args> \
66 _INCLUDEJS_EXPOSE_FUNCTION_INTERNAL(function, function<Args...>)
67#endif
68
69#endif