Oxygen Engine
Modern C++ 3D Engine using OpenGL
Loading...
Searching...
No Matches
javascript.h
1#ifndef OE_EXT_JAVASCRIPT_H
2#define OE_EXT_JAVASCRIPT_H
3
4#include <OxygenEngine/lib/json.hpp>
5#include <OxygenEngine/io/logger.h>
6
18namespace oe::ext::js
19{
24 {
25 std::string name;
26 duk_c_function callback;
27 duk_idx_t callback_args_count = 0;
28 };
29
93 struct Runtime
94 {
95 Runtime()
96 {
97 ctx = duk_create_heap_default();
98 }
99
100 ~Runtime()
101 {
102 duk_destroy_heap(ctx);
103 }
104
110 void addClass(CallableEntity class_info, const std::vector<CallableEntity>& methods)
111 {
112 duk_push_c_function(ctx, class_info.callback, class_info.callback_args_count);
113
114 duk_push_object(ctx);
115
116 for (auto&& it : methods)
117 {
118 duk_push_c_function(ctx, it.callback, it.callback_args_count);
119 duk_put_prop_string(ctx, -2, it.name.c_str());
120 }
121
122 duk_put_prop_string(ctx, -2, "prototype");
123
124 duk_put_global_string(ctx, class_info.name.c_str());
125 }
126
130 void addFunction(CallableEntity function_info)
131 {
132 duk_push_c_function(ctx, function_info.callback, function_info.callback_args_count);
133 duk_put_global_string(ctx, function_info.name.c_str());
134 }
135
145 static void pushJson(duk_context* ctx, const nlohmann::json& data, bool is_object = true)
146 {
147 auto obj_idx = is_object
148 ? duk_push_object(ctx)
149 : duk_push_array(ctx)
150 ;
151
152 for (auto& it : data.items())
153 {
154 duk_push_string(ctx, it.key().c_str());
155
156 const auto& value = it.value();
157
158 if (value.is_object())
159 {
160 pushJson(ctx, value);
161 }
162 else if (value.is_array())
163 {
164 pushJson(ctx, value, false);
165 }
166 else if (value.is_string())
167 {
168 duk_push_string(ctx, value.get<std::string>().c_str());
169 }
170 else if (value.is_boolean())
171 {
172 duk_push_boolean(ctx, value.get<bool>());
173 }
174 else if (value.is_number_integer())
175 {
176 duk_push_int(ctx, value.get<int32_t>());
177 }
178 else if (value.is_number_float())
179 {
180 duk_push_number(ctx, value.get<float>());
181 }
182 else
183 {
184 duk_push_null(ctx);
185 }
186
187 duk_put_prop(ctx, obj_idx);
188 }
189 }
190
196 static nlohmann::json getJson(duk_context *ctx, int index)
197 {
198 return nlohmann::json::parse(duk_json_encode(ctx, index));
199 }
200
206 void eval(const char* data)
207 {
208 if (duk_peval_string(ctx, data) != 0)
209 {
210 oe::log.critical() << "Error: " << duk_safe_to_string(ctx, -1);
211 }
212 duk_pop(ctx);
213 }
214
215 duk_context* ctx;
216 };
217}
218
219#endif
LoggerStream critical(const LogVerbosity verbosity=default_log_verbosity)
Prepare and return a logger stream to receive criticals logs.
Definition logger.h:205
Embedded JavaScript runtime using Duktape.
Definition javascript.h:19
io::Logger & log
Global logger instance.
JavaScript callable entity (can be either a class or a function)
Definition javascript.h:24
duk_c_function callback
Callback of the entity (For classes, this means the constructor)
Definition javascript.h:26
std::string name
Name of the entity.
Definition javascript.h:25
duk_idx_t callback_args_count
Number of arguments of the callback.
Definition javascript.h:27
JavaScript runtime.
Definition javascript.h:94
static void pushJson(duk_context *ctx, const nlohmann::json &data, bool is_object=true)
push a nlhomann::json object as a json to the runtime
Definition javascript.h:145
void addFunction(CallableEntity function_info)
add a C function available in in JavaScript code
Definition javascript.h:130
void eval(const char *data)
Evaluate a script in the current runtime.
Definition javascript.h:206
static nlohmann::json getJson(duk_context *ctx, int index)
pull a nlhomann::json object from a parameter
Definition javascript.h:196
void addClass(CallableEntity class_info, const std::vector< CallableEntity > &methods)
Add a class type available in JavaScript code.
Definition javascript.h:110