Skip to content

Commit 875cc27

Browse files
committed
Fix: module needs to be loaded to get parameters for invoked function
Removes the `--file` option Fix wrong number of arguments check
1 parent 535dcb8 commit 875cc27

File tree

2 files changed

+61
-57
lines changed

2 files changed

+61
-57
lines changed

platforms/CLI-Emulator/main.cpp

+60-56
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ void print_help() {
4545
fprintf(stdout,
4646
" --watcompiler Command to compile Wat files to Wasm "
4747
"binaries (default: wat2wasm)\n");
48-
fprintf(stdout,
49-
" --file Wasm file (module) to load and execute\n");
5048
fprintf(stdout,
5149
" --no-debug Run without debug thread"
5250
"(default: false)\n");
@@ -233,6 +231,31 @@ bool configureSerialPort(int serialPort, const char *argument) {
233231
return true;
234232
}
235233

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+
236259
int main(int argc, const char *argv[]) {
237260
ARGV_SHIFT(); // Skip command name
238261

@@ -255,6 +278,13 @@ int main(int argc, const char *argv[]) {
255278

256279
if (argc > 0) {
257280
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});
258288
}
259289

260290
// Parse options
@@ -270,8 +300,6 @@ int main(int argc, const char *argv[]) {
270300
return 0;
271301
} else if (!strcmp("--loop", arg)) {
272302
return_exception = false;
273-
} else if (!strcmp("--file", arg)) {
274-
ARGV_GET(file_name);
275303
} else if (!strcmp("--asserts", arg)) {
276304
run_tests = true;
277305
ARGV_GET(asserts_file);
@@ -293,70 +321,46 @@ int main(int argc, const char *argv[]) {
293321
ARGV_GET(mode);
294322
} else if (!strcmp("--invoke", arg)) {
295323
ARGV_GET(fname);
296-
}
297-
}
298324

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+
}
314332

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];
323334

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);
345342
}
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))));
349346
}
350-
arguments.push_back(value);
351-
ARGV_SHIFT();
352347
}
353348
}
354349

355-
if (argc != 0) {
350+
if (argc != 0 || file_name == nullptr) {
356351
print_help();
357352
return 1;
358353
}
359354

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+
360364
if (m) {
361365
m->warduino = wac;
362366

tests/integration/run_spec_tests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def main():
9797
base = "core/" + "".join(os.path.basename(filename).split(".")[:-2])
9898
print(base)
9999
status = subprocess.run(
100-
[args.interpreter, "--file", base + ".wast", "--asserts", base + ".asserts.wast", "--watcompiler", args.compiler],
100+
[args.interpreter, base + ".wast", "--asserts", base + ".asserts.wast", "--watcompiler", args.compiler],
101101
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
102102
if status.returncode == 0:
103103
print(f"{filename}: All tests passed.\n")

0 commit comments

Comments
 (0)