diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2017-11-01 13:12:39 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-11-01 13:12:39 +0000 |
commit | 3fff90a670d860a7b0319aa0edf8628917d0a122 (patch) | |
tree | cd41a597e4a400dfa7ced5eea330fdf9f5ca832f /xfa/fxfa | |
parent | 994f20cfb76f4902491a94c4ef61f55705fc124d (diff) | |
download | pdfium-3fff90a670d860a7b0319aa0edf8628917d0a122.tar.xz |
Move some XFA JS code into fxjs/
This CL renames cxfa_scriptcontext to cfxjse_engine and
cxfa_fm2jscontext to cfxjse_formcalc_context.
From reading the code, the script context appears to handle the v8 setup
and object code. The formcalc context code is related to handling the JS
code generated from the transpiler.
I, think, these new names make the intended usage clearer. They also
move the code into fxjs/ to keep along side the rest of the JS code.
Change-Id: I50619fbe48ca1f553a44cf0e0cb0210be8e45e4f
Reviewed-on: https://pdfium-review.googlesource.com/17130
Commit-Queue: dsinclair <dsinclair@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'xfa/fxfa')
24 files changed, 55 insertions, 9011 deletions
diff --git a/xfa/fxfa/cxfa_ffdochandler.cpp b/xfa/fxfa/cxfa_ffdochandler.cpp index 8dbc55050a..11e5b47e7c 100644 --- a/xfa/fxfa/cxfa_ffdochandler.cpp +++ b/xfa/fxfa/cxfa_ffdochandler.cpp @@ -7,9 +7,9 @@ #include "xfa/fxfa/cxfa_ffdochandler.h" #include "core/fxcrt/cfx_checksumcontext.h" +#include "fxjs/cfxjse_engine.h" #include "xfa/fxfa/cxfa_ffdoc.h" #include "xfa/fxfa/parser/cxfa_node.h" -#include "xfa/fxfa/parser/cxfa_scriptcontext.h" #include "xfa/fxfa/parser/xfa_resolvenode_rs.h" CXFA_FFDocHandler::CXFA_FFDocHandler() {} @@ -21,7 +21,7 @@ CFXJSE_Value* CXFA_FFDocHandler::GetXFAScriptObject(CXFA_FFDoc* hDoc) { if (!pXFADoc) return nullptr; - CXFA_ScriptContext* pScriptContext = pXFADoc->GetScriptContext(); + CFXJSE_Engine* pScriptContext = pXFADoc->GetScriptContext(); if (!pScriptContext) return nullptr; return pScriptContext->GetJSValueFromMap(pXFADoc->GetRoot()); @@ -51,11 +51,11 @@ bool CXFA_FFDocHandler::RunDocScript(CXFA_FFDoc* hDoc, if (!pXFADoc) return false; - CXFA_ScriptContext* pScriptContext = pXFADoc->GetScriptContext(); + CFXJSE_Engine* pScriptContext = pXFADoc->GetScriptContext(); if (!pScriptContext) return false; return pScriptContext->RunScript( (XFA_SCRIPTLANGTYPE)eScriptType, wsScript, pRetValue, - pThisValue ? CXFA_ScriptContext::ToObject(pThisValue, nullptr) : nullptr); + pThisValue ? CFXJSE_Engine::ToObject(pThisValue, nullptr) : nullptr); } diff --git a/xfa/fxfa/cxfa_ffdocview.cpp b/xfa/fxfa/cxfa_ffdocview.cpp index 0bf45e919a..603b752d25 100644 --- a/xfa/fxfa/cxfa_ffdocview.cpp +++ b/xfa/fxfa/cxfa_ffdocview.cpp @@ -7,6 +7,7 @@ #include "xfa/fxfa/cxfa_ffdocview.h" #include "core/fxcrt/fx_extension.h" +#include "fxjs/cfxjse_engine.h" #include "third_party/base/ptr_util.h" #include "third_party/base/stl_util.h" #include "xfa/fxfa/cxfa_ffapp.h" @@ -30,7 +31,6 @@ #include "xfa/fxfa/cxfa_widgetacciterator.h" #include "xfa/fxfa/parser/cxfa_binditems.h" #include "xfa/fxfa/parser/cxfa_layoutprocessor.h" -#include "xfa/fxfa/parser/cxfa_scriptcontext.h" #include "xfa/fxfa/parser/xfa_resolvenode_rs.h" #define XFA_CalcRefCount (void*)(uintptr_t) FXBSTR_ID('X', 'F', 'A', 'R') @@ -491,7 +491,7 @@ CXFA_WidgetAcc* CXFA_FFDocView::GetWidgetAccByName( WideString wsExpression; uint32_t dwStyle = XFA_RESOLVENODE_Children | XFA_RESOLVENODE_Properties | XFA_RESOLVENODE_Siblings | XFA_RESOLVENODE_Parent; - CXFA_ScriptContext* pScriptContext = m_pDoc->GetXFADoc()->GetScriptContext(); + CFXJSE_Engine* pScriptContext = m_pDoc->GetXFADoc()->GetScriptContext(); if (!pScriptContext) return nullptr; @@ -716,7 +716,7 @@ void CXFA_FFDocView::RunBindItems() { continue; CXFA_BindItems binditems(item); - CXFA_ScriptContext* pScriptContext = + CFXJSE_Engine* pScriptContext = pWidgetNode->GetDocument()->GetScriptContext(); WideStringView wsRef; binditems.GetRef(wsRef); diff --git a/xfa/fxfa/cxfa_textprovider.cpp b/xfa/fxfa/cxfa_textprovider.cpp index 9f1ecd51c3..78decf8f06 100644 --- a/xfa/fxfa/cxfa_textprovider.cpp +++ b/xfa/fxfa/cxfa_textprovider.cpp @@ -12,6 +12,7 @@ #include "core/fxcrt/xml/cfx_xmlelement.h" #include "core/fxcrt/xml/cfx_xmlnode.h" +#include "fxjs/cfxjse_engine.h" #include "fxjs/cfxjse_value.h" #include "third_party/base/ptr_util.h" #include "third_party/base/stl_util.h" @@ -29,7 +30,6 @@ #include "xfa/fxfa/parser/cxfa_layoutprocessor.h" #include "xfa/fxfa/parser/cxfa_localevalue.h" #include "xfa/fxfa/parser/cxfa_node.h" -#include "xfa/fxfa/parser/cxfa_scriptcontext.h" #include "xfa/fxfa/parser/xfa_resolvenode_rs.h" CXFA_Node* CXFA_TextProvider::GetTextNode(bool& bRichText) { diff --git a/xfa/fxfa/cxfa_widgetacc.cpp b/xfa/fxfa/cxfa_widgetacc.cpp index 82eec9e16d..ddada38722 100644 --- a/xfa/fxfa/cxfa_widgetacc.cpp +++ b/xfa/fxfa/cxfa_widgetacc.cpp @@ -11,6 +11,7 @@ #include "core/fxcrt/xml/cfx_xmlelement.h" #include "core/fxcrt/xml/cfx_xmlnode.h" +#include "fxjs/cfxjse_engine.h" #include "third_party/base/stl_util.h" #include "xfa/fde/cfde_textout.h" #include "xfa/fxfa/cxfa_ffapp.h" @@ -23,7 +24,6 @@ #include "xfa/fxfa/parser/cxfa_layoutprocessor.h" #include "xfa/fxfa/parser/cxfa_localevalue.h" #include "xfa/fxfa/parser/cxfa_node.h" -#include "xfa/fxfa/parser/cxfa_scriptcontext.h" #include "xfa/fxfa/parser/xfa_utils.h" class CXFA_WidgetLayoutData { @@ -618,7 +618,7 @@ int32_t CXFA_WidgetAcc::ExecuteScript(CXFA_Script script, return XFA_EVENTERROR_Success; CXFA_FFDoc* pDoc = GetDoc(); - CXFA_ScriptContext* pContext = pDoc->GetXFADoc()->GetScriptContext(); + CFXJSE_Engine* pContext = pDoc->GetXFADoc()->GetScriptContext(); pContext->SetEventParam(*pEventParam); pContext->SetRunAtType((XFA_ATTRIBUTEENUM)script.GetRunAt()); std::vector<CXFA_Node*> refNodes; diff --git a/xfa/fxfa/fm2js/cxfa_fm2jscontext.cpp b/xfa/fxfa/fm2js/cxfa_fm2jscontext.cpp deleted file mode 100644 index 2f271d8c60..0000000000 --- a/xfa/fxfa/fm2js/cxfa_fm2jscontext.cpp +++ /dev/null @@ -1,6159 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include "xfa/fxfa/fm2js/cxfa_fm2jscontext.h" - -#include <time.h> - -#include <algorithm> -#include <string> - -#include "core/fxcrt/cfx_decimal.h" -#include "core/fxcrt/cfx_widetextbuf.h" -#include "core/fxcrt/fx_extension.h" -#include "core/fxcrt/fx_random.h" -#include "fxjs/cfxjse_arguments.h" -#include "fxjs/cfxjse_class.h" -#include "fxjs/cfxjse_value.h" -#include "third_party/base/ptr_util.h" -#include "third_party/base/stl_util.h" -#include "xfa/fxfa/cxfa_ffnotify.h" -#include "xfa/fxfa/fm2js/cxfa_fmparser.h" -#include "xfa/fxfa/fm2js/cxfa_fmtojavascriptdepth.h" -#include "xfa/fxfa/parser/cxfa_document.h" -#include "xfa/fxfa/parser/cxfa_localevalue.h" -#include "xfa/fxfa/parser/cxfa_node.h" -#include "xfa/fxfa/parser/cxfa_scriptcontext.h" -#include "xfa/fxfa/parser/cxfa_timezoneprovider.h" -#include "xfa/fxfa/parser/xfa_utils.h" - -namespace { - -const double kFinancialPrecision = 0.00000001; - -struct XFA_FMHtmlReserveCode { - uint32_t m_uCode; - const wchar_t* m_htmlReserve; -}; - -// Sorted by m_htmlReserve -XFA_FMHtmlReserveCode reservesForDecode[] = { - {198, L"AElig"}, {193, L"Aacute"}, {194, L"Acirc"}, - {192, L"Agrave"}, {913, L"Alpha"}, {197, L"Aring"}, - {195, L"Atilde"}, {196, L"Auml"}, {914, L"Beta"}, - {199, L"Ccedil"}, {935, L"Chi"}, {8225, L"Dagger"}, - {916, L"Delta"}, {208, L"ETH"}, {201, L"Eacute"}, - {202, L"Ecirc"}, {200, L"Egrave"}, {917, L"Epsilon"}, - {919, L"Eta"}, {203, L"Euml"}, {915, L"Gamma"}, - {922, L"Kappa"}, {923, L"Lambda"}, {924, L"Mu"}, - {209, L"Ntilde"}, {925, L"Nu"}, {338, L"OElig"}, - {211, L"Oacute"}, {212, L"Ocirc"}, {210, L"Ograve"}, - {937, L"Omega"}, {927, L"Omicron"}, {216, L"Oslash"}, - {213, L"Otilde"}, {214, L"Ouml"}, {934, L"Phi"}, - {928, L"Pi"}, {936, L"Psi"}, {929, L"Rho"}, - {352, L"Scaron"}, {931, L"Sigma"}, {222, L"THORN"}, - {932, L"Tau"}, {920, L"Theta"}, {218, L"Uacute"}, - {219, L"Ucirc"}, {217, L"Ugrave"}, {933, L"Upsilon"}, - {220, L"Uuml"}, {926, L"Xi"}, {221, L"Yacute"}, - {376, L"Yuml"}, {918, L"Zeta"}, {225, L"aacute"}, - {226, L"acirc"}, {180, L"acute"}, {230, L"aelig"}, - {224, L"agrave"}, {8501, L"alefsym"}, {945, L"alpha"}, - {38, L"amp"}, {8743, L"and"}, {8736, L"ang"}, - {39, L"apos"}, {229, L"aring"}, {8776, L"asymp"}, - {227, L"atilde"}, {228, L"auml"}, {8222, L"bdquo"}, - {946, L"beta"}, {166, L"brvbar"}, {8226, L"bull"}, - {8745, L"cap"}, {231, L"ccedil"}, {184, L"cedil"}, - {162, L"cent"}, {967, L"chi"}, {710, L"circ"}, - {9827, L"clubs"}, {8773, L"cong"}, {169, L"copy"}, - {8629, L"crarr"}, {8746, L"cup"}, {164, L"current"}, - {8659, L"dArr"}, {8224, L"dagger"}, {8595, L"darr"}, - {176, L"deg"}, {948, L"delta"}, {9830, L"diams"}, - {247, L"divide"}, {233, L"eacute"}, {234, L"ecirc"}, - {232, L"egrave"}, {8709, L"empty"}, {8195, L"emsp"}, - {8194, L"ensp"}, {949, L"epsilon"}, {8801, L"equiv"}, - {951, L"eta"}, {240, L"eth"}, {235, L"euml"}, - {8364, L"euro"}, {8707, L"exist"}, {402, L"fnof"}, - {8704, L"forall"}, {189, L"frac12"}, {188, L"frac14"}, - {190, L"frac34"}, {8260, L"frasl"}, {947, L"gamma"}, - {8805, L"ge"}, {62, L"gt"}, {8660, L"hArr"}, - {8596, L"harr"}, {9829, L"hearts"}, {8230, L"hellip"}, - {237, L"iacute"}, {238, L"icirc"}, {161, L"iexcl"}, - {236, L"igrave"}, {8465, L"image"}, {8734, L"infin"}, - {8747, L"int"}, {953, L"iota"}, {191, L"iquest"}, - {8712, L"isin"}, {239, L"iuml"}, {954, L"kappa"}, - {8656, L"lArr"}, {205, L"lacute"}, {955, L"lambda"}, - {9001, L"lang"}, {171, L"laquo"}, {8592, L"larr"}, - {8968, L"lceil"}, {206, L"lcirc"}, {8220, L"ldquo"}, - {8804, L"le"}, {8970, L"lfloor"}, {204, L"lgrave"}, - {921, L"lota"}, {8727, L"lowast"}, {9674, L"loz"}, - {8206, L"lrm"}, {8249, L"lsaquo"}, {8216, L"lsquo"}, - {60, L"lt"}, {207, L"luml"}, {175, L"macr"}, - {8212, L"mdash"}, {181, L"micro"}, {183, L"middot"}, - {8722, L"minus"}, {956, L"mu"}, {8711, L"nabla"}, - {160, L"nbsp"}, {8211, L"ndash"}, {8800, L"ne"}, - {8715, L"ni"}, {172, L"not"}, {8713, L"notin"}, - {8836, L"nsub"}, {241, L"ntilde"}, {957, L"nu"}, - {243, L"oacute"}, {244, L"ocirc"}, {339, L"oelig"}, - {242, L"ograve"}, {8254, L"oline"}, {969, L"omega"}, - {959, L"omicron"}, {8853, L"oplus"}, {8744, L"or"}, - {170, L"ordf"}, {186, L"ordm"}, {248, L"oslash"}, - {245, L"otilde"}, {8855, L"otimes"}, {246, L"ouml"}, - {182, L"para"}, {8706, L"part"}, {8240, L"permil"}, - {8869, L"perp"}, {966, L"phi"}, {960, L"pi"}, - {982, L"piv"}, {177, L"plusmn"}, {8242, L"prime"}, - {8719, L"prod"}, {8733, L"prop"}, {968, L"psi"}, - {163, L"pund"}, {34, L"quot"}, {8658, L"rArr"}, - {8730, L"radic"}, {9002, L"rang"}, {187, L"raquo"}, - {8594, L"rarr"}, {8969, L"rceil"}, {8476, L"real"}, - {174, L"reg"}, {8971, L"rfloor"}, {961, L"rho"}, - {8207, L"rlm"}, {8250, L"rsaquo"}, {8217, L"rsquo"}, - {353, L"saron"}, {8218, L"sbquo"}, {8901, L"sdot"}, - {167, L"sect"}, {173, L"shy"}, {963, L"sigma"}, - {962, L"sigmaf"}, {8764, L"sim"}, {9824, L"spades"}, - {8834, L"sub"}, {8838, L"sube"}, {8721, L"sum"}, - {8835, L"sup"}, {185, L"sup1"}, {178, L"sup2"}, - {179, L"sup3"}, {8839, L"supe"}, {223, L"szlig"}, - {964, L"tau"}, {8221, L"tdquo"}, {8756, L"there4"}, - {952, L"theta"}, {977, L"thetasym"}, {8201, L"thinsp"}, - {254, L"thorn"}, {732, L"tilde"}, {215, L"times"}, - {8482, L"trade"}, {8657, L"uArr"}, {250, L"uacute"}, - {8593, L"uarr"}, {251, L"ucirc"}, {249, L"ugrave"}, - {168, L"uml"}, {978, L"upsih"}, {965, L"upsilon"}, - {252, L"uuml"}, {8472, L"weierp"}, {958, L"xi"}, - {253, L"yacute"}, {165, L"yen"}, {255, L"yuml"}, - {950, L"zeta"}, {8205, L"zwj"}, {8204, L"zwnj"}, -}; - -// Sorted by m_uCode -const XFA_FMHtmlReserveCode reservesForEncode[] = { - {34, L"quot"}, {38, L"amp"}, {39, L"apos"}, - {60, L"lt"}, {62, L"gt"}, {160, L"nbsp"}, - {161, L"iexcl"}, {162, L"cent"}, {163, L"pund"}, - {164, L"current"}, {165, L"yen"}, {166, L"brvbar"}, - {167, L"sect"}, {168, L"uml"}, {169, L"copy"}, - {170, L"ordf"}, {171, L"laquo"}, {172, L"not"}, - {173, L"shy"}, {174, L"reg"}, {175, L"macr"}, - {176, L"deg"}, {177, L"plusmn"}, {178, L"sup2"}, - {179, L"sup3"}, {180, L"acute"}, {181, L"micro"}, - {182, L"para"}, {183, L"middot"}, {184, L"cedil"}, - {185, L"sup1"}, {186, L"ordm"}, {187, L"raquo"}, - {188, L"frac14"}, {189, L"frac12"}, {190, L"frac34"}, - {191, L"iquest"}, {192, L"Agrave"}, {193, L"Aacute"}, - {194, L"Acirc"}, {195, L"Atilde"}, {196, L"Auml"}, - {197, L"Aring"}, {198, L"AElig"}, {199, L"Ccedil"}, - {200, L"Egrave"}, {201, L"Eacute"}, {202, L"Ecirc"}, - {203, L"Euml"}, {204, L"lgrave"}, {205, L"lacute"}, - {206, L"lcirc"}, {207, L"luml"}, {208, L"ETH"}, - {209, L"Ntilde"}, {210, L"Ograve"}, {211, L"Oacute"}, - {212, L"Ocirc"}, {213, L"Otilde"}, {214, L"Ouml"}, - {215, L"times"}, {216, L"Oslash"}, {217, L"Ugrave"}, - {218, L"Uacute"}, {219, L"Ucirc"}, {220, L"Uuml"}, - {221, L"Yacute"}, {222, L"THORN"}, {223, L"szlig"}, - {224, L"agrave"}, {225, L"aacute"}, {226, L"acirc"}, - {227, L"atilde"}, {228, L"auml"}, {229, L"aring"}, - {230, L"aelig"}, {231, L"ccedil"}, {232, L"egrave"}, - {233, L"eacute"}, {234, L"ecirc"}, {235, L"euml"}, - {236, L"igrave"}, {237, L"iacute"}, {238, L"icirc"}, - {239, L"iuml"}, {240, L"eth"}, {241, L"ntilde"}, - {242, L"ograve"}, {243, L"oacute"}, {244, L"ocirc"}, - {245, L"otilde"}, {246, L"ouml"}, {247, L"divide"}, - {248, L"oslash"}, {249, L"ugrave"}, {250, L"uacute"}, - {251, L"ucirc"}, {252, L"uuml"}, {253, L"yacute"}, - {254, L"thorn"}, {255, L"yuml"}, {338, L"OElig"}, - {339, L"oelig"}, {352, L"Scaron"}, {353, L"saron"}, - {376, L"Yuml"}, {402, L"fnof"}, {710, L"circ"}, - {732, L"tilde"}, {913, L"Alpha"}, {914, L"Beta"}, - {915, L"Gamma"}, {916, L"Delta"}, {917, L"Epsilon"}, - {918, L"Zeta"}, {919, L"Eta"}, {920, L"Theta"}, - {921, L"lota"}, {922, L"Kappa"}, {923, L"Lambda"}, - {924, L"Mu"}, {925, L"Nu"}, {926, L"Xi"}, - {927, L"Omicron"}, {928, L"Pi"}, {929, L"Rho"}, - {931, L"Sigma"}, {932, L"Tau"}, {933, L"Upsilon"}, - {934, L"Phi"}, {935, L"Chi"}, {936, L"Psi"}, - {937, L"Omega"}, {945, L"alpha"}, {946, L"beta"}, - {947, L"gamma"}, {948, L"delta"}, {949, L"epsilon"}, - {950, L"zeta"}, {951, L"eta"}, {952, L"theta"}, - {953, L"iota"}, {954, L"kappa"}, {955, L"lambda"}, - {956, L"mu"}, {957, L"nu"}, {958, L"xi"}, - {959, L"omicron"}, {960, L"pi"}, {961, L"rho"}, - {962, L"sigmaf"}, {963, L"sigma"}, {964, L"tau"}, - {965, L"upsilon"}, {966, L"phi"}, {967, L"chi"}, - {968, L"psi"}, {969, L"omega"}, {977, L"thetasym"}, - {978, L"upsih"}, {982, L"piv"}, {8194, L"ensp"}, - {8195, L"emsp"}, {8201, L"thinsp"}, {8204, L"zwnj"}, - {8205, L"zwj"}, {8206, L"lrm"}, {8207, L"rlm"}, - {8211, L"ndash"}, {8212, L"mdash"}, {8216, L"lsquo"}, - {8217, L"rsquo"}, {8218, L"sbquo"}, {8220, L"ldquo"}, - {8221, L"tdquo"}, {8222, L"bdquo"}, {8224, L"dagger"}, - {8225, L"Dagger"}, {8226, L"bull"}, {8230, L"hellip"}, - {8240, L"permil"}, {8242, L"prime"}, {8249, L"lsaquo"}, - {8250, L"rsaquo"}, {8254, L"oline"}, {8260, L"frasl"}, - {8364, L"euro"}, {8465, L"image"}, {8472, L"weierp"}, - {8476, L"real"}, {8482, L"trade"}, {8501, L"alefsym"}, - {8592, L"larr"}, {8593, L"uarr"}, {8594, L"rarr"}, - {8595, L"darr"}, {8596, L"harr"}, {8629, L"crarr"}, - {8656, L"lArr"}, {8657, L"uArr"}, {8658, L"rArr"}, - {8659, L"dArr"}, {8660, L"hArr"}, {8704, L"forall"}, - {8706, L"part"}, {8707, L"exist"}, {8709, L"empty"}, - {8711, L"nabla"}, {8712, L"isin"}, {8713, L"notin"}, - {8715, L"ni"}, {8719, L"prod"}, {8721, L"sum"}, - {8722, L"minus"}, {8727, L"lowast"}, {8730, L"radic"}, - {8733, L"prop"}, {8734, L"infin"}, {8736, L"ang"}, - {8743, L"and"}, {8744, L"or"}, {8745, L"cap"}, - {8746, L"cup"}, {8747, L"int"}, {8756, L"there4"}, - {8764, L"sim"}, {8773, L"cong"}, {8776, L"asymp"}, - {8800, L"ne"}, {8801, L"equiv"}, {8804, L"le"}, - {8805, L"ge"}, {8834, L"sub"}, {8835, L"sup"}, - {8836, L"nsub"}, {8838, L"sube"}, {8839, L"supe"}, - {8853, L"oplus"}, {8855, L"otimes"}, {8869, L"perp"}, - {8901, L"sdot"}, {8968, L"lceil"}, {8969, L"rceil"}, - {8970, L"lfloor"}, {8971, L"rfloor"}, {9001, L"lang"}, - {9002, L"rang"}, {9674, L"loz"}, {9824, L"spades"}, - {9827, L"clubs"}, {9829, L"hearts"}, {9830, L"diams"}, -}; - -const FXJSE_FUNCTION_DESCRIPTOR formcalc_fm2js_functions[] = { - {"Abs", CXFA_FM2JSContext::Abs}, - {"Avg", CXFA_FM2JSContext::Avg}, - {"Ceil", CXFA_FM2JSContext::Ceil}, - {"Count", CXFA_FM2JSContext::Count}, - {"Floor", CXFA_FM2JSContext::Floor}, - {"Max", CXFA_FM2JSContext::Max}, - {"Min", CXFA_FM2JSContext::Min}, - {"Mod", CXFA_FM2JSContext::Mod}, - {"Round", CXFA_FM2JSContext::Round}, - {"Sum", CXFA_FM2JSContext::Sum}, - {"Date", CXFA_FM2JSContext::Date}, - {"Date2Num", CXFA_FM2JSContext::Date2Num}, - {"DateFmt", CXFA_FM2JSContext::DateFmt}, - {"IsoDate2Num", CXFA_FM2JSContext::IsoDate2Num}, - {"IsoTime2Num", CXFA_FM2JSContext::IsoTime2Num}, - {"LocalDateFmt", CXFA_FM2JSContext::LocalDateFmt}, - {"LocalTimeFmt", CXFA_FM2JSContext::LocalTimeFmt}, - {"Num2Date", CXFA_FM2JSContext::Num2Date}, - {"Num2GMTime", CXFA_FM2JSContext::Num2GMTime}, - {"Num2Time", CXFA_FM2JSContext::Num2Time}, - {"Time", CXFA_FM2JSContext::Time}, - {"Time2Num", CXFA_FM2JSContext::Time2Num}, - {"TimeFmt", CXFA_FM2JSContext::TimeFmt}, - {"Apr", CXFA_FM2JSContext::Apr}, - {"Cterm", CXFA_FM2JSContext::CTerm}, - {"FV", CXFA_FM2JSContext::FV}, - {"Ipmt", CXFA_FM2JSContext::IPmt}, - {"NPV", CXFA_FM2JSContext::NPV}, - {"Pmt", CXFA_FM2JSContext::Pmt}, - {"PPmt", CXFA_FM2JSContext::PPmt}, - {"PV", CXFA_FM2JSContext::PV}, - {"Rate", CXFA_FM2JSContext::Rate}, - {"Term", CXFA_FM2JSContext::Term}, - {"Choose", CXFA_FM2JSContext::Choose}, - {"Exists", CXFA_FM2JSContext::Exists}, - {"HasValue", CXFA_FM2JSContext::HasValue}, - {"Oneof", CXFA_FM2JSContext::Oneof}, - {"Within", CXFA_FM2JSContext::Within}, - {"If", CXFA_FM2JSContext::If}, - {"Eval", CXFA_FM2JSContext::Eval}, - {"Translate", CXFA_FM2JSContext::eval_translation}, - {"Ref", CXFA_FM2JSContext::Ref}, - {"UnitType", CXFA_FM2JSContext::UnitType}, - {"UnitValue", CXFA_FM2JSContext::UnitValue}, - {"At", CXFA_FM2JSContext::At}, - {"Concat", CXFA_FM2JSContext::Concat}, - {"Decode", CXFA_FM2JSContext::Decode}, - {"Encode", CXFA_FM2JSContext::Encode}, - {"Format", CXFA_FM2JSContext::Format}, - {"Left", CXFA_FM2JSContext::Left}, - {"Len", CXFA_FM2JSContext::Len}, - {"Lower", CXFA_FM2JSContext::Lower}, - {"Ltrim", CXFA_FM2JSContext::Ltrim}, - {"Parse", CXFA_FM2JSContext::Parse}, - {"Replace", CXFA_FM2JSContext::Replace}, - {"Right", CXFA_FM2JSContext::Right}, - {"Rtrim", CXFA_FM2JSContext::Rtrim}, - {"Space", CXFA_FM2JSContext::Space}, - {"Str", CXFA_FM2JSContext::Str}, - {"Stuff", CXFA_FM2JSContext::Stuff}, - {"Substr", CXFA_FM2JSContext::Substr}, - {"Uuid", CXFA_FM2JSContext::Uuid}, - {"Upper", CXFA_FM2JSContext::Upper}, - {"WordNum", CXFA_FM2JSContext::WordNum}, - {"Get", CXFA_FM2JSContext::Get}, - {"Post", CXFA_FM2JSContext::Post}, - {"Put", CXFA_FM2JSContext::Put}, - {"pos_op", CXFA_FM2JSContext::positive_operator}, - {"neg_op", CXFA_FM2JSContext::negative_operator}, - {"log_or_op", CXFA_FM2JSContext::logical_or_operator}, - {"log_and_op", CXFA_FM2JSContext::logical_and_operator}, - {"log_not_op", CXFA_FM2JSContext::logical_not_operator}, - {"eq_op", CXFA_FM2JSContext::equality_operator}, - {"neq_op", CXFA_FM2JSContext::notequality_operator}, - {"lt_op", CXFA_FM2JSContext::less_operator}, - {"le_op", CXFA_FM2JSContext::lessequal_operator}, - {"gt_op", CXFA_FM2JSContext::greater_operator}, - {"ge_op", CXFA_FM2JSContext::greaterequal_operator}, - {"plus_op", CXFA_FM2JSContext::plus_operator}, - {"minus_op", CXFA_FM2JSContext::minus_operator}, - {"mul_op", CXFA_FM2JSContext::multiple_operator}, - {"div_op", CXFA_FM2JSContext::divide_operator}, - {"asgn_val_op", CXFA_FM2JSContext::assign_value_operator}, - {"dot_acc", CXFA_FM2JSContext::dot_accessor}, - {"dotdot_acc", CXFA_FM2JSContext::dotdot_accessor}, - {"concat_obj", CXFA_FM2JSContext::concat_fm_object}, - {"is_obj", CXFA_FM2JSContext::is_fm_object}, - {"is_ary", CXFA_FM2JSContext::is_fm_array}, - {"get_val", CXFA_FM2JSContext::get_fm_value}, - {"get_jsobj", CXFA_FM2JSContext::get_fm_jsobj}, - {"var_filter", CXFA_FM2JSContext::fm_var_filter}, -}; - -const FXJSE_CLASS_DESCRIPTOR formcalc_fm2js_descriptor = { - "XFA_FM2JS_FormCalcClass", // name - nullptr, // constructor - nullptr, // properties - formcalc_fm2js_functions, // methods - 0, // number of properties - FX_ArraySize(formcalc_fm2js_functions), // number of methods - nullptr, // dynamic prop type - nullptr, // dynamic prop getter - nullptr, // dynamic prop setter - nullptr, // dynamic prop deleter - nullptr, // dynamic prop method call -}; - -const uint8_t g_sAltTable_Date[] = { - 255, 255, 255, 3, 9, 255, 255, 255, 255, 255, 255, - 255, 2, 255, 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 1, 255, 255, 255, 255, 255, 255, 255, 255, -}; -static_assert(FX_ArraySize(g_sAltTable_Date) == L'a' - L'A' + 1, - "Invalid g_sAltTable_Date size."); - -const uint8_t g_sAltTable_Time[] = { - 14, 255, 255, 3, 9, 255, 255, 15, 255, 255, 255, - 255, 6, 255, 255, 255, 255, 255, 7, 255, 255, 255, - 255, 255, 1, 17, 255, 255, 255, 255, 255, 255, 255, -}; -static_assert(FX_ArraySize(g_sAltTable_Time) == L'a' - L'A' + 1, - "Invalid g_sAltTable_Time size."); - -void AlternateDateTimeSymbols(WideString& wsPattern, - const WideString& wsAltSymbols, - const uint8_t* pAltTable) { - int32_t nLength = wsPattern.GetLength(); - bool bInConstRange = false; - bool bEscape = false; - int32_t i = 0; - while (i < nLength) { - wchar_t wc = wsPattern[i]; - if (wc == L'\'') { - bInConstRange = !bInConstRange; - if (bEscape) { - i++; - } else { - wsPattern.Delete(i); - nLength--; - } - bEscape = !bEscape; - continue; - } - if (!bInConstRange && wc >= L'A' && wc <= L'a') { - uint8_t nAlt = pAltTable[wc - L'A']; - if (nAlt != 255) - wsPattern.SetAt(i, wsAltSymbols[nAlt]); - } - i++; - bEscape = false; - } -} - -bool PatternStringType(const ByteStringView& szPattern, uint32_t& patternType) { - WideString wsPattern = WideString::FromUTF8(szPattern); - if (L"datetime" == wsPattern.Left(8)) { - patternType = XFA_VT_DATETIME; - return true; - } - if (L"date" == wsPattern.Left(4)) { - auto pos = wsPattern.Find(L"time"); - patternType = - pos.has_value() && pos.value() != 0 ? XFA_VT_DATETIME : XFA_VT_DATE; - return true; - } - if (L"time" == wsPattern.Left(4)) { - patternType = XFA_VT_TIME; - return true; - } - if (L"text" == wsPattern.Left(4)) { - patternType = XFA_VT_TEXT; - return true; - } - if (L"num" == wsPattern.Left(3)) { - if (L"integer" == wsPattern.Mid(4, 7)) { - patternType = XFA_VT_INTEGER; - } else if (L"decimal" == wsPattern.Mid(4, 7)) { - patternType = XFA_VT_DECIMAL; - } else if (L"currency" == wsPattern.Mid(4, 8)) { - patternType = XFA_VT_FLOAT; - } else if (L"percent" == wsPattern.Mid(4, 7)) { - patternType = XFA_VT_FLOAT; - } else { - patternType = XFA_VT_FLOAT; - } - return true; - } - - patternType = XFA_VT_NULL; - wsPattern.MakeLower(); - const wchar_t* pData = wsPattern.c_str(); - int32_t iLength = wsPattern.GetLength(); - int32_t iIndex = 0; - bool bSingleQuotation = false; - wchar_t patternChar; - while (iIndex < iLength) { - patternChar = pData[iIndex]; - if (patternChar == 0x27) { - bSingleQuotation = !bSingleQuotation; - } else if (!bSingleQuotation && - (patternChar == 'y' || patternChar == 'j')) { - patternType = XFA_VT_DATE; - iIndex++; - wchar_t timePatternChar; - while (iIndex < iLength) { - timePatternChar = pData[iIndex]; - if (timePatternChar == 0x27) { - bSingleQuotation = !bSingleQuotation; - } else if (!bSingleQuotation && timePatternChar == 't') { - patternType = XFA_VT_DATETIME; - break; - } - iIndex++; - } - break; - } else if (!bSingleQuotation && - (patternChar == 'h' || patternChar == 'k')) { - patternType = XFA_VT_TIME; - break; - } else if (!bSingleQuotation && - (patternChar == 'a' || patternChar == 'x' || - patternChar == 'o' || patternChar == '0')) { - patternType = XFA_VT_TEXT; - if (patternChar == 'x' || patternChar == 'o' || patternChar == '0') { - break; - } - } else if (!bSingleQuotation && - (patternChar == 'z' || patternChar == 's' || - patternChar == 'e' || patternChar == 'v' || - patternChar == '8' || patternChar == ',' || - patternChar == '.' || patternChar == '$')) { - patternType = XFA_VT_FLOAT; - if (patternChar == 'v' || patternChar == '8' || patternChar == '$') { - break; - } - } - iIndex++; - } - if (patternType == XFA_VT_NULL) { - patternType = XFA_VT_TEXT | XFA_VT_FLOAT; - } - return false; -} - -CXFA_FM2JSContext* ToJSContext(CFXJSE_Value* pValue, CFXJSE_Class* pClass) { - CFXJSE_HostObject* pHostObj = pValue->ToHostObject(pClass); - if (!pHostObj || pHostObj->type() != CFXJSE_HostObject::kFM2JS) - return nullptr; - return static_cast<CXFA_FM2JSContext*>(pHostObj); -} - -bool IsWhitespace(char c) { - return c == 0x20 || c == 0x09 || c == 0x0B || c == 0x0C || c == 0x0A || - c == 0x0D; -} - -IFX_Locale* LocaleFromString(CXFA_Document* pDoc, - CXFA_LocaleMgr* pMgr, - const ByteStringView& szLocale) { - if (!szLocale.IsEmpty()) - return pMgr->GetLocaleByName(WideString::FromUTF8(szLocale)); - - CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject()); - ASSERT(pThisNode); - return CXFA_WidgetData(pThisNode).GetLocal(); -} - -WideString FormatFromString(IFX_Locale* pLocale, - const ByteStringView& szFormat) { - if (!szFormat.IsEmpty()) - return WideString::FromUTF8(szFormat); - - return pLocale->GetDatePattern(FX_LOCALEDATETIMESUBCATEGORY_Default); -} - -FX_LOCALEDATETIMESUBCATEGORY SubCategoryFromInt(int32_t iStyle) { - switch (iStyle) { - case 1: - return FX_LOCALEDATETIMESUBCATEGORY_Short; - case 3: - return FX_LOCALEDATETIMESUBCATEGORY_Long; - case 4: - return FX_LOCALEDATETIMESUBCATEGORY_Full; - case 0: - case 2: - default: - return FX_LOCALEDATETIMESUBCATEGORY_Medium; - } -} - -bool IsPartOfNumber(char ch) { - return std::isdigit(ch) || ch == '-' || ch == '.'; -} - -bool IsPartOfNumberW(wchar_t ch) { - return std::iswdigit(ch) || ch == L'-' || ch == L'.'; -} - -ByteString GUIDString(bool bSeparator) { - uint8_t data[16]; - FX_Random_GenerateMT(reinterpret_cast<uint32_t*>(data), 4); - data[6] = (data[6] & 0x0F) | 0x40; - - ByteString bsStr; - char* pBuf = bsStr.GetBuffer(40); - for (int32_t i = 0; i < 16; ++i, pBuf += 2) { - if (bSeparator && (i == 4 || i == 6 || i == 8 || i == 10)) - *pBuf++ = L'-'; - - FXSYS_IntToTwoHexChars(data[i], pBuf); - } - bsStr.ReleaseBuffer(bSeparator ? 36 : 32); - return bsStr; -} - -} // namespace - -// static -void CXFA_FM2JSContext::Abs(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 1) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Abs"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); - if (ValueIsNull(pThis, argOne.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - double dValue = ValueToDouble(pThis, argOne.get()); - if (dValue < 0) - dValue = -dValue; - - args.GetReturnValue()->SetDouble(dValue); -} - -// static -void CXFA_FM2JSContext::Avg(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc < 1) { - args.GetReturnValue()->SetNull(); - return; - } - - v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); - uint32_t uCount = 0; - double dSum = 0.0; - for (int32_t i = 0; i < argc; i++) { - std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i); - if (argValue->IsNull()) - continue; - - if (!argValue->IsArray()) { - dSum += ValueToDouble(pThis, argValue.get()); - uCount++; - continue; - } - - auto lengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argValue->GetObjectProperty("length", lengthValue.get()); - int32_t iLength = lengthValue->ToInteger(); - - if (iLength > 2) { - auto propertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argValue->GetObjectPropertyByIdx(1, propertyValue.get()); - - auto jsObjectValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - if (propertyValue->IsNull()) { - for (int32_t j = 2; j < iLength; j++) { - argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); - auto defaultPropValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - GetObjectDefaultValue(jsObjectValue.get(), defaultPropValue.get()); - if (defaultPropValue->IsNull()) - continue; - - dSum += ValueToDouble(pThis, defaultPropValue.get()); - uCount++; - } - } else { - for (int32_t j = 2; j < iLength; j++) { - argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); - auto newPropertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - jsObjectValue->GetObjectProperty( - propertyValue->ToString().AsStringView(), newPropertyValue.get()); - if (newPropertyValue->IsNull()) - continue; - - dSum += ValueToDouble(pThis, newPropertyValue.get()); - uCount++; - } - } - } - } - if (uCount == 0) { - args.GetReturnValue()->SetNull(); - return; - } - - args.GetReturnValue()->SetDouble(dSum / uCount); -} - -// static -void CXFA_FM2JSContext::Ceil(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 1) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Ceil"); - return; - } - - std::unique_ptr<CFXJSE_Value> argValue = GetSimpleValue(pThis, args, 0); - if (ValueIsNull(pThis, argValue.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - args.GetReturnValue()->SetFloat(ceil(ValueToFloat(pThis, argValue.get()))); -} - -// static -void CXFA_FM2JSContext::Count(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - v8::Isolate* pIsolate = pContext->GetScriptRuntime(); - int32_t iCount = 0; - for (int32_t i = 0; i < args.GetLength(); i++) { - std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i); - if (argValue->IsNull()) - continue; - - if (argValue->IsArray()) { - auto lengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argValue->GetObjectProperty("length", lengthValue.get()); - - int32_t iLength = lengthValue->ToInteger(); - if (iLength <= 2) { - pContext->ThrowArgumentMismatchException(); - return; - } - - auto propertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto jsObjectValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto newPropertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argValue->GetObjectPropertyByIdx(1, propertyValue.get()); - argValue->GetObjectPropertyByIdx(2, jsObjectValue.get()); - if (propertyValue->IsNull()) { - for (int32_t j = 2; j < iLength; j++) { - argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); - GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get()); - if (!newPropertyValue->IsNull()) - iCount++; - } - } else { - for (int32_t j = 2; j < iLength; j++) { - argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); - jsObjectValue->GetObjectProperty( - propertyValue->ToString().AsStringView(), newPropertyValue.get()); - iCount += newPropertyValue->IsNull() ? 0 : 1; - } - } - } else if (argValue->IsObject()) { - auto newPropertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - GetObjectDefaultValue(argValue.get(), newPropertyValue.get()); - if (!newPropertyValue->IsNull()) - iCount++; - } else { - iCount++; - } - } - args.GetReturnValue()->SetInteger(iCount); -} - -// static -void CXFA_FM2JSContext::Floor(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 1) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Floor"); - return; - } - - std::unique_ptr<CFXJSE_Value> argValue = GetSimpleValue(pThis, args, 0); - if (ValueIsNull(pThis, argValue.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - args.GetReturnValue()->SetFloat(floor(ValueToFloat(pThis, argValue.get()))); -} - -// static -void CXFA_FM2JSContext::Max(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - v8::Isolate* pIsolate = pContext->GetScriptRuntime(); - uint32_t uCount = 0; - double dMaxValue = 0.0; - for (int32_t i = 0; i < args.GetLength(); i++) { - std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i); - if (argValue->IsNull()) - continue; - - if (argValue->IsArray()) { - auto lengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argValue->GetObjectProperty("length", lengthValue.get()); - int32_t iLength = lengthValue->ToInteger(); - if (iLength <= 2) { - pContext->ThrowArgumentMismatchException(); - return; - } - - auto propertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto jsObjectValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto newPropertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argValue->GetObjectPropertyByIdx(1, propertyValue.get()); - argValue->GetObjectPropertyByIdx(2, jsObjectValue.get()); - if (propertyValue->IsNull()) { - for (int32_t j = 2; j < iLength; j++) { - argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); - GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get()); - if (newPropertyValue->IsNull()) - continue; - - uCount++; - double dValue = ValueToDouble(pThis, newPropertyValue.get()); - dMaxValue = (uCount == 1) ? dValue : std::max(dMaxValue, dValue); - } - } else { - for (int32_t j = 2; j < iLength; j++) { - argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); - jsObjectValue->GetObjectProperty( - propertyValue->ToString().AsStringView(), newPropertyValue.get()); - if (newPropertyValue->IsNull()) - continue; - - uCount++; - double dValue = ValueToDouble(pThis, newPropertyValue.get()); - dMaxValue = (uCount == 1) ? dValue : std::max(dMaxValue, dValue); - } - } - } else if (argValue->IsObject()) { - auto newPropertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - GetObjectDefaultValue(argValue.get(), newPropertyValue.get()); - if (newPropertyValue->IsNull()) - continue; - - uCount++; - double dValue = ValueToDouble(pThis, newPropertyValue.get()); - dMaxValue = (uCount == 1) ? dValue : std::max(dMaxValue, dValue); - } else { - uCount++; - double dValue = ValueToDouble(pThis, argValue.get()); - dMaxValue = (uCount == 1) ? dValue : std::max(dMaxValue, dValue); - } - } - if (uCount == 0) { - args.GetReturnValue()->SetNull(); - return; - } - - args.GetReturnValue()->SetDouble(dMaxValue); -} - -// static -void CXFA_FM2JSContext::Min(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - v8::Isolate* pIsolate = pContext->GetScriptRuntime(); - uint32_t uCount = 0; - double dMinValue = 0.0; - for (int32_t i = 0; i < args.GetLength(); i++) { - std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i); - if (argValue->IsNull()) - continue; - - if (argValue->IsArray()) { - auto lengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argValue->GetObjectProperty("length", lengthValue.get()); - int32_t iLength = lengthValue->ToInteger(); - if (iLength <= 2) { - pContext->ThrowArgumentMismatchException(); - return; - } - - auto propertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto jsObjectValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto newPropertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argValue->GetObjectPropertyByIdx(1, propertyValue.get()); - argValue->GetObjectPropertyByIdx(2, jsObjectValue.get()); - if (propertyValue->IsNull()) { - for (int32_t j = 2; j < iLength; j++) { - argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); - GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get()); - if (newPropertyValue->IsNull()) - continue; - - uCount++; - double dValue = ValueToDouble(pThis, newPropertyValue.get()); - dMinValue = uCount == 1 ? dValue : std::min(dMinValue, dValue); - } - } else { - for (int32_t j = 2; j < iLength; j++) { - argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); - jsObjectValue->GetObjectProperty( - propertyValue->ToString().AsStringView(), newPropertyValue.get()); - if (newPropertyValue->IsNull()) - continue; - - uCount++; - double dValue = ValueToDouble(pThis, newPropertyValue.get()); - dMinValue = uCount == 1 ? dValue : std::min(dMinValue, dValue); - } - } - } else if (argValue->IsObject()) { - auto newPropertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - GetObjectDefaultValue(argValue.get(), newPropertyValue.get()); - if (newPropertyValue->IsNull()) - continue; - - uCount++; - double dValue = ValueToDouble(pThis, newPropertyValue.get()); - dMinValue = uCount == 1 ? dValue : std::min(dMinValue, dValue); - } else { - uCount++; - double dValue = ValueToDouble(pThis, argValue.get()); - dMinValue = uCount == 1 ? dValue : std::min(dMinValue, dValue); - } - } - if (uCount == 0) { - args.GetReturnValue()->SetNull(); - return; - } - - args.GetReturnValue()->SetDouble(dMinValue); -} - -// static -void CXFA_FM2JSContext::Mod(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 2) { - pContext->ThrowParamCountMismatchException(L"Mod"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); - std::unique_ptr<CFXJSE_Value> argTwo = args.GetValue(1); - if (argOne->IsNull() || argTwo->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - - bool argOneResult; - double dDividend = ExtractDouble(pThis, argOne.get(), &argOneResult); - bool argTwoResult; - double dDivisor = ExtractDouble(pThis, argTwo.get(), &argTwoResult); - if (!argOneResult || !argTwoResult) { - pContext->ThrowArgumentMismatchException(); - return; - } - - if (dDivisor == 0.0) { - pContext->ThrowDivideByZeroException(); - return; - } - - args.GetReturnValue()->SetDouble(dDividend - - dDivisor * (int32_t)(dDividend / dDivisor)); -} - -// static -void CXFA_FM2JSContext::Round(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - int32_t argc = args.GetLength(); - if (argc < 1 || argc > 2) { - pContext->ThrowParamCountMismatchException(L"Round"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); - if (argOne->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - - bool dValueRet; - double dValue = ExtractDouble(pThis, argOne.get(), &dValueRet); - if (!dValueRet) { - pContext->ThrowArgumentMismatchException(); - return; - } - - uint8_t uPrecision = 0; - if (argc > 1) { - std::unique_ptr<CFXJSE_Value> argTwo = args.GetValue(1); - if (argTwo->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - - bool dPrecisionRet; - double dPrecision = ExtractDouble(pThis, argTwo.get(), &dPrecisionRet); - if (!dPrecisionRet) { - pContext->ThrowArgumentMismatchException(); - return; - } - - uPrecision = static_cast<uint8_t>(pdfium::clamp(dPrecision, 0.0, 12.0)); - } - - CFX_Decimal decimalValue(static_cast<float>(dValue), uPrecision); - args.GetReturnValue()->SetDouble(decimalValue); -} - -// static -void CXFA_FM2JSContext::Sum(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc == 0) { - args.GetReturnValue()->SetNull(); - return; - } - - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - v8::Isolate* pIsolate = pContext->GetScriptRuntime(); - uint32_t uCount = 0; - double dSum = 0.0; - for (int32_t i = 0; i < argc; i++) { - std::unique_ptr<CFXJSE_Value> argValue = args.GetValue(i); - if (argValue->IsNull()) - continue; - - if (argValue->IsArray()) { - auto lengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argValue->GetObjectProperty("length", lengthValue.get()); - int32_t iLength = lengthValue->ToInteger(); - if (iLength <= 2) { - pContext->ThrowArgumentMismatchException(); - return; - } - - auto propertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argValue->GetObjectPropertyByIdx(1, propertyValue.get()); - auto jsObjectValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto newPropertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - if (propertyValue->IsNull()) { - for (int32_t j = 2; j < iLength; j++) { - argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); - GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get()); - if (newPropertyValue->IsNull()) - continue; - - dSum += ValueToDouble(pThis, jsObjectValue.get()); - uCount++; - } - } else { - for (int32_t j = 2; j < iLength; j++) { - argValue->GetObjectPropertyByIdx(j, jsObjectValue.get()); - jsObjectValue->GetObjectProperty( - propertyValue->ToString().AsStringView(), newPropertyValue.get()); - if (newPropertyValue->IsNull()) - continue; - - dSum += ValueToDouble(pThis, newPropertyValue.get()); - uCount++; - } - } - } else if (argValue->IsObject()) { - auto newPropertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - GetObjectDefaultValue(argValue.get(), newPropertyValue.get()); - if (newPropertyValue->IsNull()) - continue; - - dSum += ValueToDouble(pThis, argValue.get()); - uCount++; - } else { - dSum += ValueToDouble(pThis, argValue.get()); - uCount++; - } - } - if (uCount == 0) { - args.GetReturnValue()->SetNull(); - return; - } - - args.GetReturnValue()->SetDouble(dSum); -} - -// static -void CXFA_FM2JSContext::Date(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 0) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Date"); - return; - } - - time_t currentTime; - time(¤tTime); - struct tm* pTmStruct = gmtime(¤tTime); - - ByteString bufferYear; - ByteString bufferMon; - ByteString bufferDay; - bufferYear.Format("%d", pTmStruct->tm_year + 1900); - bufferMon.Format("%02d", pTmStruct->tm_mon + 1); - bufferDay.Format("%02d", pTmStruct->tm_mday); - - ByteString bufferCurrent = bufferYear + bufferMon + bufferDay; - args.GetReturnValue()->SetInteger( - DateString2Num(bufferCurrent.AsStringView())); -} - -// static -void CXFA_FM2JSContext::Date2Num(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc < 1 || argc > 3) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Date2Num"); - return; - } - - std::unique_ptr<CFXJSE_Value> dateValue = GetSimpleValue(pThis, args, 0); - if (ValueIsNull(pThis, dateValue.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - ByteString dateString = ValueToUTF8String(dateValue.get()); - ByteString formatString; - if (argc > 1) { - std::unique_ptr<CFXJSE_Value> formatValue = GetSimpleValue(pThis, args, 1); - if (ValueIsNull(pThis, formatValue.get())) { - args.GetReturnValue()->SetNull(); - return; - } - formatString = ValueToUTF8String(formatValue.get()); - } - - ByteString localString; - if (argc > 2) { - std::unique_ptr<CFXJSE_Value> localValue = GetSimpleValue(pThis, args, 2); - if (ValueIsNull(pThis, localValue.get())) { - args.GetReturnValue()->SetNull(); - return; - } - localString = ValueToUTF8String(localValue.get()); - } - - ByteString szIsoDateString = - Local2IsoDate(pThis, dateString.AsStringView(), - formatString.AsStringView(), localString.AsStringView()); - args.GetReturnValue()->SetInteger( - DateString2Num(szIsoDateString.AsStringView())); -} - -// static -void CXFA_FM2JSContext::DateFmt(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc > 2) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Date2Num"); - return; - } - - int32_t iStyle = 0; - if (argc > 0) { - std::unique_ptr<CFXJSE_Value> argStyle = GetSimpleValue(pThis, args, 0); - if (argStyle->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - - iStyle = (int32_t)ValueToFloat(pThis, argStyle.get()); - if (iStyle < 0 || iStyle > 4) - iStyle = 0; - } - - ByteString szLocal; - if (argc > 1) { - std::unique_ptr<CFXJSE_Value> argLocal = GetSimpleValue(pThis, args, 1); - if (argLocal->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - szLocal = ValueToUTF8String(argLocal.get()); - } - - ByteString formatStr = - GetStandardDateFormat(pThis, iStyle, szLocal.AsStringView()); - args.GetReturnValue()->SetString(formatStr.AsStringView()); -} - -// static -void CXFA_FM2JSContext::IsoDate2Num(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 1) { - ToJSContext(pThis, nullptr) - ->ThrowParamCountMismatchException(L"IsoDate2Num"); - return; - } - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - if (argOne->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - ByteString szArgString = ValueToUTF8String(argOne.get()); - args.GetReturnValue()->SetInteger(DateString2Num(szArgString.AsStringView())); -} - -// static -void CXFA_FM2JSContext::IsoTime2Num(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 1) { - pContext->ThrowParamCountMismatchException(L"IsoTime2Num"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - if (ValueIsNull(pThis, argOne.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - CXFA_Document* pDoc = pContext->GetDocument(); - CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); - ByteString szArgString = ValueToUTF8String(argOne.get()); - auto pos = szArgString.Find('T', 0); - if (!pos.has_value() || pos.value() == szArgString.GetLength() - 1) { - args.GetReturnValue()->SetInteger(0); - return; - } - szArgString = szArgString.Right(szArgString.GetLength() - (pos.value() + 1)); - - CXFA_LocaleValue timeValue( - XFA_VT_TIME, WideString::FromUTF8(szArgString.AsStringView()), pMgr); - if (!timeValue.IsValid()) { - args.GetReturnValue()->SetInteger(0); - return; - } - - CFX_DateTime uniTime = timeValue.GetTime(); - int32_t hour = uniTime.GetHour(); - int32_t min = uniTime.GetMinute(); - int32_t second = uniTime.GetSecond(); - int32_t milSecond = uniTime.GetMillisecond(); - - // TODO(dsinclair): See if there is other time conversion code in pdfium and - // consolidate. - int32_t mins = hour * 60 + min; - mins -= (pMgr->GetDefLocale()->GetTimeZone().tzHour * 60); - while (mins > 1440) - mins -= 1440; - while (mins < 0) - mins += 1440; - hour = mins / 60; - min = mins % 60; - - args.GetReturnValue()->SetInteger(hour * 3600000 + min * 60000 + - second * 1000 + milSecond + 1); -} - -// static -void CXFA_FM2JSContext::LocalDateFmt(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc > 2) { - ToJSContext(pThis, nullptr) - ->ThrowParamCountMismatchException(L"LocalDateFmt"); - return; - } - - int32_t iStyle = 0; - if (argc > 0) { - std::unique_ptr<CFXJSE_Value> argStyle = GetSimpleValue(pThis, args, 0); - if (argStyle->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - iStyle = (int32_t)ValueToFloat(pThis, argStyle.get()); - if (iStyle > 4 || iStyle < 0) - iStyle = 0; - } - - ByteString szLocal; - if (argc > 1) { - std::unique_ptr<CFXJSE_Value> argLocal = GetSimpleValue(pThis, args, 1); - if (argLocal->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - szLocal = ValueToUTF8String(argLocal.get()); - } - - ByteString formatStr = - GetLocalDateFormat(pThis, iStyle, szLocal.AsStringView(), false); - args.GetReturnValue()->SetString(formatStr.AsStringView()); -} - -// static -void CXFA_FM2JSContext::LocalTimeFmt(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc > 2) { - ToJSContext(pThis, nullptr) - ->ThrowParamCountMismatchException(L"LocalTimeFmt"); - return; - } - - int32_t iStyle = 0; - if (argc > 0) { - std::unique_ptr<CFXJSE_Value> argStyle = GetSimpleValue(pThis, args, 0); - if (argStyle->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - iStyle = (int32_t)ValueToFloat(pThis, argStyle.get()); - if (iStyle > 4 || iStyle < 0) - iStyle = 0; - } - - ByteString szLocal; - if (argc > 1) { - std::unique_ptr<CFXJSE_Value> argLocal = GetSimpleValue(pThis, args, 1); - if (argLocal->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - szLocal = ValueToUTF8String(argLocal.get()); - } - - ByteString formatStr = - GetLocalTimeFormat(pThis, iStyle, szLocal.AsStringView(), false); - args.GetReturnValue()->SetString(formatStr.AsStringView()); -} - -// static -void CXFA_FM2JSContext::Num2Date(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc < 1 || argc > 3) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Num2Date"); - return; - } - - std::unique_ptr<CFXJSE_Value> dateValue = GetSimpleValue(pThis, args, 0); - if (ValueIsNull(pThis, dateValue.get())) { - args.GetReturnValue()->SetNull(); - return; - } - int32_t dDate = (int32_t)ValueToFloat(pThis, dateValue.get()); - if (dDate < 1) { - args.GetReturnValue()->SetNull(); - return; - } - - ByteString formatString; - if (argc > 1) { - std::unique_ptr<CFXJSE_Value> formatValue = GetSimpleValue(pThis, args, 1); - if (ValueIsNull(pThis, formatValue.get())) { - args.GetReturnValue()->SetNull(); - return; - } - formatString = ValueToUTF8String(formatValue.get()); - } - - ByteString localString; - if (argc > 2) { - std::unique_ptr<CFXJSE_Value> localValue = GetSimpleValue(pThis, args, 2); - if (ValueIsNull(pThis, localValue.get())) { - args.GetReturnValue()->SetNull(); - return; - } - localString = ValueToUTF8String(localValue.get()); - } - - int32_t iYear = 1900; - int32_t iMonth = 1; - int32_t iDay = 1; - int32_t i = 0; - while (dDate > 0) { - if (iMonth == 2) { - if ((!((iYear + i) % 4) && ((iYear + i) % 100)) || !((iYear + i) % 400)) { - if (dDate > 29) { - ++iMonth; - if (iMonth > 12) { - iMonth = 1; - ++i; - } - iDay = 1; - dDate -= 29; - } else { - iDay += static_cast<int32_t>(dDate) - 1; - dDate = 0; - } - } else { - if (dDate > 28) { - ++iMonth; - if (iMonth > 12) { - iMonth = 1; - ++i; - } - iDay = 1; - dDate -= 28; - } else { - iDay += static_cast<int32_t>(dDate) - 1; - dDate = 0; - } - } - } else if (iMonth < 8) { - if ((iMonth % 2 == 0)) { - if (dDate > 30) { - ++iMonth; - if (iMonth > 12) { - iMonth = 1; - ++i; - } - iDay = 1; - dDate -= 30; - } else { - iDay += static_cast<int32_t>(dDate) - 1; - dDate = 0; - } - } else { - if (dDate > 31) { - ++iMonth; - if (iMonth > 12) { - iMonth = 1; - ++i; - } - iDay = 1; - dDate -= 31; - } else { - iDay += static_cast<int32_t>(dDate) - 1; - dDate = 0; - } - } - } else { - if (iMonth % 2 != 0) { - if (dDate > 30) { - ++iMonth; - if (iMonth > 12) { - iMonth = 1; - ++i; - } - iDay = 1; - dDate -= 30; - } else { - iDay += static_cast<int32_t>(dDate) - 1; - dDate = 0; - } - } else { - if (dDate > 31) { - ++iMonth; - if (iMonth > 12) { - iMonth = 1; - ++i; - } - iDay = 1; - dDate -= 31; - } else { - iDay += static_cast<int32_t>(dDate) - 1; - dDate = 0; - } - } - } - } - - ByteString szIsoDateString; - szIsoDateString.Format("%d%02d%02d", iYear + i, iMonth, iDay); - ByteString szLocalDateString = - IsoDate2Local(pThis, szIsoDateString.AsStringView(), - formatString.AsStringView(), localString.AsStringView()); - args.GetReturnValue()->SetString(szLocalDateString.AsStringView()); -} - -// static -void CXFA_FM2JSContext::Num2GMTime(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc < 1 || argc > 3) { - ToJSContext(pThis, nullptr) - ->ThrowParamCountMismatchException(L"Num2GMTime"); - return; - } - - std::unique_ptr<CFXJSE_Value> timeValue = GetSimpleValue(pThis, args, 0); - if (timeValue->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - int32_t iTime = (int32_t)ValueToFloat(pThis, timeValue.get()); - if (abs(iTime) < 1.0) { - args.GetReturnValue()->SetNull(); - return; - } - - ByteString formatString; - if (argc > 1) { - std::unique_ptr<CFXJSE_Value> formatValue = GetSimpleValue(pThis, args, 1); - if (formatValue->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - formatString = ValueToUTF8String(formatValue.get()); - } - - ByteString localString; - if (argc > 2) { - std::unique_ptr<CFXJSE_Value> localValue = GetSimpleValue(pThis, args, 2); - if (localValue->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - localString = ValueToUTF8String(localValue.get()); - } - - ByteString szGMTTimeString = - Num2AllTime(pThis, iTime, formatString.AsStringView(), - localString.AsStringView(), true); - args.GetReturnValue()->SetString(szGMTTimeString.AsStringView()); -} - -// static -void CXFA_FM2JSContext::Num2Time(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc < 1 || argc > 3) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Num2Time"); - return; - } - - std::unique_ptr<CFXJSE_Value> timeValue = GetSimpleValue(pThis, args, 0); - if (timeValue->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - float fTime = ValueToFloat(pThis, timeValue.get()); - if (fabs(fTime) < 1.0) { - args.GetReturnValue()->SetNull(); - return; - } - - ByteString formatString; - if (argc > 1) { - std::unique_ptr<CFXJSE_Value> formatValue = GetSimpleValue(pThis, args, 1); - if (formatValue->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - formatString = ValueToUTF8String(formatValue.get()); - } - - ByteString localString; - if (argc > 2) { - std::unique_ptr<CFXJSE_Value> localValue = GetSimpleValue(pThis, args, 2); - if (localValue->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - localString = ValueToUTF8String(localValue.get()); - } - - ByteString szLocalTimeString = Num2AllTime(pThis, static_cast<int32_t>(fTime), - formatString.AsStringView(), - localString.AsStringView(), false); - args.GetReturnValue()->SetString(szLocalTimeString.AsStringView()); -} - -// static -void CXFA_FM2JSContext::Time(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 0) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Time"); - return; - } - - time_t now; - time(&now); - - struct tm* pGmt = gmtime(&now); - args.GetReturnValue()->SetInteger( - (pGmt->tm_hour * 3600 + pGmt->tm_min * 60 + pGmt->tm_sec) * 1000); -} - -// static -void CXFA_FM2JSContext::Time2Num(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc < 1 || argc > 3) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Time2Num"); - return; - } - - ByteString timeString; - std::unique_ptr<CFXJSE_Value> timeValue = GetSimpleValue(pThis, args, 0); - if (ValueIsNull(pThis, timeValue.get())) { - args.GetReturnValue()->SetNull(); - return; - } - timeString = ValueToUTF8String(timeValue.get()); - - ByteString formatString; - if (argc > 1) { - std::unique_ptr<CFXJSE_Value> formatValue = GetSimpleValue(pThis, args, 1); - if (ValueIsNull(pThis, formatValue.get())) { - args.GetReturnValue()->SetNull(); - return; - } - formatString = ValueToUTF8String(formatValue.get()); - } - - ByteString localString; - if (argc > 2) { - std::unique_ptr<CFXJSE_Value> localValue = GetSimpleValue(pThis, args, 2); - if (ValueIsNull(pThis, localValue.get())) { - args.GetReturnValue()->SetNull(); - return; - } - localString = ValueToUTF8String(localValue.get()); - } - - CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument(); - CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); - IFX_Locale* pLocale = nullptr; - if (localString.IsEmpty()) { - CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject()); - ASSERT(pThisNode); - CXFA_WidgetData widgetData(pThisNode); - pLocale = widgetData.GetLocal(); - } else { - pLocale = - pMgr->GetLocaleByName(WideString::FromUTF8(localString.AsStringView())); - } - - WideString wsFormat; - if (formatString.IsEmpty()) - wsFormat = pLocale->GetTimePattern(FX_LOCALEDATETIMESUBCATEGORY_Default); - else - wsFormat = WideString::FromUTF8(formatString.AsStringView()); - - wsFormat = L"time{" + wsFormat + L"}"; - CXFA_LocaleValue localeValue(XFA_VT_TIME, - WideString::FromUTF8(timeString.AsStringView()), - wsFormat, pLocale, pMgr); - if (!localeValue.IsValid()) { - args.GetReturnValue()->SetInteger(0); - return; - } - - CFX_DateTime uniTime = localeValue.GetTime(); - int32_t hour = uniTime.GetHour(); - int32_t min = uniTime.GetMinute(); - int32_t second = uniTime.GetSecond(); - int32_t milSecond = uniTime.GetMillisecond(); - int32_t mins = hour * 60 + min; - - mins -= (CXFA_TimeZoneProvider().GetTimeZone().tzHour * 60); - while (mins > 1440) - mins -= 1440; - - while (mins < 0) - mins += 1440; - - hour = mins / 60; - min = mins % 60; - args.GetReturnValue()->SetInteger(hour * 3600000 + min * 60000 + - second * 1000 + milSecond + 1); -} - -// static -void CXFA_FM2JSContext::TimeFmt(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc > 2) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"TimeFmt"); - return; - } - - int32_t iStyle = 0; - if (argc > 0) { - std::unique_ptr<CFXJSE_Value> argStyle = GetSimpleValue(pThis, args, 0); - if (argStyle->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - iStyle = (int32_t)ValueToFloat(pThis, argStyle.get()); - if (iStyle > 4 || iStyle < 0) - iStyle = 0; - } - - ByteString szLocal; - if (argc > 1) { - std::unique_ptr<CFXJSE_Value> argLocal = GetSimpleValue(pThis, args, 1); - if (argLocal->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - szLocal = ValueToUTF8String(argLocal.get()); - } - - ByteString formatStr = - GetStandardTimeFormat(pThis, iStyle, szLocal.AsStringView()); - args.GetReturnValue()->SetString(formatStr.AsStringView()); -} - -// static -bool CXFA_FM2JSContext::IsIsoDateFormat(const char* pData, - int32_t iLength, - int32_t& iStyle, - int32_t& iYear, - int32_t& iMonth, - int32_t& iDay) { - iYear = 0; - iMonth = 1; - iDay = 1; - - if (iLength < 4) - return false; - - char strYear[5]; - strYear[4] = '\0'; - for (int32_t i = 0; i < 4; ++i) { - if (!std::isdigit(pData[i])) - return false; - - strYear[i] = pData[i]; - } - iYear = FXSYS_atoi(strYear); - iStyle = 0; - if (iLength == 4) - return true; - - iStyle = pData[4] == '-' ? 1 : 0; - - char strTemp[3]; - strTemp[2] = '\0'; - int32_t iPosOff = iStyle == 0 ? 4 : 5; - if (!std::isdigit(pData[iPosOff]) || !std::isdigit(pData[iPosOff + 1])) - return false; - - strTemp[0] = pData[iPosOff]; - strTemp[1] = pData[iPosOff + 1]; - iMonth = FXSYS_atoi(strTemp); - if (iMonth > 12 || iMonth < 1) - return false; - - if (iStyle == 0) { - iPosOff += 2; - if (iLength == 6) - return true; - } else { - iPosOff += 3; - if (iLength == 7) - return true; - } - if (!std::isdigit(pData[iPosOff]) || !std::isdigit(pData[iPosOff + 1])) - return false; - - strTemp[0] = pData[iPosOff]; - strTemp[1] = pData[iPosOff + 1]; - iDay = FXSYS_atoi(strTemp); - if (iPosOff + 2 < iLength) - return false; - - if ((!(iYear % 4) && (iYear % 100)) || !(iYear % 400)) { - if (iMonth == 2 && iDay > 29) - return false; - } else { - if (iMonth == 2 && iDay > 28) - return false; - } - if (iMonth != 2) { - if (iMonth < 8) { - if (iDay > (iMonth % 2 == 0 ? 30 : 31)) - return false; - } else if (iDay > (iMonth % 2 == 0 ? 31 : 30)) { - return false; - } - } - return true; -} - -// static -bool CXFA_FM2JSContext::IsIsoTimeFormat(const char* pData, - int32_t iLength, - int32_t& iHour, - int32_t& iMinute, - int32_t& iSecond, - int32_t& iMilliSecond, - int32_t& iZoneHour, - int32_t& iZoneMinute) { - iHour = 0; - iMinute = 0; - iSecond = 0; - iMilliSecond = 0; - iZoneHour = 0; - iZoneMinute = 0; - if (!pData) - return false; - - char strTemp[3]; - strTemp[2] = '\0'; - int32_t iZone = 0; - int32_t i = 0; - while (i < iLength) { - if (!std::isdigit(pData[i]) && pData[i] != ':') { - iZone = i; - break; - } - ++i; - } - if (i == iLength) - iZone = iLength; - - int32_t iPos = 0; - int32_t iIndex = 0; - while (iIndex < iZone) { - if (!std::isdigit(pData[iIndex])) - return false; - - strTemp[0] = pData[iIndex]; - if (!std::isdigit(pData[iIndex + 1])) - return false; - - strTemp[1] = pData[iIndex + 1]; - if (FXSYS_atoi(strTemp) > 60) - return false; - - if (pData[2] == ':') { - if (iPos == 0) { - iHour = FXSYS_atoi(strTemp); - ++iPos; - } else if (iPos == 1) { - iMinute = FXSYS_atoi(strTemp); - ++iPos; - } else { - iSecond = FXSYS_atoi(strTemp); - } - iIndex += 3; - } else { - if (iPos == 0) { - iHour = FXSYS_atoi(strTemp); - ++iPos; - } else if (iPos == 1) { - iMinute = FXSYS_atoi(strTemp); - ++iPos; - } else if (iPos == 2) { - iSecond = FXSYS_atoi(strTemp); - ++iPos; - } - iIndex += 2; - } - } - - if (iIndex < iLength && pData[iIndex] == '.') { - constexpr int kSubSecondLength = 3; - if (iIndex + kSubSecondLength >= iLength) - return false; - - ++iIndex; - char strSec[kSubSecondLength + 1]; - for (int i = 0; i < kSubSecondLength; ++i) { - char c = pData[iIndex + i]; - if (!std::isdigit(c)) - return false; - strSec[i] = c; - } - strSec[kSubSecondLength] = '\0'; - - iMilliSecond = FXSYS_atoi(strSec); - if (iMilliSecond > 100) { - iMilliSecond = 0; - return false; - } - iIndex += kSubSecondLength; - } - - if (iIndex < iLength && FXSYS_tolower(pData[iIndex]) == 'z') - return true; - - int32_t iSign = 1; - if (iIndex < iLength) { - if (pData[iIndex] == '+') { - ++iIndex; - } else if (pData[iIndex] == '-') { - iSign = -1; - ++iIndex; - } - } - iPos = 0; - while (iIndex < iLength) { - if (!std::isdigit(pData[iIndex])) - return false; - - strTemp[0] = pData[iIndex]; - if (!std::isdigit(pData[iIndex + 1])) - return false; - - strTemp[1] = pData[iIndex + 1]; - if (FXSYS_atoi(strTemp) > 60) - return false; - - if (pData[2] == ':') { - if (iPos == 0) { - iZoneHour = FXSYS_atoi(strTemp); - } else if (iPos == 1) { - iZoneMinute = FXSYS_atoi(strTemp); - } - iIndex += 3; - } else { - if (!iPos) { - iZoneHour = FXSYS_atoi(strTemp); - ++iPos; - } else if (iPos == 1) { - iZoneMinute = FXSYS_atoi(strTemp); - ++iPos; - } - iIndex += 2; - } - } - if (iIndex < iLength) - return false; - - iZoneHour *= iSign; - return true; -} - -// static -bool CXFA_FM2JSContext::IsIsoDateTimeFormat(const char* pData, - int32_t iLength, - int32_t& iYear, - int32_t& iMonth, - int32_t& iDay, - int32_t& iHour, - int32_t& iMinute, - int32_t& iSecond, - int32_t& iMillionSecond, - int32_t& iZoneHour, - int32_t& iZoneMinute) { - iYear = 0; - iMonth = 0; - iDay = 0; - iHour = 0; - iMinute = 0; - iSecond = 0; - if (!pData) - return false; - - int32_t iIndex = 0; - while (pData[iIndex] != 'T' && pData[iIndex] != 't') { - if (iIndex >= iLength) - return false; - ++iIndex; - } - if (iIndex != 8 && iIndex != 10) - return false; - - int32_t iStyle = -1; - if (!IsIsoDateFormat(pData, iIndex, iStyle, iYear, iMonth, iDay)) - return false; - if (pData[iIndex] != 'T' && pData[iIndex] != 't') - return true; - - ++iIndex; - if (((iLength - iIndex > 13) && (iLength - iIndex < 6)) && - (iLength - iIndex != 15)) { - return true; - } - return IsIsoTimeFormat(pData + iIndex, iLength - iIndex, iHour, iMinute, - iSecond, iMillionSecond, iZoneHour, iZoneMinute); -} - -// static -ByteString CXFA_FM2JSContext::Local2IsoDate(CFXJSE_Value* pThis, - const ByteStringView& szDate, - const ByteStringView& szFormat, - const ByteStringView& szLocale) { - CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument(); - if (!pDoc) - return ByteString(); - - CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); - IFX_Locale* pLocale = LocaleFromString(pDoc, pMgr, szLocale); - if (!pLocale) - return ByteString(); - - WideString wsFormat = FormatFromString(pLocale, szFormat); - CFX_DateTime dt = CXFA_LocaleValue(XFA_VT_DATE, WideString::FromUTF8(szDate), - wsFormat, pLocale, pMgr) - .GetDate(); - - ByteString strIsoDate; - strIsoDate.Format("%4d-%02d-%02d", dt.GetYear(), dt.GetMonth(), dt.GetDay()); - return strIsoDate; -} - -// static -ByteString CXFA_FM2JSContext::IsoDate2Local(CFXJSE_Value* pThis, - const ByteStringView& szDate, - const ByteStringView& szFormat, - const ByteStringView& szLocale) { - CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument(); - if (!pDoc) - return ByteString(); - - CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); - IFX_Locale* pLocale = LocaleFromString(pDoc, pMgr, szLocale); - if (!pLocale) - return ByteString(); - - WideString wsFormat = FormatFromString(pLocale, szFormat); - WideString wsRet; - CXFA_LocaleValue(XFA_VT_DATE, WideString::FromUTF8(szDate), pMgr) - .FormatPatterns(wsRet, wsFormat, pLocale, XFA_VALUEPICTURE_Display); - return wsRet.UTF8Encode(); -} - -// static -ByteString CXFA_FM2JSContext::IsoTime2Local(CFXJSE_Value* pThis, - const ByteStringView& szTime, - const ByteStringView& szFormat, - const ByteStringView& szLocale) { - CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument(); - if (!pDoc) - return ByteString(); - - CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); - IFX_Locale* pLocale = LocaleFromString(pDoc, pMgr, szLocale); - if (!pLocale) - return ByteString(); - - WideString wsFormat = { - L"time{", FormatFromString(pLocale, szFormat).AsStringView(), L"}"}; - CXFA_LocaleValue widgetValue(XFA_VT_TIME, WideString::FromUTF8(szTime), pMgr); - WideString wsRet; - widgetValue.FormatPatterns(wsRet, wsFormat, pLocale, - XFA_VALUEPICTURE_Display); - return wsRet.UTF8Encode(); -} - -// static -int32_t CXFA_FM2JSContext::DateString2Num(const ByteStringView& szDateString) { - int32_t iLength = szDateString.GetLength(); - int32_t iYear = 0; - int32_t iMonth = 0; - int32_t iDay = 0; - if (iLength <= 10) { - int32_t iStyle = -1; - if (!IsIsoDateFormat(szDateString.unterminated_c_str(), iLength, iStyle, - iYear, iMonth, iDay)) { - return 0; - } - } else { - int32_t iHour = 0; - int32_t iMinute = 0; - int32_t iSecond = 0; - int32_t iMilliSecond = 0; - int32_t iZoneHour = 0; - int32_t iZoneMinute = 0; - if (!IsIsoDateTimeFormat(szDateString.unterminated_c_str(), iLength, iYear, - iMonth, iDay, iHour, iMinute, iSecond, - iMilliSecond, iZoneHour, iZoneMinute)) { - return 0; - } - } - - float dDays = 0; - int32_t i = 1; - if (iYear < 1900) - return 0; - - while (iYear - i >= 1900) { - dDays += - ((!((iYear - i) % 4) && ((iYear - i) % 100)) || !((iYear - i) % 400)) - ? 366 - : 365; - ++i; - } - i = 1; - while (i < iMonth) { - if (i == 2) - dDays += ((!(iYear % 4) && (iYear % 100)) || !(iYear % 400)) ? 29 : 28; - else if (i <= 7) - dDays += (i % 2 == 0) ? 30 : 31; - else - dDays += (i % 2 == 0) ? 31 : 30; - - ++i; - } - i = 0; - while (iDay - i > 0) { - dDays += 1; - ++i; - } - return (int32_t)dDays; -} - -// static -ByteString CXFA_FM2JSContext::GetLocalDateFormat(CFXJSE_Value* pThis, - int32_t iStyle, - const ByteStringView& szLocale, - bool bStandard) { - CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument(); - if (!pDoc) - return ByteString(); - - CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); - IFX_Locale* pLocale = LocaleFromString(pDoc, pMgr, szLocale); - if (!pLocale) - return ByteString(); - - WideString strRet = pLocale->GetDatePattern(SubCategoryFromInt(iStyle)); - if (!bStandard) { - AlternateDateTimeSymbols(strRet, pLocale->GetDateTimeSymbols(), - g_sAltTable_Date); - } - return strRet.UTF8Encode(); -} - -// static -ByteString CXFA_FM2JSContext::GetLocalTimeFormat(CFXJSE_Value* pThis, - int32_t iStyle, - const ByteStringView& szLocale, - bool bStandard) { - CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument(); - if (!pDoc) - return ByteString(); - - CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); - IFX_Locale* pLocale = LocaleFromString(pDoc, pMgr, szLocale); - if (!pLocale) - return ByteString(); - - WideString strRet = pLocale->GetTimePattern(SubCategoryFromInt(iStyle)); - if (!bStandard) { - AlternateDateTimeSymbols(strRet, pLocale->GetDateTimeSymbols(), - g_sAltTable_Time); - } - return strRet.UTF8Encode(); -} - -// static -ByteString CXFA_FM2JSContext::GetStandardDateFormat( - CFXJSE_Value* pThis, - int32_t iStyle, - const ByteStringView& szLocalStr) { - return GetLocalDateFormat(pThis, iStyle, szLocalStr, true); -} - -// static -ByteString CXFA_FM2JSContext::GetStandardTimeFormat( - CFXJSE_Value* pThis, - int32_t iStyle, - const ByteStringView& szLocalStr) { - return GetLocalTimeFormat(pThis, iStyle, szLocalStr, true); -} - -// static -ByteString CXFA_FM2JSContext::Num2AllTime(CFXJSE_Value* pThis, - int32_t iTime, - const ByteStringView& szFormat, - const ByteStringView& szLocale, - bool bGM) { - int32_t iHour = 0; - int32_t iMin = 0; - int32_t iSec = 0; - iHour = static_cast<int>(iTime) / 3600000; - iMin = (static_cast<int>(iTime) - iHour * 3600000) / 60000; - iSec = (static_cast<int>(iTime) - iHour * 3600000 - iMin * 60000) / 1000; - - if (!bGM) { - int32_t iZoneHour = 0; - int32_t iZoneMin = 0; - int32_t iZoneSec = 0; - GetLocalTimeZone(iZoneHour, iZoneMin, iZoneSec); - iHour += iZoneHour; - iMin += iZoneMin; - iSec += iZoneSec; - } - - ByteString strIsoTime; - strIsoTime.Format("%02d:%02d:%02d", iHour, iMin, iSec); - return IsoTime2Local(pThis, strIsoTime.AsStringView(), szFormat, szLocale); -} - -// static -void CXFA_FM2JSContext::GetLocalTimeZone(int32_t& iHour, - int32_t& iMin, - int32_t& iSec) { - time_t now; - time(&now); - - struct tm* pGmt = gmtime(&now); - struct tm* pLocal = localtime(&now); - iHour = pLocal->tm_hour - pGmt->tm_hour; - iMin = pLocal->tm_min - pGmt->tm_min; - iSec = pLocal->tm_sec - pGmt->tm_sec; -} - -// static -void CXFA_FM2JSContext::Apr(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 3) { - pContext->ThrowParamCountMismatchException(L"Apr"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); - if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get()) || - ValueIsNull(pThis, argThree.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - double nPrincipal = ValueToDouble(pThis, argOne.get()); - double nPayment = ValueToDouble(pThis, argTwo.get()); - double nPeriods = ValueToDouble(pThis, argThree.get()); - if (nPrincipal <= 0 || nPayment <= 0 || nPeriods <= 0) { - pContext->ThrowArgumentMismatchException(); - return; - } - - double r = 2 * (nPeriods * nPayment - nPrincipal) / (nPeriods * nPrincipal); - double nTemp = 1; - for (int32_t i = 0; i < nPeriods; ++i) - nTemp *= (1 + r); - - double nRet = r * nTemp / (nTemp - 1) - nPayment / nPrincipal; - while (fabs(nRet) > kFinancialPrecision) { - double nDerivative = - ((nTemp + r * nPeriods * (nTemp / (1 + r))) * (nTemp - 1) - - (r * nTemp * nPeriods * (nTemp / (1 + r)))) / - ((nTemp - 1) * (nTemp - 1)); - if (nDerivative == 0) { - args.GetReturnValue()->SetNull(); - return; - } - - r = r - nRet / nDerivative; - nTemp = 1; - for (int32_t i = 0; i < nPeriods; ++i) { - nTemp *= (1 + r); - } - nRet = r * nTemp / (nTemp - 1) - nPayment / nPrincipal; - } - args.GetReturnValue()->SetDouble(r * 12); -} - -// static -void CXFA_FM2JSContext::CTerm(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 3) { - pContext->ThrowParamCountMismatchException(L"CTerm"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); - if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get()) || - ValueIsNull(pThis, argThree.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - float nRate = ValueToFloat(pThis, argOne.get()); - float nFutureValue = ValueToFloat(pThis, argTwo.get()); - float nInitAmount = ValueToFloat(pThis, argThree.get()); - if ((nRate <= 0) || (nFutureValue <= 0) || (nInitAmount <= 0)) { - pContext->ThrowArgumentMismatchException(); - return; - } - - args.GetReturnValue()->SetFloat(log((float)(nFutureValue / nInitAmount)) / - log((float)(1 + nRate))); -} - -// static -void CXFA_FM2JSContext::FV(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 3) { - pContext->ThrowParamCountMismatchException(L"FV"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); - if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get()) || - ValueIsNull(pThis, argThree.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - double nAmount = ValueToDouble(pThis, argOne.get()); - double nRate = ValueToDouble(pThis, argTwo.get()); - double nPeriod = ValueToDouble(pThis, argThree.get()); - if ((nRate < 0) || (nPeriod <= 0) || (nAmount <= 0)) { - pContext->ThrowArgumentMismatchException(); - return; - } - - double dResult = 0; - if (nRate) { - double nTemp = 1; - for (int i = 0; i < nPeriod; ++i) { - nTemp *= 1 + nRate; - } - dResult = nAmount * (nTemp - 1) / nRate; - } else { - dResult = nAmount * nPeriod; - } - - args.GetReturnValue()->SetDouble(dResult); -} - -// static -void CXFA_FM2JSContext::IPmt(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 5) { - pContext->ThrowParamCountMismatchException(L"IPmt"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); - std::unique_ptr<CFXJSE_Value> argFour = GetSimpleValue(pThis, args, 3); - std::unique_ptr<CFXJSE_Value> argFive = GetSimpleValue(pThis, args, 4); - if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get()) || - ValueIsNull(pThis, argThree.get()) || ValueIsNull(pThis, argFour.get()) || - ValueIsNull(pThis, argFive.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - float nPrincipalAmount = ValueToFloat(pThis, argOne.get()); - float nRate = ValueToFloat(pThis, argTwo.get()); - float nPayment = ValueToFloat(pThis, argThree.get()); - float nFirstMonth = ValueToFloat(pThis, argFour.get()); - float nNumberOfMonths = ValueToFloat(pThis, argFive.get()); - if ((nPrincipalAmount <= 0) || (nRate <= 0) || (nPayment <= 0) || - (nFirstMonth < 0) || (nNumberOfMonths < 0)) { - pContext->ThrowArgumentMismatchException(); - return; - } - - float nRateOfMonth = nRate / 12; - int32_t iNums = - (int32_t)((log10((float)(nPayment / nPrincipalAmount)) - - log10((float)(nPayment / nPrincipalAmount - nRateOfMonth))) / - log10((float)(1 + nRateOfMonth))); - int32_t iEnd = std::min((int32_t)(nFirstMonth + nNumberOfMonths - 1), iNums); - - if (nPayment < nPrincipalAmount * nRateOfMonth) { - args.GetReturnValue()->SetFloat(0); - return; - } - - int32_t i = 0; - for (i = 0; i < nFirstMonth - 1; ++i) - nPrincipalAmount -= nPayment - nPrincipalAmount * nRateOfMonth; - - float nSum = 0; - for (; i < iEnd; ++i) { - nSum += nPrincipalAmount * nRateOfMonth; - nPrincipalAmount -= nPayment - nPrincipalAmount * nRateOfMonth; - } - args.GetReturnValue()->SetFloat(nSum); -} - -// static -void CXFA_FM2JSContext::NPV(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - int32_t argc = args.GetLength(); - if (argc < 3) { - pContext->ThrowParamCountMismatchException(L"NPV"); - return; - } - - std::vector<std::unique_ptr<CFXJSE_Value>> argValues; - for (int32_t i = 0; i < argc; i++) { - argValues.push_back(GetSimpleValue(pThis, args, i)); - if (ValueIsNull(pThis, argValues[i].get())) { - args.GetReturnValue()->SetNull(); - return; - } - } - - double nRate = ValueToDouble(pThis, argValues[0].get()); - if (nRate <= 0) { - pContext->ThrowArgumentMismatchException(); - return; - } - - std::vector<double> data(argc - 1); - for (int32_t i = 1; i < argc; i++) - data.push_back(ValueToDouble(pThis, argValues[i].get())); - - double nSum = 0; - int32_t iIndex = 0; - for (int32_t i = 0; i < argc - 1; i++) { - double nTemp = 1; - for (int32_t j = 0; j <= i; j++) - nTemp *= 1 + nRate; - - double nNum = data[iIndex++]; - nSum += nNum / nTemp; - } - args.GetReturnValue()->SetDouble(nSum); -} - -// static -void CXFA_FM2JSContext::Pmt(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 3) { - pContext->ThrowParamCountMismatchException(L"Pmt"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); - if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get()) || - ValueIsNull(pThis, argThree.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - float nPrincipal = ValueToFloat(pThis, argOne.get()); - float nRate = ValueToFloat(pThis, argTwo.get()); - float nPeriods = ValueToFloat(pThis, argThree.get()); - if ((nPrincipal <= 0) || (nRate <= 0) || (nPeriods <= 0)) { - pContext->ThrowArgumentMismatchException(); - return; - } - - float nTmp = 1 + nRate; - float nSum = nTmp; - for (int32_t i = 0; i < nPeriods - 1; ++i) - nSum *= nTmp; - - args.GetReturnValue()->SetFloat((nPrincipal * nRate * nSum) / (nSum - 1)); -} - -// static -void CXFA_FM2JSContext::PPmt(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 5) { - pContext->ThrowParamCountMismatchException(L"PPmt"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); - std::unique_ptr<CFXJSE_Value> argFour = GetSimpleValue(pThis, args, 3); - std::unique_ptr<CFXJSE_Value> argFive = GetSimpleValue(pThis, args, 4); - if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get()) || - ValueIsNull(pThis, argThree.get()) || ValueIsNull(pThis, argFour.get()) || - ValueIsNull(pThis, argFive.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - float nPrincipalAmount = ValueToFloat(pThis, argOne.get()); - float nRate = ValueToFloat(pThis, argTwo.get()); - float nPayment = ValueToFloat(pThis, argThree.get()); - float nFirstMonth = ValueToFloat(pThis, argFour.get()); - float nNumberOfMonths = ValueToFloat(pThis, argFive.get()); - if ((nPrincipalAmount <= 0) || (nRate <= 0) || (nPayment <= 0) || - (nFirstMonth < 0) || (nNumberOfMonths < 0)) { - pContext->ThrowArgumentMismatchException(); - return; - } - - float nRateOfMonth = nRate / 12; - int32_t iNums = - (int32_t)((log10((float)(nPayment / nPrincipalAmount)) - - log10((float)(nPayment / nPrincipalAmount - nRateOfMonth))) / - log10((float)(1 + nRateOfMonth))); - int32_t iEnd = std::min((int32_t)(nFirstMonth + nNumberOfMonths - 1), iNums); - if (nPayment < nPrincipalAmount * nRateOfMonth) { - pContext->ThrowArgumentMismatchException(); - return; - } - - int32_t i = 0; - for (i = 0; i < nFirstMonth - 1; ++i) - nPrincipalAmount -= nPayment - nPrincipalAmount * nRateOfMonth; - - float nTemp = 0; - float nSum = 0; - for (; i < iEnd; ++i) { - nTemp = nPayment - nPrincipalAmount * nRateOfMonth; - nSum += nTemp; - nPrincipalAmount -= nTemp; - } - args.GetReturnValue()->SetFloat(nSum); -} - -// static -void CXFA_FM2JSContext::PV(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 3) { - pContext->ThrowParamCountMismatchException(L"PV"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); - if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get()) || - ValueIsNull(pThis, argThree.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - double nAmount = ValueToDouble(pThis, argOne.get()); - double nRate = ValueToDouble(pThis, argTwo.get()); - double nPeriod = ValueToDouble(pThis, argThree.get()); - if ((nAmount <= 0) || (nRate < 0) || (nPeriod <= 0)) { - pContext->ThrowArgumentMismatchException(); - return; - } - - double nTemp = 1; - for (int32_t i = 0; i < nPeriod; ++i) - nTemp *= 1 + nRate; - - nTemp = 1 / nTemp; - args.GetReturnValue()->SetDouble(nAmount * ((1 - nTemp) / nRate)); -} - -// static -void CXFA_FM2JSContext::Rate(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 3) { - pContext->ThrowParamCountMismatchException(L"Rate"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); - if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get()) || - ValueIsNull(pThis, argThree.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - float nFuture = ValueToFloat(pThis, argOne.get()); - float nPresent = ValueToFloat(pThis, argTwo.get()); - float nTotalNumber = ValueToFloat(pThis, argThree.get()); - if ((nFuture <= 0) || (nPresent < 0) || (nTotalNumber <= 0)) { - pContext->ThrowArgumentMismatchException(); - return; - } - - args.GetReturnValue()->SetFloat( - FXSYS_pow((float)(nFuture / nPresent), (float)(1 / nTotalNumber)) - 1); -} - -// static -void CXFA_FM2JSContext::Term(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 3) { - pContext->ThrowParamCountMismatchException(L"Term"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); - if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get()) || - ValueIsNull(pThis, argThree.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - float nMount = ValueToFloat(pThis, argOne.get()); - float nRate = ValueToFloat(pThis, argTwo.get()); - float nFuture = ValueToFloat(pThis, argThree.get()); - if ((nMount <= 0) || (nRate <= 0) || (nFuture <= 0)) { - pContext->ThrowArgumentMismatchException(); - return; - } - - args.GetReturnValue()->SetFloat(log((float)(nFuture / nMount * nRate) + 1) / - log((float)(1 + nRate))); -} - -// static -void CXFA_FM2JSContext::Choose(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - int32_t argc = args.GetLength(); - if (argc < 2) { - pContext->ThrowParamCountMismatchException(L"Choose"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); - if (ValueIsNull(pThis, argOne.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - int32_t iIndex = (int32_t)ValueToFloat(pThis, argOne.get()); - if (iIndex < 1) { - args.GetReturnValue()->SetString(""); - return; - } - - bool bFound = false; - bool bStopCounterFlags = false; - int32_t iArgIndex = 1; - int32_t iValueIndex = 0; - v8::Isolate* pIsolate = pContext->GetScriptRuntime(); - while (!bFound && !bStopCounterFlags && (iArgIndex < argc)) { - std::unique_ptr<CFXJSE_Value> argIndexValue = args.GetValue(iArgIndex); - if (argIndexValue->IsArray()) { - auto lengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argIndexValue->GetObjectProperty("length", lengthValue.get()); - int32_t iLength = lengthValue->ToInteger(); - if (iLength > 3) - bStopCounterFlags = true; - - iValueIndex += (iLength - 2); - if (iValueIndex >= iIndex) { - auto propertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto jsObjectValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto newPropertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argIndexValue->GetObjectPropertyByIdx(1, propertyValue.get()); - argIndexValue->GetObjectPropertyByIdx( - (iLength - 1) - (iValueIndex - iIndex), jsObjectValue.get()); - if (propertyValue->IsNull()) { - GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get()); - } else { - jsObjectValue->GetObjectProperty( - propertyValue->ToString().AsStringView(), newPropertyValue.get()); - } - ByteString bsChosen = ValueToUTF8String(newPropertyValue.get()); - args.GetReturnValue()->SetString(bsChosen.AsStringView()); - bFound = true; - } - } else { - iValueIndex++; - if (iValueIndex == iIndex) { - ByteString bsChosen = ValueToUTF8String(argIndexValue.get()); - args.GetReturnValue()->SetString(bsChosen.AsStringView()); - bFound = true; - } - } - iArgIndex++; - } - if (!bFound) - args.GetReturnValue()->SetString(""); -} - -// static -void CXFA_FM2JSContext::Exists(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 1) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Exists"); - return; - } - args.GetReturnValue()->SetInteger(args.GetValue(0)->IsObject()); -} - -// static -void CXFA_FM2JSContext::HasValue(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 1) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"HasValue"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - if (!argOne->IsString()) { - args.GetReturnValue()->SetInteger(argOne->IsNumber() || - argOne->IsBoolean()); - return; - } - - ByteString valueStr = argOne->ToString(); - valueStr.TrimLeft(); - args.GetReturnValue()->SetInteger(!valueStr.IsEmpty()); -} - -// static -void CXFA_FM2JSContext::Oneof(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() < 2) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Oneof"); - return; - } - - bool bFlags = false; - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - std::vector<std::unique_ptr<CFXJSE_Value>> parameterValues; - unfoldArgs(pThis, args, ¶meterValues, 1); - for (const auto& value : parameterValues) { - if (simpleValueCompare(pThis, argOne.get(), value.get())) { - bFlags = true; - break; - } - } - - args.GetReturnValue()->SetInteger(bFlags); -} - -// static -void CXFA_FM2JSContext::Within(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 3) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Within"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - if (argOne->IsNull()) { - args.GetReturnValue()->SetUndefined(); - return; - } - - std::unique_ptr<CFXJSE_Value> argLow = GetSimpleValue(pThis, args, 1); - std::unique_ptr<CFXJSE_Value> argHigh = GetSimpleValue(pThis, args, 2); - if (argOne->IsNumber()) { - float oneNumber = ValueToFloat(pThis, argOne.get()); - float lowNumber = ValueToFloat(pThis, argLow.get()); - float heightNumber = ValueToFloat(pThis, argHigh.get()); - args.GetReturnValue()->SetInteger((oneNumber >= lowNumber) && - (oneNumber <= heightNumber)); - return; - } - - ByteString oneString = ValueToUTF8String(argOne.get()); - ByteString lowString = ValueToUTF8String(argLow.get()); - ByteString heightString = ValueToUTF8String(argHigh.get()); - args.GetReturnValue()->SetInteger( - (oneString.Compare(lowString.AsStringView()) >= 0) && - (oneString.Compare(heightString.AsStringView()) <= 0)); -} - -// static -void CXFA_FM2JSContext::If(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 3) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"If"); - return; - } - - args.GetReturnValue()->Assign(GetSimpleValue(pThis, args, 0)->ToBoolean() - ? GetSimpleValue(pThis, args, 1).get() - : GetSimpleValue(pThis, args, 2).get()); -} - -// static -void CXFA_FM2JSContext::Eval(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 1) { - pContext->ThrowParamCountMismatchException(L"Eval"); - return; - } - - v8::Isolate* pIsolate = pContext->GetScriptRuntime(); - std::unique_ptr<CFXJSE_Value> scriptValue = GetSimpleValue(pThis, args, 0); - ByteString utf8ScriptString = ValueToUTF8String(scriptValue.get()); - if (utf8ScriptString.IsEmpty()) { - args.GetReturnValue()->SetNull(); - return; - } - - CFX_WideTextBuf wsJavaScriptBuf; - if (!CXFA_FM2JSContext::Translate( - WideString::FromUTF8(utf8ScriptString.AsStringView()).AsStringView(), - &wsJavaScriptBuf)) { - pContext->ThrowCompilerErrorException(); - return; - } - - std::unique_ptr<CFXJSE_Context> pNewContext( - CFXJSE_Context::Create(pIsolate, nullptr, nullptr)); - - auto returnValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - pNewContext->ExecuteScript( - FX_UTF8Encode(wsJavaScriptBuf.AsStringView()).c_str(), returnValue.get()); - - args.GetReturnValue()->Assign(returnValue.get()); -} - -// static -void CXFA_FM2JSContext::Ref(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - v8::Isolate* pIsolate = pContext->GetScriptRuntime(); - if (args.GetLength() != 1) { - pContext->ThrowParamCountMismatchException(L"Ref"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); - if (!argOne->IsArray() && !argOne->IsObject() && !argOne->IsBoolean() && - !argOne->IsString() && !argOne->IsNull() && !argOne->IsNumber()) { - pContext->ThrowArgumentMismatchException(); - return; - } - - if (argOne->IsBoolean() || argOne->IsString() || argOne->IsNumber()) { - args.GetReturnValue()->Assign(argOne.get()); - return; - } - - std::vector<std::unique_ptr<CFXJSE_Value>> values; - for (int32_t i = 0; i < 3; i++) - values.push_back(pdfium::MakeUnique<CFXJSE_Value>(pIsolate)); - - int intVal = 3; - if (argOne->IsNull()) { - // TODO(dsinclair): Why is this 4 when the others are all 3? - intVal = 4; - values[2]->SetNull(); - } else if (argOne->IsArray()) { -#ifndef NDEBUG - auto lengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argOne->GetObjectProperty("length", lengthValue.get()); - ASSERT(lengthValue->ToInteger() >= 3); -#endif - - auto propertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto jsObjectValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argOne->GetObjectPropertyByIdx(1, propertyValue.get()); - argOne->GetObjectPropertyByIdx(2, jsObjectValue.get()); - if (!propertyValue->IsNull() || jsObjectValue->IsNull()) { - pContext->ThrowArgumentMismatchException(); - return; - } - - values[2]->Assign(jsObjectValue.get()); - } else if (argOne->IsObject()) { - values[2]->Assign(argOne.get()); - } - - values[0]->SetInteger(intVal); - values[1]->SetNull(); - args.GetReturnValue()->SetArray(values); -} - -// static -void CXFA_FM2JSContext::UnitType(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 1) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"UnitType"); - return; - } - - std::unique_ptr<CFXJSE_Value> unitspanValue = GetSimpleValue(pThis, args, 0); - if (unitspanValue->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - - ByteString unitspanString = ValueToUTF8String(unitspanValue.get()); - if (unitspanString.IsEmpty()) { - args.GetReturnValue()->SetString("in"); - return; - } - - enum XFA_FM2JS_VALUETYPE_ParserStatus { - VALUETYPE_START, - VALUETYPE_HAVEINVALIDCHAR, - VALUETYPE_HAVEDIGIT, - VALUETYPE_HAVEDIGITWHITE, - VALUETYPE_ISCM, - VALUETYPE_ISMM, - VALUETYPE_ISPT, - VALUETYPE_ISMP, - VALUETYPE_ISIN, - }; - unitspanString.MakeLower(); - WideString wsTypeString = WideString::FromUTF8(unitspanString.AsStringView()); - const wchar_t* pData = wsTypeString.c_str(); - int32_t u = 0; - int32_t uLen = wsTypeString.GetLength(); - while (IsWhitespace(pData[u])) - u++; - - XFA_FM2JS_VALUETYPE_ParserStatus eParserStatus = VALUETYPE_START; - wchar_t typeChar; - // TODO(dsinclair): Cleanup this parser, figure out what the various checks - // are for. - while (u < uLen) { - typeChar = pData[u]; - if (IsWhitespace(typeChar)) { - if (eParserStatus != VALUETYPE_HAVEDIGIT && - eParserStatus != VALUETYPE_HAVEDIGITWHITE) { - eParserStatus = VALUETYPE_ISIN; - break; - } - eParserStatus = VALUETYPE_HAVEDIGITWHITE; - } else if (IsPartOfNumberW(typeChar)) { - if (eParserStatus == VALUETYPE_HAVEDIGITWHITE) { - eParserStatus = VALUETYPE_ISIN; - break; - } - eParserStatus = VALUETYPE_HAVEDIGIT; - } else if ((typeChar == 'c' || typeChar == 'p') && (u + 1 < uLen)) { - wchar_t nextChar = pData[u + 1]; - if ((eParserStatus == VALUETYPE_START || - eParserStatus == VALUETYPE_HAVEDIGIT || - eParserStatus == VALUETYPE_HAVEDIGITWHITE) && - !IsPartOfNumberW(nextChar)) { - eParserStatus = (typeChar == 'c') ? VALUETYPE_ISCM : VALUETYPE_ISPT; - break; - } - eParserStatus = VALUETYPE_HAVEINVALIDCHAR; - } else if (typeChar == 'm' && (u + 1 < uLen)) { - wchar_t nextChar = pData[u + 1]; - if ((eParserStatus == VALUETYPE_START || - eParserStatus == VALUETYPE_HAVEDIGIT || - eParserStatus == VALUETYPE_HAVEDIGITWHITE) && - !IsPartOfNumberW(nextChar)) { - eParserStatus = VALUETYPE_ISMM; - if (nextChar == 'p' || ((u + 5 < uLen) && pData[u + 1] == 'i' && - pData[u + 2] == 'l' && pData[u + 3] == 'l' && - pData[u + 4] == 'i' && pData[u + 5] == 'p')) { - eParserStatus = VALUETYPE_ISMP; - } - break; - } - } else { - eParserStatus = VALUETYPE_HAVEINVALIDCHAR; - } - u++; - } - switch (eParserStatus) { - case VALUETYPE_ISCM: - args.GetReturnValue()->SetString("cm"); - break; - case VALUETYPE_ISMM: - args.GetReturnValue()->SetString("mm"); - break; - case VALUETYPE_ISPT: - args.GetReturnValue()->SetString("pt"); - break; - case VALUETYPE_ISMP: - args.GetReturnValue()->SetString("mp"); - break; - default: - args.GetReturnValue()->SetString("in"); - break; - } -} - -// static -void CXFA_FM2JSContext::UnitValue(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc < 1 || argc > 2) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"UnitValue"); - return; - } - - std::unique_ptr<CFXJSE_Value> unitspanValue = GetSimpleValue(pThis, args, 0); - if (unitspanValue->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - - ByteString unitspanString = ValueToUTF8String(unitspanValue.get()); - const char* pData = unitspanString.c_str(); - if (!pData) { - args.GetReturnValue()->SetInteger(0); - return; - } - - size_t u = 0; - while (IsWhitespace(pData[u])) - ++u; - - while (u < unitspanString.GetLength()) { - if (!IsPartOfNumber(pData[u])) - break; - ++u; - } - - char* pTemp = nullptr; - double dFirstNumber = strtod(pData, &pTemp); - while (IsWhitespace(pData[u])) - ++u; - - size_t uLen = unitspanString.GetLength(); - ByteString strFirstUnit; - while (u < uLen) { - if (pData[u] == ' ') - break; - - strFirstUnit += pData[u]; - ++u; - } - strFirstUnit.MakeLower(); - - ByteString strUnit; - if (argc > 1) { - std::unique_ptr<CFXJSE_Value> unitValue = GetSimpleValue(pThis, args, 1); - ByteString unitTempString = ValueToUTF8String(unitValue.get()); - const char* pChar = unitTempString.c_str(); - size_t uVal = 0; - while (IsWhitespace(pChar[uVal])) - ++uVal; - - while (uVal < unitTempString.GetLength()) { - if (!std::isdigit(pChar[uVal]) && pChar[uVal] != '.') - break; - ++uVal; - } - while (IsWhitespace(pChar[uVal])) - ++uVal; - - size_t uValLen = unitTempString.GetLength(); - while (uVal < uValLen) { - if (pChar[uVal] == ' ') - break; - - strUnit += pChar[uVal]; - ++uVal; - } - strUnit.MakeLower(); - } else { - strUnit = strFirstUnit; - } - - double dResult = 0; - if (strFirstUnit == "in" || strFirstUnit == "inches") { - if (strUnit == "mm" || strUnit == "millimeters") - dResult = dFirstNumber * 25.4; - else if (strUnit == "cm" || strUnit == "centimeters") - dResult = dFirstNumber * 2.54; - else if (strUnit == "pt" || strUnit == "points") - dResult = dFirstNumber / 72; - else if (strUnit == "mp" || strUnit == "millipoints") - dResult = dFirstNumber / 72000; - else - dResult = dFirstNumber; - } else if (strFirstUnit == "mm" || strFirstUnit == "millimeters") { - if (strUnit == "mm" || strUnit == "millimeters") - dResult = dFirstNumber; - else if (strUnit == "cm" || strUnit == "centimeters") - dResult = dFirstNumber / 10; - else if (strUnit == "pt" || strUnit == "points") - dResult = dFirstNumber / 25.4 / 72; - else if (strUnit == "mp" || strUnit == "millipoints") - dResult = dFirstNumber / 25.4 / 72000; - else - dResult = dFirstNumber / 25.4; - } else if (strFirstUnit == "cm" || strFirstUnit == "centimeters") { - if (strUnit == "mm" || strUnit == "millimeters") - dResult = dFirstNumber * 10; - else if (strUnit == "cm" || strUnit == "centimeters") - dResult = dFirstNumber; - else if (strUnit == "pt" || strUnit == "points") - dResult = dFirstNumber / 2.54 / 72; - else if (strUnit == "mp" || strUnit == "millipoints") - dResult = dFirstNumber / 2.54 / 72000; - else - dResult = dFirstNumber / 2.54; - } else if (strFirstUnit == "pt" || strFirstUnit == "points") { - if (strUnit == "mm" || strUnit == "millimeters") - dResult = dFirstNumber / 72 * 25.4; - else if (strUnit == "cm" || strUnit == "centimeters") - dResult = dFirstNumber / 72 * 2.54; - else if (strUnit == "pt" || strUnit == "points") - dResult = dFirstNumber; - else if (strUnit == "mp" || strUnit == "millipoints") - dResult = dFirstNumber * 1000; - else - dResult = dFirstNumber / 72; - } else if (strFirstUnit == "mp" || strFirstUnit == "millipoints") { - if (strUnit == "mm" || strUnit == "millimeters") - dResult = dFirstNumber / 72000 * 25.4; - else if (strUnit == "cm" || strUnit == "centimeters") - dResult = dFirstNumber / 72000 * 2.54; - else if (strUnit == "pt" || strUnit == "points") - dResult = dFirstNumber / 1000; - else if (strUnit == "mp" || strUnit == "millipoints") - dResult = dFirstNumber; - else - dResult = dFirstNumber / 72000; - } - args.GetReturnValue()->SetDouble(dResult); -} - -// static -void CXFA_FM2JSContext::At(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 2) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"At"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - ByteString stringTwo = ValueToUTF8String(argTwo.get()); - if (stringTwo.IsEmpty()) { - args.GetReturnValue()->SetInteger(1); - return; - } - - ByteString stringOne = ValueToUTF8String(argOne.get()); - auto pos = stringOne.Find(stringTwo.AsStringView()); - args.GetReturnValue()->SetInteger(pos.has_value() ? pos.value() + 1 : 0); -} - -// static -void CXFA_FM2JSContext::Concat(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc < 1) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Concat"); - return; - } - - ByteString resultString; - bool bAllNull = true; - for (int32_t i = 0; i < argc; i++) { - std::unique_ptr<CFXJSE_Value> value = GetSimpleValue(pThis, args, i); - if (ValueIsNull(pThis, value.get())) - continue; - - bAllNull = false; - resultString += ValueToUTF8String(value.get()); - } - - if (bAllNull) { - args.GetReturnValue()->SetNull(); - return; - } - - args.GetReturnValue()->SetString(resultString.AsStringView()); -} - -// static -void CXFA_FM2JSContext::Decode(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc < 1 || argc > 2) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Decode"); - return; - } - - if (argc == 1) { - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - if (ValueIsNull(pThis, argOne.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - WideString decoded = DecodeURL( - WideString::FromUTF8(ValueToUTF8String(argOne.get()).AsStringView())); - - args.GetReturnValue()->SetString( - FX_UTF8Encode(decoded.AsStringView()).AsStringView()); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - ByteString toDecodeString = ValueToUTF8String(argOne.get()); - ByteString identifyString = ValueToUTF8String(argTwo.get()); - WideString decoded; - - WideString toDecodeWideString = - WideString::FromUTF8(toDecodeString.AsStringView()); - - if (identifyString.EqualNoCase("html")) - decoded = DecodeHTML(toDecodeWideString); - else if (identifyString.EqualNoCase("xml")) - decoded = DecodeXML(toDecodeWideString); - else - decoded = DecodeURL(toDecodeWideString); - - args.GetReturnValue()->SetString( - FX_UTF8Encode(decoded.AsStringView()).AsStringView()); -} - -// static -WideString CXFA_FM2JSContext::DecodeURL(const WideString& wsURLString) { - const wchar_t* pData = wsURLString.c_str(); - size_t i = 0; - CFX_WideTextBuf wsResultBuf; - while (i < wsURLString.GetLength()) { - wchar_t ch = pData[i]; - if ('%' != ch) { - wsResultBuf.AppendChar(ch); - ++i; - continue; - } - - wchar_t chTemp = 0; - int32_t iCount = 0; - while (iCount < 2) { - ++i; - ch = pData[i]; - if (ch <= '9' && ch >= '0') { - // TODO(dsinclair): Premultiply and add rather then scale. - chTemp += (ch - '0') * (!iCount ? 16 : 1); - } else if (ch <= 'F' && ch >= 'A') { - chTemp += (ch - 'A' + 10) * (!iCount ? 16 : 1); - } else if (ch <= 'f' && ch >= 'a') { - chTemp += (ch - 'a' + 10) * (!iCount ? 16 : 1); - } else { - return WideString(); - } - ++iCount; - } - wsResultBuf.AppendChar(chTemp); - ++i; - } - wsResultBuf.AppendChar(0); - return wsResultBuf.MakeString(); -} - -// static -WideString CXFA_FM2JSContext::DecodeHTML(const WideString& wsHTMLString) { - wchar_t strString[9]; - size_t iStrIndex = 0; - size_t iLen = wsHTMLString.GetLength(); - size_t i = 0; - int32_t iCode = 0; - const wchar_t* pData = wsHTMLString.c_str(); - CFX_WideTextBuf wsResultBuf; - while (i < iLen) { - wchar_t ch = pData[i]; - if (ch != '&') { - wsResultBuf.AppendChar(ch); - ++i; - continue; - } - - ++i; - ch = pData[i]; - if (ch == '#') { - ++i; - ch = pData[i]; - if (ch != 'x' && ch != 'X') { - return WideString(); - } - - ++i; - ch = pData[i]; - if ((ch >= '0' && ch <= '9') || (ch <= 'f' && ch >= 'a') || - (ch <= 'F' && ch >= 'A')) { - while (ch != ';' && i < iLen) { - if (ch >= '0' && ch <= '9') { - iCode += ch - '0'; - } else if (ch <= 'f' && ch >= 'a') { - iCode += ch - 'a' + 10; - } else if (ch <= 'F' && ch >= 'A') { - iCode += ch - 'A' + 10; - } else { - return WideString(); - } - ++i; - // TODO(dsinclair): Postmultiply seems wrong, start at zero - // and pre-multiply then can remove the post divide. - iCode *= 16; - ch = pData[i]; - } - iCode /= 16; - } - } else { - while (ch != ';' && i < iLen) { - strString[iStrIndex++] = ch; - ++i; - ch = pData[i]; - } - strString[iStrIndex] = 0; - } - uint32_t iData = 0; - if (HTMLSTR2Code(strString, &iData)) { - wsResultBuf.AppendChar((wchar_t)iData); - } else { - wsResultBuf.AppendChar(iCode); - } - iStrIndex = 0; - strString[iStrIndex] = 0; - ++i; - } - wsResultBuf.AppendChar(0); - - return wsResultBuf.MakeString(); -} - -// static -WideString CXFA_FM2JSContext::DecodeXML(const WideString& wsXMLString) { - wchar_t strString[9]; - int32_t iStrIndex = 0; - int32_t iLen = wsXMLString.GetLength(); - int32_t i = 0; - int32_t iCode = 0; - wchar_t ch = 0; - const wchar_t* pData = wsXMLString.c_str(); - CFX_WideTextBuf wsResultBuf; - while (i < iLen) { - ch = pData[i]; - if (ch != '&') { - wsResultBuf.AppendChar(ch); - ++i; - continue; - } - - // TODO(dsinclair): This is very similar to DecodeHTML, can they be - // combined? - ++i; - ch = pData[i]; - if (ch == '#') { - ++i; - ch = pData[i]; - if (ch != 'x' && ch != 'X') { - return WideString(); - } - - ++i; - ch = pData[i]; - if ((ch >= '0' && ch <= '9') || (ch <= 'f' && ch >= 'a') || - (ch <= 'F' && ch >= 'A')) { - while (ch != ';') { - if (ch >= '0' && ch <= '9') { - iCode += ch - '0'; - } else if (ch <= 'f' && ch >= 'a') { - iCode += ch - 'a' + 10; - } else if (ch <= 'F' && ch >= 'A') { - iCode += ch - 'A' + 10; - } else { - return WideString(); - } - ++i; - iCode *= 16; - ch = pData[i]; - } - iCode /= 16; - } - } else { - while (ch != ';' && i < iLen) { - strString[iStrIndex++] = ch; - ++i; - ch = pData[i]; - } - strString[iStrIndex] = 0; - } - - const wchar_t* const strName[] = {L"quot", L"amp", L"apos", L"lt", L"gt"}; - int32_t iIndex = 0; - while (iIndex < 5) { - if (memcmp(strString, strName[iIndex], wcslen(strName[iIndex])) == 0) { - break; - } - ++iIndex; - } - switch (iIndex) { - case 0: - wsResultBuf.AppendChar('"'); - break; - case 1: - wsResultBuf.AppendChar('&'); - break; - case 2: - wsResultBuf.AppendChar('\''); - break; - case 3: - wsResultBuf.AppendChar('<'); - break; - case 4: - wsResultBuf.AppendChar('>'); - break; - default: - wsResultBuf.AppendChar(iCode); - break; - } - iStrIndex = 0; - strString[iStrIndex] = 0; - ++i; - iCode = 0; - } - wsResultBuf.AppendChar(0); - return wsResultBuf.MakeString(); -} - -// static -void CXFA_FM2JSContext::Encode(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc < 1 || argc > 2) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Encode"); - return; - } - - if (argc == 1) { - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - if (ValueIsNull(pThis, argOne.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - WideString encoded = EncodeURL(ValueToUTF8String(argOne.get())); - args.GetReturnValue()->SetString( - FX_UTF8Encode(encoded.AsStringView()).AsStringView()); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - if (ValueIsNull(pThis, argOne.get()) || ValueIsNull(pThis, argTwo.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - ByteString toEncodeString = ValueToUTF8String(argOne.get()); - ByteString identifyString = ValueToUTF8String(argTwo.get()); - WideString encoded; - if (identifyString.EqualNoCase("html")) - encoded = EncodeHTML(toEncodeString); - else if (identifyString.EqualNoCase("xml")) - encoded = EncodeXML(toEncodeString); - else - encoded = EncodeURL(toEncodeString); - - args.GetReturnValue()->SetString( - FX_UTF8Encode(encoded.AsStringView()).AsStringView()); -} - -// static -WideString CXFA_FM2JSContext::EncodeURL(const ByteString& szURLString) { - WideString wsURLString = WideString::FromUTF8(szURLString.AsStringView()); - CFX_WideTextBuf wsResultBuf; - wchar_t strEncode[4]; - strEncode[0] = '%'; - strEncode[3] = 0; - wchar_t strUnsafe[] = {' ', '<', '>', '"', '#', '%', '{', '}', - '|', '\\', '^', '~', '[', ']', '`'}; - wchar_t strReserved[] = {';', '/', '?', ':', '@', '=', '&'}; - wchar_t strSpecial[] = {'$', '-', '+', '!', '*', '\'', '(', ')', ','}; - const wchar_t* strCode = L"0123456789abcdef"; - for (auto ch : wsURLString) { - int32_t i = 0; - int32_t iCount = FX_ArraySize(strUnsafe); - while (i < iCount) { - if (ch == strUnsafe[i]) { - int32_t iIndex = ch / 16; - strEncode[1] = strCode[iIndex]; - strEncode[2] = strCode[ch - iIndex * 16]; - wsResultBuf << strEncode; - break; - } - ++i; - } - if (i < iCount) - continue; - - i = 0; - iCount = FX_ArraySize(strReserved); - while (i < iCount) { - if (ch == strReserved[i]) { - int32_t iIndex = ch / 16; - strEncode[1] = strCode[iIndex]; - strEncode[2] = strCode[ch - iIndex * 16]; - wsResultBuf << strEncode; - break; - } - ++i; - } - if (i < iCount) - continue; - - i = 0; - iCount = FX_ArraySize(strSpecial); - while (i < iCount) { - if (ch == strSpecial[i]) { - wsResultBuf.AppendChar(ch); - break; - } - ++i; - } - if (i < iCount) - continue; - - if ((ch >= 0x80 && ch <= 0xff) || ch <= 0x1f || ch == 0x7f) { - int32_t iIndex = ch / 16; - strEncode[1] = strCode[iIndex]; - strEncode[2] = strCode[ch - iIndex * 16]; - wsResultBuf << strEncode; - } else if (ch >= 0x20 && ch <= 0x7e) { - wsResultBuf.AppendChar(ch); - } else { - const wchar_t iRadix = 16; - WideString strTmp; - while (ch >= iRadix) { - wchar_t tmp = strCode[ch % iRadix]; - ch /= iRadix; - strTmp += tmp; - } - strTmp += strCode[ch]; - int32_t iLen = strTmp.GetLength(); - if (iLen < 2) - break; - - int32_t iIndex = 0; - if (iLen % 2 != 0) { - strEncode[1] = '0'; - strEncode[2] = strTmp[iLen - 1]; - iIndex = iLen - 2; - } else { - strEncode[1] = strTmp[iLen - 1]; - strEncode[2] = strTmp[iLen - 2]; - iIndex = iLen - 3; - } - wsResultBuf << strEncode; - while (iIndex > 0) { - strEncode[1] = strTmp[iIndex]; - strEncode[2] = strTmp[iIndex - 1]; - iIndex -= 2; - wsResultBuf << strEncode; - } - } - } - wsResultBuf.AppendChar(0); - return wsResultBuf.MakeString(); -} - -// static -WideString CXFA_FM2JSContext::EncodeHTML(const ByteString& szHTMLString) { - WideString wsHTMLString = WideString::FromUTF8(szHTMLString.AsStringView()); - const wchar_t* strCode = L"0123456789abcdef"; - wchar_t strEncode[9]; - strEncode[0] = '&'; - strEncode[1] = '#'; - strEncode[2] = 'x'; - strEncode[5] = ';'; - strEncode[6] = 0; - strEncode[7] = ';'; - strEncode[8] = 0; - CFX_WideTextBuf wsResultBuf; - int32_t iLen = wsHTMLString.GetLength(); - int32_t i = 0; - const wchar_t* pData = wsHTMLString.c_str(); - while (i < iLen) { - uint32_t ch = pData[i]; - WideString htmlReserve; - if (HTMLCode2STR(ch, &htmlReserve)) { - wsResultBuf.AppendChar(L'&'); - wsResultBuf << htmlReserve; - wsResultBuf.AppendChar(L';'); - } else if (ch >= 32 && ch <= 126) { - wsResultBuf.AppendChar((wchar_t)ch); - } else if (ch < 256) { - int32_t iIndex = ch / 16; - strEncode[3] = strCode[iIndex]; - strEncode[4] = strCode[ch - iIndex * 16]; - strEncode[5] = ';'; - strEncode[6] = 0; - wsResultBuf << strEncode; - } else { - int32_t iBigByte = ch / 256; - int32_t iLittleByte = ch % 256; - strEncode[3] = strCode[iBigByte / 16]; - strEncode[4] = strCode[iBigByte % 16]; - strEncode[5] = strCode[iLittleByte / 16]; - strEncode[6] = strCode[iLittleByte % 16]; - wsResultBuf << strEncode; - } - ++i; - } - wsResultBuf.AppendChar(0); - return wsResultBuf.MakeString(); -} - -// static -WideString CXFA_FM2JSContext::EncodeXML(const ByteString& szXMLString) { - WideString wsXMLString = WideString::FromUTF8(szXMLString.AsStringView()); - CFX_WideTextBuf wsResultBuf; - wchar_t strEncode[9]; - strEncode[0] = '&'; - strEncode[1] = '#'; - strEncode[2] = 'x'; - strEncode[5] = ';'; - strEncode[6] = 0; - strEncode[7] = ';'; - strEncode[8] = 0; - const wchar_t* strCode = L"0123456789abcdef"; - for (const auto& ch : wsXMLString) { - switch (ch) { - case '"': - wsResultBuf.AppendChar('&'); - wsResultBuf << WideStringView(L"quot"); - wsResultBuf.AppendChar(';'); - break; - case '&': - wsResultBuf.AppendChar('&'); - wsResultBuf << WideStringView(L"amp"); - wsResultBuf.AppendChar(';'); - break; - case '\'': - wsResultBuf.AppendChar('&'); - wsResultBuf << WideStringView(L"apos"); - wsResultBuf.AppendChar(';'); - break; - case '<': - wsResultBuf.AppendChar('&'); - wsResultBuf << WideStringView(L"lt"); - wsResultBuf.AppendChar(';'); - break; - case '>': - wsResultBuf.AppendChar('&'); - wsResultBuf << WideStringView(L"gt"); - wsResultBuf.AppendChar(';'); - break; - default: { - if (ch >= 32 && ch <= 126) { - wsResultBuf.AppendChar(ch); - } else if (ch < 256) { - int32_t iIndex = ch / 16; - strEncode[3] = strCode[iIndex]; - strEncode[4] = strCode[ch - iIndex * 16]; - strEncode[5] = ';'; - strEncode[6] = 0; - wsResultBuf << strEncode; - } else { - int32_t iBigByte = ch / 256; - int32_t iLittleByte = ch % 256; - strEncode[3] = strCode[iBigByte / 16]; - strEncode[4] = strCode[iBigByte % 16]; - strEncode[5] = strCode[iLittleByte / 16]; - strEncode[6] = strCode[iLittleByte % 16]; - wsResultBuf << strEncode; - } - break; - } - } - } - wsResultBuf.AppendChar(0); - return wsResultBuf.MakeString(); -} - -// static -bool CXFA_FM2JSContext::HTMLSTR2Code(const WideStringView& pData, - uint32_t* iCode) { - auto cmpFunc = [](const XFA_FMHtmlReserveCode& iter, - const WideStringView& val) { - // TODO(tsepez): check usage of c_str() below. - return wcscmp(val.unterminated_c_str(), iter.m_htmlReserve) > 0; - }; - const XFA_FMHtmlReserveCode* result = - std::lower_bound(std::begin(reservesForDecode), - std::end(reservesForDecode), pData, cmpFunc); - if (result != std::end(reservesForEncode) && - !wcscmp(pData.unterminated_c_str(), result->m_htmlReserve)) { - *iCode = result->m_uCode; - return true; - } - return false; -} - -// static -bool CXFA_FM2JSContext::HTMLCode2STR(uint32_t iCode, - WideString* wsHTMLReserve) { - auto cmpFunc = [](const XFA_FMHtmlReserveCode iter, const uint32_t& val) { - return iter.m_uCode < val; - }; - const XFA_FMHtmlReserveCode* result = - std::lower_bound(std::begin(reservesForEncode), - std::end(reservesForEncode), iCode, cmpFunc); - if (result != std::end(reservesForEncode) && result->m_uCode == iCode) { - *wsHTMLReserve = result->m_htmlReserve; - return true; - } - return false; -} - -// static -void CXFA_FM2JSContext::Format(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() < 2) { - pContext->ThrowParamCountMismatchException(L"Format"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - ByteString szPattern = ValueToUTF8String(argOne.get()); - - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - ByteString szValue = ValueToUTF8String(argTwo.get()); - - CXFA_Document* pDoc = pContext->GetDocument(); - CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); - CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject()); - ASSERT(pThisNode); - - CXFA_WidgetData widgetData(pThisNode); - IFX_Locale* pLocale = widgetData.GetLocal(); - uint32_t patternType; - WideString wsPattern = WideString::FromUTF8(szPattern.AsStringView()); - WideString wsValue = WideString::FromUTF8(szValue.AsStringView()); - if (!PatternStringType(szPattern.AsStringView(), patternType)) { - switch (patternType) { - case XFA_VT_DATETIME: { - auto iTChar = wsPattern.Find(L'T'); - if (!iTChar.has_value()) { - args.GetReturnValue()->SetString(""); - return; - } - WideString wsDatePattern(L"date{"); - wsDatePattern += wsPattern.Left(iTChar.value()) + L"} "; - - WideString wsTimePattern(L"time{"); - wsTimePattern += - wsPattern.Right(wsPattern.GetLength() - (iTChar.value() + 1)) + - L"}"; - wsPattern = wsDatePattern + wsTimePattern; - } break; - case XFA_VT_DATE: { - wsPattern = L"date{" + wsPattern + L"}"; - } break; - case XFA_VT_TIME: { - wsPattern = L"time{" + wsPattern + L"}"; - } break; - case XFA_VT_TEXT: { - wsPattern = L"text{" + wsPattern + L"}"; - } break; - case XFA_VT_FLOAT: { - wsPattern = L"num{" + wsPattern + L"}"; - } break; - default: { - WideString wsTestPattern; - wsTestPattern = L"num{" + wsPattern + L"}"; - CXFA_LocaleValue tempLocaleValue(XFA_VT_FLOAT, wsValue, wsTestPattern, - pLocale, pMgr); - if (tempLocaleValue.IsValid()) { - wsPattern = wsTestPattern; - patternType = XFA_VT_FLOAT; - } else { - wsTestPattern = L"text{" + wsPattern + L"}"; - wsPattern = wsTestPattern; - patternType = XFA_VT_TEXT; - } - } break; - } - } - CXFA_LocaleValue localeValue(patternType, wsValue, wsPattern, pLocale, pMgr); - WideString wsRet; - if (!localeValue.FormatPatterns(wsRet, wsPattern, pLocale, - XFA_VALUEPICTURE_Display)) { - args.GetReturnValue()->SetString(""); - return; - } - - args.GetReturnValue()->SetString(wsRet.UTF8Encode().AsStringView()); -} - -// static -void CXFA_FM2JSContext::Left(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 2) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Left"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - if ((ValueIsNull(pThis, argOne.get())) || - (ValueIsNull(pThis, argTwo.get()))) { - args.GetReturnValue()->SetNull(); - return; - } - - ByteString sourceString = ValueToUTF8String(argOne.get()); - int32_t count = std::max(0, ValueToInteger(pThis, argTwo.get())); - args.GetReturnValue()->SetString(sourceString.Left(count).AsStringView()); -} - -// static -void CXFA_FM2JSContext::Len(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 1) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Len"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - if (ValueIsNull(pThis, argOne.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - ByteString sourceString = ValueToUTF8String(argOne.get()); - args.GetReturnValue()->SetInteger(sourceString.GetLength()); -} - -// static -void CXFA_FM2JSContext::Lower(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc < 1 || argc > 2) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Lower"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - if (ValueIsNull(pThis, argOne.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - CFX_WideTextBuf lowStringBuf; - ByteString argString = ValueToUTF8String(argOne.get()); - WideString wsArgString = WideString::FromUTF8(argString.AsStringView()); - const wchar_t* pData = wsArgString.c_str(); - size_t i = 0; - while (i < argString.GetLength()) { - int32_t ch = pData[i]; - if ((ch >= 0x41 && ch <= 0x5A) || (ch >= 0xC0 && ch <= 0xDE)) - ch += 32; - else if (ch == 0x100 || ch == 0x102 || ch == 0x104) - ch += 1; - - lowStringBuf.AppendChar(ch); - ++i; - } - lowStringBuf.AppendChar(0); - - args.GetReturnValue()->SetString( - FX_UTF8Encode(lowStringBuf.AsStringView()).AsStringView()); -} - -// static -void CXFA_FM2JSContext::Ltrim(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 1) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Ltrim"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - if (ValueIsNull(pThis, argOne.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - ByteString sourceString = ValueToUTF8String(argOne.get()); - sourceString.TrimLeft(); - args.GetReturnValue()->SetString(sourceString.AsStringView()); -} - -// static -void CXFA_FM2JSContext::Parse(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 2) { - pContext->ThrowParamCountMismatchException(L"Parse"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - if (ValueIsNull(pThis, argTwo.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - ByteString szPattern = ValueToUTF8String(argOne.get()); - ByteString szValue = ValueToUTF8String(argTwo.get()); - CXFA_Document* pDoc = pContext->GetDocument(); - CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); - CXFA_Node* pThisNode = ToNode(pDoc->GetScriptContext()->GetThisObject()); - ASSERT(pThisNode); - - CXFA_WidgetData widgetData(pThisNode); - IFX_Locale* pLocale = widgetData.GetLocal(); - WideString wsPattern = WideString::FromUTF8(szPattern.AsStringView()); - WideString wsValue = WideString::FromUTF8(szValue.AsStringView()); - uint32_t patternType; - if (PatternStringType(szPattern.AsStringView(), patternType)) { - CXFA_LocaleValue localeValue(patternType, wsValue, wsPattern, pLocale, - pMgr); - if (!localeValue.IsValid()) { - args.GetReturnValue()->SetString(""); - return; - } - args.GetReturnValue()->SetString( - localeValue.GetValue().UTF8Encode().AsStringView()); - return; - } - - switch (patternType) { - case XFA_VT_DATETIME: { - auto iTChar = wsPattern.Find(L'T'); - if (!iTChar.has_value()) { - args.GetReturnValue()->SetString(""); - return; - } - WideString wsDatePattern(L"date{" + wsPattern.Left(iTChar.value()) + - L"} "); - WideString wsTimePattern( - L"time{" + - wsPattern.Right(wsPattern.GetLength() - (iTChar.value() + 1)) + L"}"); - wsPattern = wsDatePattern + wsTimePattern; - CXFA_LocaleValue localeValue(patternType, wsValue, wsPattern, pLocale, - pMgr); - if (!localeValue.IsValid()) { - args.GetReturnValue()->SetString(""); - return; - } - args.GetReturnValue()->SetString( - localeValue.GetValue().UTF8Encode().AsStringView()); - return; - } - case XFA_VT_DATE: { - wsPattern = L"date{" + wsPattern + L"}"; - CXFA_LocaleValue localeValue(patternType, wsValue, wsPattern, pLocale, - pMgr); - if (!localeValue.IsValid()) { - args.GetReturnValue()->SetString(""); - return; - } - args.GetReturnValue()->SetString( - localeValue.GetValue().UTF8Encode().AsStringView()); - return; - } - case XFA_VT_TIME: { - wsPattern = L"time{" + wsPattern + L"}"; - CXFA_LocaleValue localeValue(patternType, wsValue, wsPattern, pLocale, - pMgr); - if (!localeValue.IsValid()) { - args.GetReturnValue()->SetString(""); - return; - } - args.GetReturnValue()->SetString( - localeValue.GetValue().UTF8Encode().AsStringView()); - return; - } - case XFA_VT_TEXT: { - wsPattern = L"text{" + wsPattern + L"}"; - CXFA_LocaleValue localeValue(XFA_VT_TEXT, wsValue, wsPattern, pLocale, - pMgr); - if (!localeValue.IsValid()) { - args.GetReturnValue()->SetString(""); - return; - } - args.GetReturnValue()->SetString( - localeValue.GetValue().UTF8Encode().AsStringView()); - return; - } - case XFA_VT_FLOAT: { - wsPattern = L"num{" + wsPattern + L"}"; - CXFA_LocaleValue localeValue(XFA_VT_FLOAT, wsValue, wsPattern, pLocale, - pMgr); - if (!localeValue.IsValid()) { - args.GetReturnValue()->SetString(""); - return; - } - args.GetReturnValue()->SetDouble(localeValue.GetDoubleNum()); - return; - } - default: { - WideString wsTestPattern; - wsTestPattern = L"num{" + wsPattern + L"}"; - CXFA_LocaleValue localeValue(XFA_VT_FLOAT, wsValue, wsTestPattern, - pLocale, pMgr); - if (localeValue.IsValid()) { - args.GetReturnValue()->SetDouble(localeValue.GetDoubleNum()); - return; - } - - wsTestPattern = L"text{" + wsPattern + L"}"; - CXFA_LocaleValue localeValue2(XFA_VT_TEXT, wsValue, wsTestPattern, - pLocale, pMgr); - if (!localeValue2.IsValid()) { - args.GetReturnValue()->SetString(""); - return; - } - args.GetReturnValue()->SetString( - localeValue2.GetValue().UTF8Encode().AsStringView()); - return; - } - } -} - -// static -void CXFA_FM2JSContext::Replace(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc < 2 || argc > 3) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Replace"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - ByteString oneString; - ByteString twoString; - if (!ValueIsNull(pThis, argOne.get()) && !ValueIsNull(pThis, argTwo.get())) { - oneString = ValueToUTF8String(argOne.get()); - twoString = ValueToUTF8String(argTwo.get()); - } - - ByteString threeString; - if (argc > 2) { - std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); - threeString = ValueToUTF8String(argThree.get()); - } - - size_t iFindLen = twoString.GetLength(); - std::ostringstream resultString; - size_t iFindIndex = 0; - for (size_t u = 0; u < oneString.GetLength(); ++u) { - char ch = static_cast<char>(oneString[u]); - if (ch != static_cast<char>(twoString[iFindIndex])) { - resultString << ch; - continue; - } - - size_t iTemp = u + 1; - ++iFindIndex; - while (iFindIndex < iFindLen) { - uint8_t chTemp = oneString[iTemp]; - if (chTemp != twoString[iFindIndex]) { - iFindIndex = 0; - break; - } - - ++iTemp; - ++iFindIndex; - } - if (iFindIndex == iFindLen) { - resultString << threeString; - u += iFindLen - 1; - iFindIndex = 0; - } else { - resultString << ch; - } - } - resultString << '\0'; - args.GetReturnValue()->SetString(ByteStringView(resultString.str().c_str())); -} - -// static -void CXFA_FM2JSContext::Right(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 2) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Right"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - if ((ValueIsNull(pThis, argOne.get())) || - (ValueIsNull(pThis, argTwo.get()))) { - args.GetReturnValue()->SetNull(); - return; - } - - ByteString sourceString = ValueToUTF8String(argOne.get()); - int32_t count = std::max(0, ValueToInteger(pThis, argTwo.get())); - args.GetReturnValue()->SetString(sourceString.Right(count).AsStringView()); -} - -// static -void CXFA_FM2JSContext::Rtrim(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 1) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Rtrim"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - if (ValueIsNull(pThis, argOne.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - ByteString sourceString = ValueToUTF8String(argOne.get()); - sourceString.TrimRight(); - args.GetReturnValue()->SetString(sourceString.AsStringView()); -} - -// static -void CXFA_FM2JSContext::Space(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 1) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Space"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - if (argOne->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - - int32_t count = std::max(0, ValueToInteger(pThis, argOne.get())); - std::ostringstream spaceString; - int32_t index = 0; - while (index < count) { - spaceString << ' '; - index++; - } - spaceString << '\0'; - args.GetReturnValue()->SetString(ByteStringView(spaceString.str().c_str())); -} - -// static -void CXFA_FM2JSContext::Str(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc < 1 || argc > 3) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Str"); - return; - } - - std::unique_ptr<CFXJSE_Value> numberValue = GetSimpleValue(pThis, args, 0); - if (numberValue->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - float fNumber = ValueToFloat(pThis, numberValue.get()); - - int32_t iWidth = 10; - if (argc > 1) { - std::unique_ptr<CFXJSE_Value> widthValue = GetSimpleValue(pThis, args, 1); - iWidth = static_cast<int32_t>(ValueToFloat(pThis, widthValue.get())); - } - - int32_t iPrecision = 0; - if (argc > 2) { - std::unique_ptr<CFXJSE_Value> precisionValue = - GetSimpleValue(pThis, args, 2); - iPrecision = std::max( - 0, static_cast<int32_t>(ValueToFloat(pThis, precisionValue.get()))); - } - - ByteString numberString; - ByteString formatStr = "%"; - if (iPrecision) { - formatStr += "."; - formatStr += ByteString::FormatInteger(iPrecision); - } - formatStr += "f"; - numberString.Format(formatStr.c_str(), fNumber); - - const char* pData = numberString.c_str(); - int32_t iLength = numberString.GetLength(); - int32_t u = 0; - while (u < iLength) { - if (pData[u] == '.') - break; - - ++u; - } - - std::ostringstream resultBuf; - if (u > iWidth || (iPrecision + u) >= iWidth) { - int32_t i = 0; - while (i < iWidth) { - resultBuf << '*'; - ++i; - } - resultBuf << '\0'; - args.GetReturnValue()->SetString(ByteStringView(resultBuf.str().c_str())); - return; - } - - if (u == iLength) { - if (iLength > iWidth) { - int32_t i = 0; - while (i < iWidth) { - resultBuf << '*'; - ++i; - } - } else { - int32_t i = 0; - while (i < iWidth - iLength) { - resultBuf << ' '; - ++i; - } - resultBuf << pData; - } - args.GetReturnValue()->SetString(ByteStringView(resultBuf.str().c_str())); - return; - } - - int32_t iLeavingSpace = iWidth - u - iPrecision; - if (iPrecision != 0) - iLeavingSpace--; - - int32_t i = 0; - while (i < iLeavingSpace) { - resultBuf << ' '; - ++i; - } - i = 0; - while (i < u) { - resultBuf << pData[i]; - ++i; - } - if (iPrecision != 0) - resultBuf << '.'; - - u++; - i = 0; - while (u < iLength) { - if (i >= iPrecision) - break; - - resultBuf << pData[u]; - ++i; - ++u; - } - while (i < iPrecision) { - resultBuf << '0'; - ++i; - } - resultBuf << '\0'; - args.GetReturnValue()->SetString(ByteStringView(resultBuf.str().c_str())); -} - -// static -void CXFA_FM2JSContext::Stuff(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc < 3 || argc > 4) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Stuff"); - return; - } - - ByteString sourceString; - ByteString insertString; - int32_t iLength = 0; - int32_t iStart = 0; - int32_t iDelete = 0; - std::unique_ptr<CFXJSE_Value> sourceValue = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> startValue = GetSimpleValue(pThis, args, 1); - std::unique_ptr<CFXJSE_Value> deleteValue = GetSimpleValue(pThis, args, 2); - if (!sourceValue->IsNull() && !startValue->IsNull() && - !deleteValue->IsNull()) { - sourceString = ValueToUTF8String(sourceValue.get()); - iLength = sourceString.GetLength(); - iStart = pdfium::clamp( - static_cast<int32_t>(ValueToFloat(pThis, startValue.get())), 1, - iLength); - iDelete = std::max( - 0, static_cast<int32_t>(ValueToFloat(pThis, deleteValue.get()))); - } - - if (argc > 3) { - std::unique_ptr<CFXJSE_Value> insertValue = GetSimpleValue(pThis, args, 3); - insertString = ValueToUTF8String(insertValue.get()); - } - - iStart -= 1; - std::ostringstream resultString; - int32_t i = 0; - while (i < iStart) { - resultString << static_cast<char>(sourceString[i]); - ++i; - } - resultString << insertString.AsStringView(); - i = iStart + iDelete; - while (i < iLength) { - resultString << static_cast<char>(sourceString[i]); - ++i; - } - resultString << '\0'; - args.GetReturnValue()->SetString(ByteStringView(resultString.str().c_str())); -} - -// static -void CXFA_FM2JSContext::Substr(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 3) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Substr"); - return; - } - - std::unique_ptr<CFXJSE_Value> stringValue = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> startValue = GetSimpleValue(pThis, args, 1); - std::unique_ptr<CFXJSE_Value> endValue = GetSimpleValue(pThis, args, 2); - if (ValueIsNull(pThis, stringValue.get()) || - (ValueIsNull(pThis, startValue.get())) || - (ValueIsNull(pThis, endValue.get()))) { - args.GetReturnValue()->SetNull(); - return; - } - - int32_t iStart = 0; - int32_t iCount = 0; - ByteString szSourceStr = ValueToUTF8String(stringValue.get()); - int32_t iLength = szSourceStr.GetLength(); - if (iLength == 0) { - args.GetReturnValue()->SetString(""); - return; - } - - iStart = pdfium::clamp( - iLength, 1, static_cast<int32_t>(ValueToFloat(pThis, startValue.get()))); - iCount = - std::max(0, static_cast<int32_t>(ValueToFloat(pThis, endValue.get()))); - - iStart -= 1; - args.GetReturnValue()->SetString( - szSourceStr.Mid(iStart, iCount).AsStringView()); -} - -// static -void CXFA_FM2JSContext::Uuid(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc < 0 || argc > 1) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Uuid"); - return; - } - - int32_t iNum = 0; - if (argc > 0) { - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - iNum = static_cast<int32_t>(ValueToFloat(pThis, argOne.get())); - } - args.GetReturnValue()->SetString(GUIDString(!!iNum).AsStringView()); -} - -// static -void CXFA_FM2JSContext::Upper(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc < 1 || argc > 2) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"Upper"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - if (ValueIsNull(pThis, argOne.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - CFX_WideTextBuf upperStringBuf; - ByteString argString = ValueToUTF8String(argOne.get()); - WideString wsArgString = WideString::FromUTF8(argString.AsStringView()); - const wchar_t* pData = wsArgString.c_str(); - size_t i = 0; - while (i < wsArgString.GetLength()) { - int32_t ch = pData[i]; - if ((ch >= 0x61 && ch <= 0x7A) || (ch >= 0xE0 && ch <= 0xFE)) - ch -= 32; - else if (ch == 0x101 || ch == 0x103 || ch == 0x105) - ch -= 1; - - upperStringBuf.AppendChar(ch); - ++i; - } - upperStringBuf.AppendChar(0); - - args.GetReturnValue()->SetString( - FX_UTF8Encode(upperStringBuf.AsStringView()).AsStringView()); -} - -// static -void CXFA_FM2JSContext::WordNum(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - int32_t argc = args.GetLength(); - if (argc < 1 || argc > 3) { - ToJSContext(pThis, nullptr)->ThrowParamCountMismatchException(L"WordNum"); - return; - } - - std::unique_ptr<CFXJSE_Value> numberValue = GetSimpleValue(pThis, args, 0); - if (numberValue->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - float fNumber = ValueToFloat(pThis, numberValue.get()); - - int32_t iIdentifier = 0; - if (argc > 1) { - std::unique_ptr<CFXJSE_Value> identifierValue = - GetSimpleValue(pThis, args, 1); - if (identifierValue->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - iIdentifier = - static_cast<int32_t>(ValueToFloat(pThis, identifierValue.get())); - } - - ByteString localeString; - if (argc > 2) { - std::unique_ptr<CFXJSE_Value> localeValue = GetSimpleValue(pThis, args, 2); - if (localeValue->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - localeString = ValueToUTF8String(localeValue.get()); - } - - if (fNumber < 0.0f || fNumber > 922337203685477550.0f) { - args.GetReturnValue()->SetString("*"); - return; - } - - ByteString numberString; - numberString.Format("%.2f", fNumber); - - args.GetReturnValue()->SetString( - WordUS(numberString, iIdentifier).AsStringView()); -} - -// static -ByteString CXFA_FM2JSContext::TrillionUS(const ByteStringView& szData) { - std::ostringstream strBuf; - ByteStringView pUnits[] = {"zero", "one", "two", "three", "four", - "five", "six", "seven", "eight", "nine"}; - ByteStringView pCapUnits[] = {"Zero", "One", "Two", "Three", "Four", - "Five", "Six", "Seven", "Eight", "Nine"}; - ByteStringView pTens[] = {"Ten", "Eleven", "Twelve", "Thirteen", - "Fourteen", "Fifteen", "Sixteen", "Seventeen", - "Eighteen", "Nineteen"}; - ByteStringView pLastTens[] = {"Twenty", "Thirty", "Forty", "Fifty", - "Sixty", "Seventy", "Eighty", "Ninety"}; - ByteStringView pComm[] = {" Hundred ", " Thousand ", " Million ", " Billion ", - "Trillion"}; - const char* pData = szData.unterminated_c_str(); - int32_t iLength = szData.GetLength(); - int32_t iComm = 0; - if (iLength > 12) - iComm = 4; - else if (iLength > 9) - iComm = 3; - else if (iLength > 6) - iComm = 2; - else if (iLength > 3) - iComm = 1; - - int32_t iFirstCount = iLength % 3; - if (iFirstCount == 0) - iFirstCount = 3; - - int32_t iIndex = 0; - if (iFirstCount == 3) { - if (pData[iIndex] != '0') { - strBuf << pCapUnits[pData[iIndex] - '0']; - strBuf << pComm[0]; - } - if (pData[iIndex + 1] == '0') { - strBuf << pCapUnits[pData[iIndex + 2] - '0']; - } else { - if (pData[iIndex + 1] > '1') { - strBuf << pLastTens[pData[iIndex + 1] - '2']; - strBuf << "-"; - strBuf << pUnits[pData[iIndex + 2] - '0']; - } else if (pData[iIndex + 1] == '1') { - strBuf << pTens[pData[iIndex + 2] - '0']; - } else if (pData[iIndex + 1] == '0') { - strBuf << pCapUnits[pData[iIndex + 2] - '0']; - } - } - iIndex += 3; - } else if (iFirstCount == 2) { - if (pData[iIndex] == '0') { - strBuf << pCapUnits[pData[iIndex + 1] - '0']; - } else { - if (pData[iIndex] > '1') { - strBuf << pLastTens[pData[iIndex] - '2']; - strBuf << "-"; - strBuf << pUnits[pData[iIndex + 1] - '0']; - } else if (pData[iIndex] == '1') { - strBuf << pTens[pData[iIndex + 1] - '0']; - } else if (pData[iIndex] == '0') { - strBuf << pCapUnits[pData[iIndex + 1] - '0']; - } - } - iIndex += 2; - } else if (iFirstCount == 1) { - strBuf << pCapUnits[pData[iIndex] - '0']; - iIndex += 1; - } - if (iLength > 3 && iFirstCount > 0) { - strBuf << pComm[iComm]; - --iComm; - } - while (iIndex < iLength) { - if (pData[iIndex] != '0') { - strBuf << pCapUnits[pData[iIndex] - '0']; - strBuf << pComm[0]; - } - if (pData[iIndex + 1] == '0') { - strBuf << pCapUnits[pData[iIndex + 2] - '0']; - } else { - if (pData[iIndex + 1] > '1') { - strBuf << pLastTens[pData[iIndex + 1] - '2']; - strBuf << "-"; - strBuf << pUnits[pData[iIndex + 2] - '0']; - } else if (pData[iIndex + 1] == '1') { - strBuf << pTens[pData[iIndex + 2] - '0']; - } else if (pData[iIndex + 1] == '0') { - strBuf << pCapUnits[pData[iIndex + 2] - '0']; - } - } - if (iIndex < iLength - 3) { - strBuf << pComm[iComm]; - --iComm; - } - iIndex += 3; - } - return ByteString(strBuf); -} - -// static -ByteString CXFA_FM2JSContext::WordUS(const ByteString& szData, int32_t iStyle) { - const char* pData = szData.c_str(); - int32_t iLength = szData.GetLength(); - if (iStyle < 0 || iStyle > 2) { - return ByteString(); - } - - std::ostringstream strBuf; - - int32_t iIndex = 0; - while (iIndex < iLength) { - if (pData[iIndex] == '.') - break; - ++iIndex; - } - int32_t iInteger = iIndex; - iIndex = 0; - while (iIndex < iInteger) { - int32_t iCount = (iInteger - iIndex) % 12; - if (!iCount && iInteger - iIndex > 0) - iCount = 12; - - strBuf << TrillionUS(ByteStringView(pData + iIndex, iCount)); - iIndex += iCount; - if (iIndex < iInteger) - strBuf << " Trillion "; - } - - if (iStyle > 0) - strBuf << " Dollars"; - - if (iStyle > 1 && iInteger < iLength) { - strBuf << " And "; - iIndex = iInteger + 1; - while (iIndex < iLength) { - int32_t iCount = (iLength - iIndex) % 12; - if (!iCount && iLength - iIndex > 0) - iCount = 12; - - strBuf << TrillionUS(ByteStringView(pData + iIndex, iCount)); - iIndex += iCount; - if (iIndex < iLength) - strBuf << " Trillion "; - } - strBuf << " Cents"; - } - return ByteString(strBuf); -} - -// static -void CXFA_FM2JSContext::Get(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 1) { - pContext->ThrowParamCountMismatchException(L"Get"); - return; - } - - CXFA_Document* pDoc = pContext->GetDocument(); - if (!pDoc) - return; - - IXFA_AppProvider* pAppProvider = pDoc->GetNotify()->GetAppProvider(); - if (!pAppProvider) - return; - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - ByteString urlString = ValueToUTF8String(argOne.get()); - RetainPtr<IFX_SeekableReadStream> pFile = - pAppProvider->DownloadURL(WideString::FromUTF8(urlString.AsStringView())); - if (!pFile) - return; - - int32_t size = pFile->GetSize(); - std::vector<uint8_t> dataBuf(size); - pFile->ReadBlock(dataBuf.data(), size); - args.GetReturnValue()->SetString(ByteStringView(dataBuf)); -} - -// static -void CXFA_FM2JSContext::Post(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - int32_t argc = args.GetLength(); - if (argc < 2 || argc > 5) { - pContext->ThrowParamCountMismatchException(L"Post"); - return; - } - - CXFA_Document* pDoc = pContext->GetDocument(); - if (!pDoc) - return; - - IXFA_AppProvider* pAppProvider = pDoc->GetNotify()->GetAppProvider(); - if (!pAppProvider) - return; - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - ByteString bsURL = ValueToUTF8String(argOne.get()); - - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - ByteString bsData = ValueToUTF8String(argTwo.get()); - - ByteString bsContentType; - if (argc > 2) { - std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); - bsContentType = ValueToUTF8String(argThree.get()); - } - - ByteString bsEncode; - if (argc > 3) { - std::unique_ptr<CFXJSE_Value> argFour = GetSimpleValue(pThis, args, 3); - bsEncode = ValueToUTF8String(argFour.get()); - } - - ByteString bsHeader; - if (argc > 4) { - std::unique_ptr<CFXJSE_Value> argFive = GetSimpleValue(pThis, args, 4); - bsHeader = ValueToUTF8String(argFive.get()); - } - - WideString decodedResponse; - if (!pAppProvider->PostRequestURL( - WideString::FromUTF8(bsURL.AsStringView()), - WideString::FromUTF8(bsData.AsStringView()), - WideString::FromUTF8(bsContentType.AsStringView()), - WideString::FromUTF8(bsEncode.AsStringView()), - WideString::FromUTF8(bsHeader.AsStringView()), decodedResponse)) { - pContext->ThrowServerDeniedException(); - return; - } - args.GetReturnValue()->SetString(decodedResponse.UTF8Encode().AsStringView()); -} - -// static -void CXFA_FM2JSContext::Put(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - int32_t argc = args.GetLength(); - if (argc < 2 || argc > 3) { - pContext->ThrowParamCountMismatchException(L"Put"); - return; - } - - CXFA_Document* pDoc = pContext->GetDocument(); - if (!pDoc) - return; - - IXFA_AppProvider* pAppProvider = pDoc->GetNotify()->GetAppProvider(); - if (!pAppProvider) - return; - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - ByteString bsURL = ValueToUTF8String(argOne.get()); - - std::unique_ptr<CFXJSE_Value> argTwo = GetSimpleValue(pThis, args, 1); - ByteString bsData = ValueToUTF8String(argTwo.get()); - - ByteString bsEncode; - if (argc > 2) { - std::unique_ptr<CFXJSE_Value> argThree = GetSimpleValue(pThis, args, 2); - bsEncode = ValueToUTF8String(argThree.get()); - } - - if (!pAppProvider->PutRequestURL( - WideString::FromUTF8(bsURL.AsStringView()), - WideString::FromUTF8(bsData.AsStringView()), - WideString::FromUTF8(bsEncode.AsStringView()))) { - pContext->ThrowServerDeniedException(); - return; - } - - args.GetReturnValue()->SetString(""); -} - -// static -void CXFA_FM2JSContext::assign_value_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 2) { - pContext->ThrowCompilerErrorException(); - return; - } - - std::unique_ptr<CFXJSE_Value> lValue = args.GetValue(0); - std::unique_ptr<CFXJSE_Value> rValue = GetSimpleValue(pThis, args, 1); - if (lValue->IsArray()) { - v8::Isolate* pIsolate = pContext->GetScriptRuntime(); - auto leftLengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - lValue->GetObjectProperty("length", leftLengthValue.get()); - int32_t iLeftLength = leftLengthValue->ToInteger(); - auto jsObjectValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto propertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - lValue->GetObjectPropertyByIdx(1, propertyValue.get()); - if (propertyValue->IsNull()) { - for (int32_t i = 2; i < iLeftLength; i++) { - lValue->GetObjectPropertyByIdx(i, jsObjectValue.get()); - if (!SetObjectDefaultValue(jsObjectValue.get(), rValue.get())) { - pContext->ThrowNoDefaultPropertyException(szFuncName); - return; - } - } - } else { - for (int32_t i = 2; i < iLeftLength; i++) { - lValue->GetObjectPropertyByIdx(i, jsObjectValue.get()); - jsObjectValue->SetObjectProperty( - propertyValue->ToString().AsStringView(), rValue.get()); - } - } - } else if (lValue->IsObject()) { - if (!SetObjectDefaultValue(lValue.get(), rValue.get())) { - pContext->ThrowNoDefaultPropertyException(szFuncName); - return; - } - } - args.GetReturnValue()->Assign(rValue.get()); -} - -// static -void CXFA_FM2JSContext::logical_or_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 2) { - ToJSContext(pThis, nullptr)->ThrowCompilerErrorException(); - return; - } - - std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); - if (argFirst->IsNull() && argSecond->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - - float first = ValueToFloat(pThis, argFirst.get()); - float second = ValueToFloat(pThis, argSecond.get()); - args.GetReturnValue()->SetInteger((first || second) ? 1 : 0); -} - -// static -void CXFA_FM2JSContext::logical_and_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 2) { - ToJSContext(pThis, nullptr)->ThrowCompilerErrorException(); - return; - } - - std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); - if (argFirst->IsNull() && argSecond->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - - float first = ValueToFloat(pThis, argFirst.get()); - float second = ValueToFloat(pThis, argSecond.get()); - args.GetReturnValue()->SetInteger((first && second) ? 1 : 0); -} - -// static -void CXFA_FM2JSContext::equality_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 2) { - ToJSContext(pThis, nullptr)->ThrowCompilerErrorException(); - return; - } - - if (fm_ref_equal(pThis, args)) { - args.GetReturnValue()->SetInteger(1); - return; - } - - std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); - if (argFirst->IsNull() || argSecond->IsNull()) { - args.GetReturnValue()->SetInteger( - (argFirst->IsNull() && argSecond->IsNull()) ? 1 : 0); - return; - } - - if (argFirst->IsString() && argSecond->IsString()) { - args.GetReturnValue()->SetInteger(argFirst->ToString() == - argSecond->ToString()); - return; - } - - double first = ValueToDouble(pThis, argFirst.get()); - double second = ValueToDouble(pThis, argSecond.get()); - args.GetReturnValue()->SetInteger((first == second) ? 1 : 0); -} - -// static -void CXFA_FM2JSContext::notequality_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 2) { - ToJSContext(pThis, nullptr)->ThrowCompilerErrorException(); - return; - } - - if (fm_ref_equal(pThis, args)) { - args.GetReturnValue()->SetInteger(0); - return; - } - - std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); - if (argFirst->IsNull() || argSecond->IsNull()) { - args.GetReturnValue()->SetInteger( - (argFirst->IsNull() && argSecond->IsNull()) ? 0 : 1); - return; - } - - if (argFirst->IsString() && argSecond->IsString()) { - args.GetReturnValue()->SetInteger(argFirst->ToString() != - argSecond->ToString()); - return; - } - - double first = ValueToDouble(pThis, argFirst.get()); - double second = ValueToDouble(pThis, argSecond.get()); - args.GetReturnValue()->SetInteger(first != second); -} - -// static -bool CXFA_FM2JSContext::fm_ref_equal(CFXJSE_Value* pThis, - CFXJSE_Arguments& args) { - std::unique_ptr<CFXJSE_Value> argFirst = args.GetValue(0); - std::unique_ptr<CFXJSE_Value> argSecond = args.GetValue(1); - if (!argFirst->IsArray() || !argSecond->IsArray()) - return false; - - v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); - auto firstFlagValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto secondFlagValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argFirst->GetObjectPropertyByIdx(0, firstFlagValue.get()); - argSecond->GetObjectPropertyByIdx(0, secondFlagValue.get()); - if (firstFlagValue->ToInteger() != 3 || secondFlagValue->ToInteger() != 3) - return false; - - auto firstJSObject = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto secondJSObject = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argFirst->GetObjectPropertyByIdx(2, firstJSObject.get()); - argSecond->GetObjectPropertyByIdx(2, secondJSObject.get()); - if (firstJSObject->IsNull() || secondJSObject->IsNull()) - return false; - - return (firstJSObject->ToHostObject(nullptr) == - secondJSObject->ToHostObject(nullptr)); -} - -// static -void CXFA_FM2JSContext::less_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 2) { - ToJSContext(pThis, nullptr)->ThrowCompilerErrorException(); - return; - } - - std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); - if (argFirst->IsNull() || argSecond->IsNull()) { - args.GetReturnValue()->SetInteger(0); - return; - } - - if (argFirst->IsString() && argSecond->IsString()) { - args.GetReturnValue()->SetInteger( - argFirst->ToString().Compare(argSecond->ToString().AsStringView()) == - -1); - return; - } - - double first = ValueToDouble(pThis, argFirst.get()); - double second = ValueToDouble(pThis, argSecond.get()); - args.GetReturnValue()->SetInteger((first < second) ? 1 : 0); -} - -// static -void CXFA_FM2JSContext::lessequal_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 2) { - ToJSContext(pThis, nullptr)->ThrowCompilerErrorException(); - return; - } - - std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); - if (argFirst->IsNull() || argSecond->IsNull()) { - args.GetReturnValue()->SetInteger( - (argFirst->IsNull() && argSecond->IsNull()) ? 1 : 0); - return; - } - - if (argFirst->IsString() && argSecond->IsString()) { - args.GetReturnValue()->SetInteger( - argFirst->ToString().Compare(argSecond->ToString().AsStringView()) != - 1); - return; - } - - double first = ValueToDouble(pThis, argFirst.get()); - double second = ValueToDouble(pThis, argSecond.get()); - args.GetReturnValue()->SetInteger((first <= second) ? 1 : 0); -} - -// static -void CXFA_FM2JSContext::greater_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 2) { - ToJSContext(pThis, nullptr)->ThrowCompilerErrorException(); - return; - } - - std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); - if (argFirst->IsNull() || argSecond->IsNull()) { - args.GetReturnValue()->SetInteger(0); - return; - } - - if (argFirst->IsString() && argSecond->IsString()) { - args.GetReturnValue()->SetInteger( - argFirst->ToString().Compare(argSecond->ToString().AsStringView()) == - 1); - return; - } - - double first = ValueToDouble(pThis, argFirst.get()); - double second = ValueToDouble(pThis, argSecond.get()); - args.GetReturnValue()->SetInteger((first > second) ? 1 : 0); -} - -// static -void CXFA_FM2JSContext::greaterequal_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 2) { - ToJSContext(pThis, nullptr)->ThrowCompilerErrorException(); - return; - } - - std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); - if (argFirst->IsNull() || argSecond->IsNull()) { - args.GetReturnValue()->SetInteger( - (argFirst->IsNull() && argSecond->IsNull()) ? 1 : 0); - return; - } - - if (argFirst->IsString() && argSecond->IsString()) { - args.GetReturnValue()->SetInteger( - argFirst->ToString().Compare(argSecond->ToString().AsStringView()) != - -1); - return; - } - - double first = ValueToDouble(pThis, argFirst.get()); - double second = ValueToDouble(pThis, argSecond.get()); - args.GetReturnValue()->SetInteger((first >= second) ? 1 : 0); -} - -// static -void CXFA_FM2JSContext::plus_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 2) { - ToJSContext(pThis, nullptr)->ThrowCompilerErrorException(); - return; - } - - std::unique_ptr<CFXJSE_Value> argFirst = args.GetValue(0); - std::unique_ptr<CFXJSE_Value> argSecond = args.GetValue(1); - if (ValueIsNull(pThis, argFirst.get()) && - ValueIsNull(pThis, argSecond.get())) { - args.GetReturnValue()->SetNull(); - return; - } - - double first = ValueToDouble(pThis, argFirst.get()); - double second = ValueToDouble(pThis, argSecond.get()); - args.GetReturnValue()->SetDouble(first + second); -} - -// static -void CXFA_FM2JSContext::minus_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 2) { - ToJSContext(pThis, nullptr)->ThrowCompilerErrorException(); - return; - } - - std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); - if (argFirst->IsNull() && argSecond->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - - double first = ValueToDouble(pThis, argFirst.get()); - double second = ValueToDouble(pThis, argSecond.get()); - args.GetReturnValue()->SetDouble(first - second); -} - -// static -void CXFA_FM2JSContext::multiple_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 2) { - ToJSContext(pThis, nullptr)->ThrowCompilerErrorException(); - return; - } - - std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); - if (argFirst->IsNull() && argSecond->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - - double first = ValueToDouble(pThis, argFirst.get()); - double second = ValueToDouble(pThis, argSecond.get()); - args.GetReturnValue()->SetDouble(first * second); -} - -// static -void CXFA_FM2JSContext::divide_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 2) { - pContext->ThrowCompilerErrorException(); - return; - } - - std::unique_ptr<CFXJSE_Value> argFirst = GetSimpleValue(pThis, args, 0); - std::unique_ptr<CFXJSE_Value> argSecond = GetSimpleValue(pThis, args, 1); - if (argFirst->IsNull() && argSecond->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - - double second = ValueToDouble(pThis, argSecond.get()); - if (second == 0.0) { - pContext->ThrowDivideByZeroException(); - return; - } - - double first = ValueToDouble(pThis, argFirst.get()); - args.GetReturnValue()->SetDouble(first / second); -} - -// static -void CXFA_FM2JSContext::positive_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 1) { - ToJSContext(pThis, nullptr)->ThrowCompilerErrorException(); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - if (argOne->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - args.GetReturnValue()->SetDouble(0.0 + ValueToDouble(pThis, argOne.get())); -} - -// static -void CXFA_FM2JSContext::negative_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 1) { - ToJSContext(pThis, nullptr)->ThrowCompilerErrorException(); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - if (argOne->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - args.GetReturnValue()->SetDouble(0.0 - ValueToDouble(pThis, argOne.get())); -} - -// static -void CXFA_FM2JSContext::logical_not_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 1) { - ToJSContext(pThis, nullptr)->ThrowCompilerErrorException(); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - if (argOne->IsNull()) { - args.GetReturnValue()->SetNull(); - return; - } - - double first = ValueToDouble(pThis, argOne.get()); - args.GetReturnValue()->SetInteger((first == 0.0) ? 1 : 0); -} - -// static -void CXFA_FM2JSContext::dot_accessor(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - v8::Isolate* pIsolate = pContext->GetScriptRuntime(); - int32_t argc = args.GetLength(); - if (argc < 4 || argc > 5) { - pContext->ThrowCompilerErrorException(); - return; - } - - bool bIsStar = true; - int32_t iIndexValue = 0; - if (argc > 4) { - bIsStar = false; - iIndexValue = ValueToInteger(pThis, args.GetValue(4).get()); - } - - ByteString szName = args.GetUTF8String(2); - ByteString szSomExp = GenerateSomExpression( - szName.AsStringView(), args.GetInt32(3), iIndexValue, bIsStar); - - std::unique_ptr<CFXJSE_Value> argAccessor = args.GetValue(0); - if (argAccessor->IsArray()) { - auto pLengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argAccessor->GetObjectProperty("length", pLengthValue.get()); - int32_t iLength = pLengthValue->ToInteger(); - if (iLength < 3) { - pContext->ThrowArgumentMismatchException(); - return; - } - - auto hJSObjValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - std::vector<std::vector<std::unique_ptr<CFXJSE_Value>>> resolveValues( - iLength - 2); - bool bAttribute = false; - int32_t iCounter = 0; - for (int32_t i = 2; i < iLength; i++) { - argAccessor->GetObjectPropertyByIdx(i, hJSObjValue.get()); - - XFA_RESOLVENODE_RS resoveNodeRS; - if (ResolveObjects(pThis, hJSObjValue.get(), szSomExp.AsStringView(), - resoveNodeRS, true, szName.IsEmpty()) > 0) { - ParseResolveResult(pThis, resoveNodeRS, hJSObjValue.get(), - &resolveValues[i - 2], &bAttribute); - iCounter += resolveValues[i - 2].size(); - } - } - if (iCounter < 1) { - pContext->ThrowPropertyNotInObjectException( - WideString::FromUTF8(szName.AsStringView()), - WideString::FromUTF8(szSomExp.AsStringView())); - return; - } - - std::vector<std::unique_ptr<CFXJSE_Value>> values; - for (int32_t i = 0; i < iCounter + 2; i++) - values.push_back(pdfium::MakeUnique<CFXJSE_Value>(pIsolate)); - - values[0]->SetInteger(1); - if (bAttribute) - values[1]->SetString(szName.AsStringView()); - else - values[1]->SetNull(); - - int32_t iIndex = 2; - for (int32_t i = 0; i < iLength - 2; i++) { - for (size_t j = 0; j < resolveValues[i].size(); j++) { - values[iIndex]->Assign(resolveValues[i][j].get()); - iIndex++; - } - } - args.GetReturnValue()->SetArray(values); - return; - } - - XFA_RESOLVENODE_RS resoveNodeRS; - int32_t iRet = 0; - ByteString bsAccessorName = args.GetUTF8String(1); - if (argAccessor->IsObject() || - (argAccessor->IsNull() && bsAccessorName.IsEmpty())) { - iRet = ResolveObjects(pThis, argAccessor.get(), szSomExp.AsStringView(), - resoveNodeRS, true, szName.IsEmpty()); - } else if (!argAccessor->IsObject() && !bsAccessorName.IsEmpty() && - GetObjectForName(pThis, argAccessor.get(), - bsAccessorName.AsStringView())) { - iRet = ResolveObjects(pThis, argAccessor.get(), szSomExp.AsStringView(), - resoveNodeRS, true, szName.IsEmpty()); - } - if (iRet < 1) { - pContext->ThrowPropertyNotInObjectException( - WideString::FromUTF8(szName.AsStringView()), - WideString::FromUTF8(szSomExp.AsStringView())); - return; - } - - std::vector<std::unique_ptr<CFXJSE_Value>> resolveValues; - bool bAttribute = false; - ParseResolveResult(pThis, resoveNodeRS, argAccessor.get(), &resolveValues, - &bAttribute); - - std::vector<std::unique_ptr<CFXJSE_Value>> values; - for (size_t i = 0; i < resolveValues.size() + 2; i++) - values.push_back(pdfium::MakeUnique<CFXJSE_Value>(pIsolate)); - - values[0]->SetInteger(1); - if (bAttribute) - values[1]->SetString(szName.AsStringView()); - else - values[1]->SetNull(); - - for (size_t i = 0; i < resolveValues.size(); i++) - values[i + 2]->Assign(resolveValues[i].get()); - - args.GetReturnValue()->SetArray(values); -} - -// static -void CXFA_FM2JSContext::dotdot_accessor(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - v8::Isolate* pIsolate = pContext->GetScriptRuntime(); - int32_t argc = args.GetLength(); - if (argc < 4 || argc > 5) { - pContext->ThrowCompilerErrorException(); - return; - } - - bool bIsStar = true; - int32_t iIndexValue = 0; - if (argc > 4) { - bIsStar = false; - iIndexValue = ValueToInteger(pThis, args.GetValue(4).get()); - } - - ByteString szName = args.GetUTF8String(2); - ByteString szSomExp = GenerateSomExpression( - szName.AsStringView(), args.GetInt32(3), iIndexValue, bIsStar); - - std::unique_ptr<CFXJSE_Value> argAccessor = args.GetValue(0); - if (argAccessor->IsArray()) { - auto pLengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argAccessor->GetObjectProperty("length", pLengthValue.get()); - int32_t iLength = pLengthValue->ToInteger(); - if (iLength < 3) { - pContext->ThrowArgumentMismatchException(); - return; - } - - int32_t iCounter = 0; - - std::vector<std::vector<std::unique_ptr<CFXJSE_Value>>> resolveValues( - iLength - 2); - auto hJSObjValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - bool bAttribute = false; - for (int32_t i = 2; i < iLength; i++) { - argAccessor->GetObjectPropertyByIdx(i, hJSObjValue.get()); - XFA_RESOLVENODE_RS resoveNodeRS; - if (ResolveObjects(pThis, hJSObjValue.get(), szSomExp.AsStringView(), - resoveNodeRS, false) > 0) { - ParseResolveResult(pThis, resoveNodeRS, hJSObjValue.get(), - &resolveValues[i - 2], &bAttribute); - iCounter += resolveValues[i - 2].size(); - } - } - if (iCounter < 1) { - pContext->ThrowPropertyNotInObjectException( - WideString::FromUTF8(szName.AsStringView()), - WideString::FromUTF8(szSomExp.AsStringView())); - return; - } - - std::vector<std::unique_ptr<CFXJSE_Value>> values; - for (int32_t i = 0; i < iCounter + 2; i++) - values.push_back(pdfium::MakeUnique<CFXJSE_Value>(pIsolate)); - - values[0]->SetInteger(1); - if (bAttribute) - values[1]->SetString(szName.AsStringView()); - else - values[1]->SetNull(); - - int32_t iIndex = 2; - for (int32_t i = 0; i < iLength - 2; i++) { - for (size_t j = 0; j < resolveValues[i].size(); j++) { - values[iIndex]->Assign(resolveValues[i][j].get()); - iIndex++; - } - } - args.GetReturnValue()->SetArray(values); - return; - } - - XFA_RESOLVENODE_RS resoveNodeRS; - int32_t iRet = 0; - ByteString bsAccessorName = args.GetUTF8String(1); - if (argAccessor->IsObject() || - (argAccessor->IsNull() && bsAccessorName.IsEmpty())) { - iRet = ResolveObjects(pThis, argAccessor.get(), szSomExp.AsStringView(), - resoveNodeRS, false); - } else if (!argAccessor->IsObject() && !bsAccessorName.IsEmpty() && - GetObjectForName(pThis, argAccessor.get(), - bsAccessorName.AsStringView())) { - iRet = ResolveObjects(pThis, argAccessor.get(), szSomExp.AsStringView(), - resoveNodeRS, false); - } - if (iRet < 1) { - pContext->ThrowPropertyNotInObjectException( - WideString::FromUTF8(szName.AsStringView()), - WideString::FromUTF8(szSomExp.AsStringView())); - return; - } - - std::vector<std::unique_ptr<CFXJSE_Value>> resolveValues; - bool bAttribute = false; - ParseResolveResult(pThis, resoveNodeRS, argAccessor.get(), &resolveValues, - &bAttribute); - - std::vector<std::unique_ptr<CFXJSE_Value>> values; - for (size_t i = 0; i < resolveValues.size() + 2; i++) - values.push_back(pdfium::MakeUnique<CFXJSE_Value>(pIsolate)); - - values[0]->SetInteger(1); - if (bAttribute) - values[1]->SetString(szName.AsStringView()); - else - values[1]->SetNull(); - - for (size_t i = 0; i < resolveValues.size(); i++) - values[i + 2]->Assign(resolveValues[i].get()); - - args.GetReturnValue()->SetArray(values); -} - -// static -void CXFA_FM2JSContext::eval_translation(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 1) { - pContext->ThrowParamCountMismatchException(L"Eval"); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = GetSimpleValue(pThis, args, 0); - ByteString argString = ValueToUTF8String(argOne.get()); - if (argString.IsEmpty()) { - pContext->ThrowArgumentMismatchException(); - return; - } - - WideString scriptString = WideString::FromUTF8(argString.AsStringView()); - CFX_WideTextBuf wsJavaScriptBuf; - if (!CXFA_FM2JSContext::Translate(scriptString.AsStringView(), - &wsJavaScriptBuf)) { - pContext->ThrowCompilerErrorException(); - return; - } - - args.GetReturnValue()->SetString( - FX_UTF8Encode(wsJavaScriptBuf.AsStringView()).AsStringView()); -} - -// static -void CXFA_FM2JSContext::is_fm_object(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 1) { - args.GetReturnValue()->SetBoolean(false); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); - args.GetReturnValue()->SetBoolean(argOne->IsObject()); -} - -// static -void CXFA_FM2JSContext::is_fm_array(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 1) { - args.GetReturnValue()->SetBoolean(false); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); - args.GetReturnValue()->SetBoolean(argOne->IsArray()); -} - -// static -void CXFA_FM2JSContext::get_fm_value(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 1) { - pContext->ThrowCompilerErrorException(); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); - if (argOne->IsArray()) { - v8::Isolate* pIsolate = pContext->GetScriptRuntime(); - auto propertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto jsObjectValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argOne->GetObjectPropertyByIdx(1, propertyValue.get()); - argOne->GetObjectPropertyByIdx(2, jsObjectValue.get()); - if (propertyValue->IsNull()) { - GetObjectDefaultValue(jsObjectValue.get(), args.GetReturnValue()); - return; - } - - jsObjectValue->GetObjectProperty(propertyValue->ToString().AsStringView(), - args.GetReturnValue()); - return; - } - - if (argOne->IsObject()) { - GetObjectDefaultValue(argOne.get(), args.GetReturnValue()); - return; - } - - args.GetReturnValue()->Assign(argOne.get()); -} - -// static -void CXFA_FM2JSContext::get_fm_jsobj(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - if (args.GetLength() != 1) { - ToJSContext(pThis, nullptr)->ThrowCompilerErrorException(); - return; - } - - std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); - if (!argOne->IsArray()) { - args.GetReturnValue()->Assign(argOne.get()); - return; - } - -#ifndef NDEBUG - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - v8::Isolate* pIsolate = pContext->GetScriptRuntime(); - auto lengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argOne->GetObjectProperty("length", lengthValue.get()); - ASSERT(lengthValue->ToInteger() >= 3); -#endif - - argOne->GetObjectPropertyByIdx(2, args.GetReturnValue()); -} - -// static -void CXFA_FM2JSContext::fm_var_filter(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - if (args.GetLength() != 1) { - pContext->ThrowCompilerErrorException(); - return; - } - - v8::Isolate* pIsolate = pContext->GetScriptRuntime(); - std::unique_ptr<CFXJSE_Value> argOne = args.GetValue(0); - if (!argOne->IsArray()) { - std::unique_ptr<CFXJSE_Value> simpleValue = GetSimpleValue(pThis, args, 0); - args.GetReturnValue()->Assign(simpleValue.get()); - return; - } - -#ifndef NDEBUG - auto lengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argOne->GetObjectProperty("length", lengthValue.get()); - ASSERT(lengthValue->ToInteger() >= 3); -#endif - - auto flagsValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argOne->GetObjectPropertyByIdx(0, flagsValue.get()); - int32_t iFlags = flagsValue->ToInteger(); - if (iFlags != 3 && iFlags != 4) { - std::unique_ptr<CFXJSE_Value> simpleValue = GetSimpleValue(pThis, args, 0); - args.GetReturnValue()->Assign(simpleValue.get()); - return; - } - - if (iFlags == 4) { - std::vector<std::unique_ptr<CFXJSE_Value>> values; - for (int32_t i = 0; i < 3; i++) - values.push_back(pdfium::MakeUnique<CFXJSE_Value>(pIsolate)); - - values[0]->SetInteger(3); - values[1]->SetNull(); - values[2]->SetNull(); - args.GetReturnValue()->SetArray(values); - return; - } - - auto objectValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argOne->GetObjectPropertyByIdx(2, objectValue.get()); - if (objectValue->IsNull()) { - pContext->ThrowCompilerErrorException(); - return; - } - args.GetReturnValue()->Assign(argOne.get()); -} - -// static -void CXFA_FM2JSContext::concat_fm_object(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); - uint32_t iLength = 0; - int32_t argc = args.GetLength(); - std::vector<std::unique_ptr<CFXJSE_Value>> argValues; - for (int32_t i = 0; i < argc; i++) { - argValues.push_back(args.GetValue(i)); - if (argValues[i]->IsArray()) { - auto lengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argValues[i]->GetObjectProperty("length", lengthValue.get()); - int32_t length = lengthValue->ToInteger(); - iLength = iLength + ((length > 2) ? (length - 2) : 0); - } - iLength += 1; - } - - std::vector<std::unique_ptr<CFXJSE_Value>> returnValues; - for (int32_t i = 0; i < (int32_t)iLength; i++) - returnValues.push_back(pdfium::MakeUnique<CFXJSE_Value>(pIsolate)); - - int32_t index = 0; - for (int32_t i = 0; i < argc; i++) { - if (argValues[i]->IsArray()) { - auto lengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argValues[i]->GetObjectProperty("length", lengthValue.get()); - - int32_t length = lengthValue->ToInteger(); - for (int32_t j = 2; j < length; j++) { - argValues[i]->GetObjectPropertyByIdx(j, returnValues[index].get()); - index++; - } - } - returnValues[index]->Assign(argValues[i].get()); - index++; - } - args.GetReturnValue()->SetArray(returnValues); -} - -// static -std::unique_ptr<CFXJSE_Value> CXFA_FM2JSContext::GetSimpleValue( - CFXJSE_Value* pThis, - CFXJSE_Arguments& args, - uint32_t index) { - v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); - ASSERT(index < (uint32_t)args.GetLength()); - - std::unique_ptr<CFXJSE_Value> argIndex = args.GetValue(index); - if (!argIndex->IsArray() && !argIndex->IsObject()) - return argIndex; - - if (argIndex->IsArray()) { - auto lengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argIndex->GetObjectProperty("length", lengthValue.get()); - int32_t iLength = lengthValue->ToInteger(); - auto simpleValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - if (iLength < 3) { - simpleValue.get()->SetUndefined(); - return simpleValue; - } - - auto propertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto jsObjectValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argIndex->GetObjectPropertyByIdx(1, propertyValue.get()); - argIndex->GetObjectPropertyByIdx(2, jsObjectValue.get()); - if (propertyValue->IsNull()) { - GetObjectDefaultValue(jsObjectValue.get(), simpleValue.get()); - return simpleValue; - } - - jsObjectValue->GetObjectProperty(propertyValue->ToString().AsStringView(), - simpleValue.get()); - return simpleValue; - } - - auto defaultValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - GetObjectDefaultValue(argIndex.get(), defaultValue.get()); - return defaultValue; -} - -// static -bool CXFA_FM2JSContext::ValueIsNull(CFXJSE_Value* pThis, CFXJSE_Value* arg) { - if (!arg || arg->IsNull()) - return true; - - if (!arg->IsArray() && !arg->IsObject()) - return false; - - v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); - if (arg->IsArray()) { - int32_t iLength = hvalue_get_array_length(pThis, arg); - if (iLength < 3) - return true; - - auto propertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto jsObjectValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - arg->GetObjectPropertyByIdx(1, propertyValue.get()); - arg->GetObjectPropertyByIdx(2, jsObjectValue.get()); - if (propertyValue->IsNull()) { - auto defaultValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - GetObjectDefaultValue(jsObjectValue.get(), defaultValue.get()); - return defaultValue->IsNull(); - } - - auto newPropertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - jsObjectValue->GetObjectProperty(propertyValue->ToString().AsStringView(), - newPropertyValue.get()); - return newPropertyValue->IsNull(); - } - - auto defaultValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - GetObjectDefaultValue(arg, defaultValue.get()); - return defaultValue->IsNull(); -} - -// static -int32_t CXFA_FM2JSContext::hvalue_get_array_length(CFXJSE_Value* pThis, - CFXJSE_Value* arg) { - if (!arg || !arg->IsArray()) - return 0; - - v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); - auto lengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - arg->GetObjectProperty("length", lengthValue.get()); - return lengthValue->ToInteger(); -} - -// static -bool CXFA_FM2JSContext::simpleValueCompare(CFXJSE_Value* pThis, - CFXJSE_Value* firstValue, - CFXJSE_Value* secondValue) { - if (!firstValue) - return false; - - if (firstValue->IsString()) { - ByteString firstString = ValueToUTF8String(firstValue); - ByteString secondString = ValueToUTF8String(secondValue); - return firstString == secondString; - } - if (firstValue->IsNumber()) { - float first = ValueToFloat(pThis, firstValue); - float second = ValueToFloat(pThis, secondValue); - return first == second; - } - if (firstValue->IsBoolean()) - return firstValue->ToBoolean() == secondValue->ToBoolean(); - - return firstValue->IsNull() && secondValue && secondValue->IsNull(); -} - -// static -void CXFA_FM2JSContext::unfoldArgs( - CFXJSE_Value* pThis, - CFXJSE_Arguments& args, - std::vector<std::unique_ptr<CFXJSE_Value>>* resultValues, - int32_t iStart) { - resultValues->clear(); - - int32_t iCount = 0; - v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); - int32_t argc = args.GetLength(); - std::vector<std::unique_ptr<CFXJSE_Value>> argsValue; - for (int32_t i = 0; i < argc - iStart; i++) { - argsValue.push_back(args.GetValue(i + iStart)); - if (argsValue[i]->IsArray()) { - auto lengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argsValue[i]->GetObjectProperty("length", lengthValue.get()); - int32_t iLength = lengthValue->ToInteger(); - iCount += ((iLength > 2) ? (iLength - 2) : 0); - } else { - iCount += 1; - } - } - - for (int32_t i = 0; i < iCount; i++) - resultValues->push_back(pdfium::MakeUnique<CFXJSE_Value>(pIsolate)); - - int32_t index = 0; - for (int32_t i = 0; i < argc - iStart; i++) { - if (argsValue[i]->IsArray()) { - auto lengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argsValue[i]->GetObjectProperty("length", lengthValue.get()); - int32_t iLength = lengthValue->ToInteger(); - if (iLength < 3) - continue; - - auto propertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto jsObjectValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - argsValue[i]->GetObjectPropertyByIdx(1, propertyValue.get()); - if (propertyValue->IsNull()) { - for (int32_t j = 2; j < iLength; j++) { - argsValue[i]->GetObjectPropertyByIdx(j, jsObjectValue.get()); - GetObjectDefaultValue(jsObjectValue.get(), - (*resultValues)[index].get()); - index++; - } - } else { - for (int32_t j = 2; j < iLength; j++) { - argsValue[i]->GetObjectPropertyByIdx(j, jsObjectValue.get()); - jsObjectValue->GetObjectProperty( - propertyValue->ToString().AsStringView(), - (*resultValues)[index].get()); - index++; - } - } - } else if (argsValue[i]->IsObject()) { - GetObjectDefaultValue(argsValue[i].get(), (*resultValues)[index].get()); - index++; - } else { - (*resultValues)[index]->Assign(argsValue[i].get()); - index++; - } - } -} - -// static -void CXFA_FM2JSContext::GetObjectDefaultValue(CFXJSE_Value* pValue, - CFXJSE_Value* pDefaultValue) { - CXFA_Node* pNode = ToNode(CXFA_ScriptContext::ToObject(pValue, nullptr)); - if (!pNode) { - pDefaultValue->SetNull(); - return; - } - pNode->Script_Som_DefaultValue(pDefaultValue, false, (XFA_ATTRIBUTE)-1); -} - -// static -bool CXFA_FM2JSContext::SetObjectDefaultValue(CFXJSE_Value* pValue, - CFXJSE_Value* hNewValue) { - CXFA_Node* pNode = ToNode(CXFA_ScriptContext::ToObject(pValue, nullptr)); - if (!pNode) - return false; - - pNode->Script_Som_DefaultValue(hNewValue, true, (XFA_ATTRIBUTE)-1); - return true; -} - -// static -ByteString CXFA_FM2JSContext::GenerateSomExpression( - const ByteStringView& szName, - int32_t iIndexFlags, - int32_t iIndexValue, - bool bIsStar) { - if (bIsStar) - return ByteString(szName, "[*]"); - - if (iIndexFlags == 0) - return ByteString(szName); - - if (iIndexFlags == 1 || iIndexValue == 0) { - return ByteString(szName, "[") + ByteString::FormatInteger(iIndexValue) + - "]"; - } - ByteString szSomExp; - if (iIndexFlags == 2) { - szSomExp = (iIndexValue < 0) ? (szName + "[-") : (szName + "[+"); - iIndexValue = (iIndexValue < 0) ? (0 - iIndexValue) : iIndexValue; - szSomExp += ByteString::FormatInteger(iIndexValue); - szSomExp += "]"; - } else { - szSomExp = (iIndexValue < 0) ? (szName + "[") : (szName + "[-"); - iIndexValue = (iIndexValue < 0) ? (0 - iIndexValue) : iIndexValue; - szSomExp += ByteString::FormatInteger(iIndexValue); - szSomExp += "]"; - } - return szSomExp; -} - -// static -bool CXFA_FM2JSContext::GetObjectForName(CFXJSE_Value* pThis, - CFXJSE_Value* accessorValue, - const ByteStringView& szAccessorName) { - CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument(); - if (!pDoc) - return false; - - CXFA_ScriptContext* pScriptContext = pDoc->GetScriptContext(); - XFA_RESOLVENODE_RS resoveNodeRS; - uint32_t dwFlags = XFA_RESOLVENODE_Children | XFA_RESOLVENODE_Properties | - XFA_RESOLVENODE_Siblings | XFA_RESOLVENODE_Parent; - int32_t iRet = pScriptContext->ResolveObjects( - pScriptContext->GetThisObject(), - WideString::FromUTF8(szAccessorName).AsStringView(), resoveNodeRS, - dwFlags); - if (iRet >= 1 && resoveNodeRS.dwFlags == XFA_RESOVENODE_RSTYPE_Nodes) { - accessorValue->Assign( - pScriptContext->GetJSValueFromMap(resoveNodeRS.objects.front())); - return true; - } - return false; -} - -// static -int32_t CXFA_FM2JSContext::ResolveObjects(CFXJSE_Value* pThis, - CFXJSE_Value* pRefValue, - const ByteStringView& bsSomExp, - XFA_RESOLVENODE_RS& resoveNodeRS, - bool bdotAccessor, - bool bHasNoResolveName) { - CXFA_Document* pDoc = ToJSContext(pThis, nullptr)->GetDocument(); - if (!pDoc) - return -1; - - WideString wsSomExpression = WideString::FromUTF8(bsSomExp); - CXFA_ScriptContext* pScriptContext = pDoc->GetScriptContext(); - CXFA_Object* pNode = nullptr; - uint32_t dFlags = 0UL; - if (bdotAccessor) { - if (pRefValue && pRefValue->IsNull()) { - pNode = pScriptContext->GetThisObject(); - dFlags = XFA_RESOLVENODE_Siblings | XFA_RESOLVENODE_Parent; - } else { - pNode = CXFA_ScriptContext::ToObject(pRefValue, nullptr); - ASSERT(pNode); - if (bHasNoResolveName) { - WideString wsName; - if (CXFA_Node* pXFANode = pNode->AsNode()) - pXFANode->GetAttribute(XFA_ATTRIBUTE_Name, wsName, false); - if (wsName.IsEmpty()) - wsName = L"#" + pNode->GetClassName(); - - wsSomExpression = wsName + wsSomExpression; - dFlags = XFA_RESOLVENODE_Siblings; - } else { - dFlags = (bsSomExp == "*") - ? (XFA_RESOLVENODE_Children) - : (XFA_RESOLVENODE_Children | XFA_RESOLVENODE_Attributes | - XFA_RESOLVENODE_Properties); - } - } - } else { - pNode = CXFA_ScriptContext::ToObject(pRefValue, nullptr); - dFlags = XFA_RESOLVENODE_AnyChild; - } - return pScriptContext->ResolveObjects(pNode, wsSomExpression.AsStringView(), - resoveNodeRS, dFlags); -} - -// static -void CXFA_FM2JSContext::ParseResolveResult( - CFXJSE_Value* pThis, - const XFA_RESOLVENODE_RS& resoveNodeRS, - CFXJSE_Value* pParentValue, - std::vector<std::unique_ptr<CFXJSE_Value>>* resultValues, - bool* bAttribute) { - ASSERT(bAttribute); - - resultValues->clear(); - - CXFA_FM2JSContext* pContext = ToJSContext(pThis, nullptr); - v8::Isolate* pIsolate = pContext->GetScriptRuntime(); - - if (resoveNodeRS.dwFlags == XFA_RESOVENODE_RSTYPE_Nodes) { - *bAttribute = false; - CXFA_ScriptContext* pScriptContext = - pContext->GetDocument()->GetScriptContext(); - for (CXFA_Object* pObject : resoveNodeRS.objects) { - resultValues->push_back(pdfium::MakeUnique<CFXJSE_Value>(pIsolate)); - resultValues->back()->Assign(pScriptContext->GetJSValueFromMap(pObject)); - } - return; - } - - CXFA_ValueArray objectProperties(pIsolate); - int32_t iRet = resoveNodeRS.GetAttributeResult(&objectProperties); - *bAttribute = true; - if (iRet != 0) { - *bAttribute = false; - for (int32_t i = 0; i < iRet; i++) { - resultValues->push_back(pdfium::MakeUnique<CFXJSE_Value>(pIsolate)); - resultValues->back()->Assign(objectProperties.m_Values[i].get()); - } - return; - } - - if (!pParentValue || !pParentValue->IsObject()) - return; - - resultValues->push_back(pdfium::MakeUnique<CFXJSE_Value>(pIsolate)); - resultValues->back()->Assign(pParentValue); -} - -// static -int32_t CXFA_FM2JSContext::ValueToInteger(CFXJSE_Value* pThis, - CFXJSE_Value* pValue) { - if (!pValue) - return 0; - - v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); - if (pValue->IsArray()) { - auto propertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto jsObjectValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto newPropertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - pValue->GetObjectPropertyByIdx(1, propertyValue.get()); - pValue->GetObjectPropertyByIdx(2, jsObjectValue.get()); - if (propertyValue->IsNull()) { - GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get()); - return ValueToInteger(pThis, newPropertyValue.get()); - } - - jsObjectValue->GetObjectProperty(propertyValue->ToString().AsStringView(), - newPropertyValue.get()); - return ValueToInteger(pThis, newPropertyValue.get()); - } - if (pValue->IsObject()) { - auto newPropertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - GetObjectDefaultValue(pValue, newPropertyValue.get()); - return ValueToInteger(pThis, newPropertyValue.get()); - } - if (pValue->IsString()) - return FXSYS_atoi(pValue->ToString().c_str()); - return pValue->ToInteger(); -} - -// static -float CXFA_FM2JSContext::ValueToFloat(CFXJSE_Value* pThis, CFXJSE_Value* arg) { - if (!arg) - return 0.0f; - - v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); - if (arg->IsArray()) { - auto propertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto jsObjectValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto newPropertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - arg->GetObjectPropertyByIdx(1, propertyValue.get()); - arg->GetObjectPropertyByIdx(2, jsObjectValue.get()); - if (propertyValue->IsNull()) { - GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get()); - return ValueToFloat(pThis, newPropertyValue.get()); - } - jsObjectValue->GetObjectProperty(propertyValue->ToString().AsStringView(), - newPropertyValue.get()); - return ValueToFloat(pThis, newPropertyValue.get()); - } - if (arg->IsObject()) { - auto newPropertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - GetObjectDefaultValue(arg, newPropertyValue.get()); - return ValueToFloat(pThis, newPropertyValue.get()); - } - if (arg->IsString()) - return (float)XFA_ByteStringToDouble(arg->ToString().AsStringView()); - if (arg->IsUndefined()) - return 0; - - return arg->ToFloat(); -} - -// static -double CXFA_FM2JSContext::ValueToDouble(CFXJSE_Value* pThis, - CFXJSE_Value* arg) { - if (!arg) - return 0; - - v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); - if (arg->IsArray()) { - auto propertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto jsObjectValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto newPropertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - arg->GetObjectPropertyByIdx(1, propertyValue.get()); - arg->GetObjectPropertyByIdx(2, jsObjectValue.get()); - if (propertyValue->IsNull()) { - GetObjectDefaultValue(jsObjectValue.get(), newPropertyValue.get()); - return ValueToDouble(pThis, newPropertyValue.get()); - } - jsObjectValue->GetObjectProperty(propertyValue->ToString().AsStringView(), - newPropertyValue.get()); - return ValueToDouble(pThis, newPropertyValue.get()); - } - if (arg->IsObject()) { - auto newPropertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - GetObjectDefaultValue(arg, newPropertyValue.get()); - return ValueToDouble(pThis, newPropertyValue.get()); - } - if (arg->IsString()) - return XFA_ByteStringToDouble(arg->ToString().AsStringView()); - if (arg->IsUndefined()) - return 0; - return arg->ToDouble(); -} - -// static. -double CXFA_FM2JSContext::ExtractDouble(CFXJSE_Value* pThis, - CFXJSE_Value* src, - bool* ret) { - ASSERT(ret); - *ret = true; - - if (!src) - return 0; - - if (!src->IsArray()) - return ValueToDouble(pThis, src); - - v8::Isolate* pIsolate = ToJSContext(pThis, nullptr)->GetScriptRuntime(); - auto lengthValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - src->GetObjectProperty("length", lengthValue.get()); - int32_t iLength = lengthValue->ToInteger(); - if (iLength <= 2) { - *ret = false; - return 0.0; - } - - auto propertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - auto jsObjectValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - src->GetObjectPropertyByIdx(1, propertyValue.get()); - src->GetObjectPropertyByIdx(2, jsObjectValue.get()); - if (propertyValue->IsNull()) - return ValueToDouble(pThis, jsObjectValue.get()); - - auto newPropertyValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate); - jsObjectValue->GetObjectProperty(propertyValue->ToString().AsStringView(), - newPropertyValue.get()); - return ValueToDouble(pThis, newPropertyValue.get()); -} - -// static -ByteString CXFA_FM2JSContext::ValueToUTF8String(CFXJSE_Value* arg) { - if (!arg || arg->IsNull() || arg->IsUndefined()) - return ByteString(); - if (arg->IsBoolean()) - return arg->ToBoolean() ? "1" : "0"; - return arg->ToString(); -} - -// static. -bool CXFA_FM2JSContext::Translate(const WideStringView& wsFormcalc, - CFX_WideTextBuf* wsJavascript) { - if (wsFormcalc.IsEmpty()) { - wsJavascript->Clear(); - return true; - } - - CXFA_FMParser parser(wsFormcalc); - std::unique_ptr<CXFA_FMFunctionDefinition> func = parser.Parse(); - if (!func || parser.HasError()) - return false; - - CXFA_FMToJavaScriptDepth::Reset(); - if (!func->ToJavaScript(*wsJavascript)) - return false; - - wsJavascript->AppendChar(0); - - return !CXFA_IsTooBig(*wsJavascript); -} - -CXFA_FM2JSContext::CXFA_FM2JSContext(v8::Isolate* pScriptIsolate, - CFXJSE_Context* pScriptContext, - CXFA_Document* pDoc) - : CFXJSE_HostObject(kFM2JS), - m_pIsolate(pScriptIsolate), - m_pFMClass(CFXJSE_Class::Create(pScriptContext, - &formcalc_fm2js_descriptor, - false)), - m_pValue(pdfium::MakeUnique<CFXJSE_Value>(pScriptIsolate)), - m_pDocument(pDoc) { - m_pValue.get()->SetObject(this, m_pFMClass); -} - -CXFA_FM2JSContext::~CXFA_FM2JSContext() {} - -void CXFA_FM2JSContext::GlobalPropertyGetter(CFXJSE_Value* pValue) { - pValue->Assign(m_pValue.get()); -} - -void CXFA_FM2JSContext::ThrowNoDefaultPropertyException( - const ByteStringView& name) const { - // TODO(tsepez): check usage of c_str() below. - ThrowException(L"%.16S doesn't have a default property.", - name.unterminated_c_str()); -} - -void CXFA_FM2JSContext::ThrowCompilerErrorException() const { - ThrowException(L"Compiler error."); -} - -void CXFA_FM2JSContext::ThrowDivideByZeroException() const { - ThrowException(L"Divide by zero."); -} - -void CXFA_FM2JSContext::ThrowServerDeniedException() const { - ThrowException(L"Server does not permit operation."); -} - -void CXFA_FM2JSContext::ThrowPropertyNotInObjectException( - const WideString& name, - const WideString& exp) const { - ThrowException( - L"An attempt was made to reference property '%.16s' of a non-object " - L"in SOM expression %.16s.", - name.c_str(), exp.c_str()); -} - -void CXFA_FM2JSContext::ThrowParamCountMismatchException( - const WideString& method) const { - ThrowException(L"Incorrect number of parameters calling method '%.16s'.", - method.c_str()); -} - -void CXFA_FM2JSContext::ThrowArgumentMismatchException() const { - ThrowException(L"Argument mismatch in property or function argument."); -} - -void CXFA_FM2JSContext::ThrowException(const wchar_t* str, ...) const { - WideString wsMessage; - va_list arg_ptr; - va_start(arg_ptr, str); - wsMessage.FormatV(str, arg_ptr); - va_end(arg_ptr); - ASSERT(!wsMessage.IsEmpty()); - FXJSE_ThrowMessage(wsMessage.UTF8Encode().AsStringView()); -} diff --git a/xfa/fxfa/fm2js/cxfa_fm2jscontext.h b/xfa/fxfa/fm2js/cxfa_fm2jscontext.h deleted file mode 100644 index 8d1b33bf81..0000000000 --- a/xfa/fxfa/fm2js/cxfa_fm2jscontext.h +++ /dev/null @@ -1,445 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef XFA_FXFA_FM2JS_CXFA_FM2JSCONTEXT_H_ -#define XFA_FXFA_FM2JS_CXFA_FM2JSCONTEXT_H_ - -#include <memory> -#include <vector> - -#include "fxjs/cfxjse_arguments.h" -#include "fxjs/cfxjse_context.h" -#include "xfa/fxfa/parser/xfa_resolvenode_rs.h" - -class CFX_WideTextBuf; -class CXFA_Document; - -class CXFA_FM2JSContext : public CFXJSE_HostObject { - public: - CXFA_FM2JSContext(v8::Isolate* pScriptIsolate, - CFXJSE_Context* pScriptContext, - CXFA_Document* pDoc); - ~CXFA_FM2JSContext() override; - - static void Abs(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Avg(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Ceil(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Count(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Floor(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Max(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Min(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Mod(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Round(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Sum(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Date(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Date2Num(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void DateFmt(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void IsoDate2Num(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void IsoTime2Num(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void LocalDateFmt(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void LocalTimeFmt(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Num2Date(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Num2GMTime(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Num2Time(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Time(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Time2Num(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void TimeFmt(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - - static bool IsIsoDateFormat(const char* pData, - int32_t iLength, - int32_t& iStyle, - int32_t& iYear, - int32_t& iMonth, - int32_t& iDay); - static bool IsIsoTimeFormat(const char* pData, - int32_t iLength, - int32_t& iHour, - int32_t& iMinute, - int32_t& iSecond, - int32_t& iMilliSecond, - int32_t& iZoneHour, - int32_t& iZoneMinute); - static bool IsIsoDateTimeFormat(const char* pData, - int32_t iLength, - int32_t& iYear, - int32_t& iMonth, - int32_t& iDay, - int32_t& iHour, - int32_t& iMinute, - int32_t& iSecond, - int32_t& iMillionSecond, - int32_t& iZoneHour, - int32_t& iZoneMinute); - static ByteString Local2IsoDate(CFXJSE_Value* pThis, - const ByteStringView& szDate, - const ByteStringView& szFormat, - const ByteStringView& szLocale); - static ByteString IsoDate2Local(CFXJSE_Value* pThis, - const ByteStringView& szDate, - const ByteStringView& szFormat, - const ByteStringView& szLocale); - static ByteString IsoTime2Local(CFXJSE_Value* pThis, - const ByteStringView& szTime, - const ByteStringView& szFormat, - const ByteStringView& szLocale); - static int32_t DateString2Num(const ByteStringView& szDateString); - static ByteString GetLocalDateFormat(CFXJSE_Value* pThis, - int32_t iStyle, - const ByteStringView& szLocalStr, - bool bStandard); - static ByteString GetLocalTimeFormat(CFXJSE_Value* pThis, - int32_t iStyle, - const ByteStringView& szLocalStr, - bool bStandard); - static ByteString GetStandardDateFormat(CFXJSE_Value* pThis, - int32_t iStyle, - const ByteStringView& szLocalStr); - static ByteString GetStandardTimeFormat(CFXJSE_Value* pThis, - int32_t iStyle, - const ByteStringView& szLocalStr); - static ByteString Num2AllTime(CFXJSE_Value* pThis, - int32_t iTime, - const ByteStringView& szFormat, - const ByteStringView& szLocale, - bool bGM); - static void GetLocalTimeZone(int32_t& iHour, int32_t& iMin, int32_t& iSec); - - static void Apr(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void CTerm(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void FV(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void IPmt(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void NPV(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Pmt(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void PPmt(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void PV(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Rate(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Term(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Choose(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Exists(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void HasValue(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Oneof(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Within(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void If(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Eval(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Ref(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void UnitType(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void UnitValue(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - - static void At(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Concat(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Decode(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static WideString DecodeURL(const WideString& wsURLString); - static WideString DecodeHTML(const WideString& wsHTMLString); - static WideString DecodeXML(const WideString& wsXMLString); - static void Encode(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static WideString EncodeURL(const ByteString& szURLString); - static WideString EncodeHTML(const ByteString& szHTMLString); - static WideString EncodeXML(const ByteString& szXMLString); - static bool HTMLSTR2Code(const WideStringView& pData, uint32_t* iCode); - static bool HTMLCode2STR(uint32_t iCode, WideString* wsHTMLReserve); - static void Format(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Left(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Len(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Lower(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Ltrim(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Parse(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Replace(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Right(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Rtrim(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Space(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Str(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Stuff(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Substr(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Uuid(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Upper(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void WordNum(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static ByteString TrillionUS(const ByteStringView& szData); - static ByteString WordUS(const ByteString& szData, int32_t iStyle); - - static void Get(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Post(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void Put(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void assign_value_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void logical_or_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void logical_and_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void equality_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void notequality_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static bool fm_ref_equal(CFXJSE_Value* pThis, CFXJSE_Arguments& args); - static void less_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void lessequal_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void greater_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void greaterequal_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void plus_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void minus_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void multiple_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void divide_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void positive_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void negative_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void logical_not_operator(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void dot_accessor(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void dotdot_accessor(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void eval_translation(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void is_fm_object(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void is_fm_array(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void get_fm_value(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void get_fm_jsobj(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void fm_var_filter(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static void concat_fm_object(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - - static int32_t hvalue_get_array_length(CFXJSE_Value* pThis, - CFXJSE_Value* arg); - static bool simpleValueCompare(CFXJSE_Value* pThis, - CFXJSE_Value* firstValue, - CFXJSE_Value* secondValue); - static void unfoldArgs( - CFXJSE_Value* pThis, - CFXJSE_Arguments& args, - std::vector<std::unique_ptr<CFXJSE_Value>>* resultValues, - int32_t iStart = 0); - static void GetObjectDefaultValue(CFXJSE_Value* pObjectValue, - CFXJSE_Value* pDefaultValue); - static bool SetObjectDefaultValue(CFXJSE_Value* pObjectValue, - CFXJSE_Value* pNewValue); - static ByteString GenerateSomExpression(const ByteStringView& szName, - int32_t iIndexFlags, - int32_t iIndexValue, - bool bIsStar); - static bool GetObjectForName(CFXJSE_Value* pThis, - CFXJSE_Value* accessorValue, - const ByteStringView& szAccessorName); - static int32_t ResolveObjects(CFXJSE_Value* pThis, - CFXJSE_Value* pParentValue, - const ByteStringView& bsSomExp, - XFA_RESOLVENODE_RS& resoveNodeRS, - bool bdotAccessor = true, - bool bHasNoResolveName = false); - static void ParseResolveResult( - CFXJSE_Value* pThis, - const XFA_RESOLVENODE_RS& resoveNodeRS, - CFXJSE_Value* pParentValue, - std::vector<std::unique_ptr<CFXJSE_Value>>* resultValues, - bool* bAttribute); - - static std::unique_ptr<CFXJSE_Value> GetSimpleValue(CFXJSE_Value* pThis, - CFXJSE_Arguments& args, - uint32_t index); - static bool ValueIsNull(CFXJSE_Value* pThis, CFXJSE_Value* pValue); - static int32_t ValueToInteger(CFXJSE_Value* pThis, CFXJSE_Value* pValue); - static float ValueToFloat(CFXJSE_Value* pThis, CFXJSE_Value* pValue); - static double ValueToDouble(CFXJSE_Value* pThis, CFXJSE_Value* pValue); - static ByteString ValueToUTF8String(CFXJSE_Value* pValue); - static double ExtractDouble(CFXJSE_Value* pThis, - CFXJSE_Value* src, - bool* ret); - - static bool Translate(const WideStringView& wsFormcalc, - CFX_WideTextBuf* wsJavascript); - - void GlobalPropertyGetter(CFXJSE_Value* pValue); - - private: - v8::Isolate* GetScriptRuntime() const { return m_pIsolate; } - CXFA_Document* GetDocument() const { return m_pDocument.Get(); } - - void ThrowNoDefaultPropertyException(const ByteStringView& name) const; - void ThrowCompilerErrorException() const; - void ThrowDivideByZeroException() const; - void ThrowServerDeniedException() const; - void ThrowPropertyNotInObjectException(const WideString& name, - const WideString& exp) const; - void ThrowArgumentMismatchException() const; - void ThrowParamCountMismatchException(const WideString& method) const; - void ThrowException(const wchar_t* str, ...) const; - - v8::Isolate* m_pIsolate; - CFXJSE_Class* m_pFMClass; - std::unique_ptr<CFXJSE_Value> m_pValue; - UnownedPtr<CXFA_Document> const m_pDocument; -}; - -#endif // XFA_FXFA_FM2JS_CXFA_FM2JSCONTEXT_H_ diff --git a/xfa/fxfa/fm2js/cxfa_fm2jscontext_embeddertest.cpp b/xfa/fxfa/fm2js/cxfa_fm2jscontext_embeddertest.cpp deleted file mode 100644 index ff2e200186..0000000000 --- a/xfa/fxfa/fm2js/cxfa_fm2jscontext_embeddertest.cpp +++ /dev/null @@ -1,1446 +0,0 @@ -// Copyright 2017 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "testing/gtest/include/gtest/gtest.h" -#include "testing/xfa_js_embedder_test.h" - -class FM2JSContextEmbedderTest : public XFAJSEmbedderTest {}; - -// TODO(dsinclair): Comment out tests are broken and need to be fixed. - -TEST_F(FM2JSContextEmbedderTest, TranslateEmpty) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - const char input[] = ""; - EXPECT_TRUE(Execute(input)); - // TODO(dsinclair): This should probably throw as a blank formcalc script - // is invalid. -} - -TEST_F(FM2JSContextEmbedderTest, TranslateNumber) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - const char input[] = "123"; - EXPECT_TRUE(Execute(input)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()); - EXPECT_EQ(123, value->ToInteger()) << "Program: " << input; -} - -TEST_F(FM2JSContextEmbedderTest, Numeric) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - int result; - } tests[] = {{"123 + 456", 579}, - {"2 - 3 * 10 / 2 + 7", -6}, - {"10 * 3 + 5 * 4", 50}, - {"(5 - \"abc\") * 3", 15}, - {"\"100\" / 10e1", 1}, - {"5 + null + 3", 8}, - // {"if (\"abc\") then\n" - // " 10\n" - // "else\n" - // " 20\n" - // "endif", - // 20}, - // {"3 / 0 + 1", 0}, - {"-(17)", -17}, - {"-(-17)", 17}, - {"+(17)", 17}, - {"+(-17)", -17}, - {"if (1 < 2) then\n1\nendif", 1}, - {"if (\"abc\" > \"def\") then\n" - " 1 and 0\n" - "else\n" - " 0\n" - "endif", - 0}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()); - EXPECT_EQ(tests[i].result, value->ToInteger()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, Strings) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = { - {"\"abc\"", "abc"}, - {"concat(\"The total is \", 2, \" dollars and \", 57, \" cents.\")", - "The total is 2 dollars and 57 cents."}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, Booleans) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - bool result; - } tests[] = {{"0 and 1 or 2 > 1", true}, - {"2 < 3 not 1 == 1", false}, - {"\"abc\" | 2", true}, - {"1 or 0", true}, - {"0 | 0", false}, - {"0 or 1 | 0 or 0", true}, - {"1 and 0", false}, - // {"0 & 0", true}, // TODO(dsinclair) Confirm with Reader. - {"0 and 1 & 0 and 0", false}, - {"not(\"true\")", true}, - {"not(1)", false}, - {"3 == 3", true}, - {"3 <> 4", true}, - {"\"abc\" eq \"def\"", false}, - {"\"def\" ne \"abc\"", true}, - {"5 + 5 == 10", true}, - {"5 + 5 <> \"10\"", false}, - {"3 < 3", false}, - {"3 > 4", false}, - {"\"abc\" <= \"def\"", true}, - {"\"def\" > \"abc\"", true}, - {"12 >= 12", true}, - {"\"true\" < \"false\"", false}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()) << "Program: " << tests[i].program; - EXPECT_EQ(tests[i].result, value->ToBoolean()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, Abs) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - float result; - } tests[] = {{"Abs(1.03)", 1.03f}, {"Abs(-1.03)", 1.03f}, {"Abs(0)", 0.0f}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsNumber()); - EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, Avg) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - float result; - } tests[] = {{"Avg(0, 32, 16)", 16.0f}, {"Avg(2.5, 17, null)", 9.75f}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsNumber()); - EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, Ceil) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - int result; - } tests[] = {{"Ceil(2.5875)", 3}, {"Ceil(-5.9)", -5}, {"Ceil(\"abc\")", 0}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()); - EXPECT_EQ(tests[i].result, value->ToInteger()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, Count) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - int result; - } tests[] = {{"Count(\"Tony\", \"Blue\", 41)", 3}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()); - EXPECT_EQ(tests[i].result, value->ToInteger()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, Floor) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - int result; - } tests[] = {{"Floor(21.3409873)", 21}, - {"Floor(5.999965342)", 5}, - {"Floor(3.2 * 15)", 48}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()); - EXPECT_EQ(tests[i].result, value->ToInteger()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, Max) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - int result; - } tests[] = {{"Max(234, 15, 107)", 234}, - {"Max(\"abc\", 15, \"Tony Blue\")", 15}, - {"Max(\"abc\")", 0}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()); - EXPECT_EQ(tests[i].result, value->ToInteger()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, Min) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - int result; - } tests[] = {{"Min(234, 15, 107)", 15}, - // TODO(dsinclair): Verify with Reader; I believe this should - // have a return of 0. - // {"Min(\"abc\", 15, \"Tony Blue\")", 15}, - {"Min(\"abc\")", 0}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()); - EXPECT_EQ(tests[i].result, value->ToInteger()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, Mod) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - int result; - } tests[] = {{"Mod(64, -3)", 1}, {"Mod(-13, 3)", -1}, {"Mod(\"abc\", 2)", 0}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()); - EXPECT_EQ(tests[i].result, value->ToInteger()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, Round) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - float result; - } tests[] = {{"Round(12.389764537, 4)", 12.3898f}, - {"Round(20/3, 2)", 6.67f}, - {"Round(8.9897, \"abc\")", 9.0f}, - {"Round(FV(400, 0.10/12, 30*12), 2)", 904195.17f}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsNumber()) << "Program: " << tests[i].program; - EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, Sum) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - int result; - } tests[] = {{"Sum(2, 4, 6, 8)", 20}, - {"Sum(-2, 4, -6, 8)", 4}, - {"Sum(4, 16, \"abc\", 19)", 39}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()); - EXPECT_EQ(tests[i].result, value->ToInteger()) - << "Program: " << tests[i].program; - } -} - -// TEST_F(FM2JSContextEmbedderTest, DISABLED_Date) { -// ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); -// -// TODO(dsinclair): Make compatible with windows. -// time_t seconds = time(nullptr); -// int days = seconds / (60 * 60 * 24); - -// EXPECT_TRUE(Execute("Date()")); - -// CFXJSE_Value* value = GetValue(); -// EXPECT_TRUE(value->IsNumber()); -// EXPECT_EQ(days, value->ToInteger()); -// } - -TEST_F(FM2JSContextEmbedderTest, Date2Num) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - int result; - } tests[] = { - // {"Date2Num(\"Mar 15, 1996\")", 35138}, - {"Date2Num(\"1/1/1900\", \"D/M/YYYY\")", 1}, - {"Date2Num(\"03/15/96\", \"MM/DD/YY\")", 35138}, - // {"Date2Num(\"Aug 1, 1996\", \"MMM D, YYYY\")", 35277}, - {"Date2Num(\"96-08-20\", \"YY-MM-DD\", \"fr_FR\")", 35296}, - {"Date2Num(\"1/3/00\", \"D/M/YY\") - Date2Num(\"1/2/00\", \"D/M/YY\")", - 29}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()); - EXPECT_EQ(tests[i].result, value->ToInteger()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, DateFmt) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = { - // {"DateFmt(1)", "M/D/YY"}, - // {"DateFmt(2, \"fr_CA\")", "YY-MM-DD"}, - {"DateFmt(3, \"de_DE\")", "D. MMMM YYYY"}, - // {"DateFmt(4, \"fr_FR\")", "EEE D' MMMM YYYY"} - }; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, IsoDate2Num) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - int result; - } tests[] = {{"IsoDate2Num(\"1900\")", 1}, - {"IsoDate2Num(\"1900-01\")", 1}, - {"IsoDate2Num(\"1900-01-01\")", 1}, - {"IsoDate2Num(\"19960315T20:20:20\")", 35138}, - {"IsoDate2Num(\"2000-03-01\") - IsoDate2Num(\"20000201\")", 29}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()); - EXPECT_EQ(tests[i].result, value->ToInteger()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, DISABLED_IsoTime2Num) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - int result; - } tests[] = {{"IsoTime2Num(\"00:00:00Z\")", 1}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()); - EXPECT_EQ(tests[i].result, value->ToInteger()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, LocalDateFmt) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {// {"LocalDateFmt(1, \"de_DE\")", "tt.MM.uu"}, - // {"LocalDateFmt(2, \"fr_CA\")", "aa-MM-jj"}, - {"LocalDateFmt(3, \"de_CH\")", "t. MMMM jjjj"}, - {"LocalDateFmt(4, \"fr_FR\")", "EEEE j MMMM aaaa"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, DISABLED_LocalTimeFmt) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"LocalTimeFmt(1, \"de_DE\")", "HH:mm"}, - {"LocalTimeFmt(2, \"fr_CA\")", "HH:mm::ss"}, - {"LocalTimeFmt(3, \"de_CH\")", "HH:mm:ss z"}, - {"LocalTimeFmt(4, \"fr_FR\")", "HH' h 'mm z"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, Num2Date) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = { - {"Num2Date(1, \"DD/MM/YYYY\")", "01/01/1900"}, - {"Num2Date(35139, \"DD-MMM-YYYY\", \"de_DE\")", "16-Mrz-1996"}, - // {"Num2Date(Date2Num(\"Mar 15, 2000\") - Date2Num(\"98-03-15\", " - // "\"YY-MM-DD\", \"fr_CA\"))", - // "Jan 1, 1902"} - }; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()) << "Program: " << tests[i].program; - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, DISABLED_Num2GMTime) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {// Broken on Windows only. - {"Num2GMTime(1, \"HH:MM:SS\")", "00:00:00"}, - // Below broken on other platforms. - {"Num2GMTime(65593001, \"HH:MM:SS Z\")", "18:13:13 GMT"}, - {"Num2GMTime(43993001, TimeFmt(4, \"de_DE\"), \"de_DE\")", - "12.13 Uhr GMT"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -// TODO(dsinclair): Broken on Mac ... -TEST_F(FM2JSContextEmbedderTest, DISABLED_Num2Time) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"Num2Time(1, \"HH:MM:SS\")", "00:00:00"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -// TEST_F(FM2JSContextEmbedderTest, DISABLED_Time) { -// ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); -// TODO(dsinclair): Make compatible with windows. -// struct timeval tp; -// gettimeofday(&tp, nullptr); - -// EXPECT_TRUE(Execute("Time()")); - -// CFXJSE_Value* value = GetValue(); -// EXPECT_TRUE(value->IsInteger()); -// EXPECT_EQ(tp.tv_sec * 1000L + tp.tv_usec / 1000, value->ToInteger()) -// << "Program: Time()"; -// } - -TEST_F(FM2JSContextEmbedderTest, Time2Num) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - int result; - } tests[] = { - // {"Time2Num(\"00:00:00 GMT\", \"HH:MM:SS Z\")", 1}, - {"Time2Num(\"13:13:13 GMT\", \"HH:MM:SS Z\", \"fr_FR\")", 47593001}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()); - EXPECT_EQ(tests[i].result, value->ToInteger()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, TimeFmt) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = { - // {"TimeFmt(1)", "h::MM A"}, - {"TimeFmt(2, \"fr_CA\")", "HH:MM:SS"}, - {"TimeFmt(3, \"fr_FR\")", "HH:MM:SS Z"}, - // {"TimeFmt(4, \"de_DE\")", "H.MM' Uhr 'Z"} - }; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, DISABLED_Apr) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - float result; - } tests[] = {{"Apr(35000, 269.50, 360)", 0.08515404566f}, - {"Apr(210000 * 0.75, 850 + 110, 25 * 26)", 0.07161332404f}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsNumber()); - EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, CTerm) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - float result; - } tests[] = { - // {"CTerm(0.02, 1000, 100)", 116.2767474515f}, - {"CTerm(0.10, 500000, 12000)", 39.13224648502f}, - // {"CTerm(0.0275 + 0.0025, 1000000, 55000 * 0.10)", 176.02226044975f} - }; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsNumber()); - EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, FV) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - float result; - } tests[] = {{"FV(400, 0.10 / 12, 30 * 12)", 904195.16991842445f}, - {"FV(1000, 0.075 / 4, 10 * 4)", 58791.96145535981f}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsNumber()); - EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, IPmt) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - float result; - } tests[] = {{"IPmt(30000, 0.085, 295.50, 7, 3)", 624.8839283142f}, - {"IPmt(160000, 0.0475, 980, 24, 12)", 7103.80833569485f}, - {"IPmt(15000, 0.065, 65.50, 15, 1)", 0.0f}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsNumber()); - EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, DISABLED_NPV) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - float result; - } tests[] = {{"NPV(0.065, 5000)", 4694.83568075117f}, - {"NPV(0.10, 500, 1500, 4000, 10000)", 11529.60863329007f}, - {"NPV(0.0275 / 12, 50, 60, 40, 100, 25)", 273.14193838457f}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsNumber()) << "Program: " << tests[i].program; - EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, Pmt) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - float result; - } tests[] = {// {"Pmt(150000, 0.0475 / 12, 25 * 12)", 855.17604207164f}, - {"Pmt(25000, 0.085, 12)", 3403.82145169876f}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsNumber()); - EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, PPmt) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - float result; - } tests[] = { - {"PPmt(30000, 0.085, 295.50, 7, 3)", 261.6160716858f}, - {"PPmt(160000, 0.0475, 980, 24, 12)", 4656.19166430515f}, - // {"PPmt(15000, 0.065, 65.50, 15, 1)", 0.0f} - }; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsNumber()); - EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, PV) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - float result; - } tests[] = { - {"PV(400, 0.10 / 12, 30 * 12)", 45580.32799074439f}, - // {"PV(1000, 0.075 / 4, 10 * 4)", 58791.96145535981f} - }; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsNumber()); - EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, DISABLED_Rate) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - float result; - } tests[] = {{"Rate(12000, 8000, 5)", 0.0844717712f}, - {"Rate(10000, 0.25 * 5000, 4 * 12)", 0.04427378243f}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsNumber()); - EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, Term) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - float result; - } tests[] = {// {"Term(475, .05, 1500)", 3.00477517728f}, - {"Term(2500, 0.0275 + 0.0025, 5000)", 1.97128786369f}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsNumber()); - EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, Choose) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = { - {"Choose(3, \"Taxes\", \"Price\", \"Person\", \"Teller\")", "Person"}, - {"Choose(2, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)", "9"}, - {"Choose(20/3, \"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\")", - "F"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, Exists) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - EXPECT_TRUE(Execute("Exists(\"hello world\")")); - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()); - EXPECT_FALSE(value->ToBoolean()); -} - -TEST_F(FM2JSContextEmbedderTest, HasValue) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - bool result; - } tests[] = {{"HasValue(2)", true}, {"HasValue(\" \")", false}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()) << "Program: " << tests[i].program; - EXPECT_EQ(tests[i].result, value->ToBoolean()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, Oneof) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - bool result; - } tests[] = { - {"Oneof(3, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)", true}, - {"Oneof(\"John\", \"Bill\", \"Gary\", \"Joan\", \"John\", \"Lisa\")", - true}, - {"Oneof(3, 1, 25)", false}, - {"Oneof(3, 3, null)", true}, - {"Oneof(3, null, null)", false}, - }; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()) << "Program: " << tests[i].program; - EXPECT_EQ(tests[i].result, value->ToBoolean()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, Within) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - bool result; - } tests[] = {{"Within(\"C\", \"A\", \"D\")", true}, - {"Within(1.5, 0, 2)", true}, - {"Within(-1, 0, 2)", false}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()) << "Program: " << tests[i].program; - EXPECT_EQ(tests[i].result, value->ToBoolean()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, DISABLED_Eval) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - int result; - } tests[] = {{"eval(\"10*3+5*4\")", 50}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()); - EXPECT_EQ(tests[i].result, value->ToInteger()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, DISABLED_Null) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"Null()", "null"}, - {"Concat(\"ABC\", Null(), \"DEF\")", "ABCDEF"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } - - EXPECT_TRUE(Execute("Null() + 5")); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()); - EXPECT_EQ(5, value->ToInteger()); -} - -TEST_F(FM2JSContextEmbedderTest, Ref) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"Ref(\"10*3+5*4\")", "10*3+5*4"}, {"Ref(\"hello\")", "hello"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, UnitType) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"UnitType(\"36 in\")", "in"}, - {"UnitType(\"2.54centimeters\")", "cm"}, - {"UnitType(\"picas\")", "pt"}, - {"UnitType(\"2.cm\")", "cm"}, - {"UnitType(\"2.zero cm\")", "in"}, - {"UnitType(\"kilometers\")", "in"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, UnitValue) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - float result; - } tests[] = { - {"UnitValue(\"2in\")", 2.0f}, {"UnitValue(\"2in\", \"cm\")", 5.08f}, - // {"UnitValue(\"6\", \"pt\")", 432f}, - // {"UnitType(\"A\", \"cm\")", 0.0f}, - // {"UnitType(\"5.08cm\", \"kilograms\")", 2.0f} - }; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsNumber()); - EXPECT_FLOAT_EQ(tests[i].result, value->ToFloat()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, At) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - int result; - } tests[] = {{"At(\"ABCDEFGH\", \"AB\")", 1}, - {"At(\"ABCDEFGH\", \"F\")", 6}, - {"At(23412931298471, 29)", 5}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()); - EXPECT_EQ(tests[i].result, value->ToInteger()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, Concat) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"Concat(\"ABC\", \"DEF\")", "ABCDEF"}, - {"Concat(\"Tony\", Space(1), \"Blue\")", "Tony Blue"}, - {"Concat(\"You owe \", WordNum(1154.67, 2), \".\")", - "You owe One Thousand One Hundred Fifty-four Dollars And " - "Sixty-seven Cents."}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, Decode) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = { - {"Decode(\"ÆÁÂÁÂ\", \"html\")", "ÆÁÂÁÂ"}, - // {"Decode(\"~!@#$%%^&*()_+|`{"}[]<>?,./;':\", " - // "\"xml\")", - // "~!@#$%%^&*()_+|`{" - // "}[]<>?,./;':"} - }; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, DISABLED_Encode) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = { - {"Encode(\"\"\"hello, world!\"\"\", \"url\")", - "%%22hello,%%20world!%%22"}, - {"Encode(\"ÁÂÃÄÅÆ\", \"html\")", "ÁÂÃÄÅÆ"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, DISABLED_Format) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"Format(\"MMM D, YYYY\", \"20020901\")", "Sep 1, 2002"}, - {"Format(\"$9,999,999.99\", 1234567.89)", "$1,234,567.89"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, Left) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"Left(\"ABCDEFGH\", 3)", "ABC"}, - {"Left(\"Tony Blue\", 5)", "Tony "}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, Len) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - int result; - } tests[] = { - {"Len(\"ABCDEFGH\")", 8}, {"Len(4)", 1}, {"Len(Str(4.532, 6, 4))", 6}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsInteger()); - EXPECT_EQ(tests[i].result, value->ToInteger()) - << "Program: " << tests[i].program; - } -} - -TEST_F(FM2JSContextEmbedderTest, Lower) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"Lower(\"ABC\")", "abc"}, - {"Lower(\"21 Main St.\")", "21 main st."}, - {"Lower(15)", "15"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, Ltrim) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"Ltrim(\" ABCD\")", "ABCD"}, - {"Ltrim(Rtrim(\" Tony Blue \"))", "Tony Blue"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, DISABLED_Parse) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"Parse(\"MMM D, YYYY\", \"Sep 1, 2002\")", "2002-09-01"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } - - EXPECT_TRUE(Execute("Parse(\"$9,999,999.99\", \"$1,234,567.89\")")); - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsNumber()); - EXPECT_FLOAT_EQ(1234567.89f, value->ToFloat()); -} - -TEST_F(FM2JSContextEmbedderTest, Replace) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"Replace(\"Tony Blue\", \"Tony\", \"Chris\")", "Chris Blue"}, - {"Replace(\"ABCDEFGH\", \"D\")", "ABCEFGH"}, - {"Replace(\"ABCDEFGH\", \"d\")", "ABCDEFGH"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, Right) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"Right(\"ABCDEFGH\", 3)", "FGH"}, - {"Right(\"Tony Blue\", 5)", " Blue"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, Rtrim) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"Rtrim(\"ABCD \")", "ABCD"}, - {"Rtrim(\"Tony Blue \t\")", "Tony Blue"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, Space) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"Space(5)", " "}, - {"Concat(\"Tony\", Space(1), \"Blue\")", "Tony Blue"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, Str) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"Str(2.456)", " 2"}, - {"Str(4.532, 6, 4)", "4.5320"}, - {"Str(234.458, 4)", " 234"}, - {"Str(31.2345, 4, 2)", "****"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, Stuff) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"Stuff(\"TonyBlue\", 5, 0, \" \")", "Tony Blue"}, - {"Stuff(\"ABCDEFGH\", 4, 2)", "ABCFGH"}, - {"Stuff(\"members-list@myweb.com\", 0, 0, \"cc:\")", - "cc:members-list@myweb.com"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, Substr) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"Substr(\"ABCDEFG\", 3, 4)", "CDEF"}, - {"Substr(3214, 2, 1)", "2"}, - {"Substr(\"ABCDEFG\", 5, 0)", ""}, - {"Substr(\"21 Waterloo St.\", 4, 5)", "Water"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, Uuid) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - EXPECT_TRUE(Execute("Uuid()")); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); -} - -TEST_F(FM2JSContextEmbedderTest, Upper) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = {{"Upper(\"abc\")", "ABC"}, - {"Upper(\"21 Main St.\")", "21 MAIN ST."}, - {"Upper(15)", "15"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, WordNum) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - struct { - const char* program; - const char* result; - } tests[] = { - // {"WordNum(123.45)", - // "One Hundred and Twenty-three"}, // This looks like it's wrong in the - // // Formcalc document. - // {"WordNum(123.45, 1)", "One Hundred and Twenty-three Dollars"}, - {"WordNum(1154.67, 2)", - "One Thousand One Hundred Fifty-four Dollars And Sixty-seven Cents"}, - {"WordNum(43, 2)", "Forty-three Dollars And Zero Cents"}}; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_TRUE(Execute(tests[i].program)); - - CFXJSE_Value* value = GetValue(); - EXPECT_TRUE(value->IsString()); - EXPECT_STREQ(tests[i].result, value->ToString().c_str()) - << "Program: " << tests[i].program << " Result: '" - << value->ToString().c_str() << "'"; - } -} - -TEST_F(FM2JSContextEmbedderTest, Get) { - // TODO(dsinclair): Is this supported? -} - -TEST_F(FM2JSContextEmbedderTest, Post) { - // TODO(dsinclair): Is this supported? -} - -TEST_F(FM2JSContextEmbedderTest, Put) { - // TODO(dsinclair): Is this supported? -} - -TEST_F(FM2JSContextEmbedderTest, InvalidFunctions) { - ASSERT_TRUE(OpenDocument("simple_xfa.pdf")); - - const char* const tests[] = { - "F()", "()", "()()()", "Round(2.0)()", - }; - - for (size_t i = 0; i < FX_ArraySize(tests); ++i) { - EXPECT_FALSE(ExecuteSilenceFailure(tests[i])); - } -} diff --git a/xfa/fxfa/parser/cscript_eventpseudomodel.cpp b/xfa/fxfa/parser/cscript_eventpseudomodel.cpp index ecf2a98915..1f20f291e6 100644 --- a/xfa/fxfa/parser/cscript_eventpseudomodel.cpp +++ b/xfa/fxfa/parser/cscript_eventpseudomodel.cpp @@ -7,12 +7,12 @@ #include "xfa/fxfa/parser/cscript_eventpseudomodel.h" #include "fxjs/cfxjse_arguments.h" +#include "fxjs/cfxjse_engine.h" #include "xfa/fxfa/cxfa_eventparam.h" #include "xfa/fxfa/cxfa_ffnotify.h" #include "xfa/fxfa/cxfa_ffwidgethandler.h" #include "xfa/fxfa/parser/cxfa_document.h" #include "xfa/fxfa/parser/cxfa_localemgr.h" -#include "xfa/fxfa/parser/cxfa_scriptcontext.h" #include "xfa/fxfa/parser/xfa_utils.h" namespace { @@ -54,7 +54,7 @@ CScript_EventPseudoModel::~CScript_EventPseudoModel() {} void CScript_EventPseudoModel::Property(CFXJSE_Value* pValue, XFA_Event dwFlag, bool bSetting) { - CXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext(); + CFXJSE_Engine* pScriptContext = m_pDocument->GetScriptContext(); if (!pScriptContext) return; @@ -198,7 +198,7 @@ void CScript_EventPseudoModel::Target(CFXJSE_Value* pValue, Property(pValue, XFA_Event::Target, bSetting); } void CScript_EventPseudoModel::Emit(CFXJSE_Arguments* pArguments) { - CXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext(); + CFXJSE_Engine* pScriptContext = m_pDocument->GetScriptContext(); if (!pScriptContext) { return; } @@ -217,7 +217,7 @@ void CScript_EventPseudoModel::Emit(CFXJSE_Arguments* pArguments) { pWidgetHandler->ProcessEvent(pEventParam->m_pTarget, pEventParam); } void CScript_EventPseudoModel::Reset(CFXJSE_Arguments* pArguments) { - CXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext(); + CFXJSE_Engine* pScriptContext = m_pDocument->GetScriptContext(); if (!pScriptContext) { return; } diff --git a/xfa/fxfa/parser/cscript_hostpseudomodel.cpp b/xfa/fxfa/parser/cscript_hostpseudomodel.cpp index f1eaff8c93..c074e0ed2e 100644 --- a/xfa/fxfa/parser/cscript_hostpseudomodel.cpp +++ b/xfa/fxfa/parser/cscript_hostpseudomodel.cpp @@ -9,12 +9,12 @@ #include <memory> #include "fxjs/cfxjse_arguments.h" +#include "fxjs/cfxjse_engine.h" #include "xfa/fxfa/cxfa_ffnotify.h" #include "xfa/fxfa/parser/cxfa_document.h" #include "xfa/fxfa/parser/cxfa_layoutprocessor.h" #include "xfa/fxfa/parser/cxfa_localemgr.h" #include "xfa/fxfa/parser/cxfa_node.h" -#include "xfa/fxfa/parser/cxfa_scriptcontext.h" #include "xfa/fxfa/parser/xfa_resolvenode_rs.h" #include "xfa/fxfa/parser/xfa_utils.h" @@ -244,7 +244,7 @@ void CScript_HostPseudoModel::OpenList(CFXJSE_Arguments* pArguments) { if (pValue->IsObject()) { pNode = ToNode(pValue.get(), nullptr); } else if (pValue->IsString()) { - CXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext(); + CFXJSE_Engine* pScriptContext = m_pDocument->GetScriptContext(); if (!pScriptContext) return; @@ -364,7 +364,7 @@ void CScript_HostPseudoModel::ResetData(CFXJSE_Arguments* pArguments) { int32_t iExpLength = wsExpression.GetLength(); while (iStart < iExpLength) { iStart = XFA_FilterName(wsExpression.AsStringView(), iStart, wsName); - CXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext(); + CFXJSE_Engine* pScriptContext = m_pDocument->GetScriptContext(); if (!pScriptContext) return; @@ -425,7 +425,7 @@ void CScript_HostPseudoModel::SetFocus(CFXJSE_Arguments* pArguments) { if (pValue->IsObject()) { pNode = ToNode(pValue.get(), nullptr); } else if (pValue->IsString()) { - CXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext(); + CFXJSE_Engine* pScriptContext = m_pDocument->GetScriptContext(); if (!pScriptContext) return; diff --git a/xfa/fxfa/parser/cscript_layoutpseudomodel.cpp b/xfa/fxfa/parser/cscript_layoutpseudomodel.cpp index e35a3b8aa0..3195b8448d 100644 --- a/xfa/fxfa/parser/cscript_layoutpseudomodel.cpp +++ b/xfa/fxfa/parser/cscript_layoutpseudomodel.cpp @@ -9,6 +9,7 @@ #include <set> #include "fxjs/cfxjse_arguments.h" +#include "fxjs/cfxjse_engine.h" #include "third_party/base/stl_util.h" #include "xfa/fxfa/cxfa_ffnotify.h" #include "xfa/fxfa/parser/cxfa_arraynodelist.h" @@ -20,7 +21,6 @@ #include "xfa/fxfa/parser/cxfa_localemgr.h" #include "xfa/fxfa/parser/cxfa_measurement.h" #include "xfa/fxfa/parser/cxfa_node.h" -#include "xfa/fxfa/parser/cxfa_scriptcontext.h" #include "xfa/fxfa/parser/cxfa_traversestrategy_contentlayoutitem.h" #include "xfa/fxfa/parser/xfa_utils.h" diff --git a/xfa/fxfa/parser/cscript_signaturepseudomodel.cpp b/xfa/fxfa/parser/cscript_signaturepseudomodel.cpp index a1136b1ddf..ff98953590 100644 --- a/xfa/fxfa/parser/cscript_signaturepseudomodel.cpp +++ b/xfa/fxfa/parser/cscript_signaturepseudomodel.cpp @@ -7,10 +7,10 @@ #include "xfa/fxfa/parser/cscript_signaturepseudomodel.h" #include "fxjs/cfxjse_arguments.h" +#include "fxjs/cfxjse_engine.h" #include "xfa/fxfa/cxfa_ffnotify.h" #include "xfa/fxfa/parser/cxfa_document.h" #include "xfa/fxfa/parser/cxfa_localemgr.h" -#include "xfa/fxfa/parser/cxfa_scriptcontext.h" #include "xfa/fxfa/parser/xfa_utils.h" CScript_SignaturePseudoModel::CScript_SignaturePseudoModel( diff --git a/xfa/fxfa/parser/cxfa_document.cpp b/xfa/fxfa/parser/cxfa_document.cpp index cd60f47bed..3ab1a988b5 100644 --- a/xfa/fxfa/parser/cxfa_document.cpp +++ b/xfa/fxfa/parser/cxfa_document.cpp @@ -7,6 +7,7 @@ #include "xfa/fxfa/parser/cxfa_document.h" #include "core/fxcrt/fx_extension.h" +#include "fxjs/cfxjse_engine.h" #include "xfa/fxfa/cxfa_ffnotify.h" #include "xfa/fxfa/parser/cscript_datawindow.h" #include "xfa/fxfa/parser/cscript_eventpseudomodel.h" @@ -18,7 +19,6 @@ #include "xfa/fxfa/parser/cxfa_layoutprocessor.h" #include "xfa/fxfa/parser/cxfa_localemgr.h" #include "xfa/fxfa/parser/cxfa_node.h" -#include "xfa/fxfa/parser/cxfa_scriptcontext.h" #include "xfa/fxfa/parser/cxfa_traversestrategy_xfanode.h" #include "xfa/fxfa/parser/xfa_resolvenode_rs.h" #include "xfa/fxfa/parser/xfa_utils.h" @@ -284,15 +284,15 @@ CXFA_LocaleMgr* CXFA_Document::GetLocalMgr() { return m_pLocalMgr.get(); } -CXFA_ScriptContext* CXFA_Document::InitScriptContext(v8::Isolate* pIsolate) { - CXFA_ScriptContext* result = GetScriptContext(); +CFXJSE_Engine* CXFA_Document::InitScriptContext(v8::Isolate* pIsolate) { + CFXJSE_Engine* result = GetScriptContext(); result->Initialize(pIsolate); return result; } -CXFA_ScriptContext* CXFA_Document::GetScriptContext() { +CFXJSE_Engine* CXFA_Document::GetScriptContext() { if (!m_pScriptContext) - m_pScriptContext = pdfium::MakeUnique<CXFA_ScriptContext>(this); + m_pScriptContext = pdfium::MakeUnique<CFXJSE_Engine>(this); return m_pScriptContext.get(); } diff --git a/xfa/fxfa/parser/cxfa_document.h b/xfa/fxfa/parser/cxfa_document.h index 69154b21e8..7b71f7bf56 100644 --- a/xfa/fxfa/parser/cxfa_document.h +++ b/xfa/fxfa/parser/cxfa_document.h @@ -55,14 +55,14 @@ class CXFA_LayoutProcessor; class CXFA_DocumentParser; class CXFA_ContainerLayoutItem; class CXFA_FFNotify; -class CXFA_ScriptContext; +class CFXJSE_Engine; class CXFA_Document { public: explicit CXFA_Document(CXFA_DocumentParser* pParser); ~CXFA_Document(); - CXFA_ScriptContext* InitScriptContext(v8::Isolate* pIsolate); + CFXJSE_Engine* InitScriptContext(v8::Isolate* pIsolate); CXFA_Node* GetRoot() const { return m_pRootNode; } @@ -74,7 +74,7 @@ class CXFA_Document { CXFA_Node* GetNotBindNode(const std::vector<CXFA_Object*>& arrayNodes); CXFA_LayoutProcessor* GetLayoutProcessor(); CXFA_LayoutProcessor* GetDocLayout(); - CXFA_ScriptContext* GetScriptContext(); + CFXJSE_Engine* GetScriptContext(); void SetRoot(CXFA_Node* pNewRoot); @@ -111,7 +111,7 @@ class CXFA_Document { private: CXFA_DocumentParser* m_pParser; CXFA_Node* m_pRootNode; - std::unique_ptr<CXFA_ScriptContext> m_pScriptContext; + std::unique_ptr<CFXJSE_Engine> m_pScriptContext; std::unique_ptr<CXFA_LayoutProcessor> m_pLayoutProcessor; std::unique_ptr<CXFA_LocaleMgr> m_pLocalMgr; std::unique_ptr<CScript_DataWindow> m_pScriptDataWindow; diff --git a/xfa/fxfa/parser/cxfa_layoutpagemgr.cpp b/xfa/fxfa/parser/cxfa_layoutpagemgr.cpp index a9e5449e3f..759d19de79 100644 --- a/xfa/fxfa/parser/cxfa_layoutpagemgr.cpp +++ b/xfa/fxfa/parser/cxfa_layoutpagemgr.cpp @@ -6,6 +6,7 @@ #include "xfa/fxfa/parser/cxfa_layoutpagemgr.h" +#include "fxjs/cfxjse_engine.h" #include "third_party/base/stl_util.h" #include "xfa/fxfa/cxfa_ffnotify.h" #include "xfa/fxfa/parser/cxfa_containerlayoutitem.h" @@ -17,7 +18,6 @@ #include "xfa/fxfa/parser/cxfa_measurement.h" #include "xfa/fxfa/parser/cxfa_node.h" #include "xfa/fxfa/parser/cxfa_object.h" -#include "xfa/fxfa/parser/cxfa_scriptcontext.h" #include "xfa/fxfa/parser/cxfa_traversestrategy_contentareacontainerlayoutitem.h" #include "xfa/fxfa/parser/cxfa_traversestrategy_layoutitem.h" #include "xfa/fxfa/parser/cxfa_traversestrategy_xfacontainernode.h" diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp index 0a95b5f0ee..f9333187a2 100644 --- a/xfa/fxfa/parser/cxfa_node.cpp +++ b/xfa/fxfa/parser/cxfa_node.cpp @@ -19,6 +19,7 @@ #include "core/fxcrt/xml/cfx_xmlelement.h" #include "core/fxcrt/xml/cfx_xmlnode.h" #include "core/fxcrt/xml/cfx_xmltext.h" +#include "fxjs/cfxjse_engine.h" #include "fxjs/cfxjse_value.h" #include "third_party/base/logging.h" #include "third_party/base/ptr_util.h" @@ -32,7 +33,6 @@ #include "xfa/fxfa/parser/cxfa_layoutprocessor.h" #include "xfa/fxfa/parser/cxfa_measurement.h" #include "xfa/fxfa/parser/cxfa_occur.h" -#include "xfa/fxfa/parser/cxfa_scriptcontext.h" #include "xfa/fxfa/parser/cxfa_simple_parser.h" #include "xfa/fxfa/parser/cxfa_traversestrategy_xfacontainernode.h" #include "xfa/fxfa/parser/xfa_basic_data.h" @@ -970,7 +970,7 @@ void CXFA_Node::Script_TreeClass_ResolveNode(CFXJSE_Arguments* pArguments) { } WideString wsExpression = WideString::FromUTF8(pArguments->GetUTF8String(0).AsStringView()); - CXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext(); + CFXJSE_Engine* pScriptContext = m_pDocument->GetScriptContext(); if (!pScriptContext) return; CXFA_Node* refNode = this; @@ -1029,7 +1029,7 @@ void CXFA_Node::Script_Som_ResolveNodeList(CFXJSE_Value* pValue, WideString wsExpression, uint32_t dwFlag, CXFA_Node* refNode) { - CXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext(); + CFXJSE_Engine* pScriptContext = m_pDocument->GetScriptContext(); if (!pScriptContext) return; XFA_RESOLVENODE_RS resoveNodeRS; @@ -1073,7 +1073,7 @@ void CXFA_Node::Script_TreeClass_All(CFXJSE_Value* pValue, void CXFA_Node::Script_TreeClass_Nodes(CFXJSE_Value* pValue, bool bSetting, XFA_ATTRIBUTE eAttribute) { - CXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext(); + CFXJSE_Engine* pScriptContext = m_pDocument->GetScriptContext(); if (!pScriptContext) return; if (bSetting) { @@ -4259,7 +4259,7 @@ bool CXFA_Node::TryContent(WideString& wsContent, } if (pNode) { if (bScriptModify) { - CXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext(); + CFXJSE_Engine* pScriptContext = m_pDocument->GetScriptContext(); if (pScriptContext) { m_pDocument->GetScriptContext()->AddNodesOfRunScript(this); } @@ -4622,7 +4622,7 @@ CXFA_Node* CXFA_Node::GetNextSameClassSibling(XFA_Element eType) const { } int32_t CXFA_Node::GetNodeSameNameIndex() const { - CXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext(); + CFXJSE_Engine* pScriptContext = m_pDocument->GetScriptContext(); if (!pScriptContext) { return -1; } @@ -4630,7 +4630,7 @@ int32_t CXFA_Node::GetNodeSameNameIndex() const { } int32_t CXFA_Node::GetNodeSameClassIndex() const { - CXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext(); + CFXJSE_Engine* pScriptContext = m_pDocument->GetScriptContext(); if (!pScriptContext) { return -1; } @@ -4638,7 +4638,7 @@ int32_t CXFA_Node::GetNodeSameClassIndex() const { } void CXFA_Node::GetSOMExpression(WideString& wsSOMExpression) { - CXFA_ScriptContext* pScriptContext = m_pDocument->GetScriptContext(); + CFXJSE_Engine* pScriptContext = m_pDocument->GetScriptContext(); if (!pScriptContext) { return; } diff --git a/xfa/fxfa/parser/cxfa_nodehelper.cpp b/xfa/fxfa/parser/cxfa_nodehelper.cpp index bc91c66f77..289149998e 100644 --- a/xfa/fxfa/parser/cxfa_nodehelper.cpp +++ b/xfa/fxfa/parser/cxfa_nodehelper.cpp @@ -7,10 +7,10 @@ #include "xfa/fxfa/parser/cxfa_nodehelper.h" #include "core/fxcrt/fx_extension.h" +#include "fxjs/cfxjse_engine.h" #include "xfa/fxfa/parser/cxfa_document.h" #include "xfa/fxfa/parser/cxfa_localemgr.h" #include "xfa/fxfa/parser/cxfa_node.h" -#include "xfa/fxfa/parser/cxfa_scriptcontext.h" #include "xfa/fxfa/parser/xfa_resolvenode_rs.h" #include "xfa/fxfa/parser/xfa_utils.h" @@ -313,11 +313,10 @@ bool CXFA_NodeHelper::CreateNode_ForCondition(WideString& wsCondition) { return false; } -bool CXFA_NodeHelper::ResolveNodes_CreateNode( - WideString wsName, - WideString wsCondition, - bool bLastNode, - CXFA_ScriptContext* pScriptContext) { +bool CXFA_NodeHelper::ResolveNodes_CreateNode(WideString wsName, + WideString wsCondition, + bool bLastNode, + CFXJSE_Engine* pScriptContext) { if (!m_pCreateParent) { return false; } diff --git a/xfa/fxfa/parser/cxfa_nodehelper.h b/xfa/fxfa/parser/cxfa_nodehelper.h index 4c7d7ae10c..6c63a7b4ae 100644 --- a/xfa/fxfa/parser/cxfa_nodehelper.h +++ b/xfa/fxfa/parser/cxfa_nodehelper.h @@ -11,7 +11,7 @@ #include "xfa/fxfa/parser/xfa_resolvenode_rs.h" -class CXFA_ScriptContext; +class CFXJSE_Engine; enum XFA_LOGIC_TYPE { XFA_LOGIC_NoTransparent, @@ -56,7 +56,7 @@ class CXFA_NodeHelper { bool ResolveNodes_CreateNode(WideString wsName, WideString wsCondition, bool bLastNode, - CXFA_ScriptContext* pScriptContext); + CFXJSE_Engine* pScriptContext); bool CreateNode_ForCondition(WideString& wsCondition); void SetCreateNodeType(CXFA_Node* refNode); bool NodeIsProperty(CXFA_Node* refNode); diff --git a/xfa/fxfa/parser/cxfa_nodelist.cpp b/xfa/fxfa/parser/cxfa_nodelist.cpp index cea1f127a6..cbee420870 100644 --- a/xfa/fxfa/parser/cxfa_nodelist.cpp +++ b/xfa/fxfa/parser/cxfa_nodelist.cpp @@ -9,9 +9,9 @@ #include <memory> #include "core/fxcrt/fx_extension.h" +#include "fxjs/cfxjse_engine.h" #include "xfa/fxfa/parser/cxfa_document.h" #include "xfa/fxfa/parser/cxfa_node.h" -#include "xfa/fxfa/parser/cxfa_scriptcontext.h" CXFA_NodeList::CXFA_NodeList(CXFA_Document* pDocument) : CXFA_Object(pDocument, diff --git a/xfa/fxfa/parser/cxfa_resolveprocessor.cpp b/xfa/fxfa/parser/cxfa_resolveprocessor.cpp index 4d0ec516b2..a72ad7b3f6 100644 --- a/xfa/fxfa/parser/cxfa_resolveprocessor.cpp +++ b/xfa/fxfa/parser/cxfa_resolveprocessor.cpp @@ -11,6 +11,7 @@ #include <vector> #include "core/fxcrt/fx_extension.h" +#include "fxjs/cfxjse_engine.h" #include "third_party/base/ptr_util.h" #include "third_party/base/stl_util.h" #include "xfa/fxfa/parser/cxfa_document.h" @@ -18,7 +19,6 @@ #include "xfa/fxfa/parser/cxfa_node.h" #include "xfa/fxfa/parser/cxfa_nodehelper.h" #include "xfa/fxfa/parser/cxfa_object.h" -#include "xfa/fxfa/parser/cxfa_scriptcontext.h" #include "xfa/fxfa/parser/xfa_resolvenode_rs.h" #include "xfa/fxfa/parser/xfa_utils.h" @@ -669,7 +669,7 @@ void CXFA_ResolveProcessor::DoPredicateFilter(int32_t iCurIndex, return; } - CXFA_ScriptContext* pContext = rnd.m_pSC; + CFXJSE_Engine* pContext = rnd.m_pSC; wsExpression = wsCondition.Mid(2, wsCondition.GetLength() - 3); for (int32_t i = iFoundCount - 1; i >= 0; i--) { auto pRetValue = pdfium::MakeUnique<CFXJSE_Value>(rnd.m_pSC->GetRuntime()); @@ -781,7 +781,7 @@ void CXFA_ResolveProcessor::SetIndexDataBind(WideString& wsNextCondition, } } -CXFA_ResolveNodesData::CXFA_ResolveNodesData(CXFA_ScriptContext* pSC) +CXFA_ResolveNodesData::CXFA_ResolveNodesData(CFXJSE_Engine* pSC) : m_pSC(pSC), m_CurObject(nullptr), m_wsName(), diff --git a/xfa/fxfa/parser/cxfa_resolveprocessor.h b/xfa/fxfa/parser/cxfa_resolveprocessor.h index b48ee36476..c12dfdc80d 100644 --- a/xfa/fxfa/parser/cxfa_resolveprocessor.h +++ b/xfa/fxfa/parser/cxfa_resolveprocessor.h @@ -13,14 +13,14 @@ #include "xfa/fxfa/parser/xfa_resolvenode_rs.h" class CXFA_NodeHelper; -class CXFA_ScriptContext; +class CFXJSE_Engine; class CXFA_ResolveNodesData { public: - explicit CXFA_ResolveNodesData(CXFA_ScriptContext* pSC = nullptr); + explicit CXFA_ResolveNodesData(CFXJSE_Engine* pSC = nullptr); ~CXFA_ResolveNodesData(); - CXFA_ScriptContext* m_pSC; + CFXJSE_Engine* m_pSC; CXFA_Object* m_CurObject; WideString m_wsName; XFA_HashCode m_uHashName; diff --git a/xfa/fxfa/parser/cxfa_scriptcontext.cpp b/xfa/fxfa/parser/cxfa_scriptcontext.cpp deleted file mode 100644 index 4e78f46f17..0000000000 --- a/xfa/fxfa/parser/cxfa_scriptcontext.cpp +++ /dev/null @@ -1,779 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include "xfa/fxfa/parser/cxfa_scriptcontext.h" - -#include <utility> - -#include "core/fxcrt/autorestorer.h" -#include "core/fxcrt/cfx_widetextbuf.h" -#include "core/fxcrt/fx_extension.h" -#include "fxjs/cfxjse_arguments.h" -#include "fxjs/cfxjse_class.h" -#include "fxjs/cfxjse_value.h" -#include "third_party/base/ptr_util.h" -#include "third_party/base/stl_util.h" -#include "xfa/fxfa/cxfa_eventparam.h" -#include "xfa/fxfa/cxfa_ffnotify.h" -#include "xfa/fxfa/parser/cxfa_document.h" -#include "xfa/fxfa/parser/cxfa_localemgr.h" -#include "xfa/fxfa/parser/cxfa_node.h" -#include "xfa/fxfa/parser/cxfa_nodehelper.h" -#include "xfa/fxfa/parser/cxfa_nodelist.h" -#include "xfa/fxfa/parser/cxfa_object.h" -#include "xfa/fxfa/parser/cxfa_resolveprocessor.h" -#include "xfa/fxfa/parser/cxfa_thisproxy.h" -#include "xfa/fxfa/parser/xfa_basic_data.h" -#include "xfa/fxfa/parser/xfa_resolvenode_rs.h" -#include "xfa/fxfa/parser/xfa_utils.h" - -namespace { - -const FXJSE_CLASS_DESCRIPTOR GlobalClassDescriptor = { - "Root", // name - nullptr, // constructor - nullptr, // properties - nullptr, // methods - 0, // property count - 0, // method count - CXFA_ScriptContext::GlobalPropTypeGetter, - CXFA_ScriptContext::GlobalPropertyGetter, - CXFA_ScriptContext::GlobalPropertySetter, - nullptr, // property deleter - CXFA_ScriptContext::NormalMethodCall, -}; - -const FXJSE_CLASS_DESCRIPTOR NormalClassDescriptor = { - "XFAObject", // name - nullptr, // constructor - nullptr, // properties - nullptr, // methods - 0, // property count - 0, // method count - CXFA_ScriptContext::NormalPropTypeGetter, - CXFA_ScriptContext::NormalPropertyGetter, - CXFA_ScriptContext::NormalPropertySetter, - nullptr, // property deleter - CXFA_ScriptContext::NormalMethodCall, -}; - -const FXJSE_CLASS_DESCRIPTOR VariablesClassDescriptor = { - "XFAScriptObject", // name - nullptr, // constructor - nullptr, // properties - nullptr, // methods - 0, // property count - 0, // method count - CXFA_ScriptContext::NormalPropTypeGetter, - CXFA_ScriptContext::GlobalPropertyGetter, - CXFA_ScriptContext::GlobalPropertySetter, - nullptr, // property deleter - CXFA_ScriptContext::NormalMethodCall, -}; - -const char kFormCalcRuntime[] = "pfm_rt"; - -CXFA_ThisProxy* ToThisProxy(CFXJSE_Value* pValue, CFXJSE_Class* pClass) { - return static_cast<CXFA_ThisProxy*>(pValue->ToHostObject(pClass)); -} - -const XFA_METHODINFO* GetMethodByName(XFA_Element eElement, - const WideStringView& wsMethodName) { - if (wsMethodName.IsEmpty()) - return nullptr; - - int32_t iElementIndex = static_cast<int32_t>(eElement); - while (iElementIndex >= 0 && iElementIndex < g_iScriptIndexCount) { - const XFA_SCRIPTHIERARCHY* scriptIndex = g_XFAScriptIndex + iElementIndex; - int32_t icount = scriptIndex->wMethodCount; - if (icount == 0) { - iElementIndex = scriptIndex->wParentIndex; - continue; - } - uint32_t uHash = FX_HashCode_GetW(wsMethodName, false); - int32_t iStart = scriptIndex->wMethodStart; - // TODO(dsinclair): Switch to std::lower_bound. - int32_t iEnd = iStart + icount - 1; - do { - int32_t iMid = (iStart + iEnd) / 2; - const XFA_METHODINFO* pInfo = g_SomMethodData + iMid; - if (uHash == pInfo->uHash) - return pInfo; - if (uHash < pInfo->uHash) - iEnd = iMid - 1; - else - iStart = iMid + 1; - } while (iStart <= iEnd); - iElementIndex = scriptIndex->wParentIndex; - } - return nullptr; -} - -} // namespace - -// static. -CXFA_Object* CXFA_ScriptContext::ToObject(CFXJSE_Value* pValue, - CFXJSE_Class* pClass) { - CFXJSE_HostObject* pHostObj = pValue->ToHostObject(pClass); - if (!pHostObj || pHostObj->type() != CFXJSE_HostObject::kXFA) - return nullptr; - return static_cast<CXFA_Object*>(pHostObj); -} - -CXFA_ScriptContext::CXFA_ScriptContext(CXFA_Document* pDocument) - : m_pDocument(pDocument), - m_pIsolate(nullptr), - m_pJsClass(nullptr), - m_eScriptType(XFA_SCRIPTLANGTYPE_Unkown), - m_pScriptNodeArray(nullptr), - m_pThisObject(nullptr), - m_dwBuiltInInFlags(0), - m_eRunAtType(XFA_ATTRIBUTEENUM_Client) {} - -CXFA_ScriptContext::~CXFA_ScriptContext() { - for (const auto& pair : m_mapVariableToContext) - delete ToThisProxy(pair.second->GetGlobalObject().get(), nullptr); -} - -void CXFA_ScriptContext::Initialize(v8::Isolate* pIsolate) { - m_pIsolate = pIsolate; - DefineJsContext(); - DefineJsClass(); - m_ResolveProcessor = pdfium::MakeUnique<CXFA_ResolveProcessor>(); -} - -bool CXFA_ScriptContext::RunScript(XFA_SCRIPTLANGTYPE eScriptType, - const WideStringView& wsScript, - CFXJSE_Value* hRetValue, - CXFA_Object* pThisObject) { - ByteString btScript; - AutoRestorer<XFA_SCRIPTLANGTYPE> typeRestorer(&m_eScriptType); - m_eScriptType = eScriptType; - if (eScriptType == XFA_SCRIPTLANGTYPE_Formcalc) { - if (!m_FM2JSContext) { - m_FM2JSContext = pdfium::MakeUnique<CXFA_FM2JSContext>( - m_pIsolate, m_JsContext.get(), m_pDocument.Get()); - } - CFX_WideTextBuf wsJavaScript; - if (!CXFA_FM2JSContext::Translate(wsScript, &wsJavaScript)) { - hRetValue->SetUndefined(); - return false; - } - btScript = FX_UTF8Encode(wsJavaScript.AsStringView()); - } else { - btScript = FX_UTF8Encode(wsScript); - } - AutoRestorer<CXFA_Object*> nodeRestorer(&m_pThisObject); - m_pThisObject = pThisObject; - CFXJSE_Value* pValue = pThisObject ? GetJSValueFromMap(pThisObject) : nullptr; - return m_JsContext->ExecuteScript(btScript.c_str(), hRetValue, pValue); -} - -void CXFA_ScriptContext::GlobalPropertySetter(CFXJSE_Value* pObject, - const ByteStringView& szPropName, - CFXJSE_Value* pValue) { - CXFA_Object* lpOrginalNode = ToObject(pObject, nullptr); - CXFA_Document* pDoc = lpOrginalNode->GetDocument(); - CXFA_ScriptContext* lpScriptContext = pDoc->GetScriptContext(); - CXFA_Object* lpCurNode = lpScriptContext->GetVariablesThis(lpOrginalNode); - WideString wsPropName = WideString::FromUTF8(szPropName); - uint32_t dwFlag = XFA_RESOLVENODE_Parent | XFA_RESOLVENODE_Siblings | - XFA_RESOLVENODE_Children | XFA_RESOLVENODE_Properties | - XFA_RESOLVENODE_Attributes; - CXFA_Node* pRefNode = ToNode(lpScriptContext->GetThisObject()); - if (lpOrginalNode->IsVariablesThis()) - pRefNode = ToNode(lpCurNode); - if (lpScriptContext->QueryNodeByFlag(pRefNode, wsPropName.AsStringView(), - pValue, dwFlag, true)) { - return; - } - if (lpOrginalNode->IsVariablesThis()) { - if (pValue && pValue->IsUndefined()) { - pObject->SetObjectOwnProperty(szPropName, pValue); - return; - } - } - CXFA_FFNotify* pNotify = pDoc->GetNotify(); - if (!pNotify) { - return; - } - pNotify->GetDocEnvironment()->SetGlobalProperty(pNotify->GetHDOC(), - szPropName, pValue); -} -bool CXFA_ScriptContext::QueryNodeByFlag(CXFA_Node* refNode, - const WideStringView& propname, - CFXJSE_Value* pValue, - uint32_t dwFlag, - bool bSetting) { - if (!refNode) - return false; - XFA_RESOLVENODE_RS resolveRs; - if (ResolveObjects(refNode, propname, resolveRs, dwFlag) <= 0) - return false; - if (resolveRs.dwFlags == XFA_RESOVENODE_RSTYPE_Nodes) { - pValue->Assign(GetJSValueFromMap(resolveRs.objects.front())); - return true; - } - if (resolveRs.dwFlags == XFA_RESOVENODE_RSTYPE_Attribute) { - const XFA_SCRIPTATTRIBUTEINFO* lpAttributeInfo = resolveRs.pScriptAttribute; - if (lpAttributeInfo) { - (resolveRs.objects.front()->*(lpAttributeInfo->lpfnCallback))( - pValue, bSetting, (XFA_ATTRIBUTE)lpAttributeInfo->eAttribute); - } - } - return true; -} -void CXFA_ScriptContext::GlobalPropertyGetter(CFXJSE_Value* pObject, - const ByteStringView& szPropName, - CFXJSE_Value* pValue) { - CXFA_Object* pOriginalObject = ToObject(pObject, nullptr); - CXFA_Document* pDoc = pOriginalObject->GetDocument(); - CXFA_ScriptContext* lpScriptContext = pDoc->GetScriptContext(); - CXFA_Object* lpCurNode = lpScriptContext->GetVariablesThis(pOriginalObject); - WideString wsPropName = WideString::FromUTF8(szPropName); - if (lpScriptContext->GetType() == XFA_SCRIPTLANGTYPE_Formcalc) { - if (szPropName == kFormCalcRuntime) { - lpScriptContext->m_FM2JSContext->GlobalPropertyGetter(pValue); - return; - } - XFA_HashCode uHashCode = static_cast<XFA_HashCode>( - FX_HashCode_GetW(wsPropName.AsStringView(), false)); - if (uHashCode != XFA_HASHCODE_Layout) { - CXFA_Object* pObj = - lpScriptContext->GetDocument()->GetXFAObject(uHashCode); - if (pObj) { - pValue->Assign(lpScriptContext->GetJSValueFromMap(pObj)); - return; - } - } - } - uint32_t dwFlag = XFA_RESOLVENODE_Children | XFA_RESOLVENODE_Properties | - XFA_RESOLVENODE_Attributes; - CXFA_Node* pRefNode = ToNode(lpScriptContext->GetThisObject()); - if (pOriginalObject->IsVariablesThis()) { - pRefNode = ToNode(lpCurNode); - } - if (lpScriptContext->QueryNodeByFlag(pRefNode, wsPropName.AsStringView(), - pValue, dwFlag, false)) { - return; - } - dwFlag = XFA_RESOLVENODE_Parent | XFA_RESOLVENODE_Siblings; - if (lpScriptContext->QueryNodeByFlag(pRefNode, wsPropName.AsStringView(), - pValue, dwFlag, false)) { - return; - } - CXFA_Object* pScriptObject = - lpScriptContext->GetVariablesThis(pOriginalObject, true); - if (pScriptObject && - lpScriptContext->QueryVariableValue(pScriptObject->AsNode(), szPropName, - pValue, true)) { - return; - } - CXFA_FFNotify* pNotify = pDoc->GetNotify(); - if (!pNotify) { - return; - } - pNotify->GetDocEnvironment()->GetGlobalProperty(pNotify->GetHDOC(), - szPropName, pValue); -} -void CXFA_ScriptContext::NormalPropertyGetter(CFXJSE_Value* pOriginalValue, - const ByteStringView& szPropName, - CFXJSE_Value* pReturnValue) { - CXFA_Object* pOriginalObject = ToObject(pOriginalValue, nullptr); - if (!pOriginalObject) { - pReturnValue->SetUndefined(); - return; - } - WideString wsPropName = WideString::FromUTF8(szPropName); - CXFA_ScriptContext* lpScriptContext = - pOriginalObject->GetDocument()->GetScriptContext(); - CXFA_Object* pObject = lpScriptContext->GetVariablesThis(pOriginalObject); - if (wsPropName == L"xfa") { - CFXJSE_Value* pValue = lpScriptContext->GetJSValueFromMap( - lpScriptContext->GetDocument()->GetRoot()); - pReturnValue->Assign(pValue); - return; - } - uint32_t dwFlag = XFA_RESOLVENODE_Children | XFA_RESOLVENODE_Properties | - XFA_RESOLVENODE_Attributes; - bool bRet = lpScriptContext->QueryNodeByFlag( - ToNode(pObject), wsPropName.AsStringView(), pReturnValue, dwFlag, false); - if (bRet) { - return; - } - if (pObject == lpScriptContext->GetThisObject() || - (lpScriptContext->GetType() == XFA_SCRIPTLANGTYPE_Javascript && - !lpScriptContext->IsStrictScopeInJavaScript())) { - dwFlag = XFA_RESOLVENODE_Parent | XFA_RESOLVENODE_Siblings; - bRet = lpScriptContext->QueryNodeByFlag(ToNode(pObject), - wsPropName.AsStringView(), - pReturnValue, dwFlag, false); - } - if (bRet) { - return; - } - CXFA_Object* pScriptObject = - lpScriptContext->GetVariablesThis(pOriginalObject, true); - if (pScriptObject) { - bRet = lpScriptContext->QueryVariableValue(ToNode(pScriptObject), - szPropName, pReturnValue, true); - } - if (!bRet) { - pReturnValue->SetUndefined(); - } -} -void CXFA_ScriptContext::NormalPropertySetter(CFXJSE_Value* pOriginalValue, - const ByteStringView& szPropName, - CFXJSE_Value* pReturnValue) { - CXFA_Object* pOriginalObject = ToObject(pOriginalValue, nullptr); - if (!pOriginalObject) - return; - - CXFA_ScriptContext* lpScriptContext = - pOriginalObject->GetDocument()->GetScriptContext(); - CXFA_Object* pObject = lpScriptContext->GetVariablesThis(pOriginalObject); - WideString wsPropName = WideString::FromUTF8(szPropName); - const XFA_SCRIPTATTRIBUTEINFO* lpAttributeInfo = XFA_GetScriptAttributeByName( - pObject->GetElementType(), wsPropName.AsStringView()); - if (lpAttributeInfo) { - (pObject->*(lpAttributeInfo->lpfnCallback))( - pReturnValue, true, (XFA_ATTRIBUTE)lpAttributeInfo->eAttribute); - } else { - if (pObject->IsNode()) { - if (wsPropName[0] == '#') { - wsPropName = wsPropName.Right(wsPropName.GetLength() - 1); - } - CXFA_Node* pNode = ToNode(pObject); - CXFA_Node* pPropOrChild = nullptr; - XFA_Element eType = XFA_GetElementTypeForName(wsPropName.AsStringView()); - if (eType != XFA_Element::Unknown) - pPropOrChild = pNode->GetProperty(0, eType); - else - pPropOrChild = pNode->GetFirstChildByName(wsPropName.AsStringView()); - - if (pPropOrChild) { - WideString wsDefaultName(L"{default}"); - const XFA_SCRIPTATTRIBUTEINFO* lpAttrInfo = - XFA_GetScriptAttributeByName(pPropOrChild->GetElementType(), - wsDefaultName.AsStringView()); - if (lpAttrInfo) { - (pPropOrChild->*(lpAttrInfo->lpfnCallback))( - pReturnValue, true, (XFA_ATTRIBUTE)lpAttrInfo->eAttribute); - return; - } - } - } - CXFA_Object* pScriptObject = - lpScriptContext->GetVariablesThis(pOriginalObject, true); - if (pScriptObject) { - lpScriptContext->QueryVariableValue(ToNode(pScriptObject), szPropName, - pReturnValue, false); - } - } -} -int32_t CXFA_ScriptContext::NormalPropTypeGetter( - CFXJSE_Value* pOriginalValue, - const ByteStringView& szPropName, - bool bQueryIn) { - CXFA_Object* pObject = ToObject(pOriginalValue, nullptr); - if (!pObject) - return FXJSE_ClassPropType_None; - - CXFA_ScriptContext* lpScriptContext = - pObject->GetDocument()->GetScriptContext(); - pObject = lpScriptContext->GetVariablesThis(pObject); - XFA_Element eType = pObject->GetElementType(); - WideString wsPropName = WideString::FromUTF8(szPropName); - if (GetMethodByName(eType, wsPropName.AsStringView())) { - return FXJSE_ClassPropType_Method; - } - if (bQueryIn && - !XFA_GetScriptAttributeByName(eType, wsPropName.AsStringView())) { - return FXJSE_ClassPropType_None; - } - return FXJSE_ClassPropType_Property; -} -int32_t CXFA_ScriptContext::GlobalPropTypeGetter( - CFXJSE_Value* pOriginalValue, - const ByteStringView& szPropName, - bool bQueryIn) { - CXFA_Object* pObject = ToObject(pOriginalValue, nullptr); - if (!pObject) - return FXJSE_ClassPropType_None; - - CXFA_ScriptContext* lpScriptContext = - pObject->GetDocument()->GetScriptContext(); - pObject = lpScriptContext->GetVariablesThis(pObject); - XFA_Element eType = pObject->GetElementType(); - WideString wsPropName = WideString::FromUTF8(szPropName); - if (GetMethodByName(eType, wsPropName.AsStringView())) { - return FXJSE_ClassPropType_Method; - } - return FXJSE_ClassPropType_Property; -} -void CXFA_ScriptContext::NormalMethodCall(CFXJSE_Value* pThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args) { - CXFA_Object* pObject = ToObject(pThis, nullptr); - if (!pObject) - return; - - CXFA_ScriptContext* lpScriptContext = - pObject->GetDocument()->GetScriptContext(); - pObject = lpScriptContext->GetVariablesThis(pObject); - WideString wsFunName = WideString::FromUTF8(szFuncName); - const XFA_METHODINFO* lpMethodInfo = - GetMethodByName(pObject->GetElementType(), wsFunName.AsStringView()); - if (!lpMethodInfo) - return; - - (pObject->*(lpMethodInfo->lpfnCallback))(&args); -} -bool CXFA_ScriptContext::IsStrictScopeInJavaScript() { - return m_pDocument->HasFlag(XFA_DOCFLAG_StrictScoping); -} -XFA_SCRIPTLANGTYPE CXFA_ScriptContext::GetType() { - return m_eScriptType; -} -void CXFA_ScriptContext::DefineJsContext() { - m_JsContext = CFXJSE_Context::Create(m_pIsolate, &GlobalClassDescriptor, - m_pDocument->GetRoot()); - RemoveBuiltInObjs(m_JsContext.get()); - m_JsContext->EnableCompatibleMode(); -} - -CFXJSE_Context* CXFA_ScriptContext::CreateVariablesContext( - CXFA_Node* pScriptNode, - CXFA_Node* pSubform) { - if (!pScriptNode || !pSubform) - return nullptr; - - auto pNewContext = - CFXJSE_Context::Create(m_pIsolate, &VariablesClassDescriptor, - new CXFA_ThisProxy(pSubform, pScriptNode)); - RemoveBuiltInObjs(pNewContext.get()); - pNewContext->EnableCompatibleMode(); - CFXJSE_Context* pResult = pNewContext.get(); - m_mapVariableToContext[pScriptNode] = std::move(pNewContext); - return pResult; -} - -CXFA_Object* CXFA_ScriptContext::GetVariablesThis(CXFA_Object* pObject, - bool bScriptNode) { - if (!pObject->IsVariablesThis()) - return pObject; - - CXFA_ThisProxy* pProxy = static_cast<CXFA_ThisProxy*>(pObject); - return bScriptNode ? pProxy->GetScriptNode() : pProxy->GetThisNode(); -} - -bool CXFA_ScriptContext::RunVariablesScript(CXFA_Node* pScriptNode) { - if (!pScriptNode) - return false; - - if (pScriptNode->GetElementType() != XFA_Element::Script) - return true; - - CXFA_Node* pParent = pScriptNode->GetNodeItem(XFA_NODEITEM_Parent); - if (!pParent || pParent->GetElementType() != XFA_Element::Variables) - return false; - - auto it = m_mapVariableToContext.find(pScriptNode); - if (it != m_mapVariableToContext.end() && it->second) - return true; - - CXFA_Node* pTextNode = pScriptNode->GetNodeItem(XFA_NODEITEM_FirstChild); - if (!pTextNode) - return false; - - WideStringView wsScript; - if (!pTextNode->TryCData(XFA_ATTRIBUTE_Value, wsScript)) - return false; - - ByteString btScript = FX_UTF8Encode(wsScript); - auto hRetValue = pdfium::MakeUnique<CFXJSE_Value>(m_pIsolate); - CXFA_Node* pThisObject = pParent->GetNodeItem(XFA_NODEITEM_Parent); - CFXJSE_Context* pVariablesContext = - CreateVariablesContext(pScriptNode, pThisObject); - AutoRestorer<CXFA_Object*> nodeRestorer(&m_pThisObject); - m_pThisObject = pThisObject; - return pVariablesContext->ExecuteScript(btScript.c_str(), hRetValue.get()); -} - -bool CXFA_ScriptContext::QueryVariableValue(CXFA_Node* pScriptNode, - const ByteStringView& szPropName, - CFXJSE_Value* pValue, - bool bGetter) { - if (!pScriptNode || pScriptNode->GetElementType() != XFA_Element::Script) - return false; - - CXFA_Node* variablesNode = pScriptNode->GetNodeItem(XFA_NODEITEM_Parent); - if (!variablesNode || - variablesNode->GetElementType() != XFA_Element::Variables) - return false; - - auto it = m_mapVariableToContext.find(pScriptNode); - if (it == m_mapVariableToContext.end() || !it->second) - return false; - - CFXJSE_Context* pVariableContext = it->second.get(); - std::unique_ptr<CFXJSE_Value> pObject = pVariableContext->GetGlobalObject(); - auto hVariableValue = pdfium::MakeUnique<CFXJSE_Value>(m_pIsolate); - if (!bGetter) { - pObject->SetObjectOwnProperty(szPropName, pValue); - return true; - } - if (pObject->HasObjectOwnProperty(szPropName, false)) { - pObject->GetObjectProperty(szPropName, hVariableValue.get()); - if (hVariableValue->IsFunction()) - pValue->SetFunctionBind(hVariableValue.get(), pObject.get()); - else if (bGetter) - pValue->Assign(hVariableValue.get()); - else - hVariableValue.get()->Assign(pValue); - return true; - } - return false; -} - -void CXFA_ScriptContext::DefineJsClass() { - m_pJsClass = - CFXJSE_Class::Create(m_JsContext.get(), &NormalClassDescriptor, false); -} - -void CXFA_ScriptContext::RemoveBuiltInObjs(CFXJSE_Context* pContext) const { - static const ByteStringView OBJ_NAME[2] = {"Number", "Date"}; - std::unique_ptr<CFXJSE_Value> pObject = pContext->GetGlobalObject(); - auto hProp = pdfium::MakeUnique<CFXJSE_Value>(m_pIsolate); - for (int i = 0; i < 2; ++i) { - if (pObject->GetObjectProperty(OBJ_NAME[i], hProp.get())) - pObject->DeleteObjectProperty(OBJ_NAME[i]); - } -} -CFXJSE_Class* CXFA_ScriptContext::GetJseNormalClass() { - return m_pJsClass; -} - -int32_t CXFA_ScriptContext::ResolveObjects(CXFA_Object* refObject, - const WideStringView& wsExpression, - XFA_RESOLVENODE_RS& resolveNodeRS, - uint32_t dwStyles, - CXFA_Node* bindNode) { - if (wsExpression.IsEmpty()) - return 0; - - if (m_eScriptType != XFA_SCRIPTLANGTYPE_Formcalc || - (dwStyles & (XFA_RESOLVENODE_Parent | XFA_RESOLVENODE_Siblings))) { - m_upObjectArray.clear(); - } - if (refObject && refObject->IsNode() && - (dwStyles & (XFA_RESOLVENODE_Parent | XFA_RESOLVENODE_Siblings))) { - m_upObjectArray.push_back(refObject->AsNode()); - } - - bool bNextCreate = false; - if (dwStyles & XFA_RESOLVENODE_CreateNode) { - m_ResolveProcessor->GetNodeHelper()->SetCreateNodeType(bindNode); - } - m_ResolveProcessor->GetNodeHelper()->m_pCreateParent = nullptr; - m_ResolveProcessor->GetNodeHelper()->m_iCurAllStart = -1; - - CXFA_ResolveNodesData rndFind; - int32_t nStart = 0; - int32_t nLevel = 0; - int32_t nRet = -1; - rndFind.m_pSC = this; - std::vector<CXFA_Object*> findObjects; - findObjects.push_back(refObject ? refObject : m_pDocument->GetRoot()); - int32_t nNodes = 0; - while (true) { - nNodes = pdfium::CollectionSize<int32_t>(findObjects); - int32_t i = 0; - rndFind.m_dwStyles = dwStyles; - m_ResolveProcessor->SetCurStart(nStart); - nStart = m_ResolveProcessor->GetFilter(wsExpression, nStart, rndFind); - if (nStart < 1) { - if ((dwStyles & XFA_RESOLVENODE_CreateNode) && !bNextCreate) { - CXFA_Node* pDataNode = nullptr; - nStart = m_ResolveProcessor->GetNodeHelper()->m_iCurAllStart; - if (nStart != -1) { - pDataNode = m_pDocument->GetNotBindNode(findObjects); - if (pDataNode) { - findObjects.clear(); - findObjects.push_back(pDataNode); - break; - } - } else { - pDataNode = findObjects.front()->AsNode(); - findObjects.clear(); - findObjects.push_back(pDataNode); - break; - } - dwStyles |= XFA_RESOLVENODE_Bind; - findObjects.clear(); - findObjects.push_back( - m_ResolveProcessor->GetNodeHelper()->m_pAllStartParent); - continue; - } else { - break; - } - } - if (bNextCreate) { - bool bCreate = - m_ResolveProcessor->GetNodeHelper()->ResolveNodes_CreateNode( - rndFind.m_wsName, rndFind.m_wsCondition, - nStart == - pdfium::base::checked_cast<int32_t>(wsExpression.GetLength()), - this); - if (bCreate) { - continue; - } else { - break; - } - } - std::vector<CXFA_Object*> retObjects; - while (i < nNodes) { - bool bDataBind = false; - if (((dwStyles & XFA_RESOLVENODE_Bind) || - (dwStyles & XFA_RESOLVENODE_CreateNode)) && - nNodes > 1) { - CXFA_ResolveNodesData rndBind; - m_ResolveProcessor->GetFilter(wsExpression, nStart, rndBind); - m_ResolveProcessor->SetIndexDataBind(rndBind.m_wsCondition, i, nNodes); - bDataBind = true; - } - rndFind.m_CurObject = findObjects[i++]; - rndFind.m_nLevel = nLevel; - rndFind.m_dwFlag = XFA_RESOVENODE_RSTYPE_Nodes; - nRet = m_ResolveProcessor->Resolve(rndFind); - if (nRet < 1) { - continue; - } - if (rndFind.m_dwFlag == XFA_RESOVENODE_RSTYPE_Attribute && - rndFind.m_pScriptAttribute && - nStart < - pdfium::base::checked_cast<int32_t>(wsExpression.GetLength())) { - auto pValue = pdfium::MakeUnique<CFXJSE_Value>(m_pIsolate); - (rndFind.m_Objects.front() - ->*(rndFind.m_pScriptAttribute->lpfnCallback))( - pValue.get(), false, - (XFA_ATTRIBUTE)rndFind.m_pScriptAttribute->eAttribute); - rndFind.m_Objects.front() = ToObject(pValue.get(), nullptr); - } - if (!m_upObjectArray.empty()) - m_upObjectArray.pop_back(); - retObjects.insert(retObjects.end(), rndFind.m_Objects.begin(), - rndFind.m_Objects.end()); - rndFind.m_Objects.clear(); - if (bDataBind) - break; - } - findObjects.clear(); - nNodes = pdfium::CollectionSize<int32_t>(retObjects); - if (nNodes < 1) { - if (dwStyles & XFA_RESOLVENODE_CreateNode) { - bNextCreate = true; - if (!m_ResolveProcessor->GetNodeHelper()->m_pCreateParent) { - m_ResolveProcessor->GetNodeHelper()->m_pCreateParent = - ToNode(rndFind.m_CurObject); - m_ResolveProcessor->GetNodeHelper()->m_iCreateCount = 1; - } - bool bCreate = - m_ResolveProcessor->GetNodeHelper()->ResolveNodes_CreateNode( - rndFind.m_wsName, rndFind.m_wsCondition, - nStart == pdfium::base::checked_cast<int32_t>( - wsExpression.GetLength()), - this); - if (bCreate) { - continue; - } else { - break; - } - } else { - break; - } - } - findObjects = - std::vector<CXFA_Object*>(retObjects.begin(), retObjects.end()); - rndFind.m_Objects.clear(); - if (nLevel == 0) { - dwStyles &= ~(XFA_RESOLVENODE_Parent | XFA_RESOLVENODE_Siblings); - } - nLevel++; - } - if (!bNextCreate) { - resolveNodeRS.dwFlags = rndFind.m_dwFlag; - if (nNodes > 0) { - resolveNodeRS.objects.insert(resolveNodeRS.objects.end(), - findObjects.begin(), findObjects.end()); - } - if (rndFind.m_dwFlag == XFA_RESOVENODE_RSTYPE_Attribute) { - resolveNodeRS.pScriptAttribute = rndFind.m_pScriptAttribute; - return 1; - } - } - if (dwStyles & (XFA_RESOLVENODE_CreateNode | XFA_RESOLVENODE_Bind | - XFA_RESOLVENODE_BindNew)) { - m_ResolveProcessor->SetResultCreateNode(resolveNodeRS, - rndFind.m_wsCondition); - if (!bNextCreate && (dwStyles & XFA_RESOLVENODE_CreateNode)) { - resolveNodeRS.dwFlags = XFA_RESOVENODE_RSTYPE_ExistNodes; - } - return pdfium::CollectionSize<int32_t>(resolveNodeRS.objects); - } - return nNodes; -} - -void CXFA_ScriptContext::AddToCacheList(std::unique_ptr<CXFA_NodeList> pList) { - m_CacheList.push_back(std::move(pList)); -} - -CFXJSE_Value* CXFA_ScriptContext::GetJSValueFromMap(CXFA_Object* pObject) { - if (!pObject) - return nullptr; - if (pObject->IsNode()) - RunVariablesScript(pObject->AsNode()); - - auto iter = m_mapObjectToValue.find(pObject); - if (iter != m_mapObjectToValue.end()) - return iter->second.get(); - - auto jsValue = pdfium::MakeUnique<CFXJSE_Value>(m_pIsolate); - jsValue->SetObject(pObject, m_pJsClass); - CFXJSE_Value* pValue = jsValue.get(); - m_mapObjectToValue.insert(std::make_pair(pObject, std::move(jsValue))); - return pValue; -} -int32_t CXFA_ScriptContext::GetIndexByName(CXFA_Node* refNode) { - CXFA_NodeHelper* lpNodeHelper = m_ResolveProcessor->GetNodeHelper(); - return lpNodeHelper->GetIndex(refNode, XFA_LOGIC_Transparent, - lpNodeHelper->NodeIsProperty(refNode), false); -} -int32_t CXFA_ScriptContext::GetIndexByClassName(CXFA_Node* refNode) { - CXFA_NodeHelper* lpNodeHelper = m_ResolveProcessor->GetNodeHelper(); - return lpNodeHelper->GetIndex(refNode, XFA_LOGIC_Transparent, - lpNodeHelper->NodeIsProperty(refNode), true); -} -void CXFA_ScriptContext::GetSomExpression(CXFA_Node* refNode, - WideString& wsExpression) { - CXFA_NodeHelper* lpNodeHelper = m_ResolveProcessor->GetNodeHelper(); - lpNodeHelper->GetNameExpression(refNode, wsExpression, true, - XFA_LOGIC_Transparent); -} -void CXFA_ScriptContext::SetNodesOfRunScript(std::vector<CXFA_Node*>* pArray) { - m_pScriptNodeArray = pArray; -} - -void CXFA_ScriptContext::AddNodesOfRunScript( - const std::vector<CXFA_Node*>& nodes) { - if (m_pScriptNodeArray && !nodes.empty()) - *m_pScriptNodeArray = nodes; -} - -void CXFA_ScriptContext::AddNodesOfRunScript(CXFA_Node* pNode) { - if (m_pScriptNodeArray && !pdfium::ContainsValue(*m_pScriptNodeArray, pNode)) - m_pScriptNodeArray->push_back(pNode); -} diff --git a/xfa/fxfa/parser/cxfa_scriptcontext.h b/xfa/fxfa/parser/cxfa_scriptcontext.h deleted file mode 100644 index 75d30278bd..0000000000 --- a/xfa/fxfa/parser/cxfa_scriptcontext.h +++ /dev/null @@ -1,126 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef XFA_FXFA_PARSER_CXFA_SCRIPTCONTEXT_H_ -#define XFA_FXFA_PARSER_CXFA_SCRIPTCONTEXT_H_ - -#include <map> -#include <memory> -#include <vector> - -#include "fxjs/cfxjse_arguments.h" -#include "xfa/fxfa/cxfa_eventparam.h" -#include "xfa/fxfa/fm2js/cxfa_fm2jscontext.h" -#include "xfa/fxfa/parser/cxfa_document.h" -#include "xfa/fxfa/parser/xfa_resolvenode_rs.h" - -#define XFA_RESOLVENODE_TagName 0x0002 - -class CXFA_ResolveProcessor; - -class CXFA_ScriptContext { - public: - explicit CXFA_ScriptContext(CXFA_Document* pDocument); - ~CXFA_ScriptContext(); - - void Initialize(v8::Isolate* pIsolate); - void SetEventParam(CXFA_EventParam param) { m_eventParam = param; } - CXFA_EventParam* GetEventParam() { return &m_eventParam; } - bool RunScript(XFA_SCRIPTLANGTYPE eScriptType, - const WideStringView& wsScript, - CFXJSE_Value* pRetValue, - CXFA_Object* pThisObject); - - int32_t ResolveObjects(CXFA_Object* refObject, - const WideStringView& wsExpression, - XFA_RESOLVENODE_RS& resolveNodeRS, - uint32_t dwStyles = XFA_RESOLVENODE_Children, - CXFA_Node* bindNode = nullptr); - CFXJSE_Value* GetJSValueFromMap(CXFA_Object* pObject); - void AddToCacheList(std::unique_ptr<CXFA_NodeList> pList); - CXFA_Object* GetThisObject() const { return m_pThisObject; } - v8::Isolate* GetRuntime() const { return m_pIsolate; } - - int32_t GetIndexByName(CXFA_Node* refNode); - int32_t GetIndexByClassName(CXFA_Node* refNode); - void GetSomExpression(CXFA_Node* refNode, WideString& wsExpression); - - void SetNodesOfRunScript(std::vector<CXFA_Node*>* pArray); - void AddNodesOfRunScript(const std::vector<CXFA_Node*>& nodes); - void AddNodesOfRunScript(CXFA_Node* pNode); - CFXJSE_Class* GetJseNormalClass(); - - void SetRunAtType(XFA_ATTRIBUTEENUM eRunAt) { m_eRunAtType = eRunAt; } - bool IsRunAtClient() { return m_eRunAtType != XFA_ATTRIBUTEENUM_Server; } - bool QueryNodeByFlag(CXFA_Node* refNode, - const WideStringView& propname, - CFXJSE_Value* pValue, - uint32_t dwFlag, - bool bSetting); - bool QueryVariableValue(CXFA_Node* pScriptNode, - const ByteStringView& szPropName, - CFXJSE_Value* pValue, - bool bGetter); - bool QueryBuiltinValue(const ByteStringView& szPropName, - CFXJSE_Value* pValue); - static void GlobalPropertyGetter(CFXJSE_Value* pObject, - const ByteStringView& szPropName, - CFXJSE_Value* pValue); - static void GlobalPropertySetter(CFXJSE_Value* pObject, - const ByteStringView& szPropName, - CFXJSE_Value* pValue); - static void NormalPropertyGetter(CFXJSE_Value* pObject, - const ByteStringView& szPropName, - CFXJSE_Value* pValue); - static void NormalPropertySetter(CFXJSE_Value* pObject, - const ByteStringView& szPropName, - CFXJSE_Value* pValue); - static void NormalMethodCall(CFXJSE_Value* hThis, - const ByteStringView& szFuncName, - CFXJSE_Arguments& args); - static int32_t NormalPropTypeGetter(CFXJSE_Value* pObject, - const ByteStringView& szPropName, - bool bQueryIn); - static int32_t GlobalPropTypeGetter(CFXJSE_Value* pObject, - const ByteStringView& szPropName, - bool bQueryIn); - bool RunVariablesScript(CXFA_Node* pScriptNode); - CXFA_Object* GetVariablesThis(CXFA_Object* pObject, bool bScriptNode = false); - bool IsStrictScopeInJavaScript(); - XFA_SCRIPTLANGTYPE GetType(); - std::vector<CXFA_Node*>* GetUpObjectArray() { return &m_upObjectArray; } - CXFA_Document* GetDocument() const { return m_pDocument.Get(); } - - static CXFA_Object* ToObject(CFXJSE_Value* pValue, CFXJSE_Class* pClass); - - private: - void DefineJsContext(); - CFXJSE_Context* CreateVariablesContext(CXFA_Node* pScriptNode, - CXFA_Node* pSubform); - void DefineJsClass(); - void RemoveBuiltInObjs(CFXJSE_Context* pContext) const; - - UnownedPtr<CXFA_Document> const m_pDocument; - std::unique_ptr<CFXJSE_Context> m_JsContext; - v8::Isolate* m_pIsolate; - CFXJSE_Class* m_pJsClass; - XFA_SCRIPTLANGTYPE m_eScriptType; - std::map<CXFA_Object*, std::unique_ptr<CFXJSE_Value>> m_mapObjectToValue; - std::map<CXFA_Object*, std::unique_ptr<CFXJSE_Context>> - m_mapVariableToContext; - CXFA_EventParam m_eventParam; - std::vector<CXFA_Node*> m_upObjectArray; - // CacheList holds the NodeList items so we can clean them up when we're done. - std::vector<std::unique_ptr<CXFA_NodeList>> m_CacheList; - std::vector<CXFA_Node*>* m_pScriptNodeArray; - std::unique_ptr<CXFA_ResolveProcessor> m_ResolveProcessor; - std::unique_ptr<CXFA_FM2JSContext> m_FM2JSContext; - CXFA_Object* m_pThisObject; - uint32_t m_dwBuiltInInFlags; - XFA_ATTRIBUTEENUM m_eRunAtType; -}; - -#endif // XFA_FXFA_PARSER_CXFA_SCRIPTCONTEXT_H_ diff --git a/xfa/fxfa/parser/cxfa_valuearray.cpp b/xfa/fxfa/parser/cxfa_valuearray.cpp index eded624c36..83ffe6b275 100644 --- a/xfa/fxfa/parser/cxfa_valuearray.cpp +++ b/xfa/fxfa/parser/cxfa_valuearray.cpp @@ -8,7 +8,7 @@ #include <algorithm> -#include "xfa/fxfa/parser/cxfa_scriptcontext.h" +#include "fxjs/cfxjse_engine.h" CXFA_ValueArray::CXFA_ValueArray(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {} @@ -19,7 +19,7 @@ std::vector<CXFA_Object*> CXFA_ValueArray::GetAttributeObject() { std::vector<CXFA_Object*> result(m_Values.size()); std::transform(m_Values.begin(), m_Values.end(), result.begin(), [](const std::unique_ptr<CFXJSE_Value>& value) { - return CXFA_ScriptContext::ToObject(value.get(), nullptr); + return CFXJSE_Engine::ToObject(value.get(), nullptr); }); return result; } diff --git a/xfa/fxfa/parser/xfa_document_datamerger_imp.cpp b/xfa/fxfa/parser/xfa_document_datamerger_imp.cpp index 373412ebf9..12a1ba77c4 100644 --- a/xfa/fxfa/parser/xfa_document_datamerger_imp.cpp +++ b/xfa/fxfa/parser/xfa_document_datamerger_imp.cpp @@ -12,6 +12,7 @@ #include "core/fxcrt/fx_extension.h" #include "core/fxcrt/xml/cfx_xmlelement.h" #include "core/fxcrt/xml/cfx_xmlnode.h" +#include "fxjs/cfxjse_engine.h" #include "third_party/base/logging.h" #include "third_party/base/stl_util.h" #include "xfa/fxfa/parser/cxfa_document.h" @@ -19,7 +20,6 @@ #include "xfa/fxfa/parser/cxfa_localemgr.h" #include "xfa/fxfa/parser/cxfa_node.h" #include "xfa/fxfa/parser/cxfa_occur.h" -#include "xfa/fxfa/parser/cxfa_scriptcontext.h" #include "xfa/fxfa/parser/cxfa_traversestrategy_xfacontainernode.h" #include "xfa/fxfa/parser/cxfa_traversestrategy_xfanode.h" #include "xfa/fxfa/parser/xfa_resolvenode_rs.h" |