@@ -542,6 +542,11 @@ void AppendInlineExportFields(Module* module,
542542 module ->AppendFields (fields);
543543}
544544
545+ bool IsIndexLikelyType (Index index) {
546+ // TODO: Incorrect values can be misinterpreted by the parser.
547+ return index >= static_cast <Index>(Type::Void);
548+ }
549+
545550} // End of anonymous namespace
546551
547552WastParser::WastParser (WastLexer* lexer,
@@ -937,10 +942,10 @@ Result WastParser::ParseValueTypeList(TypeVector* out_type_list,
937942 CHECK_RESULT (ParseValueType (&type));
938943
939944 if (type.is_index ()) {
940- // TODO: Incorrect values can be misinterpreted by the parser.
941- if (type.index () >= static_cast <Index>(Type::Void)) {
945+ if (IsIndexLikelyType (type.index ())) {
942946 out_type_list->push_back (Type (type.index ()));
943947 } else {
948+ type_vars->push_back (ReferenceVar (out_type_list->size (), type));
944949 out_type_list->push_back (Type (Type::Reference, type.index ()));
945950 }
946951 } else {
@@ -1313,11 +1318,24 @@ Result WastParser::ResolveRefTypes(const Module& module,
13131318
13141319 assert (type.IsReferenceWithIndex ());
13151320
1321+ if (ref_var.var .is_index ()) {
1322+ if (type.GetReferenceIndex () >= module .types .size ()) {
1323+ errors->emplace_back (
1324+ ErrorLevel::Error, ref_var.var .loc ,
1325+ StringPrintf (" reference type out of range: %d (max: %d)" ,
1326+ static_cast <int >(type.GetReferenceIndex ()),
1327+ static_cast <int >(module .types .size ())));
1328+ result = Result::Error;
1329+ }
1330+ continue ;
1331+ }
1332+
13161333 if (type.GetReferenceIndex () != kInvalidIndex ) {
1334+ // Resolved earlier.
13171335 continue ;
13181336 }
13191337
1320- const auto type_index = module .type_bindings .FindIndex (ref_var.var .name ());
1338+ Index type_index = module .type_bindings .FindIndex (ref_var.var .name ());
13211339
13221340 if (type_index == kInvalidIndex ) {
13231341 errors->emplace_back (ErrorLevel::Error, ref_var.var .loc ,
@@ -2057,7 +2075,12 @@ Result WastParser::ParseBoundValueTypeList(TokenType token,
20572075 bindings->emplace (name,
20582076 Binding (loc, binding_index_offset + types->size ()));
20592077 if (type.is_index ()) {
2060- types->push_back (Type (type.index ()));
2078+ if (IsIndexLikelyType (type.index ())) {
2079+ types->push_back (Type (type.index ()));
2080+ } else {
2081+ type_vars->push_back (ReferenceVar (types->size (), type));
2082+ types->push_back (Type (Type::Reference, type.index ()));
2083+ }
20612084 } else {
20622085 assert (type.is_name ());
20632086 assert (options_->features .function_references_enabled ());
0 commit comments