1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
|
// Copyright 2016 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/cfxjse_class.h"
#include "fxjs/cfxjse_context.h"
#include "fxjs/cfxjse_value.h"
namespace {
void V8FunctionCallback_Wrapper(
const v8::FunctionCallbackInfo<v8::Value>& info) {
const FXJSE_FUNCTION_DESCRIPTOR* lpFunctionInfo =
static_cast<FXJSE_FUNCTION_DESCRIPTOR*>(
info.Data().As<v8::External>()->Value());
if (!lpFunctionInfo)
return;
CFX_ByteStringC szFunctionName(lpFunctionInfo->name);
std::unique_ptr<CFXJSE_Value> lpThisValue(
new CFXJSE_Value(info.GetIsolate()));
lpThisValue->ForceSetValue(info.This());
std::unique_ptr<CFXJSE_Value> lpRetValue(new CFXJSE_Value(info.GetIsolate()));
CFXJSE_Arguments impl(&info, lpRetValue.get());
lpFunctionInfo->callbackProc(lpThisValue.get(), szFunctionName, impl);
if (!lpRetValue->DirectGetValue().IsEmpty())
info.GetReturnValue().Set(lpRetValue->DirectGetValue());
}
void V8ClassGlobalConstructorCallback_Wrapper(
const v8::FunctionCallbackInfo<v8::Value>& info) {
const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition =
static_cast<FXJSE_CLASS_DESCRIPTOR*>(
info.Data().As<v8::External>()->Value());
if (!lpClassDefinition)
return;
CFX_ByteStringC szFunctionName(lpClassDefinition->name);
std::unique_ptr<CFXJSE_Value> lpThisValue(
new CFXJSE_Value(info.GetIsolate()));
lpThisValue->ForceSetValue(info.This());
std::unique_ptr<CFXJSE_Value> lpRetValue(new CFXJSE_Value(info.GetIsolate()));
CFXJSE_Arguments impl(&info, lpRetValue.get());
lpClassDefinition->constructor(lpThisValue.get(), szFunctionName, impl);
if (!lpRetValue->DirectGetValue().IsEmpty())
info.GetReturnValue().Set(lpRetValue->DirectGetValue());
}
void V8GetterCallback_Wrapper(v8::Local<v8::String> property,
const v8::PropertyCallbackInfo<v8::Value>& info) {
const FXJSE_PROPERTY_DESCRIPTOR* lpPropertyInfo =
static_cast<FXJSE_PROPERTY_DESCRIPTOR*>(
info.Data().As<v8::External>()->Value());
if (!lpPropertyInfo)
return;
CFX_ByteStringC szPropertyName(lpPropertyInfo->name);
std::unique_ptr<CFXJSE_Value> lpThisValue(
new CFXJSE_Value(info.GetIsolate()));
std::unique_ptr<CFXJSE_Value> lpPropValue(
new CFXJSE_Value(info.GetIsolate()));
lpThisValue->ForceSetValue(info.This());
lpPropertyInfo->getProc(lpThisValue.get(), szPropertyName, lpPropValue.get());
info.GetReturnValue().Set(lpPropValue->DirectGetValue());
}
void V8SetterCallback_Wrapper(v8::Local<v8::String> property,
v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<void>& info) {
const FXJSE_PROPERTY_DESCRIPTOR* lpPropertyInfo =
static_cast<FXJSE_PROPERTY_DESCRIPTOR*>(
info.Data().As<v8::External>()->Value());
if (!lpPropertyInfo)
return;
CFX_ByteStringC szPropertyName(lpPropertyInfo->name);
std::unique_ptr<CFXJSE_Value> lpThisValue(
new CFXJSE_Value(info.GetIsolate()));
std::unique_ptr<CFXJSE_Value> lpPropValue(
new CFXJSE_Value(info.GetIsolate()));
lpThisValue->ForceSetValue(info.This());
lpPropValue->ForceSetValue(value);
lpPropertyInfo->setProc(lpThisValue.get(), szPropertyName, lpPropValue.get());
}
void V8ConstructorCallback_Wrapper(
const v8::FunctionCallbackInfo<v8::Value>& info) {
if (!info.IsConstructCall())
return;
const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition =
static_cast<FXJSE_CLASS_DESCRIPTOR*>(
info.Data().As<v8::External>()->Value());
if (!lpClassDefinition)
return;
ASSERT(info.This()->InternalFieldCount());
info.This()->SetAlignedPointerInInternalField(0, nullptr);
}
void Context_GlobalObjToString(
const v8::FunctionCallbackInfo<v8::Value>& info) {
const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>(
info.Data().As<v8::External>()->Value());
if (!lpClass)
return;
if (info.This() == info.Holder() && lpClass->name) {
CFX_ByteString szStringVal;
szStringVal.Format("[object %s]", lpClass->name);
info.GetReturnValue().Set(v8::String::NewFromUtf8(
info.GetIsolate(), szStringVal.c_str(), v8::String::kNormalString,
szStringVal.GetLength()));
return;
}
v8::Local<v8::String> local_str =
info.This()
->ObjectProtoToString(info.GetIsolate()->GetCurrentContext())
.FromMaybe(v8::Local<v8::String>());
info.GetReturnValue().Set(local_str);
}
void DynPropGetterAdapter_MethodCallback(
const v8::FunctionCallbackInfo<v8::Value>& info) {
v8::Local<v8::Object> hCallBackInfo = info.Data().As<v8::Object>();
FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>(
hCallBackInfo->GetAlignedPointerFromInternalField(0));
v8::Local<v8::String> hPropName =
hCallBackInfo->GetInternalField(1).As<v8::String>();
ASSERT(lpClass && !hPropName.IsEmpty());
v8::String::Utf8Value szPropName(hPropName);
CFX_ByteStringC szFxPropName = *szPropName;
std::unique_ptr<CFXJSE_Value> lpThisValue(
new CFXJSE_Value(info.GetIsolate()));
lpThisValue->ForceSetValue(info.This());
std::unique_ptr<CFXJSE_Value> lpRetValue(new CFXJSE_Value(info.GetIsolate()));
CFXJSE_Arguments impl(&info, lpRetValue.get());
lpClass->dynMethodCall(lpThisValue.get(), szFxPropName, impl);
if (!lpRetValue->DirectGetValue().IsEmpty())
info.GetReturnValue().Set(lpRetValue->DirectGetValue());
}
void DynPropGetterAdapter(const FXJSE_CLASS_DESCRIPTOR* lpClass,
CFXJSE_Value* pObject,
const CFX_ByteStringC& szPropName,
CFXJSE_Value* pValue) {
ASSERT(lpClass);
int32_t nPropType =
lpClass->dynPropTypeGetter == nullptr
? FXJSE_ClassPropType_Property
: lpClass->dynPropTypeGetter(pObject, szPropName, false);
if (nPropType == FXJSE_ClassPropType_Property) {
if (lpClass->dynPropGetter)
lpClass->dynPropGetter(pObject, szPropName, pValue);
} else if (nPropType == FXJSE_ClassPropType_Method) {
if (lpClass->dynMethodCall && pValue) {
v8::Isolate* pIsolate = pValue->GetIsolate();
v8::HandleScope hscope(pIsolate);
v8::Local<v8::ObjectTemplate> hCallBackInfoTemplate =
v8::ObjectTemplate::New(pIsolate);
hCallBackInfoTemplate->SetInternalFieldCount(2);
v8::Local<v8::Object> hCallBackInfo =
hCallBackInfoTemplate->NewInstance();
hCallBackInfo->SetAlignedPointerInInternalField(
0, const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClass));
hCallBackInfo->SetInternalField(
1, v8::String::NewFromUtf8(
pIsolate, reinterpret_cast<const char*>(szPropName.raw_str()),
v8::String::kNormalString, szPropName.GetLength()));
pValue->ForceSetValue(
v8::Function::New(pValue->GetIsolate()->GetCurrentContext(),
DynPropGetterAdapter_MethodCallback, hCallBackInfo,
0, v8::ConstructorBehavior::kThrow)
.ToLocalChecked());
}
}
}
void DynPropSetterAdapter(const FXJSE_CLASS_DESCRIPTOR* lpClass,
CFXJSE_Value* pObject,
const CFX_ByteStringC& szPropName,
CFXJSE_Value* pValue) {
ASSERT(lpClass);
int32_t nPropType =
lpClass->dynPropTypeGetter == nullptr
? FXJSE_ClassPropType_Property
: lpClass->dynPropTypeGetter(pObject, szPropName, false);
if (nPropType != FXJSE_ClassPropType_Method) {
if (lpClass->dynPropSetter)
lpClass->dynPropSetter(pObject, szPropName, pValue);
}
}
bool DynPropQueryAdapter(const FXJSE_CLASS_DESCRIPTOR* lpClass,
CFXJSE_Value* pObject,
const CFX_ByteStringC& szPropName) {
ASSERT(lpClass);
int32_t nPropType =
lpClass->dynPropTypeGetter == nullptr
? FXJSE_ClassPropType_Property
: lpClass->dynPropTypeGetter(pObject, szPropName, true);
return nPropType != FXJSE_ClassPropType_None;
}
bool DynPropDeleterAdapter(const FXJSE_CLASS_DESCRIPTOR* lpClass,
CFXJSE_Value* pObject,
const CFX_ByteStringC& szPropName) {
ASSERT(lpClass);
int32_t nPropType =
lpClass->dynPropTypeGetter == nullptr
? FXJSE_ClassPropType_Property
: lpClass->dynPropTypeGetter(pObject, szPropName, false);
if (nPropType != FXJSE_ClassPropType_Method) {
if (lpClass->dynPropDeleter)
return lpClass->dynPropDeleter(pObject, szPropName);
return nPropType != FXJSE_ClassPropType_Property;
}
return false;
}
void NamedPropertyQueryCallback(
v8::Local<v8::Name> property,
const v8::PropertyCallbackInfo<v8::Integer>& info) {
v8::Local<v8::Object> thisObject = info.This();
const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>(
info.Data().As<v8::External>()->Value());
v8::Isolate* pIsolate = info.GetIsolate();
v8::HandleScope scope(pIsolate);
v8::String::Utf8Value szPropName(property);
CFX_ByteStringC szFxPropName(*szPropName, szPropName.length());
std::unique_ptr<CFXJSE_Value> lpThisValue(
new CFXJSE_Value(info.GetIsolate()));
lpThisValue->ForceSetValue(thisObject);
if (DynPropQueryAdapter(lpClass, lpThisValue.get(), szFxPropName)) {
info.GetReturnValue().Set(v8::DontDelete);
return;
}
const int32_t iV8Absent = 64;
info.GetReturnValue().Set(iV8Absent);
}
void NamedPropertyDeleterCallback(
v8::Local<v8::Name> property,
const v8::PropertyCallbackInfo<v8::Boolean>& info) {
v8::Local<v8::Object> thisObject = info.This();
const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>(
info.Data().As<v8::External>()->Value());
v8::Isolate* pIsolate = info.GetIsolate();
v8::HandleScope scope(pIsolate);
v8::String::Utf8Value szPropName(property);
CFX_ByteStringC szFxPropName(*szPropName, szPropName.length());
std::unique_ptr<CFXJSE_Value> lpThisValue(
new CFXJSE_Value(info.GetIsolate()));
lpThisValue->ForceSetValue(thisObject);
info.GetReturnValue().Set(
!!DynPropDeleterAdapter(lpClass, lpThisValue.get(), szFxPropName));
}
void NamedPropertyGetterCallback(
v8::Local<v8::Name> property,
const v8::PropertyCallbackInfo<v8::Value>& info) {
v8::Local<v8::Object> thisObject = info.This();
const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>(
info.Data().As<v8::External>()->Value());
v8::String::Utf8Value szPropName(property);
CFX_ByteStringC szFxPropName(*szPropName, szPropName.length());
std::unique_ptr<CFXJSE_Value> lpThisValue(
new CFXJSE_Value(info.GetIsolate()));
lpThisValue->ForceSetValue(thisObject);
std::unique_ptr<CFXJSE_Value> lpNewValue(new CFXJSE_Value(info.GetIsolate()));
DynPropGetterAdapter(lpClass, lpThisValue.get(), szFxPropName,
lpNewValue.get());
info.GetReturnValue().Set(lpNewValue->DirectGetValue());
}
void NamedPropertySetterCallback(
v8::Local<v8::Name> property,
v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<v8::Value>& info) {
v8::Local<v8::Object> thisObject = info.This();
const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>(
info.Data().As<v8::External>()->Value());
v8::String::Utf8Value szPropName(property);
CFX_ByteStringC szFxPropName(*szPropName, szPropName.length());
std::unique_ptr<CFXJSE_Value> lpThisValue(
new CFXJSE_Value(info.GetIsolate()));
lpThisValue->ForceSetValue(thisObject);
std::unique_ptr<CFXJSE_Value> lpNewValue(new CFXJSE_Value(info.GetIsolate()));
lpNewValue->ForceSetValue(value);
DynPropSetterAdapter(lpClass, lpThisValue.get(), szFxPropName,
lpNewValue.get());
info.GetReturnValue().Set(value);
}
void NamedPropertyEnumeratorCallback(
const v8::PropertyCallbackInfo<v8::Array>& info) {
const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast<FXJSE_CLASS_DESCRIPTOR*>(
info.Data().As<v8::External>()->Value());
v8::Isolate* pIsolate = info.GetIsolate();
v8::Local<v8::Array> newArray = v8::Array::New(pIsolate, lpClass->propNum);
for (int i = 0; i < lpClass->propNum; i++) {
newArray->Set(
i, v8::String::NewFromUtf8(pIsolate, lpClass->properties[i].name));
}
info.GetReturnValue().Set(newArray);
}
} // namespace
// static
CFXJSE_Class* CFXJSE_Class::Create(
CFXJSE_Context* lpContext,
const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition,
bool bIsJSGlobal) {
if (!lpContext || !lpClassDefinition)
return nullptr;
CFXJSE_Class* pClass =
GetClassFromContext(lpContext, lpClassDefinition->name);
if (pClass)
return pClass;
v8::Isolate* pIsolate = lpContext->m_pIsolate;
pClass = new CFXJSE_Class(lpContext);
pClass->m_szClassName = lpClassDefinition->name;
pClass->m_lpClassDefinition = lpClassDefinition;
CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
v8::Local<v8::FunctionTemplate> hFunctionTemplate = v8::FunctionTemplate::New(
pIsolate, bIsJSGlobal ? 0 : V8ConstructorCallback_Wrapper,
v8::External::New(
pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClassDefinition)));
hFunctionTemplate->SetClassName(
v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name));
hFunctionTemplate->InstanceTemplate()->SetInternalFieldCount(1);
v8::Local<v8::ObjectTemplate> hObjectTemplate =
hFunctionTemplate->InstanceTemplate();
SetUpNamedPropHandler(pIsolate, hObjectTemplate, lpClassDefinition);
if (lpClassDefinition->propNum) {
for (int32_t i = 0; i < lpClassDefinition->propNum; i++) {
hObjectTemplate->SetNativeDataProperty(
v8::String::NewFromUtf8(pIsolate,
lpClassDefinition->properties[i].name),
lpClassDefinition->properties[i].getProc ? V8GetterCallback_Wrapper
: nullptr,
lpClassDefinition->properties[i].setProc ? V8SetterCallback_Wrapper
: nullptr,
v8::External::New(pIsolate, const_cast<FXJSE_PROPERTY_DESCRIPTOR*>(
lpClassDefinition->properties + i)),
static_cast<v8::PropertyAttribute>(v8::DontDelete));
}
}
if (lpClassDefinition->methNum) {
for (int32_t i = 0; i < lpClassDefinition->methNum; i++) {
v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(
pIsolate, V8FunctionCallback_Wrapper,
v8::External::New(pIsolate, const_cast<FXJSE_FUNCTION_DESCRIPTOR*>(
lpClassDefinition->methods + i)));
fun->RemovePrototype();
hObjectTemplate->Set(
v8::String::NewFromUtf8(pIsolate, lpClassDefinition->methods[i].name),
fun,
static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));
}
}
if (lpClassDefinition->constructor) {
if (bIsJSGlobal) {
hObjectTemplate->Set(
v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name),
v8::FunctionTemplate::New(
pIsolate, V8ClassGlobalConstructorCallback_Wrapper,
v8::External::New(pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>(
lpClassDefinition))),
static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete));
} else {
v8::Local<v8::Context> hLocalContext =
v8::Local<v8::Context>::New(pIsolate, lpContext->m_hContext);
FXJSE_GetGlobalObjectFromContext(hLocalContext)
->Set(v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name),
v8::Function::New(
pIsolate, V8ClassGlobalConstructorCallback_Wrapper,
v8::External::New(pIsolate,
const_cast<FXJSE_CLASS_DESCRIPTOR*>(
lpClassDefinition))));
}
}
if (bIsJSGlobal) {
v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(
pIsolate, Context_GlobalObjToString,
v8::External::New(
pIsolate, const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClassDefinition)));
fun->RemovePrototype();
hObjectTemplate->Set(v8::String::NewFromUtf8(pIsolate, "toString"), fun);
}
pClass->m_hTemplate.Reset(lpContext->m_pIsolate, hFunctionTemplate);
lpContext->m_rgClasses.push_back(std::unique_ptr<CFXJSE_Class>(pClass));
return pClass;
}
// static
CFXJSE_Class* CFXJSE_Class::GetClassFromContext(CFXJSE_Context* pContext,
const CFX_ByteStringC& szName) {
for (const auto& pClass : pContext->m_rgClasses) {
if (pClass->m_szClassName == szName)
return pClass.get();
}
return nullptr;
}
// static
void CFXJSE_Class::SetUpNamedPropHandler(
v8::Isolate* pIsolate,
v8::Local<v8::ObjectTemplate>& hObjectTemplate,
const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition) {
v8::NamedPropertyHandlerConfiguration configuration(
lpClassDefinition->dynPropGetter ? NamedPropertyGetterCallback : 0,
lpClassDefinition->dynPropSetter ? NamedPropertySetterCallback : 0,
lpClassDefinition->dynPropTypeGetter ? NamedPropertyQueryCallback : 0,
lpClassDefinition->dynPropDeleter ? NamedPropertyDeleterCallback : 0,
NamedPropertyEnumeratorCallback,
v8::External::New(pIsolate,
const_cast<FXJSE_CLASS_DESCRIPTOR*>(lpClassDefinition)),
v8::PropertyHandlerFlags::kNonMasking);
hObjectTemplate->SetHandler(configuration);
}
CFXJSE_Class::CFXJSE_Class(CFXJSE_Context* lpContext)
: m_lpClassDefinition(nullptr), m_pContext(lpContext) {}
CFXJSE_Class::~CFXJSE_Class() {}
|