Fix build broken at 506df426d5d6.
[pdfium.git] / fpdfsdk / src / jsapi / fxjs_v8.cpp
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.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "../../../core/include/fxcrt/fx_basic.h"
8 #include "../../include/fsdk_define.h"
9 #include "../../include/jsapi/fxjs_v8.h"
10
11 const wchar_t kFXJSValueNameString[] = L"string";
12 const wchar_t kFXJSValueNameNumber[] = L"number";
13 const wchar_t kFXJSValueNameBoolean[] = L"boolean";
14 const wchar_t kFXJSValueNameDate[] = L"date";
15 const wchar_t kFXJSValueNameObject[] = L"object";
16 const wchar_t kFXJSValueNameFxobj[] = L"fxobj";
17 const wchar_t kFXJSValueNameNull[] = L"null";
18 const wchar_t kFXJSValueNameUndefined[] = L"undefined";
19
20 static unsigned int g_embedderDataSlot = 0u;
21
22 class CFXJS_PrivateData {
23  public:
24   CFXJS_PrivateData() : ObjDefID(-1), pPrivate(NULL) {}
25   int ObjDefID;
26   void* pPrivate;
27 };
28
29 class CFXJS_ObjDefintion {
30  public:
31   CFXJS_ObjDefintion(v8::Isolate* isolate,
32                      const wchar_t* sObjName,
33                      FXJSOBJTYPE eObjType,
34                      FXJS_CONSTRUCTOR pConstructor,
35                      FXJS_DESTRUCTOR pDestructor)
36       : objName(sObjName),
37         objType(eObjType),
38         m_pConstructor(pConstructor),
39         m_pDestructor(pDestructor),
40         m_bSetAsGlobalObject(FALSE) {
41     v8::Isolate::Scope isolate_scope(isolate);
42     v8::HandleScope handle_scope(isolate);
43
44     v8::Local<v8::ObjectTemplate> objTemplate =
45         v8::ObjectTemplate::New(isolate);
46     objTemplate->SetInternalFieldCount(2);
47     m_objTemplate.Reset(isolate, objTemplate);
48
49     // Document as the global object.
50     if (FXSYS_wcscmp(sObjName, L"Document") == 0) {
51       m_bSetAsGlobalObject = TRUE;
52     }
53   }
54   ~CFXJS_ObjDefintion() {
55     m_objTemplate.Reset();
56     m_StaticObj.Reset();
57   }
58
59  public:
60   const wchar_t* objName;
61   FXJSOBJTYPE objType;
62   FXJS_CONSTRUCTOR m_pConstructor;
63   FXJS_DESTRUCTOR m_pDestructor;
64   FX_BOOL m_bSetAsGlobalObject;
65
66   v8::Global<v8::ObjectTemplate> m_objTemplate;
67   v8::Global<v8::Object> m_StaticObj;
68 };
69
70 void* FXJS_ArrayBufferAllocator::Allocate(size_t length) {
71   return calloc(1, length);
72 }
73
74 void* FXJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) {
75   return malloc(length);
76 }
77
78 void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) {
79   free(data);
80 }
81
82 void FXJS_PrepareIsolate(v8::Isolate* pIsolate) {
83   if (!pIsolate->GetData(g_embedderDataSlot))
84     pIsolate->SetData(g_embedderDataSlot, new CFX_PtrArray());
85 }
86
87 int FXJS_DefineObj(v8::Isolate* pIsolate,
88                    const wchar_t* sObjName,
89                    FXJSOBJTYPE eObjType,
90                    FXJS_CONSTRUCTOR pConstructor,
91                    FXJS_DESTRUCTOR pDestructor) {
92   v8::Isolate::Scope isolate_scope(pIsolate);
93   v8::HandleScope handle_scope(pIsolate);
94
95   FXJS_PrepareIsolate(pIsolate);
96   CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot);
97   CFXJS_ObjDefintion* pObjDef = new CFXJS_ObjDefintion(
98       pIsolate, sObjName, eObjType, pConstructor, pDestructor);
99   pArray->Add(pObjDef);
100   return pArray->GetSize() - 1;
101 }
102
103 void FXJS_DefineObjMethod(v8::Isolate* pIsolate,
104                           int nObjDefnID,
105                           const wchar_t* sMethodName,
106                           v8::FunctionCallback pMethodCall) {
107   v8::Isolate::Scope isolate_scope(pIsolate);
108   v8::HandleScope handle_scope(pIsolate);
109
110   CFX_WideString ws = CFX_WideString(sMethodName);
111   CFX_ByteString bsMethodName = ws.UTF8Encode();
112   CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot);
113
114   // Note: GetAt() halts if out-of-range even in release builds.
115   CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
116   v8::Local<v8::ObjectTemplate> objTemp =
117       v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate);
118   objTemp->Set(
119       v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(),
120                               v8::NewStringType::kNormal).ToLocalChecked(),
121       v8::FunctionTemplate::New(pIsolate, pMethodCall), v8::ReadOnly);
122   pObjDef->m_objTemplate.Reset(pIsolate, objTemp);
123 }
124
125 void FXJS_DefineObjProperty(v8::Isolate* pIsolate,
126                             int nObjDefnID,
127                             const wchar_t* sPropName,
128                             v8::AccessorGetterCallback pPropGet,
129                             v8::AccessorSetterCallback pPropPut) {
130   v8::Isolate::Scope isolate_scope(pIsolate);
131   v8::HandleScope handle_scope(pIsolate);
132
133   CFX_WideString ws = CFX_WideString(sPropName);
134   CFX_ByteString bsPropertyName = ws.UTF8Encode();
135   CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot);
136
137   // Note: GetAt() halts if out-of-range even in release builds.
138   CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
139   v8::Local<v8::ObjectTemplate> objTemp =
140       v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate);
141   objTemp->SetAccessor(
142       v8::String::NewFromUtf8(pIsolate, bsPropertyName.c_str(),
143                               v8::NewStringType::kNormal).ToLocalChecked(),
144       pPropGet, pPropPut);
145   pObjDef->m_objTemplate.Reset(pIsolate, objTemp);
146 }
147
148 void FXJS_DefineObjAllProperties(v8::Isolate* pIsolate,
149                                  int nObjDefnID,
150                                  v8::NamedPropertyQueryCallback pPropQurey,
151                                  v8::NamedPropertyGetterCallback pPropGet,
152                                  v8::NamedPropertySetterCallback pPropPut,
153                                  v8::NamedPropertyDeleterCallback pPropDel) {
154   v8::Isolate::Scope isolate_scope(pIsolate);
155   v8::HandleScope handle_scope(pIsolate);
156   CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot);
157
158   // Note: GetAt() halts if out-of-range even in release builds.
159   CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
160   v8::Local<v8::ObjectTemplate> objTemp =
161       v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate);
162   objTemp->SetNamedPropertyHandler(pPropGet, pPropPut, pPropQurey, pPropDel);
163   pObjDef->m_objTemplate.Reset(pIsolate, objTemp);
164 }
165
166 void FXJS_DefineObjConst(v8::Isolate* pIsolate,
167                          int nObjDefnID,
168                          const wchar_t* sConstName,
169                          v8::Local<v8::Value> pDefault) {
170   v8::Isolate::Scope isolate_scope(pIsolate);
171   v8::HandleScope handle_scope(pIsolate);
172
173   CFX_WideString ws = CFX_WideString(sConstName);
174   CFX_ByteString bsConstName = ws.UTF8Encode();
175   CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot);
176
177   // Note: GetAt() halts if out-of-range even in release builds.
178   CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
179   v8::Local<v8::ObjectTemplate> objTemp =
180       v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate);
181   objTemp->Set(pIsolate, bsConstName.c_str(), pDefault);
182   pObjDef->m_objTemplate.Reset(pIsolate, objTemp);
183 }
184
185 static v8::Global<v8::ObjectTemplate>& _getGlobalObjectTemplate(
186     v8::Isolate* pIsolate) {
187   v8::Isolate::Scope isolate_scope(pIsolate);
188   v8::HandleScope handle_scope(pIsolate);
189
190   CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot);
191   ASSERT(pArray != NULL);
192   for (int i = 0; i < pArray->GetSize(); i++) {
193     CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(i);
194     if (pObjDef->m_bSetAsGlobalObject)
195       return pObjDef->m_objTemplate;
196   }
197   static v8::Global<v8::ObjectTemplate> gloabalObjectTemplate;
198   return gloabalObjectTemplate;
199 }
200
201 void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate,
202                              const wchar_t* sMethodName,
203                              v8::FunctionCallback pMethodCall) {
204   v8::Isolate::Scope isolate_scope(pIsolate);
205   v8::HandleScope handle_scope(pIsolate);
206
207   CFX_WideString ws = CFX_WideString(sMethodName);
208   CFX_ByteString bsMethodName = ws.UTF8Encode();
209
210   v8::Local<v8::FunctionTemplate> funTempl =
211       v8::FunctionTemplate::New(pIsolate, pMethodCall);
212   v8::Local<v8::ObjectTemplate> objTemp;
213
214   v8::Global<v8::ObjectTemplate>& globalObjTemp =
215       _getGlobalObjectTemplate(pIsolate);
216   if (globalObjTemp.IsEmpty())
217     objTemp = v8::ObjectTemplate::New(pIsolate);
218   else
219     objTemp = v8::Local<v8::ObjectTemplate>::New(pIsolate, globalObjTemp);
220   objTemp->Set(
221       v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(),
222                               v8::NewStringType::kNormal).ToLocalChecked(),
223       funTempl, v8::ReadOnly);
224
225   globalObjTemp.Reset(pIsolate, objTemp);
226 }
227
228 void FXJS_DefineGlobalConst(v8::Isolate* pIsolate,
229                             const wchar_t* sConstName,
230                             v8::Local<v8::Value> pDefault) {
231   v8::Isolate::Scope isolate_scope(pIsolate);
232   v8::HandleScope handle_scope(pIsolate);
233
234   CFX_WideString ws = CFX_WideString(sConstName);
235   CFX_ByteString bsConst = ws.UTF8Encode();
236
237   v8::Local<v8::ObjectTemplate> objTemp;
238
239   v8::Global<v8::ObjectTemplate>& globalObjTemp =
240       _getGlobalObjectTemplate(pIsolate);
241   if (globalObjTemp.IsEmpty())
242     objTemp = v8::ObjectTemplate::New(pIsolate);
243   else
244     objTemp = v8::Local<v8::ObjectTemplate>::New(pIsolate, globalObjTemp);
245   objTemp->Set(
246       v8::String::NewFromUtf8(pIsolate, bsConst.c_str(),
247                               v8::NewStringType::kNormal).ToLocalChecked(),
248       pDefault, v8::ReadOnly);
249
250   globalObjTemp.Reset(pIsolate, objTemp);
251 }
252
253 void FXJS_InitializeRuntime(v8::Isolate* pIsolate,
254                             IFXJS_Runtime* pFXRuntime,
255                             IFXJS_Context* context,
256                             v8::Global<v8::Context>& v8PersistentContext) {
257   v8::Isolate::Scope isolate_scope(pIsolate);
258   v8::HandleScope handle_scope(pIsolate);
259
260   v8::Global<v8::ObjectTemplate>& globalObjTemp =
261       _getGlobalObjectTemplate(pIsolate);
262   v8::Local<v8::Context> v8Context = v8::Context::New(
263       pIsolate, NULL,
264       v8::Local<v8::ObjectTemplate>::New(pIsolate, globalObjTemp));
265   v8::Context::Scope context_scope(v8Context);
266
267   v8::Local<v8::External> ptr = v8::External::New(pIsolate, pFXRuntime);
268   v8Context->SetEmbedderData(1, ptr);
269
270   CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot);
271   if (!pArray)
272     return;
273
274   for (int i = 0; i < pArray->GetSize(); i++) {
275     CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(i);
276     CFX_WideString ws = CFX_WideString(pObjDef->objName);
277     CFX_ByteString bs = ws.UTF8Encode();
278     v8::Local<v8::String> objName =
279         v8::String::NewFromUtf8(pIsolate, bs.c_str(),
280                                 v8::NewStringType::kNormal,
281                                 bs.GetLength()).ToLocalChecked();
282
283     if (pObjDef->objType == FXJS_DYNAMIC) {
284       // Document is set as global object, need to construct it first.
285       if (ws.Equal(L"Document")) {
286         CFXJS_PrivateData* pPrivateData = new CFXJS_PrivateData;
287         pPrivateData->ObjDefID = i;
288
289         v8Context->Global()
290             ->GetPrototype()
291             ->ToObject(v8Context)
292             .ToLocalChecked()
293             ->SetAlignedPointerInInternalField(0, pPrivateData);
294
295         if (pObjDef->m_pConstructor)
296           pObjDef->m_pConstructor(context, v8Context->Global()
297                                                ->GetPrototype()
298                                                ->ToObject(v8Context)
299                                                .ToLocalChecked(),
300                                   v8Context->Global()
301                                       ->GetPrototype()
302                                       ->ToObject(v8Context)
303                                       .ToLocalChecked());
304       }
305     } else {
306       v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, context, i);
307       v8Context->Global()->Set(v8Context, objName, obj).FromJust();
308       pObjDef->m_StaticObj.Reset(pIsolate, obj);
309     }
310   }
311   v8PersistentContext.Reset(pIsolate, v8Context);
312 }
313
314 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate,
315                          v8::Global<v8::Context>& v8PersistentContext) {
316   v8::Isolate::Scope isolate_scope(pIsolate);
317   v8::HandleScope handle_scope(pIsolate);
318   v8::Local<v8::Context> context =
319       v8::Local<v8::Context>::New(pIsolate, v8PersistentContext);
320   v8::Context::Scope context_scope(context);
321
322   CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot);
323   if (!pArray)
324     return;
325
326   for (int i = 0; i < pArray->GetSize(); i++) {
327     CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(i);
328     if (!pObjDef->m_StaticObj.IsEmpty()) {
329       v8::Local<v8::Object> pObj =
330           v8::Local<v8::Object>::New(pIsolate, pObjDef->m_StaticObj);
331       if (pObjDef->m_pDestructor)
332         pObjDef->m_pDestructor(pObj);
333       FXJS_FreePrivate(pObj);
334     }
335     delete pObjDef;
336   }
337   delete pArray;
338   pIsolate->SetData(g_embedderDataSlot, NULL);
339 }
340
341 void FXJS_Initialize(unsigned int embedderDataSlot) {
342   g_embedderDataSlot = embedderDataSlot;
343 }
344
345 void FXJS_Release() {
346 }
347
348 int FXJS_Execute(v8::Isolate* pIsolate,
349                  IFXJS_Context* pJSContext,
350                  const wchar_t* script,
351                  long length,
352                  FXJSErr* pError) {
353   v8::Isolate::Scope isolate_scope(pIsolate);
354   v8::TryCatch try_catch(pIsolate);
355
356   CFX_WideString wsScript(script);
357   CFX_ByteString bsScript = wsScript.UTF8Encode();
358
359   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
360   v8::Local<v8::Script> compiled_script;
361   if (!v8::Script::Compile(
362            context, v8::String::NewFromUtf8(
363                         pIsolate, bsScript.c_str(), v8::NewStringType::kNormal,
364                         bsScript.GetLength()).ToLocalChecked())
365            .ToLocal(&compiled_script)) {
366     v8::String::Utf8Value error(try_catch.Exception());
367     // TODO(tsepez): return error via pError->message.
368     return -1;
369   }
370
371   v8::Local<v8::Value> result;
372   if (!compiled_script->Run(context).ToLocal(&result)) {
373     v8::String::Utf8Value error(try_catch.Exception());
374     // TODO(tsepez): return error via pError->message.
375     return -1;
376   }
377   return 0;
378 }
379
380 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate,
381                                            IFXJS_Context* pJSContext,
382                                            int nObjDefnID) {
383   v8::Isolate::Scope isolate_scope(pIsolate);
384   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
385   if (-1 == nObjDefnID) {
386     v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate);
387     v8::Local<v8::Object> obj;
388     if (objTempl->NewInstance(context).ToLocal(&obj))
389       return obj;
390     return v8::Local<v8::Object>();
391   }
392
393   CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot);
394   if (!pArray)
395     return v8::Local<v8::Object>();
396
397   if (nObjDefnID < 0 || nObjDefnID >= pArray->GetSize())
398     return v8::Local<v8::Object>();
399   CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
400
401   v8::Local<v8::ObjectTemplate> objTemp =
402       v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate);
403   v8::Local<v8::Object> obj;
404   if (!objTemp->NewInstance(context).ToLocal(&obj))
405     return v8::Local<v8::Object>();
406
407   CFXJS_PrivateData* pPrivateData = new CFXJS_PrivateData;
408   pPrivateData->ObjDefID = nObjDefnID;
409
410   obj->SetAlignedPointerInInternalField(0, pPrivateData);
411   if (pObjDef->m_pConstructor)
412     pObjDef->m_pConstructor(
413         pJSContext, obj,
414         context->Global()->GetPrototype()->ToObject(context).ToLocalChecked());
415
416   return obj;
417 }
418
419 v8::Local<v8::Object> FXJS_GetStaticObj(v8::Isolate* pIsolate, int nObjDefnID) {
420   v8::Isolate::Scope isolate_scope(pIsolate);
421   CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot);
422   if (!pArray)
423     return v8::Local<v8::Object>();
424
425   if (nObjDefnID < 0 || nObjDefnID >= pArray->GetSize())
426     return v8::Local<v8::Object>();
427   CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
428   v8::Local<v8::Object> obj =
429       v8::Local<v8::Object>::New(pIsolate, pObjDef->m_StaticObj);
430   return obj;
431 }
432
433 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) {
434   // Return the global object.
435   v8::Isolate::Scope isolate_scope(pIsolate);
436   CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot);
437   if (!pArray)
438     return v8::Local<v8::Object>();
439
440   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
441   return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked();
442 }
443
444 int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj) {
445   if (pObj.IsEmpty() || !pObj->InternalFieldCount())
446     return -1;
447   CFXJS_PrivateData* pPrivateData =
448       (CFXJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0);
449   if (pPrivateData)
450     return pPrivateData->ObjDefID;
451   return -1;
452 }
453
454 v8::Isolate* FXJS_GetRuntime(v8::Local<v8::Object> pObj) {
455   if (pObj.IsEmpty())
456     return NULL;
457   v8::Local<v8::Context> context = pObj->CreationContext();
458   if (context.IsEmpty())
459     return NULL;
460   return context->GetIsolate();
461 }
462
463 int FXJS_GetObjDefnID(v8::Isolate* pIsolate, const wchar_t* pObjName) {
464   v8::Isolate::Scope isolate_scope(pIsolate);
465   CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot);
466   if (!pArray)
467     return -1;
468
469   for (int i = 0; i < pArray->GetSize(); i++) {
470     CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(i);
471     if (FXSYS_wcscmp(pObjDef->objName, pObjName) == 0)
472       return i;
473   }
474   return -1;
475 }
476
477 void FXJS_Error(v8::Isolate* pIsolate, const CFX_WideString& message) {
478   // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t
479   // wide-strings isn't handled by v8, so use UTF8 as a common
480   // intermediate format.
481   CFX_ByteString utf8_message = message.UTF8Encode();
482   pIsolate->ThrowException(
483       v8::String::NewFromUtf8(pIsolate, utf8_message.c_str(),
484                               v8::NewStringType::kNormal).ToLocalChecked());
485 }
486
487 const wchar_t* FXJS_GetTypeof(v8::Local<v8::Value> pObj) {
488   if (pObj.IsEmpty())
489     return NULL;
490   if (pObj->IsString())
491     return kFXJSValueNameString;
492   if (pObj->IsNumber())
493     return kFXJSValueNameNumber;
494   if (pObj->IsBoolean())
495     return kFXJSValueNameBoolean;
496   if (pObj->IsDate())
497     return kFXJSValueNameDate;
498   if (pObj->IsObject())
499     return kFXJSValueNameObject;
500   if (pObj->IsNull())
501     return kFXJSValueNameNull;
502   if (pObj->IsUndefined())
503     return kFXJSValueNameUndefined;
504   return NULL;
505 }
506 void FXJS_SetPrivate(v8::Local<v8::Object> pObj, void* p) {
507   FXJS_SetPrivate(NULL, pObj, p);
508 }
509
510 void* FXJS_GetPrivate(v8::Local<v8::Object> pObj) {
511   return FXJS_GetPrivate(NULL, pObj);
512 }
513
514 void FXJS_SetPrivate(v8::Isolate* pIsolate,
515                      v8::Local<v8::Object> pObj,
516                      void* p) {
517   if (pObj.IsEmpty() || !pObj->InternalFieldCount())
518     return;
519   CFXJS_PrivateData* pPrivateData =
520       (CFXJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0);
521   if (!pPrivateData)
522     return;
523   pPrivateData->pPrivate = p;
524 }
525
526 void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj) {
527   if (pObj.IsEmpty())
528     return NULL;
529   CFXJS_PrivateData* pPrivateData = NULL;
530   if (pObj->InternalFieldCount())
531     pPrivateData =
532         (CFXJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0);
533   else {
534     // It could be a global proxy object.
535     v8::Local<v8::Value> v = pObj->GetPrototype();
536     v8::Isolate* isolate = (v8::Isolate*)pIsolate;
537     v8::Local<v8::Context> context = isolate->GetCurrentContext();
538     if (v->IsObject())
539       pPrivateData = (CFXJS_PrivateData*)v->ToObject(context)
540                          .ToLocalChecked()
541                          ->GetAlignedPointerFromInternalField(0);
542   }
543   if (!pPrivateData)
544     return NULL;
545   return pPrivateData->pPrivate;
546 }
547
548 void FXJS_FreePrivate(void* pPrivateData) {
549   delete (CFXJS_PrivateData*)pPrivateData;
550 }
551
552 void FXJS_FreePrivate(v8::Local<v8::Object> pObj) {
553   if (pObj.IsEmpty() || !pObj->InternalFieldCount())
554     return;
555   FXJS_FreePrivate(pObj->GetAlignedPointerFromInternalField(0));
556   pObj->SetAlignedPointerInInternalField(0, NULL);
557 }
558
559 v8::Local<v8::String> FXJS_WSToJSString(v8::Isolate* pIsolate,
560                                         const wchar_t* PropertyName,
561                                         int Len) {
562   CFX_WideString ws = CFX_WideString(PropertyName, Len);
563   CFX_ByteString bs = ws.UTF8Encode();
564   if (!pIsolate)
565     pIsolate = v8::Isolate::GetCurrent();
566   return v8::String::NewFromUtf8(pIsolate, bs.c_str(),
567                                  v8::NewStringType::kNormal).ToLocalChecked();
568 }
569
570 v8::Local<v8::Value> FXJS_GetObjectValue(v8::Local<v8::Object> pObj) {
571   return pObj;
572 }
573
574 v8::Local<v8::Value> FXJS_GetObjectElement(v8::Isolate* pIsolate,
575                                            v8::Local<v8::Object> pObj,
576                                            const wchar_t* PropertyName) {
577   if (pObj.IsEmpty())
578     return v8::Local<v8::Value>();
579   v8::Local<v8::Value> val;
580   if (!pObj->Get(pIsolate->GetCurrentContext(),
581                  FXJS_WSToJSString(pIsolate, PropertyName)).ToLocal(&val))
582     return v8::Local<v8::Value>();
583   return val;
584 }
585
586 v8::Local<v8::Array> FXJS_GetObjectElementNames(v8::Isolate* pIsolate,
587                                                 v8::Local<v8::Object> pObj) {
588   if (pObj.IsEmpty())
589     return v8::Local<v8::Array>();
590   v8::Local<v8::Array> val;
591   if (!pObj->GetPropertyNames(pIsolate->GetCurrentContext()).ToLocal(&val))
592     return v8::Local<v8::Array>();
593   return val;
594 }
595
596 void FXJS_PutObjectString(v8::Isolate* pIsolate,
597                           v8::Local<v8::Object> pObj,
598                           const wchar_t* PropertyName,
599                           const wchar_t* sValue)  // VT_string
600 {
601   if (pObj.IsEmpty())
602     return;
603   pObj->Set(pIsolate->GetCurrentContext(),
604             FXJS_WSToJSString(pIsolate, PropertyName),
605             FXJS_WSToJSString(pIsolate, sValue)).FromJust();
606 }
607
608 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
609                           v8::Local<v8::Object> pObj,
610                           const wchar_t* PropertyName,
611                           int nValue) {
612   if (pObj.IsEmpty())
613     return;
614   pObj->Set(pIsolate->GetCurrentContext(),
615             FXJS_WSToJSString(pIsolate, PropertyName),
616             v8::Int32::New(pIsolate, nValue)).FromJust();
617 }
618
619 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
620                           v8::Local<v8::Object> pObj,
621                           const wchar_t* PropertyName,
622                           float fValue) {
623   if (pObj.IsEmpty())
624     return;
625   pObj->Set(pIsolate->GetCurrentContext(),
626             FXJS_WSToJSString(pIsolate, PropertyName),
627             v8::Number::New(pIsolate, (double)fValue)).FromJust();
628 }
629
630 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
631                           v8::Local<v8::Object> pObj,
632                           const wchar_t* PropertyName,
633                           double dValue) {
634   if (pObj.IsEmpty())
635     return;
636   pObj->Set(pIsolate->GetCurrentContext(),
637             FXJS_WSToJSString(pIsolate, PropertyName),
638             v8::Number::New(pIsolate, (double)dValue)).FromJust();
639 }
640
641 void FXJS_PutObjectBoolean(v8::Isolate* pIsolate,
642                            v8::Local<v8::Object> pObj,
643                            const wchar_t* PropertyName,
644                            bool bValue) {
645   if (pObj.IsEmpty())
646     return;
647   pObj->Set(pIsolate->GetCurrentContext(),
648             FXJS_WSToJSString(pIsolate, PropertyName),
649             v8::Boolean::New(pIsolate, bValue)).FromJust();
650 }
651
652 void FXJS_PutObjectObject(v8::Isolate* pIsolate,
653                           v8::Local<v8::Object> pObj,
654                           const wchar_t* PropertyName,
655                           v8::Local<v8::Object> pPut) {
656   if (pObj.IsEmpty())
657     return;
658   pObj->Set(pIsolate->GetCurrentContext(),
659             FXJS_WSToJSString(pIsolate, PropertyName), pPut).FromJust();
660 }
661
662 void FXJS_PutObjectNull(v8::Isolate* pIsolate,
663                         v8::Local<v8::Object> pObj,
664                         const wchar_t* PropertyName) {
665   if (pObj.IsEmpty())
666     return;
667   pObj->Set(pIsolate->GetCurrentContext(),
668             FXJS_WSToJSString(pIsolate, PropertyName),
669             v8::Local<v8::Object>()).FromJust();
670 }
671
672 v8::Local<v8::Array> FXJS_NewArray(v8::Isolate* pIsolate) {
673   return v8::Array::New(pIsolate);
674 }
675
676 unsigned FXJS_PutArrayElement(v8::Isolate* pIsolate,
677                               v8::Local<v8::Array> pArray,
678                               unsigned index,
679                               v8::Local<v8::Value> pValue) {
680   if (pArray.IsEmpty())
681     return 0;
682   if (pArray->Set(pIsolate->GetCurrentContext(), index, pValue).IsNothing())
683     return 0;
684   return 1;
685 }
686
687 v8::Local<v8::Value> FXJS_GetArrayElement(v8::Isolate* pIsolate,
688                                           v8::Local<v8::Array> pArray,
689                                           unsigned index) {
690   if (pArray.IsEmpty())
691     return v8::Local<v8::Value>();
692   v8::Local<v8::Value> val;
693   if (!pArray->Get(pIsolate->GetCurrentContext(), index).ToLocal(&val))
694     return v8::Local<v8::Value>();
695   return val;
696 }
697
698 unsigned FXJS_GetArrayLength(v8::Local<v8::Array> pArray) {
699   if (pArray.IsEmpty())
700     return 0;
701   return pArray->Length();
702 }
703
704 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, int number) {
705   return v8::Int32::New(pIsolate, number);
706 }
707
708 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, double number) {
709   return v8::Number::New(pIsolate, number);
710 }
711
712 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, float number) {
713   return v8::Number::New(pIsolate, (float)number);
714 }
715
716 v8::Local<v8::Value> FXJS_NewBoolean(v8::Isolate* pIsolate, bool b) {
717   return v8::Boolean::New(pIsolate, b);
718 }
719
720 v8::Local<v8::Value> FXJS_NewObject(v8::Isolate* pIsolate,
721                                     v8::Local<v8::Object> pObj) {
722   if (pObj.IsEmpty())
723     return v8::Local<v8::Value>();
724   return pObj->Clone();
725 }
726
727 v8::Local<v8::Value> FXJS_NewObject2(v8::Isolate* pIsolate,
728                                      v8::Local<v8::Array> pObj) {
729   if (pObj.IsEmpty())
730     return v8::Local<v8::Value>();
731   return pObj->Clone();
732 }
733
734 v8::Local<v8::Value> FXJS_NewString(v8::Isolate* pIsolate,
735                                     const wchar_t* string) {
736   return FXJS_WSToJSString(pIsolate, string);
737 }
738
739 v8::Local<v8::Value> FXJS_NewString(v8::Isolate* pIsolate,
740                                     const wchar_t* string,
741                                     unsigned nLen) {
742   return FXJS_WSToJSString(pIsolate, string, nLen);
743 }
744
745 v8::Local<v8::Value> FXJS_NewNull() {
746   return v8::Local<v8::Value>();
747 }
748
749 v8::Local<v8::Value> FXJS_NewDate(v8::Isolate* pIsolate, double d) {
750   return v8::Date::New(pIsolate->GetCurrentContext(), d).ToLocalChecked();
751 }
752
753 v8::Local<v8::Value> FXJS_NewValue(v8::Isolate* pIsolate) {
754   return v8::Local<v8::Value>();
755 }
756
757 v8::Local<v8::Value> FXJS_GetListValue(v8::Isolate* pIsolate,
758                                        v8::Local<v8::Value> pList,
759                                        int index) {
760   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
761   if (!pList.IsEmpty() && pList->IsObject()) {
762     v8::Local<v8::Object> obj;
763     if (pList->ToObject(context).ToLocal(&obj)) {
764       v8::Local<v8::Value> val;
765       if (obj->Get(context, index).ToLocal(&val))
766         return val;
767     }
768   }
769   return v8::Local<v8::Value>();
770 }
771
772 int FXJS_ToInt32(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) {
773   if (pValue.IsEmpty())
774     return 0;
775   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
776   return pValue->ToInt32(context).ToLocalChecked()->Value();
777 }
778
779 bool FXJS_ToBoolean(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) {
780   if (pValue.IsEmpty())
781     return false;
782   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
783   return pValue->ToBoolean(context).ToLocalChecked()->Value();
784 }
785
786 double FXJS_ToNumber(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) {
787   if (pValue.IsEmpty())
788     return 0.0;
789   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
790   return pValue->ToNumber(context).ToLocalChecked()->Value();
791 }
792
793 v8::Local<v8::Object> FXJS_ToObject(v8::Isolate* pIsolate,
794                                     v8::Local<v8::Value> pValue) {
795   if (pValue.IsEmpty())
796     return v8::Local<v8::Object>();
797   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
798   return pValue->ToObject(context).ToLocalChecked();
799 }
800
801 CFX_WideString FXJS_ToString(v8::Isolate* pIsolate,
802                              v8::Local<v8::Value> pValue) {
803   if (pValue.IsEmpty())
804     return L"";
805   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
806   v8::String::Utf8Value s(pValue->ToString(context).ToLocalChecked());
807   return CFX_WideString::FromUTF8(*s, s.length());
808 }
809
810 v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate,
811                                   v8::Local<v8::Value> pValue) {
812   if (pValue.IsEmpty())
813     return v8::Local<v8::Array>();
814   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
815   return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked());
816 }
817
818 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) {
819   pTo = pFrom;
820 }
821
822