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