-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathassemble.js
208 lines (206 loc) · 5.53 KB
/
assemble.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env node
'use strict';
var yargs = require ('yargs');
var fs = require ('fs');
var path = require ('path');
yargs.command(['$0 <filename> [options]', 'assemble', 'a'], 'Assemble a file', (yargs) =>
{
yargs.option('log', {
alias: 'l',
default: false,
desc: 'Print assembly log'
}).option('debug', {
alias: 'd',
default: false,
desc: 'Include debug information: function and variable names'
}).option('output', {
alias: 'o',
default: null,
desc: 'Output file name, default is input file name with the .wasm extension'
});
}, (argv) => {
var libwabt = require ('wabt')();
var source = null;
// console.log (argv.filename);
try
{
source = fs.readFileSync (argv.filename).toString ();
}
catch (e)
{
console.error (argv.filename+' file not found');
process.exit (-1);
}
let binary = null;
let binaryout = null;
try
{
if (argv.verbose) console.log ('Parsing '+argv.filename);
binary = libwabt.parseWat (argv.filename, source);
if (argv.verbose) console.log ('Resolving names '+argv.filename);
binary.resolveNames ();
if (argv.verbose) console.log ('Validating '+argv.filename);
binary.validate ();
binaryout = binary.toBinary ({log: argv.log, write_debug_names: argv.debug});
}
catch (e)
{
console.error (argv.filename+' '+e.message);
process.exit (-1);
}
if (argv.log) console.log (binaryout.log);
var output = argv.output;
if (output === null)
{
output = path.join (path.dirname(argv.filename), path.basename (argv.filename, '.wat')+'.wasm');
}
if (argv.verbose) console.log ('Writing '+output);
try
{
fs.writeFileSync (output, binaryout.buffer);
}
catch (e)
{
console.error (argv.filename+' '+e.message);
}
}).command(['disassemble <filename> [options]', 'd'], 'Disassemble a file', (yargs) => {
yargs.option('output', {
alias: 'o',
default: null,
desc: 'Output file name, default is input file name with the .wat extension'
}).option('debug', {
alias: 'd',
default: false,
desc: 'Load debug information: function and variable names'
}).option('names', {
alias: 'n',
default: false,
desc: 'Generate names for functions and variables'
}).option('inline-export', {
alias: 'e',
default: false,
desc: 'Write inline export'
}).option('fold', {
alias: 'f',
default: false,
desc: 'Folde expresssion'
});
}, (argv) => {
var libwabt = require ('wabt')();
var binary = null;
// console.log (argv.filename);
try
{
binary = fs.readFileSync (argv.filename).toString ();
// console.log (binary);
}
catch (e)
{
console.error (argv.filename+' file not found');
process.exit (-1);
}
let source = null;
// let sourceout = null;
try
{
if (argv.verbose) console.log ('Reading '+argv.filename);
source = libwabt.readWasm(binary, {readDebugNames: argv.debug});
// console.log (source.toText ({}));
if (argv.names)
{
if (argv.verbose) console.log ('Generating names '+argv.filename);
source.generateNames ();
source.applyNames ();
}
// if (argv.verbose) console.log ('Validating '+argv.filename);
// binary.validate ();
// binaryout = binary.toBinary ({log: argv.log, write_debug_names: argv.debug});
}
catch (e)
{
console.error (argv.filename+' '+e.message);
process.exit (-1);
}
console.log (source.toBinary({log: true}).log);
// if (argv.log) console.log (binaryout.log);
var output = argv.output;
if (output !== null)
{
if (output === true)
{
output = path.join (path.dirname(argv.filename), path.basename (argv.filename, '.wasm')+'.wat');
}
if (argv.verbose) console.log ('Writing '+output);
try
{
fs.writeFileSync (output, source.toText ({foldeExprs: argv.fold, inlineExport: argv.exports}));
}
catch (e)
{
console.error (argv.filename+' '+e.message);
}
}
}).command(['run <filename> [options]', 'r'], 'Run a file', (yargs) => {
yargs.option ('import', {
alias: 'i',
type: 'array',
desc: 'Load module (javascript)'
}).option ('memory', {
alias: 'm',
type: 'string',
desc: 'Set memory, ninPages:maxPages'
});
}, async (argv) => {
let memory = null;
if (argv.memory)
{
let mem = argv.memory.split (':');
if (argv.verbose) console.log ('Alocating memory '+parseInt(mem[0])+'/'+parseInt(mem[1]));
memory = new WebAssembly.Memory ({initial: parseInt(mem[0]), maximum: parseInt(mem[1])});
}
if (argv.verbose) console.log ('Loading imports');
let importModules = {};
if (argv.verbose) console.log ('Module io from libraries/io.js');
importModules.io = require ('./libraries/io.js');
if (typeof importModules.io.init === 'function') importModules.io.init (memory);
if (argv.imports)
{
for (let moduleFile of argv.imports)
{
let moduleName = path.basename (moduleFile, 'js');
let moduleFullFile = path.resolve (moduleFile);
try
{
importModules[moduleName] = require (path.resolve (moduleFullFile));
if (typeof importModules[moduleName].init === 'function') importModules[moduleName].init (memory);
if (argv.verbose) console.log ('Module '+moduleName+' from '+moduleFullFile);
}
catch (e)
{
console.error ('Module '+moduleName+' '+e.message);
}
}
}
if (argv.verbose) console.log ('Loading '+argv.filename);
// console.log (WebAssembly);
try
{
let wasm = fs.readFileSync (argv.filename);
let wmodule = await WebAssembly.compile (wasm);
if (!importModules.io) importModules.io = {};
importModules.io.mem = memory;
await new WebAssembly.Instance (wmodule, importModules);
}
catch (e)
{
console.error (argv.filename+' '+e.message);
process.exit (255);
}
})
.option('verbose', {
alias: 'v',
default: false,
desc: 'Print verbose debug'
})
.demandCommand ()
.argv;