-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathpolyfills.js
326 lines (272 loc) · 10.3 KB
/
polyfills.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
if (typeof Polyglot !== "undefined") {
const Maybe = tortoise_require('brazier/maybe');
const BAIS = Java.type('java.io.ByteArrayInputStream');
const BAOS = Java.type('java.io.ByteArrayOutputStream');
const Base64 = Java.type('java.util.Base64');
const ByteArr = Java.type("byte[]");
const Color = Java.type('java.awt.Color');
const Compiler = Java.type('org.nlogo.tortoise.compiler.Polyfills');
const Files = Java.type('java.nio.file.Files');
const ImageIO = Java.type('javax.imageio.ImageIO');
const Paths = Java.type('java.nio.file.Paths');
const Scanner = Java.type('java.util.Scanner');
const URL = Java.type('java.net.URL');
const File = Java.type('java.io.File');
const BufferedImage = Java.type('java.awt.image.BufferedImage');
const base64ToImageData =
function(base64) {
const splits = base64.split(',');
if (splits.length != 2) {
throw new Error(`splits not 2: ${splits.length}, ${base64.substring(0, 40)}...`)
}
const trimmed = splits[1];
const bytes = Base64.getDecoder().decode(trimmed);
const bais = new BAIS(bytes);
const image = ImageIO.read(bais);
if (image === null) {
throw new Error(`image was null?: ${bytes.length}, ${base64.substring(0, 40)}...`)
}
bais.close();
const output = [];
for (let y = 0; y < image.getHeight(); y++) {
for (let x = 0; x < image.getWidth(); x++) {
const pixel = new Color(image.getRGB(x, y), true);
output.push(pixel.getRed());
output.push(pixel.getGreen());
output.push(pixel.getBlue());
output.push(pixel.getAlpha());
}
}
return { data: output, height: image.getHeight(), width: image.getWidth() };
}
const imageDataToBufferedImage =
function(imageData) {
const image = new BufferedImage(imageData.width, imageData.height, BufferedImage.TYPE_3BYTE_BGR)
for (var i = 0; i < imageData.data.length; i += 4) {
const color = ((imageData.data[i] & 0xFF) << 16) + ((imageData.data[i + 1] & 0xFF) << 8) + ((imageData.data[i + 2] & 0xFF))
const pixelIndex = i / 4
image.setRGB((pixelIndex % imageData.width), Math.floor(pixelIndex / imageData.width), color)
}
return image
}
const imageDataToBase64 =
function(imageData) {
const output = new BAOS();
const image = imageDataToBufferedImage(imageData)
ImageIO.write(image, "png", output);
const base64 = Base64.getEncoder().encodeToString(output.toByteArray());
return `data:image/png;base64,${base64}`;
}
const exportFile =
function(str) {
return function(filename) {
const path = Paths.get(filename);
const parent = path.getParent();
if (parent !== null) {
Files.createDirectories(parent);
}
Files.write(path, Compiler.getBytes(str));
};
};
const slurpByType =
function(mimeStr, slurpText, slurpImage) {
if (mimeStr == "content/unknown" || mimeStr.startsWith('text/') || mimeStr.startsWith('application/')) {
return slurpText();
} else if (mimeStr.startsWith('image/')) {
return slurpImage();
} else {
throw new Error("Unslurpable content type: " + mimeStr);
}
};
const slurpFileDialogAsync =
function(callback) {
throw new Error("You can't get user input headlessly.");
};
const slurpFilepathAsync =
function(filename) {
return function(callback) {
const path = Paths.get(filename);
const mimeStr = path.toUri().toURL().openConnection().getContentType();
const slurpImage = function() { return slurpImageFromFile(filename, mimeStr); };
const slurpText = function() { return slurpTextFromFile(filename); };
const slurped = slurpByType(mimeStr, slurpText, slurpImage);
callback(slurped);
};
};
const slurpImageFromFile =
function(filename, mimeStr) {
const path = Paths.get(filename);
const bytes = Files.readAllBytes(path);
const byteStr = Base64.getEncoder().encodeToString(bytes);
return "data:" + mimeStr + ";base64," + byteStr;
};
const slurpImageFromURL =
function(url, mimeStr) {
const baos = new BAOS();
const stream = url.openStream();
const buffer = new ByteArr(1024);
var n = 0;
while ((n = stream.read(buffer)) > 0) {
baos.write(buffer, 0, n);
}
stream.close();
const byteStr = Base64.getEncoder().encodeToString(baos.toByteArray());
return "data:" + mimeStr + ";base64," + byteStr;
};
const slurpTextFromFile =
function(filename) {
const out = [];
const path = Paths.get(filename);
Files.readAllLines(path).forEach(function(line) { out.push(line); });
return out.join("\n");
};
const slurpTextFromURL =
function(url) {
return new Scanner(url.openStream()).useDelimiter("\\A").next();
};
const slurpURLSynchronously =
function(url) {
const jurl = new URL(url);
const mimeStr = jurl.openConnection().getContentType();
const slurpImage = function() { return slurpImageFromURL(jurl, mimeStr); };
const slurpText = function() { return slurpTextFromURL(jurl); };
const slurped = slurpByType(mimeStr, slurpText, slurpImage);
return slurped;
};
const slurpURLAsync =
function(url) {
return function(callback) {
const jurl = new URL(url);
const mimeStr = jurl.openConnection().getContentType();
const slurpImage = function() { return slurpImageFromURL(jurl, mimeStr); };
const slurpText = function() { return slurpTextFromURL(jurl); };
const slurped = slurpByType(mimeStr, slurpText, slurpImage);
callback(slurped);
};
};
const asyncDialog =
{ getChoice: function(message, choices) { return function() { return Maybe.None; }; }
, getText: function(message) { return function() { return Maybe.None; }; }
, getYesOrNo: function(message) { return function() { return Maybe.None; }; }
, showMessage: function(message) { return function() { return Maybe.None; }; }
}
const dialog =
{ confirm: function(str) { return true; }
, input: function(str) { return 'dummy implementation'; }
, notify: function(str) { console.log(str); }
, yesOrNo: function(str) { return true; }
}
let outputBuffer = ""
const importExport =
{ exportFile: exportFile
, getNlogo: function() { return ""; }
, getOutput: function() { return outputBuffer; }
, getViewBase64: function() { return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVQYV2NgYAAAAAMAAWgmWQ0AAAAASUVORK5CYII="; }
, importFile: slurpFilepathAsync
};
const inspection =
{ clearDead: function() {}
, inspect: function(agent) {}
, stopInspecting: function(agent) {}
};
const io =
{ importFile: slurpFilepathAsync
, slurpFileDialogAsync: slurpFileDialogAsync
, slurpURL: slurpURLSynchronously
, slurpURLAsync: slurpURLAsync
};
const output =
{ clear: function() { outputBuffer = ""; }
, write: function(str) { outputBuffer += str; }
};
const world = { resizeWorld: function(agent) {} };
// This was created to help during development of the Bitmap extension for NetLogo Web. I'm going to leave it here in
// case it is useful for future debugging of that or anything else. -Jeremy B November 2022
global.writeImage = function(name, imageData) {
const image = imageDataToBufferedImage(imageData)
ImageIO.write(image, "png", new File(name));
}
global.atob = function(base64) {
let bytes = Base64.getDecoder().decode(base64);
return String.fromCharCode(...bytes);
}
global.btoa = function(str) {
return Base64.getEncoder().encodeToString(Compiler.getBytes(str));
}
global.crypto = {
// only works for int32 values, just for testing, please don't ever let this into the wild. -Jeremy B September
// 2022
getRandomValues: (arr) => {
arr.forEach( (_, i) => {
const thirtyTwoLimit = 2 ** 31
arr[i] = Math.floor(thirtyTwoLimit * 2 * Math.random() - thirtyTwoLimit)
})
return arr
}
}
global.TextDecoder = class {
decode(bytes) {
// http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt
/* utf.js - UTF-8 <=> UTF-16 convertion
*
* Copyright (C) 1999 Masanao Izumo <[email protected]>
* Version: 1.0
* This library is free. You can redistribute it and/or modify it.
*/
let out = "";
let i = 0;
while (i < bytes.length) {
const char1 = bytes[i++];
switch (char1 >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
// 0xxxxxxx
out += String.fromCharCode(char1);
break;
}
case 12: case 13: {
// 110x xxxx 10xx xxxx
const char2 = bytes[i++];
const code = ((char1 & 0x1F) << 6) |
((char2 & 0x3F) << 0)
out += String.fromCharCode(code);
break;
}
case 14: {
// 1110 xxxx 10xx xxxx 10xx xxxx
const char2 = bytes[i++];
const char3 = bytes[i++];
const code = ((char1 & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0)
out += String.fromCharCode(code);
break;
}
}
}
return out;
}
}
global.TextEncoder = class {
encode(str) {
// Java does signed bytes, but we want unsigned bytes. --Jason B. (3/16/22)
const jBytes = Compiler.getBytes(str);
const out = [];
for (let i = 0; i < jBytes.length; i++) {
out[i] = jBytes[i] & 0xFF;
}
return out;
}
}
global.modelConfig =
{ asyncDialog: asyncDialog
, base64ToImageData: base64ToImageData
, imageDataToBase64: imageDataToBase64
, dialog: dialog
, importExport: importExport
, importImage: () => {}
, inspection: inspection
, io: io
, output: output
, world: world
}
}