Wean CJS_Value off of v8::Isolate.
[pdfium.git] / fpdfsdk / src / javascript / global.cpp
index aa3bfa0..c8510d2 100644 (file)
@@ -4,16 +4,17 @@
 
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
+#include "global.h"
+
+#include "../../../core/include/fxcrt/fx_ext.h"
 #include "../../include/javascript/IJavaScript.h"
-#include "../../include/javascript/JS_Context.h"
-#include "../../include/javascript/JS_Define.h"
-#include "../../include/javascript/JS_EventHandler.h"
-#include "../../include/javascript/JS_GlobalData.h"
-#include "../../include/javascript/JS_Object.h"
-#include "../../include/javascript/JS_Value.h"
-#include "../../include/javascript/JavaScript.h"
-#include "../../include/javascript/global.h"
-#include "../../include/javascript/resource.h"
+#include "JS_Context.h"
+#include "JS_Define.h"
+#include "JS_EventHandler.h"
+#include "JS_GlobalData.h"
+#include "JS_Object.h"
+#include "JS_Value.h"
+#include "resource.h"
 
 /* ---------------------------- global ---------------------------- */
 
@@ -38,21 +39,24 @@ struct CHash<ACC, N, Ns...> {
   static const unsigned value = CHash<CHash<ACC, N>::value, Ns...>::value;
 };
 
-extern const unsigned int JSCONST_nStringHash =
+const unsigned int JSCONST_nStringHash =
     CHash<'s', 't', 'r', 'i', 'n', 'g'>::value;
-extern const unsigned int JSCONST_nNumberHash =
+const unsigned int JSCONST_nNumberHash =
     CHash<'n', 'u', 'm', 'b', 'e', 'r'>::value;
-extern const unsigned int JSCONST_nBoolHash =
+const unsigned int JSCONST_nBoolHash =
     CHash<'b', 'o', 'o', 'l', 'e', 'a', 'n'>::value;
-extern const unsigned int JSCONST_nDateHash = CHash<'d', 'a', 't', 'e'>::value;
-extern const unsigned int JSCONST_nObjectHash =
+const unsigned int JSCONST_nDateHash = CHash<'d', 'a', 't', 'e'>::value;
+const unsigned int JSCONST_nObjectHash =
     CHash<'o', 'b', 'j', 'e', 'c', 't'>::value;
-extern const unsigned int JSCONST_nFXobjHash =
-    CHash<'f', 'x', 'o', 'b', 'j'>::value;
-extern const unsigned int JSCONST_nNullHash = CHash<'n', 'u', 'l', 'l'>::value;
-extern const unsigned int JSCONST_nUndefHash =
+const unsigned int JSCONST_nFXobjHash = CHash<'f', 'x', 'o', 'b', 'j'>::value;
+const unsigned int JSCONST_nNullHash = CHash<'n', 'u', 'l', 'l'>::value;
+const unsigned int JSCONST_nUndefHash =
     CHash<'u', 'n', 'd', 'e', 'f', 'i', 'n', 'e', 'd'>::value;
 
+static unsigned JS_CalcHash(const wchar_t* main) {
+  return (unsigned)FX_HashCode_String_GetW(main, FXSYS_wcslen(main));
+}
+
 #ifdef _DEBUG
 class HashVerify {
  public:
@@ -60,22 +64,14 @@ class HashVerify {
 } g_hashVerify;
 
 HashVerify::HashVerify() {
-  ASSERT(JSCONST_nStringHash ==
-         JS_CalcHash(VALUE_NAME_STRING, wcslen(VALUE_NAME_STRING)));
-  ASSERT(JSCONST_nNumberHash ==
-         JS_CalcHash(VALUE_NAME_NUMBER, wcslen(VALUE_NAME_NUMBER)));
-  ASSERT(JSCONST_nBoolHash ==
-         JS_CalcHash(VALUE_NAME_BOOLEAN, wcslen(VALUE_NAME_BOOLEAN)));
-  ASSERT(JSCONST_nDateHash ==
-         JS_CalcHash(VALUE_NAME_DATE, wcslen(VALUE_NAME_DATE)));
-  ASSERT(JSCONST_nObjectHash ==
-         JS_CalcHash(VALUE_NAME_OBJECT, wcslen(VALUE_NAME_OBJECT)));
-  ASSERT(JSCONST_nFXobjHash ==
-         JS_CalcHash(VALUE_NAME_FXOBJ, wcslen(VALUE_NAME_FXOBJ)));
-  ASSERT(JSCONST_nNullHash ==
-         JS_CalcHash(VALUE_NAME_NULL, wcslen(VALUE_NAME_NULL)));
-  ASSERT(JSCONST_nUndefHash ==
-         JS_CalcHash(VALUE_NAME_UNDEFINED, wcslen(VALUE_NAME_UNDEFINED)));
+  ASSERT(JSCONST_nStringHash == JS_CalcHash(kFXJSValueNameString));
+  ASSERT(JSCONST_nNumberHash == JS_CalcHash(kFXJSValueNameNumber));
+  ASSERT(JSCONST_nBoolHash == JS_CalcHash(kFXJSValueNameBoolean));
+  ASSERT(JSCONST_nDateHash == JS_CalcHash(kFXJSValueNameDate));
+  ASSERT(JSCONST_nObjectHash == JS_CalcHash(kFXJSValueNameObject));
+  ASSERT(JSCONST_nFXobjHash == JS_CalcHash(kFXJSValueNameFxobj));
+  ASSERT(JSCONST_nNullHash == JS_CalcHash(kFXJSValueNameNull));
+  ASSERT(JSCONST_nUndefHash == JS_CalcHash(kFXJSValueNameUndefined));
 }
 #endif
 
@@ -91,7 +87,7 @@ END_JS_STATIC_METHOD()
 
 IMPLEMENT_SPECIAL_JS_CLASS(CJS_Global, JSGlobalAlternate, global);
 
-FX_BOOL CJS_Global::InitInstance(IFXJS_Context* cc) {
+FX_BOOL CJS_Global::InitInstance(IJS_Context* cc) {
   CJS_Context* pContext = (CJS_Context*)cc;
   ASSERT(pContext != NULL);
 
@@ -109,12 +105,12 @@ JSGlobalAlternate::JSGlobalAlternate(CJS_Object* pJSObject)
 
 JSGlobalAlternate::~JSGlobalAlternate() {
   DestroyGlobalPersisitentVariables();
-  m_pApp->GetRuntimeFactory()->ReleaseGlobalData();
+  m_pGlobalData->Release();
 }
 
 void JSGlobalAlternate::Initial(CPDFDoc_Environment* pApp) {
   m_pApp = pApp;
-  m_pGlobalData = pApp->GetRuntimeFactory()->NewGlobalData(pApp);
+  m_pGlobalData = CJS_GlobalData::GetRetainedInstance(pApp);
   UpdateGlobalPersistentVariables();
 }
 
@@ -122,56 +118,53 @@ FX_BOOL JSGlobalAlternate::QueryProperty(const FX_WCHAR* propname) {
   return CFX_WideString(propname) != L"setPersistent";
 }
 
-FX_BOOL JSGlobalAlternate::DelProperty(IFXJS_Context* cc,
+FX_BOOL JSGlobalAlternate::DelProperty(IJS_Context* cc,
                                        const FX_WCHAR* propname,
                                        CFX_WideString& sError) {
-  JSGlobalData* pData = NULL;
-  CFX_ByteString sPropName = CFX_ByteString::FromUnicode(propname);
-
-  if (m_mapGlobal.Lookup(sPropName, (void*&)pData)) {
-    pData->bDeleted = TRUE;
-    return TRUE;
-  }
+  auto it = m_mapGlobal.find(CFX_ByteString::FromUnicode(propname));
+  if (it == m_mapGlobal.end())
+    return FALSE;
 
-  return FALSE;
+  it->second->bDeleted = TRUE;
+  return TRUE;
 }
 
-FX_BOOL JSGlobalAlternate::DoProperty(IFXJS_Context* cc,
+FX_BOOL JSGlobalAlternate::DoProperty(IJS_Context* cc,
                                       const FX_WCHAR* propname,
                                       CJS_PropValue& vp,
                                       CFX_WideString& sError) {
   if (vp.IsSetting()) {
     CFX_ByteString sPropName = CFX_ByteString::FromUnicode(propname);
     switch (vp.GetType()) {
-      case VT_number: {
+      case CJS_Value::VT_number: {
         double dData;
         vp >> dData;
         return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_NUMBER, dData,
                                   false, "", v8::Local<v8::Object>(), FALSE);
       }
-      case VT_boolean: {
+      case CJS_Value::VT_boolean: {
         bool bData;
         vp >> bData;
         return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_BOOLEAN, 0,
                                   bData, "", v8::Local<v8::Object>(), FALSE);
       }
-      case VT_string: {
+      case CJS_Value::VT_string: {
         CFX_ByteString sData;
         vp >> sData;
         return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_STRING, 0,
                                   false, sData, v8::Local<v8::Object>(), FALSE);
       }
-      case VT_object: {
-        JSObject pData;
+      case CJS_Value::VT_object: {
+        v8::Local<v8::Object> pData;
         vp >> pData;
         return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_OBJECT, 0,
                                   false, "", pData, FALSE);
       }
-      case VT_null: {
+      case CJS_Value::VT_null: {
         return SetGlobalVariables(sPropName, JS_GLOBALDATA_TYPE_NULL, 0, false,
                                   "", v8::Local<v8::Object>(), FALSE);
       }
-      case VT_undefined: {
+      case CJS_Value::VT_undefined: {
         DelProperty(cc, propname, sError);
         return TRUE;
       }
@@ -179,19 +172,16 @@ FX_BOOL JSGlobalAlternate::DoProperty(IFXJS_Context* cc,
         break;
     }
   } else {
-    void* pVoid = nullptr;
-    if (!m_mapGlobal.Lookup(CFX_ByteString::FromUnicode(propname), pVoid)) {
+    auto it = m_mapGlobal.find(CFX_ByteString::FromUnicode(propname));
+    if (it == m_mapGlobal.end()) {
       vp.SetNull();
       return TRUE;
     }
-    if (!pVoid) {
+    JSGlobalData* pData = it->second;
+    if (pData->bDeleted) {
       vp.SetNull();
       return TRUE;
     }
-    JSGlobalData* pData = (JSGlobalData*)pVoid;
-    if (pData->bDeleted)
-      return TRUE;
-
     switch (pData->nType) {
       case JS_GLOBALDATA_TYPE_NUMBER:
         vp << pData->dData;
@@ -203,8 +193,8 @@ FX_BOOL JSGlobalAlternate::DoProperty(IFXJS_Context* cc,
         vp << pData->sData;
         return TRUE;
       case JS_GLOBALDATA_TYPE_OBJECT: {
-        v8::Local<v8::Object> obj =
-            v8::Local<v8::Object>::New(vp.GetIsolate(), pData->pData);
+        v8::Local<v8::Object> obj = v8::Local<v8::Object>::New(
+            vp.GetJSRuntime()->GetIsolate(), pData->pData);
         vp << obj;
         return TRUE;
       }
@@ -218,7 +208,7 @@ FX_BOOL JSGlobalAlternate::DoProperty(IFXJS_Context* cc,
   return FALSE;
 }
 
-FX_BOOL JSGlobalAlternate::setPersistent(IFXJS_Context* cc,
+FX_BOOL JSGlobalAlternate::setPersistent(IJS_Context* cc,
                                          const CJS_Parameters& params,
                                          CJS_Value& vRet,
                                          CFX_WideString& sError) {
@@ -228,11 +218,10 @@ FX_BOOL JSGlobalAlternate::setPersistent(IFXJS_Context* cc,
     return FALSE;
   }
 
-  CFX_ByteString sName = params[0].ToCFXByteString();
-
-  JSGlobalData* pData = NULL;
-  if (m_mapGlobal.Lookup(sName, (void*&)pData)) {
-    if (pData && !pData->bDeleted) {
+  auto it = m_mapGlobal.find(params[0].ToCFXByteString());
+  if (it != m_mapGlobal.end()) {
+    JSGlobalData* pData = it->second;
+    if (!pData->bDeleted) {
       pData->bPersistent = params[1].ToBool();
       return TRUE;
     }
@@ -254,147 +243,136 @@ void JSGlobalAlternate::UpdateGlobalPersistentVariables() {
         SetGlobalVariables(pData->data.sKey, JS_GLOBALDATA_TYPE_NUMBER,
                            pData->data.dData, false, "",
                            v8::Local<v8::Object>(), pData->bPersistent == 1);
-        JS_PutObjectNumber(NULL, (JSFXObject)(*m_pJSObject),
-                           pData->data.sKey.UTF8Decode().c_str(),
-                           pData->data.dData);
+        FXJS_PutObjectNumber(NULL, m_pJSObject->ToV8Object(),
+                             pData->data.sKey.UTF8Decode().c_str(),
+                             pData->data.dData);
         break;
       case JS_GLOBALDATA_TYPE_BOOLEAN:
         SetGlobalVariables(pData->data.sKey, JS_GLOBALDATA_TYPE_BOOLEAN, 0,
                            (bool)(pData->data.bData == 1), "",
                            v8::Local<v8::Object>(), pData->bPersistent == 1);
-        JS_PutObjectBoolean(NULL, (JSFXObject)(*m_pJSObject),
-                            pData->data.sKey.UTF8Decode().c_str(),
-                            (bool)(pData->data.bData == 1));
+        FXJS_PutObjectBoolean(NULL, m_pJSObject->ToV8Object(),
+                              pData->data.sKey.UTF8Decode().c_str(),
+                              (bool)(pData->data.bData == 1));
         break;
       case JS_GLOBALDATA_TYPE_STRING:
         SetGlobalVariables(pData->data.sKey, JS_GLOBALDATA_TYPE_STRING, 0,
                            false, pData->data.sData, v8::Local<v8::Object>(),
                            pData->bPersistent == 1);
-        JS_PutObjectString(NULL, (JSFXObject)(*m_pJSObject),
-                           pData->data.sKey.UTF8Decode().c_str(),
-                           pData->data.sData.UTF8Decode().c_str());
+        FXJS_PutObjectString(NULL, m_pJSObject->ToV8Object(),
+                             pData->data.sKey.UTF8Decode().c_str(),
+                             pData->data.sData.UTF8Decode().c_str());
         break;
       case JS_GLOBALDATA_TYPE_OBJECT: {
-        IJS_Runtime* pRuntime = JS_GetRuntime((JSFXObject)(*m_pJSObject));
-        v8::Local<v8::Object> pObj = JS_NewFxDynamicObj(pRuntime, NULL, -1);
+        v8::Isolate* pRuntime = FXJS_GetRuntime(m_pJSObject->ToV8Object());
+        v8::Local<v8::Object> pObj = FXJS_NewFxDynamicObj(pRuntime, NULL, -1);
 
         PutObjectProperty(pObj, &pData->data);
 
         SetGlobalVariables(pData->data.sKey, JS_GLOBALDATA_TYPE_OBJECT, 0,
-                           false, "", (JSObject)pObj, pData->bPersistent == 1);
-        JS_PutObjectObject(NULL, (JSFXObject)(*m_pJSObject),
-                           pData->data.sKey.UTF8Decode().c_str(),
-                           (JSObject)pObj);
+                           false, "", pObj, pData->bPersistent == 1);
+        FXJS_PutObjectObject(NULL, m_pJSObject->ToV8Object(),
+                             pData->data.sKey.UTF8Decode().c_str(), pObj);
+
       } break;
       case JS_GLOBALDATA_TYPE_NULL:
         SetGlobalVariables(pData->data.sKey, JS_GLOBALDATA_TYPE_NULL, 0, false,
                            "", v8::Local<v8::Object>(),
                            pData->bPersistent == 1);
-        JS_PutObjectNull(NULL, (JSFXObject)(*m_pJSObject),
-                         pData->data.sKey.UTF8Decode().c_str());
+        FXJS_PutObjectNull(NULL, m_pJSObject->ToV8Object(),
+                           pData->data.sKey.UTF8Decode().c_str());
         break;
     }
   }
 }
 
-void JSGlobalAlternate::CommitGlobalPersisitentVariables() {
-  ASSERT(m_pGlobalData != NULL);
-
-  FX_POSITION pos = m_mapGlobal.GetStartPosition();
-  while (pos) {
-    CFX_ByteString name;
-    JSGlobalData* pData = NULL;
-    m_mapGlobal.GetNextAssoc(pos, name, (void*&)pData);
-
-    if (pData) {
-      if (pData->bDeleted) {
-        m_pGlobalData->DeleteGlobalVariable(name);
-      } else {
-        switch (pData->nType) {
-          case JS_GLOBALDATA_TYPE_NUMBER:
-            m_pGlobalData->SetGlobalVariableNumber(name, pData->dData);
-            m_pGlobalData->SetGlobalVariablePersistent(name,
-                                                       pData->bPersistent);
-            break;
-          case JS_GLOBALDATA_TYPE_BOOLEAN:
-            m_pGlobalData->SetGlobalVariableBoolean(name, pData->bData);
-            m_pGlobalData->SetGlobalVariablePersistent(name,
-                                                       pData->bPersistent);
-            break;
-          case JS_GLOBALDATA_TYPE_STRING:
-            m_pGlobalData->SetGlobalVariableString(name, pData->sData);
-            m_pGlobalData->SetGlobalVariablePersistent(name,
-                                                       pData->bPersistent);
-            break;
-          case JS_GLOBALDATA_TYPE_OBJECT:
-            // if (pData->pData)
-            {
-              CJS_GlobalVariableArray array;
-              v8::Local<v8::Object> obj = v8::Local<v8::Object>::New(
-                  GetJSObject()->GetIsolate(), pData->pData);
-              ObjectToArray(obj, array);
-              m_pGlobalData->SetGlobalVariableObject(name, array);
-              m_pGlobalData->SetGlobalVariablePersistent(name,
-                                                         pData->bPersistent);
-            }
-            break;
-          case JS_GLOBALDATA_TYPE_NULL:
-            m_pGlobalData->SetGlobalVariableNull(name);
+void JSGlobalAlternate::CommitGlobalPersisitentVariables(IJS_Context* cc) {
+  for (auto it = m_mapGlobal.begin(); it != m_mapGlobal.end(); ++it) {
+    CFX_ByteString name = it->first;
+    JSGlobalData* pData = it->second;
+    if (pData->bDeleted) {
+      m_pGlobalData->DeleteGlobalVariable(name);
+    } else {
+      switch (pData->nType) {
+        case JS_GLOBALDATA_TYPE_NUMBER:
+          m_pGlobalData->SetGlobalVariableNumber(name, pData->dData);
+          m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent);
+          break;
+        case JS_GLOBALDATA_TYPE_BOOLEAN:
+          m_pGlobalData->SetGlobalVariableBoolean(name, pData->bData);
+          m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent);
+          break;
+        case JS_GLOBALDATA_TYPE_STRING:
+          m_pGlobalData->SetGlobalVariableString(name, pData->sData);
+          m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent);
+          break;
+        case JS_GLOBALDATA_TYPE_OBJECT:
+          {
+            CJS_GlobalVariableArray array;
+            v8::Local<v8::Object> obj = v8::Local<v8::Object>::New(
+                GetJSObject()->GetIsolate(), pData->pData);
+            ObjectToArray(cc, obj, array);
+            m_pGlobalData->SetGlobalVariableObject(name, array);
             m_pGlobalData->SetGlobalVariablePersistent(name,
                                                        pData->bPersistent);
-            break;
-        }
+          }
+          break;
+        case JS_GLOBALDATA_TYPE_NULL:
+          m_pGlobalData->SetGlobalVariableNull(name);
+          m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent);
+          break;
       }
     }
   }
 }
 
-void JSGlobalAlternate::ObjectToArray(v8::Local<v8::Object> pObj,
+void JSGlobalAlternate::ObjectToArray(IJS_Context* cc,
+                                      v8::Local<v8::Object> pObj,
                                       CJS_GlobalVariableArray& array) {
   v8::Local<v8::Context> context = pObj->CreationContext();
   v8::Isolate* isolate = context->GetIsolate();
-  v8::Local<v8::Array> pKeyList = JS_GetObjectElementNames(isolate, pObj);
-  int nObjElements = pKeyList->Length();
+  CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
 
+  v8::Local<v8::Array> pKeyList = FXJS_GetObjectElementNames(isolate, pObj);
+  int nObjElements = pKeyList->Length();
   for (int i = 0; i < nObjElements; i++) {
     CFX_WideString ws =
-        JS_ToString(isolate, JS_GetArrayElement(isolate, pKeyList, i));
+        FXJS_ToString(isolate, FXJS_GetArrayElement(isolate, pKeyList, i));
     CFX_ByteString sKey = ws.UTF8Encode();
 
-    v8::Local<v8::Value> v = JS_GetObjectElement(isolate, pObj, ws.c_str());
-    FXJSVALUETYPE vt = GET_VALUE_TYPE(v);
-    switch (vt) {
-      case VT_number: {
+    v8::Local<v8::Value> v = FXJS_GetObjectElement(isolate, pObj, ws.c_str());
+    switch (GET_VALUE_TYPE(v)) {
+      case CJS_Value::VT_number: {
         CJS_KeyValue* pObjElement = new CJS_KeyValue;
         pObjElement->nType = JS_GLOBALDATA_TYPE_NUMBER;
         pObjElement->sKey = sKey;
-        pObjElement->dData = JS_ToNumber(isolate, v);
+        pObjElement->dData = FXJS_ToNumber(isolate, v);
         array.Add(pObjElement);
       } break;
-      case VT_boolean: {
+      case CJS_Value::VT_boolean: {
         CJS_KeyValue* pObjElement = new CJS_KeyValue;
         pObjElement->nType = JS_GLOBALDATA_TYPE_BOOLEAN;
         pObjElement->sKey = sKey;
-        pObjElement->dData = JS_ToBoolean(isolate, v);
+        pObjElement->dData = FXJS_ToBoolean(isolate, v);
         array.Add(pObjElement);
       } break;
-      case VT_string: {
+      case CJS_Value::VT_string: {
         CFX_ByteString sValue =
-            CJS_Value(isolate, v, VT_string).ToCFXByteString();
+            CJS_Value(pRuntime, v, CJS_Value::VT_string).ToCFXByteString();
         CJS_KeyValue* pObjElement = new CJS_KeyValue;
         pObjElement->nType = JS_GLOBALDATA_TYPE_STRING;
         pObjElement->sKey = sKey;
         pObjElement->sData = sValue;
         array.Add(pObjElement);
       } break;
-      case VT_object: {
+      case CJS_Value::VT_object: {
         CJS_KeyValue* pObjElement = new CJS_KeyValue;
         pObjElement->nType = JS_GLOBALDATA_TYPE_OBJECT;
         pObjElement->sKey = sKey;
-        ObjectToArray(JS_ToObject(isolate, v), pObjElement->objData);
+        ObjectToArray(cc, FXJS_ToObject(isolate, v), pObjElement->objData);
         array.Add(pObjElement);
       } break;
-      case VT_null: {
+      case CJS_Value::VT_null: {
         CJS_KeyValue* pObjElement = new CJS_KeyValue;
         pObjElement->nType = JS_GLOBALDATA_TYPE_NULL;
         pObjElement->sKey = sKey;
@@ -416,46 +394,37 @@ void JSGlobalAlternate::PutObjectProperty(v8::Local<v8::Object> pObj,
 
     switch (pObjData->nType) {
       case JS_GLOBALDATA_TYPE_NUMBER:
-        JS_PutObjectNumber(NULL, (JSObject)pObj,
-                           pObjData->sKey.UTF8Decode().c_str(),
-                           pObjData->dData);
+        FXJS_PutObjectNumber(NULL, pObj, pObjData->sKey.UTF8Decode().c_str(),
+                             pObjData->dData);
         break;
       case JS_GLOBALDATA_TYPE_BOOLEAN:
-        JS_PutObjectBoolean(NULL, (JSObject)pObj,
-                            pObjData->sKey.UTF8Decode().c_str(),
-                            (bool)(pObjData->bData == 1));
+        FXJS_PutObjectBoolean(NULL, pObj, pObjData->sKey.UTF8Decode().c_str(),
+                              pObjData->bData == 1);
         break;
       case JS_GLOBALDATA_TYPE_STRING:
-        JS_PutObjectString(NULL, (JSObject)pObj,
-                           pObjData->sKey.UTF8Decode().c_str(),
-                           pObjData->sData.UTF8Decode().c_str());
+        FXJS_PutObjectString(NULL, pObj, pObjData->sKey.UTF8Decode().c_str(),
+                             pObjData->sData.UTF8Decode().c_str());
         break;
       case JS_GLOBALDATA_TYPE_OBJECT: {
-        IJS_Runtime* pRuntime = JS_GetRuntime((JSFXObject)(*m_pJSObject));
-        v8::Local<v8::Object> pNewObj = JS_NewFxDynamicObj(pRuntime, NULL, -1);
+        v8::Isolate* pRuntime = FXJS_GetRuntime(m_pJSObject->ToV8Object());
+        v8::Local<v8::Object> pNewObj =
+            FXJS_NewFxDynamicObj(pRuntime, NULL, -1);
         PutObjectProperty(pNewObj, pObjData);
-        JS_PutObjectObject(NULL, (JSObject)pObj,
-                           pObjData->sKey.UTF8Decode().c_str(),
-                           (JSObject)pNewObj);
+        FXJS_PutObjectObject(NULL, pObj, pObjData->sKey.UTF8Decode().c_str(),
+                             pNewObj);
       } break;
       case JS_GLOBALDATA_TYPE_NULL:
-        JS_PutObjectNull(NULL, (JSObject)pObj,
-                         pObjData->sKey.UTF8Decode().c_str());
+        FXJS_PutObjectNull(NULL, pObj, pObjData->sKey.UTF8Decode().c_str());
         break;
     }
   }
 }
 
 void JSGlobalAlternate::DestroyGlobalPersisitentVariables() {
-  FX_POSITION pos = m_mapGlobal.GetStartPosition();
-  while (pos) {
-    CFX_ByteString name;
-    JSGlobalData* pData = NULL;
-    m_mapGlobal.GetNextAssoc(pos, name, (void*&)pData);
-    delete pData;
+  for (const auto& pair : m_mapGlobal) {
+    delete pair.second;
   }
-
-  m_mapGlobal.RemoveAll();
+  m_mapGlobal.clear();
 }
 
 FX_BOOL JSGlobalAlternate::SetGlobalVariables(const FX_CHAR* propname,
@@ -463,15 +432,14 @@ FX_BOOL JSGlobalAlternate::SetGlobalVariables(const FX_CHAR* propname,
                                               double dData,
                                               bool bData,
                                               const CFX_ByteString& sData,
-                                              JSObject pData,
+                                              v8::Local<v8::Object> pData,
                                               bool bDefaultPersistent) {
-  if (propname == NULL)
+  if (!propname)
     return FALSE;
 
-  JSGlobalData* pTemp = NULL;
-  m_mapGlobal.Lookup(propname, (void*&)pTemp);
-
-  if (pTemp) {
+  auto it = m_mapGlobal.find(propname);
+  if (it != m_mapGlobal.end()) {
+    JSGlobalData* pTemp = it->second;
     if (pTemp->bDeleted || pTemp->nType != nType) {
       pTemp->dData = 0;
       pTemp->bData = 0;
@@ -480,7 +448,6 @@ FX_BOOL JSGlobalAlternate::SetGlobalVariables(const FX_CHAR* propname,
     }
 
     pTemp->bDeleted = FALSE;
-
     switch (nType) {
       case JS_GLOBALDATA_TYPE_NUMBER: {
         pTemp->dData = dData;
@@ -492,14 +459,13 @@ FX_BOOL JSGlobalAlternate::SetGlobalVariables(const FX_CHAR* propname,
         pTemp->sData = sData;
       } break;
       case JS_GLOBALDATA_TYPE_OBJECT: {
-        pTemp->pData.Reset(JS_GetRuntime(pData), pData);
+        pTemp->pData.Reset(FXJS_GetRuntime(pData), pData);
       } break;
       case JS_GLOBALDATA_TYPE_NULL:
         break;
       default:
         return FALSE;
     }
-
     return TRUE;
   }
 
@@ -527,7 +493,7 @@ FX_BOOL JSGlobalAlternate::SetGlobalVariables(const FX_CHAR* propname,
     case JS_GLOBALDATA_TYPE_OBJECT: {
       pNewData = new JSGlobalData;
       pNewData->nType = JS_GLOBALDATA_TYPE_OBJECT;
-      pNewData->pData.Reset(JS_GetRuntime(pData), pData);
+      pNewData->pData.Reset(FXJS_GetRuntime(pData), pData);
       pNewData->bPersistent = bDefaultPersistent;
     } break;
     case JS_GLOBALDATA_TYPE_NULL: {
@@ -539,30 +505,29 @@ FX_BOOL JSGlobalAlternate::SetGlobalVariables(const FX_CHAR* propname,
       return FALSE;
   }
 
-  m_mapGlobal.SetAt(propname, (void*)pNewData);
-
+  m_mapGlobal[propname] = pNewData;
   return TRUE;
 }
 
-FXJSVALUETYPE GET_VALUE_TYPE(v8::Local<v8::Value> p) {
-  const unsigned int nHash = JS_CalcHash(JS_GetTypeof(p));
+CJS_Value::Type GET_VALUE_TYPE(v8::Local<v8::Value> p) {
+  const unsigned int nHash = JS_CalcHash(FXJS_GetTypeof(p));
 
   if (nHash == JSCONST_nUndefHash)
-    return VT_undefined;
+    return CJS_Value::VT_undefined;
   if (nHash == JSCONST_nNullHash)
-    return VT_null;
+    return CJS_Value::VT_null;
   if (nHash == JSCONST_nStringHash)
-    return VT_string;
+    return CJS_Value::VT_string;
   if (nHash == JSCONST_nNumberHash)
-    return VT_number;
+    return CJS_Value::VT_number;
   if (nHash == JSCONST_nBoolHash)
-    return VT_boolean;
+    return CJS_Value::VT_boolean;
   if (nHash == JSCONST_nDateHash)
-    return VT_date;
+    return CJS_Value::VT_date;
   if (nHash == JSCONST_nObjectHash)
-    return VT_object;
+    return CJS_Value::VT_object;
   if (nHash == JSCONST_nFXobjHash)
-    return VT_fxobject;
+    return CJS_Value::VT_fxobject;
 
-  return VT_unknown;
+  return CJS_Value::VT_unknown;
 }