/
variablescontainer.ts
314 lines (300 loc) · 9.47 KB
/
variablescontainer.ts
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* GDevelop JS Platform
* Copyright 2013-2016 Florian Rival (Florian.Rival@gmail.com). All rights reserved.
* This project is released under the MIT License.
*/
namespace gdjs {
/**
* VariablesContainer stores variables, usually for a a RuntimeGame, a RuntimeScene
* or a RuntimeObject.
*/
export class VariablesContainer {
_variables: Hashtable<gdjs.Variable>;
_variablesArray: gdjs.Variable[] = [];
/**
* @param [initialVariablesData] Optional array containing representations of the base variables.
*/
constructor(initialVariablesData?: VariableData[]) {
this._variables = new Hashtable();
if (initialVariablesData !== undefined) {
this.initFrom(initialVariablesData);
}
}
/**
* Initialize variables from a container data.<br>
* If `keepOldVariables` is set to false (by default), all already existing variables will be
* erased, but the new variables will be accessible thanks to getFromIndex. <br>
* if `keepOldVariables` is set to true, already existing variables won't be erased and will be
* still accessible thanks to getFromIndex.
*
* @param data The array containing data used to initialize variables.
* @param [keepOldVariables] If set to true, already existing variables won't be erased.
*/
initFrom(data: VariableData[], keepOldVariables?: Boolean) {
if (keepOldVariables === undefined) {
keepOldVariables = false;
}
if (!keepOldVariables) {
VariablesContainer._deletedVars = VariablesContainer._deletedVars || [];
// @ts-ignore
this._variables.keys(VariablesContainer._deletedVars);
}
const that = this;
let i = 0;
for (let j = 0; j < data.length; ++j) {
const varData = data[j];
if (!varData.name) continue;
//Get the variable:
const variable = that.get(varData.name);
variable.reinitialize(varData);
if (!keepOldVariables) {
//Register the variable in the extra array to ensure a fast lookup using getFromIndex.
if (i < that._variablesArray.length) {
that._variablesArray[i] = variable;
} else {
that._variablesArray.push(variable);
}
++i;
//Remove the variable from the list of variables to be deleted.
const idx = VariablesContainer._deletedVars.indexOf(varData.name);
if (idx !== -1) {
VariablesContainer._deletedVars[idx] = undefined;
}
}
}
if (!keepOldVariables) {
this._variablesArray.length = i;
//If we do not want to keep the already existing variables,
//remove all the variables not assigned above.
//(Here, remove means flag the variable as not existing, to avoid garbage creation ).
for (
let i = 0, len = VariablesContainer._deletedVars.length;
i < len;
++i
) {
const variableName = VariablesContainer._deletedVars[i];
if (variableName !== undefined) {
this._variables.get(variableName).setUndefinedInContainer();
}
}
}
}
/**
* Add a new variable.
* This can be costly, don't use in performance sensitive paths.
*
* @param name Variable name
* @param newVariable The variable to be added
*/
add(name: string, newVariable: gdjs.Variable) {
const oldVariable = this._variables.get(name);
// Variable is either already defined, considered as undefined
// in the container or missing in the container.
// Whatever the case, replace it by the new.
this._variables.put(name, newVariable);
if (oldVariable) {
// If variable is indexed, ensure that the variable as the index
// is replaced too. This can be costly (indexOf) but we assume `add` is not
// used in performance sensitive code.
const variableIndex = this._variablesArray.indexOf(oldVariable);
if (variableIndex !== -1) {
this._variablesArray[variableIndex] = newVariable;
}
}
}
/**
* Remove a variable.
* (the variable is not really removed from the container to avoid creating garbage, but marked as undefined)
* @param name Variable to be removed
*/
remove(name: string) {
const variable = this._variables.get(name);
if (variable) {
variable.setUndefinedInContainer();
}
}
/**
* Get a variable.
* @param name The variable's name
* @return The specified variable. If not found, an empty variable is added to the container.
*/
get(name: string): gdjs.Variable {
let variable = this._variables.get(name);
if (!variable) {
//Add automatically inexisting variables.
variable = new gdjs.Variable();
this._variables.put(name, variable);
} else {
if (
//Reuse variables removed before.
variable.isUndefinedInContainer()
) {
variable.reinitialize();
}
}
return variable;
}
/**
* Get a variable using its index. If you're unsure about how to use this method, prefer to use `get`.
* The index of a variable is its index in the data passed to initFrom.
*
* This method is generally used by events generated code to increase lookup speed for variables.
*
* @param id The variable index
* @return The specified variable. If not found, an empty variable is added to the container, but it
* should not happen.
*/
getFromIndex(id: number): gdjs.Variable {
if (id >= this._variablesArray.length) {
//Add automatically non-existing variables.
let variable = new gdjs.Variable();
this._variables.put('', variable);
return variable;
} else {
let variable: gdjs.Variable = this._variablesArray[id];
//Reuse variables removed before.
if (variable.isUndefinedInContainer()) {
variable.reinitialize();
}
return variable;
}
}
/**
* Check if a variable exists in the container.
* @param name The variable's name
* @return true if the variable exists.
*/
has(name: string): boolean {
const variable = this._variables.get(name);
return variable && !variable.isUndefinedInContainer();
}
static _deletedVars: Array<string | undefined> = [];
/**
* "Bad" variable container, used by events when no other valid container can be found.
* This container has no state and always returns the bad variable ( see VariablesContainer.badVariable ).
* @static
*/
static badVariablesContainer: VariablesContainer = {
_variables: new Hashtable(),
_variablesArray: [],
has: function () {
return false;
},
getFromIndex: function () {
return VariablesContainer.badVariable;
},
get: function () {
return VariablesContainer.badVariable;
},
remove: function () {
return;
},
add: function () {
return;
},
initFrom: function () {
return;
},
};
/**
* "Bad" variable, used by events when no other valid variable can be found.
* This variable has no state and always return 0 or the empty string.
* @static
*/
static badVariable: Variable = {
_type: 'number',
_bool: false,
_children: {},
_childrenArray: [],
_str: '',
_undefinedInContainer: true,
_value: 0,
fromJSON: () => gdjs.VariablesContainer.badVariable,
toJSObject: () => 0,
fromJSObject: () => gdjs.VariablesContainer.badVariable,
reinitialize: () => {},
addChild: () => gdjs.VariablesContainer.badVariable,
castTo: () => {},
clearChildren: () => {},
clone: () => gdjs.VariablesContainer.badVariable,
getChildrenCount: () => 0,
replaceChildren: () => {},
replaceChildrenArray: () => {},
getType: function () {
return 'number';
},
isPrimitive: function () {
return true;
},
setValue: () => {},
getValue: () => 0,
getChild: () => gdjs.VariablesContainer.badVariable,
getChildAt: () => gdjs.VariablesContainer.badVariable,
hasChild: function () {
return false;
},
isStructure: function () {
return false;
},
isNumber: function () {
return true;
},
removeChild: function () {
return;
},
setNumber: function () {
return;
},
setString: function () {
return;
},
setBoolean: function () {
return;
},
getAsString: function () {
return '0';
},
getAsNumber: function () {
return 0;
},
getAsBoolean: function () {
return false;
},
getAllChildren: function () {
return {};
},
getAllChildrenArray: function () {
return [];
},
pushVariableCopy: () => {},
pushValue: () => {},
removeAtIndex: function () {
return;
},
add: function () {
return;
},
sub: function () {
return;
},
mul: function () {
return;
},
div: function () {
return;
},
concatenate: function () {
return;
},
concatenateString: function () {
return;
},
setUndefinedInContainer: function () {
return;
},
isUndefinedInContainer: function () {
return true;
},
};
}
}