Backed out changeset 8f976ed899d7 (bug 1847231) for causing bc failures on browser_se...
[gecko.git] / js / src / vm / ModuleBuilder.h
blob3716a6cdd2b7e22a7a25deba526cf958ab8d3302
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef vm_ModuleBuilder_h
8 #define vm_ModuleBuilder_h
10 #include "mozilla/Attributes.h" // MOZ_STACK_CLASS
12 #include "jstypes.h" // JS_PUBLIC_API
13 #include "frontend/EitherParser.h" // js::frontend::EitherParser
14 #include "frontend/ParserAtom.h" // js::frontend::TaggedParserAtomIndex
15 #include "frontend/Stencil.h" // js::frontend::StencilModuleEntry
16 #include "frontend/TaggedParserAtomIndexHasher.h" // frontend::TaggedParserAtomIndexHasher
17 #include "js/GCVector.h" // JS::GCVector
19 struct JS_PUBLIC_API JSContext;
20 class JS_PUBLIC_API JSAtom;
22 namespace js {
24 namespace frontend {
26 class BinaryNode;
27 class ListNode;
28 class ParseNode;
30 } // namespace frontend
32 // Process a module's parse tree to collate the import and export data used when
33 // creating a ModuleObject.
34 class MOZ_STACK_CLASS ModuleBuilder {
35 explicit ModuleBuilder(FrontendContext* fc,
36 const frontend::EitherParser& eitherParser);
38 public:
39 template <class Parser>
40 explicit ModuleBuilder(FrontendContext* fc, Parser* parser)
41 : ModuleBuilder(fc, frontend::EitherParser(parser)) {}
43 bool processImport(frontend::BinaryNode* importNode);
44 bool processExport(frontend::ParseNode* exportNode);
45 bool processExportFrom(frontend::BinaryNode* exportNode);
47 bool hasExportedName(frontend::TaggedParserAtomIndex name) const;
49 bool buildTables(frontend::StencilModuleMetadata& metadata);
51 // During BytecodeEmitter we note top-level functions, and afterwards we must
52 // call finishFunctionDecls on the list.
53 bool noteFunctionDeclaration(FrontendContext* fc, uint32_t funIndex);
54 void finishFunctionDecls(frontend::StencilModuleMetadata& metadata);
56 void noteAsync(frontend::StencilModuleMetadata& metadata);
58 private:
59 using MaybeModuleRequestIndex = frontend::MaybeModuleRequestIndex;
60 using ModuleRequestVector = frontend::StencilModuleMetadata::RequestVector;
61 using RequestedModuleVector = frontend::StencilModuleMetadata::EntryVector;
63 using AtomSet = HashSet<frontend::TaggedParserAtomIndex,
64 frontend::TaggedParserAtomIndexHasher>;
65 using ExportEntryVector = Vector<frontend::StencilModuleEntry>;
66 using ImportEntryMap =
67 HashMap<frontend::TaggedParserAtomIndex, frontend::StencilModuleEntry,
68 frontend::TaggedParserAtomIndexHasher>;
70 FrontendContext* fc_;
71 frontend::EitherParser eitherParser_;
73 // These are populated while parsing.
74 ModuleRequestVector moduleRequests_;
75 AtomSet requestedModuleSpecifiers_;
76 RequestedModuleVector requestedModules_;
77 ImportEntryMap importEntries_;
78 ExportEntryVector exportEntries_;
79 AtomSet exportNames_;
81 // These are populated while emitting bytecode.
82 FunctionDeclarationVector functionDecls_;
84 frontend::StencilModuleEntry* importEntryFor(
85 frontend::TaggedParserAtomIndex localName) const;
87 bool processExportBinding(frontend::ParseNode* binding);
88 bool processExportArrayBinding(frontend::ListNode* array);
89 bool processExportObjectBinding(frontend::ListNode* obj);
91 MaybeModuleRequestIndex appendModuleRequest(
92 frontend::TaggedParserAtomIndex specifier,
93 frontend::ListNode* assertionList);
95 bool appendExportEntry(frontend::TaggedParserAtomIndex exportName,
96 frontend::TaggedParserAtomIndex localName,
97 frontend::ParseNode* node = nullptr);
99 bool maybeAppendRequestedModule(MaybeModuleRequestIndex moduleRequest,
100 frontend::ParseNode* node);
102 void markUsedByStencil(frontend::TaggedParserAtomIndex name);
104 [[nodiscard]] bool processAssertions(frontend::StencilModuleRequest& request,
105 frontend::ListNode* assertionList);
107 [[nodiscard]] bool isAssertionSupported(
108 JS::ImportAssertion supportedAssertion,
109 frontend::TaggedParserAtomIndex key);
112 template <typename T>
113 ArrayObject* CreateArray(JSContext* cx,
114 const JS::Rooted<JS::GCVector<T>>& vector);
116 } // namespace js
118 #endif // vm_ModuleBuilder_h