OLD | NEW |
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 Loading... |
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; |
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++; |
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 if (args.GetLength() != 2) { |
896 if (args.GetLength() == 2) { | |
897 std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); | |
898 std::unique_ptr<CFXJSE_Value> argTwo = args.GetValue(1); | |
899 if (FXJSE_Value_IsNull(argOne.get()) || FXJSE_Value_IsNull(argTwo.get())) { | |
900 FXJSE_Value_SetNull(args.GetReturnValue()); | |
901 } else { | |
902 FX_DOUBLE dDividend = 0.0; | |
903 FX_DOUBLE dDividor = 0.0; | |
904 if (FXJSE_Value_IsArray(argOne.get())) { | |
905 std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); | |
906 FXJSE_Value_GetObjectProp(argOne.get(), "length", lengthValue.get()); | |
907 int32_t iLength = FXJSE_Value_ToInteger(lengthValue.get()); | |
908 if (iLength > 2) { | |
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 } | |
970 } else { | |
971 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, | 877 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, |
972 L"Mod"); | 878 L"Mod"); |
| 879 return; |
973 } | 880 } |
| 881 |
| 882 std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); |
| 883 std::unique_ptr<CFXJSE_Value> argTwo = args.GetValue(1); |
| 884 if (FXJSE_Value_IsNull(argOne.get()) || FXJSE_Value_IsNull(argTwo.get())) { |
| 885 FXJSE_Value_SetNull(args.GetReturnValue()); |
| 886 return; |
| 887 } |
| 888 |
| 889 bool argOneResult; |
| 890 FX_DOUBLE dDividend = ExtractDouble(pThis, argOne.get(), &argOneResult); |
| 891 bool argTwoResult; |
| 892 FX_DOUBLE dDivisor = ExtractDouble(pThis, argTwo.get(), &argTwoResult); |
| 893 if (!argOneResult || !argTwoResult) { |
| 894 pContext->ThrowScriptErrorMessage(XFA_IDS_ARGUMENT_MISMATCH); |
| 895 return; |
| 896 } |
| 897 |
| 898 if (dDivisor == 0.0) { |
| 899 pContext->ThrowScriptErrorMessage(XFA_IDS_DIVIDE_ZERO); |
| 900 return; |
| 901 } |
| 902 |
| 903 FXJSE_Value_SetDouble(args.GetReturnValue(), |
| 904 dDividend - dDivisor * (int32_t)(dDividend / dDivisor)); |
974 } | 905 } |
975 | 906 |
976 // static | 907 // static |
977 void CXFA_FM2JSContext::Round(CFXJSE_Value* pThis, | 908 void CXFA_FM2JSContext::Round(CFXJSE_Value* pThis, |
978 const CFX_ByteStringC& szFuncName, | 909 const CFX_ByteStringC& szFuncName, |
979 CFXJSE_Arguments& args) { | 910 CFXJSE_Arguments& args) { |
980 CXFA_FM2JSContext* pContext = | 911 CXFA_FM2JSContext* pContext = |
981 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); | 912 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); |
982 v8::Isolate* pIsolate = pContext->GetScriptRuntime(); | |
983 int32_t argc = args.GetLength(); | 913 int32_t argc = args.GetLength(); |
984 uint8_t uPrecision = 0; | 914 if (argc != 1 && argc != 2) { |
985 if (argc == 1) { | |
986 std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); | |
987 if (FXJSE_Value_IsNull(argOne.get())) { | |
988 FXJSE_Value_SetNull(args.GetReturnValue()); | |
989 } else { | |
990 FX_DOUBLE dValue = 0.0; | |
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 } | |
1016 } else if (argc == 2) { | |
1017 std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); | |
1018 std::unique_ptr<CFXJSE_Value> argTwo = args.GetValue(1); | |
1019 if (FXJSE_Value_IsNull(argOne.get()) || FXJSE_Value_IsNull(argTwo.get())) { | |
1020 FXJSE_Value_SetNull(args.GetReturnValue()); | |
1021 } else { | |
1022 FX_DOUBLE dValue = 0.0; | |
1023 if (FXJSE_Value_IsArray(argOne.get())) { | |
1024 std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); | |
1025 std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); | |
1026 FXJSE_Value_GetObjectPropByIdx(argOne.get(), 1, propertyValue.get()); | |
1027 FXJSE_Value_GetObjectPropByIdx(argOne.get(), 2, jsObjectValue.get()); | |
1028 if (FXJSE_Value_IsNull(propertyValue.get())) { | |
1029 dValue = ValueToDouble(pThis, jsObjectValue.get()); | |
1030 } else { | |
1031 CFX_ByteString propertyStr; | |
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 { | |
1041 dValue = ValueToDouble(pThis, argOne.get()); | |
1042 } | |
1043 FX_DOUBLE dPrecision = 0.0; | |
1044 if (FXJSE_Value_IsArray(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 } | |
1076 } else { | |
1077 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, | 915 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, |
1078 L"Round"); | 916 L"Round"); |
| 917 return; |
1079 } | 918 } |
| 919 |
| 920 std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); |
| 921 if (FXJSE_Value_IsNull(argOne.get())) { |
| 922 FXJSE_Value_SetNull(args.GetReturnValue()); |
| 923 return; |
| 924 } |
| 925 |
| 926 bool dValueRet; |
| 927 FX_DOUBLE dValue = ExtractDouble(pThis, argOne.get(), &dValueRet); |
| 928 if (!dValueRet) { |
| 929 pContext->ThrowScriptErrorMessage(XFA_IDS_ARGUMENT_MISMATCH); |
| 930 return; |
| 931 } |
| 932 |
| 933 uint8_t uPrecision = 0; |
| 934 if (argc == 2) { |
| 935 std::unique_ptr<CFXJSE_Value> argTwo = args.GetValue(1); |
| 936 if (FXJSE_Value_IsNull(argTwo.get())) { |
| 937 FXJSE_Value_SetNull(args.GetReturnValue()); |
| 938 return; |
| 939 } |
| 940 |
| 941 bool dPrecisionRet; |
| 942 FX_DOUBLE dPrecision = ExtractDouble(pThis, argTwo.get(), &dPrecisionRet); |
| 943 if (!dPrecisionRet) { |
| 944 pContext->ThrowScriptErrorMessage(XFA_IDS_ARGUMENT_MISMATCH); |
| 945 return; |
| 946 } |
| 947 |
| 948 uPrecision = |
| 949 static_cast<uint8_t>(std::min(std::max(dPrecision, 0.0), 12.0)); |
| 950 } |
| 951 |
| 952 CFX_Decimal decimalValue((FX_FLOAT)dValue, uPrecision); |
| 953 CFX_WideString wsValue = decimalValue; |
| 954 FXJSE_Value_SetUTF8String(args.GetReturnValue(), |
| 955 wsValue.UTF8Encode().AsStringC()); |
1080 } | 956 } |
1081 | 957 |
1082 // static | 958 // static |
1083 void CXFA_FM2JSContext::Sum(CFXJSE_Value* pThis, | 959 void CXFA_FM2JSContext::Sum(CFXJSE_Value* pThis, |
1084 const CFX_ByteStringC& szFuncName, | 960 const CFX_ByteStringC& szFuncName, |
1085 CFXJSE_Arguments& args) { | 961 CFXJSE_Arguments& args) { |
| 962 int32_t argc = args.GetLength(); |
| 963 if (argc == 0) { |
| 964 FXJSE_Value_SetNull(args.GetReturnValue()); |
| 965 return; |
| 966 } |
| 967 |
1086 CXFA_FM2JSContext* pContext = | 968 CXFA_FM2JSContext* pContext = |
1087 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); | 969 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); |
1088 v8::Isolate* pIsolate = pContext->GetScriptRuntime(); | 970 v8::Isolate* pIsolate = pContext->GetScriptRuntime(); |
1089 int32_t argc = args.GetLength(); | |
1090 uint32_t uCount = 0; | 971 uint32_t uCount = 0; |
1091 FX_DOUBLE dSum = 0.0; | 972 FX_DOUBLE dSum = 0.0; |
1092 if (argc) { | 973 for (int32_t i = 0; i < argc; i++) { |
1093 for (int32_t i = 0; i < argc; i++) { | 974 std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i); |
1094 std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i); | 975 if (FXJSE_Value_IsNull(argValue.get())) |
1095 if (FXJSE_Value_IsNull(argValue.get())) { | 976 continue; |
1096 continue; | 977 |
1097 } else if (FXJSE_Value_IsArray(argValue.get())) { | 978 if (FXJSE_Value_IsArray(argValue.get())) { |
1098 std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); | 979 std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); |
1099 FXJSE_Value_GetObjectProp(argValue.get(), "length", lengthValue.get()); | 980 FXJSE_Value_GetObjectProp(argValue.get(), "length", lengthValue.get()); |
1100 int32_t iLength = FXJSE_Value_ToInteger(lengthValue.get()); | 981 int32_t iLength = FXJSE_Value_ToInteger(lengthValue.get()); |
1101 if (iLength > 2) { | 982 if (iLength <= 2) { |
1102 std::unique_ptr<CFXJSE_Value> propertyValue( | 983 pContext->ThrowScriptErrorMessage(XFA_IDS_ARGUMENT_MISMATCH); |
1103 new CFXJSE_Value(pIsolate)); | 984 return; |
1104 FXJSE_Value_GetObjectPropByIdx(argValue.get(), 1, | 985 } |
1105 propertyValue.get()); | 986 |
1106 std::unique_ptr<CFXJSE_Value> jsObjectValue( | 987 std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); |
1107 new CFXJSE_Value(pIsolate)); | 988 FXJSE_Value_GetObjectPropByIdx(argValue.get(), 1, propertyValue.get()); |
1108 std::unique_ptr<CFXJSE_Value> newPropertyValue( | 989 std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); |
1109 new CFXJSE_Value(pIsolate)); | 990 std::unique_ptr<CFXJSE_Value> newPropertyValue( |
1110 if (FXJSE_Value_IsNull(propertyValue.get())) { | 991 new CFXJSE_Value(pIsolate)); |
1111 for (int32_t j = 2; j < iLength; j++) { | 992 if (FXJSE_Value_IsNull(propertyValue.get())) { |
1112 FXJSE_Value_GetObjectPropByIdx(argValue.get(), j, | 993 for (int32_t j = 2; j < iLength; j++) { |
1113 jsObjectValue.get()); | 994 FXJSE_Value_GetObjectPropByIdx(argValue.get(), j, |
1114 GetObjectDefaultValue(jsObjectValue.get(), | 995 jsObjectValue.get()); |
1115 newPropertyValue.get()); | 996 GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get()); |
1116 if (!FXJSE_Value_IsNull(newPropertyValue.get())) { | 997 if (FXJSE_Value_IsNull(newPropertyValue.get())) |
1117 dSum += ValueToDouble(pThis, jsObjectValue.get()); | 998 continue; |
1118 uCount++; | 999 |
1119 } | 1000 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++; | 1001 uCount++; |
1146 } | 1002 } |
1147 } else { | 1003 } else { |
1148 dSum += ValueToDouble(pThis, argValue.get()); | 1004 CFX_ByteString propertyStr; |
1149 uCount++; | 1005 FXJSE_Value_ToUTF8String(propertyValue.get(), propertyStr); |
| 1006 for (int32_t j = 2; j < iLength; j++) { |
| 1007 FXJSE_Value_GetObjectPropByIdx(argValue.get(), j, |
| 1008 jsObjectValue.get()); |
| 1009 FXJSE_Value_GetObjectProp(jsObjectValue.get(), |
| 1010 propertyStr.AsStringC(), |
| 1011 newPropertyValue.get()); |
| 1012 if (FXJSE_Value_IsNull(newPropertyValue.get())) |
| 1013 continue; |
| 1014 |
| 1015 dSum += ValueToDouble(pThis, newPropertyValue.get()); |
| 1016 uCount++; |
| 1017 } |
1150 } | 1018 } |
| 1019 } else if (FXJSE_Value_IsObject(argValue.get())) { |
| 1020 std::unique_ptr<CFXJSE_Value> newPropertyValue( |
| 1021 new CFXJSE_Value(pIsolate)); |
| 1022 GetObjectDefaultValue(argValue.get(), newPropertyValue.get()); |
| 1023 if (FXJSE_Value_IsNull(newPropertyValue.get())) |
| 1024 continue; |
| 1025 |
| 1026 dSum += ValueToDouble(pThis, argValue.get()); |
| 1027 uCount++; |
| 1028 } else { |
| 1029 dSum += ValueToDouble(pThis, argValue.get()); |
| 1030 uCount++; |
1151 } | 1031 } |
1152 } | 1032 } |
1153 if (uCount < 1) { | 1033 if (uCount == 0) { |
1154 FXJSE_Value_SetNull(args.GetReturnValue()); | 1034 FXJSE_Value_SetNull(args.GetReturnValue()); |
1155 } else { | 1035 return; |
1156 FXJSE_Value_SetDouble(args.GetReturnValue(), dSum); | |
1157 } | 1036 } |
| 1037 |
| 1038 FXJSE_Value_SetDouble(args.GetReturnValue(), dSum); |
1158 } | 1039 } |
1159 | 1040 |
1160 // static | 1041 // static |
1161 void CXFA_FM2JSContext::Date(CFXJSE_Value* pThis, | 1042 void CXFA_FM2JSContext::Date(CFXJSE_Value* pThis, |
1162 const CFX_ByteStringC& szFuncName, | 1043 const CFX_ByteStringC& szFuncName, |
1163 CFXJSE_Arguments& args) { | 1044 CFXJSE_Arguments& args) { |
1164 if (args.GetLength() == 0) { | 1045 if (args.GetLength() != 0) { |
1165 struct tm* pTmStruct = 0; | |
1166 time_t currentTime; | |
1167 time(¤tTime); | |
1168 pTmStruct = gmtime(¤tTime); | |
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 = | 1046 CXFA_FM2JSContext* pContext = |
1180 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); | 1047 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); |
1181 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, | 1048 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, |
1182 L"Date"); | 1049 L"Date"); |
| 1050 return; |
1183 } | 1051 } |
| 1052 |
| 1053 time_t currentTime; |
| 1054 time(¤tTime); |
| 1055 struct tm* pTmStruct = gmtime(¤tTime); |
| 1056 |
| 1057 CFX_ByteString bufferYear; |
| 1058 CFX_ByteString bufferMon; |
| 1059 CFX_ByteString bufferDay; |
| 1060 bufferYear.Format("%d", pTmStruct->tm_year + 1900); |
| 1061 bufferMon.Format("%02d", pTmStruct->tm_mon + 1); |
| 1062 bufferDay.Format("%02d", pTmStruct->tm_mday); |
| 1063 |
| 1064 CFX_ByteString bufferCurrent = bufferYear + bufferMon + bufferDay; |
| 1065 FXJSE_Value_SetInteger(args.GetReturnValue(), |
| 1066 DateString2Num(bufferCurrent.AsStringC())); |
1184 } | 1067 } |
1185 | 1068 |
1186 // static | 1069 // static |
1187 void CXFA_FM2JSContext::Date2Num(CFXJSE_Value* pThis, | 1070 void CXFA_FM2JSContext::Date2Num(CFXJSE_Value* pThis, |
1188 const CFX_ByteStringC& szFuncName, | 1071 const CFX_ByteStringC& szFuncName, |
1189 CFXJSE_Arguments& args) { | 1072 CFXJSE_Arguments& args) { |
1190 int32_t argc = args.GetLength(); | 1073 int32_t argc = args.GetLength(); |
1191 if ((argc > 0) && (argc < 4)) { | 1074 if (argc <= 0 || argc >= 4) { |
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 = | 1075 CXFA_FM2JSContext* pContext = |
1235 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); | 1076 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); |
1236 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, | 1077 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, |
1237 L"Date2Num"); | 1078 L"Date2Num"); |
| 1079 return; |
1238 } | 1080 } |
| 1081 |
| 1082 std::unique_ptr<CFXJSE_Value> dateValue = GetSimpleValue(pThis, args, 0); |
| 1083 if (ValueIsNull(pThis, dateValue.get())) { |
| 1084 FXJSE_Value_SetNull(args.GetReturnValue()); |
| 1085 return; |
| 1086 } |
| 1087 |
| 1088 CFX_ByteString dateString; |
| 1089 ValueToUTF8String(dateValue.get(), dateString); |
| 1090 |
| 1091 CFX_ByteString formatString; |
| 1092 if (argc > 1) { |
| 1093 std::unique_ptr<CFXJSE_Value> formatValue = GetSimpleValue(pThis, args, 1); |
| 1094 if (ValueIsNull(pThis, formatValue.get())) { |
| 1095 FXJSE_Value_SetNull(args.GetReturnValue()); |
| 1096 return; |
| 1097 } |
| 1098 ValueToUTF8String(formatValue.get(), formatString); |
| 1099 } |
| 1100 |
| 1101 CFX_ByteString localString; |
| 1102 if (argc == 3) { |
| 1103 std::unique_ptr<CFXJSE_Value> localValue = GetSimpleValue(pThis, args, 2); |
| 1104 if (ValueIsNull(pThis, localValue.get())) { |
| 1105 FXJSE_Value_SetNull(args.GetReturnValue()); |
| 1106 return; |
| 1107 } |
| 1108 ValueToUTF8String(localValue.get(), localString); |
| 1109 } |
| 1110 |
| 1111 CFX_ByteString szIsoDateString; |
| 1112 if (!Local2IsoDate(pThis, dateString.AsStringC(), formatString.AsStringC(), |
| 1113 localString.AsStringC(), szIsoDateString)) { |
| 1114 FXJSE_Value_SetInteger(args.GetReturnValue(), 0); |
| 1115 return; |
| 1116 } |
| 1117 |
| 1118 FXJSE_Value_SetInteger(args.GetReturnValue(), |
| 1119 DateString2Num(szIsoDateString.AsStringC())); |
1239 } | 1120 } |
1240 | 1121 |
1241 // static | 1122 // static |
1242 void CXFA_FM2JSContext::DateFmt(CFXJSE_Value* pThis, | 1123 void CXFA_FM2JSContext::DateFmt(CFXJSE_Value* pThis, |
1243 const CFX_ByteStringC& szFuncName, | 1124 const CFX_ByteStringC& szFuncName, |
1244 CFXJSE_Arguments& args) { | 1125 CFXJSE_Arguments& args) { |
1245 int32_t argc = args.GetLength(); | 1126 int32_t argc = args.GetLength(); |
1246 if (argc < 3) { | 1127 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 = | 1128 CXFA_FM2JSContext* pContext = |
1280 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); | 1129 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); |
1281 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, | 1130 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, |
1282 L"Date2Num"); | 1131 L"Date2Num"); |
| 1132 return; |
1283 } | 1133 } |
| 1134 |
| 1135 int32_t iStyle = 0; |
| 1136 if (argc > 0) { |
| 1137 std::unique_ptr<CFXJSE_Value> argStyle = GetSimpleValue(pThis, args, 0); |
| 1138 if (FXJSE_Value_IsNull(argStyle.get())) { |
| 1139 FXJSE_Value_SetNull(args.GetReturnValue()); |
| 1140 return; |
| 1141 } |
| 1142 |
| 1143 iStyle = (int32_t)ValueToFloat(pThis, argStyle.get()); |
| 1144 if (iStyle < 0 || iStyle > 4) |
| 1145 iStyle = 0; |
| 1146 } |
| 1147 |
| 1148 CFX_ByteString szLocal; |
| 1149 if (argc == 2) { |
| 1150 std::unique_ptr<CFXJSE_Value> argLocal = GetSimpleValue(pThis, args, 1); |
| 1151 if (FXJSE_Value_IsNull(argLocal.get())) { |
| 1152 FXJSE_Value_SetNull(args.GetReturnValue()); |
| 1153 return; |
| 1154 } |
| 1155 ValueToUTF8String(argLocal.get(), szLocal); |
| 1156 } |
| 1157 |
| 1158 CFX_ByteString formatStr; |
| 1159 GetStandardDateFormat(pThis, iStyle, szLocal.AsStringC(), formatStr); |
| 1160 FXJSE_Value_SetUTF8String(args.GetReturnValue(), formatStr.AsStringC()); |
1284 } | 1161 } |
1285 | 1162 |
1286 // static | 1163 // static |
1287 void CXFA_FM2JSContext::IsoDate2Num(CFXJSE_Value* pThis, | 1164 void CXFA_FM2JSContext::IsoDate2Num(CFXJSE_Value* pThis, |
1288 const CFX_ByteStringC& szFuncName, | 1165 const CFX_ByteStringC& szFuncName, |
1289 CFXJSE_Arguments& args) { | 1166 CFXJSE_Arguments& args) { |
1290 if (args.GetLength() == 1) { | 1167 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 = | 1168 CXFA_FM2JSContext* pContext = |
1302 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); | 1169 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); |
1303 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, | 1170 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, |
1304 L"IsoDate2Num"); | 1171 L"IsoDate2Num"); |
| 1172 return; |
1305 } | 1173 } |
| 1174 |
| 1175 std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 1176 if (FXJSE_Value_IsNull(argOne.get())) { |
| 1177 FXJSE_Value_SetNull(args.GetReturnValue()); |
| 1178 return; |
| 1179 } |
| 1180 |
| 1181 CFX_ByteString szArgString; |
| 1182 ValueToUTF8String(argOne.get(), szArgString); |
| 1183 FXJSE_Value_SetInteger(args.GetReturnValue(), |
| 1184 DateString2Num(szArgString.AsStringC())); |
1306 } | 1185 } |
1307 | 1186 |
1308 // static | 1187 // static |
1309 void CXFA_FM2JSContext::IsoTime2Num(CFXJSE_Value* pThis, | 1188 void CXFA_FM2JSContext::IsoTime2Num(CFXJSE_Value* pThis, |
1310 const CFX_ByteStringC& szFuncName, | 1189 const CFX_ByteStringC& szFuncName, |
1311 CFXJSE_Arguments& args) { | 1190 CFXJSE_Arguments& args) { |
1312 CXFA_FM2JSContext* pContext = | 1191 CXFA_FM2JSContext* pContext = |
1313 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); | 1192 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); |
1314 if (args.GetLength() == 1) { | 1193 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, | 1194 pContext->ThrowScriptErrorMessage(XFA_IDS_INCORRECT_NUMBER_OF_METHOD, |
1361 L"IsoTime2Num"); | 1195 L"IsoTime2Num"); |
| 1196 return; |
1362 } | 1197 } |
| 1198 |
| 1199 std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); |
| 1200 if (ValueIsNull(pThis, argOne.get())) { |
| 1201 FXJSE_Value_SetNull(args.GetReturnValue()); |
| 1202 return; |
| 1203 } |
| 1204 |
| 1205 CXFA_Document* pDoc = pContext->GetDocument(); |
| 1206 CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); |
| 1207 |
| 1208 CFX_ByteString szArgString; |
| 1209 ValueToUTF8String(argOne.get(), szArgString); |
| 1210 szArgString = szArgString.Mid(szArgString.Find('T', 0) + 1); |
| 1211 if (szArgString.IsEmpty()) { |
| 1212 FXJSE_Value_SetInteger(args.GetReturnValue(), 0); |
| 1213 return; |
| 1214 } |
| 1215 |
| 1216 CXFA_LocaleValue timeValue( |
| 1217 XFA_VT_TIME, CFX_WideString::FromUTF8(szArgString.AsStringC()), pMgr); |
| 1218 if (!timeValue.IsValid()) { |
| 1219 FXJSE_Value_SetInteger(args.GetReturnValue(), 0); |
| 1220 return; |
| 1221 } |
| 1222 |
| 1223 CFX_Unitime uniTime = timeValue.GetTime(); |
| 1224 int32_t hour = uniTime.GetHour(); |
| 1225 int32_t min = uniTime.GetMinute(); |
| 1226 int32_t second = uniTime.GetSecond(); |
| 1227 int32_t milSecond = uniTime.GetMillisecond(); |
| 1228 |
| 1229 FX_TIMEZONE tzLocale; |
| 1230 pMgr->GetDefLocale()->GetTimeZone(tzLocale); |
| 1231 |
| 1232 // TODO(dsinclair): See if there is other time conversion code in pdfium and |
| 1233 // consolidate. |
| 1234 int32_t mins = hour * 60 + min; |
| 1235 mins -= (tzLocale.tzHour * 60); |
| 1236 while (mins > 1440) |
| 1237 mins -= 1440; |
| 1238 while (mins < 0) |
| 1239 mins += 1440; |
| 1240 hour = mins / 60; |
| 1241 min = mins % 60; |
| 1242 int32_t iResult = |
| 1243 hour * 3600000 + min * 60000 + second * 1000 + milSecond + 1; |
| 1244 FXJSE_Value_SetInteger(args.GetReturnValue(), iResult); |
1363 } | 1245 } |
1364 | 1246 |
1365 // static | 1247 // static |
1366 void CXFA_FM2JSContext::LocalDateFmt(CFXJSE_Value* pThis, | 1248 void CXFA_FM2JSContext::LocalDateFmt(CFXJSE_Value* pThis, |
1367 const CFX_ByteStringC& szFuncName, | 1249 const CFX_ByteStringC& szFuncName, |
1368 CFXJSE_Arguments& args) { | 1250 CFXJSE_Arguments& args) { |
1369 int32_t argc = args.GetLength(); | 1251 int32_t argc = args.GetLength(); |
1370 if (argc < 3) { | 1252 if (argc < 3) { |
1371 FX_BOOL bFlags = FALSE; | 1253 FX_BOOL bFlags = FALSE; |
1372 int32_t iStyle = 0; | 1254 int32_t iStyle = 0; |
(...skipping 5730 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7103 FXJSE_Value_ToUTF8String(arg, bsOutput); | 6985 FXJSE_Value_ToUTF8String(arg, bsOutput); |
7104 dRet = XFA_ByteStringToDouble(bsOutput.AsStringC()); | 6986 dRet = XFA_ByteStringToDouble(bsOutput.AsStringC()); |
7105 } else if (FXJSE_Value_IsUndefined(arg)) { | 6987 } else if (FXJSE_Value_IsUndefined(arg)) { |
7106 dRet = 0; | 6988 dRet = 0; |
7107 } else { | 6989 } else { |
7108 dRet = FXJSE_Value_ToDouble(arg); | 6990 dRet = FXJSE_Value_ToDouble(arg); |
7109 } | 6991 } |
7110 return dRet; | 6992 return dRet; |
7111 } | 6993 } |
7112 | 6994 |
| 6995 // static. |
| 6996 double CXFA_FM2JSContext::ExtractDouble(CFXJSE_Value* pThis, |
| 6997 CFXJSE_Value* src, |
| 6998 bool* ret) { |
| 6999 ASSERT(ret); |
| 7000 |
| 7001 CXFA_FM2JSContext* pContext = |
| 7002 static_cast<CXFA_FM2JSContext*>(FXJSE_Value_ToObject(pThis, nullptr)); |
| 7003 v8::Isolate* pIsolate = pContext->GetScriptRuntime(); |
| 7004 |
| 7005 *ret = true; |
| 7006 |
| 7007 if (FXJSE_Value_IsArray(src)) { |
| 7008 std::unique_ptr<CFXJSE_Value> lengthValue(new CFXJSE_Value(pIsolate)); |
| 7009 FXJSE_Value_GetObjectProp(src, "length", lengthValue.get()); |
| 7010 int32_t iLength = FXJSE_Value_ToInteger(lengthValue.get()); |
| 7011 if (iLength <= 2) { |
| 7012 *ret = false; |
| 7013 return 0.0; |
| 7014 } |
| 7015 |
| 7016 std::unique_ptr<CFXJSE_Value> propertyValue(new CFXJSE_Value(pIsolate)); |
| 7017 std::unique_ptr<CFXJSE_Value> jsObjectValue(new CFXJSE_Value(pIsolate)); |
| 7018 FXJSE_Value_GetObjectPropByIdx(src, 1, propertyValue.get()); |
| 7019 FXJSE_Value_GetObjectPropByIdx(src, 2, jsObjectValue.get()); |
| 7020 if (FXJSE_Value_IsNull(propertyValue.get())) |
| 7021 return ValueToDouble(pThis, jsObjectValue.get()); |
| 7022 |
| 7023 CFX_ByteString propertyStr; |
| 7024 FXJSE_Value_ToUTF8String(propertyValue.get(), propertyStr); |
| 7025 std::unique_ptr<CFXJSE_Value> newPropertyValue(new CFXJSE_Value(pIsolate)); |
| 7026 FXJSE_Value_GetObjectProp(jsObjectValue.get(), propertyStr.AsStringC(), |
| 7027 newPropertyValue.get()); |
| 7028 return ValueToDouble(pThis, newPropertyValue.get()); |
| 7029 } |
| 7030 return ValueToDouble(pThis, src); |
| 7031 } |
| 7032 |
7113 // static | 7033 // static |
7114 void CXFA_FM2JSContext::ValueToUTF8String(CFXJSE_Value* arg, | 7034 void CXFA_FM2JSContext::ValueToUTF8String(CFXJSE_Value* arg, |
7115 CFX_ByteString& szOutputString) { | 7035 CFX_ByteString& szOutputString) { |
7116 if (FXJSE_Value_IsNull(arg) || FXJSE_Value_IsUndefined(arg)) { | 7036 if (FXJSE_Value_IsNull(arg) || FXJSE_Value_IsUndefined(arg)) { |
7117 szOutputString = ""; | 7037 szOutputString = ""; |
7118 } else if (FXJSE_Value_IsBoolean(arg)) { | 7038 } else if (FXJSE_Value_IsBoolean(arg)) { |
7119 szOutputString = FXJSE_Value_ToBoolean(arg) ? "1" : "0"; | 7039 szOutputString = FXJSE_Value_ToBoolean(arg) ? "1" : "0"; |
7120 } else { | 7040 } else { |
7121 szOutputString = ""; | 7041 szOutputString = ""; |
7122 FXJSE_Value_ToUTF8String(arg, szOutputString); | 7042 FXJSE_Value_ToUTF8String(arg, szOutputString); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7171 CFX_WideString wsFormat; | 7091 CFX_WideString wsFormat; |
7172 pAppProvider->LoadString(iStringID, wsFormat); | 7092 pAppProvider->LoadString(iStringID, wsFormat); |
7173 CFX_WideString wsMessage; | 7093 CFX_WideString wsMessage; |
7174 va_list arg_ptr; | 7094 va_list arg_ptr; |
7175 va_start(arg_ptr, iStringID); | 7095 va_start(arg_ptr, iStringID); |
7176 wsMessage.FormatV(wsFormat.c_str(), arg_ptr); | 7096 wsMessage.FormatV(wsFormat.c_str(), arg_ptr); |
7177 va_end(arg_ptr); | 7097 va_end(arg_ptr); |
7178 FXJSE_ThrowMessage( | 7098 FXJSE_ThrowMessage( |
7179 "", FX_UTF8Encode(wsMessage.c_str(), wsMessage.GetLength()).AsStringC()); | 7099 "", FX_UTF8Encode(wsMessage.c_str(), wsMessage.GetLength()).AsStringC()); |
7180 } | 7100 } |
OLD | NEW |