diff options
author | Ben Smith <binji@chromium.org> | 2020-03-09 11:45:17 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-09 11:45:17 -0700 |
commit | f649b7ca9af02ff3213d8c2b135eaef9d87f5e0d (patch) | |
tree | 41ee03ea4b45fcf9ba2e1347c6f41400c89e53dd /src/binary-writer.cc | |
parent | bec78eafbc203d81b9a6d1ce81f5a80dd7bf692a (diff) | |
download | wabt-f649b7ca9af02ff3213d8c2b135eaef9d87f5e0d.tar.gz wabt-f649b7ca9af02ff3213d8c2b135eaef9d87f5e0d.tar.bz2 wabt-f649b7ca9af02ff3213d8c2b135eaef9d87f5e0d.zip |
Initial pass parsing/reading struct (#1352)
This parses just the format `(struct)` as a new type. I added a test for
this using `wat2wasm`, but that requires a rudimentary binary format.
The test runner automatically attempts to rountrip all wat2wasm tests,
so this required implementing the wat writing and binary reading too.
Here's a summary of the changes:
* binary-reader:h: Rename `BinaryReader::OnType` callbacks to `OnFuncType`
* binary-reader.h: Add `BinaryReader::OnStructType`
* binary-reader.cc: Use a switch after reading the type form to
determine whether we're reading a function or struct.
* tokens.def: Add new `TokenType::Struct`
* lexer-keywords.txt: Add new `struct` keyword
* type.h: Add `Type::Struct` type form
* wast-parser.cc: Parse `(struct)` in text format
* wat-writer.cc: Write Func or Struct type forms
Diffstat (limited to 'src/binary-writer.cc')
-rw-r--r-- | src/binary-writer.cc | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/src/binary-writer.cc b/src/binary-writer.cc index ab4d96ea..3f45863a 100644 --- a/src/binary-writer.cc +++ b/src/binary-writer.cc @@ -880,21 +880,33 @@ Result BinaryWriter::WriteModule() { BeginKnownSection(BinarySection::Type); WriteU32Leb128(stream_, module_->types.size(), "num types"); for (size_t i = 0; i < module_->types.size(); ++i) { - const FuncType* func_type = cast<FuncType>(module_->types[i]); - const FuncSignature* sig = &func_type->sig; - WriteHeader("type", i); - WriteType(stream_, Type::Func); - - Index num_params = sig->param_types.size(); - Index num_results = sig->result_types.size(); - WriteU32Leb128(stream_, num_params, "num params"); - for (size_t j = 0; j < num_params; ++j) { - WriteType(stream_, sig->param_types[j]); - } + const TypeEntry* type = module_->types[i]; + switch (type->kind()) { + case TypeEntryKind::Func: { + const FuncType* func_type = cast<FuncType>(type); + const FuncSignature* sig = &func_type->sig; + WriteHeader("type", i); // TODO: switch to "func type"? + WriteType(stream_, Type::Func); + + Index num_params = sig->param_types.size(); + Index num_results = sig->result_types.size(); + WriteU32Leb128(stream_, num_params, "num params"); + for (size_t j = 0; j < num_params; ++j) { + WriteType(stream_, sig->param_types[j]); + } - WriteU32Leb128(stream_, num_results, "num results"); - for (size_t j = 0; j < num_results; ++j) { - WriteType(stream_, sig->result_types[j]); + WriteU32Leb128(stream_, num_results, "num results"); + for (size_t j = 0; j < num_results; ++j) { + WriteType(stream_, sig->result_types[j]); + } + break; + } + + case TypeEntryKind::Struct: + WriteHeader("struct type", i); + WriteType(stream_, Type::Struct); + // TODO + break; } } EndSection(); |