Ocean
Loading...
Searching...
No Matches
JSContext.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_INTERACTION_JS_CONTEXT_H
9#define META_OCEAN_INTERACTION_JS_CONTEXT_H
10
13
14#include "ocean/base/Caller.h"
16
17#include "ocean/io/File.h"
18
20
21#include <v8.h>
22
23namespace Ocean
24{
25
26namespace Interaction
27{
28
29namespace JavaScript
30{
31
32/**
33 * This class implements a wrapper for a JavaScript context.
34 * @ingroup interactionjs
35 */
36class OCEAN_INTERACTION_JS_EXPORT JSContext
37{
38 public:
39
40 /**
41 * Definition of a vector holding JavaScript code objects.
42 */
43 typedef std::vector<std::unique_ptr<JSScript>> Scripts;
44
45 /**
46 * Definition of a vector holding value handles.
47 */
48 typedef std::vector<v8::Handle<v8::Value>> Values;
49
50 /**
51 * Definition of a caller object returning JavaScript parameters.
52 */
54
55 protected:
56
57 /**
58 * Defines a pair holding function names and function parameters.
59 */
60 typedef std::pair<std::string, FunctionParameterCaller> FunctionPair;
61
62 /**
63 * Definition of a vector holding function pairs.
64 */
65 typedef std::vector<FunctionPair> FunctionPairs;
66
67 public:
68
69 /**
70 * Creates a new context object.
71 * @see initialize().
72 */
73 JSContext() = default;
74
75 /**
76 * Destructs a context object.
77 */
79
80 /**
81 * Initializes this context.
82 * This function must be called after the context has been constructed.
83 * @param thisContext The shared pointer to this context, must be valid
84 * @param globalTemplate The global template object holding the definition of all wrapper classes and global objects not part of java script natively
85 */
86 void initialize(const std::shared_ptr<JSContext>& thisContext, const v8::Local<v8::ObjectTemplate>& globalTemplate);
87
88 /**
89 * Returns the JavaScript context object.
90 * @return Context object
91 */
92 inline v8::Local<v8::Context> context() const;
93
94 /**
95 * Returns a corresponding filenames of the scripts managed by this context object
96 * @return Names of all script files
97 */
98 inline const std::vector<std::string>& filenames() const;
99
100 /**
101 * Adds a new script to this context.
102 * @param code Code of the new script
103 * @return True, if succeeded to compile and to add the code
104 */
105 bool addScriptCode(const std::string& code);
106
107 /**
108 * Adds a new script to this context.
109 * @param filename Name of the script file to add
110 * @return True, if succeeded to compile and to add the code
111 */
112 bool addScriptFile(const std::string& filename);
113
114 /**
115 * Initialize function.
116 * @param engine Current rendering engine
117 * @param timestamp Recent initialization timestamp
118 */
119 void onInitialize(const Rendering::EngineRef& engine, const Timestamp timestamp);
120
121 /**
122 * Release function.
123 * @param engine Current rendering engine
124 * @param timestamp Recent release timestamp
125 */
126 void onRelease(const Rendering::EngineRef& engine, const Timestamp timestamp);
127
128 /**
129 * Calls a given JavaScript function.
130 * @param function JavaScript function to call
131 * @param returnValue Optional JavaScript function return value
132 * @param parameters Optional function parameters
133 * @param errorMessage Optional resulting error message
134 * @return True, if succeeded
135 */
136 bool callFunction(v8::Local<v8::Function> function, v8::Local<v8::Value>& returnValue, Values& parameters, std::string& errorMessage);
137
138 /**
139 * Executes a spcific script function.
140 * @param function Name of the function to execute
141 * @param returnValue Resulting return value, if any
142 * @param parameters Optional function parameter
143 * @param errorMessage Resulting error message, if any
144 * @return True, if succeeded
145 */
146 bool callFunction(const std::string& function, v8::Local<v8::Value>& returnValue, Values& parameters, std::string& errorMessage);
147
148 /**
149 * Adds a new function to the queue of explicit functions.
150 * @param function Name of the function to be executed
151 * @param parameterCreator Caller function creating the parameters
152 */
153 void addExplicitFunctionCall(const std::string& function, const FunctionParameterCaller& parameterCreator);
154
155 /**
156 * Resolves the entire path of a given file.
157 * @param file File to resolve
158 * @return All possible resolved files
159 */
161
162 /**
163 * Resolves the entire path of a given file to the first unique existing file
164 * @param file File to resolve
165 * @param resolved Path of the unique existing file
166 * @return True, if the file could be resolved
167 */
168 bool resolveFile(const IO::File& file, std::string& resolved);
169
170 /**
171 * Pre file load interaction function.
172 * @see JSLibrary::preFileLoad().
173 */
174 void preFileLoad(const std::string& filename);
175
176 /**
177 * Post file load interaction function.
178 * @see JSLibrary::postFileLoad().
179 */
180 void postFileLoad(const std::string& filename, const bool succeeded);
181
182 /**
183 * Pre update interaction function.
184 * The returned timestamp is used for the next JavaScript interaction file.
185 * @see JSLibrary::preUpdate().
186 */
187 Timestamp preUpdate(const Rendering::EngineRef& engine, const Timestamp timestamp);
188
189 /**
190 * Post update interaction function.
191 * @see JSLibrary::postUpdate().
192 */
193 void postUpdate(const Rendering::EngineRef& engine, const Timestamp timestamp);
194
195 /**
196 * Mouse press event function.
197 * @see JSLibrary::onMousePress().
198 */
199 void onMousePress(const Rendering::EngineRef& engine, const std::string& button, const Vector2& screenPosition, const Line3& ray, const std::string& pickedObject, const Vector3& pickedPosition, const Timestamp timestamp);
200
201 /**
202 * Mouse move event function.
203 * @see JSLibrary::onMouseMove().
204 */
205 void onMouseMove(const Rendering::EngineRef& engine, const std::string& button, const Vector2& screenPosition, const Line3& ray, const std::string& pickedObject, const Vector3& pickedPosition, const Timestamp timestamp);
206
207 /**
208 * Mouse release event function.
209 * @see JSLibrary::onMouseRelease().
210 */
211 void onMouseRelease(const Rendering::EngineRef& engine, const std::string& button, const Vector2& screenPosition, const Line3& ray, const std::string& pickedObject, const Vector3& pickedPosition, const Timestamp timestamp);
212
213 /**
214 * Key press function.
215 * @see JSLibrary::onKeyPress().
216 */
217 void onKeyPress(const Rendering::EngineRef& engine, const std::string& key, const Timestamp timestamp);
218
219 /**
220 * Key release function.
221 * @see JSLibrary::onKeyRelease().
222 */
223 void onKeyRelease(const Rendering::EngineRef& engine, const std::string& key, const Timestamp timestamp);
224
225 /**
226 * Returns the current JavaScript context object.
227 * @return JavaScript current context
228 */
229 static inline const v8::Local<v8::Context>& currentContext();
230
231 /**
232 * Returns the current JSContext object.
233 * @return The current JSContext object
234 */
235 static inline std::shared_ptr<JSContext> currentJSContext();
236
237 protected:
238
239 /**
240 * Disabled copy constructor.
241 * @param context Object which would be copied
242 */
243 JSContext(const JSContext& context) = delete;
244
245 /**
246 * Makes this context the current context.
247 */
249
250 /**
251 * Returns whether the script contains a specific function.
252 * @param function Name of the function to check
253 * @return True, if so
254 */
255 bool hasFunction(const std::string& function) const;
256
257 /**
258 * Returns whether the script contains a specific global object.
259 * @param object Name of the object to check
260 * @return True, if so
261 */
262 bool hasObject(const std::string& object) const;
263
264 /**
265 * Disabled copy operator.
266 * @param context Object which would be copied
267 * @return Reference to this object
268 */
269 JSContext& operator=(const JSContext& context) = delete;
270
271 protected:
272
273 /// Script code objects.
275
276 /// JavaScript context object handle.
277 v8::UniquePersistent<v8::Context> context_;
278
279 /// The reference to this object.
280 std::weak_ptr<JSContext> thisJSContext_;
281
282 /// Name of the file the initial script is defined inside.
283 std::vector<std::string> filenames_;
284
285 /// True, if the context contains a preUpdate function.
286 bool hasPreUpdateFunction_ = false;
287
288 /// True, if the context contains a postUpdate function.
289 bool hasPostUpdateFunction_ = false;
290
291 /// True, if the context contains a mousePress event function.
292 bool hasMousePressEventFunction_ = false;
293
294 /// True, if the context contains a mouseMove event function.
295 bool hasMouseMoveEventFunction_ = false;
296
297 /// True, if the context contains a mouseRelease event function.
298 bool hasMouseReleaseEventFunction_ = false;
299
300 /// True, if the context contains a keyPress event function.
301 bool hasKeyPressFunction_ = false;
302
303 /// True, if the context contains a keyRelease event function.
304 bool hasKeyReleaseFunction_ = false;
305
306 /// Functions to be called in the next update step explicitly.
308
309 /// The context's lock.
311
312 /// The current JavaScript context.
313 static v8::Local<v8::Context> currentContext_;
314
315 /// The current context object.
316 static std::weak_ptr<JSContext> currentJSContext_;
317
318 /// Global counter for context objects interested in mouse events.
319 static unsigned int mouseEventCounter_;
320};
321
322inline v8::Local<v8::Context> JSContext::context() const
323{
324 return context_.Get(v8::Isolate::GetCurrent());
325}
326
327inline std::shared_ptr<JSContext> JSContext::currentJSContext()
328{
329 return currentJSContext_.lock();
330}
331
332inline const std::vector<std::string>& JSContext::filenames() const
333{
334 return filenames_;
335}
336
337inline const v8::Local<v8::Context>& JSContext::currentContext()
338{
339 return currentContext_;
340}
341
342}
343
344}
345
346}
347
348#endif // META_OCEAN_INTERACTION_JS_CONTEXT_H
This class implements a callback function container using defined function parameters.
Definition Caller.h:1565
This class holds a file.
Definition File.h:36
This class implements a wrapper for a JavaScript context.
Definition JSContext.h:37
void onMouseMove(const Rendering::EngineRef &engine, const std::string &button, const Vector2 &screenPosition, const Line3 &ray, const std::string &pickedObject, const Vector3 &pickedPosition, const Timestamp timestamp)
Mouse move event function.
IO::Files resolveFile(const IO::File &file)
Resolves the entire path of a given file.
void postUpdate(const Rendering::EngineRef &engine, const Timestamp timestamp)
Post update interaction function.
bool hasFunction(const std::string &function) const
Returns whether the script contains a specific function.
void onInitialize(const Rendering::EngineRef &engine, const Timestamp timestamp)
Initialize function.
Lock lock_
The context's lock.
Definition JSContext.h:310
bool addScriptCode(const std::string &code)
Adds a new script to this context.
Scripts scripts_
Script code objects.
Definition JSContext.h:274
void initialize(const std::shared_ptr< JSContext > &thisContext, const v8::Local< v8::ObjectTemplate > &globalTemplate)
Initializes this context.
Caller< Values > FunctionParameterCaller
Definition of a caller object returning JavaScript parameters.
Definition JSContext.h:53
static const v8::Local< v8::Context > & currentContext()
Returns the current JavaScript context object.
Definition JSContext.h:337
std::vector< FunctionPair > FunctionPairs
Definition of a vector holding function pairs.
Definition JSContext.h:65
JSContext & operator=(const JSContext &context)=delete
Disabled copy operator.
static v8::Local< v8::Context > currentContext_
The current JavaScript context.
Definition JSContext.h:313
JSContext()=default
Creates a new context object.
void preFileLoad(const std::string &filename)
Pre file load interaction function.
JSContext(const JSContext &context)=delete
Disabled copy constructor.
Timestamp preUpdate(const Rendering::EngineRef &engine, const Timestamp timestamp)
Pre update interaction function.
std::vector< std::unique_ptr< JSScript > > Scripts
Definition of a vector holding JavaScript code objects.
Definition JSContext.h:43
void makeCurrent()
Makes this context the current context.
v8::UniquePersistent< v8::Context > context_
JavaScript context object handle.
Definition JSContext.h:277
bool hasObject(const std::string &object) const
Returns whether the script contains a specific global object.
bool callFunction(v8::Local< v8::Function > function, v8::Local< v8::Value > &returnValue, Values &parameters, std::string &errorMessage)
Calls a given JavaScript function.
bool addScriptFile(const std::string &filename)
Adds a new script to this context.
static std::weak_ptr< JSContext > currentJSContext_
The current context object.
Definition JSContext.h:316
static unsigned int mouseEventCounter_
Global counter for context objects interested in mouse events.
Definition JSContext.h:319
v8::Local< v8::Context > context() const
Returns the JavaScript context object.
Definition JSContext.h:322
void addExplicitFunctionCall(const std::string &function, const FunctionParameterCaller &parameterCreator)
Adds a new function to the queue of explicit functions.
std::weak_ptr< JSContext > thisJSContext_
The reference to this object.
Definition JSContext.h:280
static std::shared_ptr< JSContext > currentJSContext()
Returns the current JSContext object.
Definition JSContext.h:327
std::pair< std::string, FunctionParameterCaller > FunctionPair
Defines a pair holding function names and function parameters.
Definition JSContext.h:60
FunctionPairs explicitFunctions_
Functions to be called in the next update step explicitly.
Definition JSContext.h:307
bool resolveFile(const IO::File &file, std::string &resolved)
Resolves the entire path of a given file to the first unique existing file.
void onKeyRelease(const Rendering::EngineRef &engine, const std::string &key, const Timestamp timestamp)
Key release function.
void onRelease(const Rendering::EngineRef &engine, const Timestamp timestamp)
Release function.
std::vector< v8::Handle< v8::Value > > Values
Definition of a vector holding value handles.
Definition JSContext.h:48
bool callFunction(const std::string &function, v8::Local< v8::Value > &returnValue, Values &parameters, std::string &errorMessage)
Executes a spcific script function.
void onMousePress(const Rendering::EngineRef &engine, const std::string &button, const Vector2 &screenPosition, const Line3 &ray, const std::string &pickedObject, const Vector3 &pickedPosition, const Timestamp timestamp)
Mouse press event function.
~JSContext()
Destructs a context object.
void onKeyPress(const Rendering::EngineRef &engine, const std::string &key, const Timestamp timestamp)
Key press function.
void onMouseRelease(const Rendering::EngineRef &engine, const std::string &button, const Vector2 &screenPosition, const Line3 &ray, const std::string &pickedObject, const Vector3 &pickedPosition, const Timestamp timestamp)
Mouse release event function.
const std::vector< std::string > & filenames() const
Returns a corresponding filenames of the scripts managed by this context object.
Definition JSContext.h:332
std::vector< std::string > filenames_
Name of the file the initial script is defined inside.
Definition JSContext.h:283
void postFileLoad(const std::string &filename, const bool succeeded)
Post file load interaction function.
This class implements an infinite line in 3D space.
Definition Line3.h:68
This class implements a recursive lock object.
Definition Lock.h:31
This class implements a timestamp.
Definition Timestamp.h:36
std::vector< File > Files
Definition of a vector holding files.
Definition File.h:29
The namespace covering the entire Ocean framework.
Definition Accessor.h:15