-
Notifications
You must be signed in to change notification settings - Fork 37
/
krell_repl.js
189 lines (167 loc) · 4.62 KB
/
krell_repl.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { Platform } from "react-native";
import TcpSocket from "react-native-tcp-socket";
import DeviceInfo from "react-native-device-info";
import { krellPortMap } from '../app.json';
import {
bootstrapRepl,
evaluate,
loadFile,
onSourceLoad
} from "./closure_bootstrap.js";
var CONNECTED = false;
var RECONNECT_INTERVAL = 3000;
var SERVER_IP = "$KRELL_SERVER_IP";
var SERVER_PORT = krellPortMap ? krellPortMap[DeviceInfo.getDeviceId()] || $KRELL_SERVER_PORT : $KRELL_SERVER_PORT;
const KRELL_VERBOSE = $KRELL_VERBOSE;
var reloadListeners = [];
console.log("Krell sez howdy, Device ID:", DeviceInfo.getDeviceId());
// =============================================================================
// REPL Server
const exists_ = (obj, xs) => {
if(typeof xs == "string") {
xs = xs.split(".");
}
if(xs.length >= 1) {
let key = xs[0],
hasKey = obj.hasOwnProperty(key);
if (xs.length === 1) {
return hasKey;
} else {
if(hasKey) {
return exists_(obj[key], xs.slice(1));
}
}
return false;
} else {
return false;
}
};
const notifyReloadListeners = () => {
reloadListeners.forEach(function(x) {
x();
});
};
const onKrellReload = (cb) => {
reloadListeners.push(cb);
};
const errString = (err) => {
if(typeof cljs !== "undefined") {
if(typeof cljs.repl !== "undefined") {
cljs.repl.error__GT_str(err)
} else {
return err.toString();
}
} else {
return err.toString();
}
};
const handleMessage = (socket, data) => {
var err = null,
ret = null,
msg = null;
data = data.replace(/\0/g, "");
try {
msg = JSON.parse(data);
if(msg.form) {
ret = evaluate(msg.form);
}
} catch (e) {
if(msg && msg.form) {
console.error("Could not evaluate form:", msg.form, e);
} else {
console.error("Invalid message:", msg, e);
}
err = e;
}
if (err) {
socket.write(
JSON.stringify({
type: "result",
status: "exception",
value: errString(err)
})+"\0"
);
} else {
if (ret !== undefined && ret !== null) {
socket.write(
JSON.stringify({
type: "result",
status: "success",
value: ret.toString(),
})+"\0"
);
} else {
socket.write(
JSON.stringify({
type: "result",
status: "success",
value: null,
})+"\0"
);
}
}
};
global.KRELL_RELOAD = async function(nses) {
for(let ns of nses) {
let path = goog.debugLoader_.getPathFromDeps_(ns);
await loadFile(path);
console.log("Reloaded", ns);
}
notifyReloadListeners();
}
const initSocket = (socket) => {
var buffer = "";
socket.on("data", data => {
if (data[data.length - 1] !== 0) {
buffer += data;
} else {
data = buffer + data;
buffer = "";
handleMessage(socket, data);
}
});
socket.on("error", error => {
socket.destroy();
//console.log("An error occurred with client socket:", error);
});
socket.on("close", error => {
socket.destroy();
if (CONNECTED) {
console.log("Closed connection with", socket.address());
}
CONNECTED = false;
setTimeout(tryConnection, RECONNECT_INTERVAL);
});
};
// Loop to connect from client to server
const tryConnection = () => {
if(!CONNECTED) {
var socket = TcpSocket.createConnection({
host: SERVER_IP,
port: SERVER_PORT,
tls: false,
}, function(address) {
console.log("Connected to Krell REPL Server");
CONNECTED = true;
// once cljs.core loaded, can monkey-patch Closure and setup printing
if(exists_(global, "cljs.core")) {
bootstrapRepl(socket);
} else {
onSourceLoad(goog.debugLoader_.getPathFromDeps_("cljs.core"), () => {
bootstrapRepl(socket);
});
}
});
initSocket(socket);
} else {
setTimeout(tryConnection, RECONNECT_INTERVAL);
}
};
if(!process.env["KRELL_NO_REPL"]) {
tryConnection();
}
module.exports = {
evaluate: evaluate,
onKrellReload: onKrellReload,
onSourceLoad: onSourceLoad
};