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