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 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 7 | #ifndef FPDFSDK_JAVASCRIPT_JS_DEFINE_H_ |
| 8 | #define FPDFSDK_JAVASCRIPT_JS_DEFINE_H_ |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 9 | |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 12 | #include "fpdfsdk/javascript/JS_Object.h" |
| 13 | #include "fpdfsdk/javascript/JS_Value.h" |
| 14 | #include "fpdfsdk/javascript/resource.h" |
dsinclair | b3f2467 | 2016-07-12 10:42:14 -0700 | [diff] [blame] | 15 | #include "fxjs/include/fxjs_v8.h" |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 16 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 17 | struct JSConstSpec { |
| 18 | const wchar_t* pName; |
| 19 | double number; |
thestig | fbe14b9 | 2016-05-02 13:31:10 -0700 | [diff] [blame] | 20 | const wchar_t* str; |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 21 | uint8_t t; // 0:double 1:str |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 22 | }; |
| 23 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 24 | struct JSPropertySpec { |
| 25 | const wchar_t* pName; |
| 26 | v8::AccessorGetterCallback pPropGet; |
| 27 | v8::AccessorSetterCallback pPropPut; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 30 | struct JSMethodSpec { |
| 31 | const wchar_t* pName; |
| 32 | v8::FunctionCallback pMethodCall; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 35 | #define JS_WIDESTRING(widestring) L## #widestring |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 36 | #define BEGIN_JS_STATIC_CONST(js_class_name) \ |
| 37 | JSConstSpec js_class_name::JS_Class_Consts[] = { |
| 38 | #define JS_STATIC_CONST_ENTRY_NUMBER(const_name, pValue) \ |
Nico Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 39 | { const_name, pValue, L"", 0 } \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 40 | , |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 41 | |
Nico Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 42 | #define JS_STATIC_CONST_ENTRY_STRING(const_name, pValue) \ |
| 43 | { const_name, 0, pValue, 1 } \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 44 | , |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 45 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 46 | #define END_JS_STATIC_CONST() \ |
| 47 | { 0, 0, 0, 0 } \ |
| 48 | } \ |
Dan Sinclair | 50cce60 | 2016-02-24 09:51:16 -0500 | [diff] [blame] | 49 | ; // NOLINT |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 50 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 51 | #define BEGIN_JS_STATIC_PROP(js_class_name) \ |
| 52 | JSPropertySpec js_class_name::JS_Class_Properties[] = { |
thestig | fbe14b9 | 2016-05-02 13:31:10 -0700 | [diff] [blame] | 53 | #define JS_STATIC_PROP_ENTRY(prop_name) \ |
| 54 | {JS_WIDESTRING(prop_name), get_##prop_name##_static, \ |
| 55 | set_##prop_name##_static}, // NOLINT |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 56 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 57 | #define END_JS_STATIC_PROP() \ |
| 58 | { 0, 0, 0 } \ |
| 59 | } \ |
Dan Sinclair | 50cce60 | 2016-02-24 09:51:16 -0500 | [diff] [blame] | 60 | ; // NOLINT |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 61 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 62 | #define BEGIN_JS_STATIC_METHOD(js_class_name) \ |
| 63 | JSMethodSpec js_class_name::JS_Class_Methods[] = { |
| 64 | #define JS_STATIC_METHOD_ENTRY(method_name) \ |
| 65 | { JS_WIDESTRING(method_name), method_name##_static } \ |
| 66 | , |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 67 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 68 | #define END_JS_STATIC_METHOD() \ |
| 69 | { 0, 0 } \ |
| 70 | } \ |
Dan Sinclair | 50cce60 | 2016-02-24 09:51:16 -0500 | [diff] [blame] | 71 | ; // NOLINT |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 72 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 73 | template <class C, |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 74 | FX_BOOL (C::*M)(IJS_Context*, CJS_PropValue&, CFX_WideString&)> |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 75 | void JSPropGetter(const char* prop_name_string, |
| 76 | const char* class_name_string, |
| 77 | v8::Local<v8::String> property, |
| 78 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
Tom Sepez | b8ec0a3 | 2015-11-20 14:23:02 -0800 | [diff] [blame] | 79 | CJS_Runtime* pRuntime = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 80 | CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
Tom Sepez | bd7fabf | 2015-09-28 10:31:27 -0700 | [diff] [blame] | 81 | if (!pRuntime) |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 82 | return; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 83 | CJS_Object* pJSObj = |
| 84 | static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 85 | C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 86 | CFX_WideString sError; |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 87 | CJS_PropValue value(pRuntime); |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 88 | value.StartGetting(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 89 | if (!(pObj->*M)(pRuntime->GetCurrentContext(), value, sError)) { |
| 90 | pRuntime->Error( |
| 91 | JSFormatErrorString(class_name_string, prop_name_string, sError)); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 92 | return; |
| 93 | } |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 94 | info.GetReturnValue().Set(value.GetJSValue()->ToV8Value(pRuntime)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 97 | template <class C, |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 98 | FX_BOOL (C::*M)(IJS_Context*, CJS_PropValue&, CFX_WideString&)> |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 99 | void JSPropSetter(const char* prop_name_string, |
| 100 | const char* class_name_string, |
| 101 | v8::Local<v8::String> property, |
| 102 | v8::Local<v8::Value> value, |
| 103 | const v8::PropertyCallbackInfo<void>& info) { |
Tom Sepez | b8ec0a3 | 2015-11-20 14:23:02 -0800 | [diff] [blame] | 104 | CJS_Runtime* pRuntime = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 105 | CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
Tom Sepez | bd7fabf | 2015-09-28 10:31:27 -0700 | [diff] [blame] | 106 | if (!pRuntime) |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 107 | return; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 108 | CJS_Object* pJSObj = |
| 109 | static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 110 | C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 111 | CFX_WideString sError; |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 112 | CJS_PropValue propValue(pRuntime, CJS_Value(pRuntime, value)); |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 113 | propValue.StartSetting(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 114 | if (!(pObj->*M)(pRuntime->GetCurrentContext(), propValue, sError)) { |
| 115 | pRuntime->Error( |
| 116 | JSFormatErrorString(class_name_string, prop_name_string, sError)); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 117 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 120 | #define JS_STATIC_PROP(prop_name, class_name) \ |
| 121 | static void get_##prop_name##_static( \ |
| 122 | v8::Local<v8::String> property, \ |
| 123 | const v8::PropertyCallbackInfo<v8::Value>& info) { \ |
| 124 | JSPropGetter<class_name, &class_name::prop_name>(#prop_name, #class_name, \ |
| 125 | property, info); \ |
| 126 | } \ |
| 127 | static void set_##prop_name##_static( \ |
| 128 | v8::Local<v8::String> property, v8::Local<v8::Value> value, \ |
| 129 | const v8::PropertyCallbackInfo<void>& info) { \ |
| 130 | JSPropSetter<class_name, &class_name::prop_name>(#prop_name, #class_name, \ |
| 131 | property, value, info); \ |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 132 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 133 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 134 | template <class C, |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 135 | FX_BOOL (C::*M)(IJS_Context*, |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 136 | const std::vector<CJS_Value>&, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 137 | CJS_Value&, |
| 138 | CFX_WideString&)> |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 139 | void JSMethod(const char* method_name_string, |
| 140 | const char* class_name_string, |
| 141 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
Tom Sepez | b8ec0a3 | 2015-11-20 14:23:02 -0800 | [diff] [blame] | 142 | CJS_Runtime* pRuntime = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 143 | CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
Tom Sepez | bd7fabf | 2015-09-28 10:31:27 -0700 | [diff] [blame] | 144 | if (!pRuntime) |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 145 | return; |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 146 | std::vector<CJS_Value> parameters; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 147 | for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 148 | parameters.push_back(CJS_Value(pRuntime, info[i])); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 149 | } |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 150 | CJS_Object* pJSObj = |
| 151 | static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 152 | C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 153 | CFX_WideString sError; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 154 | CJS_Value valueRes(pRuntime); |
| 155 | if (!(pObj->*M)(pRuntime->GetCurrentContext(), parameters, valueRes, |
| 156 | sError)) { |
| 157 | pRuntime->Error( |
| 158 | JSFormatErrorString(class_name_string, method_name_string, sError)); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 159 | return; |
| 160 | } |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 161 | info.GetReturnValue().Set(valueRes.ToV8Value(pRuntime)); |
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; \ |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 184 | static void DefineJSObjects(CFXJS_Engine* pEngine, 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 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 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(CFXJS_Engine* pEngine, \ |
| 199 | FXJSOBJTYPE eObjType) { \ |
| 200 | g_nObjDefnID = pEngine->DefineObj(js_class_name::g_pClassName, eObjType, \ |
| 201 | nullptr, nullptr); \ |
| 202 | DefineConsts(pEngine); \ |
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[]; \ |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 207 | static void DefineConsts(CFXJS_Engine* pEngine); |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 208 | |
thestig | fbe14b9 | 2016-05-02 13:31:10 -0700 | [diff] [blame] | 209 | #define IMPLEMENT_JS_CLASS_CONST_PART(js_class_name, class_name) \ |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 210 | void js_class_name::DefineConsts(CFXJS_Engine* pEngine) { \ |
thestig | fbe14b9 | 2016-05-02 13:31:10 -0700 | [diff] [blame] | 211 | for (size_t i = 0; i < FX_ArraySize(JS_Class_Consts) - 1; ++i) { \ |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 212 | pEngine->DefineObjConst( \ |
| 213 | g_nObjDefnID, JS_Class_Consts[i].pName, \ |
thestig | fbe14b9 | 2016-05-02 13:31:10 -0700 | [diff] [blame] | 214 | JS_Class_Consts[i].t == 0 \ |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 215 | ? pEngine->NewNumber(JS_Class_Consts[i].number) \ |
| 216 | : pEngine->NewString(JS_Class_Consts[i].str)); \ |
thestig | fbe14b9 | 2016-05-02 13:31:10 -0700 | [diff] [blame] | 217 | } \ |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 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 | |
tsepez | b469424 | 2016-08-15 16:44:55 -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(CFXJS_Engine* pEngine, \ |
| 237 | FXJSOBJTYPE eObjType) { \ |
| 238 | g_nObjDefnID = pEngine->DefineObj(js_class_name::g_pClassName, eObjType, \ |
| 239 | JSConstructor, JSDestructor); \ |
| 240 | DefineConsts(pEngine); \ |
| 241 | DefineProps(pEngine); \ |
| 242 | DefineMethods(pEngine); \ |
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 | 3342090 | 2015-10-13 15:00:10 -0700 | [diff] [blame] | 245 | #define DECLARE_JS_CLASS_RICH_PART() \ |
tsepez | a494191 | 2016-08-15 11:40:12 -0700 | [diff] [blame] | 246 | static void JSConstructor(CFXJS_Engine* pEngine, v8::Local<v8::Object> obj); \ |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 247 | static void JSDestructor(CFXJS_Engine* pEngine, v8::Local<v8::Object> obj); \ |
| 248 | static void DefineProps(CFXJS_Engine* pEngine); \ |
| 249 | static void DefineMethods(CFXJS_Engine* pEngine); \ |
Tom Sepez | 3342090 | 2015-10-13 15:00:10 -0700 | [diff] [blame] | 250 | static JSPropertySpec JS_Class_Properties[]; \ |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 251 | static JSMethodSpec JS_Class_Methods[]; |
| 252 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 253 | #define IMPLEMENT_JS_CLASS_RICH_PART(js_class_name, class_alternate, \ |
| 254 | class_name) \ |
| 255 | void js_class_name::JSConstructor(CFXJS_Engine* pEngine, \ |
| 256 | v8::Local<v8::Object> obj) { \ |
| 257 | CJS_Object* pObj = new js_class_name(obj); \ |
| 258 | pObj->SetEmbedObject(new class_alternate(pObj)); \ |
| 259 | pEngine->SetObjectPrivate(obj, (void*)pObj); \ |
| 260 | pObj->InitInstance(static_cast<CJS_Runtime*>(pEngine)); \ |
| 261 | } \ |
| 262 | void js_class_name::JSDestructor(CFXJS_Engine* pEngine, \ |
| 263 | v8::Local<v8::Object> obj) { \ |
| 264 | js_class_name* pObj = \ |
| 265 | static_cast<js_class_name*>(pEngine->GetObjectPrivate(obj)); \ |
| 266 | pObj->ExitInstance(); \ |
| 267 | delete pObj; \ |
| 268 | } \ |
| 269 | void js_class_name::DefineProps(CFXJS_Engine* pEngine) { \ |
| 270 | for (size_t i = 0; i < FX_ArraySize(JS_Class_Properties) - 1; ++i) { \ |
| 271 | pEngine->DefineObjProperty(g_nObjDefnID, JS_Class_Properties[i].pName, \ |
| 272 | JS_Class_Properties[i].pPropGet, \ |
| 273 | JS_Class_Properties[i].pPropPut); \ |
| 274 | } \ |
| 275 | } \ |
| 276 | void js_class_name::DefineMethods(CFXJS_Engine* pEngine) { \ |
| 277 | for (size_t i = 0; i < FX_ArraySize(JS_Class_Methods) - 1; ++i) { \ |
| 278 | pEngine->DefineObjMethod(g_nObjDefnID, JS_Class_Methods[i].pName, \ |
| 279 | JS_Class_Methods[i].pMethodCall); \ |
| 280 | } \ |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 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) \ |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 295 | void js_class_name::DefineJSObjects(CFXJS_Engine* pEngine, \ |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 296 | FXJSOBJTYPE eObjType) { \ |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 297 | g_nObjDefnID = pEngine->DefineObj(js_class_name::g_pClassName, eObjType, \ |
| 298 | JSConstructor, JSDestructor); \ |
| 299 | DefineConsts(pEngine); \ |
| 300 | DefineProps(pEngine); \ |
| 301 | DefineMethods(pEngine); \ |
| 302 | DefineAllProperties(pEngine); \ |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 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); \ |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 317 | static void DefineAllProperties(CFXJS_Engine* pEngine); |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 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 | } \ |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 341 | void js_class_name::DefineAllProperties(CFXJS_Engine* pEngine) { \ |
| 342 | pEngine->DefineObjAllProperties( \ |
| 343 | g_nObjDefnID, js_class_name::queryprop_static, \ |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 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) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 352 | CJS_Runtime* pRuntime = |
| 353 | CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 354 | v8::String::Utf8Value utf8_value(property); |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 355 | CFX_WideString propname = CFX_WideString::FromUTF8( |
| 356 | CFX_ByteStringC(*utf8_value, utf8_value.length())); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 357 | CJS_Object* pJSObj = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 358 | static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 359 | Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
| 360 | FX_BOOL bRet = pObj->QueryProperty(propname.c_str()); |
| 361 | info.GetReturnValue().Set(bRet ? 4 : 0); |
| 362 | } |
| 363 | |
| 364 | template <class Alt> |
| 365 | void JSSpecialPropGet(const char* class_name, |
| 366 | v8::Local<v8::String> property, |
| 367 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
Tom Sepez | b8ec0a3 | 2015-11-20 14:23:02 -0800 | [diff] [blame] | 368 | CJS_Runtime* pRuntime = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 369 | CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
Tom Sepez | bd7fabf | 2015-09-28 10:31:27 -0700 | [diff] [blame] | 370 | if (!pRuntime) |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 371 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 372 | CJS_Object* pJSObj = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 373 | static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(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); |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 376 | CFX_WideString propname = CFX_WideString::FromUTF8( |
| 377 | CFX_ByteStringC(*utf8_value, utf8_value.length())); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 378 | CFX_WideString sError; |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 379 | CJS_PropValue value(pRuntime); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 380 | value.StartGetting(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 381 | if (!pObj->DoProperty(pRuntime->GetCurrentContext(), propname.c_str(), value, |
| 382 | sError)) { |
| 383 | pRuntime->Error(JSFormatErrorString(class_name, "GetProperty", sError)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 384 | return; |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 385 | } |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 386 | info.GetReturnValue().Set(value.GetJSValue()->ToV8Value(pRuntime)); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | template <class Alt> |
| 390 | void JSSpecialPropPut(const char* class_name, |
| 391 | v8::Local<v8::String> property, |
| 392 | v8::Local<v8::Value> value, |
| 393 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
Tom Sepez | b8ec0a3 | 2015-11-20 14:23:02 -0800 | [diff] [blame] | 394 | CJS_Runtime* pRuntime = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 395 | CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
Tom Sepez | bd7fabf | 2015-09-28 10:31:27 -0700 | [diff] [blame] | 396 | if (!pRuntime) |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 397 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 398 | CJS_Object* pJSObj = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 399 | static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(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); |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 402 | CFX_WideString propname = CFX_WideString::FromUTF8( |
| 403 | CFX_ByteStringC(*utf8_value, utf8_value.length())); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 404 | CFX_WideString sError; |
tsepez | f3dc8c6 | 2016-08-10 06:29:29 -0700 | [diff] [blame] | 405 | CJS_PropValue PropValue(pRuntime, CJS_Value(pRuntime, value)); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 406 | PropValue.StartSetting(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 407 | if (!pObj->DoProperty(pRuntime->GetCurrentContext(), propname.c_str(), |
| 408 | PropValue, sError)) { |
| 409 | pRuntime->Error(JSFormatErrorString(class_name, "PutProperty", sError)); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
| 413 | template <class Alt> |
| 414 | void JSSpecialPropDel(const char* class_name, |
| 415 | v8::Local<v8::String> property, |
| 416 | const v8::PropertyCallbackInfo<v8::Boolean>& info) { |
tsepez | a494191 | 2016-08-15 11:40:12 -0700 | [diff] [blame] | 417 | CJS_Runtime* pRuntime = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 418 | CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
Tom Sepez | bd7fabf | 2015-09-28 10:31:27 -0700 | [diff] [blame] | 419 | if (!pRuntime) |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 420 | return; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 421 | CJS_Object* pJSObj = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 422 | static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(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); |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 425 | CFX_WideString propname = CFX_WideString::FromUTF8( |
| 426 | CFX_ByteStringC(*utf8_value, utf8_value.length())); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 427 | CFX_WideString sError; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 428 | if (!pObj->DelProperty(pRuntime->GetCurrentContext(), propname.c_str(), |
| 429 | sError)) { |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 430 | CFX_ByteString cbName; |
| 431 | cbName.Format("%s.%s", class_name, "DelProperty"); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 432 | // Probably a missing call to JSFX_Error(). |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 436 | template <FX_BOOL (*F)(IJS_Context*, |
| 437 | const std::vector<CJS_Value>&, |
| 438 | CJS_Value&, |
| 439 | CFX_WideString&)> |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 440 | void JSGlobalFunc(const char* func_name_string, |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 441 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 442 | CJS_Runtime* pRuntime = |
| 443 | CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
Tom Sepez | bd7fabf | 2015-09-28 10:31:27 -0700 | [diff] [blame] | 444 | if (!pRuntime) |
Tom Sepez | a25fd09 | 2015-09-28 09:06:03 -0700 | [diff] [blame] | 445 | return; |
Lei Zhang | 945fdb7 | 2015-11-11 10:18:16 -0800 | [diff] [blame] | 446 | std::vector<CJS_Value> 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++) { |
tsepez | 40faa79 | 2016-07-15 17:58:02 -0700 | [diff] [blame] | 448 | parameters.push_back(CJS_Value(pRuntime, info[i])); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 449 | } |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 450 | CJS_Value valueRes(pRuntime); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 451 | CFX_WideString sError; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 452 | if (!(*F)(pRuntime->GetCurrentContext(), parameters, valueRes, sError)) { |
| 453 | pRuntime->Error(JSFormatErrorString(func_name_string, nullptr, sError)); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 454 | return; |
| 455 | } |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 456 | info.GetReturnValue().Set(valueRes.ToV8Value(pRuntime)); |
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[]; \ |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 467 | static void DefineJSObjects(CFXJS_Engine* pEngine) |
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 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame^] | 476 | #define IMPLEMENT_JS_STATIC_GLOBAL_FUN(js_class_name) \ |
| 477 | void js_class_name::DefineJSObjects(CFXJS_Engine* pEngine) { \ |
| 478 | for (size_t i = 0; i < FX_ArraySize(global_methods) - 1; ++i) { \ |
| 479 | pEngine->DefineGlobalMethod( \ |
| 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 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 485 | #endif // FPDFSDK_JAVASCRIPT_JS_DEFINE_H_ |