Ocean
Loading...
Searching...
No Matches
JSExternal.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_EXTERNAL_H
9#define META_OCEAN_INTERACTION_JS_EXTERNAL_H
10
13
15
16#include "ocean/math/Euler.h"
18#include "ocean/math/Line3.h"
19#include "ocean/math/Plane3.h"
22#include "ocean/math/Rotation.h"
25#include "ocean/math/Vector2.h"
26#include "ocean/math/Vector3.h"
27#include "ocean/math/Vector4.h"
28
30
33
35
36namespace Ocean
37{
38
39namespace Interaction
40{
41
42namespace JavaScript
43{
44
45/**
46 * This class implements a type-safe object wrapper for non-JavaScript objects.
47 * @ingroup interactionjs
48 */
49class OCEAN_INTERACTION_JS_EXPORT JSExternal
50{
51 public:
52
53 /**
54 * Definition of different external object types.
55 */
56 enum Type
57 {
58 /// Undefined object.
60 /// Color object.
62 /// Device object object.
64 /// Device sample object.
66 /// Euler object.
68 /// HomogenousMatrix4 object.
70 /// Line3 object.
72 /// Media object object.
74 /// Plane3 object.
76 /// Quaternion object.
78 /// Rendering engine object.
80 /// Rendering object object.
82 /// Rotation object.
84 /// Scene description node.
86 /// SquareMatrix3 object.
88 /// SquareMatrix4 object.
90 /// Vector2 object.
92 /// Vector3 object.
94 /// Vector4 object.
95 TYPE_VECTOR_4
96 };
97
98 public:
99
100 /**
101 * Disabled copy constructor.
102 */
103 JSExternal(const JSExternal&) = delete;
104
105 /**
106 * Returns the type of the wrapped C++ object.
107 * @return The wrapped type
108 */
109 inline Type type() const;
110
111 /**
112 * Returns the wrapped C++ object.
113 * @return The reference to the C++ object which can be used to change the value of the object
114 * @tparam TNative The data type of the wrapped native C++ object
115 */
116 template <typename TNative>
117 inline TNative& value();
118
119 /**
120 * Returns the type of a C++ object.
121 * @return The type associated with a native C++ object
122 * @tparam TNative The data type of the wrapped native C++ object
123 */
124 template <typename TNative>
125 static inline Type type();
126
127 /**
128 * Returns the external object from a given JavaScript object.
129 * @param value The JavaScript value for which the external object will be returned
130 * @return The resulting external object, otherwise nullptr
131 */
132 static JSExternal* external(const v8::Local<v8::Value>& value);
133
134 /**
135 * Returns the external object from a given JavaScript object.
136 * @param object The JavaScript object for which the external object will be returned
137 * @return The resulting external object, otherwise nullptr
138 */
139 static JSExternal* external(const v8::Local<v8::Object>& object);
140
141 /**
142 * Sets or changes the native C++ object of an external JavaScript object.
143 * @param object The external JavaScript object for which the native vlaue will be set or changed, must be valid
144 * @param value The native value to be set, will be moved
145 * @tparam TNative The data type of the native C++ object
146 */
147 template <typename TNative>
148 static inline void setValue(v8::Local<v8::Object>& object, TNative&& value);
149
150 /**
151 * Sets or changes the native C++ object of an external JavaScript object.
152 * @param object The external JavaScript object for which the native vlaue will be set or changed, must be valid
153 * @param value The native value to be set
154 * @tparam TNative The data type of the native C++ object
155 */
156 template <typename TNative>
157 static inline void setValue(v8::Local<v8::Object>& object, const TNative& value);
158
159 /**
160 * Creates a new type-safe wrapper object for a specific native C++ object.
161 * @param data The pointer to the C++ object to be wrapped, must be valid
162 * @param owner The owner of this new JSExternal object
163 * @param isolate The current isolate
164 * @tparam TNative The data type of the native C++ object to be wrapped
165 */
166 template <typename TNative>
167 static JSExternal* create(TNative* data, v8::Local<v8::Object>& owner, v8::Isolate* isolate);
168
169 protected:
170
171 /**
172 * Creates a new type-safe wrapper object for a specific native C++ object.
173 * @param data The pointer to the C++ object to be wrapped, must be valid
174 * @param type The type of the C++ object, must be valid
175 * @param owner The owner of this new JSExternal object
176 * @param isolate The current isolate
177 */
178 JSExternal(void* data, const Type type, v8::Local<v8::Object>& owner, v8::Isolate* isolate);
179
180 /**
181 * Destructs this object.
182 */
184
185 /**
186 * The callback function which will be called from v8 once the wrapped object is not used anymore.
187 * @param info The function callback info object as provided by v8
188 */
189 static void destructorCallback(const v8::WeakCallbackInfo<JSExternal>& info);
190
191 /**
192 * Disabled assign operator.
193 * @return Reference to this object
194 */
196
197 private:
198
199 /// The pointer to the actual native object.
200 void* data_;
201
202 /// The type of the native object.
204
205 /// The v8 Persistent object ensuring that we get informed when the JS object is not needed anymore.
206 v8::Persistent<v8::Object> persistent_;
207};
208
209template <typename TNative>
210JSExternal* JSExternal::create(TNative* data, v8::Local<v8::Object>& owner, v8::Isolate* isolate)
211{
212 return new JSExternal((void*)(data), type<TNative>(), owner, isolate);
213}
214
216{
217 return type_;
218}
219
220template <>
222{
223 ocean_assert(data_ != nullptr);
224 ocean_assert(type_ == TYPE_DEVICE_OBJECT);
225
226 return *static_cast<JSDevice*>(data_);
227}
228
229template <>
231{
232 ocean_assert(data_ != nullptr);
233 ocean_assert(type_ == TYPE_DEVICE_SAMPLE);
234
235 return *static_cast<Devices::Measurement::SampleRef*>(data_);
236}
237
238template <>
240{
241 ocean_assert(data_ != nullptr);
242 ocean_assert(type_ == TYPE_EULER);
243
244 return *static_cast<Euler*>(data_);
245}
246
247template <>
249{
250 ocean_assert(data_ != nullptr);
251 ocean_assert(type_ == TYPE_HOMOGENOUS_MATRIX_4);
252
253 return *static_cast<HomogenousMatrix4*>(data_);
254}
255
256template <>
258{
259 ocean_assert(data_ != nullptr);
260 ocean_assert(type_ == TYPE_LINE_3);
261
262 return *static_cast<Line3*>(data_);
263}
264
265template <>
267{
268 ocean_assert(data_ != nullptr);
269 ocean_assert(type_ == TYPE_PLANE_3);
270
271 return *static_cast<Plane3*>(data_);
272}
273
274template <>
276{
277 ocean_assert(data_ != nullptr);
278 ocean_assert(type_ == TYPE_RENDERING_ENGINE);
279
280 return *static_cast<Rendering::EngineRef*>(data_);
281}
282
283template <>
285{
286 ocean_assert(data_ != nullptr);
287 ocean_assert(type_ == TYPE_RENDERING_OBJECT);
288
289 return *static_cast<Rendering::ObjectRef*>(data_);
290}
291
292template <>
294{
295 ocean_assert(data_ != nullptr);
296 ocean_assert(type_ == TYPE_MEDIA_OBJECT);
297
298 return *static_cast<Media::MediumRef*>(data_);
299}
300
301template <>
303{
304 ocean_assert(data_ != nullptr);
305 ocean_assert(type_ == TYPE_SCENE_DESCRIPTION_NODE);
306
307 return *static_cast<SceneDescription::NodeRef*>(data_);
308}
309
310template <>
312{
313 ocean_assert(data_ != nullptr);
314 ocean_assert(type_ == TYPE_QUATERNION);
315
316 return *static_cast<Quaternion*>(data_);
317}
318
319template <>
321{
322 ocean_assert(data_ != nullptr);
323 ocean_assert(type_ == TYPE_COLOR);
324
325 return *static_cast<RGBAColor*>(data_);
326}
327
328template <>
330{
331 ocean_assert(data_ != nullptr);
332 ocean_assert(type_ == TYPE_ROTATION);
333
334 return *static_cast<Rotation*>(data_);
335}
336
337template <>
339{
340 ocean_assert(data_ != nullptr);
341 ocean_assert(type_ == TYPE_VECTOR_2);
342
343 return *static_cast<Vector2*>(data_);
344}
345
346template <>
348{
349 ocean_assert(data_ != nullptr);
350 ocean_assert(type_ == TYPE_VECTOR_3);
351
352 return *static_cast<Vector3*>(data_);
353}
354
355template <>
357{
358 ocean_assert(data_ != nullptr);
359 ocean_assert(type_ == TYPE_VECTOR_4);
360
361 return *static_cast<Vector4*>(data_);
362}
363
364template <>
366{
367 ocean_assert(data_ != nullptr);
368 ocean_assert(type_ == TYPE_SQUARE_MATRIX_3);
369
370 return *static_cast<SquareMatrix3*>(data_);
371}
372
373template <>
375{
376 ocean_assert(data_ != nullptr);
377 ocean_assert(type_ == TYPE_SQUARE_MATRIX_4);
378
379 return *static_cast<SquareMatrix4*>(data_);
380}
381
382template <typename TNative>
383inline TNative& JSExternal::value()
384{
385 static_assert(oceanFalse<TNative>(), "This function is not defined for the data type");
386}
387
388template <>
389inline JSExternal::Type JSExternal::type<Devices::Measurement::SampleRef>()
390{
391 return TYPE_DEVICE_SAMPLE;
392}
393
394template <>
395inline JSExternal::Type JSExternal::type<JSDevice>()
396{
397 return TYPE_DEVICE_OBJECT;
398}
399
400template <>
401inline JSExternal::Type JSExternal::type<Euler>()
402{
403 return TYPE_EULER;
404}
405
406template <>
407inline JSExternal::Type JSExternal::type<HomogenousMatrix4>()
408{
410}
411
412template <>
413inline JSExternal::Type JSExternal::type<Line3>()
414{
415 return TYPE_LINE_3;
416}
417
418template <>
419inline JSExternal::Type JSExternal::type<Plane3>()
420{
421 return TYPE_PLANE_3;
422}
423
424template <>
425inline JSExternal::Type JSExternal::type<Rendering::EngineRef>()
426{
427 return TYPE_RENDERING_ENGINE;
428}
429
430template <>
431inline JSExternal::Type JSExternal::type<Rendering::ObjectRef>()
432{
433 return TYPE_RENDERING_OBJECT;
434}
435
436
437template <>
438inline JSExternal::Type JSExternal::type<Media::MediumRef>()
439{
440 return TYPE_MEDIA_OBJECT;
441}
442
443template <>
444inline JSExternal::Type JSExternal::type<SceneDescription::NodeRef>()
445{
446 return TYPE_SCENE_DESCRIPTION_NODE;
447}
448
449template <>
450inline JSExternal::Type JSExternal::type<Quaternion>()
451{
452 return TYPE_QUATERNION;
453}
454
455template <>
456inline JSExternal::Type JSExternal::type<RGBAColor>()
457{
458 return TYPE_COLOR;
459}
460
461template <>
462inline JSExternal::Type JSExternal::type<Rotation>()
463{
464 return TYPE_ROTATION;
465}
466
467template <>
468inline JSExternal::Type JSExternal::type<SquareMatrix3>()
469{
471}
472
473template <>
474inline JSExternal::Type JSExternal::type<SquareMatrix4>()
475{
477}
478
479template <>
480inline JSExternal::Type JSExternal::type<Vector2>()
481{
482 return TYPE_VECTOR_2;
483}
484
485template <>
486inline JSExternal::Type JSExternal::type<Vector3>()
487{
488 return TYPE_VECTOR_3;
489}
490
491template <>
492inline JSExternal::Type JSExternal::type<Vector4>()
493{
494 return TYPE_VECTOR_4;
495}
496
497template <typename TNative>
499{
500 static_assert(oceanFalse<TNative>(), "This function is not defined for the data type");
501}
502
503
504template <typename TNative>
505inline void JSExternal::setValue(v8::Local<v8::Object>& object, TNative&& value)
506{
507 JSExternal* externalObject = external(object);
508 ocean_assert(externalObject != nullptr);
509
510 externalObject->value<TNative>() = std::move(value);
511}
512
513template <typename TNative>
514inline void JSExternal::setValue(v8::Local<v8::Object>& object, const TNative& value)
515{
516 JSExternal* externalObject = external(object);
517 ocean_assert(externalObject != nullptr);
518
519 externalObject->value<TNative>() = value;
520}
521
522}
523
524}
525
526}
527
528#endif // META_OCEAN_INTERACTION_JS_EXTERNAL_H
This class implements an euler rotation with angles: yaw, pitch and roll.
Definition Euler.h:80
This class is a lightweight helper class for device objects to allow event callback handling.
Definition JSDevice.h:35
This class implements a type-safe object wrapper for non-JavaScript objects.
Definition JSExternal.h:50
Type type() const
Returns the type of the wrapped C++ object.
Definition JSExternal.h:215
static void destructorCallback(const v8::WeakCallbackInfo< JSExternal > &info)
The callback function which will be called from v8 once the wrapped object is not used anymore.
JSExternal & operator=(JSExternal &)=delete
Disabled assign operator.
v8::Persistent< v8::Object > persistent_
The v8 Persistent object ensuring that we get informed when the JS object is not needed anymore.
Definition JSExternal.h:206
static JSExternal * create(TNative *data, v8::Local< v8::Object > &owner, v8::Isolate *isolate)
Creates a new type-safe wrapper object for a specific native C++ object.
Definition JSExternal.h:210
TNative & value()
Returns the wrapped C++ object.
Definition JSExternal.h:383
static void setValue(v8::Local< v8::Object > &object, TNative &&value)
Sets or changes the native C++ object of an external JavaScript object.
Definition JSExternal.h:505
void * data_
The pointer to the actual native object.
Definition JSExternal.h:200
static JSExternal * external(const v8::Local< v8::Object > &object)
Returns the external object from a given JavaScript object.
Type type_
The type of the native object.
Definition JSExternal.h:203
JSExternal(const JSExternal &)=delete
Disabled copy constructor.
JSExternal(void *data, const Type type, v8::Local< v8::Object > &owner, v8::Isolate *isolate)
Creates a new type-safe wrapper object for a specific native C++ object.
Type
Definition of different external object types.
Definition JSExternal.h:57
@ TYPE_RENDERING_OBJECT
Rendering object object.
Definition JSExternal.h:81
@ TYPE_SQUARE_MATRIX_3
SquareMatrix3 object.
Definition JSExternal.h:87
@ TYPE_ROTATION
Rotation object.
Definition JSExternal.h:83
@ TYPE_DEVICE_SAMPLE
Device sample object.
Definition JSExternal.h:65
@ TYPE_VECTOR_2
Vector2 object.
Definition JSExternal.h:91
@ TYPE_SQUARE_MATRIX_4
SquareMatrix4 object.
Definition JSExternal.h:89
@ TYPE_SCENE_DESCRIPTION_NODE
Scene description node.
Definition JSExternal.h:85
@ TYPE_VECTOR_4
Vector4 object.
Definition JSExternal.h:95
@ TYPE_EULER
Euler object.
Definition JSExternal.h:67
@ TYPE_VECTOR_3
Vector3 object.
Definition JSExternal.h:93
@ TYPE_DEVICE_OBJECT
Device object object.
Definition JSExternal.h:63
@ TYPE_QUATERNION
Quaternion object.
Definition JSExternal.h:77
@ TYPE_RENDERING_ENGINE
Rendering engine object.
Definition JSExternal.h:79
@ TYPE_UNDEFINED
Undefined object.
Definition JSExternal.h:59
@ TYPE_PLANE_3
Plane3 object.
Definition JSExternal.h:75
@ TYPE_COLOR
Color object.
Definition JSExternal.h:61
@ TYPE_MEDIA_OBJECT
Media object object.
Definition JSExternal.h:73
@ TYPE_LINE_3
Line3 object.
Definition JSExternal.h:71
@ TYPE_HOMOGENOUS_MATRIX_4
HomogenousMatrix4 object.
Definition JSExternal.h:69
static JSExternal * external(const v8::Local< v8::Value > &value)
Returns the external object from a given JavaScript object.
This class implements an infinite line in 3D space.
Definition Line3.h:68
This template class implements a object reference with an internal reference counter.
Definition base/ObjectRef.h:58
This class implements a color defined by red, green, blue and alpha parameters.
Definition RGBAColor.h:41
This class implements a axis-angle rotation using floating point values.
Definition Rotation.h:79
This class implements a vector with four elements.
Definition Vector4.h:97
The namespace covering the entire Ocean framework.
Definition Accessor.h:15