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

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: Fix skipped comment 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) {
21 gin::Arguments args(info);
22
23 mojo::Handle handle = mojo::kInvalidHandle;
24 if (!args.GetNext(&handle))
25 return args.ThrowError();
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 = mojo::kInvalidHandle;
34 MojoWaitFlags flags = MOJO_WAIT_FLAG_NONE;
35 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE;
36
37 if (!args.GetNext(&handle) ||
38 !args.GetNext(&flags) ||
39 !args.GetNext(&deadline)) {
40 return args.ThrowError();
41 }
42
43 args.Return(mojo::Wait(handle, flags, deadline));
44 }
45
46 void WaitMany(const v8::FunctionCallbackInfo<v8::Value>& info) {
47 gin::Arguments args(info);
48
49 std::vector<mojo::Handle> handles;
50 std::vector<MojoWaitFlags> flags;
51 MojoDeadline deadline = MOJO_DEADLINE_INDEFINITE;
52
53 if (!args.GetNext(&handles) ||
54 !args.GetNext(&flags) ||
55 !args.GetNext(&deadline)) {
56 return args.ThrowError();
57 }
58
59 if (handles.size() != flags.size())
60 return args.ThrowTypeError("Arrays must have the same length.");
61
62 args.Return(mojo::WaitMany(handles.data(), flags.data(),
63 handles.size(), deadline));
64 }
65
66 void CreateMessagePipe(const v8::FunctionCallbackInfo<v8::Value>& info) {
67 gin::Arguments args(info);
68
69 mojo::Handle handle_0 = mojo::kInvalidHandle;
70 mojo::Handle handle_1 = mojo::kInvalidHandle;
71 MojoResult result = mojo::CreateMessagePipe(&handle_0, &handle_1);
72
73 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(info.GetIsolate());
74 dictionary.Set("result", result);
75 dictionary.Set("handle0", handle_0);
76 dictionary.Set("handle1", handle_1);
77 args.Return(dictionary);
78 }
79
80 void WriteMessage(const v8::FunctionCallbackInfo<v8::Value>& info) {
81 gin::Arguments args(info);
82
83 mojo::Handle handle = mojo::kInvalidHandle;
84 gin::ArrayBufferView buffer(args.isolate());
85 std::vector<mojo::Handle> handles;
86 MojoWaitFlags flags = MOJO_WAIT_FLAG_NONE;
87
88 if (!args.GetNext(&handle) ||
89 !args.GetNext(&buffer) ||
90 !args.GetNext(&handles) ||
91 !args.GetNext(&flags)) {
92 return args.ThrowError();
93 }
94
95 args.Return(mojo::WriteMessage(handle, buffer.bytes(), buffer.num_bytes(),
96 handles.data(), handles.size(), flags));
97 }
98
99 void ReadMessage(const v8::FunctionCallbackInfo<v8::Value>& info) {
100 gin::Arguments args(info);
101
102 mojo::Handle handle = mojo::kInvalidHandle;
103 gin::ArrayBufferView buffer(args.isolate());
104 uint32_t num_handles = 0;
105 MojoWaitFlags flags = MOJO_WAIT_FLAG_NONE;
106
107 if (!args.GetNext(&handle) ||
108 !args.GetNext(&buffer) ||
109 !args.GetNext(&num_handles) ||
110 !args.GetNext(&flags)) {
111 return args.ThrowError();
112 }
113
114 uint32_t num_bytes = buffer.num_bytes();
115 std::vector<mojo::Handle> handles(num_handles);
116 MojoResult result = mojo::ReadMessage(handle, buffer.bytes(), &num_bytes,
117 handles.data(), &num_handles, flags);
118 handles.resize(num_handles);
119
120 // TODO(abarth): We should benchmark this codepath to make sure it's ok to
121 // allocate all this memory on each read.
122 gin::Dictionary dictionary = gin::Dictionary::CreateEmpty(info.GetIsolate());
123 dictionary.Set("result", result);
124 dictionary.Set("bytesRead", num_bytes);
125 dictionary.Set("handles", handles);
126 args.Return(dictionary);
127 }
128
129 gin::WrapperInfo g_core_wrapper_info = {};
130
131 }
132
133 v8::Local<v8::ObjectTemplate> GetCoreTemplate(v8::Isolate* isolate) {
134 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
135 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate(
136 &g_core_wrapper_info);
137
138 if (templ.IsEmpty()) {
139 templ = v8::ObjectTemplate::New();
140
141 templ->Set(gin::StringToSymbol(isolate, "close"),
142 v8::FunctionTemplate::New(Close));
143 templ->Set(gin::StringToSymbol(isolate, "wait"),
144 v8::FunctionTemplate::New(Wait));
145 templ->Set(gin::StringToSymbol(isolate, "waitMany"),
146 v8::FunctionTemplate::New(WaitMany));
147 templ->Set(gin::StringToSymbol(isolate, "createMessagePipe"),
148 v8::FunctionTemplate::New(CreateMessagePipe));
149 templ->Set(gin::StringToSymbol(isolate, "writeMessage"),
150 v8::FunctionTemplate::New(WriteMessage));
151 templ->Set(gin::StringToSymbol(isolate, "readMessage"),
152 v8::FunctionTemplate::New(ReadMessage));
153
154 data->SetObjectTemplate(&g_core_wrapper_info, templ);
155 }
156
157 return templ;
158 }
159
160 } // namespace js
161 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698