mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-04-25 15:12:12 +00:00
Add a mechanism to hook into the compilation process with custom transforms, see #20
A custom transform is a node module that exports hooks called by the compiler on specific occasions, then being able to modify the intermediate results. Starting with 'afterParse' to modify the AST before compilation is performed.
This commit is contained in:
parent
7ad13f9d65
commit
113925fa7e
22
bin/asc.js
22
bin/asc.js
@ -187,6 +187,26 @@ exports.main = function main(argv, options, callback) {
|
||||
// Set up base directory
|
||||
const baseDir = args.baseDir ? path.resolve(args.baseDir) : ".";
|
||||
|
||||
// Set up transforms
|
||||
const transforms = [];
|
||||
if (args.transform) {
|
||||
if (typeof args.transform === "string") args.transform = args.transform.split(",");
|
||||
args.transform.forEach(transform =>
|
||||
transforms.push(
|
||||
require(
|
||||
path.isAbsolute(transform = transform.trim())
|
||||
? transform
|
||||
: path.join(process.cwd(), transform)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
function applyTransform(name, ...args) {
|
||||
transforms.forEach(transform => {
|
||||
if (typeof transform[name] === "function") transform[name](...args);
|
||||
});
|
||||
}
|
||||
|
||||
// Begin parsing
|
||||
var parser = null;
|
||||
|
||||
@ -340,6 +360,8 @@ exports.main = function main(argv, options, callback) {
|
||||
}
|
||||
}
|
||||
|
||||
applyTransform("afterParse", parser);
|
||||
|
||||
// Finish parsing
|
||||
const program = assemblyscript.finishParsing(parser);
|
||||
|
||||
|
@ -160,6 +160,10 @@
|
||||
"type": "string",
|
||||
"aliases": [ "feature" ]
|
||||
},
|
||||
"transform": {
|
||||
"desc": "Specifies the path to a custom transform to 'require'.",
|
||||
"type": "string"
|
||||
},
|
||||
"measure": {
|
||||
"desc": "Prints measuring information on I/O and compile times.",
|
||||
"type": "boolean"
|
||||
|
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
2
std/assembly.d.ts
vendored
2
std/assembly.d.ts
vendored
@ -267,7 +267,7 @@ declare function offsetof<T>(fieldName?: string): usize;
|
||||
declare function changetype<T>(value: any): T;
|
||||
/** Explicitly requests no bounds checks on the provided expression. Useful for array accesses. */
|
||||
declare function unchecked<T>(value: T): T;
|
||||
/** Creates a `call_indirect` instruction, calling the specified target by index with the specified arguments. */
|
||||
/** Emits a `call_indirect` instruction, calling the specified function in the function table by index with the specified arguments. Does result in a runtime error if the arguments do not match the called function. */
|
||||
declare function call_indirect<T>(target: Function | u32, ...args: any[]): T;
|
||||
/** Tests if a 32-bit or 64-bit float is `NaN`. */
|
||||
declare function isNaN<T = f32 | f64>(value: T): bool;
|
||||
|
Loading…
x
Reference in New Issue
Block a user