-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpioasm.js
86 lines (79 loc) · 2.27 KB
/
pioasm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/** pioasm in Web Assembly
* Copyright (C) 2021, Uri Shaked
*/
/// <reference types="emscripten" />
import wasm from '#pioasm-emcc';
/**
* PIO Output format:
* - c-sdk: C header suitable for use with the Raspberry Pi Pico SDK
* - python: Python file suitable for use with MicroPython
* - hex: Raw hex output (only valid for single program inputs)
* - ada: Ada specification
*
* @typedef {'c-sdk'|'python'|'hex'|'ada'} PioOutputFormat
*/
/**
* PIO Assembler wrapper class
*/
export class PIOAssembler {
/** @private */
exitCode = 0;
/** @private */
outputBuffer = [];
/** @private */
instance;
constructor() {}
/**
* Loads the pioasm Web Assembly module. Normally, `pioasm()` will load the module for
* you, but you can use the `load()` method to pre-loader the Web Assembly module, or
* if you need to provide custom options to EMScripten.
*
* For instance, you can override the `locateFile(url: string, scriptDirectory: string)`
* method to configure the URL for the compiled web assembly module.
*
* @param {Partial<EmscriptenModule>} [options]
* @returns {Promise<EmscriptenModule>}
*/
async load(options) {
if (!this.instance) {
this.instance = wasm({
noInitialRun: true,
print: (msg) => {
this.outputBuffer.push(msg);
},
printErr: (msg) => {
this.outputBuffer.push(msg);
},
quit: (code) => {
this.exitCode = code;
},
...options,
});
}
return this.instance;
}
/**
* Compiles the given PIO source file.
*
* @param {string} source PIO source to compile
* @param {PioOutputFormat} [format='c-sdk'] Output format
* @param {string} [outputParam] Add a parameter to be passed to the output format generator
* @returns Promise<{output: string, exitCode: number}>
*/
async assemble(source, format = 'c-sdk', outputParam) {
const runtime = await this.load();
runtime.FS_writeFile('/input.pio', source);
this.outputBuffer = [];
this.exitCode = 0;
const argv = ['-o', format];
if (outputParam) {
argv.push('-p', outputParam);
}
argv.push('input.pio');
runtime.callMain(argv);
return {
output: this.outputBuffer.join('\n'),
exitCode: this.exitCode,
};
}
}