summaryrefslogtreecommitdiff
path: root/weerun.c
blob: dd0105a832cc66b7afdc8688f5d2d4c4f24fcdb9 (plain)
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
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "common.h"
#include "ir.h"
#include "weewasm.h"

extern uint32_t jit_enable;
extern uint32_t jit_check;

// Initialize the runtime functions.
void init_runtime();
void init_libc_runtime();

// Disassembles? and runs a wasm module.
wasm_typed_value_t run(const byte* start,
                       const byte* end,
                       uint32_t num_args,
                       wasm_value_t* args);

// Main function.
// Parses arguments and either runs the tests or runs a file with arguments.
//  -trace: enable tracing to stderr
//  -test: run internal tests
int main(int argc, char* argv[]) {
  for (int i = 1; i < argc; i++) {
    char* arg = argv[i];
    if (strcmp(arg, "-trace") == 0) {
      g_trace = 1;
      continue;
    }
    if (strcmp(arg, "-disassemble") == 0) {
      g_disassemble = 1;
      continue;
    }
    if (strcmp(arg, "-no-jit") == 0) {
      jit_enable = 0;
      continue;
    }
    if (strcmp(arg, "-no-jit-check") == 0) {
      jit_check = 0;
      continue;
    }

    byte* start = NULL;
    byte* end = NULL;
    ssize_t r = load_file(arg, &start, &end);
    if (r >= 0) {
      TRACE("loaded %s: %ld bytes\n", arg, r);
      int num_args = argc - i - 1;
      wasm_value_t* args = NULL;
      if (num_args > 0) {
        args = (wasm_value_t*)malloc(sizeof(wasm_value_t) * num_args);
        for (int j = i + 1; j < argc; j++) {
          int a = j - i - 1;
          args[a] = parse_wasm_value(argv[j]);
          TRACE("args[%d] = ", a);
          //     trace_wasm_value(args[a]);
          TRACE("\n");
        }
      }
      init_runtime();
      init_libc_runtime();
      wasm_typed_value_t result = run(start, end, num_args, args);
      unload_file(&start, &end);
      print_wasm_value(result);
      printf("\n");
      exit(0);
      return 0;
    } else {
      ERR("failed to load: %s\n", arg);
      return 1;
    }
  }
  return 0;
}