Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sts=4 et sw=4 tw=99:
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/. */
6 :
7 : #ifndef frontend_BytecodeCompiler_h
8 : #define frontend_BytecodeCompiler_h
9 :
10 : #include "mozilla/Maybe.h"
11 :
12 : #include "NamespaceImports.h"
13 :
14 : #include "vm/Scope.h"
15 : #include "vm/String.h"
16 : #include "vm/TraceLogging.h"
17 :
18 : class JSLinearString;
19 :
20 : namespace js {
21 :
22 : class LazyScript;
23 : class LifoAlloc;
24 : class ModuleObject;
25 : class ScriptSourceObject;
26 :
27 : namespace frontend {
28 :
29 : class TokenStreamAnyChars;
30 : class FunctionBox;
31 : class ParseNode;
32 :
33 : JSScript*
34 : CompileGlobalScript(JSContext* cx, LifoAlloc& alloc, ScopeKind scopeKind,
35 : const ReadOnlyCompileOptions& options,
36 : SourceBufferHolder& srcBuf,
37 : ScriptSourceObject** sourceObjectOut = nullptr);
38 :
39 : JSScript*
40 : CompileEvalScript(JSContext* cx, LifoAlloc& alloc,
41 : HandleObject scopeChain, HandleScope enclosingScope,
42 : const ReadOnlyCompileOptions& options,
43 : SourceBufferHolder& srcBuf,
44 : ScriptSourceObject** sourceObjectOut = nullptr);
45 :
46 : ModuleObject*
47 : CompileModule(JSContext* cx, const ReadOnlyCompileOptions& options,
48 : SourceBufferHolder& srcBuf);
49 :
50 : ModuleObject*
51 : CompileModule(JSContext* cx, const ReadOnlyCompileOptions& options,
52 : SourceBufferHolder& srcBuf, LifoAlloc& alloc,
53 : ScriptSourceObject** sourceObjectOut = nullptr);
54 :
55 : MOZ_MUST_USE bool
56 : CompileLazyFunction(JSContext* cx, Handle<LazyScript*> lazy, const char16_t* chars, size_t length);
57 :
58 : //
59 : // Compile a single function. The source in srcBuf must match the ECMA-262
60 : // FunctionExpression production.
61 : //
62 : // If nonzero, parameterListEnd is the offset within srcBuf where the parameter
63 : // list is expected to end. During parsing, if we find that it ends anywhere
64 : // else, it's a SyntaxError. This is used to implement the Function constructor;
65 : // it's how we detect that these weird cases are SyntaxErrors:
66 : //
67 : // Function("/*", "*/x) {")
68 : // Function("x){ if (3", "return x;}")
69 : //
70 : MOZ_MUST_USE bool
71 : CompileStandaloneFunction(JSContext* cx, MutableHandleFunction fun,
72 : const ReadOnlyCompileOptions& options,
73 : JS::SourceBufferHolder& srcBuf,
74 : const mozilla::Maybe<uint32_t>& parameterListEnd,
75 : HandleScope enclosingScope = nullptr);
76 :
77 : MOZ_MUST_USE bool
78 : CompileStandaloneGenerator(JSContext* cx, MutableHandleFunction fun,
79 : const ReadOnlyCompileOptions& options,
80 : JS::SourceBufferHolder& srcBuf,
81 : const mozilla::Maybe<uint32_t>& parameterListEnd);
82 :
83 : MOZ_MUST_USE bool
84 : CompileStandaloneAsyncFunction(JSContext* cx, MutableHandleFunction fun,
85 : const ReadOnlyCompileOptions& options,
86 : JS::SourceBufferHolder& srcBuf,
87 : const mozilla::Maybe<uint32_t>& parameterListEnd);
88 :
89 : MOZ_MUST_USE bool
90 : CompileStandaloneAsyncGenerator(JSContext* cx, MutableHandleFunction fun,
91 : const ReadOnlyCompileOptions& options,
92 : JS::SourceBufferHolder& srcBuf,
93 : const mozilla::Maybe<uint32_t>& parameterListEnd);
94 :
95 : MOZ_MUST_USE bool
96 : CompileAsyncFunctionBody(JSContext* cx, MutableHandleFunction fun,
97 : const ReadOnlyCompileOptions& options,
98 : Handle<PropertyNameVector> formals, JS::SourceBufferHolder& srcBuf);
99 :
100 : ScriptSourceObject*
101 : CreateScriptSourceObject(JSContext* cx, const ReadOnlyCompileOptions& options,
102 : const mozilla::Maybe<uint32_t>& parameterListEnd = mozilla::Nothing());
103 :
104 : /*
105 : * True if str consists of an IdentifierStart character, followed by one or
106 : * more IdentifierPart characters, i.e. it matches the IdentifierName production
107 : * in the language spec.
108 : *
109 : * This returns true even if str is a keyword like "if".
110 : *
111 : * Defined in TokenStream.cpp.
112 : */
113 : bool
114 : IsIdentifier(JSLinearString* str);
115 :
116 : /*
117 : * As above, but taking chars + length.
118 : */
119 : bool
120 : IsIdentifier(const char* chars, size_t length);
121 : bool
122 : IsIdentifier(const char16_t* chars, size_t length);
123 :
124 : /* True if str is a keyword. Defined in TokenStream.cpp. */
125 : bool
126 : IsKeyword(JSLinearString* str);
127 :
128 : /* Trace all GC things reachable from parser. Defined in Parser.cpp. */
129 : void
130 : TraceParser(JSTracer* trc, JS::AutoGCRooter* parser);
131 :
132 13318 : class MOZ_STACK_CLASS AutoFrontendTraceLog
133 : {
134 : #ifdef JS_TRACE_LOGGING
135 : TraceLoggerThread* logger_;
136 : mozilla::Maybe<TraceLoggerEvent> frontendEvent_;
137 : mozilla::Maybe<AutoTraceLog> frontendLog_;
138 : mozilla::Maybe<AutoTraceLog> typeLog_;
139 : #endif
140 :
141 : public:
142 : AutoFrontendTraceLog(JSContext* cx, const TraceLoggerTextId id,
143 : const char* filename, size_t line, size_t column);
144 :
145 : AutoFrontendTraceLog(JSContext* cx, const TraceLoggerTextId id,
146 : const TokenStreamAnyChars& tokenStream);
147 :
148 : AutoFrontendTraceLog(JSContext* cx, const TraceLoggerTextId id,
149 : const TokenStreamAnyChars& tokenStream, FunctionBox* funbox);
150 :
151 : AutoFrontendTraceLog(JSContext* cx, const TraceLoggerTextId id,
152 : const TokenStreamAnyChars& tokenStream, ParseNode* pn);
153 : };
154 :
155 : } /* namespace frontend */
156 : } /* namespace js */
157 :
158 : #endif /* frontend_BytecodeCompiler_h */
|