Remove several dead functions from fxjs_v8.{cpp,h}.
[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 void FXJS_SetPrivate(v8::Local<v8::Object> pObj, void* p) {
493   FXJS_SetPrivate(NULL, pObj, p);
494 }
495
496 void* FXJS_GetPrivate(v8::Local<v8::Object> pObj) {
497   return FXJS_GetPrivate(NULL, pObj);
498 }
499
500 void FXJS_SetPrivate(v8::Isolate* pIsolate,
501                      v8::Local<v8::Object> pObj,
502                      void* p) {
503   if (pObj.IsEmpty() || !pObj->InternalFieldCount())
504     return;
505   CFXJS_PrivateData* pPrivateData =
506       (CFXJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0);
507   if (!pPrivateData)
508     return;
509   pPrivateData->pPrivate = p;
510 }
511
512 void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj) {
513   if (pObj.IsEmpty())
514     return NULL;
515   CFXJS_PrivateData* pPrivateData = NULL;
516   if (pObj->InternalFieldCount())
517     pPrivateData =
518         (CFXJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0);
519   else {
520     // It could be a global proxy object.
521     v8::Local<v8::Value> v = pObj->GetPrototype();
522     v8::Isolate* isolate = (v8::Isolate*)pIsolate;
523     v8::Local<v8::Context> context = isolate->GetCurrentContext();
524     if (v->IsObject())
525       pPrivateData = (CFXJS_PrivateData*)v->ToObject(context)
526                          .ToLocalChecked()
527                          ->GetAlignedPointerFromInternalField(0);
528   }
529   if (!pPrivateData)
530     return NULL;
531   return pPrivateData->pPrivate;
532 }
533
534 void FXJS_FreePrivate(void* pPrivateData) {
535   delete (CFXJS_PrivateData*)pPrivateData;
536 }
537
538 void FXJS_FreePrivate(v8::Local<v8::Object> pObj) {
539   if (pObj.IsEmpty() || !pObj->InternalFieldCount())
540     return;
541   FXJS_FreePrivate(pObj->GetAlignedPointerFromInternalField(0));
542   pObj->SetAlignedPointerInInternalField(0, NULL);
543 }
544
545 v8::Local<v8::String> FXJS_WSToJSString(v8::Isolate* pIsolate,
546                                         const wchar_t* PropertyName,
547                                         int Len) {
548   CFX_WideString ws = CFX_WideString(PropertyName, Len);
549   CFX_ByteString bs = ws.UTF8Encode();
550   if (!pIsolate)
551     pIsolate = v8::Isolate::GetCurrent();
552   return v8::String::NewFromUtf8(pIsolate, bs.c_str(),
553                                  v8::NewStringType::kNormal).ToLocalChecked();
554 }
555
556 v8::Local<v8::Value> FXJS_GetObjectElement(v8::Isolate* pIsolate,
557                                            v8::Local<v8::Object> pObj,
558                                            const wchar_t* PropertyName) {
559   if (pObj.IsEmpty())
560     return v8::Local<v8::Value>();
561   v8::Local<v8::Value> val;
562   if (!pObj->Get(pIsolate->GetCurrentContext(),
563                  FXJS_WSToJSString(pIsolate, PropertyName)).ToLocal(&val))
564     return v8::Local<v8::Value>();
565   return val;
566 }
567
568 v8::Local<v8::Array> FXJS_GetObjectElementNames(v8::Isolate* pIsolate,
569                                                 v8::Local<v8::Object> pObj) {
570   if (pObj.IsEmpty())
571     return v8::Local<v8::Array>();
572   v8::Local<v8::Array> val;
573   if (!pObj->GetPropertyNames(pIsolate->GetCurrentContext()).ToLocal(&val))
574     return v8::Local<v8::Array>();
575   return val;
576 }
577
578 void FXJS_PutObjectString(v8::Isolate* pIsolate,
579                           v8::Local<v8::Object> pObj,
580                           const wchar_t* PropertyName,
581                           const wchar_t* sValue)  // VT_string
582 {
583   if (pObj.IsEmpty())
584     return;
585   pObj->Set(pIsolate->GetCurrentContext(),
586             FXJS_WSToJSString(pIsolate, PropertyName),
587             FXJS_WSToJSString(pIsolate, sValue)).FromJust();
588 }
589
590 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
591                           v8::Local<v8::Object> pObj,
592                           const wchar_t* PropertyName,
593                           int nValue) {
594   if (pObj.IsEmpty())
595     return;
596   pObj->Set(pIsolate->GetCurrentContext(),
597             FXJS_WSToJSString(pIsolate, PropertyName),
598             v8::Int32::New(pIsolate, nValue)).FromJust();
599 }
600
601 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
602                           v8::Local<v8::Object> pObj,
603                           const wchar_t* PropertyName,
604                           float fValue) {
605   if (pObj.IsEmpty())
606     return;
607   pObj->Set(pIsolate->GetCurrentContext(),
608             FXJS_WSToJSString(pIsolate, PropertyName),
609             v8::Number::New(pIsolate, (double)fValue)).FromJust();
610 }
611
612 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
613                           v8::Local<v8::Object> pObj,
614                           const wchar_t* PropertyName,
615                           double dValue) {
616   if (pObj.IsEmpty())
617     return;
618   pObj->Set(pIsolate->GetCurrentContext(),
619             FXJS_WSToJSString(pIsolate, PropertyName),
620             v8::Number::New(pIsolate, (double)dValue)).FromJust();
621 }
622
623 void FXJS_PutObjectBoolean(v8::Isolate* pIsolate,
624                            v8::Local<v8::Object> pObj,
625                            const wchar_t* PropertyName,
626                            bool bValue) {
627   if (pObj.IsEmpty())
628     return;
629   pObj->Set(pIsolate->GetCurrentContext(),
630             FXJS_WSToJSString(pIsolate, PropertyName),
631             v8::Boolean::New(pIsolate, bValue)).FromJust();
632 }
633
634 void FXJS_PutObjectObject(v8::Isolate* pIsolate,
635                           v8::Local<v8::Object> pObj,
636                           const wchar_t* PropertyName,
637                           v8::Local<v8::Object> pPut) {
638   if (pObj.IsEmpty())
639     return;
640   pObj->Set(pIsolate->GetCurrentContext(),
641             FXJS_WSToJSString(pIsolate, PropertyName), pPut).FromJust();
642 }
643
644 void FXJS_PutObjectNull(v8::Isolate* pIsolate,
645                         v8::Local<v8::Object> pObj,
646                         const wchar_t* PropertyName) {
647   if (pObj.IsEmpty())
648     return;
649   pObj->Set(pIsolate->GetCurrentContext(),
650             FXJS_WSToJSString(pIsolate, PropertyName),
651             v8::Local<v8::Object>()).FromJust();
652 }
653
654 v8::Local<v8::Array> FXJS_NewArray(v8::Isolate* pIsolate) {
655   return v8::Array::New(pIsolate);
656 }
657
658 unsigned FXJS_PutArrayElement(v8::Isolate* pIsolate,
659                               v8::Local<v8::Array> pArray,
660                               unsigned index,
661                               v8::Local<v8::Value> pValue) {
662   if (pArray.IsEmpty())
663     return 0;
664   if (pArray->Set(pIsolate->GetCurrentContext(), index, pValue).IsNothing())
665     return 0;
666   return 1;
667 }
668
669 v8::Local<v8::Value> FXJS_GetArrayElement(v8::Isolate* pIsolate,
670                                           v8::Local<v8::Array> pArray,
671                                           unsigned index) {
672   if (pArray.IsEmpty())
673     return v8::Local<v8::Value>();
674   v8::Local<v8::Value> val;
675   if (!pArray->Get(pIsolate->GetCurrentContext(), index).ToLocal(&val))
676     return v8::Local<v8::Value>();
677   return val;
678 }
679
680 unsigned FXJS_GetArrayLength(v8::Local<v8::Array> pArray) {
681   if (pArray.IsEmpty())
682     return 0;
683   return pArray->Length();
684 }
685
686 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, int number) {
687   return v8::Int32::New(pIsolate, number);
688 }
689
690 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, double number) {
691   return v8::Number::New(pIsolate, number);
692 }
693
694 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, float number) {
695   return v8::Number::New(pIsolate, (float)number);
696 }
697
698 v8::Local<v8::Value> FXJS_NewBoolean(v8::Isolate* pIsolate, bool b) {
699   return v8::Boolean::New(pIsolate, b);
700 }
701
702 v8::Local<v8::Value> FXJS_NewObject(v8::Isolate* pIsolate,
703                                     v8::Local<v8::Object> pObj) {
704   if (pObj.IsEmpty())
705     return v8::Local<v8::Value>();
706   return pObj->Clone();
707 }
708
709 v8::Local<v8::Value> FXJS_NewObject2(v8::Isolate* pIsolate,
710                                      v8::Local<v8::Array> pObj) {
711   if (pObj.IsEmpty())
712     return v8::Local<v8::Value>();
713   return pObj->Clone();
714 }
715
716 v8::Local<v8::Value> FXJS_NewString(v8::Isolate* pIsolate,
717                                     const wchar_t* string) {
718   return FXJS_WSToJSString(pIsolate, string);
719 }
720
721 v8::Local<v8::Value> FXJS_NewNull() {
722   return v8::Local<v8::Value>();
723 }
724
725 v8::Local<v8::Value> FXJS_NewDate(v8::Isolate* pIsolate, double d) {
726   return v8::Date::New(pIsolate->GetCurrentContext(), d).ToLocalChecked();
727 }
728
729 int FXJS_ToInt32(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) {
730   if (pValue.IsEmpty())
731     return 0;
732   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
733   return pValue->ToInt32(context).ToLocalChecked()->Value();
734 }
735
736 bool FXJS_ToBoolean(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) {
737   if (pValue.IsEmpty())
738     return false;
739   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
740   return pValue->ToBoolean(context).ToLocalChecked()->Value();
741 }
742
743 double FXJS_ToNumber(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) {
744   if (pValue.IsEmpty())
745     return 0.0;
746   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
747   return pValue->ToNumber(context).ToLocalChecked()->Value();
748 }
749
750 v8::Local<v8::Object> FXJS_ToObject(v8::Isolate* pIsolate,
751                                     v8::Local<v8::Value> pValue) {
752   if (pValue.IsEmpty())
753     return v8::Local<v8::Object>();
754   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
755   return pValue->ToObject(context).ToLocalChecked();
756 }
757
758 CFX_WideString FXJS_ToString(v8::Isolate* pIsolate,
759                              v8::Local<v8::Value> pValue) {
760   if (pValue.IsEmpty())
761     return L"";
762   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
763   v8::String::Utf8Value s(pValue->ToString(context).ToLocalChecked());
764   return CFX_WideString::FromUTF8(*s, s.length());
765 }
766
767 v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate,
768                                   v8::Local<v8::Value> pValue) {
769   if (pValue.IsEmpty())
770     return v8::Local<v8::Array>();
771   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
772   return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked());
773 }
774
775 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) {
776   pTo = pFrom;
777 }
778
779