Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Side by Side Diff: fxjs/cfxjse_context.cpp

Issue 2136213002: Rename fxjse/ to fxjs/ update files to match class names. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Similarity level.... Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fxjs/cfxjse_class.cpp ('k') | fxjs/cfxjse_isolatetracker.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "fxjse/context.h" 7 #include "fxjs/include/cfxjse_context.h"
8 8
9 #include "fxjse/include/cfxjse_class.h" 9 #include "fxjs/include/cfxjse_class.h"
10 #include "fxjse/include/cfxjse_value.h" 10 #include "fxjs/include/cfxjse_value.h"
11 #include "fxjse/scope_inline.h"
12 11
13 namespace { 12 namespace {
14 13
15 const FX_CHAR szCompatibleModeScript[] = 14 const FX_CHAR szCompatibleModeScript[] =
16 "(function(global, list) {\n" 15 "(function(global, list) {\n"
17 " 'use strict';\n" 16 " 'use strict';\n"
18 " var objname;\n" 17 " var objname;\n"
19 " for (objname in list) {\n" 18 " for (objname in list) {\n"
20 " var globalobj = global[objname];\n" 19 " var globalobj = global[objname];\n"
21 " if (globalobj) {\n" 20 " if (globalobj) {\n"
(...skipping 12 matching lines...) Expand all
34 " })\n" 33 " })\n"
35 " });\n" 34 " });\n"
36 " }\n" 35 " }\n"
37 " });\n" 36 " });\n"
38 " }\n" 37 " }\n"
39 " }\n" 38 " }\n"
40 "}(this, {String: ['substr', 'toUpperCase']}));"; 39 "}(this, {String: ['substr', 'toUpperCase']}));";
41 40
42 } // namespace 41 } // namespace
43 42
43 // Note, not in the anonymous namespace due to the friend call
44 // in cfxjse_context.h
Tom Sepez 2016/07/12 16:35:37 nit: add a todo to remove the friending.
dsinclair 2016/07/12 17:22:20 Done.
45 class CFXJSE_ScopeUtil_IsolateHandleContext {
46 public:
47 explicit CFXJSE_ScopeUtil_IsolateHandleContext(CFXJSE_Context* pContext)
48 : m_context(pContext),
49 m_parent(pContext->m_pIsolate),
50 m_cscope(v8::Local<v8::Context>::New(pContext->m_pIsolate,
51 pContext->m_hContext)) {}
52 v8::Isolate* GetIsolate() { return m_context->m_pIsolate; }
53 v8::Local<v8::Context> GetLocalContext() {
54 return v8::Local<v8::Context>::New(m_context->m_pIsolate,
55 m_context->m_hContext);
56 }
57
58 private:
59 CFXJSE_ScopeUtil_IsolateHandleContext(
60 const CFXJSE_ScopeUtil_IsolateHandleContext&) = delete;
61 void operator=(const CFXJSE_ScopeUtil_IsolateHandleContext&) = delete;
62 void* operator new(size_t size) = delete;
63 void operator delete(void*, size_t) = delete;
64
65 CFXJSE_Context* m_context;
66 CFXJSE_ScopeUtil_IsolateHandle m_parent;
67 v8::Context::Scope m_cscope;
68 };
69
44 v8::Local<v8::Object> FXJSE_GetGlobalObjectFromContext( 70 v8::Local<v8::Object> FXJSE_GetGlobalObjectFromContext(
45 const v8::Local<v8::Context>& hContext) { 71 const v8::Local<v8::Context>& hContext) {
46 return hContext->Global()->GetPrototype().As<v8::Object>(); 72 return hContext->Global()->GetPrototype().As<v8::Object>();
47 } 73 }
48 74
49 void FXJSE_UpdateObjectBinding(v8::Local<v8::Object>& hObject, 75 void FXJSE_UpdateObjectBinding(v8::Local<v8::Object>& hObject,
50 CFXJSE_HostObject* lpNewBinding) { 76 CFXJSE_HostObject* lpNewBinding) {
51 ASSERT(!hObject.IsEmpty()); 77 ASSERT(!hObject.IsEmpty());
52 ASSERT(hObject->InternalFieldCount() > 0); 78 ASSERT(hObject->InternalFieldCount() > 0);
53 hObject->SetAlignedPointerInInternalField(0, 79 hObject->SetAlignedPointerInInternalField(0,
54 static_cast<void*>(lpNewBinding)); 80 static_cast<void*>(lpNewBinding));
55 } 81 }
56 82
57 CFXJSE_HostObject* FXJSE_RetrieveObjectBinding( 83 CFXJSE_HostObject* FXJSE_RetrieveObjectBinding(
58 const v8::Local<v8::Object>& hJSObject, 84 const v8::Local<v8::Object>& hJSObject,
59 CFXJSE_Class* lpClass) { 85 CFXJSE_Class* lpClass) {
60 ASSERT(!hJSObject.IsEmpty()); 86 ASSERT(!hJSObject.IsEmpty());
61 if (!hJSObject->IsObject()) { 87 if (!hJSObject->IsObject())
62 return nullptr; 88 return nullptr;
63 } 89
64 v8::Local<v8::Object> hObject = hJSObject; 90 v8::Local<v8::Object> hObject = hJSObject;
65 if (hObject->InternalFieldCount() == 0) { 91 if (hObject->InternalFieldCount() == 0) {
66 v8::Local<v8::Value> hProtoObject = hObject->GetPrototype(); 92 v8::Local<v8::Value> hProtoObject = hObject->GetPrototype();
67 if (hProtoObject.IsEmpty() || !hProtoObject->IsObject()) { 93 if (hProtoObject.IsEmpty() || !hProtoObject->IsObject())
68 return nullptr; 94 return nullptr;
69 } 95
70 hObject = hProtoObject.As<v8::Object>(); 96 hObject = hProtoObject.As<v8::Object>();
71 if (hObject->InternalFieldCount() == 0) { 97 if (hObject->InternalFieldCount() == 0)
72 return nullptr; 98 return nullptr;
73 }
74 } 99 }
75 if (lpClass) { 100 if (lpClass) {
76 v8::Local<v8::FunctionTemplate> hClass = 101 v8::Local<v8::FunctionTemplate> hClass =
77 v8::Local<v8::FunctionTemplate>::New( 102 v8::Local<v8::FunctionTemplate>::New(
78 lpClass->GetContext()->GetRuntime(), lpClass->GetTemplate()); 103 lpClass->GetContext()->GetRuntime(), lpClass->GetTemplate());
79 if (!hClass->HasInstance(hObject)) { 104 if (!hClass->HasInstance(hObject))
80 return nullptr; 105 return nullptr;
81 }
82 } 106 }
83 return static_cast<CFXJSE_HostObject*>( 107 return static_cast<CFXJSE_HostObject*>(
84 hObject->GetAlignedPointerFromInternalField(0)); 108 hObject->GetAlignedPointerFromInternalField(0));
85 } 109 }
86 110
87 v8::Local<v8::Object> FXJSE_CreateReturnValue(v8::Isolate* pIsolate, 111 v8::Local<v8::Object> FXJSE_CreateReturnValue(v8::Isolate* pIsolate,
88 v8::TryCatch& trycatch) { 112 v8::TryCatch& trycatch) {
89 v8::Local<v8::Object> hReturnValue = v8::Object::New(pIsolate); 113 v8::Local<v8::Object> hReturnValue = v8::Object::New(pIsolate);
90 if (trycatch.HasCaught()) { 114 if (trycatch.HasCaught()) {
91 v8::Local<v8::Value> hException = trycatch.Exception(); 115 v8::Local<v8::Value> hException = trycatch.Exception();
92 v8::Local<v8::Message> hMessage = trycatch.Message(); 116 v8::Local<v8::Message> hMessage = trycatch.Message();
93 if (hException->IsObject()) { 117 if (hException->IsObject()) {
94 v8::Local<v8::Value> hValue; 118 v8::Local<v8::Value> hValue;
95 hValue = hException.As<v8::Object>()->Get( 119 hValue = hException.As<v8::Object>()->Get(
96 v8::String::NewFromUtf8(pIsolate, "name")); 120 v8::String::NewFromUtf8(pIsolate, "name"));
97 if (hValue->IsString() || hValue->IsStringObject()) { 121 if (hValue->IsString() || hValue->IsStringObject())
98 hReturnValue->Set(0, hValue); 122 hReturnValue->Set(0, hValue);
99 } else { 123 else
100 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error")); 124 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error"));
101 } 125
102 hValue = hException.As<v8::Object>()->Get( 126 hValue = hException.As<v8::Object>()->Get(
103 v8::String::NewFromUtf8(pIsolate, "message")); 127 v8::String::NewFromUtf8(pIsolate, "message"));
104 if (hValue->IsString() || hValue->IsStringObject()) { 128 if (hValue->IsString() || hValue->IsStringObject())
105 hReturnValue->Set(1, hValue); 129 hReturnValue->Set(1, hValue);
106 } else { 130 else
107 hReturnValue->Set(1, hMessage->Get()); 131 hReturnValue->Set(1, hMessage->Get());
108 }
109 } else { 132 } else {
110 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error")); 133 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error"));
111 hReturnValue->Set(1, hMessage->Get()); 134 hReturnValue->Set(1, hMessage->Get());
112 } 135 }
113 hReturnValue->Set(2, hException); 136 hReturnValue->Set(2, hException);
114 hReturnValue->Set(3, v8::Integer::New(pIsolate, hMessage->GetLineNumber())); 137 hReturnValue->Set(3, v8::Integer::New(pIsolate, hMessage->GetLineNumber()));
115 hReturnValue->Set(4, hMessage->GetSourceLine()); 138 hReturnValue->Set(4, hMessage->GetSourceLine());
116 v8::Maybe<int32_t> maybe_int = 139 v8::Maybe<int32_t> maybe_int =
117 hMessage->GetStartColumn(pIsolate->GetCurrentContext()); 140 hMessage->GetStartColumn(pIsolate->GetCurrentContext());
118 hReturnValue->Set(5, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0))); 141 hReturnValue->Set(5, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0)));
119 maybe_int = hMessage->GetEndColumn(pIsolate->GetCurrentContext()); 142 maybe_int = hMessage->GetEndColumn(pIsolate->GetCurrentContext());
120 hReturnValue->Set(6, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0))); 143 hReturnValue->Set(6, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0)));
121 } 144 }
122 return hReturnValue; 145 return hReturnValue;
123 } 146 }
124 147
148 // static
125 CFXJSE_Context* CFXJSE_Context::Create( 149 CFXJSE_Context* CFXJSE_Context::Create(
126 v8::Isolate* pIsolate, 150 v8::Isolate* pIsolate,
127 const FXJSE_CLASS_DESCRIPTOR* lpGlobalClass, 151 const FXJSE_CLASS_DESCRIPTOR* lpGlobalClass,
128 CFXJSE_HostObject* lpGlobalObject) { 152 CFXJSE_HostObject* lpGlobalObject) {
129 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate); 153 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
130 CFXJSE_Context* pContext = new CFXJSE_Context(pIsolate); 154 CFXJSE_Context* pContext = new CFXJSE_Context(pIsolate);
131 CFXJSE_Class* lpGlobalClassObj = NULL; 155 CFXJSE_Class* lpGlobalClassObj = nullptr;
132 v8::Local<v8::ObjectTemplate> hObjectTemplate; 156 v8::Local<v8::ObjectTemplate> hObjectTemplate;
133 if (lpGlobalClass) { 157 if (lpGlobalClass) {
134 lpGlobalClassObj = CFXJSE_Class::Create(pContext, lpGlobalClass, TRUE); 158 lpGlobalClassObj = CFXJSE_Class::Create(pContext, lpGlobalClass, TRUE);
135 ASSERT(lpGlobalClassObj); 159 ASSERT(lpGlobalClassObj);
136 v8::Local<v8::FunctionTemplate> hFunctionTemplate = 160 v8::Local<v8::FunctionTemplate> hFunctionTemplate =
137 v8::Local<v8::FunctionTemplate>::New(pIsolate, 161 v8::Local<v8::FunctionTemplate>::New(pIsolate,
138 lpGlobalClassObj->m_hTemplate); 162 lpGlobalClassObj->m_hTemplate);
139 hObjectTemplate = hFunctionTemplate->InstanceTemplate(); 163 hObjectTemplate = hFunctionTemplate->InstanceTemplate();
140 } else { 164 } else {
141 hObjectTemplate = v8::ObjectTemplate::New(pIsolate); 165 hObjectTemplate = v8::ObjectTemplate::New(pIsolate);
142 hObjectTemplate->SetInternalFieldCount(1); 166 hObjectTemplate->SetInternalFieldCount(1);
143 } 167 }
144 hObjectTemplate->Set( 168 hObjectTemplate->Set(
145 v8::Symbol::GetToStringTag(pIsolate), 169 v8::Symbol::GetToStringTag(pIsolate),
146 v8::String::NewFromUtf8(pIsolate, "global", v8::NewStringType::kNormal) 170 v8::String::NewFromUtf8(pIsolate, "global", v8::NewStringType::kNormal)
147 .ToLocalChecked()); 171 .ToLocalChecked());
148 v8::Local<v8::Context> hNewContext = 172 v8::Local<v8::Context> hNewContext =
149 v8::Context::New(pIsolate, NULL, hObjectTemplate); 173 v8::Context::New(pIsolate, nullptr, hObjectTemplate);
150 v8::Local<v8::Context> hRootContext = v8::Local<v8::Context>::New( 174 v8::Local<v8::Context> hRootContext = v8::Local<v8::Context>::New(
151 pIsolate, CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext); 175 pIsolate, CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext);
152 hNewContext->SetSecurityToken(hRootContext->GetSecurityToken()); 176 hNewContext->SetSecurityToken(hRootContext->GetSecurityToken());
153 v8::Local<v8::Object> hGlobalObject = 177 v8::Local<v8::Object> hGlobalObject =
154 FXJSE_GetGlobalObjectFromContext(hNewContext); 178 FXJSE_GetGlobalObjectFromContext(hNewContext);
155 FXJSE_UpdateObjectBinding(hGlobalObject, lpGlobalObject); 179 FXJSE_UpdateObjectBinding(hGlobalObject, lpGlobalObject);
156 pContext->m_hContext.Reset(pIsolate, hNewContext); 180 pContext->m_hContext.Reset(pIsolate, hNewContext);
157 return pContext; 181 return pContext;
158 } 182 }
159 183
(...skipping 17 matching lines...) Expand all
177 ExecuteScript(szCompatibleModeScript, nullptr, nullptr); 201 ExecuteScript(szCompatibleModeScript, nullptr, nullptr);
178 } 202 }
179 203
180 FX_BOOL CFXJSE_Context::ExecuteScript(const FX_CHAR* szScript, 204 FX_BOOL CFXJSE_Context::ExecuteScript(const FX_CHAR* szScript,
181 CFXJSE_Value* lpRetValue, 205 CFXJSE_Value* lpRetValue,
182 CFXJSE_Value* lpNewThisObject) { 206 CFXJSE_Value* lpNewThisObject) {
183 CFXJSE_ScopeUtil_IsolateHandleContext scope(this); 207 CFXJSE_ScopeUtil_IsolateHandleContext scope(this);
184 v8::TryCatch trycatch(m_pIsolate); 208 v8::TryCatch trycatch(m_pIsolate);
185 v8::Local<v8::String> hScriptString = 209 v8::Local<v8::String> hScriptString =
186 v8::String::NewFromUtf8(m_pIsolate, szScript); 210 v8::String::NewFromUtf8(m_pIsolate, szScript);
187 if (lpNewThisObject == NULL) { 211 if (!lpNewThisObject) {
188 v8::Local<v8::Script> hScript = v8::Script::Compile(hScriptString); 212 v8::Local<v8::Script> hScript = v8::Script::Compile(hScriptString);
189 if (!trycatch.HasCaught()) { 213 if (!trycatch.HasCaught()) {
190 v8::Local<v8::Value> hValue = hScript->Run(); 214 v8::Local<v8::Value> hValue = hScript->Run();
191 if (!trycatch.HasCaught()) { 215 if (!trycatch.HasCaught()) {
192 if (lpRetValue) { 216 if (lpRetValue) {
193 lpRetValue->m_hValue.Reset(m_pIsolate, hValue); 217 lpRetValue->m_hValue.Reset(m_pIsolate, hValue);
194 } 218 }
195 return TRUE; 219 return TRUE;
196 } 220 }
197 } 221 }
(...skipping 23 matching lines...) Expand all
221 return TRUE; 245 return TRUE;
222 } 246 }
223 } 247 }
224 if (lpRetValue) { 248 if (lpRetValue) {
225 lpRetValue->m_hValue.Reset(m_pIsolate, 249 lpRetValue->m_hValue.Reset(m_pIsolate,
226 FXJSE_CreateReturnValue(m_pIsolate, trycatch)); 250 FXJSE_CreateReturnValue(m_pIsolate, trycatch));
227 } 251 }
228 return FALSE; 252 return FALSE;
229 } 253 }
230 } 254 }
OLDNEW
« no previous file with comments | « fxjs/cfxjse_class.cpp ('k') | fxjs/cfxjse_isolatetracker.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698