Don't pass null isolates to FXJS_ when we have a real isolate.
[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_GetThisObj(v8::Isolate* pIsolate) {
420   // Return the global object.
421   v8::Isolate::Scope isolate_scope(pIsolate);
422   CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot);
423   if (!pArray)
424     return v8::Local<v8::Object>();
425
426   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
427   return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked();
428 }
429
430 int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj) {
431   if (pObj.IsEmpty() || !pObj->InternalFieldCount())
432     return -1;
433   CFXJS_PrivateData* pPrivateData =
434       (CFXJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0);
435   if (pPrivateData)
436     return pPrivateData->ObjDefID;
437   return -1;
438 }
439
440 v8::Isolate* FXJS_GetRuntime(v8::Local<v8::Object> pObj) {
441   if (pObj.IsEmpty())
442     return NULL;
443   v8::Local<v8::Context> context = pObj->CreationContext();
444   if (context.IsEmpty())
445     return NULL;
446   return context->GetIsolate();
447 }
448
449 int FXJS_GetObjDefnID(v8::Isolate* pIsolate, const wchar_t* pObjName) {
450   v8::Isolate::Scope isolate_scope(pIsolate);
451   CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot);
452   if (!pArray)
453     return -1;
454
455   for (int i = 0; i < pArray->GetSize(); i++) {
456     CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(i);
457     if (FXSYS_wcscmp(pObjDef->objName, pObjName) == 0)
458       return i;
459   }
460   return -1;
461 }
462
463 void FXJS_Error(v8::Isolate* pIsolate, const CFX_WideString& message) {
464   // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t
465   // wide-strings isn't handled by v8, so use UTF8 as a common
466   // intermediate format.
467   CFX_ByteString utf8_message = message.UTF8Encode();
468   pIsolate->ThrowException(
469       v8::String::NewFromUtf8(pIsolate, utf8_message.c_str(),
470                               v8::NewStringType::kNormal).ToLocalChecked());
471 }
472
473 const wchar_t* FXJS_GetTypeof(v8::Local<v8::Value> pObj) {
474   if (pObj.IsEmpty())
475     return NULL;
476   if (pObj->IsString())
477     return kFXJSValueNameString;
478   if (pObj->IsNumber())
479     return kFXJSValueNameNumber;
480   if (pObj->IsBoolean())
481     return kFXJSValueNameBoolean;
482   if (pObj->IsDate())
483     return kFXJSValueNameDate;
484   if (pObj->IsObject())
485     return kFXJSValueNameObject;
486   if (pObj->IsNull())
487     return kFXJSValueNameNull;
488   if (pObj->IsUndefined())
489     return kFXJSValueNameUndefined;
490   return NULL;
491 }
492
493 void FXJS_SetPrivate(v8::Isolate* pIsolate,
494                      v8::Local<v8::Object> pObj,
495                      void* p) {
496   if (pObj.IsEmpty() || !pObj->InternalFieldCount())
497     return;
498   CFXJS_PrivateData* pPrivateData =
499       (CFXJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0);
500   if (!pPrivateData)
501     return;
502   pPrivateData->pPrivate = p;
503 }
504
505 void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj) {
506   if (pObj.IsEmpty())
507     return nullptr;
508   CFXJS_PrivateData* pPrivateData = nullptr;
509   if (pObj->InternalFieldCount()) {
510     pPrivateData =
511         (CFXJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0);
512   } else {
513     // It could be a global proxy object.
514     v8::Local<v8::Value> v = pObj->GetPrototype();
515     v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
516     if (v->IsObject())
517       pPrivateData = (CFXJS_PrivateData*)v->ToObject(context)
518                          .ToLocalChecked()
519                          ->GetAlignedPointerFromInternalField(0);
520   }
521   return pPrivateData ? pPrivateData->pPrivate : nullptr;
522 }
523
524 void FXJS_FreePrivate(void* pPrivateData) {
525   delete (CFXJS_PrivateData*)pPrivateData;
526 }
527
528 void FXJS_FreePrivate(v8::Local<v8::Object> pObj) {
529   if (pObj.IsEmpty() || !pObj->InternalFieldCount())
530     return;
531   FXJS_FreePrivate(pObj->GetAlignedPointerFromInternalField(0));
532   pObj->SetAlignedPointerInInternalField(0, NULL);
533 }
534
535 v8::Local<v8::String> FXJS_WSToJSString(v8::Isolate* pIsolate,
536                                         const wchar_t* PropertyName,
537                                         int Len) {
538   CFX_WideString ws = CFX_WideString(PropertyName, Len);
539   CFX_ByteString bs = ws.UTF8Encode();
540   if (!pIsolate)
541     pIsolate = v8::Isolate::GetCurrent();
542   return v8::String::NewFromUtf8(pIsolate, bs.c_str(),
543                                  v8::NewStringType::kNormal).ToLocalChecked();
544 }
545
546 v8::Local<v8::Value> FXJS_GetObjectElement(v8::Isolate* pIsolate,
547                                            v8::Local<v8::Object> pObj,
548                                            const wchar_t* PropertyName) {
549   if (pObj.IsEmpty())
550     return v8::Local<v8::Value>();
551   v8::Local<v8::Value> val;
552   if (!pObj->Get(pIsolate->GetCurrentContext(),
553                  FXJS_WSToJSString(pIsolate, PropertyName)).ToLocal(&val))
554     return v8::Local<v8::Value>();
555   return val;
556 }
557
558 v8::Local<v8::Array> FXJS_GetObjectElementNames(v8::Isolate* pIsolate,
559                                                 v8::Local<v8::Object> pObj) {
560   if (pObj.IsEmpty())
561     return v8::Local<v8::Array>();
562   v8::Local<v8::Array> val;
563   if (!pObj->GetPropertyNames(pIsolate->GetCurrentContext()).ToLocal(&val))
564     return v8::Local<v8::Array>();
565   return val;
566 }
567
568 void FXJS_PutObjectString(v8::Isolate* pIsolate,
569                           v8::Local<v8::Object> pObj,
570                           const wchar_t* PropertyName,
571                           const wchar_t* sValue)  // VT_string
572 {
573   if (pObj.IsEmpty())
574     return;
575   pObj->Set(pIsolate->GetCurrentContext(),
576             FXJS_WSToJSString(pIsolate, PropertyName),
577             FXJS_WSToJSString(pIsolate, sValue)).FromJust();
578 }
579
580 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
581                           v8::Local<v8::Object> pObj,
582                           const wchar_t* PropertyName,
583                           int nValue) {
584   if (pObj.IsEmpty())
585     return;
586   pObj->Set(pIsolate->GetCurrentContext(),
587             FXJS_WSToJSString(pIsolate, PropertyName),
588             v8::Int32::New(pIsolate, nValue)).FromJust();
589 }
590
591 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
592                           v8::Local<v8::Object> pObj,
593                           const wchar_t* PropertyName,
594                           float fValue) {
595   if (pObj.IsEmpty())
596     return;
597   pObj->Set(pIsolate->GetCurrentContext(),
598             FXJS_WSToJSString(pIsolate, PropertyName),
599             v8::Number::New(pIsolate, (double)fValue)).FromJust();
600 }
601
602 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
603                           v8::Local<v8::Object> pObj,
604                           const wchar_t* PropertyName,
605                           double dValue) {
606   if (pObj.IsEmpty())
607     return;
608   pObj->Set(pIsolate->GetCurrentContext(),
609             FXJS_WSToJSString(pIsolate, PropertyName),
610             v8::Number::New(pIsolate, (double)dValue)).FromJust();
611 }
612
613 void FXJS_PutObjectBoolean(v8::Isolate* pIsolate,
614                            v8::Local<v8::Object> pObj,
615                            const wchar_t* PropertyName,
616                            bool bValue) {
617   if (pObj.IsEmpty())
618     return;
619   pObj->Set(pIsolate->GetCurrentContext(),
620             FXJS_WSToJSString(pIsolate, PropertyName),
621             v8::Boolean::New(pIsolate, bValue)).FromJust();
622 }
623
624 void FXJS_PutObjectObject(v8::Isolate* pIsolate,
625                           v8::Local<v8::Object> pObj,
626                           const wchar_t* PropertyName,
627                           v8::Local<v8::Object> pPut) {
628   if (pObj.IsEmpty())
629     return;
630   pObj->Set(pIsolate->GetCurrentContext(),
631             FXJS_WSToJSString(pIsolate, PropertyName), pPut).FromJust();
632 }
633
634 void FXJS_PutObjectNull(v8::Isolate* pIsolate,
635                         v8::Local<v8::Object> pObj,
636                         const wchar_t* PropertyName) {
637   if (pObj.IsEmpty())
638     return;
639   pObj->Set(pIsolate->GetCurrentContext(),
640             FXJS_WSToJSString(pIsolate, PropertyName),
641             v8::Local<v8::Object>()).FromJust();
642 }
643
644 v8::Local<v8::Array> FXJS_NewArray(v8::Isolate* pIsolate) {
645   return v8::Array::New(pIsolate);
646 }
647
648 unsigned FXJS_PutArrayElement(v8::Isolate* pIsolate,
649                               v8::Local<v8::Array> pArray,
650                               unsigned index,
651                               v8::Local<v8::Value> pValue) {
652   if (pArray.IsEmpty())
653     return 0;
654   if (pArray->Set(pIsolate->GetCurrentContext(), index, pValue).IsNothing())
655     return 0;
656   return 1;
657 }
658
659 v8::Local<v8::Value> FXJS_GetArrayElement(v8::Isolate* pIsolate,
660                                           v8::Local<v8::Array> pArray,
661                                           unsigned index) {
662   if (pArray.IsEmpty())
663     return v8::Local<v8::Value>();
664   v8::Local<v8::Value> val;
665   if (!pArray->Get(pIsolate->GetCurrentContext(), index).ToLocal(&val))
666     return v8::Local<v8::Value>();
667   return val;
668 }
669
670 unsigned FXJS_GetArrayLength(v8::Local<v8::Array> pArray) {
671   if (pArray.IsEmpty())
672     return 0;
673   return pArray->Length();
674 }
675
676 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, int number) {
677   return v8::Int32::New(pIsolate, number);
678 }
679
680 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, double number) {
681   return v8::Number::New(pIsolate, number);
682 }
683
684 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, float number) {
685   return v8::Number::New(pIsolate, (float)number);
686 }
687
688 v8::Local<v8::Value> FXJS_NewBoolean(v8::Isolate* pIsolate, bool b) {
689   return v8::Boolean::New(pIsolate, b);
690 }
691
692 v8::Local<v8::Value> FXJS_NewObject(v8::Isolate* pIsolate,
693                                     v8::Local<v8::Object> pObj) {
694   if (pObj.IsEmpty())
695     return v8::Local<v8::Value>();
696   return pObj->Clone();
697 }
698
699 v8::Local<v8::Value> FXJS_NewObject2(v8::Isolate* pIsolate,
700                                      v8::Local<v8::Array> pObj) {
701   if (pObj.IsEmpty())
702     return v8::Local<v8::Value>();
703   return pObj->Clone();
704 }
705
706 v8::Local<v8::Value> FXJS_NewString(v8::Isolate* pIsolate,
707                                     const wchar_t* string) {
708   return FXJS_WSToJSString(pIsolate, string);
709 }
710
711 v8::Local<v8::Value> FXJS_NewNull() {
712   return v8::Local<v8::Value>();
713 }
714
715 v8::Local<v8::Value> FXJS_NewDate(v8::Isolate* pIsolate, double d) {
716   return v8::Date::New(pIsolate->GetCurrentContext(), d).ToLocalChecked();
717 }
718
719 int FXJS_ToInt32(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) {
720   if (pValue.IsEmpty())
721     return 0;
722   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
723   return pValue->ToInt32(context).ToLocalChecked()->Value();
724 }
725
726 bool FXJS_ToBoolean(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) {
727   if (pValue.IsEmpty())
728     return false;
729   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
730   return pValue->ToBoolean(context).ToLocalChecked()->Value();
731 }
732
733 double FXJS_ToNumber(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) {
734   if (pValue.IsEmpty())
735     return 0.0;
736   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
737   return pValue->ToNumber(context).ToLocalChecked()->Value();
738 }
739
740 v8::Local<v8::Object> FXJS_ToObject(v8::Isolate* pIsolate,
741                                     v8::Local<v8::Value> pValue) {
742   if (pValue.IsEmpty())
743     return v8::Local<v8::Object>();
744   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
745   return pValue->ToObject(context).ToLocalChecked();
746 }
747
748 CFX_WideString FXJS_ToString(v8::Isolate* pIsolate,
749                              v8::Local<v8::Value> pValue) {
750   if (pValue.IsEmpty())
751     return L"";
752   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
753   v8::String::Utf8Value s(pValue->ToString(context).ToLocalChecked());
754   return CFX_WideString::FromUTF8(*s, s.length());
755 }
756
757 v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate,
758                                   v8::Local<v8::Value> pValue) {
759   if (pValue.IsEmpty())
760     return v8::Local<v8::Array>();
761   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
762   return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked());
763 }
764
765 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) {
766   pTo = pFrom;
767 }
768
769