OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #ifndef FPDFSDK_JAVASCRIPT_JS_DEFINE_H_ | 7 #ifndef FPDFSDK_JAVASCRIPT_JS_DEFINE_H_ |
8 #define FPDFSDK_JAVASCRIPT_JS_DEFINE_H_ | 8 #define FPDFSDK_JAVASCRIPT_JS_DEFINE_H_ |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 { 0, 0 } \ | 69 { 0, 0 } \ |
70 } \ | 70 } \ |
71 ; // NOLINT | 71 ; // NOLINT |
72 | 72 |
73 template <class C, | 73 template <class C, |
74 FX_BOOL (C::*M)(IJS_Context*, CJS_PropValue&, CFX_WideString&)> | 74 FX_BOOL (C::*M)(IJS_Context*, CJS_PropValue&, CFX_WideString&)> |
75 void JSPropGetter(const char* prop_name_string, | 75 void JSPropGetter(const char* prop_name_string, |
76 const char* class_name_string, | 76 const char* class_name_string, |
77 v8::Local<v8::String> property, | 77 v8::Local<v8::String> property, |
78 const v8::PropertyCallbackInfo<v8::Value>& info) { | 78 const v8::PropertyCallbackInfo<v8::Value>& info) { |
79 v8::Isolate* isolate = info.GetIsolate(); | |
80 CJS_Runtime* pRuntime = | 79 CJS_Runtime* pRuntime = |
81 static_cast<CJS_Runtime*>(FXJS_GetCurrentEngineFromIsolate(isolate)); | 80 CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
82 if (!pRuntime) | 81 if (!pRuntime) |
83 return; | 82 return; |
84 IJS_Context* pContext = pRuntime->GetCurrentContext(); | 83 CJS_Object* pJSObj = |
85 CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, info.Holder()); | 84 static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); |
86 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); | 85 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
87 CFX_WideString sError; | 86 CFX_WideString sError; |
88 CJS_PropValue value(pRuntime); | 87 CJS_PropValue value(pRuntime); |
89 value.StartGetting(); | 88 value.StartGetting(); |
90 if (!(pObj->*M)(pContext, value, sError)) { | 89 if (!(pObj->*M)(pRuntime->GetCurrentContext(), value, sError)) { |
91 FXJS_Error(isolate, JSFormatErrorString(class_name_string, prop_name_string, | 90 pRuntime->Error( |
92 sError)); | 91 JSFormatErrorString(class_name_string, prop_name_string, sError)); |
93 return; | 92 return; |
94 } | 93 } |
95 info.GetReturnValue().Set(value.GetJSValue()->ToV8Value(isolate)); | 94 info.GetReturnValue().Set(value.GetJSValue()->ToV8Value(pRuntime)); |
96 } | 95 } |
97 | 96 |
98 template <class C, | 97 template <class C, |
99 FX_BOOL (C::*M)(IJS_Context*, CJS_PropValue&, CFX_WideString&)> | 98 FX_BOOL (C::*M)(IJS_Context*, CJS_PropValue&, CFX_WideString&)> |
100 void JSPropSetter(const char* prop_name_string, | 99 void JSPropSetter(const char* prop_name_string, |
101 const char* class_name_string, | 100 const char* class_name_string, |
102 v8::Local<v8::String> property, | 101 v8::Local<v8::String> property, |
103 v8::Local<v8::Value> value, | 102 v8::Local<v8::Value> value, |
104 const v8::PropertyCallbackInfo<void>& info) { | 103 const v8::PropertyCallbackInfo<void>& info) { |
105 v8::Isolate* isolate = info.GetIsolate(); | |
106 CJS_Runtime* pRuntime = | 104 CJS_Runtime* pRuntime = |
107 static_cast<CJS_Runtime*>(FXJS_GetCurrentEngineFromIsolate(isolate)); | 105 CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
108 if (!pRuntime) | 106 if (!pRuntime) |
109 return; | 107 return; |
110 IJS_Context* pContext = pRuntime->GetCurrentContext(); | 108 CJS_Object* pJSObj = |
111 CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, info.Holder()); | 109 static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); |
112 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); | 110 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
113 CFX_WideString sError; | 111 CFX_WideString sError; |
114 CJS_PropValue propValue(pRuntime, CJS_Value(pRuntime, value)); | 112 CJS_PropValue propValue(pRuntime, CJS_Value(pRuntime, value)); |
115 propValue.StartSetting(); | 113 propValue.StartSetting(); |
116 if (!(pObj->*M)(pContext, propValue, sError)) { | 114 if (!(pObj->*M)(pRuntime->GetCurrentContext(), propValue, sError)) { |
117 FXJS_Error(isolate, JSFormatErrorString(class_name_string, prop_name_string, | 115 pRuntime->Error( |
118 sError)); | 116 JSFormatErrorString(class_name_string, prop_name_string, sError)); |
119 } | 117 } |
120 } | 118 } |
121 | 119 |
122 #define JS_STATIC_PROP(prop_name, class_name) \ | 120 #define JS_STATIC_PROP(prop_name, class_name) \ |
123 static void get_##prop_name##_static( \ | 121 static void get_##prop_name##_static( \ |
124 v8::Local<v8::String> property, \ | 122 v8::Local<v8::String> property, \ |
125 const v8::PropertyCallbackInfo<v8::Value>& info) { \ | 123 const v8::PropertyCallbackInfo<v8::Value>& info) { \ |
126 JSPropGetter<class_name, &class_name::prop_name>(#prop_name, #class_name, \ | 124 JSPropGetter<class_name, &class_name::prop_name>(#prop_name, #class_name, \ |
127 property, info); \ | 125 property, info); \ |
128 } \ | 126 } \ |
129 static void set_##prop_name##_static( \ | 127 static void set_##prop_name##_static( \ |
130 v8::Local<v8::String> property, v8::Local<v8::Value> value, \ | 128 v8::Local<v8::String> property, v8::Local<v8::Value> value, \ |
131 const v8::PropertyCallbackInfo<void>& info) { \ | 129 const v8::PropertyCallbackInfo<void>& info) { \ |
132 JSPropSetter<class_name, &class_name::prop_name>(#prop_name, #class_name, \ | 130 JSPropSetter<class_name, &class_name::prop_name>(#prop_name, #class_name, \ |
133 property, value, info); \ | 131 property, value, info); \ |
134 } | 132 } |
135 | 133 |
136 template <class C, | 134 template <class C, |
137 FX_BOOL (C::*M)(IJS_Context*, | 135 FX_BOOL (C::*M)(IJS_Context*, |
138 const std::vector<CJS_Value>&, | 136 const std::vector<CJS_Value>&, |
139 CJS_Value&, | 137 CJS_Value&, |
140 CFX_WideString&)> | 138 CFX_WideString&)> |
141 void JSMethod(const char* method_name_string, | 139 void JSMethod(const char* method_name_string, |
142 const char* class_name_string, | 140 const char* class_name_string, |
143 const v8::FunctionCallbackInfo<v8::Value>& info) { | 141 const v8::FunctionCallbackInfo<v8::Value>& info) { |
144 v8::Isolate* isolate = info.GetIsolate(); | |
145 CJS_Runtime* pRuntime = | 142 CJS_Runtime* pRuntime = |
146 static_cast<CJS_Runtime*>(FXJS_GetCurrentEngineFromIsolate(isolate)); | 143 CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
147 if (!pRuntime) | 144 if (!pRuntime) |
148 return; | 145 return; |
149 IJS_Context* pContext = pRuntime->GetCurrentContext(); | |
150 std::vector<CJS_Value> parameters; | 146 std::vector<CJS_Value> parameters; |
151 for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { | 147 for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { |
152 parameters.push_back(CJS_Value(pRuntime, info[i])); | 148 parameters.push_back(CJS_Value(pRuntime, info[i])); |
153 } | 149 } |
154 CJS_Value valueRes(pRuntime); | 150 CJS_Object* pJSObj = |
155 CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, info.Holder()); | 151 static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); |
156 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); | 152 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
157 CFX_WideString sError; | 153 CFX_WideString sError; |
158 if (!(pObj->*M)(pContext, parameters, valueRes, sError)) { | 154 CJS_Value valueRes(pRuntime); |
159 FXJS_Error(isolate, JSFormatErrorString(class_name_string, | 155 if (!(pObj->*M)(pRuntime->GetCurrentContext(), parameters, valueRes, |
160 method_name_string, sError)); | 156 sError)) { |
| 157 pRuntime->Error( |
| 158 JSFormatErrorString(class_name_string, method_name_string, sError)); |
161 return; | 159 return; |
162 } | 160 } |
163 info.GetReturnValue().Set(valueRes.ToV8Value(isolate)); | 161 info.GetReturnValue().Set(valueRes.ToV8Value(pRuntime)); |
164 } | 162 } |
165 | 163 |
166 #define JS_STATIC_METHOD(method_name, class_name) \ | 164 #define JS_STATIC_METHOD(method_name, class_name) \ |
167 static void method_name##_static( \ | 165 static void method_name##_static( \ |
168 const v8::FunctionCallbackInfo<v8::Value>& info) { \ | 166 const v8::FunctionCallbackInfo<v8::Value>& info) { \ |
169 JSMethod<class_name, &class_name::method_name>(#method_name, #class_name, \ | 167 JSMethod<class_name, &class_name::method_name>(#method_name, #class_name, \ |
170 info); \ | 168 info); \ |
171 } | 169 } |
172 | 170 |
173 #define JS_SPECIAL_STATIC_METHOD(method_name, class_alternate, class_name) \ | 171 #define JS_SPECIAL_STATIC_METHOD(method_name, class_alternate, class_name) \ |
174 static void method_name##_static( \ | 172 static void method_name##_static( \ |
175 const v8::FunctionCallbackInfo<v8::Value>& info) { \ | 173 const v8::FunctionCallbackInfo<v8::Value>& info) { \ |
176 JSMethod<class_alternate, &class_alternate::method_name>( \ | 174 JSMethod<class_alternate, &class_alternate::method_name>( \ |
177 #method_name, #class_name, info); \ | 175 #method_name, #class_name, info); \ |
178 } | 176 } |
179 | 177 |
180 // All JS classes have a name, an object defintion ID, and the ability to | 178 // All JS classes have a name, an object defintion ID, and the ability to |
181 // register themselves with FXJS_V8. We never make a BASE class on its own | 179 // register themselves with FXJS_V8. We never make a BASE class on its own |
182 // because it can't really do anything. | 180 // because it can't really do anything. |
183 #define DECLARE_JS_CLASS_BASE_PART() \ | 181 #define DECLARE_JS_CLASS_BASE_PART() \ |
184 static const wchar_t* g_pClassName; \ | 182 static const wchar_t* g_pClassName; \ |
185 static int g_nObjDefnID; \ | 183 static int g_nObjDefnID; \ |
186 static void DefineJSObjects(v8::Isolate* pIsolate, FXJSOBJTYPE eObjType); | 184 static void DefineJSObjects(CFXJS_Engine* pEngine, FXJSOBJTYPE eObjType); |
187 | 185 |
188 #define IMPLEMENT_JS_CLASS_BASE_PART(js_class_name, class_name) \ | 186 #define IMPLEMENT_JS_CLASS_BASE_PART(js_class_name, class_name) \ |
189 const wchar_t* js_class_name::g_pClassName = JS_WIDESTRING(class_name); \ | 187 const wchar_t* js_class_name::g_pClassName = JS_WIDESTRING(class_name); \ |
190 int js_class_name::g_nObjDefnID = -1; | 188 int js_class_name::g_nObjDefnID = -1; |
191 | 189 |
192 // CONST classes provide constants, but not constructors, methods, or props. | 190 // CONST classes provide constants, but not constructors, methods, or props. |
193 #define DECLARE_JS_CLASS_CONST() \ | 191 #define DECLARE_JS_CLASS_CONST() \ |
194 DECLARE_JS_CLASS_BASE_PART() \ | 192 DECLARE_JS_CLASS_BASE_PART() \ |
195 DECLARE_JS_CLASS_CONST_PART() | 193 DECLARE_JS_CLASS_CONST_PART() |
196 | 194 |
197 #define IMPLEMENT_JS_CLASS_CONST(js_class_name, class_name) \ | 195 #define IMPLEMENT_JS_CLASS_CONST(js_class_name, class_name) \ |
198 IMPLEMENT_JS_CLASS_BASE_PART(js_class_name, class_name) \ | 196 IMPLEMENT_JS_CLASS_BASE_PART(js_class_name, class_name) \ |
199 IMPLEMENT_JS_CLASS_CONST_PART(js_class_name, class_name) \ | 197 IMPLEMENT_JS_CLASS_CONST_PART(js_class_name, class_name) \ |
200 void js_class_name::DefineJSObjects(v8::Isolate* pIsolate, \ | 198 void js_class_name::DefineJSObjects(CFXJS_Engine* pEngine, \ |
201 FXJSOBJTYPE eObjType) { \ | 199 FXJSOBJTYPE eObjType) { \ |
202 g_nObjDefnID = FXJS_DefineObj(pIsolate, js_class_name::g_pClassName, \ | 200 g_nObjDefnID = pEngine->DefineObj(js_class_name::g_pClassName, eObjType, \ |
203 eObjType, nullptr, nullptr); \ | 201 nullptr, nullptr); \ |
204 DefineConsts(pIsolate); \ | 202 DefineConsts(pEngine); \ |
205 } | 203 } |
206 | 204 |
207 #define DECLARE_JS_CLASS_CONST_PART() \ | 205 #define DECLARE_JS_CLASS_CONST_PART() \ |
208 static JSConstSpec JS_Class_Consts[]; \ | 206 static JSConstSpec JS_Class_Consts[]; \ |
209 static void DefineConsts(v8::Isolate* pIsolate); | 207 static void DefineConsts(CFXJS_Engine* pEngine); |
210 | 208 |
211 #define IMPLEMENT_JS_CLASS_CONST_PART(js_class_name, class_name) \ | 209 #define IMPLEMENT_JS_CLASS_CONST_PART(js_class_name, class_name) \ |
212 void js_class_name::DefineConsts(v8::Isolate* pIsolate) { \ | 210 void js_class_name::DefineConsts(CFXJS_Engine* pEngine) { \ |
213 for (size_t i = 0; i < FX_ArraySize(JS_Class_Consts) - 1; ++i) { \ | 211 for (size_t i = 0; i < FX_ArraySize(JS_Class_Consts) - 1; ++i) { \ |
214 FXJS_DefineObjConst( \ | 212 pEngine->DefineObjConst( \ |
215 pIsolate, g_nObjDefnID, JS_Class_Consts[i].pName, \ | 213 g_nObjDefnID, JS_Class_Consts[i].pName, \ |
216 JS_Class_Consts[i].t == 0 \ | 214 JS_Class_Consts[i].t == 0 \ |
217 ? FXJS_NewNumber(pIsolate, JS_Class_Consts[i].number) \ | 215 ? pEngine->NewNumber(JS_Class_Consts[i].number) \ |
218 : FXJS_NewString(pIsolate, JS_Class_Consts[i].str)); \ | 216 : pEngine->NewString(JS_Class_Consts[i].str)); \ |
219 } \ | 217 } \ |
220 } | 218 } |
221 | 219 |
222 // Convenience macros for declaring classes without an alternate. | 220 // Convenience macros for declaring classes without an alternate. |
223 #define DECLARE_JS_CLASS() DECLARE_JS_CLASS_RICH() | 221 #define DECLARE_JS_CLASS() DECLARE_JS_CLASS_RICH() |
224 #define IMPLEMENT_JS_CLASS(js_class_name, class_name) \ | 222 #define IMPLEMENT_JS_CLASS(js_class_name, class_name) \ |
225 IMPLEMENT_JS_CLASS_RICH(js_class_name, class_name, class_name) | 223 IMPLEMENT_JS_CLASS_RICH(js_class_name, class_name, class_name) |
226 | 224 |
227 // Rich JS classes provide constants, methods, properties, and the ability | 225 // Rich JS classes provide constants, methods, properties, and the ability |
228 // to construct native object state. | 226 // to construct native object state. |
229 #define DECLARE_JS_CLASS_RICH() \ | 227 #define DECLARE_JS_CLASS_RICH() \ |
230 DECLARE_JS_CLASS_BASE_PART() \ | 228 DECLARE_JS_CLASS_BASE_PART() \ |
231 DECLARE_JS_CLASS_CONST_PART() \ | 229 DECLARE_JS_CLASS_CONST_PART() \ |
232 DECLARE_JS_CLASS_RICH_PART() | 230 DECLARE_JS_CLASS_RICH_PART() |
233 | 231 |
234 #define IMPLEMENT_JS_CLASS_RICH(js_class_name, class_alternate, class_name) \ | 232 #define IMPLEMENT_JS_CLASS_RICH(js_class_name, class_alternate, class_name) \ |
235 IMPLEMENT_JS_CLASS_BASE_PART(js_class_name, class_name) \ | 233 IMPLEMENT_JS_CLASS_BASE_PART(js_class_name, class_name) \ |
236 IMPLEMENT_JS_CLASS_CONST_PART(js_class_name, class_name) \ | 234 IMPLEMENT_JS_CLASS_CONST_PART(js_class_name, class_name) \ |
237 IMPLEMENT_JS_CLASS_RICH_PART(js_class_name, class_alternate, class_name) \ | 235 IMPLEMENT_JS_CLASS_RICH_PART(js_class_name, class_alternate, class_name) \ |
238 void js_class_name::DefineJSObjects(v8::Isolate* pIsolate, \ | 236 void js_class_name::DefineJSObjects(CFXJS_Engine* pEngine, \ |
239 FXJSOBJTYPE eObjType) { \ | 237 FXJSOBJTYPE eObjType) { \ |
240 g_nObjDefnID = FXJS_DefineObj(pIsolate, js_class_name::g_pClassName, \ | 238 g_nObjDefnID = pEngine->DefineObj(js_class_name::g_pClassName, eObjType, \ |
241 eObjType, JSConstructor, JSDestructor); \ | 239 JSConstructor, JSDestructor); \ |
242 DefineConsts(pIsolate); \ | 240 DefineConsts(pEngine); \ |
243 DefineProps(pIsolate); \ | 241 DefineProps(pEngine); \ |
244 DefineMethods(pIsolate); \ | 242 DefineMethods(pEngine); \ |
245 } | 243 } |
246 | 244 |
247 #define DECLARE_JS_CLASS_RICH_PART() \ | 245 #define DECLARE_JS_CLASS_RICH_PART() \ |
248 static void JSConstructor(CFXJS_Engine* pEngine, v8::Local<v8::Object> obj); \ | 246 static void JSConstructor(CFXJS_Engine* pEngine, v8::Local<v8::Object> obj); \ |
249 static void JSDestructor(v8::Local<v8::Object> obj); \ | 247 static void JSDestructor(CFXJS_Engine* pEngine, v8::Local<v8::Object> obj); \ |
250 static void DefineProps(v8::Isolate* pIsoalte); \ | 248 static void DefineProps(CFXJS_Engine* pEngine); \ |
251 static void DefineMethods(v8::Isolate* pIsoalte); \ | 249 static void DefineMethods(CFXJS_Engine* pEngine); \ |
252 static JSPropertySpec JS_Class_Properties[]; \ | 250 static JSPropertySpec JS_Class_Properties[]; \ |
253 static JSMethodSpec JS_Class_Methods[]; | 251 static JSMethodSpec JS_Class_Methods[]; |
254 | 252 |
255 #define IMPLEMENT_JS_CLASS_RICH_PART(js_class_name, class_alternate, \ | 253 #define IMPLEMENT_JS_CLASS_RICH_PART(js_class_name, class_alternate, \ |
256 class_name) \ | 254 class_name) \ |
257 void js_class_name::JSConstructor(CFXJS_Engine* pEngine, \ | 255 void js_class_name::JSConstructor(CFXJS_Engine* pEngine, \ |
258 v8::Local<v8::Object> obj) { \ | 256 v8::Local<v8::Object> obj) { \ |
259 CJS_Object* pObj = new js_class_name(obj); \ | 257 CJS_Object* pObj = new js_class_name(obj); \ |
260 pObj->SetEmbedObject(new class_alternate(pObj)); \ | 258 pObj->SetEmbedObject(new class_alternate(pObj)); \ |
261 FXJS_SetPrivate(nullptr, obj, (void*)pObj); \ | 259 pEngine->SetObjectPrivate(obj, (void*)pObj); \ |
262 pObj->InitInstance(static_cast<CJS_Runtime*>(pEngine)); \ | 260 pObj->InitInstance(static_cast<CJS_Runtime*>(pEngine)); \ |
263 } \ | 261 } \ |
264 void js_class_name::JSDestructor(v8::Local<v8::Object> obj) { \ | 262 void js_class_name::JSDestructor(CFXJS_Engine* pEngine, \ |
265 js_class_name* pObj = (js_class_name*)FXJS_GetPrivate(nullptr, obj); \ | 263 v8::Local<v8::Object> obj) { \ |
266 pObj->ExitInstance(); \ | 264 js_class_name* pObj = \ |
267 delete pObj; \ | 265 static_cast<js_class_name*>(pEngine->GetObjectPrivate(obj)); \ |
268 } \ | 266 pObj->ExitInstance(); \ |
269 void js_class_name::DefineProps(v8::Isolate* pIsolate) { \ | 267 delete pObj; \ |
270 for (size_t i = 0; i < FX_ArraySize(JS_Class_Properties) - 1; ++i) { \ | 268 } \ |
271 FXJS_DefineObjProperty( \ | 269 void js_class_name::DefineProps(CFXJS_Engine* pEngine) { \ |
272 pIsolate, g_nObjDefnID, JS_Class_Properties[i].pName, \ | 270 for (size_t i = 0; i < FX_ArraySize(JS_Class_Properties) - 1; ++i) { \ |
273 JS_Class_Properties[i].pPropGet, JS_Class_Properties[i].pPropPut); \ | 271 pEngine->DefineObjProperty(g_nObjDefnID, JS_Class_Properties[i].pName, \ |
274 } \ | 272 JS_Class_Properties[i].pPropGet, \ |
275 } \ | 273 JS_Class_Properties[i].pPropPut); \ |
276 void js_class_name::DefineMethods(v8::Isolate* pIsolate) { \ | 274 } \ |
277 for (size_t i = 0; i < FX_ArraySize(JS_Class_Methods) - 1; ++i) { \ | 275 } \ |
278 FXJS_DefineObjMethod(pIsolate, g_nObjDefnID, JS_Class_Methods[i].pName, \ | 276 void js_class_name::DefineMethods(CFXJS_Engine* pEngine) { \ |
279 JS_Class_Methods[i].pMethodCall); \ | 277 for (size_t i = 0; i < FX_ArraySize(JS_Class_Methods) - 1; ++i) { \ |
280 } \ | 278 pEngine->DefineObjMethod(g_nObjDefnID, JS_Class_Methods[i].pName, \ |
| 279 JS_Class_Methods[i].pMethodCall); \ |
| 280 } \ |
281 } | 281 } |
282 | 282 |
283 // Special JS classes implement methods, props, and queries, but not consts. | 283 // Special JS classes implement methods, props, and queries, but not consts. |
284 #define DECLARE_SPECIAL_JS_CLASS() \ | 284 #define DECLARE_SPECIAL_JS_CLASS() \ |
285 DECLARE_JS_CLASS_BASE_PART() \ | 285 DECLARE_JS_CLASS_BASE_PART() \ |
286 DECLARE_JS_CLASS_CONST_PART() \ | 286 DECLARE_JS_CLASS_CONST_PART() \ |
287 DECLARE_JS_CLASS_RICH_PART() \ | 287 DECLARE_JS_CLASS_RICH_PART() \ |
288 DECLARE_SPECIAL_JS_CLASS_PART() | 288 DECLARE_SPECIAL_JS_CLASS_PART() |
289 | 289 |
290 #define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \ | 290 #define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \ |
291 IMPLEMENT_JS_CLASS_BASE_PART(js_class_name, class_name) \ | 291 IMPLEMENT_JS_CLASS_BASE_PART(js_class_name, class_name) \ |
292 IMPLEMENT_JS_CLASS_CONST_PART(js_class_name, class_name) \ | 292 IMPLEMENT_JS_CLASS_CONST_PART(js_class_name, class_name) \ |
293 IMPLEMENT_JS_CLASS_RICH_PART(js_class_name, class_alternate, class_name) \ | 293 IMPLEMENT_JS_CLASS_RICH_PART(js_class_name, class_alternate, class_name) \ |
294 IMPLEMENT_SPECIAL_JS_CLASS_PART(js_class_name, class_alternate, class_name) \ | 294 IMPLEMENT_SPECIAL_JS_CLASS_PART(js_class_name, class_alternate, class_name) \ |
295 void js_class_name::DefineJSObjects(v8::Isolate* pIsolate, \ | 295 void js_class_name::DefineJSObjects(CFXJS_Engine* pEngine, \ |
296 FXJSOBJTYPE eObjType) { \ | 296 FXJSOBJTYPE eObjType) { \ |
297 g_nObjDefnID = FXJS_DefineObj(pIsolate, js_class_name::g_pClassName, \ | 297 g_nObjDefnID = pEngine->DefineObj(js_class_name::g_pClassName, eObjType, \ |
298 eObjType, JSConstructor, JSDestructor); \ | 298 JSConstructor, JSDestructor); \ |
299 DefineConsts(pIsolate); \ | 299 DefineConsts(pEngine); \ |
300 DefineProps(pIsolate); \ | 300 DefineProps(pEngine); \ |
301 DefineMethods(pIsolate); \ | 301 DefineMethods(pEngine); \ |
302 DefineAllProperties(pIsolate); \ | 302 DefineAllProperties(pEngine); \ |
303 } | 303 } |
304 | 304 |
305 #define DECLARE_SPECIAL_JS_CLASS_PART() \ | 305 #define DECLARE_SPECIAL_JS_CLASS_PART() \ |
306 static void queryprop_static( \ | 306 static void queryprop_static( \ |
307 v8::Local<v8::String> property, \ | 307 v8::Local<v8::String> property, \ |
308 const v8::PropertyCallbackInfo<v8::Integer>& info); \ | 308 const v8::PropertyCallbackInfo<v8::Integer>& info); \ |
309 static void getprop_static(v8::Local<v8::String> property, \ | 309 static void getprop_static(v8::Local<v8::String> property, \ |
310 const v8::PropertyCallbackInfo<v8::Value>& info); \ | 310 const v8::PropertyCallbackInfo<v8::Value>& info); \ |
311 static void putprop_static(v8::Local<v8::String> property, \ | 311 static void putprop_static(v8::Local<v8::String> property, \ |
312 v8::Local<v8::Value> value, \ | 312 v8::Local<v8::Value> value, \ |
313 const v8::PropertyCallbackInfo<v8::Value>& info); \ | 313 const v8::PropertyCallbackInfo<v8::Value>& info); \ |
314 static void delprop_static( \ | 314 static void delprop_static( \ |
315 v8::Local<v8::String> property, \ | 315 v8::Local<v8::String> property, \ |
316 const v8::PropertyCallbackInfo<v8::Boolean>& info); \ | 316 const v8::PropertyCallbackInfo<v8::Boolean>& info); \ |
317 static void DefineAllProperties(v8::Isolate* pIsolate); | 317 static void DefineAllProperties(CFXJS_Engine* pEngine); |
318 | 318 |
319 #define IMPLEMENT_SPECIAL_JS_CLASS_PART(js_class_name, class_alternate, \ | 319 #define IMPLEMENT_SPECIAL_JS_CLASS_PART(js_class_name, class_alternate, \ |
320 class_name) \ | 320 class_name) \ |
321 void js_class_name::queryprop_static( \ | 321 void js_class_name::queryprop_static( \ |
322 v8::Local<v8::String> property, \ | 322 v8::Local<v8::String> property, \ |
323 const v8::PropertyCallbackInfo<v8::Integer>& info) { \ | 323 const v8::PropertyCallbackInfo<v8::Integer>& info) { \ |
324 JSSpecialPropQuery<class_alternate>(#class_name, property, info); \ | 324 JSSpecialPropQuery<class_alternate>(#class_name, property, info); \ |
325 } \ | 325 } \ |
326 void js_class_name::getprop_static( \ | 326 void js_class_name::getprop_static( \ |
327 v8::Local<v8::String> property, \ | 327 v8::Local<v8::String> property, \ |
328 const v8::PropertyCallbackInfo<v8::Value>& info) { \ | 328 const v8::PropertyCallbackInfo<v8::Value>& info) { \ |
329 JSSpecialPropGet<class_alternate>(#class_name, property, info); \ | 329 JSSpecialPropGet<class_alternate>(#class_name, property, info); \ |
330 } \ | 330 } \ |
331 void js_class_name::putprop_static( \ | 331 void js_class_name::putprop_static( \ |
332 v8::Local<v8::String> property, v8::Local<v8::Value> value, \ | 332 v8::Local<v8::String> property, v8::Local<v8::Value> value, \ |
333 const v8::PropertyCallbackInfo<v8::Value>& info) { \ | 333 const v8::PropertyCallbackInfo<v8::Value>& info) { \ |
334 JSSpecialPropPut<class_alternate>(#class_name, property, value, info); \ | 334 JSSpecialPropPut<class_alternate>(#class_name, property, value, info); \ |
335 } \ | 335 } \ |
336 void js_class_name::delprop_static( \ | 336 void js_class_name::delprop_static( \ |
337 v8::Local<v8::String> property, \ | 337 v8::Local<v8::String> property, \ |
338 const v8::PropertyCallbackInfo<v8::Boolean>& info) { \ | 338 const v8::PropertyCallbackInfo<v8::Boolean>& info) { \ |
339 JSSpecialPropDel<class_alternate>(#class_name, property, info); \ | 339 JSSpecialPropDel<class_alternate>(#class_name, property, info); \ |
340 } \ | 340 } \ |
341 void js_class_name::DefineAllProperties(v8::Isolate* pIsolate) { \ | 341 void js_class_name::DefineAllProperties(CFXJS_Engine* pEngine) { \ |
342 FXJS_DefineObjAllProperties( \ | 342 pEngine->DefineObjAllProperties( \ |
343 pIsolate, g_nObjDefnID, js_class_name::queryprop_static, \ | 343 g_nObjDefnID, js_class_name::queryprop_static, \ |
344 js_class_name::getprop_static, js_class_name::putprop_static, \ | 344 js_class_name::getprop_static, js_class_name::putprop_static, \ |
345 js_class_name::delprop_static); \ | 345 js_class_name::delprop_static); \ |
346 } | 346 } |
347 | 347 |
348 template <class Alt> | 348 template <class Alt> |
349 void JSSpecialPropQuery(const char*, | 349 void JSSpecialPropQuery(const char*, |
350 v8::Local<v8::String> property, | 350 v8::Local<v8::String> property, |
351 const v8::PropertyCallbackInfo<v8::Integer>& info) { | 351 const v8::PropertyCallbackInfo<v8::Integer>& info) { |
352 v8::Isolate* isolate = info.GetIsolate(); | 352 CJS_Runtime* pRuntime = |
| 353 CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
353 v8::String::Utf8Value utf8_value(property); | 354 v8::String::Utf8Value utf8_value(property); |
354 CFX_WideString propname = CFX_WideString::FromUTF8( | 355 CFX_WideString propname = CFX_WideString::FromUTF8( |
355 CFX_ByteStringC(*utf8_value, utf8_value.length())); | 356 CFX_ByteStringC(*utf8_value, utf8_value.length())); |
356 CJS_Object* pJSObj = | 357 CJS_Object* pJSObj = |
357 reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder())); | 358 static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); |
358 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); | 359 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
359 FX_BOOL bRet = pObj->QueryProperty(propname.c_str()); | 360 FX_BOOL bRet = pObj->QueryProperty(propname.c_str()); |
360 info.GetReturnValue().Set(bRet ? 4 : 0); | 361 info.GetReturnValue().Set(bRet ? 4 : 0); |
361 } | 362 } |
362 | 363 |
363 template <class Alt> | 364 template <class Alt> |
364 void JSSpecialPropGet(const char* class_name, | 365 void JSSpecialPropGet(const char* class_name, |
365 v8::Local<v8::String> property, | 366 v8::Local<v8::String> property, |
366 const v8::PropertyCallbackInfo<v8::Value>& info) { | 367 const v8::PropertyCallbackInfo<v8::Value>& info) { |
367 v8::Isolate* isolate = info.GetIsolate(); | |
368 CJS_Runtime* pRuntime = | 368 CJS_Runtime* pRuntime = |
369 static_cast<CJS_Runtime*>(FXJS_GetCurrentEngineFromIsolate(isolate)); | 369 CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
370 if (!pRuntime) | 370 if (!pRuntime) |
371 return; | 371 return; |
372 IJS_Context* pContext = pRuntime->GetCurrentContext(); | |
373 CJS_Object* pJSObj = | 372 CJS_Object* pJSObj = |
374 reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder())); | 373 static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); |
375 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); | 374 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
376 v8::String::Utf8Value utf8_value(property); | 375 v8::String::Utf8Value utf8_value(property); |
377 CFX_WideString propname = CFX_WideString::FromUTF8( | 376 CFX_WideString propname = CFX_WideString::FromUTF8( |
378 CFX_ByteStringC(*utf8_value, utf8_value.length())); | 377 CFX_ByteStringC(*utf8_value, utf8_value.length())); |
379 CFX_WideString sError; | 378 CFX_WideString sError; |
380 CJS_PropValue value(pRuntime); | 379 CJS_PropValue value(pRuntime); |
381 value.StartGetting(); | 380 value.StartGetting(); |
382 if (!pObj->DoProperty(pContext, propname.c_str(), value, sError)) { | 381 if (!pObj->DoProperty(pRuntime->GetCurrentContext(), propname.c_str(), value, |
383 FXJS_Error(isolate, JSFormatErrorString(class_name, "GetProperty", sError)); | 382 sError)) { |
| 383 pRuntime->Error(JSFormatErrorString(class_name, "GetProperty", sError)); |
384 return; | 384 return; |
385 } | 385 } |
386 info.GetReturnValue().Set(value.GetJSValue()->ToV8Value(isolate)); | 386 info.GetReturnValue().Set(value.GetJSValue()->ToV8Value(pRuntime)); |
387 } | 387 } |
388 | 388 |
389 template <class Alt> | 389 template <class Alt> |
390 void JSSpecialPropPut(const char* class_name, | 390 void JSSpecialPropPut(const char* class_name, |
391 v8::Local<v8::String> property, | 391 v8::Local<v8::String> property, |
392 v8::Local<v8::Value> value, | 392 v8::Local<v8::Value> value, |
393 const v8::PropertyCallbackInfo<v8::Value>& info) { | 393 const v8::PropertyCallbackInfo<v8::Value>& info) { |
394 v8::Isolate* isolate = info.GetIsolate(); | |
395 CJS_Runtime* pRuntime = | 394 CJS_Runtime* pRuntime = |
396 static_cast<CJS_Runtime*>(FXJS_GetCurrentEngineFromIsolate(isolate)); | 395 CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
397 if (!pRuntime) | 396 if (!pRuntime) |
398 return; | 397 return; |
399 IJS_Context* pContext = pRuntime->GetCurrentContext(); | |
400 CJS_Object* pJSObj = | 398 CJS_Object* pJSObj = |
401 reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder())); | 399 static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); |
402 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); | 400 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
403 v8::String::Utf8Value utf8_value(property); | 401 v8::String::Utf8Value utf8_value(property); |
404 CFX_WideString propname = CFX_WideString::FromUTF8( | 402 CFX_WideString propname = CFX_WideString::FromUTF8( |
405 CFX_ByteStringC(*utf8_value, utf8_value.length())); | 403 CFX_ByteStringC(*utf8_value, utf8_value.length())); |
406 CFX_WideString sError; | 404 CFX_WideString sError; |
407 CJS_PropValue PropValue(pRuntime, CJS_Value(pRuntime, value)); | 405 CJS_PropValue PropValue(pRuntime, CJS_Value(pRuntime, value)); |
408 PropValue.StartSetting(); | 406 PropValue.StartSetting(); |
409 if (!pObj->DoProperty(pContext, propname.c_str(), PropValue, sError)) { | 407 if (!pObj->DoProperty(pRuntime->GetCurrentContext(), propname.c_str(), |
410 FXJS_Error(isolate, JSFormatErrorString(class_name, "PutProperty", sError)); | 408 PropValue, sError)) { |
| 409 pRuntime->Error(JSFormatErrorString(class_name, "PutProperty", sError)); |
411 } | 410 } |
412 } | 411 } |
413 | 412 |
414 template <class Alt> | 413 template <class Alt> |
415 void JSSpecialPropDel(const char* class_name, | 414 void JSSpecialPropDel(const char* class_name, |
416 v8::Local<v8::String> property, | 415 v8::Local<v8::String> property, |
417 const v8::PropertyCallbackInfo<v8::Boolean>& info) { | 416 const v8::PropertyCallbackInfo<v8::Boolean>& info) { |
418 v8::Isolate* isolate = info.GetIsolate(); | |
419 CJS_Runtime* pRuntime = | 417 CJS_Runtime* pRuntime = |
420 static_cast<CJS_Runtime*>(FXJS_GetCurrentEngineFromIsolate(isolate)); | 418 CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
421 if (!pRuntime) | 419 if (!pRuntime) |
422 return; | 420 return; |
423 IJS_Context* pContext = pRuntime->GetCurrentContext(); | |
424 CJS_Object* pJSObj = | 421 CJS_Object* pJSObj = |
425 reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder())); | 422 static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); |
426 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); | 423 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
427 v8::String::Utf8Value utf8_value(property); | 424 v8::String::Utf8Value utf8_value(property); |
428 CFX_WideString propname = CFX_WideString::FromUTF8( | 425 CFX_WideString propname = CFX_WideString::FromUTF8( |
429 CFX_ByteStringC(*utf8_value, utf8_value.length())); | 426 CFX_ByteStringC(*utf8_value, utf8_value.length())); |
430 CFX_WideString sError; | 427 CFX_WideString sError; |
431 if (!pObj->DelProperty(pContext, propname.c_str(), sError)) { | 428 if (!pObj->DelProperty(pRuntime->GetCurrentContext(), propname.c_str(), |
| 429 sError)) { |
432 CFX_ByteString cbName; | 430 CFX_ByteString cbName; |
433 cbName.Format("%s.%s", class_name, "DelProperty"); | 431 cbName.Format("%s.%s", class_name, "DelProperty"); |
434 // Probably a missing call to JSFX_Error(). | 432 // Probably a missing call to JSFX_Error(). |
435 } | 433 } |
436 } | 434 } |
437 | 435 |
438 template <FX_BOOL (*F)(IJS_Context*, | 436 template <FX_BOOL (*F)(IJS_Context*, |
439 const std::vector<CJS_Value>&, | 437 const std::vector<CJS_Value>&, |
440 CJS_Value&, | 438 CJS_Value&, |
441 CFX_WideString&)> | 439 CFX_WideString&)> |
442 void JSGlobalFunc(const char* func_name_string, | 440 void JSGlobalFunc(const char* func_name_string, |
443 const v8::FunctionCallbackInfo<v8::Value>& info) { | 441 const v8::FunctionCallbackInfo<v8::Value>& info) { |
444 CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>( | 442 CJS_Runtime* pRuntime = |
445 FXJS_GetCurrentEngineFromIsolate(info.GetIsolate())); | 443 CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
446 if (!pRuntime) | 444 if (!pRuntime) |
447 return; | 445 return; |
448 IJS_Context* pContext = pRuntime->GetCurrentContext(); | |
449 std::vector<CJS_Value> parameters; | 446 std::vector<CJS_Value> parameters; |
450 for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { | 447 for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { |
451 parameters.push_back(CJS_Value(pRuntime, info[i])); | 448 parameters.push_back(CJS_Value(pRuntime, info[i])); |
452 } | 449 } |
453 CJS_Value valueRes(pRuntime); | 450 CJS_Value valueRes(pRuntime); |
454 CFX_WideString sError; | 451 CFX_WideString sError; |
455 if (!(*F)(pContext, parameters, valueRes, sError)) { | 452 if (!(*F)(pRuntime->GetCurrentContext(), parameters, valueRes, sError)) { |
456 FXJS_Error(pRuntime->GetIsolate(), | 453 pRuntime->Error(JSFormatErrorString(func_name_string, nullptr, sError)); |
457 JSFormatErrorString(func_name_string, nullptr, sError)); | |
458 return; | 454 return; |
459 } | 455 } |
460 info.GetReturnValue().Set(valueRes.ToV8Value(pRuntime->GetIsolate())); | 456 info.GetReturnValue().Set(valueRes.ToV8Value(pRuntime)); |
461 } | 457 } |
462 | 458 |
463 #define JS_STATIC_GLOBAL_FUN(fun_name) \ | 459 #define JS_STATIC_GLOBAL_FUN(fun_name) \ |
464 static void fun_name##_static( \ | 460 static void fun_name##_static( \ |
465 const v8::FunctionCallbackInfo<v8::Value>& info) { \ | 461 const v8::FunctionCallbackInfo<v8::Value>& info) { \ |
466 JSGlobalFunc<fun_name>(#fun_name, info); \ | 462 JSGlobalFunc<fun_name>(#fun_name, info); \ |
467 } | 463 } |
468 | 464 |
469 #define JS_STATIC_DECLARE_GLOBAL_FUN() \ | 465 #define JS_STATIC_DECLARE_GLOBAL_FUN() \ |
470 static JSMethodSpec global_methods[]; \ | 466 static JSMethodSpec global_methods[]; \ |
471 static void DefineJSObjects(v8::Isolate* pIsolate) | 467 static void DefineJSObjects(CFXJS_Engine* pEngine) |
472 | 468 |
473 #define BEGIN_JS_STATIC_GLOBAL_FUN(js_class_name) \ | 469 #define BEGIN_JS_STATIC_GLOBAL_FUN(js_class_name) \ |
474 JSMethodSpec js_class_name::global_methods[] = { | 470 JSMethodSpec js_class_name::global_methods[] = { |
475 #define JS_STATIC_GLOBAL_FUN_ENTRY(method_name) \ | 471 #define JS_STATIC_GLOBAL_FUN_ENTRY(method_name) \ |
476 JS_STATIC_METHOD_ENTRY(method_name) | 472 JS_STATIC_METHOD_ENTRY(method_name) |
477 | 473 |
478 #define END_JS_STATIC_GLOBAL_FUN() END_JS_STATIC_METHOD() | 474 #define END_JS_STATIC_GLOBAL_FUN() END_JS_STATIC_METHOD() |
479 | 475 |
480 #define IMPLEMENT_JS_STATIC_GLOBAL_FUN(js_class_name) \ | 476 #define IMPLEMENT_JS_STATIC_GLOBAL_FUN(js_class_name) \ |
481 void js_class_name::DefineJSObjects(v8::Isolate* pIsolate) { \ | 477 void js_class_name::DefineJSObjects(CFXJS_Engine* pEngine) { \ |
482 for (size_t i = 0; i < FX_ArraySize(global_methods) - 1; ++i) { \ | 478 for (size_t i = 0; i < FX_ArraySize(global_methods) - 1; ++i) { \ |
483 FXJS_DefineGlobalMethod(pIsolate, \ | 479 pEngine->DefineGlobalMethod( \ |
484 js_class_name::global_methods[i].pName, \ | 480 js_class_name::global_methods[i].pName, \ |
485 js_class_name::global_methods[i].pMethodCall); \ | 481 js_class_name::global_methods[i].pMethodCall); \ |
486 } \ | 482 } \ |
487 } | 483 } |
488 | 484 |
489 #endif // FPDFSDK_JAVASCRIPT_JS_DEFINE_H_ | 485 #endif // FPDFSDK_JAVASCRIPT_JS_DEFINE_H_ |
OLD | NEW |