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

Unified 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 side-by-side diff with in-line comments
Download patch
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..36702bde99808508d4a27dd526f655348f0728fc
--- /dev/null
+++ b/Source/bindings/v8/V8PromiseUtilities.cpp
@@ -0,0 +1,133 @@
+/*
+ * 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 "wtf/text/CString.h"
+#include "wtf/text/WTFString.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();
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).
+ v8::Local<v8::Value> placeholderValue = global->Get(v8::String::NewSymbol(name));
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).
+ 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);
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.
+ 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>();
+ bool result = placeholder->SetHiddenValue(V8HiddenPropertyName::jsConstructor(), value);
+ if (!result)
+ return V8ThrowException::throwError(v8TypeError, "Cannot save the promise constructor", isolate).As<v8::Function>();
+ 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"));
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
+}
+
+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)
+{
+ if (thisObject.IsEmpty()) {
+ String message = String("Cannot call method '") + name + "' of undefined";
+ return V8ThrowException::throwError(v8TypeError, message.utf8().data(), isolate);
+ }
+ v8::Handle<v8::Value> unwrapped = thisObject->GetInternalField(v8DOMWrapperObjectIndex);
+ if (unwrapped.IsEmpty() || !unwrapped->IsObject())
+ return V8ThrowException::throwError(v8TypeError, "The wrapped this value is not an object", isolate);
+
+ if (prototype.IsEmpty() || !prototype->IsObject())
+ return V8ThrowException::throwError(v8TypeError, "The prototype is not an object", isolate);
+
+ v8::Local<v8::Value> property = prototype.As<v8::Object>()->Get(v8::String::NewSymbol(name));
+ if (property.IsEmpty() || !property->IsFunction()) {
+ String message = String("Property '") + name + "' of the object is not a function";
+ return V8ThrowException::throwError(v8TypeError, message.utf8().data(), isolate);
+ }
+ return property.As<v8::Function>()->Call(unwrapped.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()) {
+ String message = String("Property '") + name + "' of the object is not a function";
+ return V8ThrowException::throwError(v8TypeError, message.utf8().data(), args.GetIsolate());
+ }
+ 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

Powered by Google App Engine
This is Rietveld 408576698