1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use std::rc::Rc;
use crate::{
builtins::function::ThisMode,
bytecompiler::ByteCompiler,
environments::CompileTimeEnvironment,
js_string,
vm::{CodeBlock, CodeBlockFlags, Opcode},
JsString,
};
use boa_ast::function::{FormalParameterList, FunctionBody};
use boa_gc::Gc;
use boa_interner::Interner;
/// `FunctionCompiler` is used to compile AST functions to bytecode.
#[derive(Debug, Clone)]
#[allow(clippy::struct_excessive_bools)]
pub(crate) struct FunctionCompiler {
name: JsString,
generator: bool,
r#async: bool,
strict: bool,
arrow: bool,
method: bool,
in_with: bool,
binding_identifier: Option<JsString>,
}
impl FunctionCompiler {
/// Create a new `FunctionCompiler`.
pub(crate) fn new() -> Self {
Self {
name: js_string!(),
generator: false,
r#async: false,
strict: false,
arrow: false,
method: false,
in_with: false,
binding_identifier: None,
}
}
/// Set the name of the function.
pub(crate) fn name<N>(mut self, name: N) -> Self
where
N: Into<Option<JsString>>,
{
let name = name.into();
if let Some(name) = name {
self.name = name;
}
self
}
/// Indicate if the function is an arrow function.
pub(crate) const fn arrow(mut self, arrow: bool) -> Self {
self.arrow = arrow;
self
}
/// Indicate if the function is a method function.
pub(crate) const fn method(mut self, method: bool) -> Self {
self.method = method;
self
}
/// Indicate if the function is a generator function.
pub(crate) const fn generator(mut self, generator: bool) -> Self {
self.generator = generator;
self
}
/// Indicate if the function is an async function.
pub(crate) const fn r#async(mut self, r#async: bool) -> Self {
self.r#async = r#async;
self
}
/// Indicate if the function is in a strict context.
pub(crate) const fn strict(mut self, strict: bool) -> Self {
self.strict = strict;
self
}
/// Indicate if the function has a binding identifier.
pub(crate) fn binding_identifier(mut self, binding_identifier: Option<JsString>) -> Self {
self.binding_identifier = binding_identifier;
self
}
/// Indicate if the function is in a `with` statement.
pub(crate) const fn in_with(mut self, in_with: bool) -> Self {
self.in_with = in_with;
self
}
/// Compile a function statement list and it's parameters into bytecode.
pub(crate) fn compile(
mut self,
parameters: &FormalParameterList,
body: &FunctionBody,
variable_environment: Rc<CompileTimeEnvironment>,
lexical_environment: Rc<CompileTimeEnvironment>,
interner: &mut Interner,
) -> Gc<CodeBlock> {
self.strict = self.strict || body.strict();
let length = parameters.length();
let mut compiler = ByteCompiler::new(
self.name,
self.strict,
false,
variable_environment,
lexical_environment,
interner,
self.in_with,
);
compiler.length = length;
compiler
.code_block_flags
.set(CodeBlockFlags::IS_ASYNC, self.r#async);
compiler
.code_block_flags
.set(CodeBlockFlags::IS_GENERATOR, self.generator);
compiler.code_block_flags.set(
CodeBlockFlags::HAS_PROTOTYPE_PROPERTY,
!self.arrow && !self.method && !self.r#async && !self.generator,
);
if self.arrow {
compiler.this_mode = ThisMode::Lexical;
}
if let Some(binding_identifier) = self.binding_identifier {
compiler.code_block_flags |= CodeBlockFlags::HAS_BINDING_IDENTIFIER;
let _ = compiler.push_compile_environment(false);
compiler
.lexical_environment
.create_immutable_binding(binding_identifier, self.strict);
}
// Function environment
let _ = compiler.push_compile_environment(true);
// Taken from:
// - 15.9.3 Runtime Semantics: EvaluateAsyncConciseBody: <https://tc39.es/ecma262/#sec-runtime-semantics-evaluateasyncconcisebody>
// - 15.8.4 Runtime Semantics: EvaluateAsyncFunctionBody: <https://tc39.es/ecma262/#sec-runtime-semantics-evaluateasyncfunctionbody>
//
// Note: In `EvaluateAsyncGeneratorBody` unlike the async non-generator functions we don't handle exceptions thrown by
// `FunctionDeclarationInstantiation` (so they are propagated).
//
// See: 15.6.2 Runtime Semantics: EvaluateAsyncGeneratorBody: https://tc39.es/ecma262/#sec-runtime-semantics-evaluateasyncgeneratorbody
if compiler.is_async() && !compiler.is_generator() {
// 1. Let promiseCapability be ! NewPromiseCapability(%Promise%).
//
// Note: If the promise capability is already set, then we do nothing.
// This is a deviation from the spec, but it allows to set the promise capability by
// ExecuteAsyncModule ( module ): <https://tc39.es/ecma262/#sec-execute-async-module>
compiler.emit_opcode(Opcode::CreatePromiseCapability);
// 2. Let declResult be Completion(FunctionDeclarationInstantiation(functionObject, argumentsList)).
//
// Note: We push an exception handler so we catch exceptions that are thrown by the
// `FunctionDeclarationInstantiation` abstract function.
//
// Patched in `ByteCompiler::finish()`.
compiler.async_handler = Some(compiler.push_handler());
}
compiler.function_declaration_instantiation(
body,
parameters,
self.arrow,
self.strict,
self.generator,
);
// Taken from:
// - 27.6.3.2 AsyncGeneratorStart ( generator, generatorBody ): <https://tc39.es/ecma262/#sec-asyncgeneratorstart>
//
// Note: We do handle exceptions thrown by generator body in `AsyncGeneratorStart`.
if compiler.is_generator() {
assert!(compiler.async_handler.is_none());
if compiler.is_async() {
// Patched in `ByteCompiler::finish()`.
compiler.async_handler = Some(compiler.push_handler());
}
}
compiler.compile_statement_list(body.statements(), false, false);
compiler.params = parameters.clone();
Gc::new(compiler.finish())
}
}