-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.ts
56 lines (55 loc) · 1.51 KB
/
helper.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
import { LppCompatibleRuntime, Thread, ThreadConstructor } from '../typing'
/**
* Bind fn to thread. Fn will be called when the thread exits.
* @param thread Thread object.
* @param fn Dedicated function.
*/
export function bindThread(thread: Thread, fn: () => void) {
// Call callback (if exists) when the thread is finished.
let status = thread.status
let alreadyCalled = false
const threadConstructor = thread.constructor as ThreadConstructor
Reflect.defineProperty(thread, 'status', {
get: () => {
return status
},
set: newStatus => {
status = newStatus
if (status === threadConstructor.STATUS_DONE) {
if (!alreadyCalled) {
alreadyCalled = true
fn()
}
}
}
})
}
/**
* Method for compiler to fix globalState.
* @param runtime Runtime.
* @param util Scratch util.
* @param thread Thread instance.
*/
export function stepThread(
runtime: LppCompatibleRuntime,
util: VM.BlockUtility,
thread: Thread
) {
const callerThread = util.thread as Thread
runtime.sequencer.stepThread(thread)
// if (util && callerThread) util.thread = callerThread // restore interpreter context
if (
thread.isCompiled &&
callerThread &&
callerThread.isCompiled &&
callerThread.generator
) {
const orig = callerThread.generator
callerThread.generator = {
next: () => {}
}
runtime.sequencer.stepThread(callerThread) // restore compiler context (globalState)
callerThread.generator = orig
}
util.thread = callerThread
}