-
Notifications
You must be signed in to change notification settings - Fork 1
/
localscript.js
172 lines (133 loc) · 4.6 KB
/
localscript.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
function LocalScript() {
}
LocalScript.readNewLine = function (inputStream) {
var fileLine = "";
//read one byte at a time
var length = 1;
var buf = new Uint8Array(length);
var stopread = false;
while (!stopread) {
try {
var bread = FS.read(inputStream, buf, 0, length);
// console.log("read bytes: " + bread);
}
catch (error) {
//for some reason, it did not work
stopread = true;
console.log("error: " + error);
}
if (!stopread) {
//is this function from emscripten? - yes
var character = "" + UTF8ArrayToString(buf, 0);
if (character == "\n") {
stopread = true;
// console.log("new line");
}
else {
// console.log("no");
}
fileLine = fileLine.concat(character);
}
}
// console.log("returned line: " + fileLine);
return fileLine;
}
LocalScript.writeLine = function (outStream, line) {
var buf = new Uint8Array(lengthBytesUTF8(line) + 1);
var actualNumBytes = stringToUTF8Array(line, buf, 0, buf.length);
FS.write(outStream, buf, 0, actualNumBytes, undefined);
}
LocalScript.putInParanthesis = function (inputStream, outputStream, nrLines) {
for (var i = 0; i < nrLines; i++) {
var line2 = LocalScript.readNewLine(inputStream).trim();
line2 = "[" + line2 + "]";
if (i != nrLines - 1)
line2 = line2 + ",";
LocalScript.writeLine(outputStream, line2);
}
}
LocalScript.graph2js = function (inputFileName, varName, outputFileName) {
//create the streams
var inStream = undefined;
//read parameters
var nrinj = 0;
var edges = 0;
var nodes = 0;
var inj = "";
console.log("GRAPH2JS " + inputFileName + " -> " + outputFileName);
try {
inStream = FS.open(inputFileName, "r");
nrinj = parseInt(LocalScript.readNewLine(inStream));//0; //`head -1 $file | tail -1`
edges = parseInt(LocalScript.readNewLine(inStream));//0; //`head -2 $file | tail -1`
nodes = parseInt(LocalScript.readNewLine(inStream));//0; //`head -3 $file | tail -1`
inj = LocalScript.readNewLine(inStream).trim();//0; //"head -4 $file | tail -1"
}
catch (error) {
console.log(inputFileName);
console.log(error);
//return;
// reset the vars. something was wrong?
// write an empty file
nrinj = 0;
edges = 0;
nodes = 0;
inj = "";
inStream = null;
}
try {
//delete if exists
FS.unlink(outputFileName);
}
catch{ }
var outStream = FS.open(outputFileName, "w");
// var line = "var " + varName + "={\"inj\":[" + inj + "],\"edges\":[";
var line = "{\"inj\":[" + inj + "],\"edges\":[";
LocalScript.writeLine(outStream, line);
if (edges != 0) {
LocalScript.putInParanthesis(inStream, outStream, edges);
}
LocalScript.writeLine(outStream, "],\"nodes\":[");
if (nodes != 0) {
LocalScript.putInParanthesis(inStream, outStream, nodes);
}
LocalScript.writeLine(outStream, "]}");
//close the files
if (inStream != null)
FS.close(inStream);
FS.close(outStream);
}
LocalScript.localScript = function (timestamp, module) {
//reset stdout emulation
document.getElementById('output').innerText = "";
var file = timestamp + "circuit.raw.in";
var afail = 0.0;
var tfail = 0.001;
var yfail = 0.5;
var pfail = 0.001;
var schedulingType = 2;
var convert = "yes";
try {
FS.unlink(file + ".geom");
FS.unlink(file + ".geom.io.toconnect.geom");
FS.unlink(file + ".geom.io.toconnect.geomdebug1");
FS.unlink(file + ".geom.io.toconnect.geomdebug2");
}
catch{ }
if (convert == "yes") {
var main_args = [file, "xxx", afail, tfail, yfail, pfail, schedulingType];
module.callMain(main_args);
console.log("converted");
console.log("scheduled");
}
else {
console.log("no convert");
}
// generate .js files
LocalScript.graph2js(file + ".geom", "graph", file + ".js_1");
LocalScript.graph2js(file + ".geom.io.toconnect.geom", "graph2", file + ".js_2");
LocalScript.graph2js(file + ".geom.io.toconnect.geomdebug1", "graph3", file + ".js_3");
LocalScript.graph2js(file + ".geom.io.toconnect.geomdebug2", "graph4", file + ".js_4");
console.log("generated geometry");
//the names of the generated files
return [file + ".js_1", file + ".js_2", file + ".js_3", file + ".js_4"];
}