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