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

Side by Side Diff: Source/bindings/v8/V8PromiseUtilities.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/V8PromiseUtilities.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
(Empty)
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "bindings/v8/V8PromiseUtilities.h"
33
34 #include "PromiseCustomScript.h"
35 #include "bindings/v8/V8Binding.h"
36 #include "bindings/v8/V8HiddenPropertyName.h"
37 #include "bindings/v8/V8ScriptRunner.h"
38 #include "bindings/v8/V8ThrowException.h"
39
40 #include <v8.h>
41
42 namespace WebCore {
43
44 v8::Handle<v8::Function> V8PromiseUtilities::promiseConstructor(v8::Isolate* iso late)
45 {
46 const char name[] = "Promise";
47 v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global();
48 v8::Local<v8::Value> placeholderValue = global->Get(v8::String::NewSymbol(na me));
49 if (placeholderValue.IsEmpty() || !placeholderValue->IsObject())
50 return V8ThrowException::throwError(v8GeneralError, "The placeholder for Promise is not found.", isolate).As<v8::Function>();
51 v8::Local<v8::Object> placeholder = placeholderValue.As<v8::Object>();
52 v8::Handle<v8::Value> value = placeholder->GetHiddenValue(V8HiddenPropertyNa me::jsConstructor());
53 if (!value.IsEmpty() && value->IsFunction()) {
54 // Return the stored constructor.
55 return value.As<v8::Function>();
56 }
57
58 value = V8ScriptRunner::compileAndRunInternalScript(v8String(String(Promise_ js, sizeof(Promise_js)), isolate), isolate);
59 if (value.IsEmpty())
60 return v8::Handle<v8::Function>();
61 if (!value->IsFunction())
62 return V8ThrowException::throwError(v8TypeError, "Promise implementation must be a function.", isolate).As<v8::Function>();
63
64 v8::Handle<v8::Function> impl = value.As<v8::Function>();
65 v8::Handle<v8::Value> args[] = {
66 };
67 value = impl->Call(v8::Object::New(), sizeof(args) / sizeof(args[0]), &args[ 0]);
68 if (value.IsEmpty())
69 return v8::Handle<v8::Function>();
70 if (!value->IsFunction())
71 return V8ThrowException::throwError(v8TypeError, "Promise constructor mu st be a function.", isolate).As<v8::Function>();
72 placeholder->SetHiddenValue(V8HiddenPropertyName::jsConstructor(), value);
tyoshino (SeeGerritForStatus) 2013/06/17 07:51:58 check return value?
yhirano 2013/06/17 08:39:23 Done.
73 return value.As<v8::Function>();
74 }
75
76 v8::Handle<v8::Value> V8PromiseUtilities::promisePrototype(v8::Isolate* isolate)
77 {
78 v8::Handle<v8::Object> constructor = promiseConstructor(isolate);
79 if (constructor.IsEmpty())
80 return constructor;
81 return constructor->Get(v8::String::NewSymbol("prototype"));
82 }
83
84 v8::Handle<v8::Value> V8PromiseUtilities::callUnwrappedMethod(const char* name, const v8::FunctionCallbackInfo<v8::Value>& args, v8::Handle<v8::Value> prototype )
85 {
86 Vector<v8::Handle<v8::Value> > arguments;
87 for (int i = 0; i < args.Length(); ++i)
88 arguments.append(args[i]);
89 return callUnwrappedMethod(name, args.This(), arguments.size(), arguments.da ta(), args.GetIsolate(), prototype);
90 }
91
92 v8::Handle<v8::Value> V8PromiseUtilities::callUnwrappedMethod(const char* name, v8::Handle<v8::Object> thisObject,
93 int argc, v8::Handle<v8::Value> argv[], v8::Isolate* isolate, v8::Handle<v8: :Value> prototype)
94 {
95 v8::Handle<v8::Value> that = thisObject->GetInternalField(v8DOMWrapperObject Index);
tyoshino (SeeGerritForStatus) 2013/06/17 07:51:58 that -> wrapper?
yhirano 2013/06/17 08:39:23 Not wrapper, but wrapped, or unwrapped. In fact, t
96 if (that.IsEmpty() || !that->IsObject())
97 return V8ThrowException::throwError(v8TypeError, "This object is not an object.", isolate);
98
99 if (prototype.IsEmpty() || !prototype->IsObject())
100 return V8ThrowException::throwError(v8TypeError, "The prototypex is not an object.", isolate);
tyoshino (SeeGerritForStatus) 2013/06/17 07:51:58 prototypex -> prototype
yhirano 2013/06/17 08:39:23 Thanks, done.
101
102 v8::Local<v8::Value> property = prototype.As<v8::Object>()->Get(v8::String:: NewSymbol(name));
103 if (property.IsEmpty() || !property->IsFunction())
104 return V8ThrowException::throwError(v8TypeError, (std::string("The prope rty ") + name + " is not a function.").c_str(), isolate);
105 return property.As<v8::Function>()->Call(that.As<v8::Object>(), argc, argv);
106 }
107
108 v8::Handle<v8::Value> V8PromiseUtilities::callStatic(const char* name, const v8: :FunctionCallbackInfo<v8::Value>& args, v8::Handle<v8::Value> constructor)
109 {
110 if (constructor.IsEmpty() || !constructor->IsObject())
111 return V8ThrowException::throwError(v8TypeError, "The constructor object is not found.", args.GetIsolate());
112 v8::Local<v8::Value> property = constructor.As<v8::Object>()->Get(v8::String ::NewSymbol(name));
113 if (property.IsEmpty() || !property->IsFunction())
114 return V8ThrowException::throwError(v8TypeError, (std::string("The prope rty ") + name + " is not a function.").c_str(), args.GetIsolate());
tyoshino (SeeGerritForStatus) 2013/06/17 07:51:58 I still don't see any use of std::string in Blink
yhirano 2013/06/17 08:39:23 Done.
115 Vector<v8::Handle<v8::Value> > arguments;
116 for (int i = 0; i < args.Length(); ++i)
117 arguments.append(args[i]);
118 return property.As<v8::Function>()->Call(args.This(), arguments.size(), argu ments.data());
119 }
120
121 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/bindings/v8/V8PromiseUtilities.h ('k') | Source/bindings/v8/custom/V8PromiseCustom.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698