repl.it
@basicer/

MrEval

Nodejs

No description

fork
loading
Files
  • index.js
  • Packager files
  • package-lock.json
index.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
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
const WebSocket = require('ws');
const esper = require('esper.js');
const express = require('express');
const ArrayValue = require('esper.js/src/values/ArrayValue.js');
const LinkValue = require('esper.js/src/values/LinkValue.js');

const app = express();

app.get('/', (req, res) => res.end("hi"));

let server = app.listen(process.env.PORT || 55555)
let engine;

function init() {
    engine  = new esper.Engine({
        executionLimit: 10000
    });
    engine.addGlobal('reset', init);
    engine.addGlobal('global', engine.realm.globalScope.object);
}

init();

const wss = new WebSocket.Server({ server });

function serialize(what, dict) {
	console.log(what, typeof what, Object.getPrototypeOf(what))
    if ( !what ) return {'type': 'undefined'};
    if ( what instanceof LinkValue ) return {'type': 'link'};

	switch ( what.specTypeName ) {
        case "undefined":
            return {
                type: "undefined",
                n: "undefined",
            }
        case "null": 
            return {
                type: "object",
                n: null,
                value: null
            }
		case "number": 
			let n = what.toNative()
            let int =  Number.isInteger(n) && n <= Number.MAX_SAFE_INTEGER && n >= Number.MIN_SAFE_INTEGER;
			return {
				type: int  ? 'int' : 'float',
                clazz: what.jsTypeName,
				value: n
			}
		case "string": 
			return {
				type: 'string',
                clazz: what.jsTypeName,
				value: what.toNative()
			}
		default: 
			let props = {}
			for ( let k of what.observableProperties(engine.realm) ) {
				let key = k.toNative();
				let oo = what.getImmediate(key, engine.realm);
				console.log(key, oo);
				let s = oo.serial;
				props[key] = s;
				if ( !dict[s] ) {
                    dict[s] = true;
					dict[s] = serialize(oo, dict);
				}
			}
			if ( what instanceof ArrayValue ) {
				return {
					type: 'array',
                    clazz: what.jsTypeName,
					value: Object.values(props)
				}
			} else {
				return {
					type: 'object',
                    clazz: what.proto.clazz || what.proto.wellKnownName || what.jsTypeName,
					value: props
				}
			}
	}
}

//{"eval":"2+2"}
wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(data) {
  	try {
  		let packet = JSON.parse(data);
  		console.log(packet);
	    if ( packet.eval ) {
            engine.realm.print = (s) => {
                ws.send(JSON.stringify({log: s}));
            }
	    	engine.load(packet.eval);
	    	engine.run().then((value) => {
	    		let parts = {};
	    		let result = serialize(value, parts);
	    		ws.send(JSON.stringify({result, parts}));
	    	}, (error) => {
	    		ws.send(JSON.stringify({error}));
	    	});
	    }
	} catch ( error ) {
		ws.send(JSON.stringify({error}));
	}
  });
});