• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "fxjs/xfa/cfxjse_runtimedata.h"
8 
9 #include <utility>
10 
11 #include "fxjs/cfxjs_engine.h"
12 #include "fxjs/xfa/cfxjse_isolatetracker.h"
13 
14 CFXJSE_RuntimeData::CFXJSE_RuntimeData() = default;
15 
16 CFXJSE_RuntimeData::~CFXJSE_RuntimeData() = default;
17 
Create(v8::Isolate * pIsolate)18 std::unique_ptr<CFXJSE_RuntimeData> CFXJSE_RuntimeData::Create(
19     v8::Isolate* pIsolate) {
20   std::unique_ptr<CFXJSE_RuntimeData> pRuntimeData(new CFXJSE_RuntimeData());
21   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
22 
23   v8::Local<v8::FunctionTemplate> hFuncTemplate =
24       v8::FunctionTemplate::New(pIsolate);
25 
26   v8::Local<v8::ObjectTemplate> hGlobalTemplate =
27       hFuncTemplate->InstanceTemplate();
28   hGlobalTemplate->Set(
29       v8::Symbol::GetToStringTag(pIsolate),
30       v8::String::NewFromUtf8(pIsolate, "global", v8::NewStringType::kNormal)
31           .ToLocalChecked());
32 
33   v8::Local<v8::Context> hContext =
34       v8::Context::New(pIsolate, 0, hGlobalTemplate);
35 
36   ASSERT(hContext->Global()->InternalFieldCount() == 0);
37   ASSERT(hContext->Global()
38              ->GetPrototype()
39              .As<v8::Object>()
40              ->InternalFieldCount() == 0);
41 
42   hContext->SetSecurityToken(v8::External::New(pIsolate, pIsolate));
43   pRuntimeData->m_hRootContextGlobalTemplate.Reset(pIsolate, hFuncTemplate);
44   pRuntimeData->m_hRootContext.Reset(pIsolate, hContext);
45   return pRuntimeData;
46 }
47 
Get(v8::Isolate * pIsolate)48 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Get(v8::Isolate* pIsolate) {
49   FXJS_PerIsolateData::SetUp(pIsolate);
50 
51   FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate);
52   if (!pData->m_pFXJSERuntimeData)
53     pData->m_pFXJSERuntimeData = CFXJSE_RuntimeData::Create(pIsolate);
54   return static_cast<CFXJSE_RuntimeData*>(pData->m_pFXJSERuntimeData.get());
55 }
56