| 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 <errno.h> | 5 #include <errno.h> |
| 6 #include <stdarg.h> | 6 #include <stdarg.h> |
| 7 | 7 |
| 8 #include "messaging.h" | 8 #include "messaging.h" |
| 9 | 9 |
| 10 void postResult(Dart_Port p, bool success, int err, Dart_CObject* response) { | 10 void postResult(Dart_Port p, bool success, int err, Dart_CObject* response) { |
| 11 DART_BOOL(wrapped_success, success) | 11 DART_BOOL(wrapped_success, success); |
| 12 DART_INT32(wrapped_err, err) | 12 DART_INT32(wrapped_err, err); |
| 13 | 13 |
| 14 Dart_CObject* values[3]; | 14 Dart_CObject* values[3]; |
| 15 values[0] = &wrapped_success; | 15 values[0] = &wrapped_success; |
| 16 values[1] = &wrapped_err; | 16 values[1] = &wrapped_err; |
| 17 values[2] = response; | 17 values[2] = response; |
| 18 | 18 |
| 19 Dart_CObject full_response; | 19 Dart_CObject full_response; |
| 20 full_response.type = kArray; | 20 full_response.type = kArray; |
| 21 full_response.value.as_array.values = values; | 21 full_response.value.as_array.values = values; |
| 22 full_response.value.as_array.length = 3; | 22 full_response.value.as_array.length = 3; |
| 23 | 23 |
| 24 Dart_PostCObject(p, &full_response); | 24 Dart_PostCObject(p, &full_response); |
| 25 } | 25 } |
| 26 | 26 |
| 27 void postError(Dart_Port p, struct archive* a) { | 27 void postError(Dart_Port p, struct archive* a) { |
| 28 DART_STRING(error_string, (char*) archive_error_string(a)) | 28 DART_STRING(error_string, (char*) archive_error_string(a)); |
| 29 postResult(p, false, archive_errno(a), &error_string); | 29 postResult(p, false, archive_errno(a), &error_string); |
| 30 } | 30 } |
| 31 | 31 |
| 32 void postInvalidArgument(Dart_Port p, const char* format, ...) { | 32 void postInvalidArgument(Dart_Port p, const char* format, ...) { |
| 33 va_list args; | 33 va_list args; |
| 34 char buffer[256]; | 34 char buffer[256]; |
| 35 | 35 |
| 36 va_start(args, format); | 36 va_start(args, format); |
| 37 vsnprintf(buffer, 256, format, args); | 37 vsnprintf(buffer, 256, format, args); |
| 38 va_end(args); | 38 va_end(args); |
| 39 | 39 |
| 40 DART_STRING(error_string, buffer) | 40 DART_STRING(error_string, buffer); |
| 41 postResult(p, false, EINVAL, &error_string); | 41 postResult(p, false, EINVAL, &error_string); |
| 42 } | 42 } |
| 43 | 43 |
| 44 void postSuccess(Dart_Port p, Dart_CObject* response) { | 44 void postSuccess(Dart_Port p, Dart_CObject* response) { |
| 45 if (response != NULL) { | 45 if (response != NULL) { |
| 46 postResult(p, true, 0, response); | 46 postResult(p, true, 0, response); |
| 47 return; | 47 return; |
| 48 } | 48 } |
| 49 | 49 |
| 50 DART_NULL(null_response) | 50 DART_NULL(null_response); |
| 51 postResult(p, true, 0, &null_response); | 51 postResult(p, true, 0, &null_response); |
| 52 } | 52 } |
| 53 | 53 |
| 54 bool checkError(Dart_Port p, struct archive* a, int result) { | 54 bool checkError(Dart_Port p, struct archive* a, int result) { |
| 55 if (result == ARCHIVE_OK) return false; | 55 if (result == ARCHIVE_OK) return false; |
| 56 // TODO(nweiz): What should we do about non-fatal warnings? | 56 // TODO(nweiz): What should we do about non-fatal warnings? |
| 57 if (result == ARCHIVE_WARN) return false; | 57 if (result == ARCHIVE_WARN) return false; |
| 58 postError(p, a); | 58 postError(p, a); |
| 59 } | 59 } |
| 60 | 60 |
| 61 bool checkPointerError(Dart_Port p, void* pointer, char* name) { |
| 62 if (pointer != NULL) return false; |
| 63 char buffer[100]; |
| 64 snprintf(buffer, 100, "Failed to allocate memory for %s.", name); |
| 65 DART_STRING(error_string, buffer); |
| 66 postResult(p, false, ENOMEM, &error_string); |
| 67 return true; |
| 68 } |
| 69 |
| 61 bool checkType(Dart_Port p, Dart_CObject* object, enum Type type) { | 70 bool checkType(Dart_Port p, Dart_CObject* object, enum Type type) { |
| 62 if (object->type == type) return false; | 71 if (object->type == type) return false; |
| 63 postInvalidArgument(p, "Invalid argument: expected type %d, was type %d.", | 72 postInvalidArgument(p, "Invalid argument: expected type %d, was type %d.", |
| 64 type, object->type); | 73 type, object->type); |
| 65 return true; | 74 return true; |
| 66 } | 75 } |
| 67 | 76 |
| 68 void checkResult(Dart_Port p, struct archive* a, int result) { | 77 void checkResult(Dart_Port p, struct archive* a, int result) { |
| 69 if (checkError(p, a, result)) return; | 78 if (checkError(p, a, result)) return; |
| 70 postSuccess(p, NULL); | 79 postSuccess(p, NULL); |
| 71 } | 80 } |
| 72 | 81 |
| 82 void checkPointerResult(Dart_Port p, void* pointer, char* name) { |
| 83 if (checkPointerError(p, pointer, name)) return; |
| 84 DART_INT64(result, (intptr_t) pointer); |
| 85 postSuccess(p, &result); |
| 86 } |
| 87 |
| 73 Dart_CObject* getArgument(Dart_Port p, Dart_CObject* request, int i) { | 88 Dart_CObject* getArgument(Dart_Port p, Dart_CObject* request, int i) { |
| 74 if (checkType(p, request, kArray)) return NULL; | 89 if (checkType(p, request, kArray)) return NULL; |
| 75 | 90 |
| 76 i += 2; // Skip over the message name and archive id. | 91 i += 2; // Skip over the message name and archive id. |
| 77 if (request->value.as_array.length > i) { | 92 if (request->value.as_array.length > i) { |
| 78 return request->value.as_array.values[i]; | 93 return request->value.as_array.values[i]; |
| 79 } | 94 } |
| 80 | 95 |
| 81 postInvalidArgument(p, "Invalid argument: expected at least %d arguments, " \ | 96 postInvalidArgument(p, "Invalid argument: expected at least %d arguments, " \ |
| 82 "were %d.", i - 2, request->value.as_array.length - 2); | 97 "were %d.", i - 2, request->value.as_array.length - 2); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 99 postInvalidArgument(p, "Invalid argument %d: expected integer, was type %d.", | 114 postInvalidArgument(p, "Invalid argument %d: expected integer, was type %d.", |
| 100 i+1, arg->type); | 115 i+1, arg->type); |
| 101 return NULL; | 116 return NULL; |
| 102 } | 117 } |
| 103 | 118 |
| 104 int64_t getInteger(Dart_CObject* object) { | 119 int64_t getInteger(Dart_CObject* object) { |
| 105 assert(object->type == kInt64 || object->type == kInt32); | 120 assert(object->type == kInt64 || object->type == kInt32); |
| 106 if (object->type == kInt64) return object->value.as_int64; | 121 if (object->type == kInt64) return object->value.as_int64; |
| 107 if (object->type == kInt32) return (int64_t) object->value.as_int32; | 122 if (object->type == kInt32) return (int64_t) object->value.as_int32; |
| 108 } | 123 } |
| 124 |
| 125 Dart_CObject* getNullableStringArgument(Dart_Port p, Dart_CObject* request, |
| 126 int i) { |
| 127 Dart_CObject* arg = getArgument(p, request, i); |
| 128 if (arg == NULL) return NULL; |
| 129 if (arg->type == kNull) return arg; |
| 130 if (arg->type == kString) return arg; |
| 131 postInvalidArgument(p, "Invalid argument %d: expected string or null, was " \ |
| 132 "type %d.", i+1, arg->type); |
| 133 return NULL; |
| 134 } |
| 135 |
| 136 char* getNullableString(Dart_CObject* object) { |
| 137 assert(object->type == kNull || object->type == kString); |
| 138 if (object->type == kString) return object->value.as_string; |
| 139 return NULL; |
| 140 } |
| OLD | NEW |