forked from hdoan741/jstutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_backend.js
67 lines (59 loc) · 1.86 KB
/
static_backend.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
var SourceExecution = require('./SourceExecution.js')
, SourceManagement = require('./SourceManagement.js')
, SourceInspection = require('./SourceInspection.js');
var StaticInspector = function() {
var pendingCode = null;
var isExecuting = false;
var currentCode;
var inspector = {};
inspector.inspect = function(source_code, callback) {
var user_program;
var inspector;
var attachDebugger = function(filename, proc, port) {
console.log('debug ready');
inspector = new SourceInspection(filename, proc, port);
inspector.on('done', function(traces) {
callback({
'code': currentCode,
'trace': traces
});
isExecuting = false;
/*
if (pendingCode) {
var pc = pendingCode;
pendingCode = null;
session.inspect(pc);
}
*/
});
inspector.on('infinite_loop', function(loginfo, refValues) {
// socket.emit('loginfo', loginfo);
// socket.emit('error', 'Too many loops. Possibly infinite loop');
user_program.kill('SIGKILL');
});
inspector.on('error', function() {
console.log('[static_server] Error in the debugger. Terminate user\' program');
user_program.kill('SIGKILL');
});
}
var executeSource = function(err, filename) {
if (err) {
// TODO: notify that some errors happened
}
user_program = SourceExecution.execute(filename);
user_program.on('debug_ready', attachDebugger);
user_program.on('error', function(data) {
// console.log('[UserProgram Error]', data);
});
}
if (!isExecuting) {
isExecuting = true;
currentCode = source_code;
SourceManagement.save(source_code, executeSource);
} else {
pendingCode = source_code;
}
};
return inspector;
};
module.exports = StaticInspector();