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 | 19922bb | 2015-05-28 13:23:12 -0700 | [diff] [blame] | 7 | #ifndef FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_ |
| 8 | #define FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_ |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 9 | |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 10 | #include "../jsapi/fxjs_v8.h" |
| 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 | /* ====================================== PUBLIC DEFINE SPEC |
| 34 | * ============================================== */ |
| 35 | #define JS_WIDESTRING(widestring) L## #widestring |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 36 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 37 | #define BEGIN_JS_STATIC_CONST(js_class_name) \ |
| 38 | JSConstSpec js_class_name::JS_Class_Consts[] = { |
| 39 | #define JS_STATIC_CONST_ENTRY_NUMBER(const_name, pValue) \ |
Nico Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 40 | { const_name, pValue, L"", 0 } \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -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 | , |
| 45 | #define END_JS_STATIC_CONST() \ |
| 46 | { 0, 0, 0, 0 } \ |
| 47 | } \ |
| 48 | ; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 49 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 50 | #define BEGIN_JS_STATIC_PROP(js_class_name) \ |
| 51 | JSPropertySpec js_class_name::JS_Class_Properties[] = { |
| 52 | #define JS_STATIC_PROP_ENTRY(prop_name) \ |
| 53 | { \ |
| 54 | JS_WIDESTRING(prop_name), get_##prop_name##_static, set_##prop_name##_static \ |
| 55 | } \ |
| 56 | , |
| 57 | #define END_JS_STATIC_PROP() \ |
| 58 | { 0, 0, 0 } \ |
| 59 | } \ |
| 60 | ; |
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 | , |
| 67 | #define END_JS_STATIC_METHOD() \ |
| 68 | { 0, 0 } \ |
| 69 | } \ |
| 70 | ; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 71 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 72 | /* ======================================== PROP CALLBACK |
| 73 | * ============================================ */ |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 74 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 75 | template <class C, |
| 76 | FX_BOOL (C::*M)(IFXJS_Context*, CJS_PropValue&, CFX_WideString&)> |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 77 | void JSPropGetter(const char* prop_name_string, |
| 78 | const char* class_name_string, |
| 79 | v8::Local<v8::String> property, |
| 80 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 81 | v8::Isolate* isolate = info.GetIsolate(); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame^] | 82 | FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(isolate); |
| 83 | IFXJS_Context* pRuntimeContext = pData->m_pFXJSRuntime->GetCurrentContext(); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 84 | CJS_PropValue value(isolate); |
| 85 | value.StartGetting(); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 86 | CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, info.Holder()); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 87 | C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 88 | CFX_WideString sError; |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 89 | if (!(pObj->*M)(pRuntimeContext, value, sError)) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 90 | FXJS_Error(isolate, JSFormatErrorString(class_name_string, prop_name_string, |
| 91 | sError)); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 92 | return; |
| 93 | } |
Jochen Eisinger | dfa2c99 | 2015-05-19 00:38:00 +0200 | [diff] [blame] | 94 | info.GetReturnValue().Set((v8::Local<v8::Value>)value); |
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, |
| 98 | FX_BOOL (C::*M)(IFXJS_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) { |
| 104 | v8::Isolate* isolate = info.GetIsolate(); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame^] | 105 | FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(isolate); |
| 106 | IFXJS_Context* pRuntimeContext = pData->m_pFXJSRuntime->GetCurrentContext(); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 107 | CJS_PropValue propValue(CJS_Value(isolate, value, CJS_Value::VT_unknown)); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 108 | propValue.StartSetting(); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 109 | CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, 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; |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 112 | if (!(pObj->*M)(pRuntimeContext, propValue, sError)) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 113 | FXJS_Error(isolate, JSFormatErrorString(class_name_string, prop_name_string, |
| 114 | sError)); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 115 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 118 | #define JS_STATIC_PROP(prop_name, class_name) \ |
| 119 | static void get_##prop_name##_static( \ |
| 120 | v8::Local<v8::String> property, \ |
| 121 | const v8::PropertyCallbackInfo<v8::Value>& info) { \ |
| 122 | JSPropGetter<class_name, &class_name::prop_name>(#prop_name, #class_name, \ |
| 123 | property, info); \ |
| 124 | } \ |
| 125 | static void set_##prop_name##_static( \ |
| 126 | v8::Local<v8::String> property, v8::Local<v8::Value> value, \ |
| 127 | const v8::PropertyCallbackInfo<void>& info) { \ |
| 128 | JSPropSetter<class_name, &class_name::prop_name>(#prop_name, #class_name, \ |
| 129 | property, value, info); \ |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 130 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 131 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 132 | /* ========================================= METHOD CALLBACK |
| 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 | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame^] | 144 | FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(isolate); |
| 145 | IFXJS_Context* pRuntimeContext = pData->m_pFXJSRuntime->GetCurrentContext(); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 146 | CJS_Parameters 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++) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 148 | parameters.push_back(CJS_Value(isolate, info[i], CJS_Value::VT_unknown)); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 149 | } |
| 150 | CJS_Value valueRes(isolate); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 151 | CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, 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; |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 154 | if (!(pObj->*M)(pRuntimeContext, parameters, valueRes, sError)) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 155 | FXJS_Error(isolate, JSFormatErrorString(class_name_string, |
| 156 | method_name_string, sError)); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 157 | return; |
| 158 | } |
Tom Sepez | f4ef3f9 | 2015-04-23 11:31:31 -0700 | [diff] [blame] | 159 | info.GetReturnValue().Set(valueRes.ToV8Value()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 162 | #define JS_STATIC_METHOD(method_name, class_name) \ |
| 163 | static void method_name##_static( \ |
| 164 | const v8::FunctionCallbackInfo<v8::Value>& info) { \ |
| 165 | JSMethod<class_name, &class_name::method_name>(#method_name, #class_name, \ |
| 166 | info); \ |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | #define JS_SPECIAL_STATIC_METHOD(method_name, class_alternate, class_name) \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 170 | static void method_name##_static( \ |
| 171 | const v8::FunctionCallbackInfo<v8::Value>& info) { \ |
| 172 | JSMethod<class_alternate, &class_alternate::method_name>( \ |
| 173 | #method_name, #class_name, info); \ |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 176 | /* ===================================== JS CLASS |
| 177 | * =============================================== */ |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 178 | |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 179 | #define DECLARE_JS_CLASS(js_class_name) \ |
| 180 | static void JSConstructor(IFXJS_Context* cc, v8::Local<v8::Object> obj, \ |
| 181 | v8::Local<v8::Object> global); \ |
| 182 | static void JSDestructor(v8::Local<v8::Object> obj); \ |
| 183 | static void DefineJSObjects(v8::Isolate* pIsolate, FXJSOBJTYPE eObjType); \ |
| 184 | static JSConstSpec JS_Class_Consts[]; \ |
| 185 | static JSPropertySpec JS_Class_Properties[]; \ |
| 186 | static JSMethodSpec JS_Class_Methods[]; \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 187 | static const wchar_t* m_pClassName |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 188 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 189 | #define IMPLEMENT_JS_CLASS_RICH(js_class_name, class_alternate, class_name) \ |
| 190 | const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name); \ |
| 191 | void js_class_name::JSConstructor(IFXJS_Context* cc, \ |
| 192 | v8::Local<v8::Object> obj, \ |
| 193 | v8::Local<v8::Object> global) { \ |
| 194 | CJS_Object* pObj = new js_class_name(obj); \ |
| 195 | pObj->SetEmbedObject(new class_alternate(pObj)); \ |
| 196 | FXJS_SetPrivate(NULL, obj, (void*)pObj); \ |
| 197 | pObj->InitInstance(cc); \ |
| 198 | } \ |
| 199 | \ |
| 200 | void js_class_name::JSDestructor(v8::Local<v8::Object> obj) { \ |
| 201 | js_class_name* pObj = (js_class_name*)FXJS_GetPrivate(NULL, obj); \ |
| 202 | pObj->ExitInstance(); \ |
| 203 | delete pObj; \ |
| 204 | } \ |
| 205 | \ |
| 206 | void js_class_name::DefineJSObjects(v8::Isolate* pIsolate, \ |
| 207 | FXJSOBJTYPE eObjType) { \ |
| 208 | int nObjDefnID = FXJS_DefineObj(pIsolate, js_class_name::m_pClassName, \ |
| 209 | eObjType, JSConstructor, JSDestructor); \ |
| 210 | for (int i = 0; i < FX_ArraySize(JS_Class_Properties) - 1; ++i) { \ |
| 211 | FXJS_DefineObjProperty( \ |
| 212 | pIsolate, nObjDefnID, JS_Class_Properties[i].pName, \ |
| 213 | JS_Class_Properties[i].pPropGet, JS_Class_Properties[i].pPropPut); \ |
| 214 | } \ |
| 215 | for (int i = 0; i < FX_ArraySize(JS_Class_Methods) - 1; ++i) { \ |
| 216 | FXJS_DefineObjMethod(pIsolate, nObjDefnID, JS_Class_Methods[i].pName, \ |
| 217 | JS_Class_Methods[i].pMethodCall); \ |
| 218 | } \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 219 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 220 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 221 | #define IMPLEMENT_JS_CLASS(js_class_name, class_name) \ |
| 222 | 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] | 223 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 224 | /* ======================================== CONST CLASS |
| 225 | * ============================================ */ |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 226 | |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 227 | #define DECLARE_JS_CLASS_CONST() \ |
| 228 | static void DefineJSObjects(v8::Isolate* pIsolate, FXJSOBJTYPE eObjType); \ |
| 229 | static JSConstSpec JS_Class_Consts[]; \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 230 | static const wchar_t* m_pClassName |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 231 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 232 | #define IMPLEMENT_JS_CLASS_CONST(js_class_name, class_name) \ |
| 233 | const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name); \ |
| 234 | void js_class_name::DefineJSObjects(v8::Isolate* pIsolate, \ |
| 235 | FXJSOBJTYPE eObjType) { \ |
| 236 | int nObjDefnID = FXJS_DefineObj(pIsolate, js_class_name::m_pClassName, \ |
| 237 | eObjType, NULL, NULL); \ |
| 238 | for (int i = 0; i < FX_ArraySize(JS_Class_Consts) - 1; ++i) { \ |
| 239 | FXJS_DefineObjConst( \ |
| 240 | pIsolate, nObjDefnID, JS_Class_Consts[i].pName, \ |
| 241 | JS_Class_Consts[i].t == 0 \ |
| 242 | ? FXJS_NewNumber(pIsolate, JS_Class_Consts[i].number) \ |
| 243 | : FXJS_NewString(pIsolate, JS_Class_Consts[i].string)); \ |
| 244 | } \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 245 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 246 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 247 | /* ===================================== SPECIAL JS CLASS |
| 248 | * =============================================== */ |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 249 | |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 250 | template <class Alt> |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 251 | void JSSpecialPropQuery(const char*, |
| 252 | v8::Local<v8::String> property, |
| 253 | const v8::PropertyCallbackInfo<v8::Integer>& info) { |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 254 | v8::Isolate* isolate = info.GetIsolate(); |
| 255 | v8::String::Utf8Value utf8_value(property); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 256 | CFX_WideString propname = |
| 257 | CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
| 258 | CJS_Object* pJSObj = |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 259 | reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder())); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 260 | Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
| 261 | FX_BOOL bRet = pObj->QueryProperty(propname.c_str()); |
| 262 | info.GetReturnValue().Set(bRet ? 4 : 0); |
| 263 | } |
| 264 | |
| 265 | template <class Alt> |
| 266 | void JSSpecialPropGet(const char* class_name, |
| 267 | v8::Local<v8::String> property, |
| 268 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 269 | v8::Isolate* isolate = info.GetIsolate(); |
| 270 | v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame^] | 271 | FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(isolate); |
| 272 | IFXJS_Context* pRuntimeContext = pData->m_pFXJSRuntime->GetCurrentContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 273 | CJS_Object* pJSObj = |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 274 | reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder())); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 275 | Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
| 276 | v8::String::Utf8Value utf8_value(property); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 277 | CFX_WideString propname = |
| 278 | CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 279 | CFX_WideString sError; |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 280 | CJS_PropValue value(isolate); |
| 281 | value.StartGetting(); |
| 282 | if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), value, sError)) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 283 | FXJS_Error(isolate, JSFormatErrorString(class_name, "GetProperty", sError)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 284 | return; |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 285 | } |
Jochen Eisinger | dfa2c99 | 2015-05-19 00:38:00 +0200 | [diff] [blame] | 286 | info.GetReturnValue().Set((v8::Local<v8::Value>)value); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | template <class Alt> |
| 290 | void JSSpecialPropPut(const char* class_name, |
| 291 | v8::Local<v8::String> property, |
| 292 | v8::Local<v8::Value> value, |
| 293 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 294 | v8::Isolate* isolate = info.GetIsolate(); |
| 295 | v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame^] | 296 | FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(isolate); |
| 297 | IFXJS_Context* pRuntimeContext = pData->m_pFXJSRuntime->GetCurrentContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 298 | CJS_Object* pJSObj = |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 299 | reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder())); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 300 | Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
| 301 | v8::String::Utf8Value utf8_value(property); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 302 | CFX_WideString propname = |
| 303 | CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 304 | CFX_WideString sError; |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 305 | CJS_PropValue PropValue(CJS_Value(isolate, value, CJS_Value::VT_unknown)); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 306 | PropValue.StartSetting(); |
| 307 | if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), PropValue, sError)) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 308 | FXJS_Error(isolate, JSFormatErrorString(class_name, "PutProperty", sError)); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
| 312 | template <class Alt> |
| 313 | void JSSpecialPropDel(const char* class_name, |
| 314 | v8::Local<v8::String> property, |
| 315 | const v8::PropertyCallbackInfo<v8::Boolean>& info) { |
| 316 | v8::Isolate* isolate = info.GetIsolate(); |
| 317 | v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame^] | 318 | FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(isolate); |
| 319 | IFXJS_Context* pRuntimeContext = pData->m_pFXJSRuntime->GetCurrentContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 320 | CJS_Object* pJSObj = |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 321 | reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder())); |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 322 | Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
| 323 | v8::String::Utf8Value utf8_value(property); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 324 | CFX_WideString propname = |
| 325 | CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 326 | CFX_WideString sError; |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 327 | if (!pObj->DelProperty(pRuntimeContext, propname.c_str(), sError)) { |
| 328 | CFX_ByteString cbName; |
| 329 | cbName.Format("%s.%s", class_name, "DelProperty"); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 330 | // Probably a missing call to JSFX_Error(). |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 334 | #define DECLARE_SPECIAL_JS_CLASS(js_class_name) \ |
| 335 | static void JSConstructor(IFXJS_Context* cc, v8::Local<v8::Object> obj, \ |
| 336 | v8::Local<v8::Object> global); \ |
| 337 | static void JSDestructor(v8::Local<v8::Object> obj); \ |
| 338 | static JSConstSpec JS_Class_Consts[]; \ |
| 339 | static JSPropertySpec JS_Class_Properties[]; \ |
| 340 | static JSMethodSpec JS_Class_Methods[]; \ |
| 341 | static void DefineJSObjects(v8::Isolate* pIsolate, FXJSOBJTYPE eObjType); \ |
| 342 | static const wchar_t* m_pClassName; \ |
| 343 | static void queryprop_##js_class_name##_static( \ |
| 344 | v8::Local<v8::String> property, \ |
| 345 | const v8::PropertyCallbackInfo<v8::Integer>& info); \ |
| 346 | static void getprop_##js_class_name##_static( \ |
| 347 | v8::Local<v8::String> property, \ |
| 348 | const v8::PropertyCallbackInfo<v8::Value>& info); \ |
| 349 | static void putprop_##js_class_name##_static( \ |
| 350 | v8::Local<v8::String> property, v8::Local<v8::Value> value, \ |
| 351 | const v8::PropertyCallbackInfo<v8::Value>& info); \ |
| 352 | static void delprop_##js_class_name##_static( \ |
| 353 | v8::Local<v8::String> property, \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 354 | const v8::PropertyCallbackInfo<v8::Boolean>& info) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 355 | |
| 356 | #define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 357 | const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name); \ |
| 358 | void js_class_name::queryprop_##js_class_name##_static( \ |
| 359 | v8::Local<v8::String> property, \ |
| 360 | const v8::PropertyCallbackInfo<v8::Integer>& info) { \ |
| 361 | JSSpecialPropQuery<class_alternate>(#class_name, property, info); \ |
| 362 | } \ |
| 363 | void js_class_name::getprop_##js_class_name##_static( \ |
| 364 | v8::Local<v8::String> property, \ |
| 365 | const v8::PropertyCallbackInfo<v8::Value>& info) { \ |
| 366 | JSSpecialPropGet<class_alternate>(#class_name, property, info); \ |
| 367 | } \ |
| 368 | void js_class_name::putprop_##js_class_name##_static( \ |
| 369 | v8::Local<v8::String> property, v8::Local<v8::Value> value, \ |
| 370 | const v8::PropertyCallbackInfo<v8::Value>& info) { \ |
| 371 | JSSpecialPropPut<class_alternate>(#class_name, property, value, info); \ |
| 372 | } \ |
| 373 | void js_class_name::delprop_##js_class_name##_static( \ |
| 374 | v8::Local<v8::String> property, \ |
| 375 | const v8::PropertyCallbackInfo<v8::Boolean>& info) { \ |
| 376 | JSSpecialPropDel<class_alternate>(#class_name, property, info); \ |
| 377 | } \ |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 378 | void js_class_name::JSConstructor(IFXJS_Context* cc, \ |
| 379 | v8::Local<v8::Object> obj, \ |
| 380 | v8::Local<v8::Object> global) { \ |
Nico Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 381 | CJS_Object* pObj = new js_class_name(obj); \ |
| 382 | pObj->SetEmbedObject(new class_alternate(pObj)); \ |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 383 | FXJS_SetPrivate(NULL, obj, (void*)pObj); \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 384 | pObj->InitInstance(cc); \ |
| 385 | } \ |
| 386 | \ |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 387 | void js_class_name::JSDestructor(v8::Local<v8::Object> obj) { \ |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 388 | js_class_name* pObj = (js_class_name*)FXJS_GetPrivate(NULL, obj); \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 389 | ASSERT(pObj != NULL); \ |
| 390 | pObj->ExitInstance(); \ |
| 391 | delete pObj; \ |
| 392 | } \ |
| 393 | \ |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 394 | void js_class_name::DefineJSObjects(v8::Isolate* pIsolate, \ |
| 395 | FXJSOBJTYPE eObjType) { \ |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 396 | int nObjDefnID = FXJS_DefineObj(pIsolate, js_class_name::m_pClassName, \ |
| 397 | eObjType, JSConstructor, JSDestructor); \ |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 398 | for (int i = 0; i < FX_ArraySize(JS_Class_Properties) - 1; ++i) { \ |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 399 | FXJS_DefineObjProperty( \ |
| 400 | pIsolate, nObjDefnID, JS_Class_Properties[i].pName, \ |
| 401 | JS_Class_Properties[i].pPropGet, JS_Class_Properties[i].pPropPut); \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 402 | } \ |
| 403 | \ |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 404 | for (int i = 0; i < FX_ArraySize(JS_Class_Methods) - 1; ++i) { \ |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 405 | FXJS_DefineObjMethod(pIsolate, nObjDefnID, JS_Class_Methods[i].pName, \ |
| 406 | JS_Class_Methods[i].pMethodCall); \ |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 407 | } \ |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 408 | FXJS_DefineObjAllProperties( \ |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 409 | pIsolate, nObjDefnID, \ |
| 410 | js_class_name::queryprop_##js_class_name##_static, \ |
| 411 | js_class_name::getprop_##js_class_name##_static, \ |
| 412 | js_class_name::putprop_##js_class_name##_static, \ |
| 413 | js_class_name::delprop_##js_class_name##_static); \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 414 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 415 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 416 | /* ======================================== GLOBAL METHODS |
| 417 | * ============================================ */ |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 418 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 419 | template <FX_BOOL ( |
| 420 | *F)(IFXJS_Context*, const CJS_Parameters&, CJS_Value&, CFX_WideString&)> |
| 421 | void JSGlobalFunc(const char* func_name_string, |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 422 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 423 | v8::Isolate* isolate = info.GetIsolate(); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame^] | 424 | FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(isolate); |
| 425 | IFXJS_Context* pRuntimeContext = pData->m_pFXJSRuntime->GetCurrentContext(); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 426 | CJS_Parameters parameters; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 427 | for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 428 | parameters.push_back(CJS_Value(isolate, info[i], CJS_Value::VT_unknown)); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 429 | } |
| 430 | CJS_Value valueRes(isolate); |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 431 | CFX_WideString sError; |
| 432 | if (!(*F)(pRuntimeContext, parameters, valueRes, sError)) { |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 433 | FXJS_Error(isolate, JSFormatErrorString(func_name_string, nullptr, sError)); |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 434 | return; |
| 435 | } |
Tom Sepez | f4ef3f9 | 2015-04-23 11:31:31 -0700 | [diff] [blame] | 436 | info.GetReturnValue().Set(valueRes.ToV8Value()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 439 | #define JS_STATIC_GLOBAL_FUN(fun_name) \ |
| 440 | static void fun_name##_static( \ |
| 441 | const v8::FunctionCallbackInfo<v8::Value>& info) { \ |
| 442 | JSGlobalFunc<fun_name>(#fun_name, info); \ |
Tom Sepez | a116045 | 2015-02-19 10:00:55 -0800 | [diff] [blame] | 443 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 444 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 445 | #define JS_STATIC_DECLARE_GLOBAL_FUN() \ |
| 446 | static JSMethodSpec global_methods[]; \ |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 447 | static void DefineJSObjects(v8::Isolate* pIsolate) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 448 | |
| 449 | #define BEGIN_JS_STATIC_GLOBAL_FUN(js_class_name) \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 450 | JSMethodSpec js_class_name::global_methods[] = { |
| 451 | #define JS_STATIC_GLOBAL_FUN_ENTRY(method_name) \ |
| 452 | JS_STATIC_METHOD_ENTRY(method_name) |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 453 | |
| 454 | #define END_JS_STATIC_GLOBAL_FUN() END_JS_STATIC_METHOD() |
| 455 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 456 | #define IMPLEMENT_JS_STATIC_GLOBAL_FUN(js_class_name) \ |
| 457 | void js_class_name::DefineJSObjects(v8::Isolate* pIsolate) { \ |
| 458 | for (int i = 0; i < FX_ArraySize(global_methods) - 1; ++i) { \ |
| 459 | FXJS_DefineGlobalMethod(pIsolate, \ |
| 460 | js_class_name::global_methods[i].pName, \ |
| 461 | js_class_name::global_methods[i].pMethodCall); \ |
| 462 | } \ |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 463 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 464 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 465 | 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] | 466 | |
Tom Sepez | 19922bb | 2015-05-28 13:23:12 -0700 | [diff] [blame] | 467 | #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_ |