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

Side by Side Diff: fxjse/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: Add todo 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 | « fxjse/context.h ('k') | fxjse/dynprop.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 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 "fxjse/context.h"
8
9 #include "fxjse/include/cfxjse_class.h"
10 #include "fxjse/include/cfxjse_value.h"
11 #include "fxjse/scope_inline.h"
12
13 namespace {
14
15 const FX_CHAR szCompatibleModeScript[] =
16 "(function(global, list) {\n"
17 " 'use strict';\n"
18 " var objname;\n"
19 " for (objname in list) {\n"
20 " var globalobj = global[objname];\n"
21 " if (globalobj) {\n"
22 " list[objname].forEach(function(name) {\n"
23 " if (!globalobj[name]) {\n"
24 " Object.defineProperty(globalobj, name, {\n"
25 " writable: true,\n"
26 " enumerable: false,\n"
27 " value: (function(obj) {\n"
28 " if (arguments.length === 0) {\n"
29 " throw new TypeError('missing argument 0 when calling "
30 " function ' + objname + '.' + name);\n"
31 " }\n"
32 " return globalobj.prototype[name].apply(obj, "
33 " Array.prototype.slice.call(arguments, 1));\n"
34 " })\n"
35 " });\n"
36 " }\n"
37 " });\n"
38 " }\n"
39 " }\n"
40 "}(this, {String: ['substr', 'toUpperCase']}));";
41
42 } // namespace
43
44 v8::Local<v8::Object> FXJSE_GetGlobalObjectFromContext(
45 const v8::Local<v8::Context>& hContext) {
46 return hContext->Global()->GetPrototype().As<v8::Object>();
47 }
48
49 void FXJSE_UpdateObjectBinding(v8::Local<v8::Object>& hObject,
50 CFXJSE_HostObject* lpNewBinding) {
51 ASSERT(!hObject.IsEmpty());
52 ASSERT(hObject->InternalFieldCount() > 0);
53 hObject->SetAlignedPointerInInternalField(0,
54 static_cast<void*>(lpNewBinding));
55 }
56
57 CFXJSE_HostObject* FXJSE_RetrieveObjectBinding(
58 const v8::Local<v8::Object>& hJSObject,
59 CFXJSE_Class* lpClass) {
60 ASSERT(!hJSObject.IsEmpty());
61 if (!hJSObject->IsObject()) {
62 return nullptr;
63 }
64 v8::Local<v8::Object> hObject = hJSObject;
65 if (hObject->InternalFieldCount() == 0) {
66 v8::Local<v8::Value> hProtoObject = hObject->GetPrototype();
67 if (hProtoObject.IsEmpty() || !hProtoObject->IsObject()) {
68 return nullptr;
69 }
70 hObject = hProtoObject.As<v8::Object>();
71 if (hObject->InternalFieldCount() == 0) {
72 return nullptr;
73 }
74 }
75 if (lpClass) {
76 v8::Local<v8::FunctionTemplate> hClass =
77 v8::Local<v8::FunctionTemplate>::New(
78 lpClass->GetContext()->GetRuntime(), lpClass->GetTemplate());
79 if (!hClass->HasInstance(hObject)) {
80 return nullptr;
81 }
82 }
83 return static_cast<CFXJSE_HostObject*>(
84 hObject->GetAlignedPointerFromInternalField(0));
85 }
86
87 v8::Local<v8::Object> FXJSE_CreateReturnValue(v8::Isolate* pIsolate,
88 v8::TryCatch& trycatch) {
89 v8::Local<v8::Object> hReturnValue = v8::Object::New(pIsolate);
90 if (trycatch.HasCaught()) {
91 v8::Local<v8::Value> hException = trycatch.Exception();
92 v8::Local<v8::Message> hMessage = trycatch.Message();
93 if (hException->IsObject()) {
94 v8::Local<v8::Value> hValue;
95 hValue = hException.As<v8::Object>()->Get(
96 v8::String::NewFromUtf8(pIsolate, "name"));
97 if (hValue->IsString() || hValue->IsStringObject()) {
98 hReturnValue->Set(0, hValue);
99 } else {
100 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error"));
101 }
102 hValue = hException.As<v8::Object>()->Get(
103 v8::String::NewFromUtf8(pIsolate, "message"));
104 if (hValue->IsString() || hValue->IsStringObject()) {
105 hReturnValue->Set(1, hValue);
106 } else {
107 hReturnValue->Set(1, hMessage->Get());
108 }
109 } else {
110 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error"));
111 hReturnValue->Set(1, hMessage->Get());
112 }
113 hReturnValue->Set(2, hException);
114 hReturnValue->Set(3, v8::Integer::New(pIsolate, hMessage->GetLineNumber()));
115 hReturnValue->Set(4, hMessage->GetSourceLine());
116 v8::Maybe<int32_t> maybe_int =
117 hMessage->GetStartColumn(pIsolate->GetCurrentContext());
118 hReturnValue->Set(5, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0)));
119 maybe_int = hMessage->GetEndColumn(pIsolate->GetCurrentContext());
120 hReturnValue->Set(6, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0)));
121 }
122 return hReturnValue;
123 }
124
125 CFXJSE_Context* CFXJSE_Context::Create(
126 v8::Isolate* pIsolate,
127 const FXJSE_CLASS_DESCRIPTOR* lpGlobalClass,
128 CFXJSE_HostObject* lpGlobalObject) {
129 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
130 CFXJSE_Context* pContext = new CFXJSE_Context(pIsolate);
131 CFXJSE_Class* lpGlobalClassObj = NULL;
132 v8::Local<v8::ObjectTemplate> hObjectTemplate;
133 if (lpGlobalClass) {
134 lpGlobalClassObj = CFXJSE_Class::Create(pContext, lpGlobalClass, TRUE);
135 ASSERT(lpGlobalClassObj);
136 v8::Local<v8::FunctionTemplate> hFunctionTemplate =
137 v8::Local<v8::FunctionTemplate>::New(pIsolate,
138 lpGlobalClassObj->m_hTemplate);
139 hObjectTemplate = hFunctionTemplate->InstanceTemplate();
140 } else {
141 hObjectTemplate = v8::ObjectTemplate::New(pIsolate);
142 hObjectTemplate->SetInternalFieldCount(1);
143 }
144 hObjectTemplate->Set(
145 v8::Symbol::GetToStringTag(pIsolate),
146 v8::String::NewFromUtf8(pIsolate, "global", v8::NewStringType::kNormal)
147 .ToLocalChecked());
148 v8::Local<v8::Context> hNewContext =
149 v8::Context::New(pIsolate, NULL, hObjectTemplate);
150 v8::Local<v8::Context> hRootContext = v8::Local<v8::Context>::New(
151 pIsolate, CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext);
152 hNewContext->SetSecurityToken(hRootContext->GetSecurityToken());
153 v8::Local<v8::Object> hGlobalObject =
154 FXJSE_GetGlobalObjectFromContext(hNewContext);
155 FXJSE_UpdateObjectBinding(hGlobalObject, lpGlobalObject);
156 pContext->m_hContext.Reset(pIsolate, hNewContext);
157 return pContext;
158 }
159
160 CFXJSE_Context::CFXJSE_Context(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {}
161
162 CFXJSE_Context::~CFXJSE_Context() {}
163
164 std::unique_ptr<CFXJSE_Value> CFXJSE_Context::GetGlobalObject() {
165 std::unique_ptr<CFXJSE_Value> pValue(new CFXJSE_Value(m_pIsolate));
166
167 CFXJSE_ScopeUtil_IsolateHandleContext scope(this);
168 v8::Local<v8::Context> hContext =
169 v8::Local<v8::Context>::New(m_pIsolate, m_hContext);
170 v8::Local<v8::Object> hGlobalObject = hContext->Global();
171 pValue->ForceSetValue(hGlobalObject);
172
173 return pValue;
174 }
175
176 void CFXJSE_Context::EnableCompatibleMode() {
177 ExecuteScript(szCompatibleModeScript, nullptr, nullptr);
178 }
179
180 FX_BOOL CFXJSE_Context::ExecuteScript(const FX_CHAR* szScript,
181 CFXJSE_Value* lpRetValue,
182 CFXJSE_Value* lpNewThisObject) {
183 CFXJSE_ScopeUtil_IsolateHandleContext scope(this);
184 v8::TryCatch trycatch(m_pIsolate);
185 v8::Local<v8::String> hScriptString =
186 v8::String::NewFromUtf8(m_pIsolate, szScript);
187 if (lpNewThisObject == NULL) {
188 v8::Local<v8::Script> hScript = v8::Script::Compile(hScriptString);
189 if (!trycatch.HasCaught()) {
190 v8::Local<v8::Value> hValue = hScript->Run();
191 if (!trycatch.HasCaught()) {
192 if (lpRetValue) {
193 lpRetValue->m_hValue.Reset(m_pIsolate, hValue);
194 }
195 return TRUE;
196 }
197 }
198 if (lpRetValue) {
199 lpRetValue->m_hValue.Reset(m_pIsolate,
200 FXJSE_CreateReturnValue(m_pIsolate, trycatch));
201 }
202 return FALSE;
203 } else {
204 v8::Local<v8::Value> hNewThis =
205 v8::Local<v8::Value>::New(m_pIsolate, lpNewThisObject->m_hValue);
206 ASSERT(!hNewThis.IsEmpty());
207 v8::Local<v8::Script> hWrapper =
208 v8::Script::Compile(v8::String::NewFromUtf8(
209 m_pIsolate, "(function () { return eval(arguments[0]); })"));
210 v8::Local<v8::Value> hWrapperValue = hWrapper->Run();
211 ASSERT(hWrapperValue->IsFunction());
212 v8::Local<v8::Function> hWrapperFn = hWrapperValue.As<v8::Function>();
213 if (!trycatch.HasCaught()) {
214 v8::Local<v8::Value> rgArgs[] = {hScriptString};
215 v8::Local<v8::Value> hValue =
216 hWrapperFn->Call(hNewThis.As<v8::Object>(), 1, rgArgs);
217 if (!trycatch.HasCaught()) {
218 if (lpRetValue) {
219 lpRetValue->m_hValue.Reset(m_pIsolate, hValue);
220 }
221 return TRUE;
222 }
223 }
224 if (lpRetValue) {
225 lpRetValue->m_hValue.Reset(m_pIsolate,
226 FXJSE_CreateReturnValue(m_pIsolate, trycatch));
227 }
228 return FALSE;
229 }
230 }
OLDNEW
« no previous file with comments | « fxjse/context.h ('k') | fxjse/dynprop.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698