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