OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string.h> |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 |
| 9 #include "dart_api.h" |
| 10 |
| 11 #include "mojo/edk/embedder/embedder.h" |
| 12 #include "mojo/edk/embedder/platform_support.h" |
| 13 #include "mojo/edk/embedder/simple_platform_support.h" |
| 14 #include "mojo/public/platform/native/system_thunks.h" |
| 15 |
| 16 |
| 17 #define HELPER_FUNCTIONS(V) \ |
| 18 V(MojoEmbedder_Init) \ |
| 19 V(MojoSystemThunks_Make) \ |
| 20 |
| 21 static Dart_NativeFunction ResolveName( |
| 22 Dart_Handle name, int argc, bool* auto_setup_scope); |
| 23 |
| 24 |
| 25 DART_EXPORT Dart_Handle mojo_dart_embedder_Init(Dart_Handle parent_library) { |
| 26 if (Dart_IsError(parent_library)) { |
| 27 return parent_library; |
| 28 } |
| 29 |
| 30 Dart_Handle result_code = Dart_SetNativeResolver( |
| 31 parent_library, ResolveName, NULL); |
| 32 if (Dart_IsError(result_code)) { |
| 33 return result_code; |
| 34 } |
| 35 |
| 36 return Dart_Null(); |
| 37 } |
| 38 |
| 39 |
| 40 static Dart_Handle HandleError(Dart_Handle handle) { |
| 41 if (Dart_IsError(handle)) { |
| 42 Dart_PropagateError(handle); |
| 43 } |
| 44 return handle; |
| 45 } |
| 46 |
| 47 |
| 48 static void MojoEmbedder_Init(Dart_NativeArguments arguments) { |
| 49 Dart_EnterScope(); |
| 50 |
| 51 mojo::embedder::Init(scoped_ptr<mojo::embedder::PlatformSupport>( |
| 52 new mojo::embedder::SimplePlatformSupport())); |
| 53 |
| 54 Dart_SetReturnValue(arguments, Dart_Null()); |
| 55 Dart_ExitScope(); |
| 56 } |
| 57 |
| 58 |
| 59 static void MojoSystemThunks_Make(Dart_NativeArguments arguments) { |
| 60 Dart_EnterScope(); |
| 61 |
| 62 Dart_Handle closure = HandleError(Dart_GetNativeArgument(arguments, 0)); |
| 63 if (Dart_IsClosure(closure)) { |
| 64 MojoSystemThunks thunks = MojoMakeSystemThunks(); |
| 65 int64_t thunks_addr = reinterpret_cast<int64_t>(&thunks); |
| 66 |
| 67 Dart_Handle t = Dart_NewInteger(thunks_addr); |
| 68 Dart_InvokeClosure(closure, 1, &t); |
| 69 } |
| 70 |
| 71 Dart_SetReturnValue(arguments, Dart_Null()); |
| 72 Dart_ExitScope(); |
| 73 } |
| 74 |
| 75 |
| 76 struct FunctionLookup { |
| 77 const char* name; |
| 78 Dart_NativeFunction function; |
| 79 }; |
| 80 |
| 81 |
| 82 #define FUNCTION_STRING_MAP(name) {#name, name}, |
| 83 |
| 84 FunctionLookup function_list[] = { |
| 85 HELPER_FUNCTIONS(FUNCTION_STRING_MAP) |
| 86 {NULL, NULL}}; |
| 87 |
| 88 #undef FUNCTION_STRING_MAP |
| 89 |
| 90 |
| 91 FunctionLookup no_scope_function_list[] = { |
| 92 {NULL, NULL} |
| 93 }; |
| 94 |
| 95 Dart_NativeFunction ResolveName(Dart_Handle name, |
| 96 int argc, |
| 97 bool* auto_setup_scope) { |
| 98 if (!Dart_IsString(name)) { |
| 99 return NULL; |
| 100 } |
| 101 Dart_NativeFunction result = NULL; |
| 102 if (auto_setup_scope == NULL) { |
| 103 return NULL; |
| 104 } |
| 105 |
| 106 Dart_EnterScope(); |
| 107 const char* cname; |
| 108 HandleError(Dart_StringToCString(name, &cname)); |
| 109 |
| 110 for (int i=0; function_list[i].name != NULL; ++i) { |
| 111 if (strcmp(function_list[i].name, cname) == 0) { |
| 112 *auto_setup_scope = true; |
| 113 result = function_list[i].function; |
| 114 break; |
| 115 } |
| 116 } |
| 117 |
| 118 if (result != NULL) { |
| 119 Dart_ExitScope(); |
| 120 return result; |
| 121 } |
| 122 |
| 123 for (int i=0; no_scope_function_list[i].name != NULL; ++i) { |
| 124 if (strcmp(no_scope_function_list[i].name, cname) == 0) { |
| 125 *auto_setup_scope = false; |
| 126 result = no_scope_function_list[i].function; |
| 127 break; |
| 128 } |
| 129 } |
| 130 |
| 131 Dart_ExitScope(); |
| 132 return result; |
| 133 } |
OLD | NEW |