OLD | NEW |
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 Loading... |
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 |
| 45 // TODO(dsinclair): Remove the friending, use public methods. |
| 46 class CFXJSE_ScopeUtil_IsolateHandleContext { |
| 47 public: |
| 48 explicit CFXJSE_ScopeUtil_IsolateHandleContext(CFXJSE_Context* pContext) |
| 49 : m_context(pContext), |
| 50 m_parent(pContext->m_pIsolate), |
| 51 m_cscope(v8::Local<v8::Context>::New(pContext->m_pIsolate, |
| 52 pContext->m_hContext)) {} |
| 53 v8::Isolate* GetIsolate() { return m_context->m_pIsolate; } |
| 54 v8::Local<v8::Context> GetLocalContext() { |
| 55 return v8::Local<v8::Context>::New(m_context->m_pIsolate, |
| 56 m_context->m_hContext); |
| 57 } |
| 58 |
| 59 private: |
| 60 CFXJSE_ScopeUtil_IsolateHandleContext( |
| 61 const CFXJSE_ScopeUtil_IsolateHandleContext&) = delete; |
| 62 void operator=(const CFXJSE_ScopeUtil_IsolateHandleContext&) = delete; |
| 63 void* operator new(size_t size) = delete; |
| 64 void operator delete(void*, size_t) = delete; |
| 65 |
| 66 CFXJSE_Context* m_context; |
| 67 CFXJSE_ScopeUtil_IsolateHandle m_parent; |
| 68 v8::Context::Scope m_cscope; |
| 69 }; |
| 70 |
44 v8::Local<v8::Object> FXJSE_GetGlobalObjectFromContext( | 71 v8::Local<v8::Object> FXJSE_GetGlobalObjectFromContext( |
45 const v8::Local<v8::Context>& hContext) { | 72 const v8::Local<v8::Context>& hContext) { |
46 return hContext->Global()->GetPrototype().As<v8::Object>(); | 73 return hContext->Global()->GetPrototype().As<v8::Object>(); |
47 } | 74 } |
48 | 75 |
49 void FXJSE_UpdateObjectBinding(v8::Local<v8::Object>& hObject, | 76 void FXJSE_UpdateObjectBinding(v8::Local<v8::Object>& hObject, |
50 CFXJSE_HostObject* lpNewBinding) { | 77 CFXJSE_HostObject* lpNewBinding) { |
51 ASSERT(!hObject.IsEmpty()); | 78 ASSERT(!hObject.IsEmpty()); |
52 ASSERT(hObject->InternalFieldCount() > 0); | 79 ASSERT(hObject->InternalFieldCount() > 0); |
53 hObject->SetAlignedPointerInInternalField(0, | 80 hObject->SetAlignedPointerInInternalField(0, |
54 static_cast<void*>(lpNewBinding)); | 81 static_cast<void*>(lpNewBinding)); |
55 } | 82 } |
56 | 83 |
57 CFXJSE_HostObject* FXJSE_RetrieveObjectBinding( | 84 CFXJSE_HostObject* FXJSE_RetrieveObjectBinding( |
58 const v8::Local<v8::Object>& hJSObject, | 85 const v8::Local<v8::Object>& hJSObject, |
59 CFXJSE_Class* lpClass) { | 86 CFXJSE_Class* lpClass) { |
60 ASSERT(!hJSObject.IsEmpty()); | 87 ASSERT(!hJSObject.IsEmpty()); |
61 if (!hJSObject->IsObject()) { | 88 if (!hJSObject->IsObject()) |
62 return nullptr; | 89 return nullptr; |
63 } | 90 |
64 v8::Local<v8::Object> hObject = hJSObject; | 91 v8::Local<v8::Object> hObject = hJSObject; |
65 if (hObject->InternalFieldCount() == 0) { | 92 if (hObject->InternalFieldCount() == 0) { |
66 v8::Local<v8::Value> hProtoObject = hObject->GetPrototype(); | 93 v8::Local<v8::Value> hProtoObject = hObject->GetPrototype(); |
67 if (hProtoObject.IsEmpty() || !hProtoObject->IsObject()) { | 94 if (hProtoObject.IsEmpty() || !hProtoObject->IsObject()) |
68 return nullptr; | 95 return nullptr; |
69 } | 96 |
70 hObject = hProtoObject.As<v8::Object>(); | 97 hObject = hProtoObject.As<v8::Object>(); |
71 if (hObject->InternalFieldCount() == 0) { | 98 if (hObject->InternalFieldCount() == 0) |
72 return nullptr; | 99 return nullptr; |
73 } | |
74 } | 100 } |
75 if (lpClass) { | 101 if (lpClass) { |
76 v8::Local<v8::FunctionTemplate> hClass = | 102 v8::Local<v8::FunctionTemplate> hClass = |
77 v8::Local<v8::FunctionTemplate>::New( | 103 v8::Local<v8::FunctionTemplate>::New( |
78 lpClass->GetContext()->GetRuntime(), lpClass->GetTemplate()); | 104 lpClass->GetContext()->GetRuntime(), lpClass->GetTemplate()); |
79 if (!hClass->HasInstance(hObject)) { | 105 if (!hClass->HasInstance(hObject)) |
80 return nullptr; | 106 return nullptr; |
81 } | |
82 } | 107 } |
83 return static_cast<CFXJSE_HostObject*>( | 108 return static_cast<CFXJSE_HostObject*>( |
84 hObject->GetAlignedPointerFromInternalField(0)); | 109 hObject->GetAlignedPointerFromInternalField(0)); |
85 } | 110 } |
86 | 111 |
87 v8::Local<v8::Object> FXJSE_CreateReturnValue(v8::Isolate* pIsolate, | 112 v8::Local<v8::Object> FXJSE_CreateReturnValue(v8::Isolate* pIsolate, |
88 v8::TryCatch& trycatch) { | 113 v8::TryCatch& trycatch) { |
89 v8::Local<v8::Object> hReturnValue = v8::Object::New(pIsolate); | 114 v8::Local<v8::Object> hReturnValue = v8::Object::New(pIsolate); |
90 if (trycatch.HasCaught()) { | 115 if (trycatch.HasCaught()) { |
91 v8::Local<v8::Value> hException = trycatch.Exception(); | 116 v8::Local<v8::Value> hException = trycatch.Exception(); |
92 v8::Local<v8::Message> hMessage = trycatch.Message(); | 117 v8::Local<v8::Message> hMessage = trycatch.Message(); |
93 if (hException->IsObject()) { | 118 if (hException->IsObject()) { |
94 v8::Local<v8::Value> hValue; | 119 v8::Local<v8::Value> hValue; |
95 hValue = hException.As<v8::Object>()->Get( | 120 hValue = hException.As<v8::Object>()->Get( |
96 v8::String::NewFromUtf8(pIsolate, "name")); | 121 v8::String::NewFromUtf8(pIsolate, "name")); |
97 if (hValue->IsString() || hValue->IsStringObject()) { | 122 if (hValue->IsString() || hValue->IsStringObject()) |
98 hReturnValue->Set(0, hValue); | 123 hReturnValue->Set(0, hValue); |
99 } else { | 124 else |
100 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error")); | 125 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error")); |
101 } | 126 |
102 hValue = hException.As<v8::Object>()->Get( | 127 hValue = hException.As<v8::Object>()->Get( |
103 v8::String::NewFromUtf8(pIsolate, "message")); | 128 v8::String::NewFromUtf8(pIsolate, "message")); |
104 if (hValue->IsString() || hValue->IsStringObject()) { | 129 if (hValue->IsString() || hValue->IsStringObject()) |
105 hReturnValue->Set(1, hValue); | 130 hReturnValue->Set(1, hValue); |
106 } else { | 131 else |
107 hReturnValue->Set(1, hMessage->Get()); | 132 hReturnValue->Set(1, hMessage->Get()); |
108 } | |
109 } else { | 133 } else { |
110 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error")); | 134 hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error")); |
111 hReturnValue->Set(1, hMessage->Get()); | 135 hReturnValue->Set(1, hMessage->Get()); |
112 } | 136 } |
113 hReturnValue->Set(2, hException); | 137 hReturnValue->Set(2, hException); |
114 hReturnValue->Set(3, v8::Integer::New(pIsolate, hMessage->GetLineNumber())); | 138 hReturnValue->Set(3, v8::Integer::New(pIsolate, hMessage->GetLineNumber())); |
115 hReturnValue->Set(4, hMessage->GetSourceLine()); | 139 hReturnValue->Set(4, hMessage->GetSourceLine()); |
116 v8::Maybe<int32_t> maybe_int = | 140 v8::Maybe<int32_t> maybe_int = |
117 hMessage->GetStartColumn(pIsolate->GetCurrentContext()); | 141 hMessage->GetStartColumn(pIsolate->GetCurrentContext()); |
118 hReturnValue->Set(5, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0))); | 142 hReturnValue->Set(5, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0))); |
119 maybe_int = hMessage->GetEndColumn(pIsolate->GetCurrentContext()); | 143 maybe_int = hMessage->GetEndColumn(pIsolate->GetCurrentContext()); |
120 hReturnValue->Set(6, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0))); | 144 hReturnValue->Set(6, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0))); |
121 } | 145 } |
122 return hReturnValue; | 146 return hReturnValue; |
123 } | 147 } |
124 | 148 |
| 149 // static |
125 CFXJSE_Context* CFXJSE_Context::Create( | 150 CFXJSE_Context* CFXJSE_Context::Create( |
126 v8::Isolate* pIsolate, | 151 v8::Isolate* pIsolate, |
127 const FXJSE_CLASS_DESCRIPTOR* lpGlobalClass, | 152 const FXJSE_CLASS_DESCRIPTOR* lpGlobalClass, |
128 CFXJSE_HostObject* lpGlobalObject) { | 153 CFXJSE_HostObject* lpGlobalObject) { |
129 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate); | 154 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate); |
130 CFXJSE_Context* pContext = new CFXJSE_Context(pIsolate); | 155 CFXJSE_Context* pContext = new CFXJSE_Context(pIsolate); |
131 CFXJSE_Class* lpGlobalClassObj = NULL; | 156 CFXJSE_Class* lpGlobalClassObj = nullptr; |
132 v8::Local<v8::ObjectTemplate> hObjectTemplate; | 157 v8::Local<v8::ObjectTemplate> hObjectTemplate; |
133 if (lpGlobalClass) { | 158 if (lpGlobalClass) { |
134 lpGlobalClassObj = CFXJSE_Class::Create(pContext, lpGlobalClass, TRUE); | 159 lpGlobalClassObj = CFXJSE_Class::Create(pContext, lpGlobalClass, TRUE); |
135 ASSERT(lpGlobalClassObj); | 160 ASSERT(lpGlobalClassObj); |
136 v8::Local<v8::FunctionTemplate> hFunctionTemplate = | 161 v8::Local<v8::FunctionTemplate> hFunctionTemplate = |
137 v8::Local<v8::FunctionTemplate>::New(pIsolate, | 162 v8::Local<v8::FunctionTemplate>::New(pIsolate, |
138 lpGlobalClassObj->m_hTemplate); | 163 lpGlobalClassObj->m_hTemplate); |
139 hObjectTemplate = hFunctionTemplate->InstanceTemplate(); | 164 hObjectTemplate = hFunctionTemplate->InstanceTemplate(); |
140 } else { | 165 } else { |
141 hObjectTemplate = v8::ObjectTemplate::New(pIsolate); | 166 hObjectTemplate = v8::ObjectTemplate::New(pIsolate); |
142 hObjectTemplate->SetInternalFieldCount(1); | 167 hObjectTemplate->SetInternalFieldCount(1); |
143 } | 168 } |
144 hObjectTemplate->Set( | 169 hObjectTemplate->Set( |
145 v8::Symbol::GetToStringTag(pIsolate), | 170 v8::Symbol::GetToStringTag(pIsolate), |
146 v8::String::NewFromUtf8(pIsolate, "global", v8::NewStringType::kNormal) | 171 v8::String::NewFromUtf8(pIsolate, "global", v8::NewStringType::kNormal) |
147 .ToLocalChecked()); | 172 .ToLocalChecked()); |
148 v8::Local<v8::Context> hNewContext = | 173 v8::Local<v8::Context> hNewContext = |
149 v8::Context::New(pIsolate, NULL, hObjectTemplate); | 174 v8::Context::New(pIsolate, nullptr, hObjectTemplate); |
150 v8::Local<v8::Context> hRootContext = v8::Local<v8::Context>::New( | 175 v8::Local<v8::Context> hRootContext = v8::Local<v8::Context>::New( |
151 pIsolate, CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext); | 176 pIsolate, CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext); |
152 hNewContext->SetSecurityToken(hRootContext->GetSecurityToken()); | 177 hNewContext->SetSecurityToken(hRootContext->GetSecurityToken()); |
153 v8::Local<v8::Object> hGlobalObject = | 178 v8::Local<v8::Object> hGlobalObject = |
154 FXJSE_GetGlobalObjectFromContext(hNewContext); | 179 FXJSE_GetGlobalObjectFromContext(hNewContext); |
155 FXJSE_UpdateObjectBinding(hGlobalObject, lpGlobalObject); | 180 FXJSE_UpdateObjectBinding(hGlobalObject, lpGlobalObject); |
156 pContext->m_hContext.Reset(pIsolate, hNewContext); | 181 pContext->m_hContext.Reset(pIsolate, hNewContext); |
157 return pContext; | 182 return pContext; |
158 } | 183 } |
159 | 184 |
(...skipping 17 matching lines...) Expand all Loading... |
177 ExecuteScript(szCompatibleModeScript, nullptr, nullptr); | 202 ExecuteScript(szCompatibleModeScript, nullptr, nullptr); |
178 } | 203 } |
179 | 204 |
180 FX_BOOL CFXJSE_Context::ExecuteScript(const FX_CHAR* szScript, | 205 FX_BOOL CFXJSE_Context::ExecuteScript(const FX_CHAR* szScript, |
181 CFXJSE_Value* lpRetValue, | 206 CFXJSE_Value* lpRetValue, |
182 CFXJSE_Value* lpNewThisObject) { | 207 CFXJSE_Value* lpNewThisObject) { |
183 CFXJSE_ScopeUtil_IsolateHandleContext scope(this); | 208 CFXJSE_ScopeUtil_IsolateHandleContext scope(this); |
184 v8::TryCatch trycatch(m_pIsolate); | 209 v8::TryCatch trycatch(m_pIsolate); |
185 v8::Local<v8::String> hScriptString = | 210 v8::Local<v8::String> hScriptString = |
186 v8::String::NewFromUtf8(m_pIsolate, szScript); | 211 v8::String::NewFromUtf8(m_pIsolate, szScript); |
187 if (lpNewThisObject == NULL) { | 212 if (!lpNewThisObject) { |
188 v8::Local<v8::Script> hScript = v8::Script::Compile(hScriptString); | 213 v8::Local<v8::Script> hScript = v8::Script::Compile(hScriptString); |
189 if (!trycatch.HasCaught()) { | 214 if (!trycatch.HasCaught()) { |
190 v8::Local<v8::Value> hValue = hScript->Run(); | 215 v8::Local<v8::Value> hValue = hScript->Run(); |
191 if (!trycatch.HasCaught()) { | 216 if (!trycatch.HasCaught()) { |
192 if (lpRetValue) { | 217 if (lpRetValue) { |
193 lpRetValue->m_hValue.Reset(m_pIsolate, hValue); | 218 lpRetValue->m_hValue.Reset(m_pIsolate, hValue); |
194 } | 219 } |
195 return TRUE; | 220 return TRUE; |
196 } | 221 } |
197 } | 222 } |
(...skipping 23 matching lines...) Expand all Loading... |
221 return TRUE; | 246 return TRUE; |
222 } | 247 } |
223 } | 248 } |
224 if (lpRetValue) { | 249 if (lpRetValue) { |
225 lpRetValue->m_hValue.Reset(m_pIsolate, | 250 lpRetValue->m_hValue.Reset(m_pIsolate, |
226 FXJSE_CreateReturnValue(m_pIsolate, trycatch)); | 251 FXJSE_CreateReturnValue(m_pIsolate, trycatch)); |
227 } | 252 } |
228 return FALSE; | 253 return FALSE; |
229 } | 254 } |
230 } | 255 } |
OLD | NEW |