/
environment.js
169 lines (143 loc) · 5.11 KB
/
environment.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
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* @fileOverview Data structures and operations for creating and manipulating
* lexical environments.
*/
define(['bes/record',
'atum/compute',
'atum/context/environment_record',
'atum/operations/object',
'atum/operations/undef'],
function(record,
compute,
environment_record,
object,
undef) {
"use strict";
/* Lexical Environment
******************************************************************************/
/**
* Abstract base lexical environment.
*
* Lexical environments store a binding record and a parent environment.
*/
var LexicalEnvironment = record.declare(null, [
'outer',
'record']);
/**
* Computation that returns the this value for the environment,
*/
LexicalEnvironment.prototype.implicitThisValue = undef.UNDEFINED;
/**
* Computation returning if the environment contains an own binding for `name`.
*/
LexicalEnvironment.prototype.hasOwnBinding = null;
/**
* Computation that returns the bound value for `name`.
*
* This does not handled lookups for bindings that do not exist in the current
* environment, use `hasOwnBinding` to ensure the binding exists.
*/
LexicalEnvironment.prototype.getBindingValue = null;
/**
* Computation to set a binding for name in this environment.
*
* Updates the existing binding or creates a new one if one does not exist.
*
* @param ref Reference to this environment
* @param {string} name Name of binding.
* @param value Value to store.
*/
LexicalEnvironment.prototype.setMutableBinding = null;
/**
* Computation to create a new immutable binding for `name` in the environment.
*
* A binding for `name` must not already exist in this environment.
*
* @param ref Reference to this environment
* @param {string} name Name of binding.
* @param value Value to store.
*/
LexicalEnvironment.prototype.putImmutableBinding = null;
/**
* Computation to delete the binding for `name` in the environment.
*
* @param ref Reference to this environment
* @param {string} name Name of binding.
*/
LexicalEnvironment.prototype.deleteBinding = null;
/* Declarative Lexical Environment
******************************************************************************/
/**
* Declarative lexical environment.
*
* @param [outer] Reference to enclosing environment.
* @param [record] Environment record for the environment.
*/
var DeclarativeLexicalEnvironment = record.extend(LexicalEnvironment,
[],
function(outer, record) {
this.outer = outer;
this.record = (record || environment_record.empty);
});
DeclarativeLexicalEnvironment.prototype.hasOwnBinding = function(name) {
return compute.bool(environment_record.hasBinding(name, this.record));
};
DeclarativeLexicalEnvironment.prototype.getBindingValue = function(name) {
return compute.just(
environment_record.getBindingValue(this.record, name));
};
DeclarativeLexicalEnvironment.prototype.setMutableBinding = function(ref, name, value) {
return ref.setValue(
this.setRecord(
environment_record.setMutableBinding(this.record, name, value)));
};
DeclarativeLexicalEnvironment.prototype.putImmutableBinding = function(ref, name, value) {
return ref.setValue(
this.setRecord(
environment_record.putImmutableBinding(this.record, name, value)));
};
DeclarativeLexicalEnvironment.prototype.deleteBinding = function(ref, name) {
return ref.setValue(
this.setRecord(
environment_record.deleteBinding(name, this.record)));
};
/* Object Lexical Environment
******************************************************************************/
/**
* Object Lexical environment.with a given set of bindings 'o' and parent 'outer'.
*
* @param [outer] Reference to enclosing environment.
* @param [object] Reference to object that stores binding.
* @param [provideThis] Does the environment provide a this value?
*/
var ObjectLexicalEnvironment = record.extend(LexicalEnvironment, [
'provideThis'],
function(outer, object, provideThis) {
this.outer = outer;
this.record = object;
this.provideThis = !!provideThis;
this.implicitThisValue = (this.provideThis ?
compute.just(this.record) :
undef.UNDEFINED);
});
ObjectLexicalEnvironment.prototype.hasOwnBinding = function(name) {
return object.hasProperty(this.record, name);
};
ObjectLexicalEnvironment.prototype.getBindingValue = function(name) {
return object.get(this.record, name);
};
ObjectLexicalEnvironment.prototype.setMutableBinding = function(ref, name, value) {
return object.set(this.record, name, value);
};
ObjectLexicalEnvironment.prototype.putImmutableBinding = ObjectLexicalEnvironment.prototype.setMutableBinding;
ObjectLexicalEnvironment.prototype.deleteBinding = function(ref, name) {
return object.deleteProperty(this.record, name);
};
/* Export
******************************************************************************/
return {
'LexicalEnvironment': LexicalEnvironment,
'DeclarativeLexicalEnvironment': DeclarativeLexicalEnvironment,
'ObjectLexicalEnvironment': ObjectLexicalEnvironment,
};
});