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

Unified Diff: mojo/public/bindings/js/core.cc

Issue 59153005: Begin implementing V8 bindings for Mojo (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Moar copyright Created 7 years, 1 month 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: mojo/public/bindings/js/core.cc
diff --git a/mojo/public/bindings/js/core.cc b/mojo/public/bindings/js/core.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3b68970368ed8618d7e408ed2fda2b5b56b4af0f
--- /dev/null
+++ b/mojo/public/bindings/js/core.cc
@@ -0,0 +1,171 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/public/bindings/js/core.h"
+
+#include "gin/arguments.h"
+#include "gin/array_buffer.h"
+#include "gin/converter.h"
+#include "gin/dictionary.h"
+#include "gin/per_isolate_data.h"
+#include "gin/wrapper_info.h"
+#include "mojo/public/bindings/js/handle.h"
+
+namespace mojo {
+namespace js {
+
+namespace {
+
+void Close(const v8::FunctionCallbackInfo<v8::Value>& info) {
Aaron Boodman 2013/11/11 19:01:08 The C++ template stuff I had in the other project
abarth-chromium 2013/11/11 20:17:42 Yes, I think gin::Bind is super cool. Would you l
Aaron Boodman 2013/11/11 20:33:23 If you think it is useful, I will do it this week.
abarth-chromium 2013/11/11 21:05:49 Thanks!
+ gin::Arguments args(info);
+
+ mojo::Handle handle;
Aaron Boodman 2013/11/11 19:01:08 The uninitialized locals that Argument's interface
abarth-chromium 2013/11/11 20:17:42 Sure, we can do that.
abarth-chromium 2013/11/11 21:05:49 Oh, I remember why I did it this way. The problem
+ if (!args.Next(&handle))
+ return args.ThrowTypeError("Expected mojo::Handle.");
+
+ args.Return(mojo::Close(handle));
+}
+
+void Wait(const v8::FunctionCallbackInfo<v8::Value>& info) {
+ gin::Arguments args(info);
+
+ mojo::Handle handle;
+ if (!args.Next(&handle))
+ return args.ThrowTypeError("Expected mojo::Handle.");
+
+ MojoWaitFlags flags;
+ if (!args.Next(&flags))
+ return args.ThrowTypeError("Expected MojoWaitFlags.");
+
+ MojoDeadline deadline;
+ if (!args.Next(&deadline))
+ return args.ThrowTypeError("Expected MojoDeadline.");
+
+ args.Return(mojo::Wait(handle, flags, deadline));
+}
+
+void WaitMany(const v8::FunctionCallbackInfo<v8::Value>& info) {
+ gin::Arguments args(info);
+
+ std::vector<mojo::Handle> handles;
+ if (!args.Next(&handles))
+ return args.ThrowTypeError("Expected array of mojo::Handle.");
+
+ std::vector<MojoWaitFlags> flags;
+ if (!args.Next(&flags))
+ return args.ThrowTypeError("Expected array of MojoWaitFlags.");
+
+ if (handles.size() != flags.size())
+ return args.ThrowTypeError("Arrays must have the same length.");
+
+ MojoDeadline deadline;
+ if (!args.Next(&deadline))
+ return args.ThrowTypeError("Expected MojoDeadline.");
+
+ args.Return(mojo::WaitMany(handles.data(), flags.data(),
+ handles.size(), deadline));
+}
+
+void CreateMessagePipe(const v8::FunctionCallbackInfo<v8::Value>& info) {
+ gin::Arguments args(info);
+
+ mojo::Handle handle_0 = mojo::kInvalidHandle;
+ mojo::Handle handle_1 = mojo::kInvalidHandle;
+ MojoResult result = mojo::CreateMessagePipe(&handle_0, &handle_1);
+
+ gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(info.GetIsolate());
+ dictionary.Set("result", result);
+ dictionary.Set("handle0", handle_0);
+ dictionary.Set("handle1", handle_1);
+ args.Return(dictionary);
+}
+
+void WriteMessage(const v8::FunctionCallbackInfo<v8::Value>& info) {
+ gin::Arguments args(info);
+
+ mojo::Handle handle;
+ if (!args.Next(&handle))
+ return args.ThrowTypeError("Expected mojo::Handle.");
+
+ gin::ArrayBufferView buffer(args.isolate());
+ if (!args.Next(&buffer))
+ return args.ThrowTypeError("Expected ArrayBufferView.");
+
+ std::vector<mojo::Handle> handles;
+ if (!args.Next(&handles))
+ return args.ThrowTypeError("Expected array of mojo::Handle.");
+
+ MojoWaitFlags flags;
+ if (!args.Next(&flags))
+ return args.ThrowTypeError("Expected MojoWaitFlags.");
Aaron Boodman 2013/11/11 19:01:08 I think you should make Arguments responsible for
abarth-chromium 2013/11/11 20:17:42 Makes sense. It won't know how to stringify the t
Aaron Boodman 2013/11/11 21:12:49 Note: you didn't address this one. I'm fine with i
+
+ args.Return(mojo::WriteMessage(handle, buffer.bytes(), buffer.num_bytes(),
+ handles.data(), handles.size(), flags));
+}
+
+void ReadMessage(const v8::FunctionCallbackInfo<v8::Value>& info) {
+ gin::Arguments args(info);
+
+ mojo::Handle handle;
+ if (!args.Next(&handle))
+ return args.ThrowTypeError("Expected mojo::Handle.");
+
+ gin::ArrayBufferView buffer(args.isolate());
+ if (!args.Next(&buffer))
+ return args.ThrowTypeError("Expected ArrayBufferView.");
+
+ uint32_t num_handles;
+ if (!args.Next(&num_handles))
+ return args.ThrowTypeError("Expected uint32_t.");
+
+ MojoWaitFlags flags;
+ if (!args.Next(&flags))
+ return args.ThrowTypeError("Expected MojoWaitFlags.");
+
+ uint32_t num_bytes = buffer.num_bytes();
+ std::vector<mojo::Handle> handles(num_handles);
+ MojoResult result = mojo::ReadMessage(handle, buffer.bytes(), &num_bytes,
+ handles.data(), &num_handles, flags);
+ handles.resize(num_handles);
+
+ // TODO(abarth): We should benchmark this codepath to make sure it's ok to
+ // allocate all this memory on each read.
+ gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(info.GetIsolate());
+ dictionary.Set("result", result);
+ dictionary.Set("bytesRead", num_bytes);
+ dictionary.Set("handles", handles);
+ args.Return(dictionary);
+}
+
+gin::WrapperInfo g_core_wrapper_info = {};
+
+}
+
+v8::Local<v8::ObjectTemplate> CoreTemplate(v8::Isolate* isolate) {
+ gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
+ v8::Local<v8::ObjectTemplate> templ;
+ if (!data->GetObjectTemplate(&g_core_wrapper_info, &templ)) {
+ templ = v8::ObjectTemplate::New();
+
+ templ->Set(gin::StringToV8(isolate, "close"),
+ v8::FunctionTemplate::New(Close));
+ templ->Set(gin::StringToV8(isolate, "wait"),
+ v8::FunctionTemplate::New(Wait));
+ templ->Set(gin::StringToV8(isolate, "waitMany"),
+ v8::FunctionTemplate::New(WaitMany));
+ templ->Set(gin::StringToV8(isolate, "createMessagePipe"),
+ v8::FunctionTemplate::New(CreateMessagePipe));
+ templ->Set(gin::StringToV8(isolate, "writeMessage"),
+ v8::FunctionTemplate::New(WriteMessage));
+ templ->Set(gin::StringToV8(isolate, "readMessage"),
+ v8::FunctionTemplate::New(ReadMessage));
+
+ data->RegisterObjectTemplate(&g_core_wrapper_info, templ);
+ }
+
+ return templ;
+}
+
+} // namespace js
+} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698