diff options
Diffstat (limited to 'fxjs/js_define.cpp')
-rw-r--r-- | fxjs/js_define.cpp | 70 |
1 files changed, 34 insertions, 36 deletions
diff --git a/fxjs/js_define.cpp b/fxjs/js_define.cpp index 0c0c02cdc..b19c2e205 100644 --- a/fxjs/js_define.cpp +++ b/fxjs/js_define.cpp @@ -1,4 +1,4 @@ -// Copyright 2014 PDFium Authors. All rights reserved. +// Copyright 2014 The PDFium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -6,8 +6,10 @@ #include "fxjs/js_define.h" +#include <math.h> +#include <stdarg.h> + #include <algorithm> -#include <cmath> #include <limits> #include <vector> @@ -15,50 +17,46 @@ #include "fxjs/cjs_document.h" #include "fxjs/cjs_object.h" #include "fxjs/fx_date_helpers.h" +#include "fxjs/fxv8.h" +#include "third_party/base/check.h" +#include "v8/include/v8-context.h" +#include "v8/include/v8-function.h" +#include "v8/include/v8-isolate.h" void JSDestructor(v8::Local<v8::Object> obj) { CFXJS_Engine::SetObjectPrivate(obj, nullptr); } -double JS_DateParse(const WideString& str) { - v8::Isolate* pIsolate = v8::Isolate::GetCurrent(); +double JS_DateParse(v8::Isolate* pIsolate, const WideString& str) { v8::Isolate::Scope isolate_scope(pIsolate); v8::HandleScope scope(pIsolate); v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); // Use the built-in object method. - v8::Local<v8::Value> v = - context->Global() - ->Get(context, v8::String::NewFromUtf8(pIsolate, "Date", - v8::NewStringType::kNormal) - .ToLocalChecked()) - .ToLocalChecked(); - if (v->IsObject()) { - v8::Local<v8::Object> o = v->ToObject(context).ToLocalChecked(); - v = o->Get(context, v8::String::NewFromUtf8(pIsolate, "parse", - v8::NewStringType::kNormal) - .ToLocalChecked()) - .ToLocalChecked(); - if (v->IsFunction()) { - v8::Local<v8::Function> funC = v8::Local<v8::Function>::Cast(v); - const int argc = 1; - v8::Local<v8::Value> timeStr = - v8::String::NewFromUtf8(pIsolate, - FX_UTF8Encode(str.AsStringView()).c_str(), - v8::NewStringType::kNormal) - .ToLocalChecked(); - v8::Local<v8::Value> 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 FX_LocalTime(date); - } - } - } - return 0; + v8::MaybeLocal<v8::Value> maybe_value = + context->Global()->Get(context, fxv8::NewStringHelper(pIsolate, "Date")); + + v8::Local<v8::Value> value; + if (!maybe_value.ToLocal(&value) || !value->IsObject()) + return 0; + + v8::Local<v8::Object> obj = value.As<v8::Object>(); + maybe_value = obj->Get(context, fxv8::NewStringHelper(pIsolate, "parse")); + if (!maybe_value.ToLocal(&value) || !value->IsFunction()) + return 0; + + v8::Local<v8::Function> func = value.As<v8::Function>(); + static constexpr int argc = 1; + v8::Local<v8::Value> argv[argc] = { + fxv8::NewStringHelper(pIsolate, str.AsStringView()), + }; + maybe_value = func->Call(context, context->Global(), argc, argv); + if (!maybe_value.ToLocal(&value) || !value->IsNumber()) + return 0; + + double date = value.As<v8::Number>()->Value(); + return isfinite(date) ? FX_LocalTime(date) : date; } std::vector<v8::Local<v8::Value>> ExpandKeywordParams( @@ -66,7 +64,7 @@ std::vector<v8::Local<v8::Value>> ExpandKeywordParams( const std::vector<v8::Local<v8::Value>>& originals, size_t nKeywords, ...) { - ASSERT(nKeywords); + DCHECK(nKeywords); std::vector<v8::Local<v8::Value>> result(nKeywords, v8::Local<v8::Value>()); size_t size = std::min(originals.size(), nKeywords); |