• 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/cfxjse_runtimedata.h"
8 
9 #include <utility>
10 
11 #include "fxjs/cfxjse_isolatetracker.h"
12 #include "fxjs/fxjs_v8.h"
13 
CFXJSE_RuntimeData(v8::Isolate * pIsolate)14 CFXJSE_RuntimeData::CFXJSE_RuntimeData(v8::Isolate* pIsolate)
15     : m_pIsolate(pIsolate) {}
16 
~CFXJSE_RuntimeData()17 CFXJSE_RuntimeData::~CFXJSE_RuntimeData() {}
18 
Create(v8::Isolate * pIsolate)19 std::unique_ptr<CFXJSE_RuntimeData> CFXJSE_RuntimeData::Create(
20     v8::Isolate* pIsolate) {
21   std::unique_ptr<CFXJSE_RuntimeData> pRuntimeData(
22       new CFXJSE_RuntimeData(pIsolate));
23   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
24 
25   v8::Local<v8::FunctionTemplate> hFuncTemplate =
26       v8::FunctionTemplate::New(pIsolate);
27   v8::Local<v8::ObjectTemplate> hGlobalTemplate =
28       hFuncTemplate->InstanceTemplate();
29   hGlobalTemplate->Set(
30       v8::Symbol::GetToStringTag(pIsolate),
31       v8::String::NewFromUtf8(pIsolate, "global", v8::NewStringType::kNormal)
32           .ToLocalChecked());
33 
34   v8::Local<v8::Context> hContext =
35       v8::Context::New(pIsolate, 0, hGlobalTemplate);
36   hContext->SetSecurityToken(v8::External::New(pIsolate, pIsolate));
37 
38   pRuntimeData->m_hRootContextGlobalTemplate.Reset(pIsolate, hFuncTemplate);
39   pRuntimeData->m_hRootContext.Reset(pIsolate, hContext);
40   return pRuntimeData;
41 }
42 
Get(v8::Isolate * pIsolate)43 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Get(v8::Isolate* pIsolate) {
44   FXJS_PerIsolateData::SetUp(pIsolate);
45 
46   FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate);
47   if (!pData->m_pFXJSERuntimeData)
48     pData->m_pFXJSERuntimeData = CFXJSE_RuntimeData::Create(pIsolate);
49   return pData->m_pFXJSERuntimeData.get();
50 }
51