summaryrefslogtreecommitdiffstats
path: root/mojo/public/dart
diff options
context:
space:
mode:
Diffstat (limited to 'mojo/public/dart')
-rw-r--r--mojo/public/dart/BUILD.gn46
-rw-r--r--mojo/public/dart/DEPS4
-rw-r--r--mojo/public/dart/README14
-rw-r--r--mojo/public/dart/core.dart19
-rw-r--r--mojo/public/dart/mojo_init.dart16
-rw-r--r--mojo/public/dart/src/buffer.dart107
-rw-r--r--mojo/public/dart/src/data_pipe.dart191
-rw-r--r--mojo/public/dart/src/handle.dart68
-rw-r--r--mojo/public/dart/src/message_pipe.dart155
-rw-r--r--mojo/public/dart/src/mojo_dart_core.cc659
-rw-r--r--mojo/public/dart/src/types.dart116
11 files changed, 1395 insertions, 0 deletions
diff --git a/mojo/public/dart/BUILD.gn b/mojo/public/dart/BUILD.gn
index 83c5a25..c2e8f6a 100644
--- a/mojo/public/dart/BUILD.gn
+++ b/mojo/public/dart/BUILD.gn
@@ -3,16 +3,62 @@
# found in the LICENSE file.
group("dart") {
+ testonly = true
deps = [
+ ":core",
":bindings",
]
}
+group("core") {
+ deps = [
+ ":mojo_dart_core",
+ ":copy_core_library",
+ ]
+}
+
+
+shared_library("mojo_dart_core") {
+ defines = ["DART_SHARED_LIB"]
+ sources = [
+ "src/mojo_dart_core.cc",
+ ]
+ deps = [
+ "//mojo/public/c/environment",
+ "//mojo/public/c/system:for_shared_library",
+ "//mojo/public/cpp/environment:standalone",
+ "//mojo/public/cpp/system",
+ "//mojo/public/cpp/utility",
+ "//mojo/public/cpp/bindings:callback",
+ ]
+}
+
+
+copy("copy_core_library") {
+ sources = [
+ "$root_out_dir/libmojo_dart_core.so",
+ ]
+ outputs = [
+ "$root_out_dir/gen/mojo/public/dart/src/libmojo_dart_core.so"
+ ]
+ deps =[
+ ":mojo_dart_core"
+ ]
+}
+
+
copy("bindings") {
sources = [
"bindings.dart",
+ "core.dart",
+ "mojo_init.dart",
+ "src/buffer.dart",
"src/codec.dart",
+ "src/data_pipe.dart",
+ "src/handle.dart",
+ "src/message_pipe.dart",
+ "src/types.dart",
]
outputs = [
"{{source_gen_dir}}/{{source_file_part}}"
diff --git a/mojo/public/dart/DEPS b/mojo/public/dart/DEPS
new file mode 100644
index 0000000..53d0007
--- /dev/null
+++ b/mojo/public/dart/DEPS
@@ -0,0 +1,4 @@
+include_rules = [
+ "+dart",
+ "+base",
+] \ No newline at end of file
diff --git a/mojo/public/dart/README b/mojo/public/dart/README
index aadb348..5ff7ee3 100644
--- a/mojo/public/dart/README
+++ b/mojo/public/dart/README
@@ -39,3 +39,17 @@ e.g. out/ReleaseX64/dart-sdk/bin to your path.
4.) Run Dart tests.
$ ./mojob.sh --release darttest
+
+
+These are instructions for adding a Dart VM source checkout to your client.
+
+1. Edit your .gclient file.
+
+ Replace "DEPS" with "DEPS.dart"
+
+2. Run:
+
+ $ gclient sync
+
+ You should now have a directory //src/dart
+
diff --git a/mojo/public/dart/core.dart b/mojo/public/dart/core.dart
new file mode 100644
index 0000000..5159440
--- /dev/null
+++ b/mojo/public/dart/core.dart
@@ -0,0 +1,19 @@
+// Copyright 2014 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.
+
+library core;
+
+import 'dart:async';
+import 'dart:core';
+import 'dart:isolate';
+import 'dart:typed_data';
+import 'dart-ext:src/mojo_dart_core';
+
+part 'src/buffer.dart';
+part 'src/data_pipe.dart';
+part 'src/handle.dart';
+part 'src/message_pipe.dart';
+part 'src/types.dart';
+
+void mojoSystemThunksSet(int thunks) native "MojoSystemThunks_Set";
diff --git a/mojo/public/dart/mojo_init.dart b/mojo/public/dart/mojo_init.dart
new file mode 100644
index 0000000..c622f5f
--- /dev/null
+++ b/mojo/public/dart/mojo_init.dart
@@ -0,0 +1,16 @@
+// Copyright 2014 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.
+
+library mojo_init;
+
+import 'core.dart' as core;
+import 'dart-ext:src/mojo_dart_init';
+
+void _init() native "MojoLibrary_Init";
+void _mojoSystemThunksMake(Function fn) native "MojoSystemThunks_Make";
+
+void mojoInit() {
+ _init();
+ _mojoSystemThunksMake(core.mojoSystemThunksSet);
+}
diff --git a/mojo/public/dart/src/buffer.dart b/mojo/public/dart/src/buffer.dart
new file mode 100644
index 0000000..a00838e
--- /dev/null
+++ b/mojo/public/dart/src/buffer.dart
@@ -0,0 +1,107 @@
+// Copyright 2014 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.
+
+part of core;
+
+class _MojoSharedBufferNatives {
+ static List Create(int num_bytes, int flags)
+ native "MojoSharedBuffer_Create";
+
+ static List Duplicate(int buffer_handle, int flags)
+ native "MojoSharedBuffer_Duplicate";
+
+ static List Map(int buffer_handle, int offset, int num_bytes, int flags)
+ native "MojoSharedBuffer_Map";
+
+ static int Unmap(ByteData buffer)
+ native "MojoSharedBuffer_Unmap";
+}
+
+
+class MojoSharedBuffer {
+ static const int CREATE_FLAG_NONE = 0;
+ static const int DUPLICATE_FLAG_NONE = 0;
+ static const int MAP_FLAG_NONE = 0;
+
+ RawMojoHandle handle;
+ MojoResult status;
+ ByteData mapping;
+
+ MojoSharedBuffer._() {
+ handle = null;
+ status = MojoResult.OK;
+ mapping = null;
+ }
+
+ factory MojoSharedBuffer(int num_bytes, [int flags = 0]) {
+ List result = _MojoSharedBufferNatives.Create(num_bytes, flags);
+ if (result == null) {
+ return null;
+ }
+ assert((result is List) && (result.length == 2));
+ var r = new MojoResult(result[0]);
+ if (!r.isOk) {
+ return null;
+ }
+
+ MojoSharedBuffer buf = new MojoSharedBuffer._();
+ buf.status = r;
+ buf.handle = new RawMojoHandle(result[1]);
+ buf.mapping = null;
+ return buf;
+ }
+
+ factory MojoSharedBuffer.duplicate(MojoSharedBuffer msb, [int flags = 0]) {
+ List result = _MojoSharedBufferNatives.Duplicate(msb.handle.h, flags);
+ if (result == null) {
+ return null;
+ }
+ assert((result is List) && (result.length == 2));
+ var r = new MojoResult(result[0]);
+ if(!r.isOk) {
+ return null;
+ }
+
+ MojoSharedBuffer dupe = new MojoSharedBuffer._();
+ dupe.status = r;
+ dupe.handle = new RawMojoHandle(result[1]);
+ dupe.mapping = msb.mapping;
+ return dupe;
+ }
+
+ MojoResult close() {
+ if (handle == null) {
+ status = MojoResult.INVALID_ARGUMENT;
+ return status;
+ }
+ MojoResult r = handle.close();
+ status = r;
+ mapping = null;
+ return status;
+ }
+
+ MojoResult map(int offset, int num_bytes, [int flags = 0]) {
+ if (handle == null) {
+ status = MojoResult.INVALID_ARGUMENT;
+ return status;
+ }
+ List result = _MojoSharedBufferNatives.Map(
+ handle.h, offset, num_bytes, flags);
+ if (result == null) {
+ status = MojoResult.INVALID_ARGUMENT;
+ return status;
+ }
+ assert((result is List) && (result.length == 2));
+ status = new MojoResult(result[0]);
+ mapping = result[1];
+ return status;
+ }
+
+ MojoResult unmap() {
+ int r = _MojoSharedBufferNatives.Unmap(mapping);
+ status = new MojoResult(r);
+ mapping = null;
+ return status;
+ }
+}
diff --git a/mojo/public/dart/src/data_pipe.dart b/mojo/public/dart/src/data_pipe.dart
new file mode 100644
index 0000000..665285d
--- /dev/null
+++ b/mojo/public/dart/src/data_pipe.dart
@@ -0,0 +1,191 @@
+// Copyright 2014 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.
+
+part of core;
+
+
+class _MojoDataPipeNatives {
+ static List MojoCreateDataPipe(
+ int element_bytes, int capacity_bytes, int flags)
+ native "MojoDataPipe_Create";
+
+ static List MojoWriteData(int handle, ByteData data, int num_bytes, int flags)
+ native "MojoDataPipe_WriteData";
+
+ static List MojoBeginWriteData(int handle, int buffer_bytes, int flags)
+ native "MojoDataPipe_BeginWriteData";
+
+ static int MojoEndWriteData(int handle, int bytes_written)
+ native "MojoDataPipe_EndWriteData";
+
+ static List MojoReadData(int handle, ByteData data, int num_bytes, int flags)
+ native "MojoDataPipe_ReadData";
+
+ static List MojoBeginReadData(int handle, int buffer_bytes, int flags)
+ native "MojoDataPipe_BeginReadData";
+
+ static int MojoEndReadData(int handle, int bytes_read)
+ native "MojoDataPipe_EndReadData";
+}
+
+
+class MojoDataPipeProducer {
+ static const int FLAG_NONE = 0;
+ static const int FLAG_ALL_OR_NONE = 1 << 0;
+
+ RawMojoHandle handle;
+ MojoResult status;
+ final int element_bytes;
+
+ MojoDataPipeProducer(this.handle,
+ this.status,
+ this.element_bytes);
+
+ int write(ByteData data, [int num_bytes = -1, int flags = 0]) {
+ if (handle == null) {
+ status = MojoResult.INVALID_ARGUMENT;
+ return status;
+ }
+
+ int data_num_bytes = (num_bytes == -1) ? data.lengthInBytes : num_bytes;
+ List result = _MojoDataPipeNatives.MojoWriteData(
+ handle.h, data, data_num_bytes, flags);
+ if (result == null) {
+ status = MojoResult.INVALID_ARGUMENT;
+ return status;
+ }
+
+ assert((result is List) && (result.length == 2));
+ status = new MojoResult(result[0]);
+ return result[1];
+ }
+
+ ByteData beginWrite(int buffer_bytes, [int flags = 0]) {
+ if (handle == null) {
+ status = MojoResult.INVALID_ARGUMENT;
+ return null;
+ }
+
+ List result = _MojoDataPipeNatives.MojoBeginWriteData(
+ handle.h, buffer_bytes, flags);
+ if (result == null) {
+ status = MojoResult.INVALID_ARGUMENT;
+ return null;
+ }
+
+ assert((result is List) && (result.length == 2));
+ status = new MojoResult(result[0]);
+ return result[1];
+ }
+
+ MojoResult endWrite(int bytes_written) {
+ if (handle == null) {
+ status = MojoResult.INVALID_ARGUMENT;
+ return status;
+ }
+ int result = _MojoDataPipeNatives.MojoEndWriteData(handle.h, bytes_written);
+ status = new MojoResult(result);
+ return status;
+ }
+}
+
+
+class MojoDataPipeConsumer {
+ static const int FLAG_NONE = 0;
+ static const int FLAG_ALL_OR_NONE = 1 << 0;
+ static const int FLAG_MAY_DISCARD = 1 << 1;
+ static const int FLAG_QUERY = 1 << 2;
+
+ RawMojoHandle handle;
+ MojoResult status;
+ final int element_bytes;
+
+ MojoDataPipeConsumer(this.handle,
+ this.status,
+ this.element_bytes);
+
+ int read(ByteData data, [int num_bytes = -1, int flags = 0]) {
+ if (handle == null) {
+ status = MojoResult.INVALID_ARGUMENT;
+ return status;
+ }
+
+ int data_num_bytes = (num_bytes == -1) ? data.lengthInBytes : num_bytes;
+ List result = _MojoDataPipeNatives.MojoReadData(
+ handle.h, data, data_num_bytes, flags);
+ if (result == null) {
+ status = MojoResult.INVALID_ARGUMENT;
+ return status;
+ }
+ assert((result is List) && (result.length == 2));
+ status = new MojoResult(result[0]);
+ return result[1];
+ }
+
+ ByteData beginRead(int buffer_bytes, [int flags = 0]) {
+ if (handle == null) {
+ status = MojoResult.INVALID_ARGUMENT;
+ return null;
+ }
+
+ List result = _MojoDataPipeNatives.MojoBeginReadData(
+ handle.h, buffer_bytes, flags);
+ if (result == null) {
+ status = MojoResult.INVALID_ARGUMENT;
+ return null;
+ }
+
+ assert((result is List) && (result.length == 2));
+ status = new MojoResult(result[0]);
+ return result[1];
+ }
+
+ MojoResult endRead(int bytes_read) {
+ if (handle == null) {
+ status = MojoResult.INVALID_ARGUMENT;
+ return status;
+ }
+ int result = _MojoDataPipeNatives.MojoEndReadData(handle.h, bytes_read);
+ status = new MojoResult(result);
+ return status;
+ }
+}
+
+
+class MojoDataPipe {
+ static const int FLAG_NONE = 0;
+ static const int FLAG_MAY_DISCARD = 1 << 0;
+ static const int DEFAULT_ELEMENT_SIZE = 1;
+ static const int DEFAULT_CAPACITY = 0;
+
+ MojoDataPipeProducer producer;
+ MojoDataPipeConsumer consumer;
+ MojoResult status;
+
+ MojoDataPipe._internal() {
+ producer = null;
+ consumer = null;
+ status = MojoResult.OK;
+ }
+
+ factory MojoDataPipe([int element_bytes = DEFAULT_ELEMENT_SIZE,
+ int capacity_bytes = DEFAULT_CAPACITY,
+ int flags = FLAG_NONE]) {
+ List result = _MojoDataPipeNatives.MojoCreateDataPipe(
+ element_bytes, capacity_bytes, flags);
+ if (result == null) {
+ return null;
+ }
+ assert((result is List) && (result.length == 3));
+ RawMojoHandle producer_handle = new RawMojoHandle(result[1]);
+ RawMojoHandle consumer_handle = new RawMojoHandle(result[2]);
+ MojoDataPipe pipe = new MojoDataPipe._internal();
+ pipe.producer = new MojoDataPipeProducer(
+ producer_handle, new MojoResult(result[0]), element_bytes);
+ pipe.consumer = new MojoDataPipeConsumer(
+ consumer_handle, new MojoResult(result[0]), element_bytes);
+ pipe.status = new MojoResult(result[0]);
+ return pipe;
+ }
+}
diff --git a/mojo/public/dart/src/handle.dart b/mojo/public/dart/src/handle.dart
new file mode 100644
index 0000000..08a23ae
--- /dev/null
+++ b/mojo/public/dart/src/handle.dart
@@ -0,0 +1,68 @@
+// Copyright 2014 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.
+
+part of core;
+
+
+class _MojoHandleNatives {
+ static int close(int handle) native "MojoHandle_Close";
+ static int wait(int handle, int signals, int deadline)
+ native "MojoHandle_Wait";
+ static int waitMany(
+ List handles, List signals, int num_handles, int deadline)
+ native "MojoHandle_WaitMany";
+}
+
+
+class RawMojoHandle {
+ static const int INVALID = 0;
+ static const int DEADLINE_INDEFINITE = -1;
+
+ RawMojoHandle(this.h);
+
+ MojoResult close() {
+ int result = _MojoHandleNatives.close(h);
+ h = INVALID;
+ return new MojoResult(result);
+ }
+
+ MojoResult wait(int signals, int deadline) {
+ int result = _MojoHandleNatives.wait(h, signals, deadline);
+ return new MojoResult(result);
+ }
+
+ bool _ready(int signal) {
+ MojoResult res = wait(signal, 0);
+ switch (res) {
+ case MojoResult.OK:
+ return true;
+ case MojoResult.DEADLINE_EXCEEDED:
+ case MojoResult.CANCELLED:
+ case MojoResult.INVALID_ARGUMENT:
+ case MojoResult.FAILED_PRECONDITION:
+ return false;
+ default:
+ // Should be unreachable.
+ throw new Exception("Unreachable");
+ }
+ }
+
+ bool readyRead() => _ready(MojoHandleSignals.READABLE);
+ bool readyWrite() => _ready(MojoHandleSignals.WRITABLE);
+
+ static MojoResult waitMany(List<int> handles,
+ List<int> signals,
+ int deadline) {
+ if (handles.length != signals.length) {
+ return MojoResult.INVALID_ARGUMENT;
+ }
+ int result = _MojoHandleNatives.waitMany(
+ handles, signals, handles.length, deadline);
+ return new MojoResult(result);
+ }
+
+ static bool isValid(RawMojoHandle h) => (h.h != INVALID);
+
+ int h;
+}
diff --git a/mojo/public/dart/src/message_pipe.dart b/mojo/public/dart/src/message_pipe.dart
new file mode 100644
index 0000000..81d9f28
--- /dev/null
+++ b/mojo/public/dart/src/message_pipe.dart
@@ -0,0 +1,155 @@
+// Copyright 2014 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.
+
+part of core;
+
+
+class _MojoMessagePipeNatives {
+ static List MojoCreateMessagePipe(int flags)
+ native "MojoMessagePipe_Create";
+
+ static int MojoWriteMessage(
+ int handle, ByteData data, int num_bytes, List<int> handles, int flags)
+ native "MojoMessagePipe_Write";
+
+ static List MojoReadMessage(
+ int handle, ByteData data, int num_bytes, List<int> handles, int flags)
+ native "MojoMessagePipe_Read";
+}
+
+
+class MojoMessagePipeReadResult {
+ final MojoResult status;
+ final int bytesRead;
+ final int handlesRead;
+
+ MojoMessagePipeReadResult(this.status, this.bytesRead, this.handlesRead);
+ MojoMessagePipeReadResult.fromList(List<int> resultList)
+ : this(new MojoResult(resultList[0]), resultList[1], resultList[2]);
+}
+
+
+class MojoMessagePipeEndpoint {
+ static const int WRITE_FLAG_NONE = 0;
+ static const int READ_FLAG_NONE = 0;
+ static const int READ_FLAG_MAY_DISCARD = 0;
+
+ RawMojoHandle handle;
+ MojoResult status;
+
+ MojoMessagePipeEndpoint(this.handle);
+
+ MojoResult write(ByteData data,
+ [int num_bytes = -1,
+ List<RawMojoHandle> handles = null,
+ int flags = 0]) {
+ if (handle == null) {
+ status = MojoResult.INVALID_ARGUMENT;
+ return status;
+ }
+
+ // If num_bytes has the default value, use the full length of the data.
+ int data_num_bytes = (num_bytes == -1) ? data.lengthInBytes : num_bytes;
+ if (data_num_bytes > data.lengthInBytes) {
+ status = MojoResult.INVALID_ARGUMENT;
+ return status;
+ }
+
+ // handles may be null, otherwise convert to ints.
+ List<int> mojo_handles =
+ (handles != null) ? handles.map((h) => h.h).toList() : null;
+
+ // Do the call.
+ int result = _MojoMessagePipeNatives.MojoWriteMessage(
+ handle.h, data, data_num_bytes, mojo_handles, flags);
+
+ status = new MojoResult(result);
+ return status;
+ }
+
+
+ MojoMessagePipeReadResult read(ByteData data,
+ [int num_bytes = -1,
+ List<RawMojoHandle> handles = null,
+ int flags = 0]) {
+ if (handle == null) {
+ status = MojoResult.INVALID_ARGUMENT;
+ return null;
+ }
+
+ // If num_bytes has the default value, use the full length of the data.
+ int data_num_bytes;
+ if (data == null) {
+ data_num_bytes = 0;
+ } else {
+ data_num_bytes = (num_bytes == -1) ? data.lengthInBytes : num_bytes;
+ }
+ if (data_num_bytes > data.lengthInBytes) {
+ status = MojoResult.INVALID_ARGUMENT;
+ return status;
+ }
+
+ // handles may be null, otherwise make an int list for the handles.
+ List<int> mojo_handles;
+ if (handles == null) {
+ mojo_handles = null;
+ } else {
+ mojo_handles = new List<int>(handles.length);
+ }
+
+ // Do the call.
+ List result = _MojoMessagePipeNatives.MojoReadMessage(
+ handle.h, data, data_num_bytes, mojo_handles, flags);
+
+ if (result == null) {
+ status = MojoResult.INVALID_ARGUMENT;
+ return null;
+ }
+
+ assert((result is List) && (result.length == 3));
+ var readResult = new MojoMessagePipeReadResult.fromList(result);
+
+ // Copy out the handles that were read.
+ if (handles != null) {
+ for (var i = 0; i < readResult.handlesRead; i++) {
+ handles[i].h = mojo_handles[i];
+ }
+ }
+
+ status = readResult.status;
+ return readResult;
+ }
+
+ MojoMessagePipeReadResult query() => read(null);
+}
+
+
+class MojoMessagePipe {
+ static const int FLAG_NONE = 0;
+
+ List<MojoMessagePipeEndpoint> endpoints;
+ MojoResult status;
+
+ MojoMessagePipe._() {
+ endpoints = null;
+ status = MojoResult.OK;
+ }
+
+ factory MojoMessagePipe([int flags = FLAG_NONE]) {
+ List result = _MojoMessagePipeNatives.MojoCreateMessagePipe(flags);
+ if (result == null) {
+ return null;
+ }
+ assert((result is List) && (result.length == 3));
+
+ RawMojoHandle end1 = new RawMojoHandle(result[1]);
+ RawMojoHandle end2 = new RawMojoHandle(result[2]);
+ MojoMessagePipe pipe = new MojoMessagePipe._();
+ pipe.endpoints = new List(2);
+ pipe.endpoints[0] = new MojoMessagePipeEndpoint(end1);
+ pipe.endpoints[1] = new MojoMessagePipeEndpoint(end2);
+ pipe.status = new MojoResult(result[0]);
+ return pipe;
+ }
+}
diff --git a/mojo/public/dart/src/mojo_dart_core.cc b/mojo/public/dart/src/mojo_dart_core.cc
new file mode 100644
index 0000000..7cb50d8
--- /dev/null
+++ b/mojo/public/dart/src/mojo_dart_core.cc
@@ -0,0 +1,659 @@
+// Copyright 2014 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 <stdio.h>
+#include <string.h>
+
+#include "base/memory/scoped_ptr.h"
+#include "dart/runtime/include/dart_api.h"
+#include "mojo/public/c/system/core.h"
+#include "mojo/public/platform/native/system_thunks.h"
+
+
+static Dart_NativeFunction ResolveName(
+ Dart_Handle name, int argc, bool* auto_setup_scope);
+
+
+DART_EXPORT Dart_Handle mojo_dart_core_Init(Dart_Handle parent_library) {
+ if (Dart_IsError(parent_library)) {
+ return parent_library;
+ }
+
+ Dart_Handle result_code = Dart_SetNativeResolver(
+ parent_library, ResolveName, NULL);
+ if (Dart_IsError(result_code)) {
+ return result_code;
+ }
+
+ return Dart_Null();
+}
+
+
+static Dart_Handle HandleError(Dart_Handle handle) {
+ if (Dart_IsError(handle)) {
+ Dart_PropagateError(handle);
+ }
+ return handle;
+}
+
+
+static void SetNullReturn(Dart_NativeArguments arguments) {
+ Dart_SetReturnValue(arguments, Dart_Null());
+}
+
+
+static void SetInvalidArgumentReturn(Dart_NativeArguments arguments) {
+ Dart_SetIntegerReturnValue(
+ arguments, static_cast<int64_t>(MOJO_RESULT_INVALID_ARGUMENT));
+}
+
+
+#define CHECK_INTEGER_ARGUMENT(args, num, result, failure) \
+ { \
+ Dart_Handle __status; \
+ __status = Dart_GetNativeIntegerArgument(args, num, result); \
+ if (Dart_IsError(__status)) { \
+ Set##failure##Return(arguments); \
+ return; \
+ } \
+ } \
+
+
+extern "C" {
+ extern size_t MojoSetSystemThunks(const MojoSystemThunks* system_thunks);
+}
+
+
+static void MojoSystemThunks_Set(Dart_NativeArguments arguments) {
+ int64_t thunks_addr = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 0, &thunks_addr, Null);
+
+ MojoSystemThunks* thunks = reinterpret_cast<MojoSystemThunks*>(thunks_addr);
+ MojoSetSystemThunks(thunks);
+
+ Dart_SetReturnValue(arguments, Dart_Null());
+}
+
+
+static void MojoHandle_Close(Dart_NativeArguments arguments) {
+ int64_t handle;
+ CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument);
+
+ MojoResult res = MojoClose(static_cast<MojoHandle>(handle));
+
+ Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res));
+}
+
+
+static void MojoHandle_Wait(Dart_NativeArguments arguments) {
+ int64_t handle = 0;
+ int64_t signals = 0;
+ int64_t deadline = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument);
+ CHECK_INTEGER_ARGUMENT(arguments, 1, &signals, InvalidArgument);
+ CHECK_INTEGER_ARGUMENT(arguments, 2, &deadline, InvalidArgument);
+
+ MojoResult r = MojoWait(static_cast<MojoHandle>(handle),
+ static_cast<MojoHandleSignals>(signals),
+ static_cast<MojoDeadline>(deadline));
+
+ Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(r));
+}
+
+
+static void MojoHandle_WaitMany(Dart_NativeArguments arguments) {
+ int64_t num_handles = 0;
+ int64_t deadline = 0;
+ Dart_Handle handles = Dart_GetNativeArgument(arguments, 0);
+ Dart_Handle signals = Dart_GetNativeArgument(arguments, 1);
+ CHECK_INTEGER_ARGUMENT(arguments, 2, &num_handles, InvalidArgument);
+ CHECK_INTEGER_ARGUMENT(arguments, 3, &deadline, InvalidArgument);
+
+ if (!Dart_IsList(handles) || !Dart_IsList(signals)) {
+ SetInvalidArgumentReturn(arguments);
+ return;
+ }
+
+ intptr_t handles_len = 0;
+ intptr_t signals_len = 0;
+ Dart_ListLength(handles, &handles_len);
+ Dart_ListLength(signals, &signals_len);
+ if ((handles_len != num_handles) || (signals_len != num_handles)) {
+ SetInvalidArgumentReturn(arguments);
+ return;
+ }
+
+ scoped_ptr<MojoHandle[]> mojo_handles(new MojoHandle[num_handles]);
+ scoped_ptr<MojoHandleSignals[]> mojo_signals(
+ new MojoHandleSignals[num_handles]);
+
+ for (int i = 0; i < num_handles; i++) {
+ Dart_Handle dart_handle = Dart_ListGetAt(handles, i);
+ Dart_Handle dart_signal = Dart_ListGetAt(signals, i);
+ if (!Dart_IsInteger(dart_handle) || !Dart_IsInteger(dart_signal)) {
+ SetInvalidArgumentReturn(arguments);
+ return;
+ }
+ int64_t mojo_handle = 0;
+ int64_t mojo_signal = 0;
+ Dart_IntegerToInt64(dart_handle, &mojo_handle);
+ Dart_IntegerToInt64(dart_signal, &mojo_signal);
+ mojo_handles[i] = static_cast<MojoHandle>(mojo_handle);
+ mojo_signals[i] = static_cast<MojoHandleSignals>(mojo_signal);
+ }
+
+ MojoResult res = MojoWaitMany(mojo_handles.get(), mojo_signals.get(),
+ static_cast<uint32_t>(num_handles),
+ static_cast<MojoDeadline>(deadline));
+ Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res));
+}
+
+
+static void MojoSharedBuffer_Create(Dart_NativeArguments arguments) {
+ int64_t num_bytes = 0;
+ int64_t flags = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 0, &num_bytes, Null);
+ CHECK_INTEGER_ARGUMENT(arguments, 1, &flags, Null);
+
+ MojoCreateSharedBufferOptions options;
+ options.struct_size = sizeof(MojoCreateSharedBufferOptions);
+ options.flags = static_cast<MojoCreateSharedBufferOptionsFlags>(flags);
+
+ MojoHandle out = MOJO_HANDLE_INVALID;;
+ MojoResult res = MojoCreateSharedBuffer(
+ &options, static_cast<int32_t>(num_bytes), &out);
+
+ Dart_Handle list = Dart_NewList(2);
+ Dart_ListSetAt(list, 0, Dart_NewInteger(res));
+ Dart_ListSetAt(list, 1, Dart_NewInteger(out));
+ Dart_SetReturnValue(arguments, list);
+}
+
+
+static void MojoSharedBuffer_Duplicate(Dart_NativeArguments arguments) {
+ int64_t handle = 0;
+ int64_t flags = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null);
+ CHECK_INTEGER_ARGUMENT(arguments, 1, &flags, Null);
+
+ MojoDuplicateBufferHandleOptions options;
+ options.struct_size = sizeof(MojoDuplicateBufferHandleOptions);
+ options.flags = static_cast<MojoDuplicateBufferHandleOptionsFlags>(flags);
+
+ MojoHandle out = MOJO_HANDLE_INVALID;;
+ MojoResult res = MojoDuplicateBufferHandle(
+ static_cast<MojoHandle>(handle), &options, &out);
+
+ Dart_Handle list = Dart_NewList(2);
+ Dart_ListSetAt(list, 0, Dart_NewInteger(res));
+ Dart_ListSetAt(list, 1, Dart_NewInteger(out));
+ Dart_SetReturnValue(arguments, list);
+}
+
+
+static void MojoSharedBuffer_Map(Dart_NativeArguments arguments) {
+ int64_t handle = 0;
+ int64_t offset = 0;
+ int64_t num_bytes = 0;
+ int64_t flags = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null);
+ CHECK_INTEGER_ARGUMENT(arguments, 1, &offset, Null);
+ CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, Null);
+ CHECK_INTEGER_ARGUMENT(arguments, 3, &flags, Null);
+
+ void* out;
+ MojoResult res = MojoMapBuffer(static_cast<MojoHandle>(handle),
+ offset,
+ num_bytes,
+ &out,
+ static_cast<MojoMapBufferFlags>(flags));
+
+ Dart_Handle list = Dart_NewList(2);
+ Dart_Handle typed_data;
+ if (res == MOJO_RESULT_OK) {
+ typed_data = Dart_NewExternalTypedData(
+ Dart_TypedData_kByteData, out, num_bytes);
+ } else {
+ typed_data = Dart_Null();
+ }
+ Dart_ListSetAt(list, 0, Dart_NewInteger(res));
+ Dart_ListSetAt(list, 1, typed_data);
+ Dart_SetReturnValue(arguments, list);
+}
+
+
+static void MojoSharedBuffer_Unmap(Dart_NativeArguments arguments) {
+ Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 0);
+ if (!Dart_IsTypedData(typed_data)) {
+ SetInvalidArgumentReturn(arguments);
+ return;
+ }
+
+ Dart_TypedData_Type typ;
+ void *data;
+ intptr_t len;
+ Dart_TypedDataAcquireData(typed_data, &typ, &data, &len);
+ MojoResult res = MojoUnmapBuffer(data);
+ Dart_TypedDataReleaseData(typed_data);
+
+ Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res));
+}
+
+
+static void MojoDataPipe_Create(Dart_NativeArguments arguments) {
+ int64_t element_bytes = 0;
+ int64_t capacity_bytes = 0;
+ int64_t flags = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 0, &element_bytes, Null);
+ CHECK_INTEGER_ARGUMENT(arguments, 1, &capacity_bytes, Null);
+ CHECK_INTEGER_ARGUMENT(arguments, 2, &flags, Null);
+
+ MojoCreateDataPipeOptions options;
+ options.struct_size = sizeof(MojoCreateDataPipeOptions);
+ options.flags = static_cast<MojoCreateDataPipeOptionsFlags>(flags);
+ options.element_num_bytes = static_cast<uint32_t>(element_bytes);
+ options.capacity_num_bytes = static_cast<uint32_t>(capacity_bytes);
+
+ MojoHandle producer = MOJO_HANDLE_INVALID;
+ MojoHandle consumer = MOJO_HANDLE_INVALID;
+ MojoResult res = MojoCreateDataPipe(&options, &producer, &consumer);
+
+ Dart_Handle list = Dart_NewList(3);
+ Dart_ListSetAt(list, 0, Dart_NewInteger(res));
+ Dart_ListSetAt(list, 1, Dart_NewInteger(producer));
+ Dart_ListSetAt(list, 2, Dart_NewInteger(consumer));
+ Dart_SetReturnValue(arguments, list);
+}
+
+
+static void MojoDataPipe_WriteData(Dart_NativeArguments arguments) {
+ int64_t handle = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null);
+
+ Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 1);
+ if (!Dart_IsTypedData(typed_data)) {
+ SetNullReturn(arguments);
+ return;
+ }
+
+ int64_t num_bytes = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, Null);
+
+ int64_t flags = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 3, &flags, Null);
+
+ Dart_TypedData_Type type;
+ void* data;
+ intptr_t data_length;
+ Dart_TypedDataAcquireData(typed_data, &type, &data, &data_length);
+ uint32_t length = static_cast<uint32_t>(num_bytes);
+ MojoResult res = MojoWriteData(
+ static_cast<MojoHandle>(handle),
+ data,
+ &length,
+ static_cast<MojoWriteDataFlags>(flags));
+ Dart_TypedDataReleaseData(typed_data);
+
+ Dart_Handle list = Dart_NewList(2);
+ Dart_ListSetAt(list, 0, Dart_NewInteger(res));
+ Dart_ListSetAt(list, 1, Dart_NewInteger(length));
+ Dart_SetReturnValue(arguments, list);
+}
+
+
+static void MojoDataPipe_BeginWriteData(Dart_NativeArguments arguments) {
+ int64_t handle = 0;
+ int64_t buffer_bytes = 0;
+ int64_t flags = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null);
+ CHECK_INTEGER_ARGUMENT(arguments, 1, &buffer_bytes, Null);
+ CHECK_INTEGER_ARGUMENT(arguments, 2, &flags, Null);
+
+ void* buffer;
+ uint32_t size = static_cast<uint32_t>(buffer_bytes);
+ MojoResult res = MojoBeginWriteData(
+ static_cast<MojoHandle>(handle),
+ &buffer,
+ &size,
+ static_cast<MojoWriteDataFlags>(flags));
+
+ Dart_Handle list = Dart_NewList(2);
+ Dart_Handle typed_data;
+ if (res == MOJO_RESULT_OK) {
+ typed_data = Dart_NewExternalTypedData(
+ Dart_TypedData_kByteData, buffer, size);
+ } else {
+ typed_data = Dart_Null();
+ }
+ Dart_ListSetAt(list, 0, Dart_NewInteger(res));
+ Dart_ListSetAt(list, 1, typed_data);
+ Dart_SetReturnValue(arguments, list);
+}
+
+
+static void MojoDataPipe_EndWriteData(Dart_NativeArguments arguments) {
+ int64_t handle = 0;
+ int64_t num_bytes_written = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument);
+ CHECK_INTEGER_ARGUMENT(arguments, 1, &num_bytes_written, InvalidArgument);
+
+ MojoResult res = MojoEndWriteData(
+ static_cast<MojoHandle>(handle),
+ static_cast<uint32_t>(num_bytes_written));
+
+ Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res));
+}
+
+
+static void MojoDataPipe_ReadData(Dart_NativeArguments arguments) {
+ int64_t handle = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null);
+
+ Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 1);
+ if (!Dart_IsTypedData(typed_data) && !Dart_IsNull(typed_data)) {
+ SetNullReturn(arguments);
+ return;
+ }
+
+ int64_t num_bytes = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, Null);
+
+ int64_t flags = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 3, &flags, Null);
+
+ Dart_TypedData_Type typ;
+ void* data = NULL;
+ intptr_t bdlen = 0;
+ if (!Dart_IsNull(typed_data)) {
+ Dart_TypedDataAcquireData(typed_data, &typ, &data, &bdlen);
+ }
+ uint32_t len = static_cast<uint32_t>(num_bytes);
+ MojoResult res = MojoReadData(
+ static_cast<MojoHandle>(handle),
+ data,
+ &len,
+ static_cast<MojoReadDataFlags>(flags));
+ if (!Dart_IsNull(typed_data)) {
+ Dart_TypedDataReleaseData(typed_data);
+ }
+
+ Dart_Handle list = Dart_NewList(2);
+ Dart_ListSetAt(list, 0, Dart_NewInteger(res));
+ Dart_ListSetAt(list, 1, Dart_NewInteger(len));
+ Dart_SetReturnValue(arguments, list);
+}
+
+
+static void MojoDataPipe_BeginReadData(Dart_NativeArguments arguments) {
+ int64_t handle = 0;
+ int64_t buffer_bytes = 0;
+ int64_t flags = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null);
+ CHECK_INTEGER_ARGUMENT(arguments, 1, &buffer_bytes, Null);
+ CHECK_INTEGER_ARGUMENT(arguments, 2, &flags, Null);
+
+ void* buffer;
+ uint32_t size = static_cast<uint32_t>(buffer_bytes);
+ MojoResult res = MojoBeginReadData(
+ static_cast<MojoHandle>(handle),
+ const_cast<const void**>(&buffer),
+ &size,
+ static_cast<MojoWriteDataFlags>(flags));
+
+ Dart_Handle list = Dart_NewList(2);
+ Dart_Handle typed_data;
+ if (res == MOJO_RESULT_OK) {
+ typed_data = Dart_NewExternalTypedData(
+ Dart_TypedData_kByteData, buffer, size);
+ } else {
+ typed_data = Dart_Null();
+ }
+ Dart_ListSetAt(list, 0, Dart_NewInteger(res));
+ Dart_ListSetAt(list, 1, typed_data);
+ Dart_SetReturnValue(arguments, list);
+}
+
+
+static void MojoDataPipe_EndReadData(Dart_NativeArguments arguments) {
+ int64_t handle = 0;
+ int64_t num_bytes_read = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument);
+ CHECK_INTEGER_ARGUMENT(arguments, 1, &num_bytes_read, InvalidArgument);
+
+ MojoResult res = MojoEndReadData(
+ static_cast<MojoHandle>(handle),
+ static_cast<uint32_t>(num_bytes_read));
+
+ Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res));
+}
+
+
+static void MojoMessagePipe_Create(Dart_NativeArguments arguments) {
+ int64_t flags = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 0, &flags, Null);
+
+ MojoCreateMessagePipeOptions options;
+ options.struct_size = sizeof(MojoCreateMessagePipeOptions);
+ options.flags = static_cast<MojoCreateMessagePipeOptionsFlags>(flags);
+
+ MojoHandle end1 = MOJO_HANDLE_INVALID;
+ MojoHandle end2 = MOJO_HANDLE_INVALID;
+ MojoResult res = MojoCreateMessagePipe(&options, &end1, &end2);
+
+ Dart_Handle list = Dart_NewList(3);
+ Dart_ListSetAt(list, 0, Dart_NewInteger(res));
+ Dart_ListSetAt(list, 1, Dart_NewInteger(end1));
+ Dart_ListSetAt(list, 2, Dart_NewInteger(end2));
+ Dart_SetReturnValue(arguments, list);
+}
+
+
+static void MojoMessagePipe_Write(Dart_NativeArguments arguments) {
+ int64_t handle = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, InvalidArgument);
+
+ Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 1);
+ if (!Dart_IsTypedData(typed_data) && !Dart_IsNull(typed_data)) {
+ SetInvalidArgumentReturn(arguments);
+ return;
+ }
+
+ int64_t num_bytes = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, InvalidArgument);
+
+ Dart_Handle handles = Dart_GetNativeArgument(arguments, 3);
+ if (!Dart_IsList(handles) && !Dart_IsNull(handles)) {
+ SetInvalidArgumentReturn(arguments);
+ return;
+ }
+
+ int64_t flags = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 4, &flags, InvalidArgument);
+
+ // Grab the data if there is any.
+ Dart_TypedData_Type typ;
+ void* bytes = NULL;
+ intptr_t bdlen = 0;
+ if (!Dart_IsNull(typed_data)) {
+ Dart_TypedDataAcquireData(typed_data, &typ, &bytes, &bdlen);
+ }
+
+ // Grab the handles if there are any.
+ scoped_ptr<MojoHandle[]> mojo_handles;
+ intptr_t handles_len = 0;
+ if (!Dart_IsNull(handles)) {
+ Dart_ListLength(handles, &handles_len);
+ mojo_handles.reset(new MojoHandle[handles_len]);
+ for (int i = 0; i < handles_len; i++) {
+ Dart_Handle dart_handle = Dart_ListGetAt(handles, i);
+ if (!Dart_IsInteger(dart_handle)) {
+ SetInvalidArgumentReturn(arguments);
+ return;
+ }
+ int64_t mojo_handle = 0;
+ Dart_IntegerToInt64(dart_handle, &mojo_handle);
+ mojo_handles[i] = static_cast<MojoHandle>(mojo_handle);
+ }
+ }
+
+ MojoResult res = MojoWriteMessage(
+ static_cast<MojoHandle>(handle),
+ const_cast<const void*>(bytes),
+ static_cast<uint32_t>(num_bytes),
+ mojo_handles.get(),
+ static_cast<uint32_t>(handles_len),
+ static_cast<MojoWriteMessageFlags>(flags));
+
+ // Release the data.
+ if (!Dart_IsNull(typed_data)) {
+ Dart_TypedDataReleaseData(typed_data);
+ }
+
+ Dart_SetIntegerReturnValue(arguments, static_cast<int64_t>(res));
+}
+
+
+static void MojoMessagePipe_Read(Dart_NativeArguments arguments) {
+ int64_t handle = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 0, &handle, Null);
+
+ Dart_Handle typed_data = Dart_GetNativeArgument(arguments, 1);
+ if (!Dart_IsTypedData(typed_data) && !Dart_IsNull(typed_data)) {
+ SetNullReturn(arguments);
+ return;
+ }
+
+ int64_t num_bytes = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 2, &num_bytes, Null);
+
+ Dart_Handle handles = Dart_GetNativeArgument(arguments, 3);
+ if (!Dart_IsList(handles) && !Dart_IsNull(handles)) {
+ SetNullReturn(arguments);
+ return;
+ }
+
+ int64_t flags = 0;
+ CHECK_INTEGER_ARGUMENT(arguments, 4, &flags, Null);
+
+ // Grab the data if there is any.
+ Dart_TypedData_Type typ;
+ void* bytes = NULL;
+ intptr_t byte_data_len = 0;
+ if (!Dart_IsNull(typed_data)) {
+ Dart_TypedDataAcquireData(typed_data, &typ, &bytes, &byte_data_len);
+ }
+ uint32_t blen = static_cast<uint32_t>(num_bytes);
+
+ // Grab the handles if there are any.
+ scoped_ptr<MojoHandle[]> mojo_handles;
+ intptr_t handles_len = 0;
+ if (!Dart_IsNull(handles)) {
+ Dart_ListLength(handles, &handles_len);
+ mojo_handles.reset(new MojoHandle[handles_len]);
+ }
+ uint32_t hlen = static_cast<uint32_t>(handles_len);
+
+ MojoResult res = MojoReadMessage(
+ static_cast<MojoHandle>(handle),
+ bytes,
+ &blen,
+ mojo_handles.get(),
+ &hlen,
+ static_cast<MojoReadMessageFlags>(flags));
+
+ // Release the data.
+ if (!Dart_IsNull(typed_data)) {
+ Dart_TypedDataReleaseData(typed_data);
+ }
+
+ if (!Dart_IsNull(handles)) {
+ for (int i = 0; i < handles_len; i++) {
+ Dart_ListSetAt(handles, i, Dart_NewInteger(mojo_handles[i]));
+ }
+ }
+
+ Dart_Handle list = Dart_NewList(3);
+ Dart_ListSetAt(list, 0, Dart_NewInteger(res));
+ Dart_ListSetAt(list, 1, Dart_NewInteger(blen));
+ Dart_ListSetAt(list, 2, Dart_NewInteger(hlen));
+ Dart_SetReturnValue(arguments, list);
+}
+
+
+#define SCOPE_FUNCTIONS(V) \
+ V(MojoSharedBuffer_Create) \
+ V(MojoSharedBuffer_Duplicate) \
+ V(MojoSharedBuffer_Map) \
+ V(MojoSharedBuffer_Unmap) \
+ V(MojoDataPipe_Create) \
+ V(MojoDataPipe_WriteData) \
+ V(MojoDataPipe_BeginWriteData) \
+ V(MojoDataPipe_ReadData) \
+ V(MojoDataPipe_BeginReadData) \
+ V(MojoDataPipe_EndReadData) \
+ V(MojoMessagePipe_Create) \
+ V(MojoMessagePipe_Write) \
+ V(MojoMessagePipe_Read) \
+
+#define NOSCOPE_FUNCTIONS(V) \
+ V(MojoSystemThunks_Set) \
+ V(MojoHandle_Close) \
+ V(MojoHandle_Wait) \
+ V(MojoHandle_WaitMany) \
+ V(MojoDataPipe_EndWriteData) \
+
+#define FUNCTION_STRING_MAP(name) {#name, name},
+
+struct FunctionLookup {
+ const char* name;
+ Dart_NativeFunction function;
+};
+
+FunctionLookup function_list[] = {
+ SCOPE_FUNCTIONS(FUNCTION_STRING_MAP)
+ {NULL, NULL}
+};
+
+FunctionLookup no_scope_function_list[] = {
+ NOSCOPE_FUNCTIONS(FUNCTION_STRING_MAP)
+ {NULL, NULL}
+};
+
+#undef FUNCTION_STRING_MAP
+
+
+Dart_NativeFunction ResolveName(Dart_Handle name,
+ int argc,
+ bool* auto_setup_scope) {
+ if (!Dart_IsString(name)) {
+ return NULL;
+ }
+ Dart_NativeFunction result = NULL;
+ if (auto_setup_scope == NULL) {
+ return NULL;
+ }
+
+ Dart_EnterScope();
+ const char* cname;
+ HandleError(Dart_StringToCString(name, &cname));
+
+ for (int i=0; function_list[i].name != NULL; ++i) {
+ if (strcmp(function_list[i].name, cname) == 0) {
+ *auto_setup_scope = true;
+ Dart_ExitScope();
+ return function_list[i].function;
+ }
+ }
+
+ for (int i=0; no_scope_function_list[i].name != NULL; ++i) {
+ if (strcmp(no_scope_function_list[i].name, cname) == 0) {
+ *auto_setup_scope = false;
+ result = no_scope_function_list[i].function;
+ break;
+ }
+ }
+
+ Dart_ExitScope();
+ return result;
+}
diff --git a/mojo/public/dart/src/types.dart b/mojo/public/dart/src/types.dart
new file mode 100644
index 0000000..658e477
--- /dev/null
+++ b/mojo/public/dart/src/types.dart
@@ -0,0 +1,116 @@
+// Copyright 2014 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.
+
+
+part of core;
+
+
+class MojoResult {
+ static const int kOk = 0;
+ static const int kCancelled = -1;
+ static const int kUnknown = -2;
+ static const int kInvalidArgument = -3;
+ static const int kDeadlineExceeded = -4;
+ static const int kNotFound = -5;
+ static const int kAlreadyExists = -6;
+ static const int kPermissionDenied = -7;
+ static const int kResourceExhausted = -8;
+ static const int kFailedPrecondition = -9;
+ static const int kAborted = -10;
+ static const int kOutOfRange = -11;
+ static const int kUnimplemented = -12;
+ static const int kInternal = -13;
+ static const int kUnavailable = -14;
+ static const int kDataLoss = -15;
+ static const int kBusy = -16;
+ static const int kShouldWait = -17;
+
+ static const OK = const MojoResult._(kOk);
+ static const CANCELLED = const MojoResult._(kCancelled);
+ static const UNKNOWN = const MojoResult._(kUnknown);
+ static const INVALID_ARGUMENT = const MojoResult._(kInvalidArgument);
+ static const DEADLINE_EXCEEDED = const MojoResult._(kDeadlineExceeded);
+ static const NOT_FOUND = const MojoResult._(kNotFound);
+ static const ALREADY_EXISTS = const MojoResult._(kAlreadyExists);
+ static const PERMISSION_DENIED = const MojoResult._(kPermissionDenied);
+ static const RESOURCE_EXHAUSTED = const MojoResult._(kResourceExhausted);
+ static const FAILED_PRECONDITION = const MojoResult._(kFailedPrecondition);
+ static const ABORTED = const MojoResult._(kAborted);
+ static const OUT_OF_RANGE = const MojoResult._(kOutOfRange);
+ static const UNIMPLEMENTED = const MojoResult._(kUnimplemented);
+ static const INTERNAL = const MojoResult._(kInternal);
+ static const UNAVAILABLE = const MojoResult._(kUnavailable);
+ static const DATA_LOSS = const MojoResult._(kDataLoss);
+ static const BUSY = const MojoResult._(kBusy);
+ static const SHOULD_WAIT = const MojoResult._(kShouldWait);
+
+ final int value;
+
+ const MojoResult._(this.value);
+
+ factory MojoResult(int value) {
+ switch (value) {
+ case kOk: return OK;
+ case kCancelled: return CANCELLED;
+ case kUnknown: return UNKNOWN;
+ case kInvalidArgument: return INVALID_ARGUMENT;
+ case kDeadlineExceeded: return DEADLINE_EXCEEDED;
+ case kNotFound: return NOT_FOUND;
+ case kAlreadyExists: return ALREADY_EXISTS;
+ case kPermissionDenied: return PERMISSION_DENIED;
+ case kResourceExhausted: return RESOURCE_EXHAUSTED;
+ case kFailedPrecondition: return FAILED_PRECONDITION;
+ case kAborted: return ABORTED;
+ case kOutOfRange: return OUT_OF_RANGE;
+ case kUnimplemented: return UNIMPLEMENTED;
+ case kInternal: return INTERNAL;
+ case kUnavailable: return UNAVAILABLE;
+ case kDataLoss: return DATA_LOSS;
+ case kBusy: return BUSY;
+ case kShouldWait: return SHOULD_WAIT;
+ default: return null;
+ }
+ }
+
+ bool get isOk => (this == OK);
+ bool get isCancelled => (this == CANCELLED);
+ bool get isUnknown => (this == UNKNOWN);
+ bool get isInvalidArgument => (this == INVALID_ARGUMENT);
+ bool get isDeadlineExceeded => (this == DEADLINE_EXCEEDED);
+ bool get isNotFound => (this == NOT_FOUND);
+ bool get isAlreadExists => (this == ALREADY_EXISTS);
+ bool get isPermissionDenied => (this == PERMISSION_DENIED);
+ bool get isResourceExhausted => (this == RESOURCE_EXHAUSTED);
+ bool get isFailedPrecondition => (this == FAILED_PRECONDITION);
+ bool get isAborted => (this == ABORTED);
+ bool get isOutOfRange => (this == OUT_OF_RANGE);
+ bool get isUnimplemented => (this == UNIMPLEMENTED);
+ bool get isInternal => (this == INTERNAL);
+ bool get isUnavailable => (this == UNAVAILABLE);
+ bool get isDataLoss => (this == DATA_LOSS);
+ bool get isBusy => (this == BUSY);
+ bool get isShouldWait => (this == SHOULD_WAIT);
+}
+
+
+class MojoHandleSignals {
+ static const int NONE = 0;
+ static const int READABLE = 1 << 0;
+ static const int WRITABLE = 1 << 1;
+ static const int READWRITE = READABLE | WRITABLE;
+
+ static bool isReadable(int mask) => (mask & READABLE) == READABLE;
+ static bool isWritable(int mask) => (mask & WRITABLE) == WRITABLE;
+ static bool isReadWrite(int mask) => (mask & READWRITE) == READWRITE;
+ static int toggleWrite(int mask) =>
+ isWritable(mask) ? (mask & ~WRITABLE) : (mask | WRITABLE);
+}
+
+
+class MojoHandleSignalsState {
+ const MojoHandleSignalsState(this.satisfied_signals,
+ this.satisfiable_signals);
+ final int satisfied_signals;
+ final int satisfiable_signals;
+}