Ocean
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"
20 #include "ocean/math/Quaternion.h"
21 #include "ocean/math/RGBAColor.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 
29 #include "ocean/media/MediumRef.h"
30 
31 #include "ocean/rendering/Engine.h"
33 
35 
36 namespace Ocean
37 {
38 
39 namespace Interaction
40 {
41 
42 namespace JavaScript
43 {
44 
45 /**
46  * This class implements a type-safe object wrapper for non-JavaScript objects.
47  * @ingroup interactionjs
48  */
49 class 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 
209 template <typename TNative>
210 JSExternal* 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 
220 template <>
222 {
223  ocean_assert(data_ != nullptr);
224  ocean_assert(type_ == TYPE_DEVICE_OBJECT);
225 
226  return *static_cast<JSDevice*>(data_);
227 }
228 
229 template <>
231 {
232  ocean_assert(data_ != nullptr);
233  ocean_assert(type_ == TYPE_DEVICE_SAMPLE);
234 
235  return *static_cast<Devices::Measurement::SampleRef*>(data_);
236 }
237 
238 template <>
239 inline Euler& JSExternal::value()
240 {
241  ocean_assert(data_ != nullptr);
242  ocean_assert(type_ == TYPE_EULER);
243 
244  return *static_cast<Euler*>(data_);
245 }
246 
247 template <>
249 {
250  ocean_assert(data_ != nullptr);
251  ocean_assert(type_ == TYPE_HOMOGENOUS_MATRIX_4);
252 
253  return *static_cast<HomogenousMatrix4*>(data_);
254 }
255 
256 template <>
257 inline Line3& JSExternal::value()
258 {
259  ocean_assert(data_ != nullptr);
260  ocean_assert(type_ == TYPE_LINE_3);
261 
262  return *static_cast<Line3*>(data_);
263 }
264 
265 template <>
266 inline Plane3& JSExternal::value()
267 {
268  ocean_assert(data_ != nullptr);
269  ocean_assert(type_ == TYPE_PLANE_3);
270 
271  return *static_cast<Plane3*>(data_);
272 }
273 
274 template <>
276 {
277  ocean_assert(data_ != nullptr);
278  ocean_assert(type_ == TYPE_RENDERING_ENGINE);
279 
280  return *static_cast<Rendering::EngineRef*>(data_);
281 }
282 
283 template <>
285 {
286  ocean_assert(data_ != nullptr);
287  ocean_assert(type_ == TYPE_RENDERING_OBJECT);
288 
289  return *static_cast<Rendering::ObjectRef*>(data_);
290 }
291 
292 template <>
294 {
295  ocean_assert(data_ != nullptr);
296  ocean_assert(type_ == TYPE_MEDIA_OBJECT);
297 
298  return *static_cast<Media::MediumRef*>(data_);
299 }
300 
301 template <>
303 {
304  ocean_assert(data_ != nullptr);
305  ocean_assert(type_ == TYPE_SCENE_DESCRIPTION_NODE);
306 
307  return *static_cast<SceneDescription::NodeRef*>(data_);
308 }
309 
310 template <>
312 {
313  ocean_assert(data_ != nullptr);
314  ocean_assert(type_ == TYPE_QUATERNION);
315 
316  return *static_cast<Quaternion*>(data_);
317 }
318 
319 template <>
320 inline RGBAColor& JSExternal::value()
321 {
322  ocean_assert(data_ != nullptr);
323  ocean_assert(type_ == TYPE_COLOR);
324 
325  return *static_cast<RGBAColor*>(data_);
326 }
327 
328 template <>
329 inline Rotation& JSExternal::value()
330 {
331  ocean_assert(data_ != nullptr);
332  ocean_assert(type_ == TYPE_ROTATION);
333 
334  return *static_cast<Rotation*>(data_);
335 }
336 
337 template <>
338 inline Vector2& JSExternal::value()
339 {
340  ocean_assert(data_ != nullptr);
341  ocean_assert(type_ == TYPE_VECTOR_2);
342 
343  return *static_cast<Vector2*>(data_);
344 }
345 
346 template <>
347 inline Vector3& JSExternal::value()
348 {
349  ocean_assert(data_ != nullptr);
350  ocean_assert(type_ == TYPE_VECTOR_3);
351 
352  return *static_cast<Vector3*>(data_);
353 }
354 
355 template <>
356 inline Vector4& JSExternal::value()
357 {
358  ocean_assert(data_ != nullptr);
359  ocean_assert(type_ == TYPE_VECTOR_4);
360 
361  return *static_cast<Vector4*>(data_);
362 }
363 
364 template <>
366 {
367  ocean_assert(data_ != nullptr);
368  ocean_assert(type_ == TYPE_SQUARE_MATRIX_3);
369 
370  return *static_cast<SquareMatrix3*>(data_);
371 }
372 
373 template <>
375 {
376  ocean_assert(data_ != nullptr);
377  ocean_assert(type_ == TYPE_SQUARE_MATRIX_4);
378 
379  return *static_cast<SquareMatrix4*>(data_);
380 }
381 
382 template <typename TNative>
383 inline TNative& JSExternal::value()
384 {
385  static_assert(oceanFalse<TNative>(), "This function is not defined for the data type");
386 }
387 
388 template <>
389 inline JSExternal::Type JSExternal::type<Devices::Measurement::SampleRef>()
390 {
391  return TYPE_DEVICE_SAMPLE;
392 }
393 
394 template <>
395 inline JSExternal::Type JSExternal::type<JSDevice>()
396 {
397  return TYPE_DEVICE_OBJECT;
398 }
399 
400 template <>
401 inline JSExternal::Type JSExternal::type<Euler>()
402 {
403  return TYPE_EULER;
404 }
405 
406 template <>
407 inline JSExternal::Type JSExternal::type<HomogenousMatrix4>()
408 {
410 }
411 
412 template <>
413 inline JSExternal::Type JSExternal::type<Line3>()
414 {
415  return TYPE_LINE_3;
416 }
417 
418 template <>
419 inline JSExternal::Type JSExternal::type<Plane3>()
420 {
421  return TYPE_PLANE_3;
422 }
423 
424 template <>
425 inline JSExternal::Type JSExternal::type<Rendering::EngineRef>()
426 {
427  return TYPE_RENDERING_ENGINE;
428 }
429 
430 template <>
431 inline JSExternal::Type JSExternal::type<Rendering::ObjectRef>()
432 {
433  return TYPE_RENDERING_OBJECT;
434 }
435 
436 
437 template <>
438 inline JSExternal::Type JSExternal::type<Media::MediumRef>()
439 {
440  return TYPE_MEDIA_OBJECT;
441 }
442 
443 template <>
444 inline JSExternal::Type JSExternal::type<SceneDescription::NodeRef>()
445 {
446  return TYPE_SCENE_DESCRIPTION_NODE;
447 }
448 
449 template <>
450 inline JSExternal::Type JSExternal::type<Quaternion>()
451 {
452  return TYPE_QUATERNION;
453 }
454 
455 template <>
456 inline JSExternal::Type JSExternal::type<RGBAColor>()
457 {
458  return TYPE_COLOR;
459 }
460 
461 template <>
462 inline JSExternal::Type JSExternal::type<Rotation>()
463 {
464  return TYPE_ROTATION;
465 }
466 
467 template <>
468 inline JSExternal::Type JSExternal::type<SquareMatrix3>()
469 {
470  return TYPE_SQUARE_MATRIX_3;
471 }
472 
473 template <>
474 inline JSExternal::Type JSExternal::type<SquareMatrix4>()
475 {
476  return TYPE_SQUARE_MATRIX_4;
477 }
478 
479 template <>
480 inline JSExternal::Type JSExternal::type<Vector2>()
481 {
482  return TYPE_VECTOR_2;
483 }
484 
485 template <>
486 inline JSExternal::Type JSExternal::type<Vector3>()
487 {
488  return TYPE_VECTOR_3;
489 }
490 
491 template <>
492 inline JSExternal::Type JSExternal::type<Vector4>()
493 {
494  return TYPE_VECTOR_4;
495 }
496 
497 template <typename TNative>
499 {
500  static_assert(oceanFalse<TNative>(), "This function is not defined for the data type");
501 }
502 
503 
504 template <typename TNative>
505 inline 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 
513 template <typename TNative>
514 inline 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.
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
static JSExternal * external(const v8::Local< v8::Object > &object)
Returns the external object from a given JavaScript object.
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
static JSExternal * external(const v8::Local< v8::Value > &value)
Returns the external object from a given JavaScript object.
void * data_
The pointer to the actual native object.
Definition: JSExternal.h:200
Type type_
The type of the native object.
Definition: JSExternal.h:203
JSExternal(const JSExternal &)=delete
Disabled copy constructor.
JSExternal & operator=(JSExternal &)=delete
Disabled assign operator.
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
This template class implements a object reference with an internal reference counter.
Definition: base/ObjectRef.h:58
QuaternionT< Scalar > Quaternion
Definition of the Quaternion object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with sin...
Definition: Quaternion.h:33
LineT3< Scalar > Line3
Definition of the Line3 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with single o...
Definition: Line3.h:21
SquareMatrixT3< Scalar > SquareMatrix3
Definition of the SquareMatrix3 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with ...
Definition: SquareMatrix3.h:35
RotationT< Scalar > Rotation
Definition of the Rotation object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION flag either with ...
Definition: Rotation.h:31
VectorT4< Scalar > Vector4
Definition of a 4D vector.
Definition: Vector4.h:22
VectorT3< Scalar > Vector3
Definition of a 3D vector.
Definition: Vector3.h:22
PlaneT3< Scalar > Plane3
Definition of the Plane3 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with single ...
Definition: Plane3.h:24
SquareMatrixT4< Scalar > SquareMatrix4
Definition of the SquareMatrix4 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with ...
Definition: SquareMatrix4.h:32
VectorT2< Scalar > Vector2
Definition of a 2D vector.
Definition: Vector2.h:21
ObjectRef< Medium > MediumRef
This class implements a medium reference with an internal reference counter.
Definition: Medium.h:23
Ocean::ObjectRef< Engine > EngineRef
Definition of an engine reference object.
Definition: Engine.h:24
Ocean::ObjectRef< Object > ObjectRef
Definition of a rendering object reference with an internal reference counter.
Definition: Object.h:28
Ocean::ObjectRef< Node > NodeRef
Definition of a scene description node reference with an internal reference counter.
Definition: scenedescription/Node.h:29
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15