Chromium Code Reviews| Index: mojo/public/bindings/js/v8_core.cc |
| diff --git a/mojo/public/bindings/js/v8_core.cc b/mojo/public/bindings/js/v8_core.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..69ce205b637236ba3d8f11ccdbdf8b31798e6ff8 |
| --- /dev/null |
| +++ b/mojo/public/bindings/js/v8_core.cc |
| @@ -0,0 +1,177 @@ |
| +// 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/v8_core.h" |
| + |
| +#include "mojo/public/bindings/js/v8_array_buffer.h" |
| +#include "mojo/public/bindings/js/v8_conversions.h" |
| +#include "mojo/public/bindings/js/v8_exceptions.h" |
| +#include "mojo/public/bindings/js/v8_per_isolate_data.h" |
| + |
| +namespace mojo { |
| +namespace js { |
| + |
| +namespace { |
| + |
| +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
|
| + |
| +bool ToHandleArray(v8::Isolate* isolate, |
| + v8::Handle<v8::Value> value, |
| + std::vector<mojo::Handle>* handles) { |
| + 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.
|
| + ThrowTypeError(isolate, "Expected array argument."); |
| + return false; |
| + } |
| + if (!ToNativeArray(v8::Handle<v8::Array>::Cast(value), handles)) { |
| + ThrowTypeError(isolate, "Expected an array of mojo::Handles."); |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +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
|
| + v8::Isolate* isolate = info.GetIsolate(); |
| + mojo::Handle handle = Wrapper<Handle>::ToNativeUnchecked(info.Holder()); |
| + |
| + MojoResult result = mojo::Close(handle); |
| + info.GetReturnValue().Set(Wrapper<MojoResult>::ToObject(isolate, result)); |
| +} |
| + |
| +void WaitCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| + v8::Isolate* isolate = info.GetIsolate(); |
| + |
| + 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.
|
| + return ThrowTypeError(isolate, kInsufficientArguments); |
| + |
| + mojo::Handle handle = Wrapper<Handle>::ToNativeUnchecked(info.Holder()); |
| + MojoWaitFlags flags = Wrapper<MojoWaitFlags>::ToNative(info[0]); |
| + MojoDeadline deadline = Wrapper<MojoDeadline>::ToNative(info[1]); |
| + |
| + MojoResult result = mojo::Wait(handle, flags, deadline); |
| + info.GetReturnValue().Set(Wrapper<MojoResult>::ToObject(isolate, result)); |
| +} |
| + |
| +void WriteMessageCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| + v8::Isolate* isolate = info.GetIsolate(); |
| + |
| + if (info.Length() < 3) |
| + return ThrowTypeError(isolate, kInsufficientArguments); |
| + |
| + mojo::Handle handle = Wrapper<Handle>::ToNativeUnchecked(info.Holder()); |
| + |
| + if (!info[0]->IsArrayBufferView()) |
| + return ThrowTypeError(isolate, "Expected an ArrayBufferView."); |
| + 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
|
| + |
| + std::vector<mojo::Handle> handles; |
| + if (!ToHandleArray(isolate, info[1], &handles)) |
| + return; |
| + |
| + MojoWaitFlags flags = Wrapper<MojoWaitFlags>::ToNative(info[2]); |
| + |
| + MojoResult result = mojo::WriteMessage(handle, |
| + buffer.bytes(), |
| + buffer.num_bytes(), |
| + handles.data(), |
| + handles.size(), |
| + flags); |
| + |
| + info.GetReturnValue().Set(Wrapper<MojoResult>::ToObject(isolate, result)); |
| +} |
| + |
| +void ReadMessageCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| + v8::Isolate* isolate = info.GetIsolate(); |
| + |
| + if (info.Length() < 3) |
| + return ThrowTypeError(isolate, kInsufficientArguments); |
| + |
| + mojo::Handle handle = Wrapper<Handle>::ToNativeUnchecked(info.Holder()); |
| + |
| + if (!info[0]->IsArrayBufferView()) |
| + return ThrowTypeError(isolate, "Expected an ArrayBufferView."); |
| + BufferView buffer(isolate, v8::Handle<v8::ArrayBufferView>::Cast(info[0])); |
| + |
| + std::vector<mojo::Handle> handles(info[1]->Uint32Value()); |
| + |
| + MojoWaitFlags flags = Wrapper<MojoWaitFlags>::ToNative(info[2]); |
| + |
| + uint32_t num_bytes = buffer.num_bytes(); |
| + uint32_t num_handles = handles.size(); |
| + 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. |
| + |
| + v8::Handle<v8::Object> object = v8::Object::New(); |
| + object->Set(v8::String::NewSymbol("result"), |
| + Wrapper<MojoResult>::ToObject(isolate, result)); |
| + object->Set(v8::String::NewSymbol("bytesRead"), |
| + Wrapper<uint32_t>::ToObject(isolate, num_bytes)); |
| + object->Set(v8::String::NewSymbol("handles"), |
| + ToObjectArray(isolate, handles)); |
| + info.GetReturnValue().Set(object); |
| +} |
| + |
| +// TODO(abarth): Implement these functions. Unlike the functions above, |
| +// these functions don't operate on an individual handle. |
| +// |
| +// MojoResult WaitMany(const Handle* handles, |
| +// const MojoWaitFlags* flags, |
| +// uint32_t num_handles, |
| +// MojoDeadline deadline); |
| +// MojoResult CreateMessagePipe(Handle* handle_0, Handle* handle_1); |
| + |
| +} |
| + |
| +v8::Local<v8::ObjectTemplate> Wrapper<mojo::Handle>::CreateTemplate() { |
| + v8::Local<v8::ObjectTemplate> handle_template = v8::ObjectTemplate::New(); |
| + handle_template->SetInternalFieldCount(kNumberOfInternalFields); |
| + handle_template->Set(v8::String::NewSymbol("close"), |
| + v8::FunctionTemplate::New(CloseCallback)); |
| + handle_template->Set(v8::String::NewSymbol("wait"), |
| + v8::FunctionTemplate::New(WaitCallback)); |
| + handle_template->Set(v8::String::NewSymbol("writeMessage"), |
| + v8::FunctionTemplate::New(WriteMessageCallback)); |
| + handle_template->Set(v8::String::NewSymbol("readMessage"), |
| + v8::FunctionTemplate::New(ReadMessageCallback)); |
| + return handle_template; |
| +} |
| + |
| +bool Wrapper<mojo::Handle>::HasInstance(v8::Handle<v8::Object> object) { |
| + return WrapperInfo::From(object) == &g_handle_wrapper_info; |
| +} |
| + |
| +v8::Handle<v8::Object> Wrapper<mojo::Handle>::ToObject( |
| + v8::Isolate* isolate, mojo::Handle handle) { |
| + V8PerIsolateData* data = V8PerIsolateData::From(isolate); |
| + v8::Local<v8::Object> object = data->handle_template()->NewInstance(); |
| + object->SetAlignedPointerInInternalField( |
| + kWrapperInfoIndex, &g_handle_wrapper_info); |
| + object->SetInternalField( |
| + kEncodedValueIndex, v8::Integer::NewFromUnsigned(handle.value, isolate)); |
| + return object; |
| +} |
| + |
| +mojo::Handle Wrapper<mojo::Handle>::ToNative( |
| + v8::Handle<v8::Object> object) { |
| + if (!HasInstance(object)) |
| + return mojo::kInvalidHandle; |
| + return ToNativeUnchecked(object); |
| +} |
| + |
| +mojo::Handle Wrapper<mojo::Handle>::ToNativeUnchecked( |
| + v8::Handle<v8::Object> object) { |
| + v8::Handle<v8::Value> field = object->GetInternalField(kEncodedValueIndex); |
| + mojo::Handle handle = { field->Uint32Value() }; |
| + return handle; |
| +} |
| + |
| +} // namespace js |
| +} // mojo |