John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 7 | #ifndef FPDFSDK_SRC_JAVASCRIPT_JS_DEFINE_H_ |
| 8 | #define FPDFSDK_SRC_JAVASCRIPT_JS_DEFINE_H_ |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 9 | |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 10 | #include "../../include/jsapi/fxjs_v8.h" |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 11 | #include "resource.h" |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 12 | #include "JS_Object.h" |
| 13 | #include "JS_Value.h" |
| 14 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 15 | struct JSConstSpec { |
| 16 | const wchar_t* pName; |
| 17 | double number; |
| 18 | const wchar_t* string; |
| 19 | uint8_t t; // 0:double 1:str |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 20 | }; |
| 21 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 22 | struct JSPropertySpec { |
| 23 | const wchar_t* pName; |
| 24 | v8::AccessorGetterCallback pPropGet; |
| 25 | v8::AccessorSetterCallback pPropPut; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 28 | struct JSMethodSpec { |
| 29 | const wchar_t* pName; |
| 30 | v8::FunctionCallback pMethodCall; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 33 | #define JS_WIDESTRING(widestring) L## #widestring |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 34 | #define BEGIN_JS_STATIC_CONST(js_class_name) \ |
| 35 | JSConstSpec js_class_name::JS_Class_Consts[] = { |
| 36 | #define JS_STATIC_CONST_ENTRY_NUMBER(const_name, pValue) \ |
Nico Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 37 | { const_name, pValue, L"", 0 } \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 38 | , |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame^] | 39 | |
Nico Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 40 | #define JS_STATIC_CONST_ENTRY_STRING(const_name, pValue) \ |
| 41 | { const_name, 0, pValue, 1 } \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 42 | , |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame^] | 43 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 44 | #define END_JS_STATIC_CONST() \ |
| 45 | { 0, 0, 0, 0 } \ |
| 46 | } \ |
| 47 | ; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 48 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 49 | #define BEGIN_JS_STATIC_PROP(js_class_name) \ |
| 50 | JSPropertySpec js_class_name::JS_Class_Properties[] = { |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 51 | #define JS_STATIC_PROP_ENTRY(prop_name) \ |
| 52 | { \ |
| 53 | JS_WIDESTRING(prop_name), get_##prop_name##_static, \ |
| 54 | set_##prop_name##_static \ |
| 55 | } \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 56 | , |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame^] | 57 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 58 | #define END_JS_STATIC_PROP() \ |
| 59 | { 0, 0, 0 } \ |
| 60 | } \ |
| 61 | ; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 62 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 63 | #define BEGIN_JS_STATIC_METHOD(js_class_name) \ |
| 64 | JSMethodSpec js_class_name::JS_Class_Methods[] = { |
| 65 | #define JS_STATIC_METHOD_ENTRY(method_name) \ |
| 66 | { JS_WIDESTRING(method_name), method_name##_static } \ |
| 67 | , |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame^] | 68 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 69 | #define END_JS_STATIC_METHOD() \ |
| 70 | { 0, 0 } \ |
| 71 | } \ |
| 72 | ; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 73 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 74 | template <class C, |
| 75 | FX_BOOL (C::*M)(IFXJS_Context*, CJS_PropValue&, CFX_WideString&)> |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 76 | void JSPropGetter(const char* prop_name_string, |
| 77 | const char* class_name_string, |
| 78 | v8::Local<v8::String> property, |
| 79 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 80 | v8::Isolate* isolate = info.GetIsolate(); |
Tom Sepez | bd7fabf | 2015-09-28 10:31:27 -0700 | [diff] [blame] | 81 | IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate); |
| 82 | if (!pRuntime) |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 83 | return; |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 84 | IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 85 | CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, info.Holder()); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 86 | C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 87 | CFX_WideString sError; |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 88 | CJS_PropValue value(isolate); |
| 89 | value.StartGetting(); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 90 | if (!(pObj->*M)(pRuntimeContext, value, sError)) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 91 | FXJS_Error(isolate, JSFormatErrorString(class_name_string, prop_name_string, |
| 92 | sError)); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 93 | return; |
| 94 | } |
Jochen Eisinger | dfa2c99 | 2015-05-19 00:38:00 +0200 | [diff] [blame] | 95 | info.GetReturnValue().Set((v8::Local<v8::Value>)value); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 98 | template <class C, |
| 99 | FX_BOOL (C::*M)(IFXJS_Context*, CJS_PropValue&, CFX_WideString&)> |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 100 | void JSPropSetter(const char* prop_name_string, |
| 101 | const char* class_name_string, |
| 102 | v8::Local<v8::String> property, |
| 103 | v8::Local<v8::Value> value, |
| 104 | const v8::PropertyCallbackInfo<void>& info) { |
| 105 | v8::Isolate* isolate = info.GetIsolate(); |
Tom Sepez | bd7fabf | 2015-09-28 10:31:27 -0700 | [diff] [blame] | 106 | IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate); |
| 107 | if (!pRuntime) |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 108 | return; |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 109 | IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 110 | CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, info.Holder()); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 111 | C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 112 | CFX_WideString sError; |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 113 | CJS_PropValue propValue(CJS_Value(isolate, value, CJS_Value::VT_unknown)); |
| 114 | propValue.StartSetting(); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 115 | if (!(pObj->*M)(pRuntimeContext, propValue, sError)) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 116 | FXJS_Error(isolate, JSFormatErrorString(class_name_string, prop_name_string, |
| 117 | sError)); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 118 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 121 | #define JS_STATIC_PROP(prop_name, class_name) \ |
| 122 | static void get_##prop_name##_static( \ |
| 123 | v8::Local<v8::String> property, \ |
| 124 | const v8::PropertyCallbackInfo<v8::Value>& info) { \ |
| 125 | JSPropGetter<class_name, &class_name::prop_name>(#prop_name, #class_name, \ |
| 126 | property, info); \ |
| 127 | } \ |
| 128 | static void set_##prop_name##_static( \ |
| 129 | v8::Local<v8::String> property, v8::Local<v8::Value> value, \ |
| 130 | const v8::PropertyCallbackInfo<void>& info) { \ |
| 131 | JSPropSetter<class_name, &class_name::prop_name>(#prop_name, #class_name, \ |
| 132 | property, value, info); \ |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 133 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 134 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 135 | template <class C, |
| 136 | FX_BOOL (C::*M)(IFXJS_Context*, |
| 137 | const CJS_Parameters&, |
| 138 | CJS_Value&, |
| 139 | CFX_WideString&)> |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 140 | void JSMethod(const char* method_name_string, |
| 141 | const char* class_name_string, |
| 142 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 143 | v8::Isolate* isolate = info.GetIsolate(); |
Tom Sepez | bd7fabf | 2015-09-28 10:31:27 -0700 | [diff] [blame] | 144 | IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate); |
| 145 | if (!pRuntime) |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 146 | return; |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 147 | IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 148 | CJS_Parameters parameters; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 149 | for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 150 | parameters.push_back(CJS_Value(isolate, info[i], CJS_Value::VT_unknown)); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 151 | } |
| 152 | CJS_Value valueRes(isolate); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 153 | CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, info.Holder()); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 154 | C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 155 | CFX_WideString sError; |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 156 | if (!(pObj->*M)(pRuntimeContext, parameters, valueRes, sError)) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 157 | FXJS_Error(isolate, JSFormatErrorString(class_name_string, |
| 158 | method_name_string, sError)); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 159 | return; |
| 160 | } |
Tom Sepez | f4ef3f9 | 2015-04-23 11:31:31 -0700 | [diff] [blame] | 161 | info.GetReturnValue().Set(valueRes.ToV8Value()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 164 | #define JS_STATIC_METHOD(method_name, class_name) \ |
| 165 | static void method_name##_static( \ |
| 166 | const v8::FunctionCallbackInfo<v8::Value>& info) { \ |
| 167 | JSMethod<class_name, &class_name::method_name>(#method_name, #class_name, \ |
| 168 | info); \ |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | #define JS_SPECIAL_STATIC_METHOD(method_name, class_alternate, class_name) \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 172 | static void method_name##_static( \ |
| 173 | const v8::FunctionCallbackInfo<v8::Value>& info) { \ |
| 174 | JSMethod<class_alternate, &class_alternate::method_name>( \ |
| 175 | #method_name, #class_name, info); \ |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame^] | 178 | // All JS classes have a name, an object defintion ID, and the ability to |
| 179 | // register themselves with FXJS_V8. We never make a BASE class on its own |
| 180 | // because it can't really do anything. |
| 181 | #define DECLARE_JS_CLASS_BASE_PART() \ |
| 182 | static const wchar_t* g_pClassName; \ |
| 183 | static int g_nObjDefnID; \ |
| 184 | static void DefineJSObjects(v8::Isolate* pIsolate, FXJSOBJTYPE eObjType); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 185 | |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame^] | 186 | #define IMPLEMENT_JS_CLASS_BASE_PART(js_class_name, class_name) \ |
| 187 | const wchar_t* js_class_name::g_pClassName = JS_WIDESTRING(class_name); \ |
| 188 | int js_class_name::g_nObjDefnID = -1; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 189 | |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame^] | 190 | // CONST classes provide constants, but not constructors, methods, or props. |
| 191 | #define DECLARE_JS_CLASS_CONST() \ |
| 192 | DECLARE_JS_CLASS_BASE_PART() \ |
| 193 | DECLARE_JS_CLASS_CONST_PART() |
| 194 | |
| 195 | #define IMPLEMENT_JS_CLASS_CONST(js_class_name, class_name) \ |
| 196 | IMPLEMENT_JS_CLASS_BASE_PART(js_class_name, class_name) \ |
| 197 | IMPLEMENT_JS_CLASS_CONST_PART(js_class_name, class_name) \ |
| 198 | void js_class_name::DefineJSObjects(v8::Isolate* pIsolate, \ |
| 199 | FXJSOBJTYPE eObjType) { \ |
| 200 | g_nObjDefnID = FXJS_DefineObj(pIsolate, js_class_name::g_pClassName, \ |
| 201 | eObjType, nullptr, nullptr); \ |
| 202 | DefineConsts(pIsolate); \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 203 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 204 | |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame^] | 205 | #define DECLARE_JS_CLASS_CONST_PART() \ |
| 206 | static JSConstSpec JS_Class_Consts[]; \ |
| 207 | static void DefineConsts(v8::Isolate* pIsolate); |
| 208 | |
| 209 | #define IMPLEMENT_JS_CLASS_CONST_PART(js_class_name, class_name) \ |
| 210 | void js_class_name::DefineConsts(v8::Isolate* pIsolate) { \ |
| 211 | for (size_t i = 0; i < FX_ArraySize(JS_Class_Consts) - 1; ++i) { \ |
| 212 | FXJS_DefineObjConst( \ |
| 213 | pIsolate, g_nObjDefnID, JS_Class_Consts[i].pName, \ |
| 214 | JS_Class_Consts[i].t == 0 \ |
| 215 | ? FXJS_NewNumber(pIsolate, JS_Class_Consts[i].number) \ |
| 216 | : FXJS_NewString(pIsolate, JS_Class_Consts[i].string)); \ |
| 217 | } \ |
| 218 | } |
| 219 | |
| 220 | // Convenience macros for declaring classes without an alternate. |
| 221 | #define DECLARE_JS_CLASS() DECLARE_JS_CLASS_RICH() |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 222 | #define IMPLEMENT_JS_CLASS(js_class_name, class_name) \ |
| 223 | IMPLEMENT_JS_CLASS_RICH(js_class_name, class_name, class_name) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 224 | |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame^] | 225 | // Rich JS classes provide constants, methods, properties, and the ability |
| 226 | // to construct native object state. |
| 227 | #define DECLARE_JS_CLASS_RICH() \ |
| 228 | DECLARE_JS_CLASS_BASE_PART() \ |
| 229 | DECLARE_JS_CLASS_CONST_PART() \ |
| 230 | DECLARE_JS_CLASS_RICH_PART() |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 231 | |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame^] | 232 | #define IMPLEMENT_JS_CLASS_RICH(js_class_name, class_alternate, class_name) \ |
| 233 | IMPLEMENT_JS_CLASS_BASE_PART(js_class_name, class_name) \ |
| 234 | IMPLEMENT_JS_CLASS_CONST_PART(js_class_name, class_name) \ |
| 235 | IMPLEMENT_JS_CLASS_RICH_PART(js_class_name, class_alternate, class_name) \ |
| 236 | void js_class_name::DefineJSObjects(v8::Isolate* pIsolate, \ |
| 237 | FXJSOBJTYPE eObjType) { \ |
| 238 | g_nObjDefnID = FXJS_DefineObj(pIsolate, js_class_name::g_pClassName, \ |
| 239 | eObjType, JSConstructor, JSDestructor); \ |
| 240 | DefineConsts(pIsolate); \ |
| 241 | DefineProps(pIsolate); \ |
| 242 | DefineMethods(pIsolate); \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 243 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 244 | |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame^] | 245 | #define DECLARE_JS_CLASS_RICH_PART() \ |
| 246 | static void JSConstructor(IFXJS_Context* cc, v8::Local<v8::Object> obj, \ |
| 247 | v8::Local<v8::Object> global); \ |
| 248 | static void JSDestructor(v8::Local<v8::Object> obj); \ |
| 249 | static void DefineProps(v8::Isolate* pIsoalte); \ |
| 250 | static void DefineMethods(v8::Isolate* pIsoalte); \ |
| 251 | static JSPropertySpec JS_Class_Properties[]; \ |
| 252 | static JSMethodSpec JS_Class_Methods[]; |
| 253 | |
| 254 | #define IMPLEMENT_JS_CLASS_RICH_PART(js_class_name, class_alternate, \ |
| 255 | class_name) \ |
| 256 | void js_class_name::JSConstructor(IFXJS_Context* cc, \ |
| 257 | v8::Local<v8::Object> obj, \ |
| 258 | v8::Local<v8::Object> global) { \ |
| 259 | CJS_Object* pObj = new js_class_name(obj); \ |
| 260 | pObj->SetEmbedObject(new class_alternate(pObj)); \ |
| 261 | FXJS_SetPrivate(nullptr, obj, (void*)pObj); \ |
| 262 | pObj->InitInstance(cc); \ |
| 263 | } \ |
| 264 | void js_class_name::JSDestructor(v8::Local<v8::Object> obj) { \ |
| 265 | js_class_name* pObj = (js_class_name*)FXJS_GetPrivate(nullptr, obj); \ |
| 266 | pObj->ExitInstance(); \ |
| 267 | delete pObj; \ |
| 268 | } \ |
| 269 | void js_class_name::DefineProps(v8::Isolate* pIsolate) { \ |
| 270 | for (size_t i = 0; i < FX_ArraySize(JS_Class_Properties) - 1; ++i) { \ |
| 271 | FXJS_DefineObjProperty( \ |
| 272 | pIsolate, g_nObjDefnID, JS_Class_Properties[i].pName, \ |
| 273 | JS_Class_Properties[i].pPropGet, JS_Class_Properties[i].pPropPut); \ |
| 274 | } \ |
| 275 | } \ |
| 276 | void js_class_name::DefineMethods(v8::Isolate* pIsolate) { \ |
| 277 | for (size_t i = 0; i < FX_ArraySize(JS_Class_Methods) - 1; ++i) { \ |
| 278 | FXJS_DefineObjMethod(pIsolate, g_nObjDefnID, JS_Class_Methods[i].pName, \ |
| 279 | JS_Class_Methods[i].pMethodCall); \ |
| 280 | } \ |
| 281 | } |
| 282 | |
| 283 | // Special JS classes implement methods, props, and queries, but not consts. |
| 284 | #define DECLARE_SPECIAL_JS_CLASS() \ |
| 285 | DECLARE_JS_CLASS_BASE_PART() \ |
| 286 | DECLARE_JS_CLASS_CONST_PART() \ |
| 287 | DECLARE_JS_CLASS_RICH_PART() \ |
| 288 | DECLARE_SPECIAL_JS_CLASS_PART() |
| 289 | |
| 290 | #define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \ |
| 291 | IMPLEMENT_JS_CLASS_BASE_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) \ |
| 294 | IMPLEMENT_SPECIAL_JS_CLASS_PART(js_class_name, class_alternate, class_name) \ |
| 295 | void js_class_name::DefineJSObjects(v8::Isolate* pIsolate, \ |
| 296 | FXJSOBJTYPE eObjType) { \ |
| 297 | g_nObjDefnID = FXJS_DefineObj(pIsolate, js_class_name::g_pClassName, \ |
| 298 | eObjType, JSConstructor, JSDestructor); \ |
| 299 | DefineConsts(pIsolate); \ |
| 300 | DefineProps(pIsolate); \ |
| 301 | DefineMethods(pIsolate); \ |
| 302 | DefineAllProperties(pIsolate); \ |
| 303 | } |
| 304 | |
| 305 | #define DECLARE_SPECIAL_JS_CLASS_PART() \ |
| 306 | static void queryprop_static( \ |
| 307 | v8::Local<v8::String> property, \ |
| 308 | const v8::PropertyCallbackInfo<v8::Integer>& info); \ |
| 309 | static void getprop_static(v8::Local<v8::String> property, \ |
| 310 | const v8::PropertyCallbackInfo<v8::Value>& info); \ |
| 311 | static void putprop_static(v8::Local<v8::String> property, \ |
| 312 | v8::Local<v8::Value> value, \ |
| 313 | const v8::PropertyCallbackInfo<v8::Value>& info); \ |
| 314 | static void delprop_static( \ |
| 315 | v8::Local<v8::String> property, \ |
| 316 | const v8::PropertyCallbackInfo<v8::Boolean>& info); \ |
| 317 | static void DefineAllProperties(v8::Isolate* pIsolate); |
| 318 | |
| 319 | #define IMPLEMENT_SPECIAL_JS_CLASS_PART(js_class_name, class_alternate, \ |
| 320 | class_name) \ |
| 321 | void js_class_name::queryprop_static( \ |
| 322 | v8::Local<v8::String> property, \ |
| 323 | const v8::PropertyCallbackInfo<v8::Integer>& info) { \ |
| 324 | JSSpecialPropQuery<class_alternate>(#class_name, property, info); \ |
| 325 | } \ |
| 326 | void js_class_name::getprop_static( \ |
| 327 | v8::Local<v8::String> property, \ |
| 328 | const v8::PropertyCallbackInfo<v8::Value>& info) { \ |
| 329 | JSSpecialPropGet<class_alternate>(#class_name, property, info); \ |
| 330 | } \ |
| 331 | void js_class_name::putprop_static( \ |
| 332 | v8::Local<v8::String> property, v8::Local<v8::Value> value, \ |
| 333 | const v8::PropertyCallbackInfo<v8::Value>& info) { \ |
| 334 | JSSpecialPropPut<class_alternate>(#class_name, property, value, info); \ |
| 335 | } \ |
| 336 | void js_class_name::delprop_static( \ |
| 337 | v8::Local<v8::String> property, \ |
| 338 | const v8::PropertyCallbackInfo<v8::Boolean>& info) { \ |
| 339 | JSSpecialPropDel<class_alternate>(#class_name, property, info); \ |
| 340 | } \ |
| 341 | void js_class_name::DefineAllProperties(v8::Isolate* pIsolate) { \ |
| 342 | FXJS_DefineObjAllProperties( \ |
| 343 | pIsolate, g_nObjDefnID, js_class_name::queryprop_static, \ |
| 344 | js_class_name::getprop_static, js_class_name::putprop_static, \ |
| 345 | js_class_name::delprop_static); \ |
| 346 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 347 | |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 348 | template <class Alt> |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 349 | void JSSpecialPropQuery(const char*, |
| 350 | v8::Local<v8::String> property, |
| 351 | const v8::PropertyCallbackInfo<v8::Integer>& info) { |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 352 | v8::Isolate* isolate = info.GetIsolate(); |
| 353 | v8::String::Utf8Value utf8_value(property); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 354 | CFX_WideString propname = |
| 355 | CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
| 356 | CJS_Object* pJSObj = |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 357 | reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder())); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 358 | Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
| 359 | FX_BOOL bRet = pObj->QueryProperty(propname.c_str()); |
| 360 | info.GetReturnValue().Set(bRet ? 4 : 0); |
| 361 | } |
| 362 | |
| 363 | template <class Alt> |
| 364 | void JSSpecialPropGet(const char* class_name, |
| 365 | v8::Local<v8::String> property, |
| 366 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 367 | v8::Isolate* isolate = info.GetIsolate(); |
Tom Sepez | bd7fabf | 2015-09-28 10:31:27 -0700 | [diff] [blame] | 368 | IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate); |
| 369 | if (!pRuntime) |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 370 | return; |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 371 | IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 372 | CJS_Object* pJSObj = |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 373 | reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder())); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 374 | Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
| 375 | v8::String::Utf8Value utf8_value(property); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 376 | CFX_WideString propname = |
| 377 | CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 378 | CFX_WideString sError; |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 379 | CJS_PropValue value(isolate); |
| 380 | value.StartGetting(); |
| 381 | if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), value, sError)) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 382 | FXJS_Error(isolate, JSFormatErrorString(class_name, "GetProperty", sError)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 383 | return; |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 384 | } |
Jochen Eisinger | dfa2c99 | 2015-05-19 00:38:00 +0200 | [diff] [blame] | 385 | info.GetReturnValue().Set((v8::Local<v8::Value>)value); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | template <class Alt> |
| 389 | void JSSpecialPropPut(const char* class_name, |
| 390 | v8::Local<v8::String> property, |
| 391 | v8::Local<v8::Value> value, |
| 392 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 393 | v8::Isolate* isolate = info.GetIsolate(); |
Tom Sepez | bd7fabf | 2015-09-28 10:31:27 -0700 | [diff] [blame] | 394 | IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate); |
| 395 | if (!pRuntime) |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 396 | return; |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 397 | IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 398 | CJS_Object* pJSObj = |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 399 | reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder())); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 400 | Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
| 401 | v8::String::Utf8Value utf8_value(property); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 402 | CFX_WideString propname = |
| 403 | CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 404 | CFX_WideString sError; |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 405 | CJS_PropValue PropValue(CJS_Value(isolate, value, CJS_Value::VT_unknown)); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 406 | PropValue.StartSetting(); |
| 407 | if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), PropValue, sError)) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 408 | FXJS_Error(isolate, JSFormatErrorString(class_name, "PutProperty", sError)); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | template <class Alt> |
| 413 | void JSSpecialPropDel(const char* class_name, |
| 414 | v8::Local<v8::String> property, |
| 415 | const v8::PropertyCallbackInfo<v8::Boolean>& info) { |
| 416 | v8::Isolate* isolate = info.GetIsolate(); |
Tom Sepez | bd7fabf | 2015-09-28 10:31:27 -0700 | [diff] [blame] | 417 | IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate); |
| 418 | if (!pRuntime) |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 419 | return; |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 420 | IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 421 | CJS_Object* pJSObj = |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 422 | reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder())); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 423 | Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
| 424 | v8::String::Utf8Value utf8_value(property); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 425 | CFX_WideString propname = |
| 426 | CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 427 | CFX_WideString sError; |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 428 | if (!pObj->DelProperty(pRuntimeContext, propname.c_str(), sError)) { |
| 429 | CFX_ByteString cbName; |
| 430 | cbName.Format("%s.%s", class_name, "DelProperty"); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 431 | // Probably a missing call to JSFX_Error(). |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame^] | 435 | template <FX_BOOL (*F)(IFXJS_Context* cc, |
| 436 | const CJS_Parameters& params, |
| 437 | CJS_Value& vRet, |
| 438 | CFX_WideString& sError)> |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 439 | void JSGlobalFunc(const char* func_name_string, |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 440 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 441 | v8::Isolate* isolate = info.GetIsolate(); |
Tom Sepez | bd7fabf | 2015-09-28 10:31:27 -0700 | [diff] [blame] | 442 | IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate); |
| 443 | if (!pRuntime) |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 444 | return; |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 445 | IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 446 | CJS_Parameters parameters; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 447 | for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 448 | parameters.push_back(CJS_Value(isolate, info[i], CJS_Value::VT_unknown)); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 449 | } |
| 450 | CJS_Value valueRes(isolate); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 451 | CFX_WideString sError; |
| 452 | if (!(*F)(pRuntimeContext, parameters, valueRes, sError)) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 453 | FXJS_Error(isolate, JSFormatErrorString(func_name_string, nullptr, sError)); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 454 | return; |
| 455 | } |
Tom Sepez | f4ef3f9 | 2015-04-23 11:31:31 -0700 | [diff] [blame] | 456 | info.GetReturnValue().Set(valueRes.ToV8Value()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 459 | #define JS_STATIC_GLOBAL_FUN(fun_name) \ |
| 460 | static void fun_name##_static( \ |
| 461 | const v8::FunctionCallbackInfo<v8::Value>& info) { \ |
| 462 | JSGlobalFunc<fun_name>(#fun_name, info); \ |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 463 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 464 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 465 | #define JS_STATIC_DECLARE_GLOBAL_FUN() \ |
| 466 | static JSMethodSpec global_methods[]; \ |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 467 | static void DefineJSObjects(v8::Isolate* pIsolate) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 468 | |
| 469 | #define BEGIN_JS_STATIC_GLOBAL_FUN(js_class_name) \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 470 | JSMethodSpec js_class_name::global_methods[] = { |
| 471 | #define JS_STATIC_GLOBAL_FUN_ENTRY(method_name) \ |
| 472 | JS_STATIC_METHOD_ENTRY(method_name) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 473 | |
| 474 | #define END_JS_STATIC_GLOBAL_FUN() END_JS_STATIC_METHOD() |
| 475 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 476 | #define IMPLEMENT_JS_STATIC_GLOBAL_FUN(js_class_name) \ |
| 477 | void js_class_name::DefineJSObjects(v8::Isolate* pIsolate) { \ |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame^] | 478 | for (size_t i = 0; i < FX_ArraySize(global_methods) - 1; ++i) { \ |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 479 | FXJS_DefineGlobalMethod(pIsolate, \ |
| 480 | js_class_name::global_methods[i].pName, \ |
| 481 | js_class_name::global_methods[i].pMethodCall); \ |
| 482 | } \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 483 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 484 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 485 | CJS_Value::Type GET_VALUE_TYPE(v8::Local<v8::Value> p); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 486 | |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 487 | #endif // FPDFSDK_SRC_JAVASCRIPT_JS_DEFINE_H_ |