Chromium Code Reviews| Index: Source/bindings/v8/V8PromiseUtilities.cpp |
| diff --git a/Source/bindings/v8/V8PromiseUtilities.cpp b/Source/bindings/v8/V8PromiseUtilities.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..85c2346eac15478da880e66eeda42233163a9d8a |
| --- /dev/null |
| +++ b/Source/bindings/v8/V8PromiseUtilities.cpp |
| @@ -0,0 +1,121 @@ |
| +/* |
| + * Copyright (C) 2013 Google Inc. All rights reserved. |
| + * |
| + * Redistribution and use in source and binary forms, with or without |
| + * modification, are permitted provided that the following conditions are |
| + * met: |
| + * |
| + * * Redistributions of source code must retain the above copyright |
| + * notice, this list of conditions and the following disclaimer. |
| + * * Redistributions in binary form must reproduce the above |
| + * copyright notice, this list of conditions and the following disclaimer |
| + * in the documentation and/or other materials provided with the |
| + * distribution. |
| + * * Neither the name of Google Inc. nor the names of its |
| + * contributors may be used to endorse or promote products derived from |
| + * this software without specific prior written permission. |
| + * |
| + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| + */ |
| + |
| +#include "config.h" |
| +#include "bindings/v8/V8PromiseUtilities.h" |
| + |
| +#include "PromiseCustomScript.h" |
| +#include "bindings/v8/V8Binding.h" |
| +#include "bindings/v8/V8HiddenPropertyName.h" |
| +#include "bindings/v8/V8ScriptRunner.h" |
| +#include "bindings/v8/V8ThrowException.h" |
| + |
| +#include <v8.h> |
| + |
| +namespace WebCore { |
| + |
| +v8::Handle<v8::Function> V8PromiseUtilities::promiseConstructor(v8::Isolate* isolate) |
| +{ |
| + const char name[] = "Promise"; |
| + v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global(); |
| + v8::Local<v8::Value> placeholderValue = global->Get(v8::String::NewSymbol(name)); |
| + if (placeholderValue.IsEmpty() || !placeholderValue->IsObject()) |
| + return V8ThrowException::throwError(v8GeneralError, "The placeholder for Promise is not found.", isolate).As<v8::Function>(); |
| + v8::Local<v8::Object> placeholder = placeholderValue.As<v8::Object>(); |
| + v8::Handle<v8::Value> value = placeholder->GetHiddenValue(V8HiddenPropertyName::jsConstructor()); |
| + if (!value.IsEmpty() && value->IsFunction()) { |
| + // Return the stored constructor. |
| + return value.As<v8::Function>(); |
| + } |
| + |
| + value = V8ScriptRunner::compileAndRunInternalScript(v8String(String(Promise_js, sizeof(Promise_js)), isolate), isolate); |
| + if (value.IsEmpty()) |
| + return v8::Handle<v8::Function>(); |
| + if (!value->IsFunction()) |
| + return V8ThrowException::throwError(v8TypeError, "Promise implementation must be a function.", isolate).As<v8::Function>(); |
| + |
| + v8::Handle<v8::Function> impl = value.As<v8::Function>(); |
| + v8::Handle<v8::Value> args[] = { |
| + }; |
| + value = impl->Call(v8::Object::New(), sizeof(args) / sizeof(args[0]), &args[0]); |
| + if (value.IsEmpty()) |
| + return v8::Handle<v8::Function>(); |
| + if (!value->IsFunction()) |
| + return V8ThrowException::throwError(v8TypeError, "Promise constructor must be a function.", isolate).As<v8::Function>(); |
| + placeholder->SetHiddenValue(V8HiddenPropertyName::jsConstructor(), value); |
|
tyoshino (SeeGerritForStatus)
2013/06/17 07:51:58
check return value?
yhirano
2013/06/17 08:39:23
Done.
|
| + return value.As<v8::Function>(); |
| +} |
| + |
| +v8::Handle<v8::Value> V8PromiseUtilities::promisePrototype(v8::Isolate* isolate) |
| +{ |
| + v8::Handle<v8::Object> constructor = promiseConstructor(isolate); |
| + if (constructor.IsEmpty()) |
| + return constructor; |
| + return constructor->Get(v8::String::NewSymbol("prototype")); |
| +} |
| + |
| +v8::Handle<v8::Value> V8PromiseUtilities::callUnwrappedMethod(const char* name, const v8::FunctionCallbackInfo<v8::Value>& args, v8::Handle<v8::Value> prototype) |
| +{ |
| + Vector<v8::Handle<v8::Value> > arguments; |
| + for (int i = 0; i < args.Length(); ++i) |
| + arguments.append(args[i]); |
| + return callUnwrappedMethod(name, args.This(), arguments.size(), arguments.data(), args.GetIsolate(), prototype); |
| +} |
| + |
| +v8::Handle<v8::Value> V8PromiseUtilities::callUnwrappedMethod(const char* name, v8::Handle<v8::Object> thisObject, |
| + int argc, v8::Handle<v8::Value> argv[], v8::Isolate* isolate, v8::Handle<v8::Value> prototype) |
| +{ |
| + v8::Handle<v8::Value> that = thisObject->GetInternalField(v8DOMWrapperObjectIndex); |
|
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
|
| + if (that.IsEmpty() || !that->IsObject()) |
| + return V8ThrowException::throwError(v8TypeError, "This object is not an object.", isolate); |
| + |
| + if (prototype.IsEmpty() || !prototype->IsObject()) |
| + 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.
|
| + |
| + v8::Local<v8::Value> property = prototype.As<v8::Object>()->Get(v8::String::NewSymbol(name)); |
| + if (property.IsEmpty() || !property->IsFunction()) |
| + return V8ThrowException::throwError(v8TypeError, (std::string("The property ") + name + " is not a function.").c_str(), isolate); |
| + return property.As<v8::Function>()->Call(that.As<v8::Object>(), argc, argv); |
| +} |
| + |
| +v8::Handle<v8::Value> V8PromiseUtilities::callStatic(const char* name, const v8::FunctionCallbackInfo<v8::Value>& args, v8::Handle<v8::Value> constructor) |
| +{ |
| + if (constructor.IsEmpty() || !constructor->IsObject()) |
| + return V8ThrowException::throwError(v8TypeError, "The constructor object is not found.", args.GetIsolate()); |
| + v8::Local<v8::Value> property = constructor.As<v8::Object>()->Get(v8::String::NewSymbol(name)); |
| + if (property.IsEmpty() || !property->IsFunction()) |
| + return V8ThrowException::throwError(v8TypeError, (std::string("The property ") + 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.
|
| + Vector<v8::Handle<v8::Value> > arguments; |
| + for (int i = 0; i < args.Length(); ++i) |
| + arguments.append(args[i]); |
| + return property.As<v8::Function>()->Call(args.This(), arguments.size(), arguments.data()); |
| +} |
| + |
| +} // namespace WebCore |