Revert "Revert "Add type cast definitions for CPDF_Dictionary.""
[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     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 IJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate) {
353   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
354   return static_cast<IJS_Runtime*>(
355       context->GetAlignedPointerFromEmbedderData(kPerContextDataIndex));
356 }
357
358 int FXJS_Execute(v8::Isolate* pIsolate,
359                  IJS_Context* pJSContext,
360                  const wchar_t* script,
361                  FXJSErr* pError) {
362   v8::Isolate::Scope isolate_scope(pIsolate);
363   v8::TryCatch try_catch(pIsolate);
364   CFX_ByteString bsScript = CFX_WideString(script).UTF8Encode();
365   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
366   v8::Local<v8::Script> compiled_script;
367   if (!v8::Script::Compile(
368            context, v8::String::NewFromUtf8(
369                         pIsolate, bsScript.c_str(), v8::NewStringType::kNormal,
370                         bsScript.GetLength()).ToLocalChecked())
371            .ToLocal(&compiled_script)) {
372     v8::String::Utf8Value error(try_catch.Exception());
373     // TODO(tsepez): return error via pError->message.
374     return -1;
375   }
376
377   v8::Local<v8::Value> result;
378   if (!compiled_script->Run(context).ToLocal(&result)) {
379     v8::String::Utf8Value error(try_catch.Exception());
380     // TODO(tsepez): return error via pError->message.
381     return -1;
382   }
383   return 0;
384 }
385
386 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate,
387                                            IJS_Runtime* pIRuntime,
388                                            int nObjDefnID) {
389   v8::Isolate::Scope isolate_scope(pIsolate);
390   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
391   if (nObjDefnID == -1) {
392     v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate);
393     v8::Local<v8::Object> obj;
394     if (!objTempl->NewInstance(context).ToLocal(&obj))
395       return v8::Local<v8::Object>();
396     return obj;
397   }
398
399   FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate);
400   if (!pData)
401     return v8::Local<v8::Object>();
402
403   if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate))
404     return v8::Local<v8::Object>();
405
406   CFXJS_ObjDefinition* pObjDef =
407       CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID);
408   v8::Local<v8::Object> obj;
409   if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj))
410     return v8::Local<v8::Object>();
411
412   obj->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(nObjDefnID));
413   if (pObjDef->m_pConstructor)
414     pObjDef->m_pConstructor(pIRuntime, obj);
415
416   return obj;
417 }
418
419 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) {
420   v8::Isolate::Scope isolate_scope(pIsolate);
421   if (!FXJS_PerIsolateData::Get(pIsolate))
422     return v8::Local<v8::Object>();
423
424   // Return the global object.
425   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
426   return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked();
427 }
428
429 int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj) {
430   if (pObj.IsEmpty() || !pObj->InternalFieldCount())
431     return -1;
432   CFXJS_PrivateData* pPrivateData =
433       (CFXJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0);
434   if (pPrivateData)
435     return pPrivateData->ObjDefID;
436   return -1;
437 }
438
439 v8::Isolate* FXJS_GetRuntime(v8::Local<v8::Object> pObj) {
440   if (pObj.IsEmpty())
441     return NULL;
442   v8::Local<v8::Context> context = pObj->CreationContext();
443   if (context.IsEmpty())
444     return NULL;
445   return context->GetIsolate();
446 }
447
448 void FXJS_Error(v8::Isolate* pIsolate, const CFX_WideString& message) {
449   // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t
450   // wide-strings isn't handled by v8, so use UTF8 as a common
451   // intermediate format.
452   CFX_ByteString utf8_message = message.UTF8Encode();
453   pIsolate->ThrowException(
454       v8::String::NewFromUtf8(pIsolate, utf8_message.c_str(),
455                               v8::NewStringType::kNormal).ToLocalChecked());
456 }
457
458 const wchar_t* FXJS_GetTypeof(v8::Local<v8::Value> pObj) {
459   if (pObj.IsEmpty())
460     return NULL;
461   if (pObj->IsString())
462     return kFXJSValueNameString;
463   if (pObj->IsNumber())
464     return kFXJSValueNameNumber;
465   if (pObj->IsBoolean())
466     return kFXJSValueNameBoolean;
467   if (pObj->IsDate())
468     return kFXJSValueNameDate;
469   if (pObj->IsObject())
470     return kFXJSValueNameObject;
471   if (pObj->IsNull())
472     return kFXJSValueNameNull;
473   if (pObj->IsUndefined())
474     return kFXJSValueNameUndefined;
475   return NULL;
476 }
477
478 void FXJS_SetPrivate(v8::Isolate* pIsolate,
479                      v8::Local<v8::Object> pObj,
480                      void* p) {
481   if (pObj.IsEmpty() || !pObj->InternalFieldCount())
482     return;
483   CFXJS_PrivateData* pPrivateData =
484       (CFXJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0);
485   if (!pPrivateData)
486     return;
487   pPrivateData->pPrivate = p;
488 }
489
490 void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj) {
491   if (pObj.IsEmpty())
492     return nullptr;
493   CFXJS_PrivateData* pPrivateData = nullptr;
494   if (pObj->InternalFieldCount()) {
495     pPrivateData =
496         (CFXJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0);
497   } else {
498     // It could be a global proxy object.
499     v8::Local<v8::Value> v = pObj->GetPrototype();
500     v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
501     if (v->IsObject()) {
502       pPrivateData = (CFXJS_PrivateData*)v->ToObject(context)
503                          .ToLocalChecked()
504                          ->GetAlignedPointerFromInternalField(0);
505     }
506   }
507   return pPrivateData ? pPrivateData->pPrivate : nullptr;
508 }
509
510 void FXJS_FreePrivate(void* pPrivateData) {
511   delete (CFXJS_PrivateData*)pPrivateData;
512 }
513
514 void FXJS_FreePrivate(v8::Local<v8::Object> pObj) {
515   if (pObj.IsEmpty() || !pObj->InternalFieldCount())
516     return;
517   FXJS_FreePrivate(pObj->GetAlignedPointerFromInternalField(0));
518   pObj->SetAlignedPointerInInternalField(0, NULL);
519 }
520
521 v8::Local<v8::String> FXJS_WSToJSString(v8::Isolate* pIsolate,
522                                         const wchar_t* PropertyName,
523                                         int Len) {
524   CFX_WideString ws = CFX_WideString(PropertyName, Len);
525   CFX_ByteString bs = ws.UTF8Encode();
526   if (!pIsolate)
527     pIsolate = v8::Isolate::GetCurrent();
528   return v8::String::NewFromUtf8(pIsolate, bs.c_str(),
529                                  v8::NewStringType::kNormal).ToLocalChecked();
530 }
531
532 v8::Local<v8::Value> FXJS_GetObjectElement(v8::Isolate* pIsolate,
533                                            v8::Local<v8::Object> pObj,
534                                            const wchar_t* PropertyName) {
535   if (pObj.IsEmpty())
536     return v8::Local<v8::Value>();
537   v8::Local<v8::Value> val;
538   if (!pObj->Get(pIsolate->GetCurrentContext(),
539                  FXJS_WSToJSString(pIsolate, PropertyName)).ToLocal(&val))
540     return v8::Local<v8::Value>();
541   return val;
542 }
543
544 v8::Local<v8::Array> FXJS_GetObjectElementNames(v8::Isolate* pIsolate,
545                                                 v8::Local<v8::Object> pObj) {
546   if (pObj.IsEmpty())
547     return v8::Local<v8::Array>();
548   v8::Local<v8::Array> val;
549   if (!pObj->GetPropertyNames(pIsolate->GetCurrentContext()).ToLocal(&val))
550     return v8::Local<v8::Array>();
551   return val;
552 }
553
554 void FXJS_PutObjectString(v8::Isolate* pIsolate,
555                           v8::Local<v8::Object> pObj,
556                           const wchar_t* PropertyName,
557                           const wchar_t* sValue)  // VT_string
558 {
559   if (pObj.IsEmpty())
560     return;
561   pObj->Set(pIsolate->GetCurrentContext(),
562             FXJS_WSToJSString(pIsolate, PropertyName),
563             FXJS_WSToJSString(pIsolate, sValue)).FromJust();
564 }
565
566 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
567                           v8::Local<v8::Object> pObj,
568                           const wchar_t* PropertyName,
569                           int nValue) {
570   if (pObj.IsEmpty())
571     return;
572   pObj->Set(pIsolate->GetCurrentContext(),
573             FXJS_WSToJSString(pIsolate, PropertyName),
574             v8::Int32::New(pIsolate, nValue)).FromJust();
575 }
576
577 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
578                           v8::Local<v8::Object> pObj,
579                           const wchar_t* PropertyName,
580                           float fValue) {
581   if (pObj.IsEmpty())
582     return;
583   pObj->Set(pIsolate->GetCurrentContext(),
584             FXJS_WSToJSString(pIsolate, PropertyName),
585             v8::Number::New(pIsolate, (double)fValue)).FromJust();
586 }
587
588 void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
589                           v8::Local<v8::Object> pObj,
590                           const wchar_t* PropertyName,
591                           double dValue) {
592   if (pObj.IsEmpty())
593     return;
594   pObj->Set(pIsolate->GetCurrentContext(),
595             FXJS_WSToJSString(pIsolate, PropertyName),
596             v8::Number::New(pIsolate, (double)dValue)).FromJust();
597 }
598
599 void FXJS_PutObjectBoolean(v8::Isolate* pIsolate,
600                            v8::Local<v8::Object> pObj,
601                            const wchar_t* PropertyName,
602                            bool bValue) {
603   if (pObj.IsEmpty())
604     return;
605   pObj->Set(pIsolate->GetCurrentContext(),
606             FXJS_WSToJSString(pIsolate, PropertyName),
607             v8::Boolean::New(pIsolate, bValue)).FromJust();
608 }
609
610 void FXJS_PutObjectObject(v8::Isolate* pIsolate,
611                           v8::Local<v8::Object> pObj,
612                           const wchar_t* PropertyName,
613                           v8::Local<v8::Object> pPut) {
614   if (pObj.IsEmpty())
615     return;
616   pObj->Set(pIsolate->GetCurrentContext(),
617             FXJS_WSToJSString(pIsolate, PropertyName), pPut).FromJust();
618 }
619
620 void FXJS_PutObjectNull(v8::Isolate* pIsolate,
621                         v8::Local<v8::Object> pObj,
622                         const wchar_t* PropertyName) {
623   if (pObj.IsEmpty())
624     return;
625   pObj->Set(pIsolate->GetCurrentContext(),
626             FXJS_WSToJSString(pIsolate, PropertyName),
627             v8::Local<v8::Object>()).FromJust();
628 }
629
630 v8::Local<v8::Array> FXJS_NewArray(v8::Isolate* pIsolate) {
631   return v8::Array::New(pIsolate);
632 }
633
634 unsigned FXJS_PutArrayElement(v8::Isolate* pIsolate,
635                               v8::Local<v8::Array> pArray,
636                               unsigned index,
637                               v8::Local<v8::Value> pValue) {
638   if (pArray.IsEmpty())
639     return 0;
640   if (pArray->Set(pIsolate->GetCurrentContext(), index, pValue).IsNothing())
641     return 0;
642   return 1;
643 }
644
645 v8::Local<v8::Value> FXJS_GetArrayElement(v8::Isolate* pIsolate,
646                                           v8::Local<v8::Array> pArray,
647                                           unsigned index) {
648   if (pArray.IsEmpty())
649     return v8::Local<v8::Value>();
650   v8::Local<v8::Value> val;
651   if (!pArray->Get(pIsolate->GetCurrentContext(), index).ToLocal(&val))
652     return v8::Local<v8::Value>();
653   return val;
654 }
655
656 unsigned FXJS_GetArrayLength(v8::Local<v8::Array> pArray) {
657   if (pArray.IsEmpty())
658     return 0;
659   return pArray->Length();
660 }
661
662 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, int number) {
663   return v8::Int32::New(pIsolate, number);
664 }
665
666 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, double number) {
667   return v8::Number::New(pIsolate, number);
668 }
669
670 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, float number) {
671   return v8::Number::New(pIsolate, (float)number);
672 }
673
674 v8::Local<v8::Value> FXJS_NewBoolean(v8::Isolate* pIsolate, bool b) {
675   return v8::Boolean::New(pIsolate, b);
676 }
677
678 v8::Local<v8::Value> FXJS_NewObject(v8::Isolate* pIsolate,
679                                     v8::Local<v8::Object> pObj) {
680   if (pObj.IsEmpty())
681     return v8::Local<v8::Value>();
682   return pObj->Clone();
683 }
684
685 v8::Local<v8::Value> FXJS_NewObject2(v8::Isolate* pIsolate,
686                                      v8::Local<v8::Array> pObj) {
687   if (pObj.IsEmpty())
688     return v8::Local<v8::Value>();
689   return pObj->Clone();
690 }
691
692 v8::Local<v8::Value> FXJS_NewString(v8::Isolate* pIsolate,
693                                     const wchar_t* string) {
694   return FXJS_WSToJSString(pIsolate, string);
695 }
696
697 v8::Local<v8::Value> FXJS_NewNull() {
698   return v8::Local<v8::Value>();
699 }
700
701 v8::Local<v8::Value> FXJS_NewDate(v8::Isolate* pIsolate, double d) {
702   return v8::Date::New(pIsolate->GetCurrentContext(), d).ToLocalChecked();
703 }
704
705 int FXJS_ToInt32(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) {
706   if (pValue.IsEmpty())
707     return 0;
708   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
709   return pValue->ToInt32(context).ToLocalChecked()->Value();
710 }
711
712 bool FXJS_ToBoolean(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) {
713   if (pValue.IsEmpty())
714     return false;
715   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
716   return pValue->ToBoolean(context).ToLocalChecked()->Value();
717 }
718
719 double FXJS_ToNumber(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) {
720   if (pValue.IsEmpty())
721     return 0.0;
722   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
723   return pValue->ToNumber(context).ToLocalChecked()->Value();
724 }
725
726 v8::Local<v8::Object> FXJS_ToObject(v8::Isolate* pIsolate,
727                                     v8::Local<v8::Value> pValue) {
728   if (pValue.IsEmpty())
729     return v8::Local<v8::Object>();
730   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
731   return pValue->ToObject(context).ToLocalChecked();
732 }
733
734 CFX_WideString FXJS_ToString(v8::Isolate* pIsolate,
735                              v8::Local<v8::Value> pValue) {
736   if (pValue.IsEmpty())
737     return L"";
738   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
739   v8::String::Utf8Value s(pValue->ToString(context).ToLocalChecked());
740   return CFX_WideString::FromUTF8(*s, s.length());
741 }
742
743 v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate,
744                                   v8::Local<v8::Value> pValue) {
745   if (pValue.IsEmpty())
746     return v8::Local<v8::Array>();
747   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
748   return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked());
749 }
750
751 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) {
752   pTo = pFrom;
753 }
754
755