This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 425
/
FunctionExpression.js
156 lines (122 loc) · 6.11 KB
/
FunctionExpression.js
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
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/* @flow strict-local */
import type { Realm } from "../realm.js";
import type { LexicalEnvironment } from "../environment.js";
import type { Value } from "../values/index.js";
import { MakeConstructor } from "../methods/index.js";
import { Create, Environment, Functions, Properties } from "../singletons.js";
import { StringValue } from "../values/index.js";
import IsStrict from "../utils/strict.js";
import type { BabelNodeFunctionExpression } from "@babel/types";
import invariant from "../invariant.js";
export default function(
ast: BabelNodeFunctionExpression,
strictCode: boolean,
env: LexicalEnvironment,
realm: Realm
): Value {
// ECMA262 14.1.21
if (ast.id) {
if (ast.generator) {
// 1. If the function code for this GeneratorExpression is strict mode code, let strict be true. Otherwise let strict be false.
let strict = strictCode || IsStrict(ast.body);
// 2. Let scope be the running execution context's LexicalEnvironment.
let scope = env;
// 3. Let funcEnv be NewDeclarativeEnvironment(scope).
let funcEnv = Environment.NewDeclarativeEnvironment(realm, scope);
// 4. Let envRec be funcEnv's EnvironmentRecord.
let envRec = funcEnv.environmentRecord;
// 5. Let name be StringValue of BindingIdentifier.
invariant(ast.id);
let name = ast.id.name;
// 6. Perform envRec.CreateImmutableBinding(name, false).
envRec.CreateImmutableBinding(name, false);
// 7. Let closure be GeneratorFunctionCreate(Normal, FormalParameters, GeneratorBody, funcEnv, strict).
let closure = Functions.GeneratorFunctionCreate(realm, "normal", ast.params, ast.body, funcEnv, strict);
closure.loc = ast.loc;
// 8. Let prototype be ObjectCreate(%GeneratorPrototype%).
let prototype = Create.ObjectCreate(realm, realm.intrinsics.GeneratorPrototype);
prototype.originalConstructor = closure;
// 9. Perform DefinePropertyOrThrow(closure, "prototype", PropertyDescriptor{[[Value]]: prototype, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false}).
Properties.DefinePropertyOrThrow(realm, closure, "prototype", {
value: prototype,
writable: true,
enumerable: false,
configurable: false,
});
// 10. Perform SetFunctionName(closure, name).
Functions.SetFunctionName(realm, closure, new StringValue(realm, name));
// 11. Perform envRec.InitializeBinding(name, closure).
envRec.InitializeBinding(name, closure);
// 12. Return closure.
return closure;
} else {
// 1. If the function code for FunctionExpression is strict mode code, let strict be true. Otherwise let strict be false.
let strict = strictCode || IsStrict(ast.body);
// 2. Let scope be the running execution context's LexicalEnvironment.
let scope = env;
// 3. Let funcEnv be NewDeclarativeEnvironment(scope).
let funcEnv = Environment.NewDeclarativeEnvironment(realm, scope, false);
// 4. Let envRec be funcEnv's EnvironmentRecord.
let envRec = funcEnv.environmentRecord;
// 5. Let name be StringValue of BindingIdentifier.
invariant(ast.id);
let name = ast.id.name;
// 6. Perform envRec.CreateImmutableBinding(name, false).
envRec.CreateImmutableBinding(name, false, false, true);
// I don't think this gets deleted anywhere else
//if (realm.modifiedBindings) realm.modifiedBindings.delete(name);
// 7. Let closure be FunctionCreate(Normal, FormalParameters, FunctionBody, funcEnv, strict).
let closure = Functions.FunctionCreate(realm, "normal", ast.params, ast.body, funcEnv, strict);
closure.loc = ast.loc;
// 8. Perform MakeConstructor(closure).
MakeConstructor(realm, closure);
// 9. Perform SetFunctionName(closure, name).
Functions.SetFunctionName(realm, closure, new StringValue(realm, name));
// 10. Perform envRec.InitializeBinding(name, closure).
envRec.InitializeBinding(name, closure, true);
// 11. Return closure.
return closure;
}
} else {
if (ast.generator) {
// 1. If the function code for this GeneratorExpression is strict mode code, let strict be true. Otherwise let strict be false.
let strict = strictCode || IsStrict(ast.body);
// 2. Let scope be the LexicalEnvironment of the running execution context.
let scope = env;
// 3. Let closure be GeneratorFunctionCreate(Normal, FormalParameters, GeneratorBody, scope, strict).
let closure = Functions.GeneratorFunctionCreate(realm, "normal", ast.params, ast.body, scope, strict);
// 4. Let prototype be ObjectCreate(%GeneratorPrototype%).
let prototype = Create.ObjectCreate(realm, realm.intrinsics.GeneratorPrototype);
prototype.originalConstructor = closure;
// 5. Perform DefinePropertyOrThrow(closure, "prototype", PropertyDescriptor{[[Value]]: prototype, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false}).
Properties.DefinePropertyOrThrow(realm, closure, "prototype", {
value: prototype,
writable: true,
enumerable: false,
configurable: false,
});
// 6. Return closure.
return closure;
} else {
// 1. If the function code for FunctionExpression is strict mode code, let strict be true. Otherwise let strict be false.
let strict = strictCode || IsStrict(ast.body);
// 2. Let scope be the LexicalEnvironment of the running execution context.
let scope = env;
// 3. Let closure be FunctionCreate(Normal, FormalParameters, FunctionBody, scope, strict).
let closure = Functions.FunctionCreate(realm, "normal", ast.params, ast.body, scope, strict);
closure.loc = ast.loc;
// 4. Perform MakeConstructor(closure).
MakeConstructor(realm, closure);
// 5. Return closure.
return closure;
}
}
}