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