Skip to content

Commit a8da044

Browse files
committed
New TLSF, see #15
1 parent 3924aa9 commit a8da044

File tree

10 files changed

+589
-789
lines changed

10 files changed

+589
-789
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ A few early examples to get an idea:
2020
A PSON decoder implemented in AssemblyScript.
2121

2222
* **[TLSF memory allocator](./examples/tlsf)**<br />
23-
A port of TLSF to AssemblyScript.
23+
An implementation of the TLSF memory allocator in AssemblyScript.
2424

2525
* **[μgc garbage collector](./examples/ugc)**<br />
2626
A port of μgc to AssemblyScript.

bin/asc

100644100755
File mode changed.

bin/asc.js

Lines changed: 99 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
var fs = require("fs");
2-
var path = require("path");
3-
var minimist = require("minimist");
4-
var glob = require("glob");
1+
#!/usr/bin/env node
2+
const fs = require("fs");
3+
const path = require("path");
4+
const minimist = require("minimist");
5+
const glob = require("glob");
6+
const { SourceMapConsumer, SourceMapGenerator } = require("source-map");
57

68
var assemblyscript;
79
var isDev = true;
@@ -15,8 +17,8 @@ try {
1517
assemblyscript = require("../src");
1618
}
1719

18-
var conf = require("./asc.json");
19-
var opts = {};
20+
const conf = require("./asc.json");
21+
const opts = {};
2022

2123
Object.keys(conf).forEach(key => {
2224
var opt = conf[key];
@@ -30,9 +32,10 @@ Object.keys(conf).forEach(key => {
3032
(opts.boolean || (opts.boolean = [])).push(key);
3133
});
3234

33-
var args = minimist(process.argv.slice(2), opts);
35+
const args = minimist(process.argv.slice(2), opts);
36+
const indent = 24;
37+
3438
var version = require("../package.json").version;
35-
var indent = 24;
3639
if (isDev) version += "-dev";
3740

3841
if (args.version) {
@@ -43,7 +46,7 @@ if (args.version) {
4346
}
4447

4548
if (args.help || args._.length < 1) {
46-
var options = [];
49+
const options = [];
4750
Object.keys(conf).forEach(name => {
4851
var option = conf[name];
4952
var text = " ";
@@ -79,6 +82,7 @@ var parser = null;
7982
var readTime = 0;
8083
var readCount = 0;
8184
var writeTime = 0;
85+
var writeCount = 0;
8286
var parseTime = 0;
8387
var compileTime = 0;
8488
var validateTime = 0;
@@ -122,7 +126,7 @@ libDirs.forEach(libDir => {
122126
var nextText = fs.readFileSync(path.join(libDir, file), { encoding: "utf8" });
123127
++readCount;
124128
var time = measure(() => {
125-
parser = assemblyscript.parseFile(nextText, "std:" + file, parser, false);
129+
parser = assemblyscript.parseFile(nextText, ".std/" + file, parser, false);
126130
});
127131
parseTime += time;
128132
notIoTime += time;
@@ -139,12 +143,14 @@ args._.forEach(filename => {
139143
try {
140144
readTime += measure(() => {
141145
entryText = fs.readFileSync(entryPath + ".ts", { encoding: "utf8" });
146+
entryPath += ".ts";
142147
});
143148
++readCount;
144149
} catch (e) {
145150
try {
146151
readTime += measure(() => {
147152
entryText = fs.readFileSync(entryPath + "/index.ts", { encoding: "utf8" });
153+
entryPath += "/index.ts";
148154
});
149155
++readCount;
150156
entryPath = entryPath + "/index";
@@ -154,21 +160,22 @@ args._.forEach(filename => {
154160
}
155161
}
156162

157-
var nextPath;
163+
var nextFile;
158164
var nextText;
159165

160166
// Load entry text
161167
parseTime += measure(() => {
162168
parser = assemblyscript.parseFile(entryText, entryPath, parser, true);
163169
});
164170

165-
while ((nextPath = parser.nextFile()) != null) {
171+
while ((nextFile = parser.nextFile()) != null) {
166172
var found = false;
167-
if (nextPath.startsWith("std:")) {
173+
if (nextFile.startsWith(".std/")) {
168174
for (var i = 0; i < libDirs.length; ++i) {
169175
readTime += measure(() => {
170176
try {
171-
nextText = fs.readFileSync(libDirs[i] + "/" + nextPath.substring(4) + ".ts", { encoding: "utf8" });
177+
nextText = fs.readFileSync(path.join(libDirs[i], nextFile.substring(4) + ".ts"), { encoding: "utf8" });
178+
nextFile = nextFile + ".ts";
172179
found = true;
173180
} catch (e) {}
174181
});
@@ -179,27 +186,29 @@ args._.forEach(filename => {
179186
} else {
180187
readTime += measure(() => {
181188
try {
182-
nextText = fs.readFileSync(nextPath + "/index.ts", { encoding: "utf8" });
189+
nextText = fs.readFileSync(nextFile + ".ts", { encoding: "utf8" });
190+
nextFile = nextFile + ".ts";
183191
found = true;
184192
} catch (e) {}
185193
});
186194
++readCount;
187195
if (!found) {
188196
readTime += measure(() => {
189197
try {
190-
nextText = fs.readFileSync(nextPath + ".ts", { encoding: "utf8" });
198+
nextText = fs.readFileSync(nextFile + "/index.ts", { encoding: "utf8" });
199+
nextFile = nextFile + "/index.ts";
191200
found = true;
192201
} catch (e) {}
193202
});
194203
++readCount;
195204
}
196205
}
197206
if (!found) {
198-
console.error("Imported file '" + nextPath + ".ts' not found.");
207+
console.error("Imported file '" + nextFile + ".ts' not found.");
199208
process.exit(1);
200209
}
201210
parseTime += measure(() => {
202-
assemblyscript.parseFile(nextText, nextPath, parser);
211+
assemblyscript.parseFile(nextText, nextFile, parser);
203212
});
204213
}
205214
checkDiagnostics(parser);
@@ -210,6 +219,7 @@ assemblyscript.setTarget(options, 0);
210219
assemblyscript.setNoTreeShaking(options, args.noTreeShaking);
211220
assemblyscript.setNoAssert(options, args.noAssert);
212221
assemblyscript.setNoMemory(options, args.noMemory);
222+
assemblyscript.setSourceMap(options, args.sourceMap != null);
213223

214224
var module;
215225
compileTime += measure(() => {
@@ -221,6 +231,7 @@ if (args.validate)
221231
validateTime += measure(() => {
222232
if (!module.validate()) {
223233
module.dispose();
234+
console.error("Validation failed");
224235
process.exit(1);
225236
}
226237
});
@@ -234,7 +245,7 @@ else if (args.trapMode === "js")
234245
module.runPasses([ "trap-mode-js" ]);
235246
});
236247
else if (args.trapMode !== "allow") {
237-
console.log("Unsupported trap mode: " + args.trapMode);
248+
console.error("Unsupported trap mode: " + args.trapMode);
238249
process.exit(1);
239250
}
240251

@@ -298,6 +309,40 @@ if (runPasses.length)
298309
module.runPasses(runPasses.map(pass => pass.trim()));
299310
});
300311

312+
function processSourceMap(sourceMap, sourceMapURL) {
313+
var json = JSON.parse(sourceMap);
314+
return SourceMapConsumer.with(sourceMap, sourceMapURL, consumer => {
315+
var generator = SourceMapGenerator.fromSourceMap(consumer);
316+
json.sources.forEach(name => {
317+
var text, found = false;
318+
if (name.startsWith(".std/")) {
319+
for (var i = 0, k = libDirs.length; i < k; ++i) {
320+
readTime += measure(() => {
321+
try {
322+
text = fs.readFileSync(path.join(libDirs[i], name.substring(4)), { encoding: "utf8" });
323+
found = true;
324+
} catch (e) {}
325+
});
326+
++readCount;
327+
}
328+
} else {
329+
readTime += measure(() => {
330+
try {
331+
text = fs.readFileSync(name, { encoding: "utf8" });
332+
found = true;
333+
} catch (e) {}
334+
});
335+
++readCount;
336+
}
337+
if (found)
338+
generator.setSourceContent(name, text);
339+
else
340+
console.error("No source content found for file '" + name + "'.");
341+
});
342+
return generator.toString();
343+
});
344+
}
345+
301346
if (!args.noEmit) {
302347
var hasOutput = false;
303348

@@ -310,47 +355,69 @@ if (!args.noEmit) {
310355
args.binaryFile = args.outFile;
311356
}
312357
if (args.binaryFile != null && args.binaryFile.length) {
358+
var sourceMapURL = args.sourceMap != null
359+
? args.sourceMap.length
360+
? args.sourceMap
361+
: path.basename(args.binaryFile) + ".map"
362+
: null;
363+
var binary;
313364
writeTime += measure(() => {
314-
fs.writeFileSync(args.binaryFile, module.toBinary());
365+
binary = module.toBinary(sourceMapURL); // FIXME: 'not a valid URL' in FF
366+
fs.writeFileSync(args.binaryFile, binary.output);
315367
});
368+
++writeCount;
369+
if (binary.sourceMap != null)
370+
processSourceMap(binary.sourceMap).then(sourceMap => {
371+
writeTime += measure(() => {
372+
fs.writeFileSync(path.join(path.dirname(args.binaryFile), path.basename(sourceMapURL)), sourceMap, { encoding: "utf8" });
373+
}, err => {
374+
throw err;
375+
});
376+
++writeCount;
377+
});
316378
hasOutput = true;
317379
}
318380
if (args.textFile != null && args.textFile.length) {
319381
writeTime += measure(() => {
320382
fs.writeFileSync(args.textFile, module.toText(), { encoding: "utf8" });
321383
});
384+
++writeCount;
322385
hasOutput = true;
323386
}
324387
if (args.asmjsFile != null && args.asmjsFile.length) {
325388
writeTime += measure(() => {
326389
fs.writeFileSync(args.asmjsFile, module.toAsmjs(), { encoding: "utf8" });
327390
});
391+
++writeCount;
328392
hasOutput = true;
329393
}
330394
if (!hasOutput) {
331-
if (args.binaryFile === "")
395+
if (args.binaryFile === "") {
332396
writeTime += measure(() => {
333397
process.stdout.write(Buffer.from(module.toBinary()));
334398
});
335-
else if (args.asmjsFile === "")
399+
++writeCount;
400+
} else if (args.asmjsFile === "") {
336401
writeTime += measure(() => {
337402
module.printAsmjs();
338403
});
339-
else
404+
++writeCount;
405+
} else {
340406
writeTime += measure(() => {
341407
module.print();
342408
});
409+
++writeCount;
410+
}
343411
}
344412
}
345413

346414
module.dispose();
347415

348-
if (args.measure)
349-
console.error([
350-
"I/O Read : " + (readTime ? (readTime / 1e6).toFixed(3) + " ms (" + readCount + " files)" : "N/A"),
351-
"I/O Write : " + (writeTime ? (writeTime / 1e6).toFixed(3) + " ms" : "N/A"),
352-
"Parse : " + (parseTime ? (parseTime / 1e6).toFixed(3) + " ms" : "N/A"),
353-
"Compile : " + (compileTime ? (compileTime / 1e6).toFixed(3) + " ms" : "N/A"),
354-
"Validate : " + (validateTime ? (validateTime / 1e6).toFixed(3) + " ms" : "N/A"),
355-
"Optimize : " + (optimizeTime ? (optimizeTime / 1e6).toFixed(3) + " ms" : "N/A")
356-
].join("\n"));
416+
if (args.measure) process.on("beforeExit", () => console.error([
417+
"I/O Read : " + (readTime ? (readTime / 1e6).toFixed(3) + " ms (" + readCount + " files)" : "N/A"),
418+
"I/O Write : " + (writeTime ? (writeTime / 1e6).toFixed(3) + " ms (" + writeCount + " files)" : "N/A"),
419+
"Parse : " + (parseTime ? (parseTime / 1e6).toFixed(3) + " ms" : "N/A"),
420+
"Compile : " + (compileTime ? (compileTime / 1e6).toFixed(3) + " ms" : "N/A"),
421+
"Validate : " + (validateTime ? (validateTime / 1e6).toFixed(3) + " ms" : "N/A"),
422+
"Optimize : " + (optimizeTime ? (optimizeTime / 1e6).toFixed(3) + " ms" : "N/A")
423+
].join("\n")));

bin/asc.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"type": "number"
3131
},
3232
"shrinkLevel": {
33-
"desc": "How much to focus on shrinking code size. [0-2]",
33+
"desc": "How much to focus on shrinking code size. [0-2, s=1, z=2]",
3434
"type": "number"
3535
},
3636
"validate": {
@@ -58,6 +58,13 @@
5858
"type": "string",
5959
"aliases": [ "a" ]
6060
},
61+
"sourceMap": {
62+
"desc": [
63+
"Enables source map generation. Optionally takes the URL",
64+
"used to reference the source map from the binary file."
65+
],
66+
"type": "string"
67+
},
6168
"noTreeShaking": {
6269
"desc": "Disables compiler-level tree-shaking.",
6370
"type": "boolean"

examples/tlsf/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
TLSF memory allocator
2-
=====================
1+
![](https://s.gravatar.com/avatar/f105de3decfafc734b8eabe9a960b25d?size=48) TLSF
2+
=================
33

4-
A port of [Matt Conte's implementation](https://github.com/mattconte/tlsf) of the [TLSF](http://www.gii.upv.es/tlsf/) memory allocator to AssemblyScript.
4+
An implementation of the [Two Level Segregate Fit](http://www.gii.upv.es/tlsf/main/docs)
5+
memory allocator in AssemblyScript.
56

67
Instructions
78
------------

examples/tlsf/assembly/LICENSE

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)