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

Side by Side Diff: Source/bindings/v8/V8ScriptRunner.cpp

Issue 17035004: [ABANDONED] Introduce Promise example implementation written in JavaScript. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years, 6 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 | « Source/bindings/v8/V8ScriptRunner.h ('k') | Source/bindings/v8/custom/V8PromiseCustom.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved. 2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE. 23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #include "config.h" 26 #include "config.h"
27 #include "bindings/v8/V8ScriptRunner.h" 27 #include "bindings/v8/V8ScriptRunner.h"
28 28
29 #include "bindings/v8/V8Binding.h" 29 #include "bindings/v8/V8Binding.h"
30 #include "bindings/v8/V8DOMWindowShell.h"
30 #include "bindings/v8/V8GCController.h" 31 #include "bindings/v8/V8GCController.h"
32 #include "bindings/v8/V8HiddenPropertyName.h"
31 #include "bindings/v8/V8RecursionScope.h" 33 #include "bindings/v8/V8RecursionScope.h"
32 #include "core/dom/ScriptExecutionContext.h" 34 #include "core/dom/ScriptExecutionContext.h"
33 #include "core/loader/CachedMetadata.h" 35 #include "core/loader/CachedMetadata.h"
34 #include "core/loader/cache/CachedScript.h" 36 #include "core/loader/cache/CachedScript.h"
35 #include "core/platform/chromium/TraceEvent.h" 37 #include "core/platform/chromium/TraceEvent.h"
36 38
37 namespace WebCore { 39 namespace WebCore {
38 40
39 PassOwnPtr<v8::ScriptData> V8ScriptRunner::precompileScript(v8::Handle<v8::Strin g> code, CachedScript* cachedScript) 41 PassOwnPtr<v8::ScriptData> V8ScriptRunner::precompileScript(v8::Handle<v8::Strin g> code, CachedScript* cachedScript)
40 { 42 {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(source, fileNa me, scriptStartPosition, scriptData, isolate); 110 v8::Handle<v8::Script> script = V8ScriptRunner::compileScript(source, fileNa me, scriptStartPosition, scriptData, isolate);
109 if (script.IsEmpty()) 111 if (script.IsEmpty())
110 return v8::Local<v8::Value>(); 112 return v8::Local<v8::Value>();
111 113
112 V8RecursionScope::MicrotaskSuppression recursionScope; 114 V8RecursionScope::MicrotaskSuppression recursionScope;
113 v8::Local<v8::Value> result = script->Run(); 115 v8::Local<v8::Value> result = script->Run();
114 crashIfV8IsDead(); 116 crashIfV8IsDead();
115 return result; 117 return result;
116 } 118 }
117 119
120 v8::Local<v8::Function> V8ScriptRunner::blinkJSConstructor(const char* name, Wra pperTypeInfo* typeInfo, const String& source, int argc, v8::Handle<v8::Value> ar gv[], v8::Isolate* isolate)
121 {
122 V8DOMWindowShell* shell = V8DOMWindowShell::ContextScope::currentWindowShell ();
123 ASSERT(shell);
124 V8DOMWindowShell::ContextScope contextScope(shell, V8DOMWindowShell::BlinkJS Context);
125
126 v8::Local<v8::Context> context = isolate->GetCurrentContext();
127 V8PerContextData* perContextData = V8PerContextData::from(context);
128 if (!perContextData) {
129 String message = String(name) + " constructor is not found";
130 throwError(v8GeneralError, message.utf8().data(), isolate);
131 return v8::Local<v8::Function>();
132 }
133 v8::Local<v8::Value> wrapperConstructor = perContextData->constructorForType (typeInfo);
134 if (wrapperConstructor.IsEmpty() || !wrapperConstructor->IsObject()) {
135 String message = String(name) + " constructor is not found";
136 throwError(v8GeneralError, message.utf8().data(), isolate);
137 return v8::Local<v8::Function>();
138 }
139 v8::Local<v8::Value> value = wrapperConstructor.As<v8::Object>()->GetHiddenV alue(V8HiddenPropertyName::constructor());
140 if (!value.IsEmpty() && value->IsFunction()) {
141 // Return the stored constructor.
142 return value.As<v8::Function>();
143 }
144
145 value = V8ScriptRunner::compileAndRunInternalScript(v8String(source, isolate ), isolate);
146 if (value.IsEmpty())
147 return v8::Local<v8::Function>();
148 if (!value->IsFunction()) {
149 String message = String(name) + " constructor implementation must be a f unction";
150 throwError(v8TypeError, message.utf8().data(), isolate);
151 return v8::Local<v8::Function>();
152 }
153
154 v8::Local<v8::Function> impl = value.As<v8::Function>();
155 value = impl->Call(v8::Object::New(), argc, argv);
156 if (value.IsEmpty())
157 return v8::Local<v8::Function>();
158 if (!value->IsFunction()) {
159 String message = String(name) + " constructor must be a function";
160 throwError(v8TypeError, message.utf8().data(), isolate);
161 return v8::Local<v8::Function>();
162 }
163 bool result = wrapperConstructor.As<v8::Object>()->SetHiddenValue(V8HiddenPr opertyName::constructor(), value);
164 if (!result) {
165 String message = String(name) + " constructor is not found";
166 throwError(v8GeneralError, message.utf8().data(), isolate);
167 return v8::Local<v8::Function>();
168 }
169 return value.As<v8::Function>();
170 }
171
118 static String functionInfo(const v8::Handle<v8::Function> function) 172 static String functionInfo(const v8::Handle<v8::Function> function)
119 { 173 {
120 String resourceName = "undefined"; 174 String resourceName = "undefined";
121 int lineNumber = 1; 175 int lineNumber = 1;
122 v8::ScriptOrigin origin = function->GetScriptOrigin(); 176 v8::ScriptOrigin origin = function->GetScriptOrigin();
123 if (!origin.ResourceName().IsEmpty()) { 177 if (!origin.ResourceName().IsEmpty()) {
124 resourceName = toWebCoreString(origin.ResourceName()); 178 resourceName = toWebCoreString(origin.ResourceName());
125 lineNumber = function->GetScriptLineNumber() + 1; 179 lineNumber = function->GetScriptLineNumber() + 1;
126 } 180 }
127 181
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 245
192 v8::Local<v8::Object> V8ScriptRunner::instantiateObjectInDocument(v8::Handle<v8: :Function> function, ScriptExecutionContext* context, int argc, v8::Handle<v8::V alue> argv[]) 246 v8::Local<v8::Object> V8ScriptRunner::instantiateObjectInDocument(v8::Handle<v8: :Function> function, ScriptExecutionContext* context, int argc, v8::Handle<v8::V alue> argv[])
193 { 247 {
194 TRACE_EVENT0("v8", "v8.newInstance"); 248 TRACE_EVENT0("v8", "v8.newInstance");
195 V8RecursionScope scope(context); 249 V8RecursionScope scope(context);
196 v8::Local<v8::Object> result = function->NewInstance(argc, argv); 250 v8::Local<v8::Object> result = function->NewInstance(argc, argv);
197 crashIfV8IsDead(); 251 crashIfV8IsDead();
198 return result; 252 return result;
199 } 253 }
200 254
255 v8::Local<v8::Value> V8ScriptRunner::callUnwrappedMethod(const char* name, const v8::FunctionCallbackInfo<v8::Value>& args, v8::Handle<v8::Value> prototype)
256 {
257 Vector<v8::Handle<v8::Value> > arguments;
258 for (int i = 0; i < args.Length(); ++i)
259 arguments.append(args[i]);
260 return callUnwrappedMethod(name, args.This(), arguments.size(), arguments.da ta(), args.GetIsolate(), prototype);
261 }
262
263 v8::Local<v8::Value> V8ScriptRunner::callUnwrappedMethod(const char* name, v8::H andle<v8::Object> thisObject,
264 int argc, v8::Handle<v8::Value> argv[], v8::Isolate* isolate, v8::Handle<v8: :Value> prototype)
265 {
266 if (thisObject.IsEmpty()) {
267 String message = String("Cannot call method '") + name + "' of undefined ";
268 throwError(v8TypeError, message.utf8().data(), isolate);
269 return v8::Local<v8::Value>();
270 }
271 v8::Handle<v8::Value> unwrapped = thisObject->GetInternalField(v8DOMWrapperO bjectIndex);
272 if (unwrapped.IsEmpty() || !unwrapped->IsObject()) {
273 throwError(v8TypeError, "The wrapped this value is not an object", isola te);
274 return v8::Local<v8::Value>();
275 }
276
277 if (prototype.IsEmpty() || !prototype->IsObject()) {
278 throwError(v8TypeError, "The prototype is not an object", isolate);
279 return v8::Local<v8::Value>();
280 }
281
282 v8::Local<v8::Value> property = prototype.As<v8::Object>()->Get(v8::String:: NewSymbol(name));
283 if (property.IsEmpty() || !property->IsFunction()) {
284 String message = String("Property '") + name + "' of the object is not a function";
285 throwError(v8TypeError, message.utf8().data(), isolate);
286 return v8::Local<v8::Value>();
287 }
288 return property.As<v8::Function>()->Call(unwrapped.As<v8::Object>(), argc, a rgv);
289 }
290
291 v8::Local<v8::Value> V8ScriptRunner::callStaticMethod(const char* name, const v8 ::FunctionCallbackInfo<v8::Value>& args, v8::Handle<v8::Value> constructor)
292 {
293 if (constructor.IsEmpty() || !constructor->IsObject()) {
294 throwError(v8TypeError, "The constructor object is not found", args.GetI solate());
295 return v8::Local<v8::Value>();
296 }
297 v8::Local<v8::Value> property = constructor.As<v8::Object>()->Get(v8::String ::NewSymbol(name));
298 if (property.IsEmpty() || !property->IsFunction()) {
299 String message = String("Property '") + name + "' of the object is not a function";
300 throwError(v8TypeError, message.utf8().data(), args.GetIsolate());
301 return v8::Local<v8::Value>();
302 }
303 Vector<v8::Handle<v8::Value> > arguments;
304 for (int i = 0; i < args.Length(); ++i)
305 arguments.append(args[i]);
306 return property.As<v8::Function>()->Call(args.This(), arguments.size(), argu ments.data());
307 }
308
201 } // namespace WebCore 309 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/bindings/v8/V8ScriptRunner.h ('k') | Source/bindings/v8/custom/V8PromiseCustom.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698