mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-04-25 15:12:12 +00:00
Add compileString convenience function to asc (#47)
This commit is contained in:
parent
8c1847b316
commit
3a8f9f1576
@ -39,3 +39,20 @@ Available command line options can also be obtained programmatically:
|
||||
const options = require("assemblyscript/bin/asc.json");
|
||||
...
|
||||
```
|
||||
|
||||
You can also compile a source string directly, eg. 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 }) => {
|
||||
// ...
|
||||
})
|
||||
```
|
||||
|
54
bin/asc.js
54
bin/asc.js
@ -66,6 +66,60 @@ exports.libraryFiles = exports.isBundle ? BUNDLE_LIBRARY : {};
|
||||
/** Bundled definition files, if any. */
|
||||
exports.definitionFiles = exports.isBundle ? BUNDLE_DEFINITIONS : {};
|
||||
|
||||
|
||||
/** 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 = {
|
||||
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["(lib)/" + 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(),
|
||||
}))
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
/** Runs the command line utility using the specified arguments array. */
|
||||
exports.main = function main(argv, options, callback) {
|
||||
if (typeof options === "function") {
|
||||
|
Loading…
x
Reference in New Issue
Block a user