Make more of CFX_V8 methods ordinary functions.
Having to obtain the CFX_V8 wrapper to invoke these doesn't
accomplish very much.
Change-Id: I795c10d7063ea9f7b27c87eb1a7f9edef11046ff
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/67510
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/fxjs/cfx_v8.cpp b/fxjs/cfx_v8.cpp
index 8972158..41cd44f 100644
--- a/fxjs/cfx_v8.cpp
+++ b/fxjs/cfx_v8.cpp
@@ -17,42 +17,20 @@
v8::Local<v8::Value> CFX_V8::GetObjectProperty(
v8::Local<v8::Object> pObj,
ByteStringView bsUTF8PropertyName) {
- if (pObj.IsEmpty())
- return v8::Local<v8::Value>();
- v8::Local<v8::Value> val;
- if (!pObj->Get(m_pIsolate->GetCurrentContext(), NewString(bsUTF8PropertyName))
- .ToLocal(&val))
- return v8::Local<v8::Value>();
- return val;
+ return fxv8::ReentrantGetObjectPropertyHelper(m_pIsolate.Get(), pObj,
+ bsUTF8PropertyName);
}
std::vector<WideString> CFX_V8::GetObjectPropertyNames(
v8::Local<v8::Object> pObj) {
- if (pObj.IsEmpty())
- return std::vector<WideString>();
-
- v8::Local<v8::Array> val;
- v8::Local<v8::Context> context = m_pIsolate->GetCurrentContext();
- if (!pObj->GetPropertyNames(context).ToLocal(&val))
- return std::vector<WideString>();
-
- std::vector<WideString> result;
- for (uint32_t i = 0; i < val->Length(); ++i) {
- result.push_back(ToWideString(val->Get(context, i).ToLocalChecked()));
- }
-
- return result;
+ return fxv8::ReentrantGetObjectPropertyNamesHelper(m_pIsolate.Get(), pObj);
}
bool CFX_V8::PutObjectProperty(v8::Local<v8::Object> pObj,
ByteStringView bsUTF8PropertyName,
v8::Local<v8::Value> pPut) {
- ASSERT(!pPut.IsEmpty());
- if (pObj.IsEmpty())
- return false;
-
- v8::Local<v8::String> name = NewString(bsUTF8PropertyName);
- return pObj->Set(m_pIsolate->GetCurrentContext(), name, pPut).IsJust();
+ return fxv8::ReentrantPutObjectPropertyHelper(m_pIsolate.Get(), pObj,
+ bsUTF8PropertyName, pPut);
}
void CFX_V8::DisposeIsolate() {
diff --git a/fxjs/fxv8.cpp b/fxjs/fxv8.cpp
index c30461b..8068f39 100644
--- a/fxjs/fxv8.cpp
+++ b/fxjs/fxv8.cpp
@@ -44,10 +44,12 @@
v8::Local<v8::Value> pValue) {
if (pValue.IsEmpty())
return WideString();
+
v8::MaybeLocal<v8::String> maybe_string =
pValue->ToString(pIsolate->GetCurrentContext());
if (maybe_string.IsEmpty())
return WideString();
+
v8::String::Utf8Value s(pIsolate, maybe_string.ToLocalChecked());
return WideString::FromUTF8(ByteStringView(*s, s.length()));
}
@@ -56,12 +58,61 @@
v8::Local<v8::Value> pValue) {
if (pValue.IsEmpty())
return ByteString();
+
v8::MaybeLocal<v8::String> maybe_string =
pValue->ToString(pIsolate->GetCurrentContext());
if (maybe_string.IsEmpty())
return ByteString();
+
v8::String::Utf8Value s(pIsolate, maybe_string.ToLocalChecked());
return ByteString(*s);
}
+v8::Local<v8::Value> ReentrantGetObjectPropertyHelper(
+ v8::Isolate* pIsolate,
+ v8::Local<v8::Object> pObj,
+ ByteStringView bsUTF8PropertyName) {
+ if (pObj.IsEmpty())
+ return v8::Local<v8::Value>();
+
+ v8::Local<v8::Value> val;
+ if (!pObj->Get(pIsolate->GetCurrentContext(),
+ NewStringHelper(pIsolate, bsUTF8PropertyName))
+ .ToLocal(&val)) {
+ return v8::Local<v8::Value>();
+ }
+ return val;
+}
+
+std::vector<WideString> ReentrantGetObjectPropertyNamesHelper(
+ v8::Isolate* pIsolate,
+ v8::Local<v8::Object> pObj) {
+ if (pObj.IsEmpty())
+ return std::vector<WideString>();
+
+ v8::Local<v8::Array> val;
+ v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
+ if (!pObj->GetPropertyNames(context).ToLocal(&val))
+ return std::vector<WideString>();
+
+ std::vector<WideString> result;
+ for (uint32_t i = 0; i < val->Length(); ++i) {
+ result.push_back(ReentrantToWideStringHelper(
+ pIsolate, val->Get(context, i).ToLocalChecked()));
+ }
+ return result;
+}
+
+bool ReentrantPutObjectPropertyHelper(v8::Isolate* pIsolate,
+ v8::Local<v8::Object> pObj,
+ ByteStringView bsUTF8PropertyName,
+ v8::Local<v8::Value> pPut) {
+ ASSERT(!pPut.IsEmpty());
+ if (pObj.IsEmpty())
+ return false;
+
+ v8::Local<v8::String> name = NewStringHelper(pIsolate, bsUTF8PropertyName);
+ return pObj->Set(pIsolate->GetCurrentContext(), name, pPut).IsJust();
+}
+
} // namespace fxv8
diff --git a/fxjs/fxv8.h b/fxjs/fxv8.h
index 690beb3..b79ec27 100644
--- a/fxjs/fxv8.h
+++ b/fxjs/fxv8.h
@@ -7,6 +7,8 @@
#ifndef FXJS_FXV8_H_
#define FXJS_FXV8_H_
+#include <vector>
+
#include "core/fxcrt/fx_string.h"
#include "v8/include/v8.h"
@@ -27,6 +29,18 @@
ByteString ReentrantToByteStringHelper(v8::Isolate* pIsolate,
v8::Local<v8::Value> pValue);
+v8::Local<v8::Value> ReentrantGetObjectPropertyHelper(
+ v8::Isolate* pIsolate,
+ v8::Local<v8::Object> pObj,
+ ByteStringView bsUTF8PropertyName);
+std::vector<WideString> ReentrantGetObjectPropertyNamesHelper(
+ v8::Isolate* pIsolate,
+ v8::Local<v8::Object> pObj);
+bool ReentrantPutObjectPropertyHelper(v8::Isolate* pIsolate,
+ v8::Local<v8::Object> pObj,
+ ByteStringView bsUTF8PropertyName,
+ v8::Local<v8::Value> pPut);
+
} // namespace fxv8
#endif // FXJS_FXV8_H_