|
@@ -200,4 +200,49 @@ RSpec.describe AST::FunctionCall do
|
200
|
200
|
).execute(Environment.new)
|
201
|
201
|
expect(result).to eq(5.0)
|
202
|
202
|
end
|
|
203
|
+
|
|
204
|
+ # function outer() {
|
|
205
|
+ # let x = "hello world";
|
|
206
|
+ # function () {
|
|
207
|
+ # x;
|
|
208
|
+ # };
|
|
209
|
+ # }
|
|
210
|
+ # let y = outer();
|
|
211
|
+ # y();
|
|
212
|
+ it 'keeps a reference to a closure\'s environment' do
|
|
213
|
+ env = Environment.new
|
|
214
|
+ AST::FunctionDefinition.new(
|
|
215
|
+ AST::Identifier.new('outer'),
|
|
216
|
+ [],
|
|
217
|
+ AST::Block.new(
|
|
218
|
+ [
|
|
219
|
+ AST::VariableDeclaration.new(
|
|
220
|
+ AST::Identifier.new('x'),
|
|
221
|
+ AST::String.new('hello world')
|
|
222
|
+ ),
|
|
223
|
+ AST::FunctionDefinition.new(
|
|
224
|
+ nil,
|
|
225
|
+ [],
|
|
226
|
+ AST::Block.new(
|
|
227
|
+ [
|
|
228
|
+ AST::Identifier.new('x'),
|
|
229
|
+ ]
|
|
230
|
+ )
|
|
231
|
+ )
|
|
232
|
+ ]
|
|
233
|
+ )
|
|
234
|
+ ).execute(env)
|
|
235
|
+ AST::VariableDeclaration.new(
|
|
236
|
+ AST::Identifier.new('y'),
|
|
237
|
+ AST::FunctionCall.new(
|
|
238
|
+ AST::Identifier.new('outer'),
|
|
239
|
+ []
|
|
240
|
+ )
|
|
241
|
+ ).execute(env)
|
|
242
|
+ result = AST::FunctionCall.new(
|
|
243
|
+ AST::Identifier.new('y'),
|
|
244
|
+ []
|
|
245
|
+ ).execute(env)
|
|
246
|
+ expect(result).to eq("hello world")
|
|
247
|
+ end
|
203
|
248
|
end
|