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/. */
6 
7 #ifndef frontend_BytecodeCompilation_h
8 #define frontend_BytecodeCompilation_h
9 
10 #include "mozilla/Maybe.h"  // mozilla::Maybe, mozilla::Nothing
11 #include "mozilla/Utf8.h"   // mozilla::Utf8Unit
12 
13 #include <stddef.h>  // size_t
14 #include <stdint.h>  // uint32_t
15 
16 #include "jstypes.h"  // JS_PUBLIC_API
17 
18 #include "frontend/CompilationStencil.h"  // CompilationStencil, ExtensibleCompilationStencil, CompilationGCOutput
19 #include "frontend/ParseContext.h"  // js::frontend::UsedNameTracker
20 #include "frontend/SharedContext.h"  // js::frontend::Directives, js::frontend::{,Eval,Global}SharedContext
21 #include "js/CompileOptions.h"  // JS::ReadOnlyCompileOptions
22 #include "js/RootingAPI.h"      // JS::{,Mutable}Handle, JS::Rooted
23 #include "js/SourceText.h"      // JS::SourceText
24 #include "js/UniquePtr.h"       // js::UniquePtr
25 #include "vm/JSScript.h"  // js::{FunctionAsync,Generator}Kind, js::BaseScript, JSScript, js::ScriptSource, js::ScriptSourceObject
26 #include "vm/Scope.h"     // js::ScopeKind
27 
28 class JS_PUBLIC_API JSFunction;
29 class JS_PUBLIC_API JSObject;
30 
31 class JSObject;
32 
33 namespace js {
34 
35 class Scope;
36 
37 namespace frontend {
38 
39 struct BytecodeEmitter;
40 class EitherParser;
41 
42 template <typename Unit>
43 class SourceAwareCompiler;
44 template <typename Unit>
45 class ScriptCompiler;
46 template <typename Unit>
47 class ModuleCompiler;
48 template <typename Unit>
49 class StandaloneFunctionCompiler;
50 
51 extern UniquePtr<CompilationStencil> CompileGlobalScriptToStencil(
52     JSContext* cx, CompilationInput& input, JS::SourceText<char16_t>& srcBuf,
53     ScopeKind scopeKind);
54 
55 extern UniquePtr<CompilationStencil> CompileGlobalScriptToStencil(
56     JSContext* cx, CompilationInput& input,
57     JS::SourceText<mozilla::Utf8Unit>& srcBuf, ScopeKind scopeKind);
58 
59 extern UniquePtr<ExtensibleCompilationStencil>
60 CompileGlobalScriptToExtensibleStencil(JSContext* cx, CompilationInput& input,
61                                        JS::SourceText<char16_t>& srcBuf,
62                                        ScopeKind scopeKind);
63 
64 extern UniquePtr<ExtensibleCompilationStencil>
65 CompileGlobalScriptToExtensibleStencil(
66     JSContext* cx, CompilationInput& input,
67     JS::SourceText<mozilla::Utf8Unit>& srcBuf, ScopeKind scopeKind);
68 
69 // Perform some operation to reduce the time taken by instantiation.
70 //
71 // Part of InstantiateStencils can be done by calling PrepareForInstantiate.
72 // PrepareForInstantiate is GC-free operation that can be performed
73 // off-main-thread without parse global.
74 [[nodiscard]] extern bool PrepareForInstantiate(
75     JSContext* cx, CompilationInput& input, const CompilationStencil& stencil,
76     CompilationGCOutput& gcOutput);
77 
78 [[nodiscard]] extern bool InstantiateStencils(JSContext* cx,
79                                               CompilationInput& input,
80                                               const CompilationStencil& stencil,
81                                               CompilationGCOutput& gcOutput);
82 
83 extern JSScript* CompileGlobalScript(JSContext* cx,
84                                      const JS::ReadOnlyCompileOptions& options,
85                                      JS::SourceText<char16_t>& srcBuf,
86                                      ScopeKind scopeKind);
87 
88 extern JSScript* CompileGlobalScript(JSContext* cx,
89                                      const JS::ReadOnlyCompileOptions& options,
90                                      JS::SourceText<mozilla::Utf8Unit>& srcBuf,
91                                      ScopeKind scopeKind);
92 
93 extern JSScript* CompileEvalScript(JSContext* cx,
94                                    const JS::ReadOnlyCompileOptions& options,
95                                    JS::SourceText<char16_t>& srcBuf,
96                                    JS::Handle<js::Scope*> enclosingScope,
97                                    JS::Handle<JSObject*> enclosingEnv);
98 
99 extern bool DelazifyCanonicalScriptedFunction(JSContext* cx,
100                                               Handle<JSFunction*> fun);
101 
102 // Certain compile options will disable the syntax parser entirely.
CanLazilyParse(const JS::ReadOnlyCompileOptions & options)103 inline bool CanLazilyParse(const JS::ReadOnlyCompileOptions& options) {
104   return !options.discardSource && !options.sourceIsLazy &&
105          !options.forceFullParse();
106 }
107 
108 }  // namespace frontend
109 
110 }  // namespace js
111 
112 #endif  // frontend_BytecodeCompilation_h
113