| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdio.h> | 5 #include <stdio.h> |
| 6 #include <string.h> | 6 #include <string.h> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "builtin.h" |
| 12 #include "dart/runtime/include/dart_api.h" | 13 #include "dart/runtime/include/dart_api.h" |
| 13 #include "mojo/dart/embedder/builtin.h" | |
| 14 #include "mojo/public/c/system/core.h" | 14 #include "mojo/public/c/system/core.h" |
| 15 #include "mojo/public/cpp/system/core.h" | 15 #include "mojo/public/cpp/system/core.h" |
| 16 | 16 |
| 17 namespace mojo { | 17 namespace blink { |
| 18 namespace dart { | 18 |
| 19 #define REGISTER_FUNCTION(name, count) \ |
| 20 { "" #name, name, count }, |
| 21 #define DECLARE_FUNCTION(name, count) \ |
| 22 extern void name(Dart_NativeArguments args); |
| 19 | 23 |
| 20 #define MOJO_NATIVE_LIST(V) \ | 24 #define MOJO_NATIVE_LIST(V) \ |
| 21 V(MojoSharedBuffer_Create, 2) \ | 25 V(MojoSharedBuffer_Create, 2) \ |
| 22 V(MojoSharedBuffer_Duplicate, 2) \ | 26 V(MojoSharedBuffer_Duplicate, 2) \ |
| 23 V(MojoSharedBuffer_Map, 5) \ | 27 V(MojoSharedBuffer_Map, 5) \ |
| 24 V(MojoSharedBuffer_Unmap, 1) \ | 28 V(MojoSharedBuffer_Unmap, 1) \ |
| 25 V(MojoDataPipe_Create, 3) \ | 29 V(MojoDataPipe_Create, 3) \ |
| 26 V(MojoDataPipe_WriteData, 4) \ | 30 V(MojoDataPipe_WriteData, 4) \ |
| 27 V(MojoDataPipe_BeginWriteData, 3) \ | 31 V(MojoDataPipe_BeginWriteData, 3) \ |
| 28 V(MojoDataPipe_EndWriteData, 2) \ | 32 V(MojoDataPipe_EndWriteData, 2) \ |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 static void SetNullReturn(Dart_NativeArguments arguments) { | 87 static void SetNullReturn(Dart_NativeArguments arguments) { |
| 84 Dart_SetReturnValue(arguments, Dart_Null()); | 88 Dart_SetReturnValue(arguments, Dart_Null()); |
| 85 } | 89 } |
| 86 | 90 |
| 87 static void SetInvalidArgumentReturn(Dart_NativeArguments arguments) { | 91 static void SetInvalidArgumentReturn(Dart_NativeArguments arguments) { |
| 88 Dart_SetIntegerReturnValue( | 92 Dart_SetIntegerReturnValue( |
| 89 arguments, static_cast<int64_t>(MOJO_RESULT_INVALID_ARGUMENT)); | 93 arguments, static_cast<int64_t>(MOJO_RESULT_INVALID_ARGUMENT)); |
| 90 } | 94 } |
| 91 | 95 |
| 92 static Dart_Handle MojoLib() { | 96 static Dart_Handle MojoLib() { |
| 93 Dart_Handle core_lib_name = Dart_NewStringFromCString("dart:mojo_core"); | 97 return DartBuiltins::LookupLibrary("dart:mojo_core"); |
| 94 return Dart_LookupLibrary(core_lib_name); | |
| 95 } | 98 } |
| 96 | 99 |
| 97 static Dart_Handle SignalsStateToDart(Dart_Handle klass, | 100 static Dart_Handle SignalsStateToDart(Dart_Handle klass, |
| 98 const MojoHandleSignalsState& state) { | 101 const MojoHandleSignalsState& state) { |
| 99 Dart_Handle arg1 = Dart_NewInteger(state.satisfied_signals); | 102 Dart_Handle arg1 = Dart_NewInteger(state.satisfied_signals); |
| 100 Dart_Handle arg2 = Dart_NewInteger(state.satisfiable_signals); | 103 Dart_Handle arg2 = Dart_NewInteger(state.satisfiable_signals); |
| 101 Dart_Handle args[] = {arg1, arg2}; | 104 Dart_Handle args[] = {arg1, arg2}; |
| 102 return Dart_New(klass, Dart_Null(), 2, args); | 105 return Dart_New(klass, Dart_Null(), 2, args); |
| 103 } | 106 } |
| 104 | 107 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 135 if (!Dart_IsInstance(mojo_handle_instance)) { | 138 if (!Dart_IsInstance(mojo_handle_instance)) { |
| 136 SetInvalidArgumentReturn(arguments); | 139 SetInvalidArgumentReturn(arguments); |
| 137 return; | 140 return; |
| 138 } | 141 } |
| 139 // TODO(zra): Here, we could check that mojo_handle_instance is really a | 142 // TODO(zra): Here, we could check that mojo_handle_instance is really a |
| 140 // MojoHandle instance, but with the Dart API it's not too easy to get a Type | 143 // MojoHandle instance, but with the Dart API it's not too easy to get a Type |
| 141 // object from the class name outside of the root library. For now, we'll rely | 144 // object from the class name outside of the root library. For now, we'll rely |
| 142 // on the existence of the right fields to be sufficient. | 145 // on the existence of the right fields to be sufficient. |
| 143 | 146 |
| 144 Dart_Handle raw_mojo_handle_instance = Dart_GetField( | 147 Dart_Handle raw_mojo_handle_instance = Dart_GetField( |
| 145 mojo_handle_instance, Dart_NewStringFromCString("_handle")); | 148 mojo_handle_instance, ToDart("_handle")); |
| 146 if (Dart_IsError(raw_mojo_handle_instance)) { | 149 if (Dart_IsError(raw_mojo_handle_instance)) { |
| 147 SetInvalidArgumentReturn(arguments); | 150 SetInvalidArgumentReturn(arguments); |
| 148 return; | 151 return; |
| 149 } | 152 } |
| 150 | 153 |
| 151 Dart_Handle mojo_handle = Dart_GetField( | 154 Dart_Handle mojo_handle = Dart_GetField( |
| 152 raw_mojo_handle_instance, Dart_NewStringFromCString("h")); | 155 raw_mojo_handle_instance, ToDart("h")); |
| 153 if (Dart_IsError(mojo_handle)) { | 156 if (Dart_IsError(mojo_handle)) { |
| 154 SetInvalidArgumentReturn(arguments); | 157 SetInvalidArgumentReturn(arguments); |
| 155 return; | 158 return; |
| 156 } | 159 } |
| 157 | 160 |
| 158 int64_t raw_handle = static_cast<int64_t>(MOJO_HANDLE_INVALID); | 161 int64_t raw_handle = static_cast<int64_t>(MOJO_HANDLE_INVALID); |
| 159 Dart_Handle result = Dart_IntegerToInt64(mojo_handle, &raw_handle); | 162 Dart_Handle result = Dart_IntegerToInt64(mojo_handle, &raw_handle); |
| 160 if (Dart_IsError(result)) { | 163 if (Dart_IsError(result)) { |
| 161 SetInvalidArgumentReturn(arguments); | 164 SetInvalidArgumentReturn(arguments); |
| 162 return; | 165 return; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 192 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); | 195 CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument); |
| 193 CHECK_INTEGER_ARGUMENT(arguments, 1, &signals, InvalidArgument); | 196 CHECK_INTEGER_ARGUMENT(arguments, 1, &signals, InvalidArgument); |
| 194 CHECK_INTEGER_ARGUMENT(arguments, 2, &deadline, InvalidArgument); | 197 CHECK_INTEGER_ARGUMENT(arguments, 2, &deadline, InvalidArgument); |
| 195 | 198 |
| 196 MojoHandleSignalsState state; | 199 MojoHandleSignalsState state; |
| 197 MojoResult r = mojo::Wait(mojo::Handle(static_cast<MojoHandle>(handle)), | 200 MojoResult r = mojo::Wait(mojo::Handle(static_cast<MojoHandle>(handle)), |
| 198 static_cast<MojoHandleSignals>(signals), | 201 static_cast<MojoHandleSignals>(signals), |
| 199 static_cast<MojoDeadline>(deadline), &state); | 202 static_cast<MojoDeadline>(deadline), &state); |
| 200 | 203 |
| 201 Dart_Handle klass = Dart_GetClass( | 204 Dart_Handle klass = Dart_GetClass( |
| 202 MojoLib(), Dart_NewStringFromCString("MojoHandleSignalsState")); | 205 MojoLib(), ToDart("MojoHandleSignalsState")); |
| 203 DART_CHECK_VALID(klass); | 206 DART_CHECK_VALID(klass); |
| 204 | 207 |
| 205 // The return value is structured as a list of length 2: | 208 // The return value is structured as a list of length 2: |
| 206 // [0] MojoResult | 209 // [0] MojoResult |
| 207 // [1] MojoHandleSignalsState. (may be null) | 210 // [1] MojoHandleSignalsState. (may be null) |
| 208 Dart_Handle list = Dart_NewList(2); | 211 Dart_Handle list = Dart_NewList(2); |
| 209 Dart_ListSetAt(list, 0, Dart_NewInteger(r)); | 212 Dart_ListSetAt(list, 0, Dart_NewInteger(r)); |
| 210 if (mojo::WaitManyResult(r).AreSignalsStatesValid()) { | 213 if (mojo::WaitManyResult(r).AreSignalsStatesValid()) { |
| 211 Dart_ListSetAt(list, 1, SignalsStateToDart(klass, state)); | 214 Dart_ListSetAt(list, 1, SignalsStateToDart(klass, state)); |
| 212 } else { | 215 } else { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 Dart_IntegerToInt64(dart_signal, &mojo_signal); | 254 Dart_IntegerToInt64(dart_signal, &mojo_signal); |
| 252 mojo_handles[i] = mojo::Handle(mojo_handle); | 255 mojo_handles[i] = mojo::Handle(mojo_handle); |
| 253 mojo_signals[i] = static_cast<MojoHandleSignals>(mojo_signal); | 256 mojo_signals[i] = static_cast<MojoHandleSignals>(mojo_signal); |
| 254 } | 257 } |
| 255 | 258 |
| 256 std::vector<MojoHandleSignalsState> states(handles_len); | 259 std::vector<MojoHandleSignalsState> states(handles_len); |
| 257 mojo::WaitManyResult wmr = mojo::WaitMany( | 260 mojo::WaitManyResult wmr = mojo::WaitMany( |
| 258 mojo_handles, mojo_signals, static_cast<MojoDeadline>(deadline), &states); | 261 mojo_handles, mojo_signals, static_cast<MojoDeadline>(deadline), &states); |
| 259 | 262 |
| 260 Dart_Handle klass = Dart_GetClass( | 263 Dart_Handle klass = Dart_GetClass( |
| 261 MojoLib(), Dart_NewStringFromCString("MojoHandleSignalsState")); | 264 MojoLib(), ToDart("MojoHandleSignalsState")); |
| 262 DART_CHECK_VALID(klass); | 265 DART_CHECK_VALID(klass); |
| 263 | 266 |
| 264 // The return value is structured as a list of length 3: | 267 // The return value is structured as a list of length 3: |
| 265 // [0] MojoResult | 268 // [0] MojoResult |
| 266 // [1] index of handle that caused a return (may be null) | 269 // [1] index of handle that caused a return (may be null) |
| 267 // [2] list of MojoHandleSignalsState. (may be null) | 270 // [2] list of MojoHandleSignalsState. (may be null) |
| 268 Dart_Handle list = Dart_NewList(3); | 271 Dart_Handle list = Dart_NewList(3); |
| 269 Dart_ListSetAt(list, 0, Dart_NewInteger(wmr.result)); | 272 Dart_ListSetAt(list, 0, Dart_NewInteger(wmr.result)); |
| 270 if (wmr.IsIndexValid()) | 273 if (wmr.IsIndexValid()) |
| 271 Dart_ListSetAt(list, 1, Dart_NewInteger(wmr.index)); | 274 Dart_ListSetAt(list, 1, Dart_NewInteger(wmr.index)); |
| (...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 791 int64_t control_handle; | 794 int64_t control_handle; |
| 792 CHECK_INTEGER_ARGUMENT(arguments, 0, &control_handle, InvalidArgument); | 795 CHECK_INTEGER_ARGUMENT(arguments, 0, &control_handle, InvalidArgument); |
| 793 mojo_control_handle = control_handle; | 796 mojo_control_handle = control_handle; |
| 794 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); | 797 Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(MOJO_RESULT_OK)); |
| 795 } | 798 } |
| 796 | 799 |
| 797 void MojoHandleWatcher_GetControlHandle(Dart_NativeArguments arguments) { | 800 void MojoHandleWatcher_GetControlHandle(Dart_NativeArguments arguments) { |
| 798 Dart_SetIntegerReturnValue(arguments, mojo_control_handle); | 801 Dart_SetIntegerReturnValue(arguments, mojo_control_handle); |
| 799 } | 802 } |
| 800 | 803 |
| 801 } // namespace dart | 804 } // namespace blink |
| 802 } // namespace mojo | |
| OLD | NEW |