From 8bd9ce00a0755d72c9b5b8ad43d8f50c2f51e5c5 Mon Sep 17 00:00:00 2001 From: dsinclair Date: Thu, 9 Jun 2016 13:24:34 -0700 Subject: Move xfa/fxjse/ to fxjse/ This makes it clear that fxjse/ is a standalone component and should not be entangled with other xfa/ components. BUG=pdfium:506 Review-Url: https://codereview.chromium.org/2056663004 --- fxjse/class.cpp | 282 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 282 insertions(+) create mode 100644 fxjse/class.cpp (limited to 'fxjse/class.cpp') diff --git a/fxjse/class.cpp b/fxjse/class.cpp new file mode 100644 index 0000000000..bb133fba58 --- /dev/null +++ b/fxjse/class.cpp @@ -0,0 +1,282 @@ +// 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 "fxjse/include/cfxjse_class.h" + +#include "fxjse/context.h" +#include "fxjse/include/cfxjse_arguments.h" +#include "fxjse/include/cfxjse_value.h" +#include "fxjse/scope_inline.h" + +static void FXJSE_V8ConstructorCallback_Wrapper( + const v8::FunctionCallbackInfo& info); +static void FXJSE_V8FunctionCallback_Wrapper( + const v8::FunctionCallbackInfo& info); +static void FXJSE_V8GetterCallback_Wrapper( + v8::Local property, + const v8::PropertyCallbackInfo& info); +static void FXJSE_V8SetterCallback_Wrapper( + v8::Local property, + v8::Local value, + const v8::PropertyCallbackInfo& info); + +static void FXJSE_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()); + } +} + +static void FXJSE_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()); + } +} + +static void FXJSE_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()); +} + +static void FXJSE_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()); +} + +static void FXJSE_V8ConstructorCallback_Wrapper( + const v8::FunctionCallbackInfo& info) { + const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition = + static_cast( + info.Data().As()->Value()); + if (!lpClassDefinition) { + return; + } + ASSERT(info.This()->InternalFieldCount()); + info.This()->SetAlignedPointerInInternalField(0, NULL); +} + +v8::Isolate* CFXJSE_Arguments::GetRuntime() const { + return m_pRetValue->GetIsolate(); +} + +int32_t CFXJSE_Arguments::GetLength() const { + return m_pInfo->Length(); +} + +std::unique_ptr CFXJSE_Arguments::GetValue(int32_t index) const { + std::unique_ptr lpArgValue( + new CFXJSE_Value(v8::Isolate::GetCurrent())); + lpArgValue->ForceSetValue((*m_pInfo)[index]); + return lpArgValue; +} + +FX_BOOL CFXJSE_Arguments::GetBoolean(int32_t index) const { + return (*m_pInfo)[index]->BooleanValue(); +} + +int32_t CFXJSE_Arguments::GetInt32(int32_t index) const { + return static_cast((*m_pInfo)[index]->NumberValue()); +} + +FX_FLOAT CFXJSE_Arguments::GetFloat(int32_t index) const { + return static_cast((*m_pInfo)[index]->NumberValue()); +} + +CFX_ByteString CFXJSE_Arguments::GetUTF8String(int32_t index) const { + v8::Local hString = (*m_pInfo)[index]->ToString(); + v8::String::Utf8Value szStringVal(hString); + return CFX_ByteString(*szStringVal); +} + +CFXJSE_HostObject* CFXJSE_Arguments::GetObject(int32_t index, + CFXJSE_Class* pClass) const { + v8::Local hValue = (*m_pInfo)[index]; + ASSERT(!hValue.IsEmpty()); + if (!hValue->IsObject()) + return nullptr; + return FXJSE_RetrieveObjectBinding(hValue.As(), pClass); +} + +CFXJSE_Value* CFXJSE_Arguments::GetReturnValue() { + return m_pRetValue; +} + +static void FXJSE_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())); + } else { + v8::Local local_str = + info.This() + ->ObjectProtoToString(info.GetIsolate()->GetCurrentContext()) + .FromMaybe(v8::Local()); + info.GetReturnValue().Set(local_str); + } +} + +CFXJSE_Class* CFXJSE_Class::Create( + CFXJSE_Context* lpContext, + const FXJSE_CLASS_DESCRIPTOR* lpClassDefinition, + FX_BOOL bIsJSGlobal) { + if (!lpContext || !lpClassDefinition) { + return NULL; + } + 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 : FXJSE_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 + ? FXJSE_V8GetterCallback_Wrapper + : NULL, + lpClassDefinition->properties[i].setProc + ? FXJSE_V8SetterCallback_Wrapper + : NULL, + 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++) { + hObjectTemplate->Set( + v8::String::NewFromUtf8(pIsolate, lpClassDefinition->methods[i].name), + v8::FunctionTemplate::New( + pIsolate, FXJSE_V8FunctionCallback_Wrapper, + v8::External::New(pIsolate, + const_cast( + lpClassDefinition->methods + i))), + static_cast(v8::ReadOnly | v8::DontDelete)); + } + } + if (lpClassDefinition->constructor) { + if (bIsJSGlobal) { + hObjectTemplate->Set( + v8::String::NewFromUtf8(pIsolate, lpClassDefinition->name), + v8::FunctionTemplate::New( + pIsolate, FXJSE_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, FXJSE_V8ClassGlobalConstructorCallback_Wrapper, + v8::External::New(pIsolate, + const_cast( + lpClassDefinition)))); + } + } + if (bIsJSGlobal) { + hObjectTemplate->Set( + v8::String::NewFromUtf8(pIsolate, "toString"), + v8::FunctionTemplate::New( + pIsolate, FXJSE_Context_GlobalObjToString, + v8::External::New(pIsolate, const_cast( + lpClassDefinition)))); + } + pClass->m_hTemplate.Reset(lpContext->m_pIsolate, hFunctionTemplate); + lpContext->m_rgClasses.push_back(std::unique_ptr(pClass)); + return pClass; +} + +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; +} -- cgit v1.2.3