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 |
| 5 #ifndef DART_ARCHIVE_MESSAGING_H_ |
| 6 #define DART_ARCHIVE_MESSAGING_H_ |
| 7 |
| 8 #include "dart_archive.h" |
| 9 |
| 10 /** |
| 11 * Posts a reponse to the main Dart isolate. Only one response should be sent |
| 12 * for each request. |
| 13 * |
| 14 * [p] is the reply port for the isolate. [success] indicates whether or not the |
| 15 * request completed successfully; if not, [err] indicates the precise nature of |
| 16 * the error. [response] contains request-specific response data to send to |
| 17 * Dart; it may not be `NULL`, but it may be the Dart `null` value. |
| 18 */ |
| 19 void postResult(Dart_Port p, bool success, int err, Dart_CObject* response); |
| 20 |
| 21 /** |
| 22 * Posts an error response to the main Dart isolate. |
| 23 * |
| 24 * This should be used when libarchive signals an error. The errno and error |
| 25 * message are taken from libarchive's built-in error information for [a]. |
| 26 */ |
| 27 void postError(Dart_Port p, struct archive* a); |
| 28 |
| 29 /** |
| 30 * Posts an invalid argument error response to the main Dart isolate. |
| 31 * |
| 32 * This should be used when the arguments sent by the Dart code have unexpected |
| 33 * types. Takes a `printf`-style [format] string for describing the error. Note |
| 34 * that the error string will be cut off at 256 characters. |
| 35 */ |
| 36 void postInvalidArgument(Dart_Port p, const char* format, ...); |
| 37 |
| 38 /** |
| 39 * Posts a success response to the main Dart isolate. [response] is the |
| 40 * request-specific Dart object containing the response data. It may be `NULL`. |
| 41 */ |
| 42 void postSuccess(Dart_Port p, Dart_CObject* response); |
| 43 |
| 44 /** |
| 45 * Checks [error], the return code of a libarchive call for error conditions, |
| 46 * and sends an appropriate error response if any are detected. |
| 47 * |
| 48 * Returns `true` if an error is detected and the containing function should |
| 49 * short-circuit. |
| 50 */ |
| 51 bool checkError(Dart_Port p, struct archive* a, int result); |
| 52 |
| 53 /** |
| 54 * Like [checkError], but sends a success message with no attached data if no |
| 55 * error is detected. No further responses should be sent after calling this. |
| 56 */ |
| 57 void checkResult(Dart_Port p, struct archive* a, int result); |
| 58 |
| 59 /** |
| 60 * Checks that [object] is of the expected type [type]. If not, sends an |
| 61 * appropriate error response. |
| 62 * |
| 63 * Returns `true` if a type error is detected and the containing function should |
| 64 * short-circuit. |
| 65 */ |
| 66 bool checkType(Dart_Port p, Dart_CObject* object, enum Type type); |
| 67 |
| 68 /** |
| 69 * Gets the [i]th argument from [request], which should be the Dart object |
| 70 * passed in to each handler function. If this fails (e.g. there isn't an [i]th |
| 71 * argument), it will return `NULL`. |
| 72 */ |
| 73 Dart_CObject* getArgument(Dart_Port p, Dart_CObject* request, int i); |
| 74 |
| 75 /** |
| 76 * Like [getArgument], but also ensures that the argument is of type [type]. |
| 77 */ |
| 78 Dart_CObject* getTypedArgument(Dart_Port p, Dart_CObject* request, int i, |
| 79 enum Type type); |
| 80 |
| 81 /** |
| 82 * Like [getArgument], but also ensures that the argument is an integer type |
| 83 * (`int32` or `int64`). [getInteger] should be used to extract the actual value |
| 84 * of the argument. |
| 85 */ |
| 86 Dart_CObject* getIntArgument(Dart_Port p, Dart_CObject* request, int i); |
| 87 |
| 88 /** |
| 89 * Gets the integer value of [object], which should be an `int32` or an `int64`. |
| 90 * Note that this does not validate the type of its argument. |
| 91 */ |
| 92 int64_t getInteger(Dart_CObject* object); |
| 93 |
| 94 /** Declares a null [Dart_CObject] named [name]. */ |
| 95 #define DART_NULL(name) \ |
| 96 Dart_CObject name; \ |
| 97 name.type = kNull; |
| 98 |
| 99 /** |
| 100 * Declares a [Dart_CObject] bool named [name] with value [val]. |
| 101 * |
| 102 * [val] should be a C boolean. |
| 103 */ |
| 104 #define DART_BOOL(name, val) \ |
| 105 Dart_CObject name; \ |
| 106 name.type = kBool; \ |
| 107 name.value.as_bool = val; |
| 108 |
| 109 /** |
| 110 * Declares a [Dart_CObject] `int32` named [name] with value [val]. |
| 111 * |
| 112 * [val] should be a C integer. |
| 113 */ |
| 114 #define DART_INT32(name, val) \ |
| 115 Dart_CObject name; \ |
| 116 name.type = kInt32; \ |
| 117 name.value.as_int32 = val; |
| 118 |
| 119 /** |
| 120 * Declares a [Dart_CObject] `int64` named [name] with value [val]. |
| 121 * |
| 122 * [val] should be a C integer. |
| 123 */ |
| 124 #define DART_INT64(name, val) \ |
| 125 Dart_CObject name; \ |
| 126 name.type = kInt64; \ |
| 127 name.value.as_int64 = val; |
| 128 |
| 129 /** |
| 130 * Declares a [Dart_CObject] double named [name] with value [val]. |
| 131 * |
| 132 * [val] should be a C float. |
| 133 */ |
| 134 #define DART_DOUBLE(name, val) \ |
| 135 Dart_CObject name; \ |
| 136 name.type = kDouble; \ |
| 137 name.value.as_double = val; |
| 138 |
| 139 /** |
| 140 * Declares a [Dart_CObject] string named [name] with value [val]. |
| 141 * |
| 142 * [val] should be a C string or `NULL`. |
| 143 */ |
| 144 #define DART_STRING(name, val) \ |
| 145 Dart_CObject name; \ |
| 146 if (val == NULL) { \ |
| 147 name.type = kNull; \ |
| 148 } else { \ |
| 149 name.type = kString; \ |
| 150 name.value.as_string = val; \ |
| 151 } |
| 152 |
| 153 #endif // DART_ARCHIVE_MESSAGING_H_ |
OLD | NEW |