Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Side by Side Diff: xfa/fxfa/fm2js/xfa_fm2jscontext.cpp

Issue 2028063002: xfa_fm2jscontext method cleanup - pt III (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "xfa/fxfa/fm2js/xfa_fm2jscontext.h" 7 #include "xfa/fxfa/fm2js/xfa_fm2jscontext.h"
8 8
9 #include <time.h> 9 #include <time.h>
10 10
(...skipping 778 matching lines...) Expand 10 before | Expand all | Expand 10 after
789 void CXFA_FM2JSContext::Min(CFXJSE_Value* pThis, 789 void CXFA_FM2JSContext::Min(CFXJSE_Value* pThis,
790 const CFX_ByteStringC& szFuncName, 790 const CFX_ByteStringC& szFuncName,
791 CFXJSE_Arguments& args) { 791 CFXJSE_Arguments& args) {
792 CXFA_FM2JSContext* pContext = 792 CXFA_FM2JSContext* pContext =
793 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); 793 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr));
794 v8::Isolate* pIsolate = pContext->GetScriptRuntime(); 794 v8::Isolate* pIsolate = pContext->GetScriptRuntime();
795 uint32_t uCount = 0; 795 uint32_t uCount = 0;
796 FX_DOUBLE dMinValue = 0.0; 796 FX_DOUBLE dMinValue = 0.0;
797 for (int32_t i = 0; i < args.GetLength(); i++) { 797 for (int32_t i = 0; i < args.GetLength(); i++) {
798 std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i); 798 std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i);
799 if (FXJSE_Value_IsNull(argValue.get())) { 799 if (FXJSE_Value_IsNull(argValue.get()))
800 continue; 800 continue;
801 } else if (FXJSE_Value_IsArray(argValue.get())) { 801
802 if (FXJSE_Value_IsArray(argValue.get())) {
802 std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); 803 std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate));
803 FXJSE_Value_GetObjectProp(argValue.get(), "length", lengthValue.get()); 804 FXJSE_Value_GetObjectProp(argValue.get(), "length", lengthValue.get());
804 int32_t iLength = FXJSE_Value_ToInteger(lengthValue.get()); 805 int32_t iLength = FXJSE_Value_ToInteger(lengthValue.get());
805 if (iLength > 2) { 806 if (iLength <= 2) {
806 std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); 807 pContext->ThrowScriptErrorMessage(XFA_IDS_ARGUMENT_MISMATCH);
807 std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); 808 return;
dsinclair 2016/06/01 14:02:01 Added return due to throw.
808 std::unique_ptr<CFXJSE_Value> newPropertyValue( 809 }
809 new CFXJSE_Value(pIsolate)); 810
810 FXJSE_Value_GetObjectPropByIdx(argValue.get(), 1, propertyValue.get()); 811 std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate));
811 FXJSE_Value_GetObjectPropByIdx(argValue.get(), 2, jsObjectValue.get()); 812 std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate));
812 if (FXJSE_Value_IsNull(propertyValue.get())) { 813 std::unique_ptr<CFXJSE_Value> newPropertyValue(
813 for (int32_t i = 2; i < iLength; i++) { 814 new CFXJSE_Value(pIsolate));
814 FXJSE_Value_GetObjectPropByIdx(argValue.get(), i, 815 FXJSE_Value_GetObjectPropByIdx(argValue.get(), 1, propertyValue.get());
815 jsObjectValue.get()); 816 FXJSE_Value_GetObjectPropByIdx(argValue.get(), 2, jsObjectValue.get());
816 GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get()); 817 if (FXJSE_Value_IsNull(propertyValue.get())) {
817 if (!FXJSE_Value_IsNull(newPropertyValue.get())) { 818 for (int32_t i = 2; i < iLength; i++) {
818 uCount++; 819 FXJSE_Value_GetObjectPropByIdx(argValue.get(), i,
819 if (uCount == 1) { 820 jsObjectValue.get());
820 dMinValue = ValueToDouble(pThis, newPropertyValue.get()); 821 GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get());
821 } else { 822 if (FXJSE_Value_IsNull(newPropertyValue.get()))
822 FX_DOUBLE dValue = ValueToDouble(pThis, newPropertyValue.get()); 823 continue;
823 if (dMinValue > dValue) { 824
824 dMinValue = dValue; 825 uCount++;
825 } 826 FX_DOUBLE dValue = ValueToDouble(pThis, newPropertyValue.get());
826 } 827 dMinValue = uCount == 1 ? dValue : std::min(dMinValue, dValue);
827 }
828 }
829 } else {
830 CFX_ByteString propertyStr;
831 FXJSE_Value_ToUTF8String(propertyValue.get(), propertyStr);
832 for (int32_t i = 2; i < iLength; i++) {
833 FXJSE_Value_GetObjectPropByIdx(argValue.get(), i,
834 jsObjectValue.get());
835 FXJSE_Value_GetObjectProp(jsObjectValue.get(),
836 propertyStr.AsStringC(),
837 newPropertyValue.get());
838 if (!FXJSE_Value_IsNull(newPropertyValue.get())) {
839 uCount++;
840 if (uCount == 1) {
841 dMinValue = ValueToDouble(pThis, newPropertyValue.get());
842 } else {
843 FX_DOUBLE dValue = ValueToDouble(pThis, newPropertyValue.get());
844 if (dMinValue > dValue) {
845 dMinValue = dValue;
846 }
847 }
848 }
849 }
850 } 828 }
851 } else { 829 } else {
852 pContext->ThrowScriptErrorMessage(XFA_IDS_ARGUMENT_MISMATCH); 830 CFX_ByteString propertyStr;
831 FXJSE_Value_ToUTF8String(propertyValue.get(), propertyStr);
832 for (int32_t i = 2; i < iLength; i++) {
833 FXJSE_Value_GetObjectPropByIdx(argValue.get(), i,
834 jsObjectValue.get());
835 FXJSE_Value_GetObjectProp(jsObjectValue.get(),
836 propertyStr.AsStringC(),
837 newPropertyValue.get());
838 if (FXJSE_Value_IsNull(newPropertyValue.get()))
839 continue;
840
841 uCount++;
842 FX_DOUBLE dValue = ValueToDouble(pThis, newPropertyValue.get());
843 dMinValue = uCount == 1 ? dValue : std::min(dMinValue, dValue);
844 }
853 } 845 }
854 } else if (FXJSE_Value_IsObject(argValue.get())) { 846 } else if (FXJSE_Value_IsObject(argValue.get())) {
855 std::unique_ptr<CFXJSE_Value> newPropertyValue( 847 std::unique_ptr<CFXJSE_Value> newPropertyValue(
856 new CFXJSE_Value(pIsolate)); 848 new CFXJSE_Value(pIsolate));
857 GetObjectDefaultValue(argValue.get(), newPropertyValue.get()); 849 GetObjectDefaultValue(argValue.get(), newPropertyValue.get());
858 if (!FXJSE_Value_IsNull(newPropertyValue.get())) { 850 if (FXJSE_Value_IsNull(newPropertyValue.get()))
859 uCount++; 851 continue;
860 if (uCount == 1) { 852
861 dMinValue = ValueToDouble(pThis, newPropertyValue.get()); 853 uCount++;
862 } else { 854 FX_DOUBLE dValue = ValueToDouble(pThis, newPropertyValue.get());
863 FX_DOUBLE dValue = ValueToDouble(pThis, newPropertyValue.get()); 855 dMinValue = uCount == 1 ? dValue : std::min(dMinValue, dValue);
864 if (dMinValue > dValue) {
865 dMinValue = dValue;
866 }
867 }
868 }
869 } else { 856 } else {
870 uCount++; 857 uCount++;
Lei Zhang 2016/06/01 19:50:46 We seem to repeat this a lot.
dsinclair 2016/06/02 02:16:22 Yea, the problem is some bits of it are inside inn
871 if (uCount == 1) { 858 FX_DOUBLE dValue = ValueToDouble(pThis, argValue.get());
872 dMinValue = ValueToDouble(pThis, argValue.get()); 859 dMinValue = uCount == 1 ? dValue : std::min(dMinValue, dValue);
873 } else {
874 FX_DOUBLE dValue = ValueToDouble(pThis, argValue.get());
875 if (dMinValue > dValue) {
876 dMinValue = dValue;
877 }
878 }
879 } 860 }
880 } 861 }
862 if (uCount == 0) {
863 FXJSE_Value_SetNull(args.GetReturnValue());
864 return;
865 }
881 866
882 if (uCount) { 867 FXJSE_Value_SetDouble(args.GetReturnValue(), dMinValue);
883 FXJSE_Value_SetDouble(args.GetReturnValue(), dMinValue);
884 } else {
885 FXJSE_Value_SetNull(args.GetReturnValue());
886 }
887 } 868 }
888 869
889 // static 870 // static
890 void CXFA_FM2JSContext::Mod(CFXJSE_Value* pThis, 871 void CXFA_FM2JSContext::Mod(CFXJSE_Value* pThis,
891 const CFX_ByteStringC& szFuncName, 872 const CFX_ByteStringC& szFuncName,
892 CFXJSE_Arguments& args) { 873 CFXJSE_Arguments& args) {
893 CXFA_FM2JSContext* pContext = 874 CXFA_FM2JSContext* pContext =
894 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); 875 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr));
895 v8::Isolate* pIsolate = pContext->GetScriptRuntime(); 876 v8::Isolate* pIsolate = pContext->GetScriptRuntime();
896 if (args.GetLength() == 2) { 877 if (args.GetLength() != 2) {
897 std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); 878 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD,
898 std::unique_ptr<CFXJSE_Value> argTwo = args.GetValue(1); 879 L"Mod");
899 if (FXJSE_Value_IsNull(argOne.get()) || FXJSE_Value_IsNull(argTwo.get())) { 880 return;
900 FXJSE_Value_SetNull(args.GetReturnValue()); 881 }
882
883 std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0);
884 std::unique_ptr<CFXJSE_Value> argTwo = args.GetValue(1);
885 if (FXJSE_Value_IsNull(argOne.get()) || FXJSE_Value_IsNull(argTwo.get())) {
886 FXJSE_Value_SetNull(args.GetReturnValue());
887 return;
888 }
889
890 FX_DOUBLE dDividend = 0.0;
891 FX_DOUBLE dDividor = 0.0;
Lei Zhang 2016/06/01 20:00:43 divisor, not dividor
Tom Sepez 2016/06/01 20:07:12 Either way, initializing dividor to 0.0 seems like
dsinclair 2016/06/02 02:16:22 We do check if it == 0 before usage, but changed t
dsinclair 2016/06/02 02:16:22 Done.
892 if (FXJSE_Value_IsArray(argOne.get())) {
893 std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate));
894 FXJSE_Value_GetObjectProp(argOne.get(), "length", lengthValue.get());
895 int32_t iLength = FXJSE_Value_ToInteger(lengthValue.get());
896 if (iLength <= 2) {
897 pContext->ThrowScriptErrorMessage(XFA_IDS_ARGUMENT_MISMATCH);
898 return;
899 }
900
901 std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate));
902 std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate));
903 FXJSE_Value_GetObjectPropByIdx(argOne.get(), 1, propertyValue.get());
904 FXJSE_Value_GetObjectPropByIdx(argOne.get(), 2, jsObjectValue.get());
905 if (FXJSE_Value_IsNull(propertyValue.get())) {
906 dDividend = ValueToDouble(pThis, jsObjectValue.get());
901 } else { 907 } else {
902 FX_DOUBLE dDividend = 0.0; 908 CFX_ByteString propertyStr;
903 FX_DOUBLE dDividor = 0.0; 909 FXJSE_Value_ToUTF8String(propertyValue.get(), propertyStr);
904 if (FXJSE_Value_IsArray(argOne.get())) { 910 std::unique_ptr<CFXJSE_Value> newPropertyValue(
905 std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); 911 new CFXJSE_Value(pIsolate));
906 FXJSE_Value_GetObjectProp(argOne.get(), "length", lengthValue.get()); 912 FXJSE_Value_GetObjectProp(jsObjectValue.get(), propertyStr.AsStringC(),
907 int32_t iLength = FXJSE_Value_ToInteger(lengthValue.get()); 913 newPropertyValue.get());
908 if (iLength > 2) { 914 dDividend = ValueToDouble(pThis, newPropertyValue.get());
909 std::unique_ptr<CFXJSE_Value> propertyValue(
910 new CFXJSE_Value(pIsolate));
911 std::unique_ptr<CFXJSE_Value> jsObjectValue(
912 new CFXJSE_Value(pIsolate));
913 FXJSE_Value_GetObjectPropByIdx(argOne.get(), 1, propertyValue.get());
914 FXJSE_Value_GetObjectPropByIdx(argOne.get(), 2, jsObjectValue.get());
915 if (FXJSE_Value_IsNull(propertyValue.get())) {
916 dDividend = ValueToDouble(pThis, jsObjectValue.get());
917 } else {
918 CFX_ByteString propertyStr;
919 FXJSE_Value_ToUTF8String(propertyValue.get(), propertyStr);
920 std::unique_ptr<CFXJSE_Value> newPropertyValue(
921 new CFXJSE_Value(pIsolate));
922 FXJSE_Value_GetObjectProp(jsObjectValue.get(),
923 propertyStr.AsStringC(),
924 newPropertyValue.get());
925 dDividend = ValueToDouble(pThis, newPropertyValue.get());
926 }
927 } else {
928 pContext->ThrowScriptErrorMessage(XFA_IDS_ARGUMENT_MISMATCH);
929 }
930 } else {
931 dDividend = ValueToDouble(pThis, argOne.get());
932 }
933 if (FXJSE_Value_IsArray(argTwo.get())) {
934 std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate));
935 FXJSE_Value_GetObjectProp(argTwo.get(), "length", lengthValue.get());
936 int32_t iLength = FXJSE_Value_ToInteger(lengthValue.get());
937 if (iLength > 2) {
938 std::unique_ptr<CFXJSE_Value> propertyValue(
939 new CFXJSE_Value(pIsolate));
940 std::unique_ptr<CFXJSE_Value> jsObjectValue(
941 new CFXJSE_Value(pIsolate));
942 FXJSE_Value_GetObjectPropByIdx(argTwo.get(), 1, propertyValue.get());
943 FXJSE_Value_GetObjectPropByIdx(argTwo.get(), 2, jsObjectValue.get());
944 if (FXJSE_Value_IsNull(propertyValue.get())) {
945 dDividor = ValueToDouble(pThis, jsObjectValue.get());
946 } else {
947 CFX_ByteString propertyStr;
948 FXJSE_Value_ToUTF8String(propertyValue.get(), propertyStr);
949 std::unique_ptr<CFXJSE_Value> newPropertyValue(
950 new CFXJSE_Value(pIsolate));
951 FXJSE_Value_GetObjectProp(jsObjectValue.get(),
952 propertyStr.AsStringC(),
953 newPropertyValue.get());
954 dDividor = ValueToDouble(pThis, newPropertyValue.get());
955 }
956 } else {
957 pContext->ThrowScriptErrorMessage(XFA_IDS_ARGUMENT_MISMATCH);
958 }
959 } else {
960 dDividor = ValueToDouble(pThis, argTwo.get());
961 }
962 if (dDividor) {
963 FXJSE_Value_SetDouble(
964 args.GetReturnValue(),
965 dDividend - dDividor * (int32_t)(dDividend / dDividor));
966 } else {
967 pContext->ThrowScriptErrorMessage(XFA_IDS_DIVIDE_ZERO);
968 }
969 } 915 }
970 } else { 916 } else {
971 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, 917 dDividend = ValueToDouble(pThis, argOne.get());
972 L"Mod");
973 } 918 }
919
920 if (FXJSE_Value_IsArray(argTwo.get())) {
Lei Zhang 2016/06/01 19:50:46 This look almost identical to the |argOne| block s
dsinclair 2016/06/02 02:16:21 Done.
921 std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate));
922 FXJSE_Value_GetObjectProp(argTwo.get(), "length", lengthValue.get());
923 int32_t iLength = FXJSE_Value_ToInteger(lengthValue.get());
924 if (iLength <= 2) {
925 pContext->ThrowScriptErrorMessage(XFA_IDS_ARGUMENT_MISMATCH);
926 return;
927 }
928
929 std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate));
930 std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate));
931 FXJSE_Value_GetObjectPropByIdx(argTwo.get(), 1, propertyValue.get());
932 FXJSE_Value_GetObjectPropByIdx(argTwo.get(), 2, jsObjectValue.get());
933 if (FXJSE_Value_IsNull(propertyValue.get())) {
934 dDividor = ValueToDouble(pThis, jsObjectValue.get());
935 } else {
936 CFX_ByteString propertyStr;
937 FXJSE_Value_ToUTF8String(propertyValue.get(), propertyStr);
938 std::unique_ptr<CFXJSE_Value> newPropertyValue(
939 new CFXJSE_Value(pIsolate));
940 FXJSE_Value_GetObjectProp(jsObjectValue.get(), propertyStr.AsStringC(),
941 newPropertyValue.get());
942 dDividor = ValueToDouble(pThis, newPropertyValue.get());
943 }
944 } else {
945 dDividor = ValueToDouble(pThis, argTwo.get());
946 }
947
948 if (dDividor == 0) {
949 pContext->ThrowScriptErrorMessage(XFA_IDS_DIVIDE_ZERO);
950 return;
951 }
952
953 FXJSE_Value_SetDouble(args.GetReturnValue(),
954 dDividend - dDividor * (int32_t)(dDividend / dDividor));
974 } 955 }
975 956
976 // static 957 // static
977 void CXFA_FM2JSContext::Round(CFXJSE_Value* pThis, 958 void CXFA_FM2JSContext::Round(CFXJSE_Value* pThis,
978 const CFX_ByteStringC& szFuncName, 959 const CFX_ByteStringC& szFuncName,
979 CFXJSE_Arguments& args) { 960 CFXJSE_Arguments& args) {
980 CXFA_FM2JSContext* pContext = 961 CXFA_FM2JSContext* pContext =
981 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); 962 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr));
982 v8::Isolate* pIsolate = pContext->GetScriptRuntime(); 963 v8::Isolate* pIsolate = pContext->GetScriptRuntime();
983 int32_t argc = args.GetLength(); 964 int32_t argc = args.GetLength();
984 uint8_t uPrecision = 0; 965 uint8_t uPrecision = 0;
966
967 if (argc != 1 && argc != 2) {
968 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD,
969 L"Round");
970 return;
971 }
972
985 if (argc == 1) { 973 if (argc == 1) {
986 std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); 974 std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0);
987 if (FXJSE_Value_IsNull(argOne.get())) { 975 if (FXJSE_Value_IsNull(argOne.get())) {
988 FXJSE_Value_SetNull(args.GetReturnValue()); 976 FXJSE_Value_SetNull(args.GetReturnValue());
977 return;
978 }
979
980 FX_DOUBLE dValue = 0.0;
981 if (FXJSE_Value_IsArray(argOne.get())) {
982 std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate));
983 std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate));
984 FXJSE_Value_GetObjectPropByIdx(argOne.get(), 1, propertyValue.get());
985 FXJSE_Value_GetObjectPropByIdx(argOne.get(), 2, jsObjectValue.get());
986 if (FXJSE_Value_IsNull(propertyValue.get())) {
Lei Zhang 2016/06/01 20:00:43 This if-else block looks identical to code that de
dsinclair 2016/06/02 02:16:22 Done.
987 dValue = ValueToDouble(pThis, jsObjectValue.get());
988 } else {
989 CFX_ByteString propertyStr;
990 FXJSE_Value_ToUTF8String(propertyValue.get(), propertyStr);
991 std::unique_ptr<CFXJSE_Value> newPropertyValue(
992 new CFXJSE_Value(pIsolate));
993 FXJSE_Value_GetObjectProp(jsObjectValue.get(), propertyStr.AsStringC(),
994 newPropertyValue.get());
995 dValue = ValueToDouble(pThis, newPropertyValue.get());
996 }
989 } else { 997 } else {
990 FX_DOUBLE dValue = 0.0; 998 dValue = ValueToDouble(pThis, argOne.get());
991 if (FXJSE_Value_IsArray(argOne.get())) {
992 std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate));
993 std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate));
994 FXJSE_Value_GetObjectPropByIdx(argOne.get(), 1, propertyValue.get());
995 FXJSE_Value_GetObjectPropByIdx(argOne.get(), 2, jsObjectValue.get());
996 if (FXJSE_Value_IsNull(propertyValue.get())) {
997 dValue = ValueToDouble(pThis, jsObjectValue.get());
998 } else {
999 CFX_ByteString propertyStr;
1000 FXJSE_Value_ToUTF8String(propertyValue.get(), propertyStr);
1001 std::unique_ptr<CFXJSE_Value> newPropertyValue(
1002 new CFXJSE_Value(pIsolate));
1003 FXJSE_Value_GetObjectProp(jsObjectValue.get(),
1004 propertyStr.AsStringC(),
1005 newPropertyValue.get());
1006 dValue = ValueToDouble(pThis, newPropertyValue.get());
1007 }
1008 } else {
1009 dValue = ValueToDouble(pThis, argOne.get());
1010 }
1011 CFX_Decimal decimalValue((FX_FLOAT)dValue, uPrecision);
1012 CFX_WideString wsValue = decimalValue;
1013 FXJSE_Value_SetUTF8String(args.GetReturnValue(),
1014 wsValue.UTF8Encode().AsStringC());
1015 } 999 }
1000 CFX_Decimal decimalValue((FX_FLOAT)dValue, uPrecision);
1001 CFX_WideString wsValue = decimalValue;
1002 FXJSE_Value_SetUTF8String(args.GetReturnValue(),
1003 wsValue.UTF8Encode().AsStringC());
1016 } else if (argc == 2) { 1004 } else if (argc == 2) {
Lei Zhang 2016/06/01 20:00:42 Just return in the argc == 1 block, assert argc ==
dsinclair 2016/06/02 02:16:22 Rewrote it as argc == 1 and argc == 2 duplicated a
1017 std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); 1005 std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0);
1018 std::unique_ptr<CFXJSE_Value> argTwo = args.GetValue(1); 1006 std::unique_ptr<CFXJSE_Value> argTwo = args.GetValue(1);
1019 if (FXJSE_Value_IsNull(argOne.get()) || FXJSE_Value_IsNull(argTwo.get())) { 1007 if (FXJSE_Value_IsNull(argOne.get()) || FXJSE_Value_IsNull(argTwo.get())) {
1020 FXJSE_Value_SetNull(args.GetReturnValue()); 1008 FXJSE_Value_SetNull(args.GetReturnValue());
1009 return;
1010 }
1011
1012 FX_DOUBLE dValue = 0.0;
1013 if (FXJSE_Value_IsArray(argOne.get())) {
Lei Zhang 2016/06/01 20:00:42 More similar looking code here and on line 1033.
dsinclair 2016/06/02 02:16:22 Done.
1014 std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate));
1015 std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate));
1016 FXJSE_Value_GetObjectPropByIdx(argOne.get(), 1, propertyValue.get());
1017 FXJSE_Value_GetObjectPropByIdx(argOne.get(), 2, jsObjectValue.get());
1018 if (FXJSE_Value_IsNull(propertyValue.get())) {
1019 dValue = ValueToDouble(pThis, jsObjectValue.get());
1020 } else {
1021 CFX_ByteString propertyStr;
1022 FXJSE_Value_ToUTF8String(propertyValue.get(), propertyStr);
1023 std::unique_ptr<CFXJSE_Value> newPropertyValue(
1024 new CFXJSE_Value(pIsolate));
1025 FXJSE_Value_GetObjectProp(jsObjectValue.get(), propertyStr.AsStringC(),
1026 newPropertyValue.get());
1027 dValue = ValueToDouble(pThis, newPropertyValue.get());
1028 }
1021 } else { 1029 } else {
1022 FX_DOUBLE dValue = 0.0; 1030 dValue = ValueToDouble(pThis, argOne.get());
1023 if (FXJSE_Value_IsArray(argOne.get())) { 1031 }
1024 std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); 1032 FX_DOUBLE dPrecision = 0.0;
1025 std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); 1033 if (FXJSE_Value_IsArray(argTwo.get())) {
1026 FXJSE_Value_GetObjectPropByIdx(argOne.get(), 1, propertyValue.get()); 1034 std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate));
1027 FXJSE_Value_GetObjectPropByIdx(argOne.get(), 2, jsObjectValue.get()); 1035 std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate));
1028 if (FXJSE_Value_IsNull(propertyValue.get())) { 1036 FXJSE_Value_GetObjectPropByIdx(argTwo.get(), 1, propertyValue.get());
1029 dValue = ValueToDouble(pThis, jsObjectValue.get()); 1037 FXJSE_Value_GetObjectPropByIdx(argTwo.get(), 2, jsObjectValue.get());
1030 } else { 1038 if (FXJSE_Value_IsNull(propertyValue.get())) {
1031 CFX_ByteString propertyStr; 1039 dPrecision = ValueToDouble(pThis, jsObjectValue.get());
1032 FXJSE_Value_ToUTF8String(propertyValue.get(), propertyStr);
1033 std::unique_ptr<CFXJSE_Value> newPropertyValue(
1034 new CFXJSE_Value(pIsolate));
1035 FXJSE_Value_GetObjectProp(jsObjectValue.get(),
1036 propertyStr.AsStringC(),
1037 newPropertyValue.get());
1038 dValue = ValueToDouble(pThis, newPropertyValue.get());
1039 }
1040 } else { 1040 } else {
1041 dValue = ValueToDouble(pThis, argOne.get()); 1041 CFX_ByteString propertyStr;
1042 FXJSE_Value_ToUTF8String(propertyValue.get(), propertyStr);
1043 std::unique_ptr<CFXJSE_Value> newPropertyValue(
1044 new CFXJSE_Value(pIsolate));
1045 FXJSE_Value_GetObjectProp(jsObjectValue.get(), propertyStr.AsStringC(),
1046 newPropertyValue.get());
1047 dPrecision = ValueToDouble(pThis, newPropertyValue.get());
1042 } 1048 }
1043 FX_DOUBLE dPrecision = 0.0; 1049 } else {
1044 if (FXJSE_Value_IsArray(argTwo.get())) { 1050 dPrecision = ValueToDouble(pThis, argTwo.get());
1045 std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate));
1046 std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate));
1047 FXJSE_Value_GetObjectPropByIdx(argTwo.get(), 1, propertyValue.get());
1048 FXJSE_Value_GetObjectPropByIdx(argTwo.get(), 2, jsObjectValue.get());
1049 if (FXJSE_Value_IsNull(propertyValue.get())) {
1050 dPrecision = ValueToDouble(pThis, jsObjectValue.get());
1051 } else {
1052 CFX_ByteString propertyStr;
1053 FXJSE_Value_ToUTF8String(propertyValue.get(), propertyStr);
1054 std::unique_ptr<CFXJSE_Value> newPropertyValue(
1055 new CFXJSE_Value(pIsolate));
1056 FXJSE_Value_GetObjectProp(jsObjectValue.get(),
1057 propertyStr.AsStringC(),
1058 newPropertyValue.get());
1059 dPrecision = ValueToDouble(pThis, newPropertyValue.get());
1060 }
1061 } else {
1062 dPrecision = ValueToDouble(pThis, argTwo.get());
1063 }
1064 if (dPrecision < 0) {
1065 uPrecision = 0;
1066 } else if (dPrecision > 12.0) {
1067 uPrecision = 12;
1068 } else {
1069 uPrecision = (uint8_t)dPrecision;
1070 }
1071 CFX_Decimal decimalValue((FX_FLOAT)dValue, uPrecision);
1072 CFX_WideString wsValue = decimalValue;
1073 FXJSE_Value_SetUTF8String(args.GetReturnValue(),
1074 wsValue.UTF8Encode().AsStringC());
1075 } 1051 }
1076 } else { 1052 if (dPrecision < 0)
Lei Zhang 2016/06/01 20:00:42 static_cast<uint8_t>(std::min(std::max(dPrecision,
dsinclair 2016/06/02 02:16:22 Done.
1077 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, 1053 uPrecision = 0;
1078 L"Round"); 1054 else if (dPrecision > 12.0)
1055 uPrecision = 12;
1056 else
1057 uPrecision = (uint8_t)dPrecision;
1058
1059 CFX_Decimal decimalValue((FX_FLOAT)dValue, uPrecision);
1060 CFX_WideString wsValue = decimalValue;
1061 FXJSE_Value_SetUTF8String(args.GetReturnValue(),
1062 wsValue.UTF8Encode().AsStringC());
1079 } 1063 }
1080 } 1064 }
1081 1065
1082 // static 1066 // static
1083 void CXFA_FM2JSContext::Sum(CFXJSE_Value* pThis, 1067 void CXFA_FM2JSContext::Sum(CFXJSE_Value* pThis,
1084 const CFX_ByteStringC& szFuncName, 1068 const CFX_ByteStringC& szFuncName,
1085 CFXJSE_Arguments& args) { 1069 CFXJSE_Arguments& args) {
1070 int32_t argc = args.GetLength();
1071 if (argc == 0) {
1072 FXJSE_Value_SetNull(args.GetReturnValue());
1073 return;
1074 }
1075
1086 CXFA_FM2JSContext* pContext = 1076 CXFA_FM2JSContext* pContext =
1087 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); 1077 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr));
1088 v8::Isolate* pIsolate = pContext->GetScriptRuntime(); 1078 v8::Isolate* pIsolate = pContext->GetScriptRuntime();
1089 int32_t argc = args.GetLength();
1090 uint32_t uCount = 0; 1079 uint32_t uCount = 0;
1091 FX_DOUBLE dSum = 0.0; 1080 FX_DOUBLE dSum = 0.0;
1092 if (argc) { 1081 for (int32_t i = 0; i < argc; i++) {
1093 for (int32_t i = 0; i < argc; i++) { 1082 std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i);
1094 std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i); 1083 if (FXJSE_Value_IsNull(argValue.get()))
1095 if (FXJSE_Value_IsNull(argValue.get())) { 1084 continue;
1096 continue; 1085
1097 } else if (FXJSE_Value_IsArray(argValue.get())) { 1086 if (FXJSE_Value_IsArray(argValue.get())) {
1098 std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); 1087 std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate));
1099 FXJSE_Value_GetObjectProp(argValue.get(), "length", lengthValue.get()); 1088 FXJSE_Value_GetObjectProp(argValue.get(), "length", lengthValue.get());
1100 int32_t iLength = FXJSE_Value_ToInteger(lengthValue.get()); 1089 int32_t iLength = FXJSE_Value_ToInteger(lengthValue.get());
1101 if (iLength > 2) { 1090 if (iLength <= 2) {
1102 std::unique_ptr<CFXJSE_Value> propertyValue( 1091 pContext->ThrowScriptErrorMessage(XFA_IDS_ARGUMENT_MISMATCH);
1103 new CFXJSE_Value(pIsolate)); 1092 return;
dsinclair 2016/06/01 14:02:00 Added return due to throw.
1104 FXJSE_Value_GetObjectPropByIdx(argValue.get(), 1, 1093 }
1105 propertyValue.get()); 1094
1106 std::unique_ptr<CFXJSE_Value> jsObjectValue( 1095 std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate));
1107 new CFXJSE_Value(pIsolate)); 1096 FXJSE_Value_GetObjectPropByIdx(argValue.get(), 1, propertyValue.get());
1108 std::unique_ptr<CFXJSE_Value> newPropertyValue( 1097 std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate));
1109 new CFXJSE_Value(pIsolate)); 1098 std::unique_ptr<CFXJSE_Value> newPropertyValue(
1110 if (FXJSE_Value_IsNull(propertyValue.get())) { 1099 new CFXJSE_Value(pIsolate));
1111 for (int32_t j = 2; j < iLength; j++) { 1100 if (FXJSE_Value_IsNull(propertyValue.get())) {
1112 FXJSE_Value_GetObjectPropByIdx(argValue.get(), j, 1101 for (int32_t j = 2; j < iLength; j++) {
1113 jsObjectValue.get()); 1102 FXJSE_Value_GetObjectPropByIdx(argValue.get(), j,
1114 GetObjectDefaultValue(jsObjectValue.get(), 1103 jsObjectValue.get());
1115 newPropertyValue.get()); 1104 GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get());
1116 if (!FXJSE_Value_IsNull(newPropertyValue.get())) { 1105 if (FXJSE_Value_IsNull(newPropertyValue.get()))
1117 dSum += ValueToDouble(pThis, jsObjectValue.get()); 1106 continue;
1118 uCount++; 1107
1119 } 1108 dSum += ValueToDouble(pThis, jsObjectValue.get());
1120 }
1121 } else {
1122 CFX_ByteString propertyStr;
1123 FXJSE_Value_ToUTF8String(propertyValue.get(), propertyStr);
1124 for (int32_t j = 2; j < iLength; j++) {
1125 FXJSE_Value_GetObjectPropByIdx(argValue.get(), j,
1126 jsObjectValue.get());
1127 FXJSE_Value_GetObjectProp(jsObjectValue.get(),
1128 propertyStr.AsStringC(),
1129 newPropertyValue.get());
1130 if (!FXJSE_Value_IsNull(newPropertyValue.get())) {
1131 dSum += ValueToDouble(pThis, newPropertyValue.get());
1132 uCount++;
1133 }
1134 }
1135 }
1136 } else {
1137 pContext->ThrowScriptErrorMessage(XFA_IDS_ARGUMENT_MISMATCH);
1138 }
1139 } else if (FXJSE_Value_IsObject(argValue.get())) {
1140 std::unique_ptr<CFXJSE_Value> newPropertyValue(
1141 new CFXJSE_Value(pIsolate));
1142 GetObjectDefaultValue(argValue.get(), newPropertyValue.get());
1143 if (!FXJSE_Value_IsNull(newPropertyValue.get())) {
1144 dSum += ValueToDouble(pThis, argValue.get());
1145 uCount++; 1109 uCount++;
1146 } 1110 }
1147 } else { 1111 } else {
1148 dSum += ValueToDouble(pThis, argValue.get()); 1112 CFX_ByteString propertyStr;
1149 uCount++; 1113 FXJSE_Value_ToUTF8String(propertyValue.get(), propertyStr);
1114 for (int32_t j = 2; j < iLength; j++) {
1115 FXJSE_Value_GetObjectPropByIdx(argValue.get(), j,
1116 jsObjectValue.get());
1117 FXJSE_Value_GetObjectProp(jsObjectValue.get(),
1118 propertyStr.AsStringC(),
1119 newPropertyValue.get());
1120 if (FXJSE_Value_IsNull(newPropertyValue.get()))
1121 continue;
1122
1123 dSum += ValueToDouble(pThis, newPropertyValue.get());
1124 uCount++;
1125 }
1150 } 1126 }
1127 } else if (FXJSE_Value_IsObject(argValue.get())) {
1128 std::unique_ptr<CFXJSE_Value> newPropertyValue(
1129 new CFXJSE_Value(pIsolate));
1130 GetObjectDefaultValue(argValue.get(), newPropertyValue.get());
1131 if (FXJSE_Value_IsNull(newPropertyValue.get()))
1132 continue;
1133
1134 dSum += ValueToDouble(pThis, argValue.get());
1135 uCount++;
1136 } else {
1137 dSum += ValueToDouble(pThis, argValue.get());
1138 uCount++;
1151 } 1139 }
1152 } 1140 }
1153 if (uCount < 1) { 1141 if (uCount == 0) {
1154 FXJSE_Value_SetNull(args.GetReturnValue()); 1142 FXJSE_Value_SetNull(args.GetReturnValue());
1155 } else { 1143 return;
1156 FXJSE_Value_SetDouble(args.GetReturnValue(), dSum);
1157 } 1144 }
1145
1146 FXJSE_Value_SetDouble(args.GetReturnValue(), dSum);
1158 } 1147 }
1159 1148
1160 // static 1149 // static
1161 void CXFA_FM2JSContext::Date(CFXJSE_Value* pThis, 1150 void CXFA_FM2JSContext::Date(CFXJSE_Value* pThis,
1162 const CFX_ByteStringC& szFuncName, 1151 const CFX_ByteStringC& szFuncName,
1163 CFXJSE_Arguments& args) { 1152 CFXJSE_Arguments& args) {
1164 if (args.GetLength() == 0) { 1153 if (args.GetLength() != 0) {
1165 struct tm* pTmStruct = 0;
1166 time_t currentTime;
1167 time(&currentTime);
1168 pTmStruct = gmtime(&currentTime);
1169 CFX_ByteString bufferYear;
1170 CFX_ByteString bufferMon;
1171 CFX_ByteString bufferDay;
1172 bufferYear.Format("%d", pTmStruct->tm_year + 1900);
1173 bufferMon.Format("%02d", pTmStruct->tm_mon + 1);
1174 bufferDay.Format("%02d", pTmStruct->tm_mday);
1175 CFX_ByteString bufferCurrent = bufferYear + bufferMon + bufferDay;
1176 int32_t dDays = DateString2Num(bufferCurrent.AsStringC());
1177 FXJSE_Value_SetInteger(args.GetReturnValue(), dDays);
1178 } else {
1179 CXFA_FM2JSContext* pContext = 1154 CXFA_FM2JSContext* pContext =
1180 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); 1155 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr));
1181 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, 1156 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD,
1182 L"Date"); 1157 L"Date");
1158 return;
1183 } 1159 }
1160
1161 time_t currentTime;
1162 time(&currentTime);
1163 struct tm* pTmStruct = gmtime(&currentTime);
1164
1165 CFX_ByteString bufferYear;
1166 CFX_ByteString bufferMon;
1167 CFX_ByteString bufferDay;
1168 bufferYear.Format("%d", pTmStruct->tm_year + 1900);
1169 bufferMon.Format("%02d", pTmStruct->tm_mon + 1);
1170 bufferDay.Format("%02d", pTmStruct->tm_mday);
1171
1172 CFX_ByteString bufferCurrent = bufferYear + bufferMon + bufferDay;
1173 FXJSE_Value_SetInteger(args.GetReturnValue(),
1174 DateString2Num(bufferCurrent.AsStringC()));
1184 } 1175 }
1185 1176
1186 // static 1177 // static
1187 void CXFA_FM2JSContext::Date2Num(CFXJSE_Value* pThis, 1178 void CXFA_FM2JSContext::Date2Num(CFXJSE_Value* pThis,
1188 const CFX_ByteStringC& szFuncName, 1179 const CFX_ByteStringC& szFuncName,
1189 CFXJSE_Arguments& args) { 1180 CFXJSE_Arguments& args) {
1190 int32_t argc = args.GetLength(); 1181 int32_t argc = args.GetLength();
1191 if ((argc > 0) && (argc < 4)) { 1182 if ((argc <= 0) || (argc >= 4)) {
Lei Zhang 2016/06/01 20:00:43 extra parenthesis
dsinclair 2016/06/02 02:16:22 Done.
1192 FX_BOOL bFlags = FALSE;
1193 CFX_ByteString dateString;
1194 CFX_ByteString formatString;
1195 CFX_ByteString localString;
1196 std::unique_ptr<CFXJSE_Value> dateValue = GetSimpleValue(pThis, args, 0);
1197 if (ValueIsNull(pThis, dateValue.get())) {
1198 bFlags = TRUE;
1199 } else {
1200 ValueToUTF8String(dateValue.get(), dateString);
1201 }
1202 if (argc > 1) {
1203 std::unique_ptr<CFXJSE_Value> formatValue =
1204 GetSimpleValue(pThis, args, 1);
1205 if (ValueIsNull(pThis, formatValue.get())) {
1206 bFlags = TRUE;
1207 } else {
1208 ValueToUTF8String(formatValue.get(), formatString);
1209 }
1210 }
1211 if (argc == 3) {
1212 std::unique_ptr<CFXJSE_Value> localValue = GetSimpleValue(pThis, args, 2);
1213 if (ValueIsNull(pThis, localValue.get())) {
1214 bFlags = TRUE;
1215 } else {
1216 ValueToUTF8String(localValue.get(), localString);
1217 }
1218 }
1219 if (!bFlags) {
1220 CFX_ByteString szIsoDateString;
1221 FX_BOOL bRet =
1222 Local2IsoDate(pThis, dateString.AsStringC(), formatString.AsStringC(),
1223 localString.AsStringC(), szIsoDateString);
1224 if (bRet) {
1225 FXJSE_Value_SetInteger(args.GetReturnValue(),
1226 DateString2Num(szIsoDateString.AsStringC()));
1227 } else {
1228 FXJSE_Value_SetInteger(args.GetReturnValue(), 0);
1229 }
1230 } else {
1231 FXJSE_Value_SetNull(args.GetReturnValue());
1232 }
1233 } else {
1234 CXFA_FM2JSContext* pContext = 1183 CXFA_FM2JSContext* pContext =
1235 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); 1184 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr));
1236 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, 1185 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD,
1237 L"Date2Num"); 1186 L"Date2Num");
1187 return;
1238 } 1188 }
1189
1190 std::unique_ptr<CFXJSE_Value> dateValue = GetSimpleValue(pThis, args, 0);
1191 if (ValueIsNull(pThis, dateValue.get())) {
1192 FXJSE_Value_SetNull(args.GetReturnValue());
1193 return;
1194 }
1195
1196 CFX_ByteString dateString;
1197 ValueToUTF8String(dateValue.get(), dateString);
1198
1199 CFX_ByteString formatString;
1200 if (argc > 1) {
1201 std::unique_ptr<CFXJSE_Value> formatValue = GetSimpleValue(pThis, args, 1);
1202 if (ValueIsNull(pThis, formatValue.get())) {
1203 FXJSE_Value_SetNull(args.GetReturnValue());
1204 return;
1205 }
1206 ValueToUTF8String(formatValue.get(), formatString);
1207 }
1208
1209 CFX_ByteString localString;
1210 if (argc == 3) {
1211 std::unique_ptr<CFXJSE_Value> localValue = GetSimpleValue(pThis, args, 2);
1212 if (ValueIsNull(pThis, localValue.get())) {
1213 FXJSE_Value_SetNull(args.GetReturnValue());
1214 return;
1215 }
1216 ValueToUTF8String(localValue.get(), localString);
1217 }
1218
1219 CFX_ByteString szIsoDateString;
1220 if (!Local2IsoDate(pThis, dateString.AsStringC(), formatString.AsStringC(),
1221 localString.AsStringC(), szIsoDateString)) {
1222 FXJSE_Value_SetInteger(args.GetReturnValue(), 0);
1223 return;
1224 }
1225
1226 FXJSE_Value_SetInteger(args.GetReturnValue(),
1227 DateString2Num(szIsoDateString.AsStringC()));
1239 } 1228 }
1240 1229
1241 // static 1230 // static
1242 void CXFA_FM2JSContext::DateFmt(CFXJSE_Value* pThis, 1231 void CXFA_FM2JSContext::DateFmt(CFXJSE_Value* pThis,
1243 const CFX_ByteStringC& szFuncName, 1232 const CFX_ByteStringC& szFuncName,
1244 CFXJSE_Arguments& args) { 1233 CFXJSE_Arguments& args) {
1245 int32_t argc = args.GetLength(); 1234 int32_t argc = args.GetLength();
1246 if (argc < 3) { 1235 if (argc >= 3) {
1247 FX_BOOL bFlags = FALSE;
1248 int32_t iStyle = 0;
1249 CFX_ByteString szLocal;
1250 if (argc > 0) {
1251 std::unique_ptr<CFXJSE_Value> argStyle = GetSimpleValue(pThis, args, 0);
1252 if (FXJSE_Value_IsNull(argStyle.get())) {
1253 bFlags = TRUE;
1254 }
1255 iStyle = (int32_t)ValueToFloat(pThis, argStyle.get());
1256 if (iStyle > 4 || iStyle < 0) {
1257 iStyle = 0;
1258 }
1259 }
1260 if (argc == 2) {
1261 std::unique_ptr<CFXJSE_Value> argLocal = GetSimpleValue(pThis, args, 1);
1262 if (FXJSE_Value_IsNull(argLocal.get())) {
1263 bFlags = TRUE;
1264 } else {
1265 ValueToUTF8String(argLocal.get(), szLocal);
1266 }
1267 }
1268 if (!bFlags) {
1269 CFX_ByteString formatStr;
1270 GetStandardDateFormat(pThis, iStyle, szLocal.AsStringC(), formatStr);
1271 if (formatStr.IsEmpty()) {
1272 formatStr = "";
1273 }
1274 FXJSE_Value_SetUTF8String(args.GetReturnValue(), formatStr.AsStringC());
1275 } else {
1276 FXJSE_Value_SetNull(args.GetReturnValue());
1277 }
1278 } else {
1279 CXFA_FM2JSContext* pContext = 1236 CXFA_FM2JSContext* pContext =
1280 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); 1237 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr));
1281 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, 1238 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD,
1282 L"Date2Num"); 1239 L"Date2Num");
1240 return;
1283 } 1241 }
1242
1243 int32_t iStyle = 0;
1244 if (argc > 0) {
1245 std::unique_ptr<CFXJSE_Value> argStyle = GetSimpleValue(pThis, args, 0);
1246 if (FXJSE_Value_IsNull(argStyle.get())) {
1247 FXJSE_Value_SetNull(args.GetReturnValue());
1248 return;
1249 }
1250
1251 iStyle = (int32_t)ValueToFloat(pThis, argStyle.get());
1252 if (iStyle < 0 || iStyle > 4)
1253 iStyle = 0;
1254 }
1255
1256 CFX_ByteString szLocal;
1257 if (argc == 2) {
1258 std::unique_ptr<CFXJSE_Value> argLocal = GetSimpleValue(pThis, args, 1);
1259 if (FXJSE_Value_IsNull(argLocal.get())) {
1260 FXJSE_Value_SetNull(args.GetReturnValue());
1261 return;
1262 }
1263 ValueToUTF8String(argLocal.get(), szLocal);
1264 }
1265
1266 CFX_ByteString formatStr;
1267 GetStandardDateFormat(pThis, iStyle, szLocal.AsStringC(), formatStr);
1268 if (formatStr.IsEmpty())
Lei Zhang 2016/06/01 20:00:43 Is this necessary?
dsinclair 2016/06/02 02:16:22 Wasn't sure, so I left it. I don't think it makes
1269 formatStr = "";
1270
1271 FXJSE_Value_SetUTF8String(args.GetReturnValue(), formatStr.AsStringC());
1284 } 1272 }
1285 1273
1286 // static 1274 // static
1287 void CXFA_FM2JSContext::IsoDate2Num(CFXJSE_Value* pThis, 1275 void CXFA_FM2JSContext::IsoDate2Num(CFXJSE_Value* pThis,
1288 const CFX_ByteStringC& szFuncName, 1276 const CFX_ByteStringC& szFuncName,
1289 CFXJSE_Arguments& args) { 1277 CFXJSE_Arguments& args) {
1290 if (args.GetLength() == 1) { 1278 if (args.GetLength() != 1) {
1291 std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0);
1292 if (FXJSE_Value_IsNull(argOne.get())) {
1293 FXJSE_Value_SetNull(args.GetReturnValue());
1294 } else {
1295 CFX_ByteString szArgString;
1296 ValueToUTF8String(argOne.get(), szArgString);
1297 int32_t dDays = DateString2Num(szArgString.AsStringC());
1298 FXJSE_Value_SetInteger(args.GetReturnValue(), (int32_t)dDays);
1299 }
1300 } else {
1301 CXFA_FM2JSContext* pContext = 1279 CXFA_FM2JSContext* pContext =
1302 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); 1280 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr));
1303 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, 1281 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD,
1304 L"IsoDate2Num"); 1282 L"IsoDate2Num");
1283 return;
1305 } 1284 }
1285
1286 std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0);
1287 if (FXJSE_Value_IsNull(argOne.get())) {
1288 FXJSE_Value_SetNull(args.GetReturnValue());
1289 return;
1290 }
1291
1292 CFX_ByteString szArgString;
1293 ValueToUTF8String(argOne.get(), szArgString);
1294 FXJSE_Value_SetInteger(args.GetReturnValue(),
1295 DateString2Num(szArgString.AsStringC()));
1306 } 1296 }
1307 1297
1308 // static 1298 // static
1309 void CXFA_FM2JSContext::IsoTime2Num(CFXJSE_Value* pThis, 1299 void CXFA_FM2JSContext::IsoTime2Num(CFXJSE_Value* pThis,
1310 const CFX_ByteStringC& szFuncName, 1300 const CFX_ByteStringC& szFuncName,
1311 CFXJSE_Arguments& args) { 1301 CFXJSE_Arguments& args) {
1312 CXFA_FM2JSContext* pContext = 1302 CXFA_FM2JSContext* pContext =
1313 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); 1303 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr));
1314 if (args.GetLength() == 1) { 1304 if (args.GetLength() != 1) {
1315 std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0);
1316 if (ValueIsNull(pThis, argOne.get())) {
1317 FXJSE_Value_SetNull(args.GetReturnValue());
1318 } else {
1319 CXFA_Document* pDoc = pContext->GetDocument();
1320 ASSERT(pDoc);
1321 IFX_LocaleMgr* pMgr = (IFX_LocaleMgr*)pDoc->GetLocalMgr();
1322 CFX_ByteString szArgString;
1323 ValueToUTF8String(argOne.get(), szArgString);
1324 szArgString = szArgString.Mid(szArgString.Find('T', 0) + 1);
1325 if (szArgString.IsEmpty()) {
1326 FXJSE_Value_SetInteger(args.GetReturnValue(), 0);
1327 return;
1328 }
1329 CXFA_LocaleValue timeValue(
1330 XFA_VT_TIME, CFX_WideString::FromUTF8(szArgString.AsStringC()),
1331 (CXFA_LocaleMgr*)pMgr);
1332 if (timeValue.IsValid()) {
1333 CFX_Unitime uniTime = timeValue.GetTime();
1334 int32_t hour = uniTime.GetHour();
1335 int32_t min = uniTime.GetMinute();
1336 int32_t second = uniTime.GetSecond();
1337 int32_t milSecond = uniTime.GetMillisecond();
1338 IFX_Locale* pDefLocale = pMgr->GetDefLocale();
1339 ASSERT(pDefLocale);
1340 FX_TIMEZONE tzLocale;
1341 pDefLocale->GetTimeZone(tzLocale);
1342 int32_t mins = hour * 60 + min;
1343 mins -= (tzLocale.tzHour * 60);
1344 while (mins > 1440) {
1345 mins -= 1440;
1346 }
1347 while (mins < 0) {
1348 mins += 1440;
1349 }
1350 hour = mins / 60;
1351 min = mins % 60;
1352 int32_t iResult =
1353 hour * 3600000 + min * 60000 + second * 1000 + milSecond + 1;
1354 FXJSE_Value_SetInteger(args.GetReturnValue(), iResult);
1355 } else {
1356 FXJSE_Value_SetInteger(args.GetReturnValue(), 0);
1357 }
1358 }
1359 } else {
1360 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, 1305 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD,
1361 L"IsoTime2Num"); 1306 L"IsoTime2Num");
1307 return;
1362 } 1308 }
1309
1310 std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0);
1311 if (ValueIsNull(pThis, argOne.get())) {
1312 FXJSE_Value_SetNull(args.GetReturnValue());
1313 return;
1314 }
1315
1316 CXFA_Document* pDoc = pContext->GetDocument();
1317 CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr();
dsinclair 2016/06/01 14:02:00 We just casted this to CXFA_LocalMgr on the old li
1318
1319 CFX_ByteString szArgString;
1320 ValueToUTF8String(argOne.get(), szArgString);
1321 szArgString = szArgString.Mid(szArgString.Find('T', 0) + 1);
1322 if (szArgString.IsEmpty()) {
1323 FXJSE_Value_SetInteger(args.GetReturnValue(), 0);
1324 return;
1325 }
1326
1327 CXFA_LocaleValue timeValue(
1328 XFA_VT_TIME, CFX_WideString::FromUTF8(szArgString.AsStringC()), pMgr);
1329 if (!timeValue.IsValid()) {
1330 FXJSE_Value_SetInteger(args.GetReturnValue(), 0);
1331 return;
1332 }
1333
1334 CFX_Unitime uniTime = timeValue.GetTime();
1335 int32_t hour = uniTime.GetHour();
1336 int32_t min = uniTime.GetMinute();
1337 int32_t second = uniTime.GetSecond();
1338 int32_t milSecond = uniTime.GetMillisecond();
1339
1340 IFX_Locale* pDefLocale = pMgr->GetDefLocale();
Lei Zhang 2016/06/01 20:00:43 Don't bother with the extra local variable?
dsinclair 2016/06/02 02:16:22 Done.
1341 FX_TIMEZONE tzLocale;
1342 pDefLocale->GetTimeZone(tzLocale);
1343
1344 int32_t mins = hour * 60 + min;
Lei Zhang 2016/06/01 20:00:43 I wonder if we have some other time conversion cod
dsinclair 2016/06/02 02:16:22 Done.
1345 mins -= (tzLocale.tzHour * 60);
1346 while (mins > 1440)
1347 mins -= 1440;
1348 while (mins < 0)
1349 mins += 1440;
1350 hour = mins / 60;
1351 min = mins % 60;
1352 int32_t iResult =
1353 hour * 3600000 + min * 60000 + second * 1000 + milSecond + 1;
1354 FXJSE_Value_SetInteger(args.GetReturnValue(), iResult);
1363 } 1355 }
1364 1356
1365 // static 1357 // static
1366 void CXFA_FM2JSContext::LocalDateFmt(CFXJSE_Value* pThis, 1358 void CXFA_FM2JSContext::LocalDateFmt(CFXJSE_Value* pThis,
1367 const CFX_ByteStringC& szFuncName, 1359 const CFX_ByteStringC& szFuncName,
1368 CFXJSE_Arguments& args) { 1360 CFXJSE_Arguments& args) {
1369 int32_t argc = args.GetLength(); 1361 int32_t argc = args.GetLength();
1370 if (argc < 3) { 1362 if (argc < 3) {
1371 FX_BOOL bFlags = FALSE; 1363 FX_BOOL bFlags = FALSE;
1372 int32_t iStyle = 0; 1364 int32_t iStyle = 0;
(...skipping 5798 matching lines...) Expand 10 before | Expand all | Expand 10 after
7171 CFX_WideString wsFormat; 7163 CFX_WideString wsFormat;
7172 pAppProvider->LoadString(iStringID, wsFormat); 7164 pAppProvider->LoadString(iStringID, wsFormat);
7173 CFX_WideString wsMessage; 7165 CFX_WideString wsMessage;
7174 va_list arg_ptr; 7166 va_list arg_ptr;
7175 va_start(arg_ptr, iStringID); 7167 va_start(arg_ptr, iStringID);
7176 wsMessage.FormatV(wsFormat.c_str(), arg_ptr); 7168 wsMessage.FormatV(wsFormat.c_str(), arg_ptr);
7177 va_end(arg_ptr); 7169 va_end(arg_ptr);
7178 FXJSE_ThrowMessage( 7170 FXJSE_ThrowMessage(
7179 "", FX_UTF8Encode(wsMessage.c_str(), wsMessage.GetLength()).AsStringC()); 7171 "", FX_UTF8Encode(wsMessage.c_str(), wsMessage.GetLength()).AsStringC());
7180 } 7172 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698