-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThread.js
More file actions
111 lines (97 loc) · 3.66 KB
/
Copy pathThread.js
File metadata and controls
111 lines (97 loc) · 3.66 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
class Thread {
constructor(target, triggeringBlock) {
this.target = target;
this.interpreter = null;
this.callback = null;
if (triggeringBlock) {
this.triggeringEvent = triggeringBlock.type;
this.triggeringBlockID = triggeringBlock.id;
}
this.id = Thread.uuidv4();
}
static uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
blockCallback(returnValue) {
if (!this.callback) {
console.warn("Unset block callback for thread: " + this.id);
return;
}
if (this.target.isEditing) {
highlightBlock(null);
}
console.log("Callback with return: ", returnValue);
this.callback(returnValue);
this.callback = null;
this.step();
}
step() {
console.log(`Stepping thread: ${this.id}`);
var hasMore = this.interpreter.run();
if (!hasMore) this.die();
}
die() {
this.target.removeThread(this.id);
}
run(code) {
let thread = this;
function initApi(interpreter, globalObject) {
interpreter.setProperty(globalObject, BlocklyConstructor.GET_VAR,
interpreter.createNativeFunction(function(name) {
return thread.target.getVar(name);
}
));
interpreter.setProperty(globalObject, BlocklyConstructor.SET_VAR,
// TODO: This won't work with non-primitives,, e.g. lists...
interpreter.createNativeFunction(function(name, value) {
thread.target.setVar(name, value);
}
));
interpreter.setProperty(globalObject, BlocklyConstructor.CALL_BLOCK,
interpreter.createAsyncFunction(function(
// Silly hack to allow any number of arguments to be passed to the same function
name, id, nArgs,
a2, a3, a4, a5, a6, a7, a8, a9, a10,
a11, a12, a13, a14, a15,
callback
) {
if (thread.target.isEditing) {
highlightBlock(id);
}
// console.log('args', arguments);
const nPreArgs = 3;
var args = [...arguments].slice(nPreArgs, nArgs + nPreArgs);
console.log('Calling: ', name, args);
Thread.callBlock({
methodName: name,
args: args,
targetID: thread.target.id,
threadID: thread.id,
});
thread.callback = callback;
}
)
);
// Add an API function for highlighting blocks.
var wrapper = function(id) {
id = String(id || '');
highlightBlock(id);
};
interpreter.setProperty(globalObject, BlocklyConstructor.HIGHLIGHT,
interpreter.createNativeFunction(wrapper));
}
this.interpreter = new Interpreter(code, initApi);
// TODO: Should step so its interruptable
this.step();
}
static callBlock(data) {
console.log('Calling', data);
window.socket.send(JSON.stringify({
'type': 'call',
'data': data,
}));
}
}