@@ -45,8 +45,6 @@ void print_help() {
45
45
fprintf (stdout,
46
46
" --watcompiler Command to compile Wat files to Wasm "
47
47
" binaries (default: wat2wasm)\n " );
48
- fprintf (stdout,
49
- " --file Wasm file (module) to load and execute\n " );
50
48
fprintf (stdout,
51
49
" --no-debug Run without debug thread"
52
50
" (default: false)\n " );
@@ -233,6 +231,31 @@ bool configureSerialPort(int serialPort, const char *argument) {
233
231
return true ;
234
232
}
235
233
234
+ StackValue parseParameter (const char *input, uint8_t value_type) {
235
+ StackValue value = {value_type, {0 }};
236
+ switch (value_type) {
237
+ case I32: {
238
+ value.value .uint32 = std::stoi (input);
239
+ break ;
240
+ }
241
+ case F32: {
242
+ value.value .f32 = std::atof (input);
243
+ break ;
244
+ }
245
+ case I64: {
246
+ value.value .uint64 = std::stoi (input);
247
+ break ;
248
+ }
249
+ case F64: {
250
+ value.value .f64 = std::atof (input);
251
+ break ;
252
+ }
253
+ default :
254
+ FATAL (" wdcli: '%s' should be of type %hhu\n " , input, value_type);
255
+ }
256
+ return value;
257
+ }
258
+
236
259
int main (int argc, const char *argv[]) {
237
260
ARGV_SHIFT (); // Skip command name
238
261
@@ -255,6 +278,13 @@ int main(int argc, const char *argv[]) {
255
278
256
279
if (argc > 0 ) {
257
280
ARGV_GET (file_name);
281
+
282
+ dbg_info (" === LOAD MODULE INTO WARDUINO ===\n " );
283
+ m = load (*wac, file_name,
284
+ {.disable_memory_bounds = false ,
285
+ .mangle_table_index = false ,
286
+ .dlsym_trim_underscore = false ,
287
+ .return_exception = return_exception});
258
288
}
259
289
260
290
// Parse options
@@ -270,8 +300,6 @@ int main(int argc, const char *argv[]) {
270
300
return 0 ;
271
301
} else if (!strcmp (" --loop" , arg)) {
272
302
return_exception = false ;
273
- } else if (!strcmp (" --file" , arg)) {
274
- ARGV_GET (file_name);
275
303
} else if (!strcmp (" --asserts" , arg)) {
276
304
run_tests = true ;
277
305
ARGV_GET (asserts_file);
@@ -293,70 +321,46 @@ int main(int argc, const char *argv[]) {
293
321
ARGV_GET (mode);
294
322
} else if (!strcmp (" --invoke" , arg)) {
295
323
ARGV_GET (fname);
296
- }
297
- }
298
324
299
- if (file_name != nullptr ) {
300
- if (run_tests) {
301
- dbg_info (" === STARTING SPEC TESTS ===\n " );
302
- return run_wasm_test (*wac, file_name, asserts_file, watcompiler);
303
- }
304
- dbg_info (" === LOAD MODULE INTO WARDUINO ===\n " );
305
- m = load (*wac, file_name,
306
- {.disable_memory_bounds = false ,
307
- .mangle_table_index = false ,
308
- .dlsym_trim_underscore = false ,
309
- .return_exception = return_exception});
310
- if (initiallyPaused) {
311
- wac->program_state = WARDUINOpause;
312
- }
313
- }
325
+ // find function
326
+ int fidx = wac->get_export_fidx (m, fname);
327
+ if (fidx < 0 ) {
328
+ fprintf (stderr, " wdcli: no exported function with name '%s'\n " ,
329
+ fname);
330
+ return 1 ;
331
+ }
314
332
315
- if (m != nullptr && fname != nullptr ) {
316
- // consume all arguments for the function passed to the invoke option
317
- int fidx = wac->get_export_fidx (m, fname);
318
- if (fidx < 0 ) {
319
- fprintf (stderr, " wdcli: no exported function with name '%s'\n " ,
320
- fname);
321
- return 1 ;
322
- }
333
+ Block function = m->functions [fidx];
323
334
324
- Block function = m->functions [fidx];
325
- for (uint32_t i = 0 ; i < function.type ->param_count ; ++i) {
326
- StackValue value = {static_cast <uint8_t >(*function.type ->params +
327
- (i * sizeof (uint32_t ))),
328
- {0 }};
329
- switch (value.value_type ) {
330
- case I32: {
331
- value.value .uint32 = std::stoi (argv[0 ]);
332
- break ;
333
- }
334
- case F32: {
335
- value.value .f32 = std::atof (argv[0 ]);
336
- break ;
337
- }
338
- case I64: {
339
- value.value .uint64 = std::stoi (argv[0 ]);
340
- break ;
341
- }
342
- case F64: {
343
- value.value .f64 = std::atof (argv[0 ]);
344
- break ;
335
+ // consume all arguments for the function
336
+ for (uint32_t i = 0 ; i < function.type ->param_count ; ++i) {
337
+ const char *number = nullptr ;
338
+ ARGV_GET (number);
339
+
340
+ if (number[0 ] == ' -' ) {
341
+ FATAL (" wdcli: wrong number of arguments for '%s'\n " , fname);
345
342
}
346
- default :
347
- FATAL ( " incorrect StackValue type \n " );
348
- break ;
343
+
344
+ arguments. push_back ( parseParameter (
345
+ number, *function. type -> params + (i * sizeof ( uint32_t )))) ;
349
346
}
350
- arguments.push_back (value);
351
- ARGV_SHIFT ();
352
347
}
353
348
}
354
349
355
- if (argc != 0 ) {
350
+ if (argc != 0 || file_name == nullptr ) {
356
351
print_help ();
357
352
return 1 ;
358
353
}
359
354
355
+ if (run_tests) {
356
+ dbg_info (" === STARTING SPEC TESTS ===\n " );
357
+ return run_wasm_test (*wac, file_name, asserts_file, watcompiler);
358
+ }
359
+
360
+ if (initiallyPaused) {
361
+ wac->program_state = WARDUINOpause;
362
+ }
363
+
360
364
if (m) {
361
365
m->warduino = wac;
362
366
0 commit comments