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 #include <errno.h> |
| 6 |
| 7 #include "messaging.h" |
| 8 |
| 9 void postResult(Dart_Port p, bool success, int err, Dart_CObject* response) { |
| 10 DART_BOOL(wrapped_success, success) |
| 11 DART_INT32(wrapped_err, err) |
| 12 |
| 13 Dart_CObject* values[3]; |
| 14 values[0] = &wrapped_success; |
| 15 values[1] = &wrapped_err; |
| 16 values[2] = response; |
| 17 |
| 18 Dart_CObject full_response; |
| 19 full_response.type = kArray; |
| 20 full_response.value.as_array.values = values; |
| 21 full_response.value.as_array.length = 3; |
| 22 |
| 23 Dart_PostCObject(p, &full_response); |
| 24 } |
| 25 |
| 26 void postError(Dart_Port p, struct archive* a) { |
| 27 DART_STRING(error_string, (char*) archive_error_string(a)) |
| 28 postResult(p, false, archive_errno(a), &error_string); |
| 29 } |
| 30 |
| 31 void postInvalidArgument(Dart_Port p, char* message) { |
| 32 DART_STRING(error_string, message) |
| 33 postResult(p, false, EINVAL, &error_string); |
| 34 } |
| 35 |
| 36 void postSuccess(Dart_Port p, Dart_CObject* response) { |
| 37 if (response != NULL) { |
| 38 postResult(p, true, 0, response); |
| 39 return; |
| 40 } |
| 41 |
| 42 DART_NULL(null_response) |
| 43 postResult(p, true, 0, &null_response); |
| 44 } |
| 45 |
| 46 bool checkError(Dart_Port p, struct archive* a, int result) { |
| 47 if (result == ARCHIVE_OK) return false; |
| 48 // TODO(nweiz): What should we do about non-fatal warnings? |
| 49 if (result == ARCHIVE_WARN) return false; |
| 50 postError(p, a); |
| 51 } |
| 52 |
| 53 bool checkType(Dart_Port p, Dart_CObject* object, enum Type type) { |
| 54 if (object->type == type) return false; |
| 55 char buffer[100]; |
| 56 snprintf(buffer, 100, "Invalid argument: expected type %d, was type %d.", |
| 57 type, object->type); |
| 58 postInvalidArgument(p, buffer); |
| 59 return true; |
| 60 } |
| 61 |
| 62 void checkResult(Dart_Port p, struct archive* a, int result) { |
| 63 if (checkError(p, a, result)) return; |
| 64 postSuccess(p, NULL); |
| 65 } |
| 66 |
| 67 Dart_CObject* getArgument(Dart_Port p, Dart_CObject* request, int i) { |
| 68 if (checkType(p, request, kArray)) return NULL; |
| 69 |
| 70 i += 2; // Skip over the message name and archive id. |
| 71 if (request->value.as_array.length > i) { |
| 72 return request->value.as_array.values[i]; |
| 73 } |
| 74 |
| 75 char buffer[100]; |
| 76 snprintf(buffer, 100, "Invalid argument: expected at least %d arguments, " \ |
| 77 "were %d.", i - 2, request->value.as_array.length - 2); |
| 78 postInvalidArgument(p, buffer); |
| 79 return NULL; |
| 80 } |
| 81 |
| 82 Dart_CObject* getTypedArgument(Dart_Port p, Dart_CObject* request, int i, |
| 83 enum Type type) { |
| 84 Dart_CObject* arg = getArgument(p, request, i); |
| 85 if (arg == NULL) return NULL; |
| 86 if (checkType(p, arg, type)) return NULL; |
| 87 return arg; |
| 88 } |
| 89 |
| 90 Dart_CObject* getIntArgument(Dart_Port p, Dart_CObject* request, int i) { |
| 91 Dart_CObject* arg = getArgument(p, request, i); |
| 92 if (arg == NULL) return NULL; |
| 93 if (arg->type == kInt64) return arg; |
| 94 if (arg->type == kInt32) return arg; |
| 95 char buffer[100]; |
| 96 snprintf(buffer, 100, "Invalid argument %d: expected integer, was type %d.", |
| 97 i+1, arg->type); |
| 98 postInvalidArgument(p, buffer); |
| 99 return NULL; |
| 100 } |
| 101 |
| 102 int64_t getInteger(Dart_CObject* object) { |
| 103 assert(object->type == kInt64 || object->type == kInt32); |
| 104 if (object->type == kInt64) return object->value.as_int64; |
| 105 if (object->type == kInt32) return (int64_t) object->value.as_int32; |
| 106 } |
OLD | NEW |