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