Unify / simplify asc API (in browsers)

This commit is contained in:
dcodeIO 2018-04-04 14:39:40 +02:00
parent acfef646ef
commit 5e20bed09a
9 changed files with 116 additions and 151 deletions

View File

@ -16,7 +16,7 @@ API
The API accepts the same options as the CLI but also lets you override stdout and stderr and/or provide a callback. Example:
```js
const asc = require("assemblyscript/bin/asc.js");
const asc = require("assemblyscript/bin/asc");
asc.main([
"myModule.ts",
"--binaryFile", "myModule.wasm",
@ -40,19 +40,9 @@ const options = require("assemblyscript/bin/asc.json");
...
```
You can also compile a source string directly, eg. in a browser environment:
You can also compile a source string directly, for example in a browser environment:
```js
const asc = require('assemblyscript/dist/asc')
const input = '...'
const args = { optimize: 2 }
asc.compileString(input, args)
.then(({ wasm, wast, stdout, stderr }) => {
// ...
})
.catch(({ err, stdout, stderr }) => {
// ...
})
const { binary, text, stdout, stderr } = asc.compileString(`...`, { optimize: 2 });
...
```

View File

@ -61,65 +61,47 @@ exports.defaultOptimizeLevel = 2;
/** Default Binaryen shrink level. */
exports.defaultShrinkLevel = 1;
/** Bundled library files, if any. */
exports.libraryFiles = exports.isBundle ? BUNDLE_LIBRARY : {};
/** Bundled definition files, if any. */
exports.definitionFiles = exports.isBundle ? BUNDLE_DEFINITIONS : {};
/** Bundled library files. */
exports.libraryFiles = exports.isBundle ? BUNDLE_LIBRARY : (() => { // set up if not a bundle
const libDir = path.join(__dirname, "..", "std", "assembly");
const libFiles = require("glob").sync("**/*.ts", { cwd: libDir });
const bundled = {};
libFiles.forEach(file => bundled[file.replace(/\.ts$/, "")] = fs.readFileSync(path.join(libDir, file), "utf8" ));
return bundled;
})();
/** Bundled definition files. */
exports.definitionFiles = exports.isBundle ? BUNDLE_DEFINITIONS : (() => { // set up if not a bundle
const stdDir = path.join(__dirname, "..", "std");
return {
"assembly": fs.readFileSync(path.join(stdDir, "assembly.d.ts"), "utf8"),
"portable": fs.readFileSync(path.join(stdDir, "portable.d.ts"), "utf8")
};
})();
/** Convenience function that parses and compiles source strings directly. */
exports.compileString = (source, extraArgs={}) => new Promise((resolve, reject) => {
const sources = {};
const output = {};
if (typeof source === "string") {
sources["input.ts"] = source;
}
Object.keys(sources).forEach(k => {
sources[`/${k}`] = sources[k];
delete sources[k];
})
const options = {
exports.compileString = (sources, options) => {
if (typeof sources === "string") sources = { "input.ts": sources };
const output = Object.create({
stdout: createMemoryStream(),
stderr: createMemoryStream(),
readFile: name => sources[name],
writeFile: (name, contents) => output[name.replace(/^\//, "")] = contents,
listFiles: Function.prototype
};
// if not a bundle, include std lib since we override readFile
if (!exports.isBundle) {
const libDir = path.join(__dirname, "../std", "assembly");
const libFiles = require("glob").sync("**/*.ts", { cwd: libDir });
libFiles.forEach(file =>
exports.libraryFiles[file.replace(/\.ts$/, "")] = readFileNode(path.join(libDir, file), { encoding: "utf8" })
);
}
const args = [
"--baseDir=/",
"--binaryFile=wasm",
"--textFile=wast",
...Object.keys(extraArgs).map(arg => `--${arg}=${extraArgs[arg]}`),
...Object.keys(sources),
];
exports.main(args, options, (err) => {
if (err) {
reject({
err,
stdout: options.stdout.toString(),
stderr: options.stderr.toString(),
})
} else {
resolve(Object.assign(output, {
stdout: options.stdout.toString(),
stderr: options.stderr.toString(),
}))
}
binary: null,
text: null
});
});
exports.main([
"--binaryFile", "binary",
"--textFile", "text",
...Object.keys(options || {}).map(arg => `--${arg}=${options[arg]}`),
...Object.keys(sources),
], {
stdout: output.stdout,
stderr: output.stderr,
readFile: name => sources.hasOwnProperty(name) ? sources[name] : null,
writeFile: (name, contents) => output[name] = contents,
listFiles: () => []
});
return output;
}
/** Runs the command line utility using the specified arguments array. */
exports.main = function main(argv, options, callback) {
@ -203,18 +185,13 @@ exports.main = function main(argv, options, callback) {
}
// Set up base directory
const baseDir = args.baseDir ? path.resolve(args.baseDir) : process.cwd();
// Include standard library if --noLib isn't set
const stdLibDir = path.join(__dirname, "..", "std", "assembly");
const libDirs = args.noLib ? [] : [ stdLibDir ];
const baseDir = args.baseDir ? path.resolve(args.baseDir) : ".";
// Include custom library components (with or without stdlib)
const customLibDirs = [];
if (args.lib) {
if (typeof args.lib === "string") {
args.lib = args.lib.split(",");
}
Array.prototype.push.apply(libDirs, args.lib.map(lib => lib.trim()));
if (typeof args.lib === "string") args.lib = args.lib.split(",");
Array.prototype.push.apply(customLibDirs, args.lib.map(lib => lib.trim()));
}
// Begin parsing
@ -258,8 +235,8 @@ exports.main = function main(argv, options, callback) {
sourceText = exports.libraryFiles[indexName];
sourcePath = exports.libraryPrefix + indexName + ".ts";
} else {
for (let i = 0, k = libDirs.length; i < k; ++i) {
const dir = libDirs[i];
for (let i = 0, k = customLibDirs.length; i < k; ++i) {
const dir = customLibDirs[i];
sourceText = readFile(path.join(dir, plainName + ".ts"));
if (sourceText !== null) {
sourcePath = exports.libraryPrefix + plainName + ".ts";
@ -293,8 +270,8 @@ exports.main = function main(argv, options, callback) {
sourceText = exports.libraryFiles[indexName];
sourcePath = exports.libraryPrefix + indexName + ".ts";
} else {
for (let i = 0, k = libDirs.length; i < k; ++i) {
const dir = libDirs[i];
for (let i = 0, k = customLibDirs.length; i < k; ++i) {
const dir = customLibDirs[i];
sourceText = readFile(path.join(dir, plainName + ".ts"));
if (sourceText !== null) {
sourcePath = exports.libraryPrefix + plainName + ".ts";
@ -312,7 +289,7 @@ exports.main = function main(argv, options, callback) {
}
}
if (sourceText == null) {
return callback(Error("Import file '" + plainName + ".ts' not found."));
return callback(Error("Import file '" + sourcePath + ".ts' not found."));
}
stats.parseCount++;
stats.parseTime += measure(() => {
@ -325,27 +302,24 @@ exports.main = function main(argv, options, callback) {
}
// Include (other) library components
var hasBundledLibrary = false;
if (!args.noLib)
if (!args.noLib) // bundled
Object.keys(exports.libraryFiles).forEach(libPath => {
if (libPath.lastIndexOf("/") >= exports.libraryPrefix.length) return;
if (libPath.indexOf("/") >= 0) return; // in sub-directory: imported on demand
stats.parseCount++;
stats.parseTime += measure(() => {
parser = assemblyscript.parseFile(
exports.libraryFiles[libPath],
libPath + ".ts",
exports.libraryPrefix + libPath + ".ts",
false,
parser
);
});
hasBundledLibrary = true;
});
for (let i = 0, k = libDirs.length; i < k; ++i) {
if (i === 0 && hasBundledLibrary) continue;
let libDir = libDirs[i];
for (let i = 0, k = customLibDirs.length; i < k; ++i) { // custom
let libDir = customLibDirs[i];
let libFiles;
if (libDir.endsWith(".ts")) {
libFiles = [path.basename(libDir)];
libFiles = [ path.basename(libDir) ];
libDir = path.dirname(libDir);
} else {
libFiles = listFiles(libDir);
@ -353,9 +327,7 @@ exports.main = function main(argv, options, callback) {
for (let j = 0, l = libFiles.length; j < l; ++j) {
let libPath = libFiles[j];
let libText = readFile(path.join(libDir, libPath));
if (libText === null) {
return callback(Error("Library file '" + libPath + "' not found."));
}
if (libText === null) return callback(Error("Library file '" + libPath + "' not found."));
stats.parseCount++;
stats.parseTime += measure(() => {
parser = assemblyscript.parseFile(
@ -562,9 +534,9 @@ exports.main = function main(argv, options, callback) {
sourceMap.sources.forEach((name, index) => {
let text = null;
if (name.startsWith(exports.libraryPrefix)) {
for (let i = 0, k = libDirs.length; i < k; ++i) {
for (let i = 0, k = customLibDirs.length; i < k; ++i) {
text = readFile(path.join(
libDirs[i],
customLibDirs[i],
name.substring(exports.libraryPrefix.length))
);
if (text !== null) break;

2
dist/asc.js vendored

File diff suppressed because one or more lines are too long

2
dist/asc.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -76,6 +76,34 @@ function readVarint64(): i64 {
return select<u64>(val | (~0 << shl), val, shl < 64 && (byt & 0x40) != 0);
}
function skipInitExpr(): void {
var op = readUint<u8>();
switch (op) {
case Opcode.i32_const: {
readVarint(32);
break;
}
case Opcode.i64_const: {
readVarint64();
break;
}
case Opcode.f32_const: {
readUint<u32>();
break;
}
case Opcode.f64_const: {
readUint64();
break;
}
case Opcode.get_global: {
readVaruint(32);
break;
}
default: unreachable(); // MVP
}
if (readUint<u8>() != Opcode.end) unreachable();
}
// Imported callbacks
declare function onSection(id: u32, offset: u32, length: u32, nameOffset: u32, nameLength: u32): bool;
declare function onType(index: u32, form: u32): void;
@ -257,32 +285,7 @@ export function parse(begin: usize, end: usize): void {
for (let i: u32 = 0; i < count; ++i) {
let type = readVarint(7) & 0x7f;
let mutability = readVaruint(1);
let op = readUint<u8>();
switch (op) {
case Opcode.i32_const: {
readVarint(32);
break;
}
case Opcode.i64_const: {
readVarint64();
break;
}
case Opcode.f32_const: {
readUint<u32>();
break;
}
case Opcode.f64_const: {
readUint64();
break;
}
case Opcode.get_global: {
readVaruint(32);
break;
}
default: unreachable();
}
op = readUint<u8>();
if (op != Opcode.end) unreachable();
skipInitExpr();
onGlobal(
glo_space_index++,
type,
@ -389,7 +392,9 @@ export function parse(begin: usize, end: usize): void {
off = payload_off + payload_len; // ignore errors
break;
}
case SectionId.Code: { // skip
case SectionId.Element:
case SectionId.Code:
case SectionId.Data: { // skip
off += payload_len;
break;
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -18,8 +18,7 @@
import {
AL_BITS,
AL_SIZE,
AL_MASK,
MAX_SIZE_32
AL_MASK
} from "./common";
const SL_BITS: u32 = 5;
@ -33,8 +32,6 @@ const FL_BITS: u32 = (sizeof<usize>() == sizeof<u32>()
: 32 // ^= up to 4GB per block
) - SB_BITS;
// assert(1 << (FL_BITS + SB_BITS) == MAX_SIZE_32);
// ╒════════════════ Block structure layout (32-bit) ══════════════╕
// 3 2 1
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 bits

View File

@ -1,31 +1,20 @@
const asc = require("../dist/asc.js");
function log(arg) {
console.log(arg);
}
console.log("# asc.main");
const stdout = asc.createMemoryStream(arg => {
console.log("out:", arg);
});
const stderr = asc.createMemoryStream(arg => {
console.log("err:", arg);
});
const files = {
"/module.ts": `import "allocator/arena";`
};
const stdout = asc.createMemoryStream(arg => console.log("out:", arg));
const stderr = asc.createMemoryStream(arg => console.log("err:", arg));
const files = { "module.ts": `import "allocator/arena";` };
asc.main([
"./module.ts",
"module.ts",
"--textFile"
], {
stdout: stdout,
stderr: stderr,
readFile: (name) => {
console.log("readFile: " + name);
if (files.hasOwnProperty(name)) {
return files[name];
}
if (files.hasOwnProperty(name)) return files[name];
return null;
},
writeFile: (name, data) => {
@ -40,8 +29,20 @@ asc.main([
console.log(">>> THROWN >>>");
console.log(err);
}
console.log(">>> STDOUT >>>");
process.stdout.write(stdout.toString());
console.log(">>> STDERR >>>");
process.stdout.write(stderr.toString());
});
console.log(">>> STDOUT >>>");
process.stdout.write(stdout.toString());
console.log(">>> STDERR >>>");
process.stdout.write(stderr.toString());
console.log("\n# asc.compileString");
const output = asc.compileString(`import "allocator/arena";`, { optimize: 2 });
console.log(">>> .stdout >>>");
process.stdout.write(output.stdout.toString());
console.log(">>> .stderr >>>");
process.stdout.write(output.stderr.toString());
console.log(">>> .text >>>");
process.stdout.write(output.text);
console.log(">>> .binary >>> " + output.binary.length + " bytes");