| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 #include <string.h> | 4 #include <string.h> |
| 5 #include <stdlib.h> | 5 #include <stdlib.h> |
| 6 #include <stdio.h> | 6 #include <stdio.h> |
| 7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
| 8 #include "include/dart_native_api.h" | 8 #include "include/dart_native_api.h" |
| 9 | 9 |
| 10 |
| 10 Dart_NativeFunction ResolveName(Dart_Handle name, | 11 Dart_NativeFunction ResolveName(Dart_Handle name, |
| 11 int argc, | 12 int argc, |
| 12 bool* auto_setup_scope); | 13 bool* auto_setup_scope); |
| 13 | 14 |
| 15 |
| 14 DART_EXPORT Dart_Handle sample_extension_Init(Dart_Handle parent_library) { | 16 DART_EXPORT Dart_Handle sample_extension_Init(Dart_Handle parent_library) { |
| 15 if (Dart_IsError(parent_library)) { return parent_library; } | 17 if (Dart_IsError(parent_library)) { |
| 18 return parent_library; |
| 19 } |
| 16 | 20 |
| 17 Dart_Handle result_code = Dart_SetNativeResolver(parent_library, ResolveName); | 21 Dart_Handle result_code = Dart_SetNativeResolver(parent_library, ResolveName); |
| 18 if (Dart_IsError(result_code)) return result_code; | 22 if (Dart_IsError(result_code)) { |
| 23 return result_code; |
| 24 } |
| 19 | 25 |
| 20 return Dart_Null(); | 26 return Dart_Null(); |
| 21 } | 27 } |
| 22 | 28 |
| 29 |
| 23 Dart_Handle HandleError(Dart_Handle handle) { | 30 Dart_Handle HandleError(Dart_Handle handle) { |
| 24 if (Dart_IsError(handle)) Dart_PropagateError(handle); | 31 if (Dart_IsError(handle)) { |
| 32 Dart_PropagateError(handle); |
| 33 } |
| 25 return handle; | 34 return handle; |
| 26 } | 35 } |
| 27 | 36 |
| 37 |
| 28 void SystemRand(Dart_NativeArguments arguments) { | 38 void SystemRand(Dart_NativeArguments arguments) { |
| 29 Dart_EnterScope(); | 39 Dart_EnterScope(); |
| 30 Dart_Handle result = HandleError(Dart_NewInteger(rand())); | 40 Dart_Handle result = HandleError(Dart_NewInteger(rand())); |
| 31 Dart_SetReturnValue(arguments, result); | 41 Dart_SetReturnValue(arguments, result); |
| 32 Dart_ExitScope(); | 42 Dart_ExitScope(); |
| 33 } | 43 } |
| 34 | 44 |
| 45 |
| 35 void SystemSrand(Dart_NativeArguments arguments) { | 46 void SystemSrand(Dart_NativeArguments arguments) { |
| 36 Dart_EnterScope(); | 47 Dart_EnterScope(); |
| 37 bool success = false; | 48 bool success = false; |
| 38 Dart_Handle seed_object = HandleError(Dart_GetNativeArgument(arguments, 0)); | 49 Dart_Handle seed_object = HandleError(Dart_GetNativeArgument(arguments, 0)); |
| 39 if (Dart_IsInteger(seed_object)) { | 50 if (Dart_IsInteger(seed_object)) { |
| 40 bool fits; | 51 bool fits; |
| 41 HandleError(Dart_IntegerFitsIntoInt64(seed_object, &fits)); | 52 HandleError(Dart_IntegerFitsIntoInt64(seed_object, &fits)); |
| 42 if (fits) { | 53 if (fits) { |
| 43 int64_t seed; | 54 int64_t seed; |
| 44 HandleError(Dart_IntegerToInt64(seed_object, &seed)); | 55 HandleError(Dart_IntegerToInt64(seed_object, &seed)); |
| 45 srand(static_cast<unsigned>(seed)); | 56 srand(static_cast<unsigned>(seed)); |
| 46 success = true; | 57 success = true; |
| 47 } | 58 } |
| 48 } | 59 } |
| 49 Dart_SetReturnValue(arguments, HandleError(Dart_NewBoolean(success))); | 60 Dart_SetReturnValue(arguments, HandleError(Dart_NewBoolean(success))); |
| 50 Dart_ExitScope(); | 61 Dart_ExitScope(); |
| 51 } | 62 } |
| 52 | 63 |
| 64 |
| 53 uint8_t* randomArray(int seed, int length) { | 65 uint8_t* randomArray(int seed, int length) { |
| 54 if (length <= 0 || length > 10000000) return NULL; | 66 if (length <= 0 || length > 10000000) { |
| 67 return NULL; |
| 68 } |
| 55 uint8_t* values = reinterpret_cast<uint8_t*>(malloc(length)); | 69 uint8_t* values = reinterpret_cast<uint8_t*>(malloc(length)); |
| 56 if (NULL == values) return NULL; | 70 if (NULL == values) { |
| 71 return NULL; |
| 72 } |
| 57 srand(seed); | 73 srand(seed); |
| 58 for (int i = 0; i < length; ++i) { | 74 for (int i = 0; i < length; ++i) { |
| 59 values[i] = rand() % 256; | 75 values[i] = rand() % 256; |
| 60 } | 76 } |
| 61 return values; | 77 return values; |
| 62 } | 78 } |
| 63 | 79 |
| 80 |
| 64 void wrappedRandomArray(Dart_Port dest_port_id, | 81 void wrappedRandomArray(Dart_Port dest_port_id, |
| 65 Dart_CObject* message) { | 82 Dart_CObject* message) { |
| 66 Dart_Port reply_port_id = ILLEGAL_PORT; | 83 Dart_Port reply_port_id = ILLEGAL_PORT; |
| 67 if (message->type == Dart_CObject_kArray && | 84 if (message->type == Dart_CObject_kArray && |
| 68 3 == message->value.as_array.length) { | 85 3 == message->value.as_array.length) { |
| 69 // Use .as_array and .as_int32 to access the data in the Dart_CObject. | 86 // Use .as_array and .as_int32 to access the data in the Dart_CObject. |
| 70 Dart_CObject* param0 = message->value.as_array.values[0]; | 87 Dart_CObject* param0 = message->value.as_array.values[0]; |
| 71 Dart_CObject* param1 = message->value.as_array.values[1]; | 88 Dart_CObject* param1 = message->value.as_array.values[1]; |
| 72 Dart_CObject* param2 = message->value.as_array.values[2]; | 89 Dart_CObject* param2 = message->value.as_array.values[2]; |
| 73 if (param0->type == Dart_CObject_kInt32 && | 90 if (param0->type == Dart_CObject_kInt32 && |
| (...skipping 16 matching lines...) Expand all Loading... |
| 90 // Dart_PostCObject has copied its data. | 107 // Dart_PostCObject has copied its data. |
| 91 return; | 108 return; |
| 92 } | 109 } |
| 93 } | 110 } |
| 94 } | 111 } |
| 95 Dart_CObject result; | 112 Dart_CObject result; |
| 96 result.type = Dart_CObject_kNull; | 113 result.type = Dart_CObject_kNull; |
| 97 Dart_PostCObject(reply_port_id, &result); | 114 Dart_PostCObject(reply_port_id, &result); |
| 98 } | 115 } |
| 99 | 116 |
| 117 |
| 100 void randomArrayServicePort(Dart_NativeArguments arguments) { | 118 void randomArrayServicePort(Dart_NativeArguments arguments) { |
| 101 Dart_EnterScope(); | 119 Dart_EnterScope(); |
| 102 Dart_SetReturnValue(arguments, Dart_Null()); | 120 Dart_SetReturnValue(arguments, Dart_Null()); |
| 103 Dart_Port service_port = | 121 Dart_Port service_port = |
| 104 Dart_NewNativePort("RandomArrayService", wrappedRandomArray, true); | 122 Dart_NewNativePort("RandomArrayService", wrappedRandomArray, true); |
| 105 if (service_port != ILLEGAL_PORT) { | 123 if (service_port != ILLEGAL_PORT) { |
| 106 Dart_Handle send_port = HandleError(Dart_NewSendPort(service_port)); | 124 Dart_Handle send_port = HandleError(Dart_NewSendPort(service_port)); |
| 107 Dart_SetReturnValue(arguments, send_port); | 125 Dart_SetReturnValue(arguments, send_port); |
| 108 } | 126 } |
| 109 Dart_ExitScope(); | 127 Dart_ExitScope(); |
| 110 } | 128 } |
| 111 | 129 |
| 112 | 130 |
| 113 struct FunctionLookup { | 131 struct FunctionLookup { |
| 114 const char* name; | 132 const char* name; |
| 115 Dart_NativeFunction function; | 133 Dart_NativeFunction function; |
| 116 }; | 134 }; |
| 117 | 135 |
| 136 |
| 118 FunctionLookup function_list[] = { | 137 FunctionLookup function_list[] = { |
| 119 {"SystemRand", SystemRand}, | 138 {"SystemRand", SystemRand}, |
| 120 {"SystemSrand", SystemSrand}, | 139 {"SystemSrand", SystemSrand}, |
| 121 {"RandomArray_ServicePort", randomArrayServicePort}, | 140 {"RandomArray_ServicePort", randomArrayServicePort}, |
| 122 {NULL, NULL}}; | 141 {NULL, NULL}}; |
| 123 | 142 |
| 143 |
| 144 FunctionLookup no_scope_function_list[] = {{"NoScopeSystemRand", SystemRand}}; |
| 145 |
| 124 Dart_NativeFunction ResolveName(Dart_Handle name, | 146 Dart_NativeFunction ResolveName(Dart_Handle name, |
| 125 int argc, | 147 int argc, |
| 126 bool* auto_setup_scope) { | 148 bool* auto_setup_scope) { |
| 127 if (!Dart_IsString(name)) return NULL; | 149 if (!Dart_IsString(name)) { |
| 150 return NULL; |
| 151 } |
| 128 Dart_NativeFunction result = NULL; | 152 Dart_NativeFunction result = NULL; |
| 129 if (auto_setup_scope == NULL) return NULL; | 153 if (auto_setup_scope == NULL) { |
| 130 *auto_setup_scope = true; | 154 return NULL; |
| 155 } |
| 156 |
| 131 Dart_EnterScope(); | 157 Dart_EnterScope(); |
| 132 const char* cname; | 158 const char* cname; |
| 133 HandleError(Dart_StringToCString(name, &cname)); | 159 HandleError(Dart_StringToCString(name, &cname)); |
| 134 | 160 |
| 135 for (int i=0; function_list[i].name != NULL; ++i) { | 161 for (int i=0; function_list[i].name != NULL; ++i) { |
| 136 if (strcmp(function_list[i].name, cname) == 0) { | 162 if (strcmp(function_list[i].name, cname) == 0) { |
| 163 *auto_setup_scope = true; |
| 137 result = function_list[i].function; | 164 result = function_list[i].function; |
| 138 break; | 165 break; |
| 139 } | 166 } |
| 140 } | 167 } |
| 168 |
| 169 if (result != NULL) { |
| 170 Dart_ExitScope(); |
| 171 return result; |
| 172 } |
| 173 |
| 174 for (int i=0; no_scope_function_list[i].name != NULL; ++i) { |
| 175 if (strcmp(no_scope_function_list[i].name, cname) == 0) { |
| 176 *auto_setup_scope = false; |
| 177 result = no_scope_function_list[i].function; |
| 178 break; |
| 179 } |
| 180 } |
| 181 |
| 141 Dart_ExitScope(); | 182 Dart_ExitScope(); |
| 142 return result; | 183 return result; |
| 143 } | 184 } |
| OLD | NEW |