OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #ifndef FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_ | 7 #ifndef FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_ |
8 #define FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_ | 8 #define FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_ |
9 | 9 |
10 #include "../jsapi/fxjs_v8.h" | 10 #include "../jsapi/fxjs_v8.h" |
11 #include "resource.h" | 11 #include "resource.h" |
12 #include "JS_Object.h" | 12 #include "JS_Object.h" |
13 #include "JS_Value.h" | 13 #include "JS_Value.h" |
14 | 14 |
15 struct JSConstSpec | 15 struct JSConstSpec { |
16 { | 16 const wchar_t* pName; |
17 » const wchar_t* pName; | 17 double number; |
18 » double number; | 18 const wchar_t* string; |
19 » const wchar_t* string; | 19 uint8_t t; // 0:double 1:str |
20 » uint8_t t; //0:double 1:str | |
21 }; | 20 }; |
22 | 21 |
23 struct JSPropertySpec | 22 struct JSPropertySpec { |
24 { | 23 const wchar_t* pName; |
25 » const wchar_t* pName; | 24 v8::AccessorGetterCallback pPropGet; |
26 » v8::AccessorGetterCallback pPropGet; | 25 v8::AccessorSetterCallback pPropPut; |
27 » v8::AccessorSetterCallback pPropPut; | |
28 }; | 26 }; |
29 | 27 |
30 struct JSMethodSpec | 28 struct JSMethodSpec { |
31 { | 29 const wchar_t* pName; |
32 » const wchar_t* pName; | 30 v8::FunctionCallback pMethodCall; |
33 » v8::FunctionCallback pMethodCall; | |
34 }; | 31 }; |
35 | 32 |
36 /* ====================================== PUBLIC DEFINE SPEC ===================
=========================== */ | 33 /* ====================================== PUBLIC DEFINE SPEC |
37 #define JS_WIDESTRING(widestring) L###widestring | 34 * ============================================== */ |
| 35 #define JS_WIDESTRING(widestring) L## #widestring |
38 | 36 |
39 #define BEGIN_JS_STATIC_CONST(js_class_name) JSConstSpec js_class_name::JS_Class
_Consts[] = { | 37 #define BEGIN_JS_STATIC_CONST(js_class_name) \ |
40 #define JS_STATIC_CONST_ENTRY_NUMBER(const_name, pValue) {JS_WIDESTRING(const_na
me), pValue, L"", 0}, | 38 JSConstSpec js_class_name::JS_Class_Consts[] = { |
41 #define JS_STATIC_CONST_ENTRY_STRING(const_name, pValue) {JS_WIDESTRING(const_na
me), 0, JS_WIDESTRING(pValue), 1}, | 39 #define JS_STATIC_CONST_ENTRY_NUMBER(const_name, pValue) \ |
42 #define END_JS_STATIC_CONST() {0, 0, 0, 0}}; | 40 { JS_WIDESTRING(const_name), pValue, L"", 0 } \ |
| 41 , |
| 42 #define JS_STATIC_CONST_ENTRY_STRING(const_name, pValue) \ |
| 43 { JS_WIDESTRING(const_name), 0, JS_WIDESTRING(pValue), 1 } \ |
| 44 , |
| 45 #define END_JS_STATIC_CONST() \ |
| 46 { 0, 0, 0, 0 } \ |
| 47 } \ |
| 48 ; |
43 | 49 |
44 #define BEGIN_JS_STATIC_PROP(js_class_name) JSPropertySpec js_class_name::JS_Cla
ss_Properties[] = { | 50 #define BEGIN_JS_STATIC_PROP(js_class_name) \ |
45 #define JS_STATIC_PROP_ENTRY(prop_name) {JS_WIDESTRING(prop_name), get_##prop_na
me##_static, set_##prop_name##_static}, | 51 JSPropertySpec js_class_name::JS_Class_Properties[] = { |
46 #define END_JS_STATIC_PROP() {0, 0, 0}}; | 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 ; |
47 | 61 |
48 #define BEGIN_JS_STATIC_METHOD(js_class_name) JSMethodSpec js_class_name::JS_Cla
ss_Methods[] = { | 62 #define BEGIN_JS_STATIC_METHOD(js_class_name) \ |
49 #define JS_STATIC_METHOD_ENTRY(method_name) {JS_WIDESTRING(method_name), method_
name##_static}, | 63 JSMethodSpec js_class_name::JS_Class_Methods[] = { |
50 #define END_JS_STATIC_METHOD() {0, 0}}; | 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 ; |
51 | 71 |
52 /* ======================================== PROP CALLBACK ======================
====================== */ | 72 /* ======================================== PROP CALLBACK |
| 73 * ============================================ */ |
53 | 74 |
54 template <class C, FX_BOOL (C::*M)(IFXJS_Context* cc, CJS_PropValue& vp, CFX_Wid
eString& sError)> | 75 template <class C, |
| 76 FX_BOOL (C::*M)(IFXJS_Context* cc, |
| 77 CJS_PropValue& vp, |
| 78 CFX_WideString& sError)> |
55 void JSPropGetter(const char* prop_name_string, | 79 void JSPropGetter(const char* prop_name_string, |
56 const char* class_name_string, | 80 const char* class_name_string, |
57 v8::Local<v8::String> property, | 81 v8::Local<v8::String> property, |
58 const v8::PropertyCallbackInfo<v8::Value>& info) { | 82 const v8::PropertyCallbackInfo<v8::Value>& info) { |
59 v8::Isolate* isolate = info.GetIsolate(); | 83 v8::Isolate* isolate = info.GetIsolate(); |
60 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 84 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
61 v8::Local<v8::Value> v = context->GetEmbedderData(1); | 85 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
62 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | 86 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); |
63 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | 87 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); |
64 IFXJS_Context* pContext = pRuntime->GetCurrentContext(); | 88 IFXJS_Context* pContext = pRuntime->GetCurrentContext(); |
65 CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate,info.Holder()); | 89 CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate, info.Holder()); |
66 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); | 90 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
67 CFX_WideString sError; | 91 CFX_WideString sError; |
68 CJS_PropValue value(isolate); | 92 CJS_PropValue value(isolate); |
69 value.StartGetting(); | 93 value.StartGetting(); |
70 if (!(pObj->*M)(pContext, value, sError)) { | 94 if (!(pObj->*M)(pContext, value, sError)) { |
71 JS_Error(isolate, JSFormatErrorString(class_name_string, prop_name_string, s
Error)); | 95 JS_Error(isolate, |
| 96 JSFormatErrorString(class_name_string, prop_name_string, sError)); |
72 return; | 97 return; |
73 } | 98 } |
74 info.GetReturnValue().Set((v8::Local<v8::Value>)value); | 99 info.GetReturnValue().Set((v8::Local<v8::Value>)value); |
75 } | 100 } |
76 | 101 |
77 template <class C, FX_BOOL (C::*M)(IFXJS_Context* cc, CJS_PropValue& vp, CFX_Wid
eString& sError)> | 102 template <class C, |
| 103 FX_BOOL (C::*M)(IFXJS_Context* cc, |
| 104 CJS_PropValue& vp, |
| 105 CFX_WideString& sError)> |
78 void JSPropSetter(const char* prop_name_string, | 106 void JSPropSetter(const char* prop_name_string, |
79 const char* class_name_string, | 107 const char* class_name_string, |
80 v8::Local<v8::String> property, | 108 v8::Local<v8::String> property, |
81 v8::Local<v8::Value> value, | 109 v8::Local<v8::Value> value, |
82 const v8::PropertyCallbackInfo<void>& info) { | 110 const v8::PropertyCallbackInfo<void>& info) { |
83 v8::Isolate* isolate = info.GetIsolate(); | 111 v8::Isolate* isolate = info.GetIsolate(); |
84 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 112 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
85 v8::Local<v8::Value> v = context->GetEmbedderData(1); | 113 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
86 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | 114 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); |
87 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | 115 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); |
88 IFXJS_Context* pContext = pRuntime->GetCurrentContext(); | 116 IFXJS_Context* pContext = pRuntime->GetCurrentContext(); |
89 CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate,info.Holder()); | 117 CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate, info.Holder()); |
90 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); | 118 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
91 CFX_WideString sError; | 119 CFX_WideString sError; |
92 CJS_PropValue propValue(CJS_Value(isolate, value, VT_unknown)); | 120 CJS_PropValue propValue(CJS_Value(isolate, value, VT_unknown)); |
93 propValue.StartSetting(); | 121 propValue.StartSetting(); |
94 if (!(pObj->*M)(pContext, propValue, sError)) { | 122 if (!(pObj->*M)(pContext, propValue, sError)) { |
95 JS_Error(isolate, JSFormatErrorString(class_name_string, prop_name_string,
sError)); | 123 JS_Error(isolate, |
| 124 JSFormatErrorString(class_name_string, prop_name_string, sError)); |
96 } | 125 } |
97 } | 126 } |
98 | 127 |
99 #define JS_STATIC_PROP(prop_name, class_name) \ | 128 #define JS_STATIC_PROP(prop_name, class_name) \ |
100 static void get_##prop_name##_static( \ | 129 static void get_##prop_name##_static( \ |
101 v8::Local<v8::String> property, \ | 130 v8::Local<v8::String> property, \ |
102 const v8::PropertyCallbackInfo<v8::Value>& info) { \ | 131 const v8::PropertyCallbackInfo<v8::Value>& info) { \ |
103 JSPropGetter<class_name, &class_name::prop_name>( \ | 132 JSPropGetter<class_name, &class_name::prop_name>(#prop_name, #class_name, \ |
104 #prop_name, #class_name, property, info); \ | 133 property, info); \ |
105 } \ | 134 } \ |
106 static void set_##prop_name##_static( \ | 135 static void set_##prop_name##_static( \ |
107 v8::Local<v8::String> property, \ | 136 v8::Local<v8::String> property, v8::Local<v8::Value> value, \ |
108 v8::Local<v8::Value> value, \ | 137 const v8::PropertyCallbackInfo<void>& info) { \ |
109 const v8::PropertyCallbackInfo<void>& info) { \ | 138 JSPropSetter<class_name, &class_name::prop_name>(#prop_name, #class_name, \ |
110 JSPropSetter<class_name, &class_name::prop_name>( \ | 139 property, value, info); \ |
111 #prop_name, #class_name, property, value, info); \ | |
112 } | 140 } |
113 | 141 |
114 /* ========================================= METHOD CALLBACK ===================
======================== */ | 142 /* ========================================= METHOD CALLBACK |
| 143 * =========================================== */ |
115 | 144 |
116 template <class C, FX_BOOL (C::*M)(IFXJS_Context* cc, const CJS_Parameters& para
ms, CJS_Value& vRet, CFX_WideString& sError)> | 145 template <class C, |
| 146 FX_BOOL (C::*M)(IFXJS_Context* cc, |
| 147 const CJS_Parameters& params, |
| 148 CJS_Value& vRet, |
| 149 CFX_WideString& sError)> |
117 void JSMethod(const char* method_name_string, | 150 void JSMethod(const char* method_name_string, |
118 const char* class_name_string, | 151 const char* class_name_string, |
119 const v8::FunctionCallbackInfo<v8::Value>& info) { | 152 const v8::FunctionCallbackInfo<v8::Value>& info) { |
120 v8::Isolate* isolate = info.GetIsolate(); | 153 v8::Isolate* isolate = info.GetIsolate(); |
121 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 154 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
122 v8::Local<v8::Value> v = context->GetEmbedderData(1); | 155 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
123 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | 156 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); |
124 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | 157 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); |
125 IFXJS_Context* cc = pRuntime->GetCurrentContext(); | 158 IFXJS_Context* cc = pRuntime->GetCurrentContext(); |
126 CJS_Parameters parameters; | 159 CJS_Parameters parameters; |
127 for (unsigned int i = 0; i<(unsigned int)info.Length(); i++) { | 160 for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { |
128 parameters.push_back(CJS_Value(isolate, info[i], VT_unknown)); | 161 parameters.push_back(CJS_Value(isolate, info[i], VT_unknown)); |
129 } | 162 } |
130 CJS_Value valueRes(isolate); | 163 CJS_Value valueRes(isolate); |
131 CJS_Object* pJSObj = (CJS_Object *)JS_GetPrivate(isolate,info.Holder()); | 164 CJS_Object* pJSObj = (CJS_Object*)JS_GetPrivate(isolate, info.Holder()); |
132 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); | 165 C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject()); |
133 CFX_WideString sError; | 166 CFX_WideString sError; |
134 if (!(pObj->*M)(cc, parameters, valueRes, sError)) { | 167 if (!(pObj->*M)(cc, parameters, valueRes, sError)) { |
135 JS_Error(isolate, JSFormatErrorString(class_name_string, method_name_strin
g, sError)); | 168 JS_Error(isolate, JSFormatErrorString(class_name_string, method_name_string, |
| 169 sError)); |
136 return; | 170 return; |
137 } | 171 } |
138 info.GetReturnValue().Set(valueRes.ToV8Value()); | 172 info.GetReturnValue().Set(valueRes.ToV8Value()); |
139 } | 173 } |
140 | 174 |
141 #define JS_STATIC_METHOD(method_name, class_name) \ | 175 #define JS_STATIC_METHOD(method_name, class_name) \ |
142 static void method_name##_static( \ | 176 static void method_name##_static( \ |
143 const v8::FunctionCallbackInfo<v8::Value>& info) { \ | 177 const v8::FunctionCallbackInfo<v8::Value>& info) { \ |
144 JSMethod<class_name, &class_name::method_name>( \ | 178 JSMethod<class_name, &class_name::method_name>(#method_name, #class_name, \ |
145 #method_name, #class_name, info); \ | 179 info); \ |
146 } | 180 } |
147 | 181 |
148 #define JS_SPECIAL_STATIC_METHOD(method_name, class_alternate, class_name) \ | 182 #define JS_SPECIAL_STATIC_METHOD(method_name, class_alternate, class_name) \ |
149 static void method_name##_static( \ | 183 static void method_name##_static( \ |
150 const v8::FunctionCallbackInfo<v8::Value>& info) { \ | 184 const v8::FunctionCallbackInfo<v8::Value>& info) { \ |
151 JSMethod<class_alternate, &class_alternate::method_name>( \ | 185 JSMethod<class_alternate, &class_alternate::method_name>( \ |
152 #method_name, #class_name, info); \ | 186 #method_name, #class_name, info); \ |
153 } | 187 } |
154 | 188 |
155 /* ===================================== JS CLASS ==============================
================= */ | 189 /* ===================================== JS CLASS |
| 190 * =============================================== */ |
156 | 191 |
157 #define DECLARE_JS_CLASS(js_class_name) \ | 192 #define DECLARE_JS_CLASS(js_class_name) \ |
158 » static void JSConstructor(IFXJS_Context* cc, JSFXObject obj,JSFXObject g
lobal);\ | 193 static void JSConstructor(IFXJS_Context* cc, JSFXObject obj, \ |
159 » static void JSDestructor(JSFXObject obj);\ | 194 JSFXObject global); \ |
160 » static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType);\ | 195 static void JSDestructor(JSFXObject obj); \ |
161 » static JSConstSpec JS_Class_Consts[];\ | 196 static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType); \ |
162 » static JSPropertySpec JS_Class_Properties[];\ | 197 static JSConstSpec JS_Class_Consts[]; \ |
163 » static JSMethodSpec» JS_Class_Methods[];\ | 198 static JSPropertySpec JS_Class_Properties[]; \ |
164 » static const wchar_t* m_pClassName | 199 static JSMethodSpec JS_Class_Methods[]; \ |
| 200 static const wchar_t* m_pClassName |
165 | 201 |
166 #define IMPLEMENT_JS_CLASS_RICH(js_class_name, class_alternate, class_name) \ | 202 #define IMPLEMENT_JS_CLASS_RICH(js_class_name, class_alternate, class_name) \ |
167 const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name);\ | 203 const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name); \ |
168 void js_class_name::JSConstructor(IFXJS_Context* cc, JSFXObject obj, JSFXObject
global)\ | 204 void js_class_name::JSConstructor(IFXJS_Context* cc, JSFXObject obj, \ |
169 {\ | 205 JSFXObject global) { \ |
170 » CJS_Object* pObj = new js_class_name(obj);\ | 206 CJS_Object* pObj = new js_class_name(obj); \ |
171 » pObj->SetEmbedObject(new class_alternate(pObj));\ | 207 pObj->SetEmbedObject(new class_alternate(pObj)); \ |
172 » JS_SetPrivate(NULL,obj,(void*)pObj); \ | 208 JS_SetPrivate(NULL, obj, (void*)pObj); \ |
173 » pObj->InitInstance(cc);\ | 209 pObj->InitInstance(cc); \ |
174 }\ | 210 } \ |
175 \ | 211 \ |
176 void js_class_name::JSDestructor(JSFXObject obj) \ | 212 void js_class_name::JSDestructor(JSFXObject obj) { \ |
177 {\ | 213 js_class_name* pObj = (js_class_name*)JS_GetPrivate(NULL, obj); \ |
178 » js_class_name* pObj = (js_class_name*)JS_GetPrivate(NULL,obj);\ | 214 ASSERT(pObj != NULL); \ |
179 » ASSERT(pObj != NULL);\ | 215 pObj->ExitInstance(); \ |
180 » pObj->ExitInstance();\ | 216 delete pObj; \ |
181 » delete pObj;\ | 217 } \ |
182 }\ | 218 \ |
183 \ | 219 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType) { \ |
184 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType)\ | 220 int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, \ |
185 {\ | 221 eObjType, JSConstructor, JSDestructor); \ |
186 » int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, eOb
jType, JSConstructor, JSDestructor);\ | 222 if (nObjDefnID >= 0) { \ |
187 » if (nObjDefnID >= 0)\ | 223 for (int j = 0, \ |
188 » {\ | 224 szj = sizeof(JS_Class_Properties) / sizeof(JSPropertySpec) - 1; \ |
189 » » for (int j=0, szj=sizeof(JS_Class_Properties)/sizeof(JSPropertyS
pec)-1; j<szj; j++)\ | 225 j < szj; j++) { \ |
190 » » {\ | 226 if (JS_DefineObjProperty(pRuntime, nObjDefnID, \ |
191 » » » if (JS_DefineObjProperty(pRuntime, nObjDefnID, JS_Class_
Properties[j].pName, JS_Class_Properties[j].pPropGet, JS_Class_Properties[j].pPr
opPut) < 0) return -1;\ | 227 JS_Class_Properties[j].pName, \ |
192 » » }\ | 228 JS_Class_Properties[j].pPropGet, \ |
193 » » for (int k=0, szk=sizeof(JS_Class_Methods)/sizeof(JSMethodSpec)-
1; k<szk; k++)\ | 229 JS_Class_Properties[j].pPropPut) < 0) \ |
194 » » {\ | 230 return -1; \ |
195 » » » if (JS_DefineObjMethod(pRuntime, nObjDefnID,JS_Class_Met
hods[k].pName, JS_Class_Methods[k].pMethodCall) < 0) return -1;\ | 231 } \ |
196 » » }\ | 232 for (int k = 0, \ |
197 » » return nObjDefnID;\ | 233 szk = sizeof(JS_Class_Methods) / sizeof(JSMethodSpec) - 1; \ |
198 » }\ | 234 k < szk; k++) { \ |
199 » return -1;\ | 235 if (JS_DefineObjMethod(pRuntime, nObjDefnID, \ |
200 } | 236 JS_Class_Methods[k].pName, \ |
| 237 JS_Class_Methods[k].pMethodCall) < 0) \ |
| 238 return -1; \ |
| 239 } \ |
| 240 return nObjDefnID; \ |
| 241 } \ |
| 242 return -1; \ |
| 243 } |
201 | 244 |
202 #define IMPLEMENT_JS_CLASS(js_class_name, class_name) IMPLEMENT_JS_CLASS_RICH(js
_class_name, class_name, class_name) | 245 #define IMPLEMENT_JS_CLASS(js_class_name, class_name) \ |
| 246 IMPLEMENT_JS_CLASS_RICH(js_class_name, class_name, class_name) |
203 | 247 |
204 /* ======================================== CONST CLASS ========================
==================== */ | 248 /* ======================================== CONST CLASS |
| 249 * ============================================ */ |
205 | 250 |
206 #define DECLARE_JS_CLASS_CONST() \ | 251 #define DECLARE_JS_CLASS_CONST() \ |
207 » static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType);\ | 252 static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType); \ |
208 » static JSConstSpec JS_Class_Consts[];\ | 253 static JSConstSpec JS_Class_Consts[]; \ |
209 » static const wchar_t* m_pClassName | 254 static const wchar_t* m_pClassName |
210 | 255 |
211 #define IMPLEMENT_JS_CLASS_CONST(js_class_name, class_name) \ | 256 #define IMPLEMENT_JS_CLASS_CONST(js_class_name, class_name) \ |
212 const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name);\ | 257 const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name); \ |
213 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType)\ | 258 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType) { \ |
214 {\ | 259 int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, \ |
215 » int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, eOb
jType, NULL, NULL);\ | 260 eObjType, NULL, NULL); \ |
216 » if (nObjDefnID >=0)\ | 261 if (nObjDefnID >= 0) { \ |
217 » {\ | 262 for (int i = 0, sz = sizeof(JS_Class_Consts) / sizeof(JSConstSpec) - 1; \ |
218 » » for (int i=0, sz=sizeof(JS_Class_Consts)/sizeof(JSConstSpec)-1;
i<sz; i++)\ | 263 i < sz; i++) { \ |
219 » » {\ | 264 if (JS_Class_Consts[i].t == 0) { \ |
220 » » » if (JS_Class_Consts[i].t == 0)\ | 265 if (JS_DefineObjConst( \ |
221 » » » {\ | 266 pRuntime, nObjDefnID, JS_Class_Consts[i].pName, \ |
222 » » » » if (JS_DefineObjConst(pRuntime, nObjDefnID, JS_C
lass_Consts[i].pName, JS_NewNumber(pRuntime,JS_Class_Consts[i].number)) < 0) ret
urn -1;\ | 267 JS_NewNumber(pRuntime, JS_Class_Consts[i].number)) < 0) \ |
223 » » » }\ | 268 return -1; \ |
224 » » » else\ | 269 } else { \ |
225 » » » {\ | 270 if (JS_DefineObjConst( \ |
226 » » » if (JS_DefineObjConst(pRuntime, nObjDefnID, JS_Class_Con
sts[i].pName, JS_NewString(pRuntime,JS_Class_Consts[i].string)) < 0) return -1;\ | 271 pRuntime, nObjDefnID, JS_Class_Consts[i].pName, \ |
227 » » » }\ | 272 JS_NewString(pRuntime, JS_Class_Consts[i].string)) < 0) \ |
228 » » }\ | 273 return -1; \ |
229 » » return nObjDefnID;\ | 274 } \ |
230 » }\ | 275 } \ |
231 » return -1;\ | 276 return nObjDefnID; \ |
232 } | 277 } \ |
| 278 return -1; \ |
| 279 } |
233 | 280 |
234 /* ===================================== SPECIAL JS CLASS ======================
========================= */ | 281 /* ===================================== SPECIAL JS CLASS |
| 282 * =============================================== */ |
235 | 283 |
236 template <class Alt> | 284 template <class Alt> |
237 void JSSpecialPropQuery(const char *, v8::Local<v8::String> property,const v8::P
ropertyCallbackInfo<v8::Integer>& info) { | 285 void JSSpecialPropQuery(const char*, |
| 286 v8::Local<v8::String> property, |
| 287 const v8::PropertyCallbackInfo<v8::Integer>& info) { |
238 v8::Isolate* isolate = info.GetIsolate(); | 288 v8::Isolate* isolate = info.GetIsolate(); |
239 v8::String::Utf8Value utf8_value(property); | 289 v8::String::Utf8Value utf8_value(property); |
240 CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_value.len
gth()); | 290 CFX_WideString propname = |
241 CJS_Object* pJSObj = reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info
.Holder())); | 291 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
| 292 CJS_Object* pJSObj = |
| 293 reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder())); |
242 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); | 294 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
243 FX_BOOL bRet = pObj->QueryProperty(propname.c_str()); | 295 FX_BOOL bRet = pObj->QueryProperty(propname.c_str()); |
244 info.GetReturnValue().Set(bRet ? 4 : 0); | 296 info.GetReturnValue().Set(bRet ? 4 : 0); |
245 } | 297 } |
246 | 298 |
247 template <class Alt> | 299 template <class Alt> |
248 void JSSpecialPropGet(const char* class_name, | 300 void JSSpecialPropGet(const char* class_name, |
249 v8::Local<v8::String> property, | 301 v8::Local<v8::String> property, |
250 const v8::PropertyCallbackInfo<v8::Value>& info) { | 302 const v8::PropertyCallbackInfo<v8::Value>& info) { |
251 v8::Isolate* isolate = info.GetIsolate(); | 303 v8::Isolate* isolate = info.GetIsolate(); |
252 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 304 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
253 v8::Local<v8::Value> v = context->GetEmbedderData(1); | 305 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
254 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | 306 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); |
255 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | 307 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); |
256 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); | 308 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); |
257 CJS_Object* pJSObj = reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info
.Holder())); | 309 CJS_Object* pJSObj = |
| 310 reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder())); |
258 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); | 311 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
259 v8::String::Utf8Value utf8_value(property); | 312 v8::String::Utf8Value utf8_value(property); |
260 CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_value.len
gth()); | 313 CFX_WideString propname = |
| 314 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
261 CFX_WideString sError; | 315 CFX_WideString sError; |
262 CJS_PropValue value(isolate); | 316 CJS_PropValue value(isolate); |
263 value.StartGetting(); | 317 value.StartGetting(); |
264 if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), value, sError)) { | 318 if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), value, sError)) { |
265 JS_Error(isolate, JSFormatErrorString(class_name, "GetProperty", sError)); | 319 JS_Error(isolate, JSFormatErrorString(class_name, "GetProperty", sError)); |
266 return; | 320 return; |
267 } | 321 } |
268 info.GetReturnValue().Set((v8::Local<v8::Value>)value); | 322 info.GetReturnValue().Set((v8::Local<v8::Value>)value); |
269 } | 323 } |
270 | 324 |
271 template <class Alt> | 325 template <class Alt> |
272 void JSSpecialPropPut(const char* class_name, | 326 void JSSpecialPropPut(const char* class_name, |
273 v8::Local<v8::String> property, | 327 v8::Local<v8::String> property, |
274 v8::Local<v8::Value> value, | 328 v8::Local<v8::Value> value, |
275 const v8::PropertyCallbackInfo<v8::Value>& info) { | 329 const v8::PropertyCallbackInfo<v8::Value>& info) { |
276 v8::Isolate* isolate = info.GetIsolate(); | 330 v8::Isolate* isolate = info.GetIsolate(); |
277 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 331 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
278 v8::Local<v8::Value> v = context->GetEmbedderData(1); | 332 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
279 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | 333 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); |
280 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | 334 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); |
281 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); | 335 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); |
282 CJS_Object* pJSObj = reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info
.Holder())); | 336 CJS_Object* pJSObj = |
| 337 reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder())); |
283 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); | 338 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
284 v8::String::Utf8Value utf8_value(property); | 339 v8::String::Utf8Value utf8_value(property); |
285 CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_value.len
gth()); | 340 CFX_WideString propname = |
| 341 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
286 CFX_WideString sError; | 342 CFX_WideString sError; |
287 CJS_PropValue PropValue(CJS_Value(isolate,value,VT_unknown)); | 343 CJS_PropValue PropValue(CJS_Value(isolate, value, VT_unknown)); |
288 PropValue.StartSetting(); | 344 PropValue.StartSetting(); |
289 if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), PropValue, sError)) { | 345 if (!pObj->DoProperty(pRuntimeContext, propname.c_str(), PropValue, sError)) { |
290 JS_Error(isolate, JSFormatErrorString(class_name, "PutProperty", sError)); | 346 JS_Error(isolate, JSFormatErrorString(class_name, "PutProperty", sError)); |
291 } | 347 } |
292 } | 348 } |
293 | 349 |
294 template <class Alt> | 350 template <class Alt> |
295 void JSSpecialPropDel(const char* class_name, | 351 void JSSpecialPropDel(const char* class_name, |
296 v8::Local<v8::String> property, | 352 v8::Local<v8::String> property, |
297 const v8::PropertyCallbackInfo<v8::Boolean>& info) { | 353 const v8::PropertyCallbackInfo<v8::Boolean>& info) { |
298 v8::Isolate* isolate = info.GetIsolate(); | 354 v8::Isolate* isolate = info.GetIsolate(); |
299 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 355 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
300 v8::Local<v8::Value> v = context->GetEmbedderData(1); | 356 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
301 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | 357 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); |
302 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | 358 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); |
303 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); | 359 IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext(); |
304 CJS_Object* pJSObj = reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info
.Holder())); | 360 CJS_Object* pJSObj = |
| 361 reinterpret_cast<CJS_Object*>(JS_GetPrivate(isolate, info.Holder())); |
305 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); | 362 Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
306 v8::String::Utf8Value utf8_value(property); | 363 v8::String::Utf8Value utf8_value(property); |
307 CFX_WideString propname = CFX_WideString::FromUTF8(*utf8_value, utf8_value.len
gth()); | 364 CFX_WideString propname = |
| 365 CFX_WideString::FromUTF8(*utf8_value, utf8_value.length()); |
308 CFX_WideString sError; | 366 CFX_WideString sError; |
309 if (!pObj->DelProperty(pRuntimeContext, propname.c_str(), sError)) { | 367 if (!pObj->DelProperty(pRuntimeContext, propname.c_str(), sError)) { |
310 CFX_ByteString cbName; | 368 CFX_ByteString cbName; |
311 cbName.Format("%s.%s", class_name, "DelProperty"); | 369 cbName.Format("%s.%s", class_name, "DelProperty"); |
312 // Probably a missing call to JS_Error(). | 370 // Probably a missing call to JS_Error(). |
313 } | 371 } |
314 } | 372 } |
315 | 373 |
316 #define DECLARE_SPECIAL_JS_CLASS(js_class_name) \ | 374 #define DECLARE_SPECIAL_JS_CLASS(js_class_name) \ |
317 » static void JSConstructor(IFXJS_Context* cc, JSFXObject obj, JSFXObject
global);\ | 375 static void JSConstructor(IFXJS_Context* cc, JSFXObject obj, \ |
318 » static void JSDestructor(JSFXObject obj);\ | 376 JSFXObject global); \ |
319 » static JSConstSpec JS_Class_Consts[];\ | 377 static void JSDestructor(JSFXObject obj); \ |
320 » static JSPropertySpec JS_Class_Properties[];\ | 378 static JSConstSpec JS_Class_Consts[]; \ |
321 » static JSMethodSpec» JS_Class_Methods[];\ | 379 static JSPropertySpec JS_Class_Properties[]; \ |
322 » static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType);\ | 380 static JSMethodSpec JS_Class_Methods[]; \ |
323 » static const wchar_t* m_pClassName;\ | 381 static int Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType); \ |
324 » static void queryprop_##js_class_name##_static(v8::Local<v8::String> pro
perty,const v8::PropertyCallbackInfo<v8::Integer>& info);\ | 382 static const wchar_t* m_pClassName; \ |
325 » static void getprop_##js_class_name##_static(v8::Local<v8::String> prope
rty, const v8::PropertyCallbackInfo<v8::Value>& info);\ | 383 static void queryprop_##js_class_name##_static( \ |
326 » static void putprop_##js_class_name##_static(v8::Local<v8::String> prope
rty,v8::Local<v8::Value> value,const v8::PropertyCallbackInfo<v8::Value>& info);
\ | 384 v8::Local<v8::String> property, \ |
327 » static void delprop_##js_class_name##_static(v8::Local<v8::String> prope
rty,const v8::PropertyCallbackInfo<v8::Boolean>& info) | 385 const v8::PropertyCallbackInfo<v8::Integer>& info); \ |
| 386 static void getprop_##js_class_name##_static( \ |
| 387 v8::Local<v8::String> property, \ |
| 388 const v8::PropertyCallbackInfo<v8::Value>& info); \ |
| 389 static void putprop_##js_class_name##_static( \ |
| 390 v8::Local<v8::String> property, v8::Local<v8::Value> value, \ |
| 391 const v8::PropertyCallbackInfo<v8::Value>& info); \ |
| 392 static void delprop_##js_class_name##_static( \ |
| 393 v8::Local<v8::String> property, \ |
| 394 const v8::PropertyCallbackInfo<v8::Boolean>& info) |
328 | 395 |
329 #define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \ | 396 #define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \ |
330 const wchar_t * js_class_name::m_pClassName = JS_WIDESTRING(class_name);\ | 397 const wchar_t* js_class_name::m_pClassName = JS_WIDESTRING(class_name); \ |
331 void js_class_name::queryprop_##js_class_name##_static(v8::Local<v8::String> pro
perty,const v8::PropertyCallbackInfo<v8::Integer>& info) { \ | 398 void js_class_name::queryprop_##js_class_name##_static( \ |
332 JSSpecialPropQuery<class_alternate>(#class_name, property, info); \ | 399 v8::Local<v8::String> property, \ |
333 }\ | 400 const v8::PropertyCallbackInfo<v8::Integer>& info) { \ |
334 void js_class_name::getprop_##js_class_name##_static(v8::Local<v8::String> prope
rty, const v8::PropertyCallbackInfo<v8::Value>& info) { \ | 401 JSSpecialPropQuery<class_alternate>(#class_name, property, info); \ |
335 JSSpecialPropGet<class_alternate>(#class_name, property, info); \ | 402 } \ |
336 } \ | 403 void js_class_name::getprop_##js_class_name##_static( \ |
337 void js_class_name::putprop_##js_class_name##_static(v8::Local<v8::String> prope
rty,v8::Local<v8::Value> value,const v8::PropertyCallbackInfo<v8::Value>& info)
{\ | 404 v8::Local<v8::String> property, \ |
338 JSSpecialPropPut<class_alternate>(#class_name, property, value, info); \ | 405 const v8::PropertyCallbackInfo<v8::Value>& info) { \ |
339 }\ | 406 JSSpecialPropGet<class_alternate>(#class_name, property, info); \ |
340 void js_class_name::delprop_##js_class_name##_static(v8::Local<v8::String> prope
rty,const v8::PropertyCallbackInfo<v8::Boolean>& info) { \ | 407 } \ |
341 JSSpecialPropDel<class_alternate>(#class_name, property, info); \ | 408 void js_class_name::putprop_##js_class_name##_static( \ |
342 } \ | 409 v8::Local<v8::String> property, v8::Local<v8::Value> value, \ |
343 void js_class_name::JSConstructor(IFXJS_Context* cc, JSFXObject obj,JSFXObject
global)\ | 410 const v8::PropertyCallbackInfo<v8::Value>& info) { \ |
344 {\ | 411 JSSpecialPropPut<class_alternate>(#class_name, property, value, info); \ |
345 » CJS_Object* pObj = new js_class_name(obj);\ | 412 } \ |
346 » pObj->SetEmbedObject(new class_alternate(pObj));\ | 413 void js_class_name::delprop_##js_class_name##_static( \ |
347 » JS_SetPrivate(NULL,obj, (void*)pObj); \ | 414 v8::Local<v8::String> property, \ |
348 » pObj->InitInstance(cc);\ | 415 const v8::PropertyCallbackInfo<v8::Boolean>& info) { \ |
349 }\ | 416 JSSpecialPropDel<class_alternate>(#class_name, property, info); \ |
350 \ | 417 } \ |
351 void js_class_name::JSDestructor(JSFXObject obj) \ | 418 void js_class_name::JSConstructor(IFXJS_Context* cc, JSFXObject obj, \ |
352 {\ | 419 JSFXObject global) { \ |
353 » js_class_name* pObj = (js_class_name*)JS_GetPrivate(NULL,obj);\ | 420 CJS_Object* pObj = new js_class_name(obj); \ |
354 » ASSERT(pObj != NULL);\ | 421 pObj->SetEmbedObject(new class_alternate(pObj)); \ |
355 » pObj->ExitInstance();\ | 422 JS_SetPrivate(NULL, obj, (void*)pObj); \ |
356 » delete pObj;\ | 423 pObj->InitInstance(cc); \ |
357 }\ | 424 } \ |
358 \ | 425 \ |
359 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType)\ | 426 void js_class_name::JSDestructor(JSFXObject obj) { \ |
360 {\ | 427 js_class_name* pObj = (js_class_name*)JS_GetPrivate(NULL, obj); \ |
361 \ | 428 ASSERT(pObj != NULL); \ |
362 » int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, eOb
jType, JSConstructor, JSDestructor);\ | 429 pObj->ExitInstance(); \ |
363 \ | 430 delete pObj; \ |
364 » if (nObjDefnID >= 0)\ | 431 } \ |
365 » {\ | 432 \ |
366 » » for (int j=0, szj=sizeof(JS_Class_Properties)/sizeof(JSPropertyS
pec)-1; j<szj; j++)\ | 433 int js_class_name::Init(IJS_Runtime* pRuntime, FXJSOBJTYPE eObjType) { \ |
367 » » {\ | 434 int nObjDefnID = JS_DefineObj(pRuntime, js_class_name::m_pClassName, \ |
368 » » » if (JS_DefineObjProperty(pRuntime, nObjDefnID, JS_Class_
Properties[j].pName, JS_Class_Properties[j].pPropGet,JS_Class_Properties[j].pPro
pPut)<0)return -1;\ | 435 eObjType, JSConstructor, JSDestructor); \ |
369 » » }\ | 436 \ |
370 \ | 437 if (nObjDefnID >= 0) { \ |
371 » » for (int k=0, szk=sizeof(JS_Class_Methods)/sizeof(JSMethodSpec)-
1; k<szk; k++)\ | 438 for (int j = 0, \ |
372 » » {\ | 439 szj = sizeof(JS_Class_Properties) / sizeof(JSPropertySpec) - 1; \ |
373 » » » if (JS_DefineObjMethod(pRuntime, nObjDefnID,JS_Class_Met
hods[k].pName,JS_Class_Methods[k].pMethodCall)<0)return -1;\ | 440 j < szj; j++) { \ |
374 » » }\ | 441 if (JS_DefineObjProperty(pRuntime, nObjDefnID, \ |
375 » » if (JS_DefineObjAllProperties(pRuntime, nObjDefnID, js_class_nam
e::queryprop_##js_class_name##_static, js_class_name::getprop_##js_class_name##_
static,js_class_name::putprop_##js_class_name##_static,js_class_name::delprop_##
js_class_name##_static)<0) return -1;\ | 442 JS_Class_Properties[j].pName, \ |
376 \ | 443 JS_Class_Properties[j].pPropGet, \ |
377 » » return nObjDefnID;\ | 444 JS_Class_Properties[j].pPropPut) < 0) \ |
378 » }\ | 445 return -1; \ |
379 \ | 446 } \ |
380 » return -1;\ | 447 \ |
381 } | 448 for (int k = 0, \ |
| 449 szk = sizeof(JS_Class_Methods) / sizeof(JSMethodSpec) - 1; \ |
| 450 k < szk; k++) { \ |
| 451 if (JS_DefineObjMethod(pRuntime, nObjDefnID, \ |
| 452 JS_Class_Methods[k].pName, \ |
| 453 JS_Class_Methods[k].pMethodCall) < 0) \ |
| 454 return -1; \ |
| 455 } \ |
| 456 if (JS_DefineObjAllProperties( \ |
| 457 pRuntime, nObjDefnID, \ |
| 458 js_class_name::queryprop_##js_class_name##_static, \ |
| 459 js_class_name::getprop_##js_class_name##_static, \ |
| 460 js_class_name::putprop_##js_class_name##_static, \ |
| 461 js_class_name::delprop_##js_class_name##_static) < 0) \ |
| 462 return -1; \ |
| 463 \ |
| 464 return nObjDefnID; \ |
| 465 } \ |
| 466 \ |
| 467 return -1; \ |
| 468 } |
382 | 469 |
383 /* ======================================== GLOBAL METHODS =====================
======================= */ | 470 /* ======================================== GLOBAL METHODS |
| 471 * ============================================ */ |
384 | 472 |
385 template <FX_BOOL (*F)(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Valu
e& vRet, CFX_WideString& sError)> | 473 template <FX_BOOL (*F)(IFXJS_Context* cc, |
386 void JSGlobalFunc(const char *func_name_string, | 474 const CJS_Parameters& params, |
| 475 CJS_Value& vRet, |
| 476 CFX_WideString& sError)> |
| 477 void JSGlobalFunc(const char* func_name_string, |
387 const v8::FunctionCallbackInfo<v8::Value>& info) { | 478 const v8::FunctionCallbackInfo<v8::Value>& info) { |
388 v8::Isolate* isolate = info.GetIsolate(); | 479 v8::Isolate* isolate = info.GetIsolate(); |
389 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | 480 v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
390 v8::Local<v8::Value> v = context->GetEmbedderData(1); | 481 v8::Local<v8::Value> v = context->GetEmbedderData(1); |
391 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); | 482 v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v); |
392 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); | 483 IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value(); |
393 IFXJS_Context* cc = pRuntime->GetCurrentContext(); | 484 IFXJS_Context* cc = pRuntime->GetCurrentContext(); |
394 CJS_Parameters parameters; | 485 CJS_Parameters parameters; |
395 for (unsigned int i = 0; i<(unsigned int)info.Length(); i++) { | 486 for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) { |
396 parameters.push_back(CJS_Value(isolate, info[i], VT_unknown)); | 487 parameters.push_back(CJS_Value(isolate, info[i], VT_unknown)); |
397 } | 488 } |
398 CJS_Value valueRes(isolate); | 489 CJS_Value valueRes(isolate); |
399 CFX_WideString sError; | 490 CFX_WideString sError; |
400 if (!(*F)(cc, parameters, valueRes, sError)) | 491 if (!(*F)(cc, parameters, valueRes, sError)) { |
401 { | 492 JS_Error(isolate, JSFormatErrorString(func_name_string, nullptr, sError)); |
402 JS_Error(isolate, JSFormatErrorString(func_name_string, nullptr, sError)); | |
403 return; | 493 return; |
404 } | 494 } |
405 info.GetReturnValue().Set(valueRes.ToV8Value()); | 495 info.GetReturnValue().Set(valueRes.ToV8Value()); |
406 } | 496 } |
407 | 497 |
408 #define JS_STATIC_GLOBAL_FUN(fun_name) \ | 498 #define JS_STATIC_GLOBAL_FUN(fun_name) \ |
409 static void fun_name##_static(const v8::FunctionCallbackInfo<v8::Value>& info)
{ \ | 499 static void fun_name##_static( \ |
410 JSGlobalFunc<fun_name>(#fun_name, info); \ | 500 const v8::FunctionCallbackInfo<v8::Value>& info) { \ |
| 501 JSGlobalFunc<fun_name>(#fun_name, info); \ |
411 } | 502 } |
412 | 503 |
413 #define JS_STATIC_DECLARE_GLOBAL_FUN() \ | 504 #define JS_STATIC_DECLARE_GLOBAL_FUN() \ |
414 static JSMethodSpec» global_methods[]; \ | 505 static JSMethodSpec global_methods[]; \ |
415 static int Init(IJS_Runtime* pRuntime) | 506 static int Init(IJS_Runtime* pRuntime) |
416 | 507 |
417 #define BEGIN_JS_STATIC_GLOBAL_FUN(js_class_name) \ | 508 #define BEGIN_JS_STATIC_GLOBAL_FUN(js_class_name) \ |
418 JSMethodSpec js_class_name::global_methods[] = { | 509 JSMethodSpec js_class_name::global_methods[] = { |
419 | 510 #define JS_STATIC_GLOBAL_FUN_ENTRY(method_name) \ |
420 #define JS_STATIC_GLOBAL_FUN_ENTRY(method_name) JS_STATIC_METHOD_ENTRY(method_na
me) | 511 JS_STATIC_METHOD_ENTRY(method_name) |
421 | 512 |
422 #define END_JS_STATIC_GLOBAL_FUN() END_JS_STATIC_METHOD() | 513 #define END_JS_STATIC_GLOBAL_FUN() END_JS_STATIC_METHOD() |
423 | 514 |
424 #define IMPLEMENT_JS_STATIC_GLOBAL_FUN(js_class_name) \ | 515 #define IMPLEMENT_JS_STATIC_GLOBAL_FUN(js_class_name) \ |
425 int js_class_name::Init(IJS_Runtime* pRuntime)\ | 516 int js_class_name::Init(IJS_Runtime* pRuntime) { \ |
426 {\ | 517 for (int i = 0, sz = sizeof(js_class_name::global_methods) / \ |
427 » for (int i=0, sz=sizeof(js_class_name::global_methods)/sizeof(JSMethodSp
ec)-1; i<sz; i++)\ | 518 sizeof(JSMethodSpec) - \ |
428 » {\ | 519 1; \ |
429 » » if (JS_DefineGlobalMethod(pRuntime,\ | 520 i < sz; i++) { \ |
430 » » » » js_class_name::global_methods[i].pName,\ | 521 if (JS_DefineGlobalMethod( \ |
431 » » » » js_class_name::global_methods[i].pMethodCall\ | 522 pRuntime, js_class_name::global_methods[i].pName, \ |
432 » » » » ) < 0\ | 523 js_class_name::global_methods[i].pMethodCall) < 0) \ |
433 » » » )return -1;\ | 524 return -1; \ |
434 » }\ | 525 } \ |
435 » return 0;\ | 526 return 0; \ |
436 } | 527 } |
437 | 528 |
438 /* ======================================== GLOBAL CONSTS ======================
====================== */ | 529 /* ======================================== GLOBAL CONSTS |
439 #define DEFINE_GLOBAL_CONST(pRuntime, const_name , const_value)\ | 530 * ============================================ */ |
440 if (JS_DefineGlobalConst(pRuntime,JS_WIDESTRING(const_name),JS_NewString(pRuntim
e,JS_WIDESTRING(const_value)))) return -1 | 531 #define DEFINE_GLOBAL_CONST(pRuntime, const_name, const_value) \ |
| 532 if (JS_DefineGlobalConst( \ |
| 533 pRuntime, JS_WIDESTRING(const_name), \ |
| 534 JS_NewString(pRuntime, JS_WIDESTRING(const_value)))) \ |
| 535 return -1 |
441 | 536 |
442 /* ======================================== GLOBAL ARRAYS ======================
====================== */ | 537 /* ======================================== GLOBAL ARRAYS |
| 538 * ============================================ */ |
443 | 539 |
444 #define DEFINE_GLOBAL_ARRAY(pRuntime)\ | 540 #define DEFINE_GLOBAL_ARRAY(pRuntime) \ |
445 int size = FX_ArraySize(ArrayContent);\ | 541 int size = FX_ArraySize(ArrayContent); \ |
446 \ | 542 \ |
447 CJS_Array array(pRuntime);\ | 543 CJS_Array array(pRuntime); \ |
448 for (int i=0; i<size; i++) array.SetElement(i,CJS_Value(pRuntime,ArrayContent[i]
));\ | 544 for (int i = 0; i < size; i++) \ |
449 \ | 545 array.SetElement(i, CJS_Value(pRuntime, ArrayContent[i])); \ |
450 CJS_PropValue prop(pRuntime);\ | 546 \ |
451 prop << array;\ | 547 CJS_PropValue prop(pRuntime); \ |
452 if (JS_DefineGlobalConst(pRuntime, (const wchar_t*)ArrayName, prop.ToV8Value())
< 0)\ | 548 prop << array; \ |
453 » return -1 | 549 if (JS_DefineGlobalConst(pRuntime, (const wchar_t*)ArrayName, \ |
| 550 prop.ToV8Value()) < 0) \ |
| 551 return -1 |
454 | 552 |
455 /* ============================================================ */ | 553 /* ============================================================ */ |
456 | 554 |
457 #define VALUE_NAME_STRING» » L"string" | 555 #define VALUE_NAME_STRING L"string" |
458 #define VALUE_NAME_NUMBER» » L"number" | 556 #define VALUE_NAME_NUMBER L"number" |
459 #define VALUE_NAME_BOOLEAN» » L"boolean" | 557 #define VALUE_NAME_BOOLEAN L"boolean" |
460 #define VALUE_NAME_DATE»» » L"date" | 558 #define VALUE_NAME_DATE L"date" |
461 #define VALUE_NAME_OBJECT» » L"object" | 559 #define VALUE_NAME_OBJECT L"object" |
462 #define VALUE_NAME_FXOBJ» » L"fxobj" | 560 #define VALUE_NAME_FXOBJ L"fxobj" |
463 #define VALUE_NAME_NULL»» » L"null" | 561 #define VALUE_NAME_NULL L"null" |
464 #define VALUE_NAME_UNDEFINED» L"undefined" | 562 #define VALUE_NAME_UNDEFINED L"undefined" |
465 | 563 |
466 FXJSVALUETYPE GET_VALUE_TYPE(v8::Local<v8::Value> p); | 564 FXJSVALUETYPE GET_VALUE_TYPE(v8::Local<v8::Value> p); |
467 | 565 |
468 #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_ | 566 #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_DEFINE_H_ |
OLD | NEW |