OLD | NEW |
---|---|
(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/v8_core.h" | |
6 | |
7 #include "mojo/public/bindings/js/v8_array_buffer.h" | |
8 #include "mojo/public/bindings/js/v8_conversions.h" | |
9 #include "mojo/public/bindings/js/v8_exceptions.h" | |
10 #include "mojo/public/bindings/js/v8_per_isolate_data.h" | |
11 | |
12 namespace mojo { | |
13 namespace js { | |
14 | |
15 namespace { | |
16 | |
17 static WrapperInfo g_handle_wrapper_info = {}; | |
Aaron Boodman
2013/11/09 08:26:02
We aren't supposed to have non-primitive static da
abarth-chromium
2013/11/09 08:52:37
WrapperInfo is POD. It doesn't have any static in
Aaron Boodman
2013/11/09 21:12:12
It seems like that should be OK logically, but I v
| |
18 | |
19 bool ToHandleArray(v8::Isolate* isolate, | |
20 v8::Handle<v8::Value> value, | |
21 std::vector<mojo::Handle>* handles) { | |
22 if (!value->IsArray()) { | |
Aaron Boodman
2013/11/09 08:26:02
It seems like Wrapper<std::vector<mojo::Handle>> s
abarth-chromium
2013/11/09 08:52:37
Good point.
| |
23 ThrowTypeError(isolate, "Expected array argument."); | |
24 return false; | |
25 } | |
26 if (!ToNativeArray(v8::Handle<v8::Array>::Cast(value), handles)) { | |
27 ThrowTypeError(isolate, "Expected an array of mojo::Handles."); | |
28 return false; | |
29 } | |
30 return true; | |
31 } | |
32 | |
33 void CloseCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { | |
Aaron Boodman
2013/11/09 08:26:02
I think the term 'callback' here is confusing beca
abarth-chromium
2013/11/09 08:52:37
Sorry, that's terminology from the Blink bindings
| |
34 v8::Isolate* isolate = info.GetIsolate(); | |
35 mojo::Handle handle = Wrapper<Handle>::ToNativeUnchecked(info.Holder()); | |
36 | |
37 MojoResult result = mojo::Close(handle); | |
38 info.GetReturnValue().Set(Wrapper<MojoResult>::ToObject(isolate, result)); | |
39 } | |
40 | |
41 void WaitCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { | |
42 v8::Isolate* isolate = info.GetIsolate(); | |
43 | |
44 if (info.Length() < 2) | |
Aaron Boodman
2013/11/09 08:26:02
In my experience, this kind of code quickly gets i
abarth-chromium
2013/11/09 08:52:37
Ok. I'll work something up along those lines.
| |
45 return ThrowTypeError(isolate, kInsufficientArguments); | |
46 | |
47 mojo::Handle handle = Wrapper<Handle>::ToNativeUnchecked(info.Holder()); | |
48 MojoWaitFlags flags = Wrapper<MojoWaitFlags>::ToNative(info[0]); | |
49 MojoDeadline deadline = Wrapper<MojoDeadline>::ToNative(info[1]); | |
50 | |
51 MojoResult result = mojo::Wait(handle, flags, deadline); | |
52 info.GetReturnValue().Set(Wrapper<MojoResult>::ToObject(isolate, result)); | |
53 } | |
54 | |
55 void WriteMessageCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { | |
56 v8::Isolate* isolate = info.GetIsolate(); | |
57 | |
58 if (info.Length() < 3) | |
59 return ThrowTypeError(isolate, kInsufficientArguments); | |
60 | |
61 mojo::Handle handle = Wrapper<Handle>::ToNativeUnchecked(info.Holder()); | |
62 | |
63 if (!info[0]->IsArrayBufferView()) | |
64 return ThrowTypeError(isolate, "Expected an ArrayBufferView."); | |
65 BufferView buffer(isolate, v8::Handle<v8::ArrayBufferView>::Cast(info[0])); | |
Aaron Boodman
2013/11/09 08:26:02
It would be cool if BufferView could again have a
abarth-chromium
2013/11/09 08:52:37
Ok. I'll have to do some more work to make Buffer
| |
66 | |
67 std::vector<mojo::Handle> handles; | |
68 if (!ToHandleArray(isolate, info[1], &handles)) | |
69 return; | |
70 | |
71 MojoWaitFlags flags = Wrapper<MojoWaitFlags>::ToNative(info[2]); | |
72 | |
73 MojoResult result = mojo::WriteMessage(handle, | |
74 buffer.bytes(), | |
75 buffer.num_bytes(), | |
76 handles.data(), | |
77 handles.size(), | |
78 flags); | |
79 | |
80 info.GetReturnValue().Set(Wrapper<MojoResult>::ToObject(isolate, result)); | |
81 } | |
82 | |
83 void ReadMessageCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { | |
84 v8::Isolate* isolate = info.GetIsolate(); | |
85 | |
86 if (info.Length() < 3) | |
87 return ThrowTypeError(isolate, kInsufficientArguments); | |
88 | |
89 mojo::Handle handle = Wrapper<Handle>::ToNativeUnchecked(info.Holder()); | |
90 | |
91 if (!info[0]->IsArrayBufferView()) | |
92 return ThrowTypeError(isolate, "Expected an ArrayBufferView."); | |
93 BufferView buffer(isolate, v8::Handle<v8::ArrayBufferView>::Cast(info[0])); | |
94 | |
95 std::vector<mojo::Handle> handles(info[1]->Uint32Value()); | |
96 | |
97 MojoWaitFlags flags = Wrapper<MojoWaitFlags>::ToNative(info[2]); | |
98 | |
99 uint32_t num_bytes = buffer.num_bytes(); | |
100 uint32_t num_handles = handles.size(); | |
101 MojoResult result = mojo::ReadMessage(handle, | |
102 buffer.bytes(), | |
103 &num_bytes, | |
104 handles.data(), | |
105 &num_handles, | |
106 flags); | |
107 handles.resize(num_handles); | |
108 | |
109 // TODO(abarth): We should benchmark this codepath to make sure it's ok to | |
110 // allocate all this memory on each read. | |
111 | |
112 v8::Handle<v8::Object> object = v8::Object::New(); | |
113 object->Set(v8::String::NewSymbol("result"), | |
114 Wrapper<MojoResult>::ToObject(isolate, result)); | |
115 object->Set(v8::String::NewSymbol("bytesRead"), | |
116 Wrapper<uint32_t>::ToObject(isolate, num_bytes)); | |
117 object->Set(v8::String::NewSymbol("handles"), | |
118 ToObjectArray(isolate, handles)); | |
119 info.GetReturnValue().Set(object); | |
120 } | |
121 | |
122 // TODO(abarth): Implement these functions. Unlike the functions above, | |
123 // these functions don't operate on an individual handle. | |
124 // | |
125 // MojoResult WaitMany(const Handle* handles, | |
126 // const MojoWaitFlags* flags, | |
127 // uint32_t num_handles, | |
128 // MojoDeadline deadline); | |
129 // MojoResult CreateMessagePipe(Handle* handle_0, Handle* handle_1); | |
130 | |
131 } | |
132 | |
133 v8::Local<v8::ObjectTemplate> Wrapper<mojo::Handle>::CreateTemplate() { | |
134 v8::Local<v8::ObjectTemplate> handle_template = v8::ObjectTemplate::New(); | |
135 handle_template->SetInternalFieldCount(kNumberOfInternalFields); | |
136 handle_template->Set(v8::String::NewSymbol("close"), | |
137 v8::FunctionTemplate::New(CloseCallback)); | |
138 handle_template->Set(v8::String::NewSymbol("wait"), | |
139 v8::FunctionTemplate::New(WaitCallback)); | |
140 handle_template->Set(v8::String::NewSymbol("writeMessage"), | |
141 v8::FunctionTemplate::New(WriteMessageCallback)); | |
142 handle_template->Set(v8::String::NewSymbol("readMessage"), | |
143 v8::FunctionTemplate::New(ReadMessageCallback)); | |
144 return handle_template; | |
145 } | |
146 | |
147 bool Wrapper<mojo::Handle>::HasInstance(v8::Handle<v8::Object> object) { | |
148 return WrapperInfo::From(object) == &g_handle_wrapper_info; | |
149 } | |
150 | |
151 v8::Handle<v8::Object> Wrapper<mojo::Handle>::ToObject( | |
152 v8::Isolate* isolate, mojo::Handle handle) { | |
153 V8PerIsolateData* data = V8PerIsolateData::From(isolate); | |
154 v8::Local<v8::Object> object = data->handle_template()->NewInstance(); | |
155 object->SetAlignedPointerInInternalField( | |
156 kWrapperInfoIndex, &g_handle_wrapper_info); | |
157 object->SetInternalField( | |
158 kEncodedValueIndex, v8::Integer::NewFromUnsigned(handle.value, isolate)); | |
159 return object; | |
160 } | |
161 | |
162 mojo::Handle Wrapper<mojo::Handle>::ToNative( | |
163 v8::Handle<v8::Object> object) { | |
164 if (!HasInstance(object)) | |
165 return mojo::kInvalidHandle; | |
166 return ToNativeUnchecked(object); | |
167 } | |
168 | |
169 mojo::Handle Wrapper<mojo::Handle>::ToNativeUnchecked( | |
170 v8::Handle<v8::Object> object) { | |
171 v8::Handle<v8::Value> field = object->GetInternalField(kEncodedValueIndex); | |
172 mojo::Handle handle = { field->Uint32Value() }; | |
173 return handle; | |
174 } | |
175 | |
176 } // namespace js | |
177 } // mojo | |
OLD | NEW |