| 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 | 4 |
| 5 #include <assert.h> | 5 #include <assert.h> |
| 6 #include <errno.h> | 6 #include <errno.h> |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 | 8 |
| 9 #include "dart_archive.h" | 9 #include "dart_archive.h" |
| 10 #include "messaging.h" | 10 #include "messaging.h" |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 Dart_Port service_port = | 194 Dart_Port service_port = |
| 195 Dart_NewNativePort("ArchiveService", archiveDispatch, false); | 195 Dart_NewNativePort("ArchiveService", archiveDispatch, false); |
| 196 if (service_port != ILLEGAL_PORT) { | 196 if (service_port != ILLEGAL_PORT) { |
| 197 Dart_Handle send_port = handleError(Dart_NewSendPort(service_port)); | 197 Dart_Handle send_port = handleError(Dart_NewSendPort(service_port)); |
| 198 Dart_SetReturnValue(arguments, send_port); | 198 Dart_SetReturnValue(arguments, send_port); |
| 199 } | 199 } |
| 200 Dart_ExitScope(); | 200 Dart_ExitScope(); |
| 201 } | 201 } |
| 202 | 202 |
| 203 /** | 203 /** |
| 204 * The C callback that runs the Dart finalizer for an object. Set up by |
| 205 * [attachDartFinalizer]. [handle] is the object that's been collected, and |
| 206 * [peerPtr] is a Dart list containing the callback and its argument. |
| 207 */ |
| 208 static void runDartFinalizer(Dart_Handle handle, void* peerPtr) { |
| 209 Dart_EnterScope(); |
| 210 Dart_Handle wrappedPeer = (Dart_Handle) peerPtr; |
| 211 Dart_Handle callback = handleError(Dart_ListGetAt(wrappedPeer, 0)); |
| 212 Dart_Handle peer = handleError(Dart_ListGetAt(wrappedPeer, 1)); |
| 213 |
| 214 handleError(Dart_InvokeClosure(callback, 1, &peer)); |
| 215 Dart_DeletePersistentHandle(wrappedPeer); |
| 216 Dart_ExitScope(); |
| 217 } |
| 218 |
| 219 /** |
| 220 * Attaches a finalizer callback to a Dart object. |
| 221 * |
| 222 * This takes a Dart object, a callback function, and an argument to pass to the |
| 223 * callback function. The callback will be called with the given argument some |
| 224 * time after the object has been garbage collected. |
| 225 */ |
| 226 static void attachDartFinalizer(Dart_NativeArguments arguments) { |
| 227 Dart_EnterScope(); |
| 228 Dart_SetReturnValue(arguments, Dart_Null()); |
| 229 Dart_Handle object = handleError(Dart_GetNativeArgument(arguments, 0)); |
| 230 Dart_Handle callback = handleError(Dart_GetNativeArgument(arguments, 1)); |
| 231 Dart_Handle peer = handleError(Dart_GetNativeArgument(arguments, 2)); |
| 232 |
| 233 Dart_Handle wrappedPeer = handleError(Dart_NewList(2)); |
| 234 handleError(Dart_ListSetAt(wrappedPeer, 0, callback)); |
| 235 handleError(Dart_ListSetAt(wrappedPeer, 1, peer)); |
| 236 wrappedPeer = handleError(Dart_NewPersistentHandle(wrappedPeer)); |
| 237 |
| 238 handleError(Dart_NewWeakPersistentHandle( |
| 239 object, wrappedPeer, runDartFinalizer)); |
| 240 Dart_ExitScope(); |
| 241 } |
| 242 |
| 243 /** |
| 204 * A struct representing a function exposed to Dart and the name under which it | 244 * A struct representing a function exposed to Dart and the name under which it |
| 205 * can be looked up. | 245 * can be looked up. |
| 206 */ | 246 */ |
| 207 struct FunctionLookup { | 247 struct FunctionLookup { |
| 208 const char* name; | 248 const char* name; |
| 209 Dart_NativeFunction function; | 249 Dart_NativeFunction function; |
| 210 }; | 250 }; |
| 211 | 251 |
| 212 /** The list of functions exposed to Dart. */ | 252 /** The list of functions exposed to Dart. */ |
| 213 struct FunctionLookup function_list[] = { | 253 struct FunctionLookup function_list[] = { |
| 214 {"Archive_ServicePort", archiveServicePort}, | 254 {"Archive_ServicePort", archiveServicePort}, |
| 255 {"Archive_AttachFinalizer", attachDartFinalizer}, |
| 215 {NULL, NULL} | 256 {NULL, NULL} |
| 216 }; | 257 }; |
| 217 | 258 |
| 218 /** | 259 /** |
| 219 * Resolves a Dart name as provided in a `native` declaration and returns the | 260 * Resolves a Dart name as provided in a `native` declaration and returns the |
| 220 * C function that should be invoked for that name. | 261 * C function that should be invoked for that name. |
| 221 */ | 262 */ |
| 222 static Dart_NativeFunction resolveName(Dart_Handle name, int argc) { | 263 static Dart_NativeFunction resolveName(Dart_Handle name, int argc) { |
| 223 if (!Dart_IsString8(name)) return NULL; | 264 if (!Dart_IsString8(name)) return NULL; |
| 224 Dart_EnterScope(); | 265 Dart_EnterScope(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 239 | 280 |
| 240 /** Initializes the C extension. */ | 281 /** Initializes the C extension. */ |
| 241 DART_EXPORT Dart_Handle dart_archive_Init(Dart_Handle parent_library) { | 282 DART_EXPORT Dart_Handle dart_archive_Init(Dart_Handle parent_library) { |
| 242 if (Dart_IsError(parent_library)) return parent_library; | 283 if (Dart_IsError(parent_library)) return parent_library; |
| 243 | 284 |
| 244 Dart_Handle result_code = Dart_SetNativeResolver(parent_library, resolveName); | 285 Dart_Handle result_code = Dart_SetNativeResolver(parent_library, resolveName); |
| 245 if (Dart_IsError(result_code)) return result_code; | 286 if (Dart_IsError(result_code)) return result_code; |
| 246 | 287 |
| 247 return Dart_Null(); | 288 return Dart_Null(); |
| 248 } | 289 } |
| OLD | NEW |