Ocean
JSObject.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_OBJECT_H
9 #define META_OCEAN_INTERACTION_JS_OBJECT_H
10 
13 
14 #include <v8.h>
15 
16 namespace Ocean
17 {
18 
19 namespace Interaction
20 {
21 
22 namespace JavaScript
23 {
24 
25 /**
26  * This class implements the base class for all native wrapper of JavaScript objects.
27  * @ingroup interactionjs
28  */
29 template <typename T, typename TNative>
30 class JSObject : public JSBase
31 {
32  public:
33 
34  /**
35  * Definition of the native data type which is wrapped by this Java Script object.
36  */
37  typedef TNative NativeType;
38 
39  public:
40 
41  /**
42  * Returns the function template for this object.
43  * @return The function template
44  */
45  static inline v8::Local<v8::FunctionTemplate> functionTemplate();
46 
47  /**
48  * Returns the object template for this object.
49  * @return The object template
50  */
51  static inline v8::Local<v8::ObjectTemplate> objectTemplate();
52 
53  /**
54  * Creates an new wrapped object.
55  * @param value The value of the new object, will be moved
56  * @param context The context in which the object will be created
57  * @return The new JavaScript object
58  */
59  static inline v8::Local<v8::Value> create(NativeType&& value, const v8::Local<v8::Context>& context);
60 
61  /**
62  * Releases the function and object template for this object.
63  */
64  static void release();
65 
66  protected:
67 
68  /// Function template for the Vector2 object.
69  static v8::Persistent<v8::FunctionTemplate> functionTemplate_;
70 
71  /// Object template for the Vector2 object.
72  static v8::Persistent<v8::ObjectTemplate> objectTemplate_;
73 };
74 
75 template <typename T, typename TNative>
76 v8::Persistent<v8::FunctionTemplate> JSObject<T, TNative>::functionTemplate_;
77 
78 template <typename T, typename TNative>
79 v8::Persistent<v8::ObjectTemplate> JSObject<T, TNative>::objectTemplate_;
80 
81 template <typename T, typename TNative>
82 inline v8::Local<v8::FunctionTemplate> JSObject<T, TNative>::functionTemplate()
83 {
84  if (functionTemplate_.IsEmpty())
85  {
86  T::createFunctionTemplate();
87  }
88 
89  ocean_assert(!functionTemplate_.IsEmpty());
90 
91  return functionTemplate_.Get(v8::Isolate::GetCurrent());
92 }
93 
94 template <typename T, typename TNative>
95 inline v8::Local<v8::ObjectTemplate> JSObject<T, TNative>::objectTemplate()
96 {
97  ocean_assert(!objectTemplate_.IsEmpty());
98  return objectTemplate_.Get(v8::Isolate::GetCurrent());
99 }
100 
101 template <typename T, typename TNative>
102 inline v8::Local<v8::Value> JSObject<T, TNative>::create(NativeType&& value, const v8::Local<v8::Context>& context)
103 {
104  return createObject<T>(std::move(value), context);
105 }
106 
107 template <typename T, typename TNative>
109 {
110  ocean_assert(!functionTemplate_.IsEmpty());
111  functionTemplate_.Reset();
112 
113  ocean_assert(!objectTemplate_.IsEmpty());
114  objectTemplate_.Reset();
115 }
116 
117 }
118 
119 }
120 
121 }
122 
123 #endif // META_OCEAN_INTERACTION_JS_OBJECT_H
This class implements the base class for all JavaScript wrappers.
Definition: JSBase.h:34
This class implements the base class for all native wrapper of JavaScript objects.
Definition: JSObject.h:31
static v8::Persistent< v8::FunctionTemplate > functionTemplate_
Function template for the Vector2 object.
Definition: JSObject.h:69
static void release()
Releases the function and object template for this object.
Definition: JSObject.h:108
static v8::Local< v8::FunctionTemplate > functionTemplate()
Returns the function template for this object.
Definition: JSObject.h:82
static v8::Local< v8::Value > create(NativeType &&value, const v8::Local< v8::Context > &context)
Creates an new wrapped object.
Definition: JSObject.h:102
static v8::Local< v8::ObjectTemplate > objectTemplate()
Returns the object template for this object.
Definition: JSObject.h:95
static v8::Persistent< v8::ObjectTemplate > objectTemplate_
Object template for the Vector2 object.
Definition: JSObject.h:72
TNative NativeType
Definition of the native data type which is wrapped by this Java Script object.
Definition: JSObject.h:37
This template class implements a object reference with an internal reference counter.
Definition: base/ObjectRef.h:58
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15