summaryrefslogtreecommitdiffstats
path: root/theme/javascript/utils/execute.js
blob: 76033f6bf5aa296babe9ee028d8c284c4d7071e3 (plain)
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
define([
    "execute/javascript"
], function(javascript) {
    var LANGUAGES = {
        "javascript": javascript
    };


    var evalJS = function(lang, code, callback) {
        var ready = false;
        var finished = false;

        var finish = function() {
            if(finished) {
                return console.error('Already finished');
            }
            finished = true;
            return callback.apply(null, arguments);
        };

        var repl;

        // Handles all our events
        var eventHandler = function(data, eventType) {
            console.log([eventType, data]);
            switch(eventType) {
                case 'progress':
                    // Update UI loading bar
                    break;
                case 'timeout':
                    finish(new Error(data));
                    break;
                case 'result':
                    finish(null, {
                        value: data,
                        type: 'result'
                    });
                    break;
                case 'error':
                    if(ready) {
                        return finish(null, {
                            value: data,
                            type: 'error'
                        });
                    }
                    return finish(new Error(data));
                    break
                case 'ready':
                    // We're good to get results and stuff back now
                    ready = true;
                    // Eval our code now that the runtime is ready
                    repl.eval(code);
                    break;
                default:
                    console.log('Unhandled event =', eventType, 'data =', data);
            }
        };

        repl = new lang.REPL({
            input: eventHandler,
            output: eventHandler,
            result: eventHandler,
            error: eventHandler,
            progress: eventHandler,
            timeout: {
                time: 30000,
                callback: eventHandler
            }
        });

        repl.loadLanguage(lang.id, eventHandler);
    };

    var execute = function(lang, solution, validation, callback) {
        // Check language is supported
        if (!LANGUAGES[lang]) return callback(new Error("Language '"+lang+"' not available for execution"));

        // Validate with validation code
        var code = [solution, LANGUAGES[lang].assertCode, validation].join(";\n");
        evalJS(LANGUAGES[lang], code, function(err, res) {
            if(err) return callback(err);

            if (res.type == "error") callback(new Error(res.value));
            else callback(null, res.value);
        });
    };

    return execute;
});