From a146507fdbe8a8d13fe590b5fa883efc7bffe90a Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Mon, 30 Oct 2017 17:05:31 +0000 Subject: Move JS_Value into JS_Define This CL moves CJS_Return to its own files and moves the remaining function definitions from JS_Value to JS_Define. Change-Id: Ic7058a02330153f22d1fff9fc6cdebd9167c498b Reviewed-on: https://pdfium-review.googlesource.com/17038 Commit-Queue: dsinclair Reviewed-by: Tom Sepez --- fpdfsdk/javascript/JS_Define.cpp | 308 ++++++++++++++++++++++++++++++ fpdfsdk/javascript/JS_Define.h | 27 ++- fpdfsdk/javascript/JS_Value.cpp | 320 -------------------------------- fpdfsdk/javascript/JS_Value.h | 65 ------- fpdfsdk/javascript/PublicMethods.cpp | 1 - fpdfsdk/javascript/app.cpp | 1 - fpdfsdk/javascript/cjs_annot.cpp | 1 - fpdfsdk/javascript/cjs_document.h | 1 + fpdfsdk/javascript/cjs_eventhandler.cpp | 1 - fpdfsdk/javascript/cjs_global.cpp | 1 - fpdfsdk/javascript/cjs_return.cpp | 18 ++ fpdfsdk/javascript/cjs_return.h | 34 ++++ fpdfsdk/javascript/cjs_runtime.cpp | 1 - fpdfsdk/javascript/color.cpp | 1 - fpdfsdk/javascript/console.cpp | 1 - fpdfsdk/javascript/event.cpp | 1 - fpdfsdk/javascript/report.cpp | 1 - fpdfsdk/javascript/util.cpp | 1 - 18 files changed, 387 insertions(+), 397 deletions(-) create mode 100644 fpdfsdk/javascript/JS_Define.cpp delete mode 100644 fpdfsdk/javascript/JS_Value.cpp delete mode 100644 fpdfsdk/javascript/JS_Value.h create mode 100644 fpdfsdk/javascript/cjs_return.cpp create mode 100644 fpdfsdk/javascript/cjs_return.h (limited to 'fpdfsdk') diff --git a/fpdfsdk/javascript/JS_Define.cpp b/fpdfsdk/javascript/JS_Define.cpp new file mode 100644 index 0000000000..18887e636a --- /dev/null +++ b/fpdfsdk/javascript/JS_Define.cpp @@ -0,0 +1,308 @@ +// 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 "fpdfsdk/javascript/JS_Define.h" + +#include + +#include +#include +#include +#include + +#include "fpdfsdk/javascript/cjs_document.h" +#include "fpdfsdk/javascript/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 + +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::CurrentRuntimeFromIsolate(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/fpdfsdk/javascript/JS_Define.h b/fpdfsdk/javascript/JS_Define.h index 5824011665..4f82a74b0d 100644 --- a/fpdfsdk/javascript/JS_Define.h +++ b/fpdfsdk/javascript/JS_Define.h @@ -9,11 +9,36 @@ #include -#include "fpdfsdk/javascript/JS_Value.h" #include "fpdfsdk/javascript/cjs_object.h" +#include "fpdfsdk/javascript/cjs_return.h" #include "fpdfsdk/javascript/resource.h" #include "fxjs/fxjs_v8.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. diff --git a/fpdfsdk/javascript/JS_Value.cpp b/fpdfsdk/javascript/JS_Value.cpp deleted file mode 100644 index da09ff28ac..0000000000 --- a/fpdfsdk/javascript/JS_Value.cpp +++ /dev/null @@ -1,320 +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 "fpdfsdk/javascript/JS_Value.h" - -#include - -#include -#include -#include -#include - -#include "fpdfsdk/javascript/JS_Define.h" -#include "fpdfsdk/javascript/cjs_document.h" -#include "fpdfsdk/javascript/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 - -CJS_Return::CJS_Return(bool result) : is_error_(!result) {} - -CJS_Return::CJS_Return(const WideString& err) : is_error_(true), error_(err) {} - -CJS_Return::CJS_Return(v8::Local ret) - : is_error_(false), return_(ret) {} - -CJS_Return::CJS_Return(const CJS_Return&) = default; - -CJS_Return::~CJS_Return() = default; - -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::CurrentRuntimeFromIsolate(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/fpdfsdk/javascript/JS_Value.h b/fpdfsdk/javascript/JS_Value.h deleted file mode 100644 index 046d959e68..0000000000 --- a/fpdfsdk/javascript/JS_Value.h +++ /dev/null @@ -1,65 +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 FPDFSDK_JAVASCRIPT_JS_VALUE_H_ -#define FPDFSDK_JAVASCRIPT_JS_VALUE_H_ - -#include - -#include "fxjs/fxjs_v8.h" - -class CJS_Document; -class CJS_Object; -class CJS_Runtime; - -class CJS_Return { - public: - explicit CJS_Return(bool); - explicit CJS_Return(const WideString&); - explicit CJS_Return(v8::Local); - CJS_Return(const CJS_Return&); - ~CJS_Return(); - - bool HasError() const { return is_error_; } - WideString Error() const { return error_; } - - bool HasReturn() const { return !return_.IsEmpty(); } - v8::Local Return() const { return return_; } - - private: - CJS_Return() = delete; - - bool is_error_ = false; - WideString error_; - v8::Local return_; -}; - -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, - ...); - -#endif // FPDFSDK_JAVASCRIPT_JS_VALUE_H_ diff --git a/fpdfsdk/javascript/PublicMethods.cpp b/fpdfsdk/javascript/PublicMethods.cpp index b2469ad218..7969b62d9a 100644 --- a/fpdfsdk/javascript/PublicMethods.cpp +++ b/fpdfsdk/javascript/PublicMethods.cpp @@ -20,7 +20,6 @@ #include "fpdfsdk/cpdfsdk_formfillenvironment.h" #include "fpdfsdk/cpdfsdk_interform.h" #include "fpdfsdk/javascript/JS_Define.h" -#include "fpdfsdk/javascript/JS_Value.h" #include "fpdfsdk/javascript/cjs_event_context.h" #include "fpdfsdk/javascript/cjs_eventhandler.h" #include "fpdfsdk/javascript/cjs_field.h" diff --git a/fpdfsdk/javascript/app.cpp b/fpdfsdk/javascript/app.cpp index 8a91a1da39..3e8ca0bf1a 100644 --- a/fpdfsdk/javascript/app.cpp +++ b/fpdfsdk/javascript/app.cpp @@ -13,7 +13,6 @@ #include "fpdfsdk/cpdfsdk_formfillenvironment.h" #include "fpdfsdk/cpdfsdk_interform.h" #include "fpdfsdk/javascript/JS_Define.h" -#include "fpdfsdk/javascript/JS_Value.h" #include "fpdfsdk/javascript/cjs_document.h" #include "fpdfsdk/javascript/cjs_event_context.h" #include "fpdfsdk/javascript/cjs_eventhandler.h" diff --git a/fpdfsdk/javascript/cjs_annot.cpp b/fpdfsdk/javascript/cjs_annot.cpp index fa7773fab4..8ff0d2c72e 100644 --- a/fpdfsdk/javascript/cjs_annot.cpp +++ b/fpdfsdk/javascript/cjs_annot.cpp @@ -7,7 +7,6 @@ #include "fpdfsdk/javascript/cjs_annot.h" #include "fpdfsdk/javascript/JS_Define.h" -#include "fpdfsdk/javascript/JS_Value.h" #include "fpdfsdk/javascript/cjs_event_context.h" #include "fpdfsdk/javascript/cjs_object.h" diff --git a/fpdfsdk/javascript/cjs_document.h b/fpdfsdk/javascript/cjs_document.h index e6aab6b2c0..f935eae7d4 100644 --- a/fpdfsdk/javascript/cjs_document.h +++ b/fpdfsdk/javascript/cjs_document.h @@ -13,6 +13,7 @@ #include "fpdfsdk/javascript/JS_Define.h" +class CJS_Document; class CPDF_TextObject; struct CJS_DelayData; diff --git a/fpdfsdk/javascript/cjs_eventhandler.cpp b/fpdfsdk/javascript/cjs_eventhandler.cpp index 002f6b0fd8..406fd0bb1c 100644 --- a/fpdfsdk/javascript/cjs_eventhandler.cpp +++ b/fpdfsdk/javascript/cjs_eventhandler.cpp @@ -8,7 +8,6 @@ #include "core/fpdfdoc/cpdf_formfield.h" #include "fpdfsdk/javascript/JS_Define.h" -#include "fpdfsdk/javascript/JS_Value.h" #include "fpdfsdk/javascript/cjs_document.h" #include "fpdfsdk/javascript/cjs_event_context.h" #include "fpdfsdk/javascript/cjs_field.h" diff --git a/fpdfsdk/javascript/cjs_global.cpp b/fpdfsdk/javascript/cjs_global.cpp index 5ef4d09894..d89005d2af 100644 --- a/fpdfsdk/javascript/cjs_global.cpp +++ b/fpdfsdk/javascript/cjs_global.cpp @@ -15,7 +15,6 @@ #include "fpdfsdk/javascript/JS_Define.h" #include "fpdfsdk/javascript/JS_GlobalData.h" #include "fpdfsdk/javascript/JS_KeyValue.h" -#include "fpdfsdk/javascript/JS_Value.h" #include "fpdfsdk/javascript/cjs_event_context.h" #include "fpdfsdk/javascript/cjs_eventhandler.h" #include "fpdfsdk/javascript/cjs_object.h" diff --git a/fpdfsdk/javascript/cjs_return.cpp b/fpdfsdk/javascript/cjs_return.cpp new file mode 100644 index 0000000000..ecf36da705 --- /dev/null +++ b/fpdfsdk/javascript/cjs_return.cpp @@ -0,0 +1,18 @@ +// 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 + +#include "fpdfsdk/javascript/cjs_return.h" + +CJS_Return::CJS_Return(bool result) : is_error_(!result) {} + +CJS_Return::CJS_Return(const WideString& err) : is_error_(true), error_(err) {} + +CJS_Return::CJS_Return(v8::Local ret) + : is_error_(false), return_(ret) {} + +CJS_Return::CJS_Return(const CJS_Return&) = default; + +CJS_Return::~CJS_Return() = default; diff --git a/fpdfsdk/javascript/cjs_return.h b/fpdfsdk/javascript/cjs_return.h new file mode 100644 index 0000000000..f277c1a5a9 --- /dev/null +++ b/fpdfsdk/javascript/cjs_return.h @@ -0,0 +1,34 @@ +// 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 FPDFSDK_JAVASCRIPT_CJS_RETURN_H_ +#define FPDFSDK_JAVASCRIPT_CJS_RETURN_H_ + +#include "fxjs/fxjs_v8.h" + +class CJS_Return { + public: + explicit CJS_Return(bool); + explicit CJS_Return(const WideString&); + explicit CJS_Return(v8::Local); + CJS_Return(const CJS_Return&); + ~CJS_Return(); + + bool HasError() const { return is_error_; } + WideString Error() const { return error_; } + + bool HasReturn() const { return !return_.IsEmpty(); } + v8::Local Return() const { return return_; } + + private: + CJS_Return() = delete; + + bool is_error_ = false; + WideString error_; + v8::Local return_; +}; + +#endif // FPDFSDK_JAVASCRIPT_CJS_RETURN_H_ diff --git a/fpdfsdk/javascript/cjs_runtime.cpp b/fpdfsdk/javascript/cjs_runtime.cpp index 64123ce395..139dacf6da 100644 --- a/fpdfsdk/javascript/cjs_runtime.cpp +++ b/fpdfsdk/javascript/cjs_runtime.cpp @@ -11,7 +11,6 @@ #include "fpdfsdk/cpdfsdk_formfillenvironment.h" #include "fpdfsdk/javascript/JS_Define.h" #include "fpdfsdk/javascript/JS_GlobalData.h" -#include "fpdfsdk/javascript/JS_Value.h" #include "fpdfsdk/javascript/PublicMethods.h" #include "fpdfsdk/javascript/app.h" #include "fpdfsdk/javascript/cjs_annot.h" diff --git a/fpdfsdk/javascript/color.cpp b/fpdfsdk/javascript/color.cpp index 405f12d0f3..0f9ed0a4b8 100644 --- a/fpdfsdk/javascript/color.cpp +++ b/fpdfsdk/javascript/color.cpp @@ -9,7 +9,6 @@ #include #include "fpdfsdk/javascript/JS_Define.h" -#include "fpdfsdk/javascript/JS_Value.h" #include "fpdfsdk/javascript/cjs_event_context.h" #include "fpdfsdk/javascript/cjs_eventhandler.h" #include "fpdfsdk/javascript/cjs_object.h" diff --git a/fpdfsdk/javascript/console.cpp b/fpdfsdk/javascript/console.cpp index 81db44d2a1..67ca1655ff 100644 --- a/fpdfsdk/javascript/console.cpp +++ b/fpdfsdk/javascript/console.cpp @@ -9,7 +9,6 @@ #include #include "fpdfsdk/javascript/JS_Define.h" -#include "fpdfsdk/javascript/JS_Value.h" #include "fpdfsdk/javascript/cjs_event_context.h" #include "fpdfsdk/javascript/cjs_eventhandler.h" #include "fpdfsdk/javascript/cjs_object.h" diff --git a/fpdfsdk/javascript/event.cpp b/fpdfsdk/javascript/event.cpp index 7682a64033..22aaa04315 100644 --- a/fpdfsdk/javascript/event.cpp +++ b/fpdfsdk/javascript/event.cpp @@ -7,7 +7,6 @@ #include "fpdfsdk/javascript/event.h" #include "fpdfsdk/javascript/JS_Define.h" -#include "fpdfsdk/javascript/JS_Value.h" #include "fpdfsdk/javascript/cjs_event_context.h" #include "fpdfsdk/javascript/cjs_eventhandler.h" #include "fpdfsdk/javascript/cjs_field.h" diff --git a/fpdfsdk/javascript/report.cpp b/fpdfsdk/javascript/report.cpp index 676451af82..88ea57569a 100644 --- a/fpdfsdk/javascript/report.cpp +++ b/fpdfsdk/javascript/report.cpp @@ -9,7 +9,6 @@ #include #include "fpdfsdk/javascript/JS_Define.h" -#include "fpdfsdk/javascript/JS_Value.h" #include "fpdfsdk/javascript/cjs_object.h" const JSMethodSpec CJS_Report::MethodSpecs[] = {{"save", save_static}, diff --git a/fpdfsdk/javascript/util.cpp b/fpdfsdk/javascript/util.cpp index a8f5486c35..99934726ff 100644 --- a/fpdfsdk/javascript/util.cpp +++ b/fpdfsdk/javascript/util.cpp @@ -16,7 +16,6 @@ #include "core/fxcrt/fx_extension.h" #include "fpdfsdk/javascript/JS_Define.h" -#include "fpdfsdk/javascript/JS_Value.h" #include "fpdfsdk/javascript/PublicMethods.h" #include "fpdfsdk/javascript/cjs_event_context.h" #include "fpdfsdk/javascript/cjs_eventhandler.h" -- cgit v1.2.3