mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-04-26 15:32:16 +00:00
Unify / simplify asc API (in browsers)
This commit is contained in:
parent
acfef646ef
commit
5e20bed09a
@ -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:
|
The API accepts the same options as the CLI but also lets you override stdout and stderr and/or provide a callback. Example:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const asc = require("assemblyscript/bin/asc.js");
|
const asc = require("assemblyscript/bin/asc");
|
||||||
asc.main([
|
asc.main([
|
||||||
"myModule.ts",
|
"myModule.ts",
|
||||||
"--binaryFile", "myModule.wasm",
|
"--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
|
```js
|
||||||
const asc = require('assemblyscript/dist/asc')
|
const { binary, text, stdout, stderr } = asc.compileString(`...`, { optimize: 2 });
|
||||||
|
...
|
||||||
const input = '...'
|
|
||||||
const args = { optimize: 2 }
|
|
||||||
|
|
||||||
asc.compileString(input, args)
|
|
||||||
.then(({ wasm, wast, stdout, stderr }) => {
|
|
||||||
// ...
|
|
||||||
})
|
|
||||||
.catch(({ err, stdout, stderr }) => {
|
|
||||||
// ...
|
|
||||||
})
|
|
||||||
```
|
```
|
||||||
|
130
bin/asc.js
130
bin/asc.js
@ -61,65 +61,47 @@ exports.defaultOptimizeLevel = 2;
|
|||||||
/** Default Binaryen shrink level. */
|
/** Default Binaryen shrink level. */
|
||||||
exports.defaultShrinkLevel = 1;
|
exports.defaultShrinkLevel = 1;
|
||||||
|
|
||||||
/** Bundled library files, if any. */
|
/** Bundled library files. */
|
||||||
exports.libraryFiles = exports.isBundle ? BUNDLE_LIBRARY : {};
|
exports.libraryFiles = exports.isBundle ? BUNDLE_LIBRARY : (() => { // set up if not a bundle
|
||||||
|
const libDir = path.join(__dirname, "..", "std", "assembly");
|
||||||
/** Bundled definition files, if any. */
|
const libFiles = require("glob").sync("**/*.ts", { cwd: libDir });
|
||||||
exports.definitionFiles = exports.isBundle ? BUNDLE_DEFINITIONS : {};
|
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. */
|
/** Convenience function that parses and compiles source strings directly. */
|
||||||
exports.compileString = (source, extraArgs={}) => new Promise((resolve, reject) => {
|
exports.compileString = (sources, options) => {
|
||||||
const sources = {};
|
if (typeof sources === "string") sources = { "input.ts": sources };
|
||||||
const output = {};
|
const output = Object.create({
|
||||||
|
|
||||||
if (typeof source === "string") {
|
|
||||||
sources["input.ts"] = source;
|
|
||||||
}
|
|
||||||
Object.keys(sources).forEach(k => {
|
|
||||||
sources[`/${k}`] = sources[k];
|
|
||||||
delete sources[k];
|
|
||||||
})
|
|
||||||
const options = {
|
|
||||||
stdout: createMemoryStream(),
|
stdout: createMemoryStream(),
|
||||||
stderr: createMemoryStream(),
|
stderr: createMemoryStream(),
|
||||||
readFile: name => sources[name],
|
binary: null,
|
||||||
writeFile: (name, contents) => output[name.replace(/^\//, "")] = contents,
|
text: null
|
||||||
listFiles: Function.prototype
|
});
|
||||||
};
|
exports.main([
|
||||||
|
"--binaryFile", "binary",
|
||||||
// if not a bundle, include std lib since we override readFile
|
"--textFile", "text",
|
||||||
if (!exports.isBundle) {
|
...Object.keys(options || {}).map(arg => `--${arg}=${options[arg]}`),
|
||||||
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),
|
...Object.keys(sources),
|
||||||
];
|
], {
|
||||||
|
stdout: output.stdout,
|
||||||
exports.main(args, options, (err) => {
|
stderr: output.stderr,
|
||||||
if (err) {
|
readFile: name => sources.hasOwnProperty(name) ? sources[name] : null,
|
||||||
reject({
|
writeFile: (name, contents) => output[name] = contents,
|
||||||
err,
|
listFiles: () => []
|
||||||
stdout: options.stdout.toString(),
|
});
|
||||||
stderr: options.stderr.toString(),
|
return output;
|
||||||
})
|
|
||||||
} else {
|
|
||||||
resolve(Object.assign(output, {
|
|
||||||
stdout: options.stdout.toString(),
|
|
||||||
stderr: options.stderr.toString(),
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
/** Runs the command line utility using the specified arguments array. */
|
/** Runs the command line utility using the specified arguments array. */
|
||||||
exports.main = function main(argv, options, callback) {
|
exports.main = function main(argv, options, callback) {
|
||||||
@ -203,18 +185,13 @@ exports.main = function main(argv, options, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set up base directory
|
// Set up base directory
|
||||||
const baseDir = args.baseDir ? path.resolve(args.baseDir) : process.cwd();
|
const baseDir = args.baseDir ? path.resolve(args.baseDir) : ".";
|
||||||
|
|
||||||
// Include standard library if --noLib isn't set
|
|
||||||
const stdLibDir = path.join(__dirname, "..", "std", "assembly");
|
|
||||||
const libDirs = args.noLib ? [] : [ stdLibDir ];
|
|
||||||
|
|
||||||
// Include custom library components (with or without stdlib)
|
// Include custom library components (with or without stdlib)
|
||||||
|
const customLibDirs = [];
|
||||||
if (args.lib) {
|
if (args.lib) {
|
||||||
if (typeof args.lib === "string") {
|
if (typeof args.lib === "string") args.lib = args.lib.split(",");
|
||||||
args.lib = args.lib.split(",");
|
Array.prototype.push.apply(customLibDirs, args.lib.map(lib => lib.trim()));
|
||||||
}
|
|
||||||
Array.prototype.push.apply(libDirs, args.lib.map(lib => lib.trim()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Begin parsing
|
// Begin parsing
|
||||||
@ -258,8 +235,8 @@ exports.main = function main(argv, options, callback) {
|
|||||||
sourceText = exports.libraryFiles[indexName];
|
sourceText = exports.libraryFiles[indexName];
|
||||||
sourcePath = exports.libraryPrefix + indexName + ".ts";
|
sourcePath = exports.libraryPrefix + indexName + ".ts";
|
||||||
} else {
|
} else {
|
||||||
for (let i = 0, k = libDirs.length; i < k; ++i) {
|
for (let i = 0, k = customLibDirs.length; i < k; ++i) {
|
||||||
const dir = libDirs[i];
|
const dir = customLibDirs[i];
|
||||||
sourceText = readFile(path.join(dir, plainName + ".ts"));
|
sourceText = readFile(path.join(dir, plainName + ".ts"));
|
||||||
if (sourceText !== null) {
|
if (sourceText !== null) {
|
||||||
sourcePath = exports.libraryPrefix + plainName + ".ts";
|
sourcePath = exports.libraryPrefix + plainName + ".ts";
|
||||||
@ -293,8 +270,8 @@ exports.main = function main(argv, options, callback) {
|
|||||||
sourceText = exports.libraryFiles[indexName];
|
sourceText = exports.libraryFiles[indexName];
|
||||||
sourcePath = exports.libraryPrefix + indexName + ".ts";
|
sourcePath = exports.libraryPrefix + indexName + ".ts";
|
||||||
} else {
|
} else {
|
||||||
for (let i = 0, k = libDirs.length; i < k; ++i) {
|
for (let i = 0, k = customLibDirs.length; i < k; ++i) {
|
||||||
const dir = libDirs[i];
|
const dir = customLibDirs[i];
|
||||||
sourceText = readFile(path.join(dir, plainName + ".ts"));
|
sourceText = readFile(path.join(dir, plainName + ".ts"));
|
||||||
if (sourceText !== null) {
|
if (sourceText !== null) {
|
||||||
sourcePath = exports.libraryPrefix + plainName + ".ts";
|
sourcePath = exports.libraryPrefix + plainName + ".ts";
|
||||||
@ -312,7 +289,7 @@ exports.main = function main(argv, options, callback) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (sourceText == null) {
|
if (sourceText == null) {
|
||||||
return callback(Error("Import file '" + plainName + ".ts' not found."));
|
return callback(Error("Import file '" + sourcePath + ".ts' not found."));
|
||||||
}
|
}
|
||||||
stats.parseCount++;
|
stats.parseCount++;
|
||||||
stats.parseTime += measure(() => {
|
stats.parseTime += measure(() => {
|
||||||
@ -325,24 +302,21 @@ exports.main = function main(argv, options, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Include (other) library components
|
// Include (other) library components
|
||||||
var hasBundledLibrary = false;
|
if (!args.noLib) // bundled
|
||||||
if (!args.noLib)
|
|
||||||
Object.keys(exports.libraryFiles).forEach(libPath => {
|
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.parseCount++;
|
||||||
stats.parseTime += measure(() => {
|
stats.parseTime += measure(() => {
|
||||||
parser = assemblyscript.parseFile(
|
parser = assemblyscript.parseFile(
|
||||||
exports.libraryFiles[libPath],
|
exports.libraryFiles[libPath],
|
||||||
libPath + ".ts",
|
exports.libraryPrefix + libPath + ".ts",
|
||||||
false,
|
false,
|
||||||
parser
|
parser
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
hasBundledLibrary = true;
|
|
||||||
});
|
});
|
||||||
for (let i = 0, k = libDirs.length; i < k; ++i) {
|
for (let i = 0, k = customLibDirs.length; i < k; ++i) { // custom
|
||||||
if (i === 0 && hasBundledLibrary) continue;
|
let libDir = customLibDirs[i];
|
||||||
let libDir = libDirs[i];
|
|
||||||
let libFiles;
|
let libFiles;
|
||||||
if (libDir.endsWith(".ts")) {
|
if (libDir.endsWith(".ts")) {
|
||||||
libFiles = [ path.basename(libDir) ];
|
libFiles = [ path.basename(libDir) ];
|
||||||
@ -353,9 +327,7 @@ exports.main = function main(argv, options, callback) {
|
|||||||
for (let j = 0, l = libFiles.length; j < l; ++j) {
|
for (let j = 0, l = libFiles.length; j < l; ++j) {
|
||||||
let libPath = libFiles[j];
|
let libPath = libFiles[j];
|
||||||
let libText = readFile(path.join(libDir, libPath));
|
let libText = readFile(path.join(libDir, libPath));
|
||||||
if (libText === null) {
|
if (libText === null) return callback(Error("Library file '" + libPath + "' not found."));
|
||||||
return callback(Error("Library file '" + libPath + "' not found."));
|
|
||||||
}
|
|
||||||
stats.parseCount++;
|
stats.parseCount++;
|
||||||
stats.parseTime += measure(() => {
|
stats.parseTime += measure(() => {
|
||||||
parser = assemblyscript.parseFile(
|
parser = assemblyscript.parseFile(
|
||||||
@ -562,9 +534,9 @@ exports.main = function main(argv, options, callback) {
|
|||||||
sourceMap.sources.forEach((name, index) => {
|
sourceMap.sources.forEach((name, index) => {
|
||||||
let text = null;
|
let text = null;
|
||||||
if (name.startsWith(exports.libraryPrefix)) {
|
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(
|
text = readFile(path.join(
|
||||||
libDirs[i],
|
customLibDirs[i],
|
||||||
name.substring(exports.libraryPrefix.length))
|
name.substring(exports.libraryPrefix.length))
|
||||||
);
|
);
|
||||||
if (text !== null) break;
|
if (text !== null) break;
|
||||||
|
2
dist/asc.js
vendored
2
dist/asc.js
vendored
File diff suppressed because one or more lines are too long
2
dist/asc.js.map
vendored
2
dist/asc.js.map
vendored
File diff suppressed because one or more lines are too long
@ -76,6 +76,34 @@ function readVarint64(): i64 {
|
|||||||
return select<u64>(val | (~0 << shl), val, shl < 64 && (byt & 0x40) != 0);
|
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
|
// Imported callbacks
|
||||||
declare function onSection(id: u32, offset: u32, length: u32, nameOffset: u32, nameLength: u32): bool;
|
declare function onSection(id: u32, offset: u32, length: u32, nameOffset: u32, nameLength: u32): bool;
|
||||||
declare function onType(index: u32, form: u32): void;
|
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) {
|
for (let i: u32 = 0; i < count; ++i) {
|
||||||
let type = readVarint(7) & 0x7f;
|
let type = readVarint(7) & 0x7f;
|
||||||
let mutability = readVaruint(1);
|
let mutability = readVaruint(1);
|
||||||
let op = readUint<u8>();
|
skipInitExpr();
|
||||||
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();
|
|
||||||
onGlobal(
|
onGlobal(
|
||||||
glo_space_index++,
|
glo_space_index++,
|
||||||
type,
|
type,
|
||||||
@ -389,7 +392,9 @@ export function parse(begin: usize, end: usize): void {
|
|||||||
off = payload_off + payload_len; // ignore errors
|
off = payload_off + payload_len; // ignore errors
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SectionId.Code: { // skip
|
case SectionId.Element:
|
||||||
|
case SectionId.Code:
|
||||||
|
case SectionId.Data: { // skip
|
||||||
off += payload_len;
|
off += payload_len;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -18,8 +18,7 @@
|
|||||||
import {
|
import {
|
||||||
AL_BITS,
|
AL_BITS,
|
||||||
AL_SIZE,
|
AL_SIZE,
|
||||||
AL_MASK,
|
AL_MASK
|
||||||
MAX_SIZE_32
|
|
||||||
} from "./common";
|
} from "./common";
|
||||||
|
|
||||||
const SL_BITS: u32 = 5;
|
const SL_BITS: u32 = 5;
|
||||||
@ -33,8 +32,6 @@ const FL_BITS: u32 = (sizeof<usize>() == sizeof<u32>()
|
|||||||
: 32 // ^= up to 4GB per block
|
: 32 // ^= up to 4GB per block
|
||||||
) - SB_BITS;
|
) - SB_BITS;
|
||||||
|
|
||||||
// assert(1 << (FL_BITS + SB_BITS) == MAX_SIZE_32);
|
|
||||||
|
|
||||||
// ╒════════════════ Block structure layout (32-bit) ══════════════╕
|
// ╒════════════════ Block structure layout (32-bit) ══════════════╕
|
||||||
// 3 2 1
|
// 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
|
// 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
|
||||||
|
@ -1,31 +1,20 @@
|
|||||||
const asc = require("../dist/asc.js");
|
const asc = require("../dist/asc.js");
|
||||||
|
|
||||||
function log(arg) {
|
console.log("# asc.main");
|
||||||
console.log(arg);
|
|
||||||
}
|
|
||||||
|
|
||||||
const stdout = asc.createMemoryStream(arg => {
|
const stdout = asc.createMemoryStream(arg => console.log("out:", arg));
|
||||||
console.log("out:", arg);
|
const stderr = asc.createMemoryStream(arg => console.log("err:", arg));
|
||||||
});
|
const files = { "module.ts": `import "allocator/arena";` };
|
||||||
const stderr = asc.createMemoryStream(arg => {
|
|
||||||
console.log("err:", arg);
|
|
||||||
});
|
|
||||||
|
|
||||||
const files = {
|
|
||||||
"/module.ts": `import "allocator/arena";`
|
|
||||||
};
|
|
||||||
|
|
||||||
asc.main([
|
asc.main([
|
||||||
"./module.ts",
|
"module.ts",
|
||||||
"--textFile"
|
"--textFile"
|
||||||
], {
|
], {
|
||||||
stdout: stdout,
|
stdout: stdout,
|
||||||
stderr: stderr,
|
stderr: stderr,
|
||||||
readFile: (name) => {
|
readFile: (name) => {
|
||||||
console.log("readFile: " + name);
|
console.log("readFile: " + name);
|
||||||
if (files.hasOwnProperty(name)) {
|
if (files.hasOwnProperty(name)) return files[name];
|
||||||
return files[name];
|
|
||||||
}
|
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
writeFile: (name, data) => {
|
writeFile: (name, data) => {
|
||||||
@ -40,8 +29,20 @@ asc.main([
|
|||||||
console.log(">>> THROWN >>>");
|
console.log(">>> THROWN >>>");
|
||||||
console.log(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
console.log(">>> STDOUT >>>");
|
console.log(">>> STDOUT >>>");
|
||||||
process.stdout.write(stdout.toString());
|
process.stdout.write(stdout.toString());
|
||||||
console.log(">>> STDERR >>>");
|
console.log(">>> STDERR >>>");
|
||||||
process.stdout.write(stderr.toString());
|
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");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user