| 828 | } |
| 829 | |
| 830 | void ParseContext::parse_globalsection() { |
| 831 | tok_.expect(TokenType::LParen); |
| 832 | tok_.expect_keyword("global"); |
| 833 | index_t global_idx = global_map_.records.size(); |
| 834 | // optional Id |
| 835 | if (tok_.peek().type == TokenType::Id) { |
| 836 | std::string id = tok_.consume().text; |
| 837 | if (global_map_.id_map.contains(id)) |
| 838 | throw Exception::Parse("duplicated global id '" + id + "'", tok_.location()); |
| 839 | global_map_.id_map[id] = global_idx; |
| 840 | } |
| 841 | // exportabbr* |
| 842 | while (tok_.peek().type == TokenType::LParen && |
| 843 | tok_.peek2().type == TokenType::Keyword && tok_.peek2().text == "export") { |
| 844 | WasmExport& exp = module_.exports.emplace_back(); |
| 845 | exp.name = parse_exportabbr(); |
| 846 | exp.index = global_idx; |
| 847 | exp.desc = WasmExport::DescType::global; |
| 848 | } |
| 849 | // import or normal |
| 850 | if (tok_.peek().type == TokenType::LParen && |
| 851 | tok_.peek2().type == TokenType::Keyword && tok_.peek2().text == "import") { |
| 852 | global_map_.records.emplace_back(IndexSpace::Type::Import); |
| 853 | auto [imod, iname] = parse_importabbr(); |
| 854 | WasmImport& imp = module_.imports.emplace_back(); |
| 855 | imp.desc = parse_globaltype(); |
| 856 | imp.module = imod; imp.name = iname; |
| 857 | } else { |
| 858 | global_map_.records.emplace_back(IndexSpace::Type::Normal); |
| 859 | WasmGlobal& global = module_.globals.emplace_back(); |
| 860 | global.type = parse_globaltype(); |
| 861 | global.init = parse_constexpr(); |
| 862 | } |
| 863 | tok_.expect(TokenType::RParen); |
| 864 | } |
| 865 | |
| 866 | void ParseContext::parse_exportsection() { |
| 867 | tok_.expect(TokenType::LParen); |