// 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/include/cfxjse_class.h" #include "fxjs/include/cfxjse_context.h" #include "fxjs/include/cfxjse_value.h" namespace { void V8FunctionCallback_Wrapper( const v8::FunctionCallbackInfo& info) { const FXJSE_FUNCTION_DESCRIPTOR* lpFunctionInfo = static_cast( info.Data().As()->Value()); if (!lpFunctionInfo) return; CFX_ByteStringC szFunctionName(lpFunctionInfo->name); std::unique_ptr lpThisValue( new CFXJSE_Value(info.GetIsolate())); lpThisValue->ForceSetValue(info.This()); std::unique_ptr 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& info) { const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition = static_cast( info.Data().As()->Value()); if (!lpClassDefinition) return; CFX_ByteStringC szFunctionName(lpClassDefinition->name); std::unique_ptr lpThisValue( new CFXJSE_Value(info.GetIsolate())); lpThisValue->ForceSetValue(info.This()); std::unique_ptr 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 property, const v8::PropertyCallbackInfo& info) { const FXJSE_PROPERTY_DESCRIPTOR* lpPropertyInfo = static_cast( info.Data().As()->Value()); if (!lpPropertyInfo) return; CFX_ByteStringC szPropertyName(lpPropertyInfo->name); std::unique_ptr lpThisValue( new CFXJSE_Value(info.GetIsolate())); std::unique_ptr 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 property, v8::Local value, const v8::PropertyCallbackInfo& info) { const FXJSE_PROPERTY_DESCRIPTOR* lpPropertyInfo = static_cast( info.Data().As()->Value()); if (!lpPropertyInfo) return; CFX_ByteStringC szPropertyName(lpPropertyInfo->name); std::unique_ptr lpThisValue( new CFXJSE_Value(info.GetIsolate())); std::unique_ptr 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& info) { if (!info.IsConstructCall()) return; const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition = static_cast( info.Data().As()->Value()); if (!lpClassDefinition) return; ASSERT(info.This()->InternalFieldCount()); info.This()->SetAlignedPointerInInternalField(0, nullptr); } void Context_GlobalObjToString( const v8::FunctionCallbackInfo& info) { const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast( info.Data().As()->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 local_str = info.This() ->ObjectProtoToString(info.GetIsolate()->GetCurrentContext()) .FromMaybe(v8::Local()); info.GetReturnValue().Set(local_str); } void DynPropGetterAdapter_MethodCallback( const v8::FunctionCallbackInfo& info) { v8::Local hCallBackInfo = info.Data().As(); FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast( hCallBackInfo->GetAlignedPointerFromInternalField(0)); v8::Local hPropName = hCallBackInfo->GetInternalField(1).As(); ASSERT(lpClass && !hPropName.IsEmpty()); v8::String::Utf8Value szPropName(hPropName); CFX_ByteStringC szFxPropName = *szPropName; std::unique_ptr lpThisValue( new CFXJSE_Value(info.GetIsolate())); lpThisValue->ForceSetValue(info.This()); std::unique_ptr 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 hCallBackInfoTemplate = v8::ObjectTemplate::New(pIsolate); hCallBackInfoTemplate->SetInternalFieldCount(2); v8::Local hCallBackInfo = hCallBackInfoTemplate->NewInstance(); hCallBackInfo->SetAlignedPointerInInternalField( 0, const_cast(lpClass)); hCallBackInfo->SetInternalField( 1, v8::String::NewFromUtf8( pIsolate, reinterpret_cast(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); } } FX_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; } FX_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 ? FALSE : TRUE; } return FALSE; } void NamedPropertyQueryCallback( v8::Local property, const v8::PropertyCallbackInfo& info) { v8::Local thisObject = info.This(); const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast( info.Data().As()->Value()); v8::Isolate* pIsolate = info.GetIsolate(); v8::HandleScope scope(pIsolate); v8::String::Utf8Value szPropName(property); CFX_ByteStringC szFxPropName(*szPropName, szPropName.length()); std::unique_ptr 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 property, const v8::PropertyCallbackInfo& info) { v8::Local thisObject = info.This(); const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast( info.Data().As()->Value()); v8::Isolate* pIsolate = info.GetIsolate(); v8::HandleScope scope(pIsolate); v8::String::Utf8Value szPropName(property); CFX_ByteStringC szFxPropName(*szPropName, szPropName.length()); std::unique_ptr lpThisValue( new CFXJSE_Value(info.GetIsolate())); lpThisValue->ForceSetValue(thisObject); info.GetReturnValue().Set( !!DynPropDeleterAdapter(lpClass, lpThisValue.get(), szFxPropName)); } void NamedPropertyGetterCallback( v8::Local property, const v8::PropertyCallbackInfo& info) { v8::Local thisObject = info.This(); const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast( info.Data().As()->Value()); v8::String::Utf8Value szPropName(property); CFX_ByteStringC szFxPropName(*szPropName, szPropName.length()); std::unique_ptr lpThisValue( new CFXJSE_Value(info.GetIsolate())); lpThisValue->ForceSetValue(thisObject); std::unique_ptr lpNewValue(new CFXJSE_Value(info.GetIsolate())); DynPropGetterAdapter(lpClass, lpThisValue.get(), szFxPropName, lpNewValue.get()); info.GetReturnValue().Set(lpNewValue->DirectGetValue()); } void NamedPropertySetterCallback( v8::Local property, v8::Local value, const v8::PropertyCallbackInfo& info) { v8::Local thisObject = info.This(); const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast( info.Data().As()->Value()); v8::String::Utf8Value szPropName(property); CFX_ByteStringC szFxPropName(*szPropName, szPropName.length()); std::unique_ptr lpThisValue( new CFXJSE_Value(info.GetIsolate())); lpThisValue->ForceSetValue(thisObject); CFXJSE_Value* lpNewValue = new CFXJSE_Value(info.GetIsolate()); lpNewValue->ForceSetValue(value); DynPropSetterAdapter(lpClass, lpThisValue.get(), szFxPropName, lpNewValue); info.GetReturnValue().Set(value); } void NamedPropertyEnumeratorCallback( const v8::PropertyCallbackInfo& info) { const FXJSE_CLASS_DESCRIPTOR* lpClass = static_cast( info.Data().As()->Value()); v8::Isolate* pIsolate = info.GetIsolate(); v8::Local 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, FX_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 hFunctionTemplate = v8::FunctionTemplate::New( pIsolate, bIsJSGlobal ? 0 : V8ConstructorCallback_Wrapper, v8::External::New( pIsolate, const_cast(lpClassDefinition))); hFunctionTemplate->SetClassName( v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name)); hFunctionTemplate->InstanceTemplate()->SetInternalFieldCount(1); v8::Local 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( lpClassDefinition->properties + i)), static_cast(v8::DontDelete)); } } if (lpClassDefinition->methNum) { for (int32_t i = 0; i < lpClassDefinition->methNum; i++) { v8::Local fun = v8::FunctionTemplate::New( pIsolate, V8FunctionCallback_Wrapper, v8::External::New(pIsolate, const_cast( lpClassDefinition->methods + i))); fun->RemovePrototype(); hObjectTemplate->Set( v8::String::NewFromUtf8(pIsolate, lpClassDefinition->methods[i].name), fun, static_cast(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( lpClassDefinition))), static_cast(v8::ReadOnly | v8::DontDelete)); } else { v8::Local hLocalContext = v8::Local::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( lpClassDefinition)))); } } if (bIsJSGlobal) { v8::Local fun = v8::FunctionTemplate::New( pIsolate, Context_GlobalObjToString, v8::External::New( pIsolate, const_cast(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(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& 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(lpClassDefinition)), v8::PropertyHandlerFlags::kNonMasking); hObjectTemplate->SetHandler(configuration); } CFXJSE_Class::CFXJSE_Class(CFXJSE_Context* lpContext) : m_lpClassDefinition(nullptr), m_pContext(lpContext) {} CFXJSE_Class::~CFXJSE_Class() {}