Cleanup some numeric code.
[pdfium.git] / fpdfsdk / src / javascript / PublicMethods.cpp
index d0ecd29..13239ae 100644 (file)
@@ -4,33 +4,20 @@
 
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
-#include "../../include/javascript/JavaScript.h"
-#include "../../include/javascript/IJavaScript.h"
-#include "../../include/javascript/JS_Define.h"
-#include "../../include/javascript/JS_Object.h"
-#include "../../include/javascript/JS_Value.h"
-#include "../../include/javascript/PublicMethods.h"
-#include "../../include/javascript/JS_EventHandler.h"
-#include "../../include/javascript/resource.h"
-#include "../../include/javascript/JS_Context.h"
-#include "../../include/javascript/JS_Value.h"
-#include "../../include/javascript/util.h"
-#include "../../include/javascript/Field.h"
-#include "../../include/javascript/color.h"
-#include "../../include/javascript/JS_Runtime.h"
-
-static v8::Isolate* GetIsolate(IFXJS_Context* cc) {
-  CJS_Context* pContext = (CJS_Context*)cc;
-  ASSERT(pContext != NULL);
+#include "PublicMethods.h"
 
-  CJS_Runtime* pRuntime = pContext->GetJSRuntime();
-  ASSERT(pRuntime != NULL);
-
-  return pRuntime->GetIsolate();
-}
-
-/* -------------------------------- CJS_PublicMethods
- * -------------------------------- */
+#include "../../include/fsdk_mgr.h"  // For CPDFDoc_Environment.
+#include "../../include/javascript/IJavaScript.h"
+#include "Field.h"
+#include "JS_Context.h"
+#include "JS_Define.h"
+#include "JS_EventHandler.h"
+#include "JS_Object.h"
+#include "JS_Runtime.h"
+#include "JS_Value.h"
+#include "color.h"
+#include "resource.h"
+#include "util.h"
 
 #define DOUBLE_CORRECT 0.000000000000001
 
@@ -61,14 +48,31 @@ END_JS_STATIC_GLOBAL_FUN()
 
 IMPLEMENT_JS_STATIC_GLOBAL_FUN(CJS_PublicMethods)
 
-static const FX_WCHAR* months[] = {L"Jan", L"Feb", L"Mar", L"Apr",
-                                   L"May", L"Jun", L"Jul", L"Aug",
-                                   L"Sep", L"Oct", L"Nov", L"Dec"};
-
-static const FX_WCHAR* fullmonths[] = {L"January", L"February", L"March",
-                                       L"April",   L"May",      L"June",
-                                       L"July",    L"August",   L"September",
-                                       L"October", L"November", L"December"};
+static const FX_WCHAR* const months[] = {L"Jan",
+                                         L"Feb",
+                                         L"Mar",
+                                         L"Apr",
+                                         L"May",
+                                         L"Jun",
+                                         L"Jul",
+                                         L"Aug",
+                                         L"Sep",
+                                         L"Oct",
+                                         L"Nov",
+                                         L"Dec"};
+
+static const FX_WCHAR* const fullmonths[] = {L"January",
+                                             L"February",
+                                             L"March",
+                                             L"April",
+                                             L"May",
+                                             L"June",
+                                             L"July",
+                                             L"August",
+                                             L"September",
+                                             L"October",
+                                             L"November",
+                                             L"December"};
 
 FX_BOOL CJS_PublicMethods::IsNumber(const FX_WCHAR* string) {
   CFX_WideString sTrim = StrTrim(string);
@@ -112,7 +116,7 @@ FX_BOOL CJS_PublicMethods::IsDigit(wchar_t ch) {
 }
 
 FX_BOOL CJS_PublicMethods::IsDigit(char ch) {
-  return (ch >= '0' && ch <= '9');
+  return std::isdigit(ch);
 }
 
 FX_BOOL CJS_PublicMethods::IsAlphabetic(wchar_t ch) {
@@ -346,9 +350,9 @@ FX_BOOL CJS_PublicMethods::ConvertStringToNumber(const FX_WCHAR* swSource,
   return bAllDigits;
 }
 
-CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(v8::Isolate* isolate,
+CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
                                                   CJS_Value val) {
-  CJS_Array StrArray(isolate);
+  CJS_Array StrArray(pRuntime);
   if (val.IsArrayObject()) {
     val.ConvertToArray(StrArray);
     return StrArray;
@@ -363,14 +367,14 @@ CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(v8::Isolate* isolate,
   while (*p) {
     const char* pTemp = strchr(p, ch);
     if (pTemp == NULL) {
-      StrArray.SetElement(nIndex, CJS_Value(isolate, StrTrim(p).c_str()));
+      StrArray.SetElement(nIndex, CJS_Value(pRuntime, StrTrim(p).c_str()));
       break;
     } else {
       char* pSub = new char[pTemp - p + 1];
       strncpy(pSub, p, pTemp - p);
       *(pSub + (pTemp - p)) = '\0';
 
-      StrArray.SetElement(nIndex, CJS_Value(isolate, StrTrim(pSub).c_str()));
+      StrArray.SetElement(nIndex, CJS_Value(pRuntime, StrTrim(pSub).c_str()));
       delete[] pSub;
 
       nIndex++;
@@ -906,26 +910,24 @@ CFX_WideString CJS_PublicMethods::MakeFormatDate(double dDate,
 
 // function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency,
 // bCurrencyPrepend)
-FX_BOOL CJS_PublicMethods::AFNumber_Format(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFNumber_Format(IJS_Context* cc,
                                            const CJS_Parameters& params,
                                            CJS_Value& vRet,
                                            CFX_WideString& sError) {
 #if _FX_OS_ != _FX_ANDROID_
-  v8::Isolate* isolate = ::GetIsolate(cc);
   CJS_Context* pContext = (CJS_Context*)cc;
-  ASSERT(pContext != NULL);
-  CJS_EventHandler* pEvent = pContext->GetEventHandler();
-  ASSERT(pEvent != NULL);
-
   if (params.size() != 6) {
     sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
     return FALSE;
   }
+
+  CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
+  CJS_EventHandler* pEvent = pContext->GetEventHandler();
   if (!pEvent->m_pValue)
     return FALSE;
+
   CFX_WideString& Value = pEvent->Value();
   CFX_ByteString strValue = StrTrim(CFX_ByteString::FromUnicode(Value));
-
   if (strValue.IsEmpty())
     return TRUE;
 
@@ -999,10 +1001,7 @@ FX_BOOL CJS_PublicMethods::AFNumber_Format(IFXJS_Context* cc,
     else
       cSeperator = '.';
 
-    int iDecPositive;
-    iDecPositive = iDec2;
-
-    for (iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
+    for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
       strValue.Insert(iDecPositive, cSeperator);
       iMax++;
     }
@@ -1031,8 +1030,8 @@ FX_BOOL CJS_PublicMethods::AFNumber_Format(IFXJS_Context* cc,
     }
     if (iNegStyle == 1 || iNegStyle == 3) {
       if (Field* fTarget = pEvent->Target_Field()) {
-        CJS_Array arColor(isolate);
-        CJS_Value vColElm(isolate);
+        CJS_Array arColor(pRuntime);
+        CJS_Value vColElm(pRuntime);
         vColElm = L"RGB";
         arColor.SetElement(0, vColElm);
         vColElm = 1;
@@ -1042,7 +1041,7 @@ FX_BOOL CJS_PublicMethods::AFNumber_Format(IFXJS_Context* cc,
 
         arColor.SetElement(3, vColElm);
 
-        CJS_PropValue vProp(isolate);
+        CJS_PropValue vProp(pRuntime);
         vProp.StartGetting();
         vProp << arColor;
         vProp.StartSetting();
@@ -1052,8 +1051,8 @@ FX_BOOL CJS_PublicMethods::AFNumber_Format(IFXJS_Context* cc,
   } else {
     if (iNegStyle == 1 || iNegStyle == 3) {
       if (Field* fTarget = pEvent->Target_Field()) {
-        CJS_Array arColor(isolate);
-        CJS_Value vColElm(isolate);
+        CJS_Array arColor(pRuntime);
+        CJS_Value vColElm(pRuntime);
         vColElm = L"RGB";
         arColor.SetElement(0, vColElm);
         vColElm = 0;
@@ -1061,11 +1060,11 @@ FX_BOOL CJS_PublicMethods::AFNumber_Format(IFXJS_Context* cc,
         arColor.SetElement(2, vColElm);
         arColor.SetElement(3, vColElm);
 
-        CJS_PropValue vProp(isolate);
+        CJS_PropValue vProp(pRuntime);
         vProp.StartGetting();
         fTarget->textColor(cc, vProp, sError);
 
-        CJS_Array aProp(isolate);
+        CJS_Array aProp(pRuntime);
         vProp.ConvertToArray(aProp);
 
         CPWL_Color crProp;
@@ -1074,7 +1073,7 @@ FX_BOOL CJS_PublicMethods::AFNumber_Format(IFXJS_Context* cc,
         color::ConvertArrayToPWLColor(arColor, crColor);
 
         if (crColor != crProp) {
-          CJS_PropValue vProp2(isolate);
+          CJS_PropValue vProp2(pRuntime);
           vProp2.StartGetting();
           vProp2 << arColor;
           vProp2.StartSetting();
@@ -1090,7 +1089,7 @@ FX_BOOL CJS_PublicMethods::AFNumber_Format(IFXJS_Context* cc,
 
 // function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency,
 // bCurrencyPrepend)
-FX_BOOL CJS_PublicMethods::AFNumber_Keystroke(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFNumber_Keystroke(IJS_Context* cc,
                                               const CJS_Parameters& params,
                                               CJS_Value& vRet,
                                               CFX_WideString& sError) {
@@ -1209,7 +1208,7 @@ FX_BOOL CJS_PublicMethods::AFNumber_Keystroke(IFXJS_Context* cc,
 }
 
 // function AFPercent_Format(nDec, sepStyle)
-FX_BOOL CJS_PublicMethods::AFPercent_Format(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFPercent_Format(IJS_Context* cc,
                                             const CJS_Parameters& params,
                                             CJS_Value& vRet,
                                             CFX_WideString& sError) {
@@ -1288,10 +1287,7 @@ FX_BOOL CJS_PublicMethods::AFPercent_Format(IFXJS_Context* cc,
     else
       cSeperator = '.';
 
-    int iDecPositive;
-    iDecPositive = iDec2;
-
-    for (iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
+    for (int iDecPositive = iDec2 - 3; iDecPositive > 0; iDecPositive -= 3) {
       strValue.Insert(iDecPositive, cSeperator);
       iMax++;
     }
@@ -1306,7 +1302,7 @@ FX_BOOL CJS_PublicMethods::AFPercent_Format(IFXJS_Context* cc,
   return TRUE;
 }
 // AFPercent_Keystroke(nDec, sepStyle)
-FX_BOOL CJS_PublicMethods::AFPercent_Keystroke(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFPercent_Keystroke(IJS_Context* cc,
                                                const CJS_Parameters& params,
                                                CJS_Value& vRet,
                                                CFX_WideString& sError) {
@@ -1314,7 +1310,7 @@ FX_BOOL CJS_PublicMethods::AFPercent_Keystroke(IFXJS_Context* cc,
 }
 
 // function AFDate_FormatEx(cFormat)
-FX_BOOL CJS_PublicMethods::AFDate_FormatEx(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFDate_FormatEx(IJS_Context* cc,
                                            const CJS_Parameters& params,
                                            CJS_Value& vRet,
                                            CFX_WideString& sError) {
@@ -1430,7 +1426,7 @@ double CJS_PublicMethods::MakeInterDate(CFX_WideString strValue) {
 }
 
 // AFDate_KeystrokeEx(cFormat)
-FX_BOOL CJS_PublicMethods::AFDate_KeystrokeEx(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFDate_KeystrokeEx(IJS_Context* cc,
                                               const CJS_Parameters& params,
                                               CJS_Value& vRet,
                                               CFX_WideString& sError) {
@@ -1466,16 +1462,12 @@ FX_BOOL CJS_PublicMethods::AFDate_KeystrokeEx(IFXJS_Context* cc,
   return TRUE;
 }
 
-FX_BOOL CJS_PublicMethods::AFDate_Format(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFDate_Format(IJS_Context* cc,
                                          const CJS_Parameters& params,
                                          CJS_Value& vRet,
                                          CFX_WideString& sError) {
-  v8::Isolate* isolate = ::GetIsolate(cc);
-
+  CJS_Context* pContext = (CJS_Context*)cc;
   if (params.size() != 1) {
-    CJS_Context* pContext = (CJS_Context*)cc;
-    ASSERT(pContext != NULL);
-
     sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
     return FALSE;
   }
@@ -1496,29 +1488,22 @@ FX_BOOL CJS_PublicMethods::AFDate_Format(IFXJS_Context* cc,
                                 L"m/d/yy h:MM tt",
                                 L"m/d/yy HH:MM"};
 
-  ASSERT(iIndex < FX_ArraySize(cFormats));
-
-  if (iIndex < 0)
-    iIndex = 0;
-  if (iIndex >= FX_ArraySize(cFormats))
+  if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
     iIndex = 0;
+
   CJS_Parameters newParams;
-  CJS_Value val(isolate, cFormats[iIndex]);
-  newParams.push_back(val);
+  newParams.push_back(
+      CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
   return AFDate_FormatEx(cc, newParams, vRet, sError);
 }
 
 // AFDate_KeystrokeEx(cFormat)
-FX_BOOL CJS_PublicMethods::AFDate_Keystroke(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFDate_Keystroke(IJS_Context* cc,
                                             const CJS_Parameters& params,
                                             CJS_Value& vRet,
                                             CFX_WideString& sError) {
-  v8::Isolate* isolate = ::GetIsolate(cc);
-
+  CJS_Context* pContext = (CJS_Context*)cc;
   if (params.size() != 1) {
-    CJS_Context* pContext = (CJS_Context*)cc;
-    ASSERT(pContext != NULL);
-
     sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
     return FALSE;
   }
@@ -1539,28 +1524,22 @@ FX_BOOL CJS_PublicMethods::AFDate_Keystroke(IFXJS_Context* cc,
                                 L"m/d/yy h:MM tt",
                                 L"m/d/yy HH:MM"};
 
-  ASSERT(iIndex < FX_ArraySize(cFormats));
-
-  if (iIndex < 0)
-    iIndex = 0;
-  if (iIndex >= FX_ArraySize(cFormats))
+  if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
     iIndex = 0;
+
   CJS_Parameters newParams;
-  CJS_Value val(isolate, cFormats[iIndex]);
-  newParams.push_back(val);
+  newParams.push_back(
+      CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
   return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
 }
 
 // function AFTime_Format(ptf)
-FX_BOOL CJS_PublicMethods::AFTime_Format(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFTime_Format(IJS_Context* cc,
                                          const CJS_Parameters& params,
                                          CJS_Value& vRet,
                                          CFX_WideString& sError) {
-  v8::Isolate* isolate = ::GetIsolate(cc);
-
+  CJS_Context* pContext = (CJS_Context*)cc;
   if (params.size() != 1) {
-    CJS_Context* pContext = (CJS_Context*)cc;
-    ASSERT(pContext != NULL);
     sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
     return FALSE;
   }
@@ -1569,26 +1548,21 @@ FX_BOOL CJS_PublicMethods::AFTime_Format(IFXJS_Context* cc,
   const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
                                 L"h:MM:ss tt"};
 
-  ASSERT(iIndex < FX_ArraySize(cFormats));
-
-  if (iIndex < 0)
-    iIndex = 0;
-  if (iIndex >= FX_ArraySize(cFormats))
+  if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
     iIndex = 0;
+
   CJS_Parameters newParams;
-  CJS_Value val(isolate, cFormats[iIndex]);
-  newParams.push_back(val);
+  newParams.push_back(
+      CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
   return AFDate_FormatEx(cc, newParams, vRet, sError);
 }
 
-FX_BOOL CJS_PublicMethods::AFTime_Keystroke(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFTime_Keystroke(IJS_Context* cc,
                                             const CJS_Parameters& params,
                                             CJS_Value& vRet,
                                             CFX_WideString& sError) {
-  v8::Isolate* isolate = ::GetIsolate(cc);
+  CJS_Context* pContext = (CJS_Context*)cc;
   if (params.size() != 1) {
-    CJS_Context* pContext = (CJS_Context*)cc;
-    ASSERT(pContext != NULL);
     sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
     return FALSE;
   }
@@ -1597,26 +1571,23 @@ FX_BOOL CJS_PublicMethods::AFTime_Keystroke(IFXJS_Context* cc,
   const FX_WCHAR* cFormats[] = {L"HH:MM", L"h:MM tt", L"HH:MM:ss",
                                 L"h:MM:ss tt"};
 
-  ASSERT(iIndex < FX_ArraySize(cFormats));
-
-  if (iIndex < 0)
-    iIndex = 0;
-  if (iIndex >= FX_ArraySize(cFormats))
+  if (iIndex < 0 || (static_cast<size_t>(iIndex) >= FX_ArraySize(cFormats)))
     iIndex = 0;
+
   CJS_Parameters newParams;
-  CJS_Value val(isolate, cFormats[iIndex]);
-  newParams.push_back(val);
+  newParams.push_back(
+      CJS_Value(CJS_Runtime::FromContext(cc), cFormats[iIndex]));
   return AFDate_KeystrokeEx(cc, newParams, vRet, sError);
 }
 
-FX_BOOL CJS_PublicMethods::AFTime_FormatEx(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFTime_FormatEx(IJS_Context* cc,
                                            const CJS_Parameters& params,
                                            CJS_Value& vRet,
                                            CFX_WideString& sError) {
   return AFDate_FormatEx(cc, params, vRet, sError);
 }
 
-FX_BOOL CJS_PublicMethods::AFTime_KeystrokeEx(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFTime_KeystrokeEx(IJS_Context* cc,
                                               const CJS_Parameters& params,
                                               CJS_Value& vRet,
                                               CFX_WideString& sError) {
@@ -1624,7 +1595,7 @@ FX_BOOL CJS_PublicMethods::AFTime_KeystrokeEx(IFXJS_Context* cc,
 }
 
 // function AFSpecial_Format(psf)
-FX_BOOL CJS_PublicMethods::AFSpecial_Format(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFSpecial_Format(IJS_Context* cc,
                                             const CJS_Parameters& params,
                                             CJS_Value& vRet,
                                             CFX_WideString& sError) {
@@ -1675,7 +1646,7 @@ FX_BOOL CJS_PublicMethods::AFSpecial_Format(IFXJS_Context* cc,
 }
 
 // function AFSpecial_KeystrokeEx(mask)
-FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(IJS_Context* cc,
                                                  const CJS_Parameters& params,
                                                  CJS_Value& vRet,
                                                  CFX_WideString& sError) {
@@ -1698,22 +1669,21 @@ FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(IFXJS_Context* cc,
   if (wstrMask.IsEmpty())
     return TRUE;
 
-  std::wstring wstrValue = valEvent.c_str();
+  const size_t wstrMaskLen = wstrMask.GetLength();
+  const std::wstring wstrValue = valEvent.c_str();
 
   if (pEvent->WillCommit()) {
     if (wstrValue.empty())
       return TRUE;
-    int iIndexMask = 0;
-    for (std::wstring::iterator it = wstrValue.begin(); it != wstrValue.end();
-         it++) {
-      wchar_t w_Value = *it;
+    size_t iIndexMask = 0;
+    for (const auto& w_Value : wstrValue) {
       if (!maskSatisfied(w_Value, wstrMask[iIndexMask]))
         break;
       iIndexMask++;
     }
 
-    if (iIndexMask != wstrMask.GetLength() ||
-        (iIndexMask != wstrValue.size() && wstrMask.GetLength() != 0)) {
+    if (iIndexMask != wstrMaskLen ||
+        (iIndexMask != wstrValue.size() && wstrMaskLen != 0)) {
       Alert(
           pContext,
           JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str());
@@ -1729,16 +1699,16 @@ FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(IFXJS_Context* cc,
 
   int iIndexMask = pEvent->SelStart();
 
-  if (wstrValue.length() - (pEvent->SelEnd() - pEvent->SelStart()) +
-          wChange.length() >
-      (FX_DWORD)wstrMask.GetLength()) {
+  size_t combined_len = wstrValue.length() + wChange.length() -
+                        (pEvent->SelEnd() - pEvent->SelStart());
+  if (combined_len > wstrMaskLen) {
     Alert(pContext,
           JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
     pEvent->Rc() = FALSE;
     return TRUE;
   }
 
-  if (iIndexMask >= wstrMask.GetLength() && (!wChange.empty())) {
+  if (iIndexMask >= wstrMaskLen && (!wChange.empty())) {
     Alert(pContext,
           JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
     pEvent->Rc() = FALSE;
@@ -1746,7 +1716,7 @@ FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(IFXJS_Context* cc,
   }
 
   for (std::wstring::iterator it = wChange.begin(); it != wChange.end(); it++) {
-    if (iIndexMask >= wstrMask.GetLength()) {
+    if (iIndexMask >= wstrMaskLen) {
       Alert(pContext,
             JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str());
       pEvent->Rc() = FALSE;
@@ -1769,28 +1739,22 @@ FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx(IFXJS_Context* cc,
 }
 
 // function AFSpecial_Keystroke(psf)
-FX_BOOL CJS_PublicMethods::AFSpecial_Keystroke(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFSpecial_Keystroke(IJS_Context* cc,
                                                const CJS_Parameters& params,
                                                CJS_Value& vRet,
                                                CFX_WideString& sError) {
-  v8::Isolate* isolate = ::GetIsolate(cc);
-
   CJS_Context* pContext = (CJS_Context*)cc;
-  ASSERT(pContext != NULL);
-  CJS_EventHandler* pEvent = pContext->GetEventHandler();
-  ASSERT(pEvent != NULL);
-
   if (params.size() != 1) {
     sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
     return FALSE;
   }
 
-  std::string cFormat;
-  int iIndex = params[0].ToInt();
-
+  CJS_EventHandler* pEvent = pContext->GetEventHandler();
   if (!pEvent->m_pValue)
     return FALSE;
-  // CJS_Value val = pEvent->Value();
+
+  std::string cFormat;
+  int iIndex = params[0].ToInt();
   CFX_WideString& val = pEvent->Value();
   std::string strSrc = CFX_ByteString::FromUnicode(val).c_str();
   std::wstring wstrChange = pEvent->Change().c_str();
@@ -1821,13 +1785,11 @@ FX_BOOL CJS_PublicMethods::AFSpecial_Keystroke(IFXJS_Context* cc,
   }
 
   CJS_Parameters params2;
-  CJS_Value vMask(isolate, cFormat.c_str());
-  params2.push_back(vMask);
-
+  params2.push_back(CJS_Value(CJS_Runtime::FromContext(cc), cFormat.c_str()));
   return AFSpecial_KeystrokeEx(cc, params2, vRet, sError);
 }
 
-FX_BOOL CJS_PublicMethods::AFMergeChange(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFMergeChange(IJS_Context* cc,
                                          const CJS_Parameters& params,
                                          CJS_Value& vRet,
                                          CFX_WideString& sError) {
@@ -1869,7 +1831,7 @@ FX_BOOL CJS_PublicMethods::AFMergeChange(IFXJS_Context* cc,
   return TRUE;
 }
 
-FX_BOOL CJS_PublicMethods::AFParseDateEx(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFParseDateEx(IJS_Context* cc,
                                          const CJS_Parameters& params,
                                          CJS_Value& vRet,
                                          CFX_WideString& sError) {
@@ -1899,7 +1861,7 @@ FX_BOOL CJS_PublicMethods::AFParseDateEx(IFXJS_Context* cc,
   return TRUE;
 }
 
-FX_BOOL CJS_PublicMethods::AFSimple(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFSimple(IJS_Context* cc,
                                     const CJS_Parameters& params,
                                     CJS_Value& vRet,
                                     CFX_WideString& sError) {
@@ -1916,7 +1878,7 @@ FX_BOOL CJS_PublicMethods::AFSimple(IFXJS_Context* cc,
   return TRUE;
 }
 
-FX_BOOL CJS_PublicMethods::AFMakeNumber(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFMakeNumber(IJS_Context* cc,
                                         const CJS_Parameters& params,
                                         CJS_Value& vRet,
                                         CFX_WideString& sError) {
@@ -1931,49 +1893,35 @@ FX_BOOL CJS_PublicMethods::AFMakeNumber(IFXJS_Context* cc,
   return TRUE;
 }
 
-FX_BOOL CJS_PublicMethods::AFSimple_Calculate(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFSimple_Calculate(IJS_Context* cc,
                                               const CJS_Parameters& params,
                                               CJS_Value& vRet,
                                               CFX_WideString& sError) {
-  v8::Isolate* isolate = ::GetIsolate(cc);
-
   CJS_Context* pContext = (CJS_Context*)cc;
-  ASSERT(pContext != NULL);
-
   if (params.size() != 2) {
     sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
     return FALSE;
   }
 
   CJS_Value params1 = params[1];
-
-  if (!params1.IsArrayObject() && params1.GetType() != VT_string) {
+  if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) {
     sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
     return FALSE;
   }
 
   CPDFSDK_Document* pReaderDoc = pContext->GetReaderDocument();
-  ASSERT(pReaderDoc != NULL);
-
   CPDFSDK_InterForm* pReaderInterForm = pReaderDoc->GetInterForm();
-  ASSERT(pReaderInterForm != NULL);
-
   CPDF_InterForm* pInterForm = pReaderInterForm->GetInterForm();
-  ASSERT(pInterForm != NULL);
 
-  double dValue;
   CFX_WideString sFunction = params[0].ToCFXWideString();
-  if (wcscmp(sFunction.c_str(), L"PRD") == 0)
-    dValue = 1.0;
-  else
-    dValue = 0.0;
-
-  CJS_Array FieldNameArray = AF_MakeArrayFromList(isolate, params1);
+  double dValue = wcscmp(sFunction.c_str(), L"PRD") == 0 ? 1.0 : 0.0;
 
+  CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
+  CJS_Array FieldNameArray = AF_MakeArrayFromList(pRuntime, params1);
   int nFieldsCount = 0;
 
   for (int i = 0, isz = FieldNameArray.GetLength(); i < isz; i++) {
-    CJS_Value jsValue(isolate);
+    CJS_Value jsValue(pRuntime);
     FieldNameArray.GetElement(i, jsValue);
     CFX_WideString wsFieldName = jsValue.ToCFXWideString();
 
@@ -2035,10 +1983,9 @@ FX_BOOL CJS_PublicMethods::AFSimple_Calculate(IFXJS_Context* cc,
 
   dValue = (double)floor(dValue * FXSYS_pow((double)10, (double)6) + 0.49) /
            FXSYS_pow((double)10, (double)6);
-  CJS_Value jsValue(isolate, dValue);
-  if ((CJS_EventHandler*)pContext->GetEventHandler()->m_pValue)
-    ((CJS_EventHandler*)pContext->GetEventHandler())->Value() =
-        jsValue.ToCFXWideString();
+  CJS_Value jsValue(pRuntime, dValue);
+  if (pContext->GetEventHandler()->m_pValue)
+    pContext->GetEventHandler()->Value() = jsValue.ToCFXWideString();
 
   return TRUE;
 }
@@ -2046,7 +1993,7 @@ FX_BOOL CJS_PublicMethods::AFSimple_Calculate(IFXJS_Context* cc,
 /* This function validates the current event to ensure that its value is
 ** within the specified range. */
 
-FX_BOOL CJS_PublicMethods::AFRange_Validate(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFRange_Validate(IJS_Context* cc,
                                             const CJS_Parameters& params,
                                             CJS_Value& vRet,
                                             CFX_WideString& sError) {
@@ -2093,20 +2040,18 @@ FX_BOOL CJS_PublicMethods::AFRange_Validate(IFXJS_Context* cc,
   return TRUE;
 }
 
-FX_BOOL CJS_PublicMethods::AFExtractNums(IFXJS_Context* cc,
+FX_BOOL CJS_PublicMethods::AFExtractNums(IJS_Context* cc,
                                          const CJS_Parameters& params,
                                          CJS_Value& vRet,
                                          CFX_WideString& sError) {
-  v8::Isolate* isolate = ::GetIsolate(cc);
   CJS_Context* pContext = (CJS_Context*)cc;
-  ASSERT(pContext != NULL);
-
   if (params.size() != 1) {
     sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR);
     return FALSE;
   }
 
-  CJS_Array nums(isolate);
+  CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
+  CJS_Array nums(pRuntime);
 
   CFX_WideString str = params[0].ToCFXWideString();
   CFX_WideString sPart;
@@ -2121,7 +2066,7 @@ FX_BOOL CJS_PublicMethods::AFExtractNums(IFXJS_Context* cc,
       sPart += wc;
     } else {
       if (sPart.GetLength() > 0) {
-        nums.SetElement(nIndex, CJS_Value(isolate, sPart.c_str()));
+        nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str()));
         sPart = L"";
         nIndex++;
       }
@@ -2129,7 +2074,7 @@ FX_BOOL CJS_PublicMethods::AFExtractNums(IFXJS_Context* cc,
   }
 
   if (sPart.GetLength() > 0) {
-    nums.SetElement(nIndex, CJS_Value(isolate, sPart.c_str()));
+    nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str()));
   }
 
   if (nums.GetLength() > 0)