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/fxv8.h"
13 #include "fxjs/xfa/cfxjse_isolatetracker.h"
14 
15 CFXJSE_RuntimeData::CFXJSE_RuntimeData() = default;
16 
17 CFXJSE_RuntimeData::~CFXJSE_RuntimeData() = default;
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(new CFXJSE_RuntimeData());
22   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
23   v8::Local<v8::FunctionTemplate> hFuncTemplate =
24       v8::FunctionTemplate::New(pIsolate);
25 
26   v8::Local<v8::ObjectTemplate> hGlobalTemplate =
27       hFuncTemplate->InstanceTemplate();
28   hGlobalTemplate->Set(v8::Symbol::GetToStringTag(pIsolate),
29                        fxv8::NewStringHelper(pIsolate, "global"));
30 
31   v8::Local<v8::Context> hContext =
32       v8::Context::New(pIsolate, 0, hGlobalTemplate);
33 
34   ASSERT(hContext->Global()->InternalFieldCount() == 0);
35   ASSERT(hContext->Global()
36              ->GetPrototype()
37              .As<v8::Object>()
38              ->InternalFieldCount() == 0);
39 
40   hContext->SetSecurityToken(v8::External::New(pIsolate, pIsolate));
41   pRuntimeData->m_hRootContextGlobalTemplate.Reset(pIsolate, hFuncTemplate);
42   pRuntimeData->m_hRootContext.Reset(pIsolate, hContext);
43   return pRuntimeData;
44 }
45 
Get(v8::Isolate * pIsolate)46 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Get(v8::Isolate* pIsolate) {
47   FXJS_PerIsolateData::SetUp(pIsolate);
48 
49   FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate);
50   if (!pData->m_pFXJSERuntimeData)
51     pData->m_pFXJSERuntimeData = CFXJSE_RuntimeData::Create(pIsolate);
52   return static_cast<CFXJSE_RuntimeData*>(pData->m_pFXJSERuntimeData.get());
53 }
54