• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The PDFium Authors
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 #include "third_party/base/check_op.h"
15 #include "v8/include/v8-context.h"
16 #include "v8/include/v8-external.h"
17 #include "v8/include/v8-isolate.h"
18 #include "v8/include/v8-object.h"
19 #include "v8/include/v8-primitive.h"
20 #include "v8/include/v8-template.h"
21 
22 CFXJSE_RuntimeData::CFXJSE_RuntimeData() = default;
23 
24 CFXJSE_RuntimeData::~CFXJSE_RuntimeData() = default;
25 
Create(v8::Isolate * pIsolate)26 std::unique_ptr<CFXJSE_RuntimeData> CFXJSE_RuntimeData::Create(
27     v8::Isolate* pIsolate) {
28   std::unique_ptr<CFXJSE_RuntimeData> pRuntimeData(new CFXJSE_RuntimeData());
29   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
30   v8::Local<v8::FunctionTemplate> hFuncTemplate =
31       v8::FunctionTemplate::New(pIsolate);
32 
33   v8::Local<v8::ObjectTemplate> hGlobalTemplate =
34       hFuncTemplate->InstanceTemplate();
35   hGlobalTemplate->Set(v8::Symbol::GetToStringTag(pIsolate),
36                        fxv8::NewStringHelper(pIsolate, "global"));
37 
38   v8::Local<v8::Context> hContext =
39       v8::Context::New(pIsolate, 0, hGlobalTemplate);
40 
41   DCHECK_EQ(hContext->Global()->InternalFieldCount(), 0);
42   DCHECK_EQ(
43       hContext->Global()->GetPrototype().As<v8::Object>()->InternalFieldCount(),
44       0);
45 
46   hContext->SetSecurityToken(v8::External::New(pIsolate, pIsolate));
47   pRuntimeData->m_hRootContextGlobalTemplate.Reset(pIsolate, hFuncTemplate);
48   pRuntimeData->m_hRootContext.Reset(pIsolate, hContext);
49   return pRuntimeData;
50 }
51 
Get(v8::Isolate * pIsolate)52 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Get(v8::Isolate* pIsolate) {
53   FXJS_PerIsolateData::SetUp(pIsolate);
54   FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate);
55   if (!pData->GetExtension())
56     pData->SetExtension(CFXJSE_RuntimeData::Create(pIsolate));
57   return static_cast<CFXJSE_RuntimeData*>(pData->GetExtension());
58 }
59