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
11Dart_NativeFunction ResolveName(Dart_Handle name,
12 int argc,
13 bool* auto_setup_scope);
14
15
16DART_EXPORT Dart_Handle sample_extension_Init(Dart_Handle parent_library) {
17 if (Dart_IsError(parent_library)) {
18 return parent_library;
19 }
20
21 Dart_Handle result_code =
22 Dart_SetNativeResolver(parent_library, ResolveName, NULL);
23 if (Dart_IsError(result_code)) {
24 return result_code;
25 }
26
27 return Dart_Null();
28}
29
30
31Dart_Handle HandleError(Dart_Handle handle) {
32 if (Dart_IsError(handle)) {
33 Dart_PropagateError(handle);
34 }
35 return handle;
36}
37
38
39void 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
47void 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
66uint8_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
82void 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.id;
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 if (Dart_PostCObject(reply_port_id, &result)) {
106 Dart_CObject error;
107 error.type = Dart_CObject_kNull;
108 Dart_PostCObject(reply_port_id, &error);
109 }
110 free(values);
111 // It is OK that result is destroyed when function exits.
112 // Dart_PostCObject has copied its data.
113 return;
114 }
115 }
116 }
117 fprintf(stderr, "Invalid message received, cannot proceed. Aborting the process.\n");
118 abort();
119}
120
121
122void randomArrayServicePort(Dart_NativeArguments arguments) {
123 Dart_EnterScope();
124 Dart_SetReturnValue(arguments, Dart_Null());
125 Dart_Port service_port =
126 Dart_NewNativePort("RandomArrayService", wrappedRandomArray, true);
127 if (service_port != ILLEGAL_PORT) {
128 Dart_Handle send_port = HandleError(Dart_NewSendPort(service_port));
129 Dart_SetReturnValue(arguments, send_port);
130 }
131 Dart_ExitScope();
132}
133
134
135struct FunctionLookup {
136 const char* name;
137 Dart_NativeFunction function;
138};
139
140
141FunctionLookup function_list[] = {
142 {"SystemRand", SystemRand},
143 {"SystemSrand", SystemSrand},
144 {"RandomArray_ServicePort", randomArrayServicePort},
145 {NULL, NULL}};
146
147
148FunctionLookup no_scope_function_list[] = {
149 {"NoScopeSystemRand", SystemRand},
150 {NULL, NULL}
151};
152
153Dart_NativeFunction ResolveName(Dart_Handle name,
154 int argc,
155 bool* auto_setup_scope) {
156 if (!Dart_IsString(name)) {
157 return NULL;
158 }
159 Dart_NativeFunction result = NULL;
160 if (auto_setup_scope == NULL) {
161 return NULL;
162 }
163
164 Dart_EnterScope();
165 const char* cname;
166 HandleError(Dart_StringToCString(name, &cname));
167
168 for (int i=0; function_list[i].name != NULL; ++i) {
169 if (strcmp(function_list[i].name, cname) == 0) {
170 *auto_setup_scope = true;
171 result = function_list[i].function;
172 break;
173 }
174 }
175
176 if (result != NULL) {
177 Dart_ExitScope();
178 return result;
179 }
180
181 for (int i=0; no_scope_function_list[i].name != NULL; ++i) {
182 if (strcmp(no_scope_function_list[i].name, cname) == 0) {
183 *auto_setup_scope = false;
184 result = no_scope_function_list[i].function;
185 break;
186 }
187 }
188
189 Dart_ExitScope();
190 return result;
191}
192