forked from scratchfoundation/scratch-vm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtual-machine.js
More file actions
381 lines (330 loc) · 10.8 KB
/
virtual-machine.js
File metadata and controls
381 lines (330 loc) · 10.8 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
const test = require('tap').test;
const VirtualMachine = require('../../src/virtual-machine.js');
const Sprite = require('../../src/sprites/sprite.js');
test('renameSprite throws when there is no sprite with that id', t => {
const vm = new VirtualMachine();
vm.runtime.getTargetById = () => null;
t.throws(
(() => vm.renameSprite('id', 'name')),
new Error('No target with the provided id.')
);
t.end();
});
test('renameSprite throws when used on a non-sprite target', t => {
const vm = new VirtualMachine();
const fakeTarget = {
isSprite: () => false
};
vm.runtime.getTargetById = () => (fakeTarget);
t.throws(
(() => vm.renameSprite('id', 'name')),
new Error('Cannot rename non-sprite targets.')
);
t.end();
});
test('renameSprite throws when there is no sprite for given target', t => {
const vm = new VirtualMachine();
const fakeTarget = {
sprite: null,
isSprite: () => true
};
vm.runtime.getTargetById = () => (fakeTarget);
t.throws(
(() => vm.renameSprite('id', 'name')),
new Error('No sprite associated with this target.')
);
t.end();
});
test('renameSprite sets the sprite name', t => {
const vm = new VirtualMachine();
const fakeTarget = {
sprite: {name: 'original'},
isSprite: () => true
};
vm.runtime.getTargetById = () => (fakeTarget);
vm.renameSprite('id', 'not-original');
t.equal(fakeTarget.sprite.name, 'not-original');
t.end();
});
test('renameSprite does not set sprite names to an empty string', t => {
const vm = new VirtualMachine();
const fakeTarget = {
sprite: {name: 'original'},
isSprite: () => true
};
vm.runtime.getTargetById = () => (fakeTarget);
vm.renameSprite('id', '');
t.equal(fakeTarget.sprite.name, 'original');
t.end();
});
test('renameSprite does not set sprite names to reserved names', t => {
const vm = new VirtualMachine();
const fakeTarget = {
sprite: {name: 'original'},
isSprite: () => true
};
vm.runtime.getTargetById = () => (fakeTarget);
vm.renameSprite('id', '_mouse_');
t.equal(fakeTarget.sprite.name, 'original');
t.end();
});
test('renameSprite increments from existing sprite names', t => {
const vm = new VirtualMachine();
vm.emitTargetsUpdate = () => {};
const spr1 = new Sprite(null, vm.runtime);
const target1 = spr1.createClone();
const spr2 = new Sprite(null, vm.runtime);
const target2 = spr2.createClone();
vm.runtime.targets = [target1, target2];
vm.renameSprite(target1.id, 'foo');
t.equal(vm.runtime.targets[0].sprite.name, 'foo');
vm.renameSprite(target2.id, 'foo');
t.equal(vm.runtime.targets[1].sprite.name, 'foo2');
t.end();
});
test('renameSprite does not increment when renaming to the same name', t => {
const vm = new VirtualMachine();
vm.emitTargetsUpdate = () => {};
const spr = new Sprite(null, vm.runtime);
spr.name = 'foo';
const target = spr.createClone();
vm.runtime.targets = [target];
t.equal(vm.runtime.targets[0].sprite.name, 'foo');
vm.renameSprite(target.id, 'foo');
t.equal(vm.runtime.targets[0].sprite.name, 'foo');
t.end();
});
test('deleteSprite throws when used on a non-sprite target', t => {
const vm = new VirtualMachine();
vm.runtime.targets = [{
id: 'id',
isSprite: () => false
}];
t.throws(
(() => vm.deleteSprite('id')),
new Error('Cannot delete non-sprite targets.')
);
t.end();
});
test('deleteSprite throws when there is no sprite for the given target', t => {
const vm = new VirtualMachine();
vm.runtime.targets = [{
id: 'id',
isSprite: () => true,
sprite: null
}];
t.throws(
(() => vm.deleteSprite('id')),
new Error('No sprite associated with this target.')
);
t.end();
});
test('deleteSprite throws when there is no target with given id', t => {
const vm = new VirtualMachine();
vm.runtime.targets = [{
id: 'id',
isSprite: () => true,
sprite: {
name: 'this name'
}
}];
t.throws(
(() => vm.deleteSprite('id1')),
new Error('No target with the provided id.')
);
t.end();
});
test('deleteSprite deletes a sprite when given id is associated with a known sprite', t => {
const vm = new VirtualMachine();
const spr = new Sprite(null, vm.runtime);
const currTarget = spr.createClone();
vm.runtime.targets = [currTarget];
t.equal(currTarget.sprite.clones.length, 1);
vm.deleteSprite(currTarget.id);
t.equal(currTarget.sprite.clones.length, 0);
t.end();
});
// eslint-disable-next-line max-len
test('deleteSprite sets editing target as null when given sprite is current editing target, and the only target in the runtime', t => {
const vm = new VirtualMachine();
const spr = new Sprite(null, vm.runtime);
const currTarget = spr.createClone();
vm.editingTarget = currTarget;
vm.runtime.targets = [currTarget];
vm.deleteSprite(currTarget.id);
t.equal(vm.runtime.targets.length, 0);
t.equal(vm.editingTarget, null);
t.end();
});
// eslint-disable-next-line max-len
test('deleteSprite updates editingTarget when sprite being deleted is current editing target, and there is another target in the runtime', t => {
const vm = new VirtualMachine();
const spr1 = new Sprite(null, vm.runtime);
const spr2 = new Sprite(null, vm.runtime);
const currTarget = spr1.createClone();
const otherTarget = spr2.createClone();
vm.emitWorkspaceUpdate = () => null;
vm.runtime.targets = [currTarget, otherTarget];
vm.editingTarget = currTarget;
t.equal(vm.runtime.targets.length, 2);
vm.deleteSprite(currTarget.id);
t.equal(vm.runtime.targets.length, 1);
t.equal(vm.editingTarget.id, otherTarget.id);
// now let's try them in the other order in the runtime.targets list
// can't reuse deleted targets
const currTarget2 = spr1.createClone();
const otherTarget2 = spr2.createClone();
vm.runtime.targets = [otherTarget2, currTarget2];
vm.editingTarget = currTarget2;
t.equal(vm.runtime.targets.length, 2);
vm.deleteSprite(currTarget2.id);
t.equal(vm.editingTarget.id, otherTarget2.id);
t.equal(vm.runtime.targets.length, 1);
t.end();
});
test('duplicateSprite throws when there is no target with given id', t => {
const vm = new VirtualMachine();
vm.runtime.targets = [{
id: 'id',
isSprite: () => true,
sprite: {
name: 'this name'
}
}];
t.throws(
(() => vm.duplicateSprite('id1')),
new Error('No target with the provided id')
);
t.end();
});
test('duplicateSprite throws when used on a non-sprite target', t => {
const vm = new VirtualMachine();
vm.runtime.targets = [{
id: 'id',
isSprite: () => false
}];
t.throws(
(() => vm.duplicateSprite('id')),
new Error('Cannot duplicate non-sprite targets.')
);
t.end();
});
test('duplicateSprite throws when there is no sprite for the given target', t => {
const vm = new VirtualMachine();
vm.runtime.targets = [{
id: 'id',
isSprite: () => true,
sprite: null
}];
t.throws(
(() => vm.duplicateSprite('id')),
new Error('No sprite associated with this target.')
);
t.end();
});
test('duplicateSprite duplicates a sprite when given id is associated with known sprite', t => {
const vm = new VirtualMachine();
const spr = new Sprite(null, vm.runtime);
const currTarget = spr.createClone();
vm.editingTarget = currTarget;
vm.emitWorkspaceUpdate = () => null;
vm.runtime.targets = [currTarget];
t.equal(vm.runtime.targets.length, 1);
vm.duplicateSprite(currTarget.id).then(() => {
t.equal(vm.runtime.targets.length, 2);
t.end();
});
});
test('duplicateSprite assigns duplicated sprite a fresh name', t => {
const vm = new VirtualMachine();
const spr = new Sprite(null, vm.runtime);
spr.name = 'sprite1';
const currTarget = spr.createClone();
vm.editingTarget = currTarget;
vm.emitWorkspaceUpdate = () => null;
vm.runtime.targets = [currTarget];
t.equal(vm.runtime.targets.length, 1);
vm.duplicateSprite(currTarget.id).then(() => {
t.equal(vm.runtime.targets.length, 2);
t.equal(vm.runtime.targets[0].sprite.name, 'sprite1');
t.equal(vm.runtime.targets[1].sprite.name, 'sprite2');
t.end();
});
});
test('emitWorkspaceUpdate', t => {
const vm = new VirtualMachine();
vm.runtime.targets = [
{
isStage: true,
variables: {
global: {
toXML: () => 'global'
}
},
blocks: {
toXML: () => 'blocks'
}
}, {
variables: {
unused: {
toXML: () => 'unused'
}
},
blocks: {
toXML: () => 'blocks'
}
}, {
variables: {
local: {
toXML: () => 'local'
}
},
blocks: {
toXML: () => 'blocks'
}
}
];
vm.editingTarget = vm.runtime.targets[2];
let xml = null;
vm.emit = (event, data) => (xml = data.xml);
vm.emitWorkspaceUpdate();
t.notEqual(xml.indexOf('global'), -1);
t.notEqual(xml.indexOf('local'), -1);
t.equal(xml.indexOf('unused'), -1);
t.notEqual(xml.indexOf('blocks'), -1);
t.end();
});
test('drag IO redirect', t => {
const vm = new VirtualMachine();
const sprite1Info = [];
const sprite2Info = [];
vm.runtime.targets = [
{
id: 'sprite1',
postSpriteInfo: data => sprite1Info.push(data)
}, {
id: 'sprite2',
postSpriteInfo: data => sprite2Info.push(data),
startDrag: () => {},
stopDrag: () => {}
}
];
vm.editingTarget = vm.runtime.targets[0];
// Stub emitWorkspace/TargetsUpdate, it needs data we don't care about here
vm.emitWorkspaceUpdate = () => null;
vm.emitTargetsUpdate = () => null;
// postSpriteInfo should go to the editing target by default``
vm.postSpriteInfo('sprite1 info');
t.equal(sprite1Info[0], 'sprite1 info');
// postSprite info goes to the drag target if it exists
vm.startDrag('sprite2');
vm.postSpriteInfo('sprite2 info');
t.equal(sprite2Info[0], 'sprite2 info');
// stop drag should set the editing target
vm.stopDrag('sprite2');
t.equal(vm.editingTarget.id, 'sprite2');
// Then postSpriteInfo should continue posting to the new editing target
vm.postSpriteInfo('sprite2 info 2');
t.equal(sprite2Info[1], 'sprite2 info 2');
t.end();
});