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 "core/fxcrt/check_op.h"
12 #include "fxjs/cfxjs_engine.h"
13 #include "fxjs/fxv8.h"
14 #include "fxjs/xfa/cfxjse_isolatetracker.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, nullptr, 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->root_context_global_template_.Reset(pIsolate, hFuncTemplate);
48 pRuntimeData->root_context_.Reset(pIsolate, hContext);
49 return pRuntimeData;
50 }
51
Get(v8::Isolate * pIsolate)52 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Get(v8::Isolate* pIsolate) {
53 CFXJS_PerIsolateData::SetUp(pIsolate);
54 CFXJS_PerIsolateData* pData = CFXJS_PerIsolateData::Get(pIsolate);
55 if (!pData->GetExtension())
56 pData->SetExtension(CFXJSE_RuntimeData::Create(pIsolate));
57 return static_cast<CFXJSE_RuntimeData*>(pData->GetExtension());
58 }
59
GetRootContext(v8::Isolate * pIsolate)60 v8::Local<v8::Context> CFXJSE_RuntimeData::GetRootContext(
61 v8::Isolate* pIsolate) {
62 return v8::Local<v8::Context>::New(pIsolate, root_context_);
63 }
64