|
37 | 37 | #include <alloca.h> |
38 | 38 | #endif |
39 | 39 |
|
40 | | -#define ERROR_IF(expr, ...) \ |
41 | | - do { \ |
42 | | - if (expr) { \ |
43 | | - PrintError(__VA_ARGS__); \ |
44 | | - return Result::Error; \ |
45 | | - } \ |
| 40 | +#define ERROR(...) \ |
| 41 | + do { \ |
| 42 | + PrintError(__VA_ARGS__); \ |
| 43 | + return Result::Error; \ |
| 44 | + } while (0) |
| 45 | + |
| 46 | +#define ERROR_IF(expr, ...) \ |
| 47 | + do { \ |
| 48 | + if (expr) { \ |
| 49 | + ERROR(__VA_ARGS__); \ |
| 50 | + } \ |
46 | 51 | } while (0) |
47 | 52 |
|
48 | 53 | #define ERROR_UNLESS(expr, ...) ERROR_IF(!(expr), __VA_ARGS__) |
@@ -100,6 +105,7 @@ class BinaryReader { |
100 | 105 | const char* type_name, |
101 | 106 | const char* desc); |
102 | 107 | [[nodiscard]] Result ReadU8(uint8_t* out_value, const char* desc); |
| 108 | + [[nodiscard]] Result ReadU16(uint16_t* out_value, const char* desc); |
103 | 109 | [[nodiscard]] Result ReadU32(uint32_t* out_value, const char* desc); |
104 | 110 | [[nodiscard]] Result ReadF32(uint32_t* out_value, const char* desc); |
105 | 111 | [[nodiscard]] Result ReadF64(uint64_t* out_value, const char* desc); |
@@ -300,6 +306,10 @@ Result BinaryReader::ReadU8(uint8_t* out_value, const char* desc) { |
300 | 306 | return ReadT(out_value, "uint8_t", desc); |
301 | 307 | } |
302 | 308 |
|
| 309 | +Result BinaryReader::ReadU16(uint16_t* out_value, const char* desc) { |
| 310 | + return ReadT(out_value, "uint16_t", desc); |
| 311 | +} |
| 312 | + |
303 | 313 | Result BinaryReader::ReadU32(uint32_t* out_value, const char* desc) { |
304 | 314 | return ReadT(out_value, "uint32_t", desc); |
305 | 315 | } |
@@ -3086,11 +3096,24 @@ Result BinaryReader::ReadModule(const ReadModuleOptions& options) { |
3086 | 3096 | uint32_t magic = 0; |
3087 | 3097 | CHECK_RESULT(ReadU32(&magic, "magic")); |
3088 | 3098 | ERROR_UNLESS(magic == WABT_BINARY_MAGIC, "bad magic value"); |
3089 | | - uint32_t version = 0; |
3090 | | - CHECK_RESULT(ReadU32(&version, "version")); |
3091 | | - ERROR_UNLESS(version == WABT_BINARY_VERSION, |
3092 | | - "bad wasm file version: %#x (expected %#x)", version, |
3093 | | - WABT_BINARY_VERSION); |
| 3099 | + |
| 3100 | + uint16_t version = 0, layer = 0; |
| 3101 | + CHECK_RESULT(ReadU16(&version, "version")); |
| 3102 | + CHECK_RESULT(ReadU16(&layer, "layer")); |
| 3103 | + |
| 3104 | + switch (layer) { |
| 3105 | + case WABT_BINARY_LAYER_MODULE: |
| 3106 | + ERROR_UNLESS(version == WABT_BINARY_VERSION, |
| 3107 | + "bad wasm file version: %#x (expected %#x)", version, |
| 3108 | + WABT_BINARY_VERSION); |
| 3109 | + break; |
| 3110 | + case WABT_BINARY_LAYER_COMPONENT: |
| 3111 | + ERROR("wasm components are not yet supported in this tool"); |
| 3112 | + break; |
| 3113 | + default: |
| 3114 | + ERROR("unsupported wasm layer: %#x", layer); |
| 3115 | + break; |
| 3116 | + } |
3094 | 3117 |
|
3095 | 3118 | CALLBACK(BeginModule, version); |
3096 | 3119 | CHECK_RESULT(ReadSections(ReadSectionsOptions{options.stop_on_first_error})); |
|
0 commit comments