Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/wabt/wast-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class WastParser {
Result ParseCatchInstrList(CatchVector* catches);
Result ParseCatchExprList(CatchVector* catches);
Result ParseGlobalType(Global*);
Result ParseField(Field*);
Result ParseField(Field*, Var* out_type, bool* defer_named_ref);
Result ParseFieldList(std::vector<Field>*);

template <typename T>
Expand Down
45 changes: 36 additions & 9 deletions src/wast-parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1790,7 +1790,12 @@ Result WastParser::ParseTypeModuleField(Module* module) {
Error(loc, "array type not allowed");
}
auto array_type = std::make_unique<ArrayType>(name);
CHECK_RESULT(ParseField(&array_type->field));
Var type_var;
bool defer_named_ref = false;
CHECK_RESULT(ParseField(&array_type->field, &type_var, &defer_named_ref));
if (defer_named_ref) {
VarToType(type_var, &array_type->field.type);
}
field->type = std::move(array_type);
} else {
return ErrorExpected({"func", "struct", "array"});
Expand All @@ -1802,21 +1807,28 @@ Result WastParser::ParseTypeModuleField(Module* module) {
return Result::Ok;
}

Result WastParser::ParseField(Field* field) {
Result WastParser::ParseField(Field* field,
Var* out_type,
bool* defer_named_ref) {
WABT_TRACE(ParseField);
auto parse_mut_valuetype = [&]() -> Result {
// TODO: Share with ParseGlobalType?
if (MatchLpar(TokenType::Mut)) {
field->mutable_ = true;
Var type;
CHECK_RESULT(ParseValueType(&type));
field->type = Type(type.opt_type());
CHECK_RESULT(ParseValueType(out_type));
EXPECT(Rpar);
} else {
field->mutable_ = false;
Var type;
CHECK_RESULT(ParseValueType(&type));
field->type = Type(type.opt_type());
CHECK_RESULT(ParseValueType(out_type));
}

if (!Type::EnumIsReferenceWithIndex(out_type->opt_type()) ||
out_type->is_index()) {
field->type = out_type->to_type();
*defer_named_ref = false;
} else {
field->type = Type(out_type->opt_type(), kInvalidIndex);
*defer_named_ref = true;
}
return Result::Ok;
};
Expand All @@ -1834,11 +1846,26 @@ Result WastParser::ParseField(Field* field) {

Result WastParser::ParseFieldList(std::vector<Field>* fields) {
WABT_TRACE(ParseFieldList);
struct Deferred {
size_t index;
Var var;
};
std::vector<Deferred> deferred;

while (PeekMatch(TokenType::ValueType) || PeekMatch(TokenType::Lpar)) {
Field field;
CHECK_RESULT(ParseField(&field));
Var type_var;
bool defer_named_ref = false;
CHECK_RESULT(ParseField(&field, &type_var, &defer_named_ref));
if (defer_named_ref) {
deferred.push_back({fields->size(), type_var});
}
fields->push_back(field);
}

for (const auto& d : deferred) {
VarToType(d.var, &(*fields)[d.index].type);
}
return Result::Ok;
}

Expand Down
23 changes: 23 additions & 0 deletions test/regress/regress-2750.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
;;; TOOL: run-roundtrip
;;; ARGS: --stdout --enable-gc --enable-function-references

(module
(type $t0 (struct))
(type $t1 (func))

(type
(struct
(field (ref 1))
(field (mut (ref null 1)))
(field (ref $t1))))

(type
(array (ref 1))))

(;; STDOUT ;;;
(module
(type (;0;) (struct))
(type (;1;) (func))
(type (;2;) (struct (field (;0;) (ref 1)) (field (;1;) (mut (ref null 1))) (field (;2;) (ref 1))))
(type (;3;) (array (ref 1))))
;;; STDOUT ;;)
3 changes: 3 additions & 0 deletions test/run-roundtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def main(args):
parser.add_argument('--enable-exceptions', action='store_true')
parser.add_argument('--enable-saturating-float-to-int', action='store_true')
parser.add_argument('--enable-function-references', action='store_true')
parser.add_argument('--enable-gc', action='store_true')
parser.add_argument('--enable-threads', action='store_true')
parser.add_argument('--enable-sign-extension', action='store_true')
parser.add_argument('--enable-multi-value', action='store_true')
Expand Down Expand Up @@ -136,6 +137,7 @@ def main(args):
options.enable_saturating_float_to_int,
'--enable-sign-extension': options.enable_sign_extension,
'--enable-function-references': options.enable_function_references,
'--enable-gc': options.enable_gc,
'--enable-threads': options.enable_threads,
'--enable-tail-call': options.enable_tail_call,
'--disable-reference-types': options.disable_reference_types,
Expand All @@ -160,6 +162,7 @@ def main(args):
'--enable-sign-extension': options.enable_sign_extension,
'--enable-tail-call': options.enable_tail_call,
'--enable-function-references': options.enable_function_references,
'--enable-gc': options.enable_gc,
'--disable-reference-types': options.disable_reference_types,
'--enable-threads': options.enable_threads,
'--enable-memory64': options.enable_memory64,
Expand Down
Loading