OLD | NEW |
---|---|
(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 #include "wtf/text/CString.h" | |
40 #include "wtf/text/WTFString.h" | |
41 | |
42 #include <v8.h> | |
43 | |
44 namespace WebCore { | |
45 | |
46 v8::Handle<v8::Function> V8PromiseUtilities::promiseConstructor(v8::Isolate* iso late) | |
47 { | |
48 const char name[] = "Promise"; | |
49 v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global(); | |
abarth-chromium
2013/06/17 18:05:06
Perhaps we should pass in the context instead of a
yhirano
2013/06/18 08:56:15
Done (in ScriptRunner::blinkJSConstructor).
| |
50 v8::Local<v8::Value> placeholderValue = global->Get(v8::String::NewSymbol(na me)); | |
abarth-chromium
2013/06/17 18:05:06
We don't want to read from the global object. The
yhirano
2013/06/18 08:56:15
Done (in ScriptRunner::blinkJSConstructor).
| |
51 if (placeholderValue.IsEmpty() || !placeholderValue->IsObject()) | |
52 return V8ThrowException::throwError(v8GeneralError, "The placeholder for 'Promise' is not found", isolate).As<v8::Function>(); | |
53 v8::Local<v8::Object> placeholder = placeholderValue.As<v8::Object>(); | |
54 v8::Handle<v8::Value> value = placeholder->GetHiddenValue(V8HiddenPropertyNa me::jsConstructor()); | |
55 if (!value.IsEmpty() && value->IsFunction()) { | |
56 // Return the stored constructor. | |
57 return value.As<v8::Function>(); | |
58 } | |
59 | |
60 value = V8ScriptRunner::compileAndRunInternalScript(v8String(String(Promise_ js, sizeof(Promise_js)), isolate), isolate); | |
abarth-chromium
2013/06/17 18:05:06
Most of the code from here down should be abstract
yhirano
2013/06/18 08:56:15
Done.
| |
61 if (value.IsEmpty()) | |
62 return v8::Handle<v8::Function>(); | |
63 if (!value->IsFunction()) | |
64 return V8ThrowException::throwError(v8TypeError, "Promise implementation must be a function", isolate).As<v8::Function>(); | |
65 | |
66 v8::Handle<v8::Function> impl = value.As<v8::Function>(); | |
67 v8::Handle<v8::Value> args[] = { | |
68 }; | |
69 value = impl->Call(v8::Object::New(), sizeof(args) / sizeof(args[0]), &args[ 0]); | |
70 if (value.IsEmpty()) | |
71 return v8::Handle<v8::Function>(); | |
72 if (!value->IsFunction()) | |
73 return V8ThrowException::throwError(v8TypeError, "Promise constructor mu st be a function", isolate).As<v8::Function>(); | |
74 bool result = placeholder->SetHiddenValue(V8HiddenPropertyName::jsConstructo r(), value); | |
75 if (!result) | |
76 return V8ThrowException::throwError(v8TypeError, "Cannot save the promis e constructor", isolate).As<v8::Function>(); | |
77 return value.As<v8::Function>(); | |
78 } | |
79 | |
80 v8::Handle<v8::Value> V8PromiseUtilities::promisePrototype(v8::Isolate* isolate) | |
81 { | |
82 v8::Handle<v8::Object> constructor = promiseConstructor(isolate); | |
83 if (constructor.IsEmpty()) | |
84 return constructor; | |
85 return constructor->Get(v8::String::NewSymbol("prototype")); | |
abarth-chromium
2013/06/17 18:05:06
This isn't right. The prototype property might ha
yhirano
2013/06/18 08:56:15
But Web developers cannot access the object. They
| |
86 } | |
87 | |
88 v8::Handle<v8::Value> V8PromiseUtilities::callUnwrappedMethod(const char* name, const v8::FunctionCallbackInfo<v8::Value>& args, v8::Handle<v8::Value> prototype ) | |
89 { | |
90 Vector<v8::Handle<v8::Value> > arguments; | |
91 for (int i = 0; i < args.Length(); ++i) | |
92 arguments.append(args[i]); | |
93 return callUnwrappedMethod(name, args.This(), arguments.size(), arguments.da ta(), args.GetIsolate(), prototype); | |
94 } | |
95 | |
96 v8::Handle<v8::Value> V8PromiseUtilities::callUnwrappedMethod(const char* name, v8::Handle<v8::Object> thisObject, | |
97 int argc, v8::Handle<v8::Value> argv[], v8::Isolate* isolate, v8::Handle<v8: :Value> prototype) | |
98 { | |
99 if (thisObject.IsEmpty()) { | |
100 String message = String("Cannot call method '") + name + "' of undefined "; | |
101 return V8ThrowException::throwError(v8TypeError, message.utf8().data(), isolate); | |
102 } | |
103 v8::Handle<v8::Value> unwrapped = thisObject->GetInternalField(v8DOMWrapperO bjectIndex); | |
104 if (unwrapped.IsEmpty() || !unwrapped->IsObject()) | |
105 return V8ThrowException::throwError(v8TypeError, "The wrapped this value is not an object", isolate); | |
106 | |
107 if (prototype.IsEmpty() || !prototype->IsObject()) | |
108 return V8ThrowException::throwError(v8TypeError, "The prototype is not a n object", isolate); | |
109 | |
110 v8::Local<v8::Value> property = prototype.As<v8::Object>()->Get(v8::String:: NewSymbol(name)); | |
111 if (property.IsEmpty() || !property->IsFunction()) { | |
112 String message = String("Property '") + name + "' of the object is not a function"; | |
113 return V8ThrowException::throwError(v8TypeError, message.utf8().data(), isolate); | |
114 } | |
115 return property.As<v8::Function>()->Call(unwrapped.As<v8::Object>(), argc, a rgv); | |
116 } | |
117 | |
118 v8::Handle<v8::Value> V8PromiseUtilities::callStatic(const char* name, const v8: :FunctionCallbackInfo<v8::Value>& args, v8::Handle<v8::Value> constructor) | |
119 { | |
120 if (constructor.IsEmpty() || !constructor->IsObject()) | |
121 return V8ThrowException::throwError(v8TypeError, "The constructor object is not found", args.GetIsolate()); | |
122 v8::Local<v8::Value> property = constructor.As<v8::Object>()->Get(v8::String ::NewSymbol(name)); | |
123 if (property.IsEmpty() || !property->IsFunction()) { | |
124 String message = String("Property '") + name + "' of the object is not a function"; | |
125 return V8ThrowException::throwError(v8TypeError, message.utf8().data(), args.GetIsolate()); | |
126 } | |
127 Vector<v8::Handle<v8::Value> > arguments; | |
128 for (int i = 0; i < args.Length(); ++i) | |
129 arguments.append(args[i]); | |
130 return property.As<v8::Function>()->Call(args.This(), arguments.size(), argu ments.data()); | |
131 } | |
132 | |
133 } // namespace WebCore | |
OLD | NEW |