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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "mojo/public/bindings/js/core.h"
6
7 #include "gin/arguments.h"
8 #include "gin/array_buffer.h"
9 #include "gin/converter.h"
10 #include "gin/dictionary.h"
11 #include "gin/per_isolate_data.h"
12 #include "gin/wrapper_info.h"
13 #include "mojo/public/bindings/js/handle.h"
14
15 namespace mojo {
16 namespace js {
17
18 namespace {
19
20 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!
21 gin::Arguments args(info);
22
23 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
24 if (!args.Next(&handle))
25 return args.ThrowTypeError("Expected mojo::Handle.");
26
27 args.Return(mojo::Close(handle));
28 }
29
30 void Wait(const v8::FunctionCallbackInfo<v8::Value>& info) {
31 gin::Arguments args(info);
32
33 mojo::Handle handle;
34 if (!args.Next(&handle))
35 return args.ThrowTypeError("Expected mojo::Handle.");
36
37 MojoWaitFlags flags;
38 if (!args.Next(&flags))
39 return args.ThrowTypeError("Expected MojoWaitFlags.");
40
41 MojoDeadline deadline;
42 if (!args.Next(&deadline))
43 return args.ThrowTypeError("Expected MojoDeadline.");
44
45 args.Return(mojo::Wait(handle, flags, deadline));
46 }
47
48 void WaitMany(const v8::FunctionCallbackInfo<v8::Value>& info) {
49 gin::Arguments args(info);
50
51 std::vector<mojo::Handle> handles;
52 if (!args.Next(&handles))
53 return args.ThrowTypeError("Expected array of mojo::Handle.");
54
55 std::vector<MojoWaitFlags> flags;
56 if (!args.Next(&flags))
57 return args.ThrowTypeError("Expected array of MojoWaitFlags.");
58
59 if (handles.size() != flags.size())
60 return args.ThrowTypeError("Arrays must have the same length.");
61
62 MojoDeadline deadline;
63 if (!args.Next(&deadline))
64 return args.ThrowTypeError("Expected MojoDeadline.");
65
66 args.Return(mojo::WaitMany(handles.data(), flags.data(),
67 handles.size(), deadline));
68 }
69
70 void CreateMessagePipe(const v8::FunctionCallbackInfo<v8::Value>& info) {
71 gin::Arguments args(info);
72
73 mojo::Handle handle_0 = mojo::kInvalidHandle;
74 mojo::Handle handle_1 = mojo::kInvalidHandle;
75 MojoResult result = mojo::CreateMessagePipe(&handle_0, &handle_1);
76
77 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(info.GetIsolate());
78 dictionary.Set("result", result);
79 dictionary.Set("handle0", handle_0);
80 dictionary.Set("handle1", handle_1);
81 args.Return(dictionary);
82 }
83
84 void WriteMessage(const v8::FunctionCallbackInfo<v8::Value>& info) {
85 gin::Arguments args(info);
86
87 mojo::Handle handle;
88 if (!args.Next(&handle))
89 return args.ThrowTypeError("Expected mojo::Handle.");
90
91 gin::ArrayBufferView buffer(args.isolate());
92 if (!args.Next(&buffer))
93 return args.ThrowTypeError("Expected ArrayBufferView.");
94
95 std::vector<mojo::Handle> handles;
96 if (!args.Next(&handles))
97 return args.ThrowTypeError("Expected array of mojo::Handle.");
98
99 MojoWaitFlags flags;
100 if (!args.Next(&flags))
101 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
102
103 args.Return(mojo::WriteMessage(handle, buffer.bytes(), buffer.num_bytes(),
104 handles.data(), handles.size(), flags));
105 }
106
107 void ReadMessage(const v8::FunctionCallbackInfo<v8::Value>& info) {
108 gin::Arguments args(info);
109
110 mojo::Handle handle;
111 if (!args.Next(&handle))
112 return args.ThrowTypeError("Expected mojo::Handle.");
113
114 gin::ArrayBufferView buffer(args.isolate());
115 if (!args.Next(&buffer))
116 return args.ThrowTypeError("Expected ArrayBufferView.");
117
118 uint32_t num_handles;
119 if (!args.Next(&num_handles))
120 return args.ThrowTypeError("Expected uint32_t.");
121
122 MojoWaitFlags flags;
123 if (!args.Next(&flags))
124 return args.ThrowTypeError("Expected MojoWaitFlags.");
125
126 uint32_t num_bytes = buffer.num_bytes();
127 std::vector<mojo::Handle> handles(num_handles);
128 MojoResult result = mojo::ReadMessage(handle, buffer.bytes(), &num_bytes,
129 handles.data(), &num_handles, flags);
130 handles.resize(num_handles);
131
132 // TODO(abarth): We should benchmark this codepath to make sure it's ok to
133 // allocate all this memory on each read.
134 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(info.GetIsolate());
135 dictionary.Set("result", result);
136 dictionary.Set("bytesRead", num_bytes);
137 dictionary.Set("handles", handles);
138 args.Return(dictionary);
139 }
140
141 gin::WrapperInfo g_core_wrapper_info = {};
142
143 }
144
145 v8::Local<v8::ObjectTemplate> CoreTemplate(v8::Isolate* isolate) {
146 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
147 v8::Local<v8::ObjectTemplate> templ;
148 if (!data->GetObjectTemplate(&g_core_wrapper_info, &templ)) {
149 templ = v8::ObjectTemplate::New();
150
151 templ->Set(gin::StringToV8(isolate, "close"),
152 v8::FunctionTemplate::New(Close));
153 templ->Set(gin::StringToV8(isolate, "wait"),
154 v8::FunctionTemplate::New(Wait));
155 templ->Set(gin::StringToV8(isolate, "waitMany"),
156 v8::FunctionTemplate::New(WaitMany));
157 templ->Set(gin::StringToV8(isolate, "createMessagePipe"),
158 v8::FunctionTemplate::New(CreateMessagePipe));
159 templ->Set(gin::StringToV8(isolate, "writeMessage"),
160 v8::FunctionTemplate::New(WriteMessage));
161 templ->Set(gin::StringToV8(isolate, "readMessage"),
162 v8::FunctionTemplate::New(ReadMessage));
163
164 data->RegisterObjectTemplate(&g_core_wrapper_info, templ);
165 }
166
167 return templ;
168 }
169
170 } // namespace js
171 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698