From 221f0b3e09b9d77b1873a52fff23c7c39df251bb Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Mon, 4 Jun 2018 22:11:27 +0000 Subject: Lowercase JS_Define.{h,cpp} and CJS_Define.h Consistency with file naming conventions. No functional change. Change-Id: I596c4be5bbf0510950c44a7d9d80f59537739c3b Reviewed-on: https://pdfium-review.googlesource.com/33593 Commit-Queue: Tom Sepez Reviewed-by: Lei Zhang Reviewed-by: Tom Sepez --- BUILD.gn | 6 +- fxjs/CJX_Define.h | 37 ----- fxjs/JS_Define.cpp | 312 ------------------------------------ fxjs/JS_Define.h | 162 ------------------- fxjs/cjs_annot.cpp | 2 +- fxjs/cjs_annot.h | 2 +- fxjs/cjs_app.h | 2 +- fxjs/cjs_border.h | 2 +- fxjs/cjs_color.cpp | 2 +- fxjs/cjs_color.h | 2 +- fxjs/cjs_console.cpp | 2 +- fxjs/cjs_console.h | 2 +- fxjs/cjs_display.h | 2 +- fxjs/cjs_document.h | 2 +- fxjs/cjs_event.cpp | 2 +- fxjs/cjs_event.h | 2 +- fxjs/cjs_event_context.cpp | 2 +- fxjs/cjs_eventhandler.cpp | 2 +- fxjs/cjs_field.h | 2 +- fxjs/cjs_font.h | 2 +- fxjs/cjs_global.cpp | 2 +- fxjs/cjs_global.h | 2 +- fxjs/cjs_globalarrays.h | 2 +- fxjs/cjs_globalconsts.h | 2 +- fxjs/cjs_highlight.h | 2 +- fxjs/cjs_icon.h | 2 +- fxjs/cjs_position.h | 2 +- fxjs/cjs_printparamsobj.h | 2 +- fxjs/cjs_publicmethods.cpp | 2 +- fxjs/cjs_publicmethods.h | 2 +- fxjs/cjs_report.cpp | 2 +- fxjs/cjs_report.h | 2 +- fxjs/cjs_runtime.cpp | 2 +- fxjs/cjs_scalehow.h | 2 +- fxjs/cjs_scalewhen.h | 2 +- fxjs/cjs_style.h | 2 +- fxjs/cjs_timerobj.h | 2 +- fxjs/cjs_util.cpp | 2 +- fxjs/cjs_util.h | 2 +- fxjs/cjs_zoomtype.h | 2 +- fxjs/cjx_define.h | 37 +++++ fxjs/js_define.cpp | 312 ++++++++++++++++++++++++++++++++++++ fxjs/js_define.h | 162 +++++++++++++++++++ fxjs/xfa/cjx_container.h | 2 +- fxjs/xfa/cjx_content.h | 2 +- fxjs/xfa/cjx_datawindow.h | 2 +- fxjs/xfa/cjx_delta.h | 2 +- fxjs/xfa/cjx_desc.h | 2 +- fxjs/xfa/cjx_eventpseudomodel.h | 2 +- fxjs/xfa/cjx_exclgroup.h | 2 +- fxjs/xfa/cjx_field.h | 2 +- fxjs/xfa/cjx_form.h | 2 +- fxjs/xfa/cjx_hostpseudomodel.h | 2 +- fxjs/xfa/cjx_instancemanager.h | 2 +- fxjs/xfa/cjx_layoutpseudomodel.h | 2 +- fxjs/xfa/cjx_list.h | 2 +- fxjs/xfa/cjx_logpseudomodel.h | 2 +- fxjs/xfa/cjx_manifest.h | 2 +- fxjs/xfa/cjx_model.h | 2 +- fxjs/xfa/cjx_node.h | 2 +- fxjs/xfa/cjx_object.h | 2 +- fxjs/xfa/cjx_packet.h | 2 +- fxjs/xfa/cjx_signaturepseudomodel.h | 2 +- fxjs/xfa/cjx_source.h | 2 +- fxjs/xfa/cjx_subform.h | 2 +- fxjs/xfa/cjx_template.h | 2 +- fxjs/xfa/cjx_textnode.h | 2 +- fxjs/xfa/cjx_tree.h | 2 +- fxjs/xfa/cjx_treelist.h | 2 +- fxjs/xfa/cjx_wsdlconnection.h | 2 +- 70 files changed, 577 insertions(+), 577 deletions(-) delete mode 100644 fxjs/CJX_Define.h delete mode 100644 fxjs/JS_Define.cpp delete mode 100644 fxjs/JS_Define.h create mode 100644 fxjs/cjx_define.h create mode 100644 fxjs/js_define.cpp create mode 100644 fxjs/js_define.h diff --git a/BUILD.gn b/BUILD.gn index e546543080..d9db5a7877 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1205,9 +1205,6 @@ jumbo_static_library("fxjs") { if (pdf_enable_v8) { sources += [ - "fxjs/CJX_Define.h", - "fxjs/JS_Define.cpp", - "fxjs/JS_Define.h", "fxjs/cfx_v8.cpp", "fxjs/cfx_v8.h", "fxjs/cfxjs_engine.cpp", @@ -1280,8 +1277,11 @@ jumbo_static_library("fxjs") { "fxjs/cjs_util.h", "fxjs/cjs_zoomtype.cpp", "fxjs/cjs_zoomtype.h", + "fxjs/cjx_define.h", "fxjs/global_timer.cpp", "fxjs/global_timer.h", + "fxjs/js_define.cpp", + "fxjs/js_define.h", "fxjs/js_resources.cpp", "fxjs/js_resources.h", ] diff --git a/fxjs/CJX_Define.h b/fxjs/CJX_Define.h deleted file mode 100644 index bff624fe00..0000000000 --- a/fxjs/CJX_Define.h +++ /dev/null @@ -1,37 +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. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef FXJS_CJX_DEFINE_H_ -#define FXJS_CJX_DEFINE_H_ - -#include - -#include "fxjs/cfx_v8.h" -#include "fxjs/cjs_return.h" - -template >& params)> -CJS_Return JSMethod(C* node, - CFX_V8* runtime, - const std::vector>& params) { - return (node->*M)(runtime, params); -} - -#define JS_METHOD(method_name, class_name) \ - static CJS_Return method_name##_static( \ - CJX_Object* node, CFX_V8* runtime, \ - const std::vector>& params) { \ - return JSMethod( \ - static_cast(node), runtime, params); \ - } \ - CJS_Return method_name(CFX_V8* runtime, \ - const std::vector>& params) - -#define JS_PROP(prop_name) \ - void prop_name(CFXJSE_Value* pValue, bool bSetting, XFA_Attribute eAttribute) - -#endif // FXJS_CJX_DEFINE_H_ diff --git a/fxjs/JS_Define.cpp b/fxjs/JS_Define.cpp deleted file mode 100644 index d5b1cefeda..0000000000 --- a/fxjs/JS_Define.cpp +++ /dev/null @@ -1,312 +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 "fxjs/JS_Define.h" - -#include - -#include -#include -#include -#include - -#include "fxjs/cjs_document.h" -#include "fxjs/cjs_object.h" - -namespace { - -double GetLocalTZA() { - if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) - return 0; - time_t t = 0; - time(&t); - localtime(&t); -#if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ - // In gcc 'timezone' is a global variable declared in time.h. In VC++, that - // variable was removed in VC++ 2015, with _get_timezone replacing it. - long timezone = 0; - _get_timezone(&timezone); -#endif // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ - return (double)(-(timezone * 1000)); -} - -int GetDaylightSavingTA(double d) { - if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) - return 0; - time_t t = (time_t)(d / 1000); - struct tm* tmp = localtime(&t); - if (!tmp) - return 0; - if (tmp->tm_isdst > 0) - // One hour. - return (int)60 * 60 * 1000; - return 0; -} - -double Mod(double x, double y) { - double r = fmod(x, y); - if (r < 0) - r += y; - return r; -} - -bool IsLeapYear(int year) { - return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 != 0)); -} - -int DayFromYear(int y) { - return (int)(365 * (y - 1970.0) + floor((y - 1969.0) / 4) - - floor((y - 1901.0) / 100) + floor((y - 1601.0) / 400)); -} - -double TimeFromYear(int y) { - return 86400000.0 * DayFromYear(y); -} - -static const uint16_t daysMonth[12] = {0, 31, 59, 90, 120, 151, - 181, 212, 243, 273, 304, 334}; -static const uint16_t leapDaysMonth[12] = {0, 31, 60, 91, 121, 152, - 182, 213, 244, 274, 305, 335}; - -double TimeFromYearMonth(int y, int m) { - const uint16_t* pMonth = IsLeapYear(y) ? leapDaysMonth : daysMonth; - return TimeFromYear(y) + ((double)pMonth[m]) * 86400000; -} - -int Day(double t) { - return static_cast(floor(t / 86400000.0)); -} - -int YearFromTime(double t) { - // estimate the time. - int y = 1970 + static_cast(t / (365.2425 * 86400000.0)); - if (TimeFromYear(y) <= t) { - while (TimeFromYear(y + 1) <= t) - y++; - } else { - while (TimeFromYear(y) > t) - y--; - } - return y; -} - -int DayWithinYear(double t) { - int year = YearFromTime(t); - int day = Day(t); - return day - DayFromYear(year); -} - -int MonthFromTime(double t) { - int day = DayWithinYear(t); - int year = YearFromTime(t); - if (0 <= day && day < 31) - return 0; - if (31 <= day && day < 59 + IsLeapYear(year)) - return 1; - if ((59 + IsLeapYear(year)) <= day && day < (90 + IsLeapYear(year))) - return 2; - if ((90 + IsLeapYear(year)) <= day && day < (120 + IsLeapYear(year))) - return 3; - if ((120 + IsLeapYear(year)) <= day && day < (151 + IsLeapYear(year))) - return 4; - if ((151 + IsLeapYear(year)) <= day && day < (181 + IsLeapYear(year))) - return 5; - if ((181 + IsLeapYear(year)) <= day && day < (212 + IsLeapYear(year))) - return 6; - if ((212 + IsLeapYear(year)) <= day && day < (243 + IsLeapYear(year))) - return 7; - if ((243 + IsLeapYear(year)) <= day && day < (273 + IsLeapYear(year))) - return 8; - if ((273 + IsLeapYear(year)) <= day && day < (304 + IsLeapYear(year))) - return 9; - if ((304 + IsLeapYear(year)) <= day && day < (334 + IsLeapYear(year))) - return 10; - if ((334 + IsLeapYear(year)) <= day && day < (365 + IsLeapYear(year))) - return 11; - - return -1; -} - -int DateFromTime(double t) { - int day = DayWithinYear(t); - int year = YearFromTime(t); - int leap = IsLeapYear(year); - int month = MonthFromTime(t); - switch (month) { - case 0: - return day + 1; - case 1: - return day - 30; - case 2: - return day - 58 - leap; - case 3: - return day - 89 - leap; - case 4: - return day - 119 - leap; - case 5: - return day - 150 - leap; - case 6: - return day - 180 - leap; - case 7: - return day - 211 - leap; - case 8: - return day - 242 - leap; - case 9: - return day - 272 - leap; - case 10: - return day - 303 - leap; - case 11: - return day - 333 - leap; - default: - return 0; - } -} - -} // namespace - -void JSDestructor(v8::Local obj) { - CFXJS_Engine::SetObjectPrivate(obj, nullptr); -} - -double JS_GetDateTime() { - if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) - return 0; - time_t t = time(nullptr); - struct tm* pTm = localtime(&t); - - int year = pTm->tm_year + 1900; - double t1 = TimeFromYear(year); - - return t1 + pTm->tm_yday * 86400000.0 + pTm->tm_hour * 3600000.0 + - pTm->tm_min * 60000.0 + pTm->tm_sec * 1000.0; -} - -int JS_GetYearFromTime(double dt) { - return YearFromTime(dt); -} - -int JS_GetMonthFromTime(double dt) { - return MonthFromTime(dt); -} - -int JS_GetDayFromTime(double dt) { - return DateFromTime(dt); -} - -int JS_GetHourFromTime(double dt) { - return (int)Mod(floor(dt / (60 * 60 * 1000)), 24); -} - -int JS_GetMinFromTime(double dt) { - return (int)Mod(floor(dt / (60 * 1000)), 60); -} - -int JS_GetSecFromTime(double dt) { - return (int)Mod(floor(dt / 1000), 60); -} - -double JS_LocalTime(double d) { - return d + GetLocalTZA() + GetDaylightSavingTA(d); -} - -double JS_DateParse(const WideString& str) { - v8::Isolate* pIsolate = v8::Isolate::GetCurrent(); - v8::Isolate::Scope isolate_scope(pIsolate); - v8::HandleScope scope(pIsolate); - - v8::Local context = pIsolate->GetCurrentContext(); - - // Use the built-in object method. - v8::Local v = - context->Global() - ->Get(context, v8::String::NewFromUtf8(pIsolate, "Date", - v8::NewStringType::kNormal) - .ToLocalChecked()) - .ToLocalChecked(); - if (v->IsObject()) { - v8::Local o = v->ToObject(context).ToLocalChecked(); - v = o->Get(context, v8::String::NewFromUtf8(pIsolate, "parse", - v8::NewStringType::kNormal) - .ToLocalChecked()) - .ToLocalChecked(); - if (v->IsFunction()) { - v8::Local funC = v8::Local::Cast(v); - const int argc = 1; - v8::Local timeStr = - CJS_Runtime::RuntimeFromIsolateCurrentContext(pIsolate)->NewString( - str.AsStringView()); - v8::Local argv[argc] = {timeStr}; - v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked(); - if (v->IsNumber()) { - double date = v->ToNumber(context).ToLocalChecked()->Value(); - if (!std::isfinite(date)) - return date; - return JS_LocalTime(date); - } - } - } - return 0; -} - -double JS_MakeDay(int nYear, int nMonth, int nDate) { - double y = static_cast(nYear); - double m = static_cast(nMonth); - double dt = static_cast(nDate); - double ym = y + floor(m / 12); - double mn = Mod(m, 12); - double t = TimeFromYearMonth(static_cast(ym), static_cast(mn)); - if (YearFromTime(t) != ym || MonthFromTime(t) != mn || DateFromTime(t) != 1) - return std::nan(""); - - return Day(t) + dt - 1; -} - -double JS_MakeTime(int nHour, int nMin, int nSec, int nMs) { - double h = static_cast(nHour); - double m = static_cast(nMin); - double s = static_cast(nSec); - double milli = static_cast(nMs); - return h * 3600000 + m * 60000 + s * 1000 + milli; -} - -double JS_MakeDate(double day, double time) { - if (!std::isfinite(day) || !std::isfinite(time)) - return std::nan(""); - - return day * 86400000 + time; -} - -std::vector> ExpandKeywordParams( - CJS_Runtime* pRuntime, - const std::vector>& originals, - size_t nKeywords, - ...) { - ASSERT(nKeywords); - - std::vector> result(nKeywords, v8::Local()); - size_t size = std::min(originals.size(), nKeywords); - for (size_t i = 0; i < size; ++i) - result[i] = originals[i]; - - if (originals.size() != 1 || !originals[0]->IsObject() || - originals[0]->IsArray()) { - return result; - } - result[0] = v8::Local(); // Make unknown. - - v8::Local pObj = pRuntime->ToObject(originals[0]); - va_list ap; - va_start(ap, nKeywords); - for (size_t i = 0; i < nKeywords; ++i) { - const wchar_t* property = va_arg(ap, const wchar_t*); - v8::Local v8Value = pRuntime->GetObjectProperty(pObj, property); - if (!v8Value->IsUndefined()) - result[i] = v8Value; - } - va_end(ap); - - return result; -} diff --git a/fxjs/JS_Define.h b/fxjs/JS_Define.h deleted file mode 100644 index 325642d8ce..0000000000 --- a/fxjs/JS_Define.h +++ /dev/null @@ -1,162 +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 FXJS_JS_DEFINE_H_ -#define FXJS_JS_DEFINE_H_ - -#include -#include - -#include "fxjs/cfxjs_engine.h" -#include "fxjs/cjs_object.h" -#include "fxjs/cjs_return.h" -#include "fxjs/js_resources.h" -#include "third_party/base/ptr_util.h" - -double JS_GetDateTime(); -int JS_GetYearFromTime(double dt); -int JS_GetMonthFromTime(double dt); -int JS_GetDayFromTime(double dt); -int JS_GetHourFromTime(double dt); -int JS_GetMinFromTime(double dt); -int JS_GetSecFromTime(double dt); -double JS_LocalTime(double d); -double JS_DateParse(const WideString& str); -double JS_MakeDay(int nYear, int nMonth, int nDay); -double JS_MakeTime(int nHour, int nMin, int nSec, int nMs); -double JS_MakeDate(double day, double time); - -// Some JS methods have the bizarre convention that they may also be called -// with a single argument which is an object containing the actual arguments -// as its properties. The varying arguments to this method are the property -// names as wchar_t string literals corresponding to each positional argument. -// The result will always contain |nKeywords| value, with unspecified ones -// being set to type VT_unknown. -std::vector> ExpandKeywordParams( - CJS_Runtime* pRuntime, - const std::vector>& originals, - size_t nKeywords, - ...); - -// All JS classes have a name, an object defintion ID, and the ability to -// register themselves with FXJS_V8. We never make a BASE class on its own -// because it can't really do anything. - -// Rich JS classes provide constants, methods, properties, and the ability -// to construct native object state. - -template -static void JSConstructor(CFXJS_Engine* pEngine, v8::Local obj) { - auto pObj = pdfium::MakeUnique(obj, static_cast(pEngine)); - pObj->InitInstance(); - pEngine->SetObjectPrivate(obj, std::move(pObj)); -} - -// CJS_Object has vitual dtor, template not required. -void JSDestructor(v8::Local obj); - -template -void JSPropGetter(const char* prop_name_string, - const char* class_name_string, - v8::Local property, - const v8::PropertyCallbackInfo& info) { - CJS_Runtime* pRuntime = - CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); - if (!pRuntime) - return; - - CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder()); - if (!pJSObj) - return; - - C* pObj = static_cast(pJSObj); - CJS_Return result = (pObj->*M)(pRuntime); - if (result.HasError()) { - pRuntime->Error(JSFormatErrorString(class_name_string, prop_name_string, - result.Error())); - return; - } - - if (result.HasReturn()) - info.GetReturnValue().Set(result.Return()); -} - -template )> -void JSPropSetter(const char* prop_name_string, - const char* class_name_string, - v8::Local property, - v8::Local value, - const v8::PropertyCallbackInfo& info) { - CJS_Runtime* pRuntime = - CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); - if (!pRuntime) - return; - - CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder()); - if (!pJSObj) - return; - - C* pObj = static_cast(pJSObj); - CJS_Return result = (pObj->*M)(pRuntime, value); - if (result.HasError()) { - pRuntime->Error(JSFormatErrorString(class_name_string, prop_name_string, - result.Error())); - } -} - -template >&)> -void JSMethod(const char* method_name_string, - const char* class_name_string, - const v8::FunctionCallbackInfo& info) { - CJS_Runtime* pRuntime = - CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); - if (!pRuntime) - return; - - CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder()); - if (!pJSObj) - return; - - std::vector> parameters; - for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) - parameters.push_back(info[i]); - - C* pObj = static_cast(pJSObj); - CJS_Return result = (pObj->*M)(pRuntime, parameters); - if (result.HasError()) { - pRuntime->Error(JSFormatErrorString(class_name_string, method_name_string, - result.Error())); - return; - } - - if (result.HasReturn()) - info.GetReturnValue().Set(result.Return()); -} - -#define JS_STATIC_PROP(err_name, prop_name, class_name) \ - static void get_##prop_name##_static( \ - v8::Local property, \ - const v8::PropertyCallbackInfo& info) { \ - JSPropGetter( \ - #err_name, class_name::kName, property, info); \ - } \ - static void set_##prop_name##_static( \ - v8::Local property, v8::Local value, \ - const v8::PropertyCallbackInfo& info) { \ - JSPropSetter( \ - #err_name, class_name::kName, property, value, info); \ - } - -#define JS_STATIC_METHOD(method_name, class_name) \ - static void method_name##_static( \ - const v8::FunctionCallbackInfo& info) { \ - JSMethod(#method_name, \ - class_name::kName, info); \ - } - -#endif // FXJS_JS_DEFINE_H_ diff --git a/fxjs/cjs_annot.cpp b/fxjs/cjs_annot.cpp index 4cb126dcff..b226621dd6 100644 --- a/fxjs/cjs_annot.cpp +++ b/fxjs/cjs_annot.cpp @@ -6,9 +6,9 @@ #include "fxjs/cjs_annot.h" -#include "fxjs/JS_Define.h" #include "fxjs/cjs_event_context.h" #include "fxjs/cjs_object.h" +#include "fxjs/js_define.h" #include "fxjs/js_resources.h" namespace { diff --git a/fxjs/cjs_annot.h b/fxjs/cjs_annot.h index cdb884f1b8..d313d5656f 100644 --- a/fxjs/cjs_annot.h +++ b/fxjs/cjs_annot.h @@ -8,7 +8,7 @@ #define FXJS_CJS_ANNOT_H_ #include "fpdfsdk/cpdfsdk_baannot.h" -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_Annot : public CJS_Object { public: diff --git a/fxjs/cjs_app.h b/fxjs/cjs_app.h index 3c3a129641..f1701ae883 100644 --- a/fxjs/cjs_app.h +++ b/fxjs/cjs_app.h @@ -11,7 +11,7 @@ #include #include -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_Runtime; class GlobalTimer; diff --git a/fxjs/cjs_border.h b/fxjs/cjs_border.h index 49e93250bd..0d10a1f735 100644 --- a/fxjs/cjs_border.h +++ b/fxjs/cjs_border.h @@ -7,7 +7,7 @@ #ifndef FXJS_CJS_BORDER_H_ #define FXJS_CJS_BORDER_H_ -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_Border : public CJS_Object { public: diff --git a/fxjs/cjs_color.cpp b/fxjs/cjs_color.cpp index 66cb9721b5..e46ddce69d 100644 --- a/fxjs/cjs_color.cpp +++ b/fxjs/cjs_color.cpp @@ -8,11 +8,11 @@ #include -#include "fxjs/JS_Define.h" #include "fxjs/cjs_event_context.h" #include "fxjs/cjs_eventhandler.h" #include "fxjs/cjs_object.h" #include "fxjs/cjs_runtime.h" +#include "fxjs/js_define.h" const JSPropertySpec CJS_Color::PropertySpecs[] = { {"black", get_black_static, set_black_static}, diff --git a/fxjs/cjs_color.h b/fxjs/cjs_color.h index 00b9546daa..581fea0427 100644 --- a/fxjs/cjs_color.h +++ b/fxjs/cjs_color.h @@ -10,7 +10,7 @@ #include #include "fpdfsdk/pwl/cpwl_wnd.h" -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_Color : public CJS_Object { public: diff --git a/fxjs/cjs_console.cpp b/fxjs/cjs_console.cpp index 8ed3472a6c..867913ca6f 100644 --- a/fxjs/cjs_console.cpp +++ b/fxjs/cjs_console.cpp @@ -8,10 +8,10 @@ #include -#include "fxjs/JS_Define.h" #include "fxjs/cjs_event_context.h" #include "fxjs/cjs_eventhandler.h" #include "fxjs/cjs_object.h" +#include "fxjs/js_define.h" const JSMethodSpec CJS_Console::MethodSpecs[] = {{"clear", clear_static}, {"hide", hide_static}, diff --git a/fxjs/cjs_console.h b/fxjs/cjs_console.h index c25bc4bf7c..7ed39bbed9 100644 --- a/fxjs/cjs_console.h +++ b/fxjs/cjs_console.h @@ -9,7 +9,7 @@ #include -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_Console : public CJS_Object { public: diff --git a/fxjs/cjs_display.h b/fxjs/cjs_display.h index e5806b03c2..2336952732 100644 --- a/fxjs/cjs_display.h +++ b/fxjs/cjs_display.h @@ -7,7 +7,7 @@ #ifndef FXJS_CJS_DISPLAY_H_ #define FXJS_CJS_DISPLAY_H_ -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_Display : public CJS_Object { public: diff --git a/fxjs/cjs_document.h b/fxjs/cjs_document.h index 5172df0e26..6727f4e5aa 100644 --- a/fxjs/cjs_document.h +++ b/fxjs/cjs_document.h @@ -11,7 +11,7 @@ #include #include -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_Document; class CPDF_TextObject; diff --git a/fxjs/cjs_event.cpp b/fxjs/cjs_event.cpp index d2537b9fff..ca0671d3a9 100644 --- a/fxjs/cjs_event.cpp +++ b/fxjs/cjs_event.cpp @@ -6,11 +6,11 @@ #include "fxjs/cjs_event.h" -#include "fxjs/JS_Define.h" #include "fxjs/cjs_event_context.h" #include "fxjs/cjs_eventhandler.h" #include "fxjs/cjs_field.h" #include "fxjs/cjs_object.h" +#include "fxjs/js_define.h" const JSPropertySpec CJS_Event::PropertySpecs[] = { {"change", get_change_static, set_change_static}, diff --git a/fxjs/cjs_event.h b/fxjs/cjs_event.h index 434f25989c..39d9cdba87 100644 --- a/fxjs/cjs_event.h +++ b/fxjs/cjs_event.h @@ -7,7 +7,7 @@ #ifndef FXJS_CJS_EVENT_H_ #define FXJS_CJS_EVENT_H_ -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_Event : public CJS_Object { public: diff --git a/fxjs/cjs_event_context.cpp b/fxjs/cjs_event_context.cpp index 0d9ba34db1..50149c4172 100644 --- a/fxjs/cjs_event_context.cpp +++ b/fxjs/cjs_event_context.cpp @@ -7,9 +7,9 @@ #include "fxjs/cjs_event_context.h" #include "core/fxcrt/autorestorer.h" -#include "fxjs/JS_Define.h" #include "fxjs/cjs_eventhandler.h" #include "fxjs/cjs_runtime.h" +#include "fxjs/js_define.h" #include "fxjs/js_resources.h" CJS_EventContext::CJS_EventContext(CJS_Runtime* pRuntime) diff --git a/fxjs/cjs_eventhandler.cpp b/fxjs/cjs_eventhandler.cpp index 5b20ce769b..2f42d4157f 100644 --- a/fxjs/cjs_eventhandler.cpp +++ b/fxjs/cjs_eventhandler.cpp @@ -7,12 +7,12 @@ #include "fxjs/cjs_eventhandler.h" #include "core/fpdfdoc/cpdf_formfield.h" -#include "fxjs/JS_Define.h" #include "fxjs/cjs_document.h" #include "fxjs/cjs_event_context.h" #include "fxjs/cjs_field.h" #include "fxjs/cjs_object.h" #include "fxjs/cjs_runtime.h" +#include "fxjs/js_define.h" CJS_EventHandler::CJS_EventHandler(CJS_EventContext* pContext) : m_pJSEventContext(pContext) {} diff --git a/fxjs/cjs_field.h b/fxjs/cjs_field.h index ba476139cf..0f089d0f13 100644 --- a/fxjs/cjs_field.h +++ b/fxjs/cjs_field.h @@ -10,7 +10,7 @@ #include #include -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CPDF_FormControl; class CPDFSDK_Widget; diff --git a/fxjs/cjs_font.h b/fxjs/cjs_font.h index 8e52778df5..be677d4045 100644 --- a/fxjs/cjs_font.h +++ b/fxjs/cjs_font.h @@ -7,7 +7,7 @@ #ifndef FXJS_CJS_FONT_H_ #define FXJS_CJS_FONT_H_ -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_Font : public CJS_Object { public: diff --git a/fxjs/cjs_global.cpp b/fxjs/cjs_global.cpp index 2a91bdd7d7..9a1f541212 100644 --- a/fxjs/cjs_global.cpp +++ b/fxjs/cjs_global.cpp @@ -12,12 +12,12 @@ #include #include "core/fxcrt/fx_extension.h" -#include "fxjs/JS_Define.h" #include "fxjs/cjs_event_context.h" #include "fxjs/cjs_eventhandler.h" #include "fxjs/cjs_globaldata.h" #include "fxjs/cjs_keyvalue.h" #include "fxjs/cjs_object.h" +#include "fxjs/js_define.h" #include "fxjs/js_resources.h" namespace { diff --git a/fxjs/cjs_global.h b/fxjs/cjs_global.h index 8a2fdab25c..e0d41b801d 100644 --- a/fxjs/cjs_global.h +++ b/fxjs/cjs_global.h @@ -11,8 +11,8 @@ #include #include -#include "fxjs/JS_Define.h" #include "fxjs/cjs_keyvalue.h" +#include "fxjs/js_define.h" class CJS_GlobalData; diff --git a/fxjs/cjs_globalarrays.h b/fxjs/cjs_globalarrays.h index 297b97b614..8e972732db 100644 --- a/fxjs/cjs_globalarrays.h +++ b/fxjs/cjs_globalarrays.h @@ -7,7 +7,7 @@ #ifndef FXJS_CJS_GLOBALARRAYS_H_ #define FXJS_CJS_GLOBALARRAYS_H_ -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_GlobalArrays : public CJS_Object { public: diff --git a/fxjs/cjs_globalconsts.h b/fxjs/cjs_globalconsts.h index 8c6618ee6f..1bef384768 100644 --- a/fxjs/cjs_globalconsts.h +++ b/fxjs/cjs_globalconsts.h @@ -7,7 +7,7 @@ #ifndef FXJS_CJS_GLOBALCONSTS_H_ #define FXJS_CJS_GLOBALCONSTS_H_ -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_GlobalConsts : public CJS_Object { public: diff --git a/fxjs/cjs_highlight.h b/fxjs/cjs_highlight.h index 2815c88c32..a5fc21df58 100644 --- a/fxjs/cjs_highlight.h +++ b/fxjs/cjs_highlight.h @@ -7,7 +7,7 @@ #ifndef FXJS_CJS_HIGHLIGHT_H_ #define FXJS_CJS_HIGHLIGHT_H_ -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_Highlight : public CJS_Object { public: diff --git a/fxjs/cjs_icon.h b/fxjs/cjs_icon.h index 2f3ee3f86f..05fa43015f 100644 --- a/fxjs/cjs_icon.h +++ b/fxjs/cjs_icon.h @@ -7,7 +7,7 @@ #ifndef FXJS_CJS_ICON_H_ #define FXJS_CJS_ICON_H_ -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_Icon : public CJS_Object { public: diff --git a/fxjs/cjs_position.h b/fxjs/cjs_position.h index 0f54c2ba63..3e62bc92bf 100644 --- a/fxjs/cjs_position.h +++ b/fxjs/cjs_position.h @@ -7,7 +7,7 @@ #ifndef FXJS_CJS_POSITION_H_ #define FXJS_CJS_POSITION_H_ -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_Position : public CJS_Object { public: diff --git a/fxjs/cjs_printparamsobj.h b/fxjs/cjs_printparamsobj.h index 90bc9b8999..572d7d8bcd 100644 --- a/fxjs/cjs_printparamsobj.h +++ b/fxjs/cjs_printparamsobj.h @@ -7,7 +7,7 @@ #ifndef FXJS_CJS_PRINTPARAMSOBJ_H_ #define FXJS_CJS_PRINTPARAMSOBJ_H_ -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_PrintParamsObj : public CJS_Object { public: diff --git a/fxjs/cjs_publicmethods.cpp b/fxjs/cjs_publicmethods.cpp index b8d62b173a..d37706c973 100644 --- a/fxjs/cjs_publicmethods.cpp +++ b/fxjs/cjs_publicmethods.cpp @@ -20,7 +20,6 @@ #include "core/fxcrt/fx_extension.h" #include "fpdfsdk/cpdfsdk_formfillenvironment.h" #include "fpdfsdk/cpdfsdk_interform.h" -#include "fxjs/JS_Define.h" #include "fxjs/cjs_color.h" #include "fxjs/cjs_event_context.h" #include "fxjs/cjs_eventhandler.h" @@ -28,6 +27,7 @@ #include "fxjs/cjs_object.h" #include "fxjs/cjs_runtime.h" #include "fxjs/cjs_util.h" +#include "fxjs/js_define.h" #include "fxjs/js_resources.h" // static diff --git a/fxjs/cjs_publicmethods.h b/fxjs/cjs_publicmethods.h index df23eb1830..89603ca79d 100644 --- a/fxjs/cjs_publicmethods.h +++ b/fxjs/cjs_publicmethods.h @@ -10,7 +10,7 @@ #include #include -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_PublicMethods : public CJS_Object { public: diff --git a/fxjs/cjs_report.cpp b/fxjs/cjs_report.cpp index 89737e6542..3a912765f6 100644 --- a/fxjs/cjs_report.cpp +++ b/fxjs/cjs_report.cpp @@ -8,8 +8,8 @@ #include -#include "fxjs/JS_Define.h" #include "fxjs/cjs_object.h" +#include "fxjs/js_define.h" const JSMethodSpec CJS_Report::MethodSpecs[] = { {"save", save_static}, diff --git a/fxjs/cjs_report.h b/fxjs/cjs_report.h index 8de0991411..36a00ce9f5 100644 --- a/fxjs/cjs_report.h +++ b/fxjs/cjs_report.h @@ -9,7 +9,7 @@ #include -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_Report : public CJS_Object { public: diff --git a/fxjs/cjs_runtime.cpp b/fxjs/cjs_runtime.cpp index b47c595350..22dc904c1f 100644 --- a/fxjs/cjs_runtime.cpp +++ b/fxjs/cjs_runtime.cpp @@ -9,7 +9,6 @@ #include #include "fpdfsdk/cpdfsdk_formfillenvironment.h" -#include "fxjs/JS_Define.h" #include "fxjs/cjs_annot.h" #include "fxjs/cjs_app.h" #include "fxjs/cjs_border.h" @@ -39,6 +38,7 @@ #include "fxjs/cjs_timerobj.h" #include "fxjs/cjs_util.h" #include "fxjs/cjs_zoomtype.h" +#include "fxjs/js_define.h" #include "public/fpdf_formfill.h" #include "third_party/base/stl_util.h" diff --git a/fxjs/cjs_scalehow.h b/fxjs/cjs_scalehow.h index bbff20a2c6..a0d16b34b5 100644 --- a/fxjs/cjs_scalehow.h +++ b/fxjs/cjs_scalehow.h @@ -7,7 +7,7 @@ #ifndef FXJS_CJS_SCALEHOW_H_ #define FXJS_CJS_SCALEHOW_H_ -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_ScaleHow : public CJS_Object { public: diff --git a/fxjs/cjs_scalewhen.h b/fxjs/cjs_scalewhen.h index d068a38a14..f30a3bdff2 100644 --- a/fxjs/cjs_scalewhen.h +++ b/fxjs/cjs_scalewhen.h @@ -7,7 +7,7 @@ #ifndef FXJS_CJS_SCALEWHEN_H_ #define FXJS_CJS_SCALEWHEN_H_ -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_ScaleWhen : public CJS_Object { public: diff --git a/fxjs/cjs_style.h b/fxjs/cjs_style.h index ba7eefa5ff..dade59f6f1 100644 --- a/fxjs/cjs_style.h +++ b/fxjs/cjs_style.h @@ -7,7 +7,7 @@ #ifndef FXJS_CJS_STYLE_H_ #define FXJS_CJS_STYLE_H_ -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_Style : public CJS_Object { public: diff --git a/fxjs/cjs_timerobj.h b/fxjs/cjs_timerobj.h index 810b1b3711..d9dc88139d 100644 --- a/fxjs/cjs_timerobj.h +++ b/fxjs/cjs_timerobj.h @@ -7,7 +7,7 @@ #ifndef FXJS_CJS_TIMEROBJ_H_ #define FXJS_CJS_TIMEROBJ_H_ -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class GlobalTimer; diff --git a/fxjs/cjs_util.cpp b/fxjs/cjs_util.cpp index ee96395a16..8da9a07a45 100644 --- a/fxjs/cjs_util.cpp +++ b/fxjs/cjs_util.cpp @@ -15,12 +15,12 @@ #include #include "core/fxcrt/fx_extension.h" -#include "fxjs/JS_Define.h" #include "fxjs/cjs_event_context.h" #include "fxjs/cjs_eventhandler.h" #include "fxjs/cjs_object.h" #include "fxjs/cjs_publicmethods.h" #include "fxjs/cjs_runtime.h" +#include "fxjs/js_define.h" #include "fxjs/js_resources.h" #if _FX_OS_ == _FX_OS_ANDROID_ diff --git a/fxjs/cjs_util.h b/fxjs/cjs_util.h index 03e1b08d8a..a601d82225 100644 --- a/fxjs/cjs_util.h +++ b/fxjs/cjs_util.h @@ -10,7 +10,7 @@ #include #include -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" // Return values for ParseDataType() below. #define UTIL_INT 0 diff --git a/fxjs/cjs_zoomtype.h b/fxjs/cjs_zoomtype.h index a5f6fda15b..5dda997bdf 100644 --- a/fxjs/cjs_zoomtype.h +++ b/fxjs/cjs_zoomtype.h @@ -7,7 +7,7 @@ #ifndef FXJS_CJS_ZOOMTYPE_H_ #define FXJS_CJS_ZOOMTYPE_H_ -#include "fxjs/JS_Define.h" +#include "fxjs/js_define.h" class CJS_Zoomtype : public CJS_Object { public: diff --git a/fxjs/cjx_define.h b/fxjs/cjx_define.h new file mode 100644 index 0000000000..bff624fe00 --- /dev/null +++ b/fxjs/cjx_define.h @@ -0,0 +1,37 @@ +// 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. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FXJS_CJX_DEFINE_H_ +#define FXJS_CJX_DEFINE_H_ + +#include + +#include "fxjs/cfx_v8.h" +#include "fxjs/cjs_return.h" + +template >& params)> +CJS_Return JSMethod(C* node, + CFX_V8* runtime, + const std::vector>& params) { + return (node->*M)(runtime, params); +} + +#define JS_METHOD(method_name, class_name) \ + static CJS_Return method_name##_static( \ + CJX_Object* node, CFX_V8* runtime, \ + const std::vector>& params) { \ + return JSMethod( \ + static_cast(node), runtime, params); \ + } \ + CJS_Return method_name(CFX_V8* runtime, \ + const std::vector>& params) + +#define JS_PROP(prop_name) \ + void prop_name(CFXJSE_Value* pValue, bool bSetting, XFA_Attribute eAttribute) + +#endif // FXJS_CJX_DEFINE_H_ diff --git a/fxjs/js_define.cpp b/fxjs/js_define.cpp new file mode 100644 index 0000000000..3e14a3dd1d --- /dev/null +++ b/fxjs/js_define.cpp @@ -0,0 +1,312 @@ +// 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 "fxjs/js_define.h" + +#include + +#include +#include +#include +#include + +#include "fxjs/cjs_document.h" +#include "fxjs/cjs_object.h" + +namespace { + +double GetLocalTZA() { + if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) + return 0; + time_t t = 0; + time(&t); + localtime(&t); +#if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ + // In gcc 'timezone' is a global variable declared in time.h. In VC++, that + // variable was removed in VC++ 2015, with _get_timezone replacing it. + long timezone = 0; + _get_timezone(&timezone); +#endif // _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_ + return (double)(-(timezone * 1000)); +} + +int GetDaylightSavingTA(double d) { + if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) + return 0; + time_t t = (time_t)(d / 1000); + struct tm* tmp = localtime(&t); + if (!tmp) + return 0; + if (tmp->tm_isdst > 0) + // One hour. + return (int)60 * 60 * 1000; + return 0; +} + +double Mod(double x, double y) { + double r = fmod(x, y); + if (r < 0) + r += y; + return r; +} + +bool IsLeapYear(int year) { + return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 != 0)); +} + +int DayFromYear(int y) { + return (int)(365 * (y - 1970.0) + floor((y - 1969.0) / 4) - + floor((y - 1901.0) / 100) + floor((y - 1601.0) / 400)); +} + +double TimeFromYear(int y) { + return 86400000.0 * DayFromYear(y); +} + +static const uint16_t daysMonth[12] = {0, 31, 59, 90, 120, 151, + 181, 212, 243, 273, 304, 334}; +static const uint16_t leapDaysMonth[12] = {0, 31, 60, 91, 121, 152, + 182, 213, 244, 274, 305, 335}; + +double TimeFromYearMonth(int y, int m) { + const uint16_t* pMonth = IsLeapYear(y) ? leapDaysMonth : daysMonth; + return TimeFromYear(y) + ((double)pMonth[m]) * 86400000; +} + +int Day(double t) { + return static_cast(floor(t / 86400000.0)); +} + +int YearFromTime(double t) { + // estimate the time. + int y = 1970 + static_cast(t / (365.2425 * 86400000.0)); + if (TimeFromYear(y) <= t) { + while (TimeFromYear(y + 1) <= t) + y++; + } else { + while (TimeFromYear(y) > t) + y--; + } + return y; +} + +int DayWithinYear(double t) { + int year = YearFromTime(t); + int day = Day(t); + return day - DayFromYear(year); +} + +int MonthFromTime(double t) { + int day = DayWithinYear(t); + int year = YearFromTime(t); + if (0 <= day && day < 31) + return 0; + if (31 <= day && day < 59 + IsLeapYear(year)) + return 1; + if ((59 + IsLeapYear(year)) <= day && day < (90 + IsLeapYear(year))) + return 2; + if ((90 + IsLeapYear(year)) <= day && day < (120 + IsLeapYear(year))) + return 3; + if ((120 + IsLeapYear(year)) <= day && day < (151 + IsLeapYear(year))) + return 4; + if ((151 + IsLeapYear(year)) <= day && day < (181 + IsLeapYear(year))) + return 5; + if ((181 + IsLeapYear(year)) <= day && day < (212 + IsLeapYear(year))) + return 6; + if ((212 + IsLeapYear(year)) <= day && day < (243 + IsLeapYear(year))) + return 7; + if ((243 + IsLeapYear(year)) <= day && day < (273 + IsLeapYear(year))) + return 8; + if ((273 + IsLeapYear(year)) <= day && day < (304 + IsLeapYear(year))) + return 9; + if ((304 + IsLeapYear(year)) <= day && day < (334 + IsLeapYear(year))) + return 10; + if ((334 + IsLeapYear(year)) <= day && day < (365 + IsLeapYear(year))) + return 11; + + return -1; +} + +int DateFromTime(double t) { + int day = DayWithinYear(t); + int year = YearFromTime(t); + int leap = IsLeapYear(year); + int month = MonthFromTime(t); + switch (month) { + case 0: + return day + 1; + case 1: + return day - 30; + case 2: + return day - 58 - leap; + case 3: + return day - 89 - leap; + case 4: + return day - 119 - leap; + case 5: + return day - 150 - leap; + case 6: + return day - 180 - leap; + case 7: + return day - 211 - leap; + case 8: + return day - 242 - leap; + case 9: + return day - 272 - leap; + case 10: + return day - 303 - leap; + case 11: + return day - 333 - leap; + default: + return 0; + } +} + +} // namespace + +void JSDestructor(v8::Local obj) { + CFXJS_Engine::SetObjectPrivate(obj, nullptr); +} + +double JS_GetDateTime() { + if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) + return 0; + time_t t = time(nullptr); + struct tm* pTm = localtime(&t); + + int year = pTm->tm_year + 1900; + double t1 = TimeFromYear(year); + + return t1 + pTm->tm_yday * 86400000.0 + pTm->tm_hour * 3600000.0 + + pTm->tm_min * 60000.0 + pTm->tm_sec * 1000.0; +} + +int JS_GetYearFromTime(double dt) { + return YearFromTime(dt); +} + +int JS_GetMonthFromTime(double dt) { + return MonthFromTime(dt); +} + +int JS_GetDayFromTime(double dt) { + return DateFromTime(dt); +} + +int JS_GetHourFromTime(double dt) { + return (int)Mod(floor(dt / (60 * 60 * 1000)), 24); +} + +int JS_GetMinFromTime(double dt) { + return (int)Mod(floor(dt / (60 * 1000)), 60); +} + +int JS_GetSecFromTime(double dt) { + return (int)Mod(floor(dt / 1000), 60); +} + +double JS_LocalTime(double d) { + return d + GetLocalTZA() + GetDaylightSavingTA(d); +} + +double JS_DateParse(const WideString& str) { + v8::Isolate* pIsolate = v8::Isolate::GetCurrent(); + v8::Isolate::Scope isolate_scope(pIsolate); + v8::HandleScope scope(pIsolate); + + v8::Local context = pIsolate->GetCurrentContext(); + + // Use the built-in object method. + v8::Local v = + context->Global() + ->Get(context, v8::String::NewFromUtf8(pIsolate, "Date", + v8::NewStringType::kNormal) + .ToLocalChecked()) + .ToLocalChecked(); + if (v->IsObject()) { + v8::Local o = v->ToObject(context).ToLocalChecked(); + v = o->Get(context, v8::String::NewFromUtf8(pIsolate, "parse", + v8::NewStringType::kNormal) + .ToLocalChecked()) + .ToLocalChecked(); + if (v->IsFunction()) { + v8::Local funC = v8::Local::Cast(v); + const int argc = 1; + v8::Local timeStr = + CJS_Runtime::RuntimeFromIsolateCurrentContext(pIsolate)->NewString( + str.AsStringView()); + v8::Local argv[argc] = {timeStr}; + v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked(); + if (v->IsNumber()) { + double date = v->ToNumber(context).ToLocalChecked()->Value(); + if (!std::isfinite(date)) + return date; + return JS_LocalTime(date); + } + } + } + return 0; +} + +double JS_MakeDay(int nYear, int nMonth, int nDate) { + double y = static_cast(nYear); + double m = static_cast(nMonth); + double dt = static_cast(nDate); + double ym = y + floor(m / 12); + double mn = Mod(m, 12); + double t = TimeFromYearMonth(static_cast(ym), static_cast(mn)); + if (YearFromTime(t) != ym || MonthFromTime(t) != mn || DateFromTime(t) != 1) + return std::nan(""); + + return Day(t) + dt - 1; +} + +double JS_MakeTime(int nHour, int nMin, int nSec, int nMs) { + double h = static_cast(nHour); + double m = static_cast(nMin); + double s = static_cast(nSec); + double milli = static_cast(nMs); + return h * 3600000 + m * 60000 + s * 1000 + milli; +} + +double JS_MakeDate(double day, double time) { + if (!std::isfinite(day) || !std::isfinite(time)) + return std::nan(""); + + return day * 86400000 + time; +} + +std::vector> ExpandKeywordParams( + CJS_Runtime* pRuntime, + const std::vector>& originals, + size_t nKeywords, + ...) { + ASSERT(nKeywords); + + std::vector> result(nKeywords, v8::Local()); + size_t size = std::min(originals.size(), nKeywords); + for (size_t i = 0; i < size; ++i) + result[i] = originals[i]; + + if (originals.size() != 1 || !originals[0]->IsObject() || + originals[0]->IsArray()) { + return result; + } + result[0] = v8::Local(); // Make unknown. + + v8::Local pObj = pRuntime->ToObject(originals[0]); + va_list ap; + va_start(ap, nKeywords); + for (size_t i = 0; i < nKeywords; ++i) { + const wchar_t* property = va_arg(ap, const wchar_t*); + v8::Local v8Value = pRuntime->GetObjectProperty(pObj, property); + if (!v8Value->IsUndefined()) + result[i] = v8Value; + } + va_end(ap); + + return result; +} diff --git a/fxjs/js_define.h b/fxjs/js_define.h new file mode 100644 index 0000000000..325642d8ce --- /dev/null +++ b/fxjs/js_define.h @@ -0,0 +1,162 @@ +// 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 FXJS_JS_DEFINE_H_ +#define FXJS_JS_DEFINE_H_ + +#include +#include + +#include "fxjs/cfxjs_engine.h" +#include "fxjs/cjs_object.h" +#include "fxjs/cjs_return.h" +#include "fxjs/js_resources.h" +#include "third_party/base/ptr_util.h" + +double JS_GetDateTime(); +int JS_GetYearFromTime(double dt); +int JS_GetMonthFromTime(double dt); +int JS_GetDayFromTime(double dt); +int JS_GetHourFromTime(double dt); +int JS_GetMinFromTime(double dt); +int JS_GetSecFromTime(double dt); +double JS_LocalTime(double d); +double JS_DateParse(const WideString& str); +double JS_MakeDay(int nYear, int nMonth, int nDay); +double JS_MakeTime(int nHour, int nMin, int nSec, int nMs); +double JS_MakeDate(double day, double time); + +// Some JS methods have the bizarre convention that they may also be called +// with a single argument which is an object containing the actual arguments +// as its properties. The varying arguments to this method are the property +// names as wchar_t string literals corresponding to each positional argument. +// The result will always contain |nKeywords| value, with unspecified ones +// being set to type VT_unknown. +std::vector> ExpandKeywordParams( + CJS_Runtime* pRuntime, + const std::vector>& originals, + size_t nKeywords, + ...); + +// All JS classes have a name, an object defintion ID, and the ability to +// register themselves with FXJS_V8. We never make a BASE class on its own +// because it can't really do anything. + +// Rich JS classes provide constants, methods, properties, and the ability +// to construct native object state. + +template +static void JSConstructor(CFXJS_Engine* pEngine, v8::Local obj) { + auto pObj = pdfium::MakeUnique(obj, static_cast(pEngine)); + pObj->InitInstance(); + pEngine->SetObjectPrivate(obj, std::move(pObj)); +} + +// CJS_Object has vitual dtor, template not required. +void JSDestructor(v8::Local obj); + +template +void JSPropGetter(const char* prop_name_string, + const char* class_name_string, + v8::Local property, + const v8::PropertyCallbackInfo& info) { + CJS_Runtime* pRuntime = + CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); + if (!pRuntime) + return; + + CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder()); + if (!pJSObj) + return; + + C* pObj = static_cast(pJSObj); + CJS_Return result = (pObj->*M)(pRuntime); + if (result.HasError()) { + pRuntime->Error(JSFormatErrorString(class_name_string, prop_name_string, + result.Error())); + return; + } + + if (result.HasReturn()) + info.GetReturnValue().Set(result.Return()); +} + +template )> +void JSPropSetter(const char* prop_name_string, + const char* class_name_string, + v8::Local property, + v8::Local value, + const v8::PropertyCallbackInfo& info) { + CJS_Runtime* pRuntime = + CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); + if (!pRuntime) + return; + + CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder()); + if (!pJSObj) + return; + + C* pObj = static_cast(pJSObj); + CJS_Return result = (pObj->*M)(pRuntime, value); + if (result.HasError()) { + pRuntime->Error(JSFormatErrorString(class_name_string, prop_name_string, + result.Error())); + } +} + +template >&)> +void JSMethod(const char* method_name_string, + const char* class_name_string, + const v8::FunctionCallbackInfo& info) { + CJS_Runtime* pRuntime = + CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); + if (!pRuntime) + return; + + CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder()); + if (!pJSObj) + return; + + std::vector> parameters; + for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) + parameters.push_back(info[i]); + + C* pObj = static_cast(pJSObj); + CJS_Return result = (pObj->*M)(pRuntime, parameters); + if (result.HasError()) { + pRuntime->Error(JSFormatErrorString(class_name_string, method_name_string, + result.Error())); + return; + } + + if (result.HasReturn()) + info.GetReturnValue().Set(result.Return()); +} + +#define JS_STATIC_PROP(err_name, prop_name, class_name) \ + static void get_##prop_name##_static( \ + v8::Local property, \ + const v8::PropertyCallbackInfo& info) { \ + JSPropGetter( \ + #err_name, class_name::kName, property, info); \ + } \ + static void set_##prop_name##_static( \ + v8::Local property, v8::Local value, \ + const v8::PropertyCallbackInfo& info) { \ + JSPropSetter( \ + #err_name, class_name::kName, property, value, info); \ + } + +#define JS_STATIC_METHOD(method_name, class_name) \ + static void method_name##_static( \ + const v8::FunctionCallbackInfo& info) { \ + JSMethod(#method_name, \ + class_name::kName, info); \ + } + +#endif // FXJS_JS_DEFINE_H_ diff --git a/fxjs/xfa/cjx_container.h b/fxjs/xfa/cjx_container.h index 0e2170fa2a..e116358f07 100644 --- a/fxjs/xfa/cjx_container.h +++ b/fxjs/xfa/cjx_container.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_CONTAINER_H_ #define FXJS_XFA_CJX_CONTAINER_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_node.h" class CXFA_Node; diff --git a/fxjs/xfa/cjx_content.h b/fxjs/xfa/cjx_content.h index 983a75005d..351728e023 100644 --- a/fxjs/xfa/cjx_content.h +++ b/fxjs/xfa/cjx_content.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_CONTENT_H_ #define FXJS_XFA_CJX_CONTENT_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_object.h" class CXFA_Content; diff --git a/fxjs/xfa/cjx_datawindow.h b/fxjs/xfa/cjx_datawindow.h index dfaa6a44ba..0541ef1e7f 100644 --- a/fxjs/xfa/cjx_datawindow.h +++ b/fxjs/xfa/cjx_datawindow.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_DATAWINDOW_H_ #define FXJS_XFA_CJX_DATAWINDOW_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_object.h" #include "xfa/fxfa/fxfa_basic.h" diff --git a/fxjs/xfa/cjx_delta.h b/fxjs/xfa/cjx_delta.h index 2b981cd56b..4d6776359c 100644 --- a/fxjs/xfa/cjx_delta.h +++ b/fxjs/xfa/cjx_delta.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_DELTA_H_ #define FXJS_XFA_CJX_DELTA_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_object.h" class CXFA_Delta; diff --git a/fxjs/xfa/cjx_desc.h b/fxjs/xfa/cjx_desc.h index 4d21c7ba32..4218ed2d7b 100644 --- a/fxjs/xfa/cjx_desc.h +++ b/fxjs/xfa/cjx_desc.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_DESC_H_ #define FXJS_XFA_CJX_DESC_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_node.h" class CXFA_Desc; diff --git a/fxjs/xfa/cjx_eventpseudomodel.h b/fxjs/xfa/cjx_eventpseudomodel.h index 5d8bcc3060..b30c659705 100644 --- a/fxjs/xfa/cjx_eventpseudomodel.h +++ b/fxjs/xfa/cjx_eventpseudomodel.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_EVENTPSEUDOMODEL_H_ #define FXJS_XFA_CJX_EVENTPSEUDOMODEL_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_object.h" class CFXJSE_Value; diff --git a/fxjs/xfa/cjx_exclgroup.h b/fxjs/xfa/cjx_exclgroup.h index a972fba5ff..fc65400214 100644 --- a/fxjs/xfa/cjx_exclgroup.h +++ b/fxjs/xfa/cjx_exclgroup.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_EXCLGROUP_H_ #define FXJS_XFA_CJX_EXCLGROUP_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_node.h" class CXFA_ExclGroup; diff --git a/fxjs/xfa/cjx_field.h b/fxjs/xfa/cjx_field.h index 4f7238fc19..697aca32d2 100644 --- a/fxjs/xfa/cjx_field.h +++ b/fxjs/xfa/cjx_field.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_FIELD_H_ #define FXJS_XFA_CJX_FIELD_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_container.h" class CXFA_Field; diff --git a/fxjs/xfa/cjx_form.h b/fxjs/xfa/cjx_form.h index e4d64daca0..235dc1405c 100644 --- a/fxjs/xfa/cjx_form.h +++ b/fxjs/xfa/cjx_form.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_FORM_H_ #define FXJS_XFA_CJX_FORM_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_model.h" class CXFA_Form; diff --git a/fxjs/xfa/cjx_hostpseudomodel.h b/fxjs/xfa/cjx_hostpseudomodel.h index b4d6fe66e2..7b9279af2d 100644 --- a/fxjs/xfa/cjx_hostpseudomodel.h +++ b/fxjs/xfa/cjx_hostpseudomodel.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_HOSTPSEUDOMODEL_H_ #define FXJS_XFA_CJX_HOSTPSEUDOMODEL_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_object.h" #include "xfa/fxfa/fxfa_basic.h" diff --git a/fxjs/xfa/cjx_instancemanager.h b/fxjs/xfa/cjx_instancemanager.h index 74efad5ba9..c869ec3559 100644 --- a/fxjs/xfa/cjx_instancemanager.h +++ b/fxjs/xfa/cjx_instancemanager.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_INSTANCEMANAGER_H_ #define FXJS_XFA_CJX_INSTANCEMANAGER_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_node.h" class CXFA_InstanceManager; diff --git a/fxjs/xfa/cjx_layoutpseudomodel.h b/fxjs/xfa/cjx_layoutpseudomodel.h index ecacdd1e83..b57c80cd62 100644 --- a/fxjs/xfa/cjx_layoutpseudomodel.h +++ b/fxjs/xfa/cjx_layoutpseudomodel.h @@ -9,7 +9,7 @@ #include -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_object.h" enum XFA_LAYOUTMODEL_HWXY { diff --git a/fxjs/xfa/cjx_list.h b/fxjs/xfa/cjx_list.h index 83801b7e08..90ecf697c3 100644 --- a/fxjs/xfa/cjx_list.h +++ b/fxjs/xfa/cjx_list.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_LIST_H_ #define FXJS_XFA_CJX_LIST_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_object.h" class CXFA_List; diff --git a/fxjs/xfa/cjx_logpseudomodel.h b/fxjs/xfa/cjx_logpseudomodel.h index 772bf30540..a6ef871e89 100644 --- a/fxjs/xfa/cjx_logpseudomodel.h +++ b/fxjs/xfa/cjx_logpseudomodel.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_LOGPSEUDOMODEL_H_ #define FXJS_XFA_CJX_LOGPSEUDOMODEL_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_object.h" class CScript_LogPseudoModel; diff --git a/fxjs/xfa/cjx_manifest.h b/fxjs/xfa/cjx_manifest.h index 75fcacdfb1..78aa5bcfd3 100644 --- a/fxjs/xfa/cjx_manifest.h +++ b/fxjs/xfa/cjx_manifest.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_MANIFEST_H_ #define FXJS_XFA_CJX_MANIFEST_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_node.h" class CXFA_Manifest; diff --git a/fxjs/xfa/cjx_model.h b/fxjs/xfa/cjx_model.h index fae1cc7cfd..607123571e 100644 --- a/fxjs/xfa/cjx_model.h +++ b/fxjs/xfa/cjx_model.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_MODEL_H_ #define FXJS_XFA_CJX_MODEL_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_node.h" class CXFA_Node; diff --git a/fxjs/xfa/cjx_node.h b/fxjs/xfa/cjx_node.h index f7cbce855b..224f44cc42 100644 --- a/fxjs/xfa/cjx_node.h +++ b/fxjs/xfa/cjx_node.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_NODE_H_ #define FXJS_XFA_CJX_NODE_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_object.h" #include "fxjs/xfa/cjx_tree.h" #include "xfa/fxfa/fxfa_basic.h" diff --git a/fxjs/xfa/cjx_object.h b/fxjs/xfa/cjx_object.h index 1fcd753b14..ac17a04522 100644 --- a/fxjs/xfa/cjx_object.h +++ b/fxjs/xfa/cjx_object.h @@ -15,7 +15,7 @@ #include "core/fxcrt/unowned_ptr.h" #include "core/fxcrt/widestring.h" #include "core/fxcrt/xml/cfx_xmlelement.h" -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "third_party/base/optional.h" #include "xfa/fxfa/fxfa_basic.h" diff --git a/fxjs/xfa/cjx_packet.h b/fxjs/xfa/cjx_packet.h index ac1c170fdd..573ea3af75 100644 --- a/fxjs/xfa/cjx_packet.h +++ b/fxjs/xfa/cjx_packet.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_PACKET_H_ #define FXJS_XFA_CJX_PACKET_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_node.h" class CXFA_Packet; diff --git a/fxjs/xfa/cjx_signaturepseudomodel.h b/fxjs/xfa/cjx_signaturepseudomodel.h index 88f90fb33a..0aecfa57a0 100644 --- a/fxjs/xfa/cjx_signaturepseudomodel.h +++ b/fxjs/xfa/cjx_signaturepseudomodel.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_SIGNATUREPSEUDOMODEL_H_ #define FXJS_XFA_CJX_SIGNATUREPSEUDOMODEL_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_object.h" class CScript_SignaturePseudoModel; diff --git a/fxjs/xfa/cjx_source.h b/fxjs/xfa/cjx_source.h index de84912910..dcbdfde26d 100644 --- a/fxjs/xfa/cjx_source.h +++ b/fxjs/xfa/cjx_source.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_SOURCE_H_ #define FXJS_XFA_CJX_SOURCE_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_node.h" class CXFA_Source; diff --git a/fxjs/xfa/cjx_subform.h b/fxjs/xfa/cjx_subform.h index a1c535b643..e982fa59b3 100644 --- a/fxjs/xfa/cjx_subform.h +++ b/fxjs/xfa/cjx_subform.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_SUBFORM_H_ #define FXJS_XFA_CJX_SUBFORM_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_container.h" class CXFA_Delta; diff --git a/fxjs/xfa/cjx_template.h b/fxjs/xfa/cjx_template.h index 2c82788153..e67b1b7495 100644 --- a/fxjs/xfa/cjx_template.h +++ b/fxjs/xfa/cjx_template.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_TEMPLATE_H_ #define FXJS_XFA_CJX_TEMPLATE_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_model.h" class CXFA_Template; diff --git a/fxjs/xfa/cjx_textnode.h b/fxjs/xfa/cjx_textnode.h index 12d44d7814..f3dbe19251 100644 --- a/fxjs/xfa/cjx_textnode.h +++ b/fxjs/xfa/cjx_textnode.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_TEXTNODE_H_ #define FXJS_XFA_CJX_TEXTNODE_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_node.h" class CXFA_Node; diff --git a/fxjs/xfa/cjx_tree.h b/fxjs/xfa/cjx_tree.h index 9389d2ec4f..22b3acad48 100644 --- a/fxjs/xfa/cjx_tree.h +++ b/fxjs/xfa/cjx_tree.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_TREE_H_ #define FXJS_XFA_CJX_TREE_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_object.h" class CXFA_Object; diff --git a/fxjs/xfa/cjx_treelist.h b/fxjs/xfa/cjx_treelist.h index 29a95e6d74..b3f1dafc48 100644 --- a/fxjs/xfa/cjx_treelist.h +++ b/fxjs/xfa/cjx_treelist.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_TREELIST_H_ #define FXJS_XFA_CJX_TREELIST_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_list.h" class CXFA_TreeList; diff --git a/fxjs/xfa/cjx_wsdlconnection.h b/fxjs/xfa/cjx_wsdlconnection.h index d755b5d075..eb729c21ea 100644 --- a/fxjs/xfa/cjx_wsdlconnection.h +++ b/fxjs/xfa/cjx_wsdlconnection.h @@ -7,7 +7,7 @@ #ifndef FXJS_XFA_CJX_WSDLCONNECTION_H_ #define FXJS_XFA_CJX_WSDLCONNECTION_H_ -#include "fxjs/CJX_Define.h" +#include "fxjs/cjx_define.h" #include "fxjs/xfa/cjx_node.h" class CXFA_WsdlConnection; -- cgit v1.2.3