-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathjs.cpp
More file actions
116 lines (99 loc) · 3.37 KB
/
js.cpp
File metadata and controls
116 lines (99 loc) · 3.37 KB
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
#include <fstream>
#include <iostream>
#include <string>
#include "builtin.h"
#include "extension-api.h"
#include "config-parser.h"
#include "host_api.h"
#include "wizer.h"
#ifdef MEM_STATS
#include <string>
#endif
extern "C" void __wasm_call_ctors();
api::Engine *engine;
api::Engine* initialize(std::vector<std::string_view> args) {
auto config_parser = starling::ConfigParser();
config_parser.apply_env()->apply_args(args);
return new api::Engine(config_parser.take());
}
static api::Engine *ENGINE = nullptr;
__attribute__((weak))
int main(int argc, const char *argv[]) {
MOZ_ASSERT_UNREACHABLE("main() should not be called");
}
static uint64_t mono_clock_offset = 0;
#define NSECS_PER_SEC 1000000000;
// This overrides wasi-libc's weakly linked implementation of clock_gettime to ensure that
// monotonic clocks really are monotonic, even across resumptions of wizer snapshots.
int clock_gettime(clockid_t clock, timespec * ts) {
__wasi_clockid_t clock_id;
if (clock == CLOCK_REALTIME) {
clock_id = __WASI_CLOCKID_REALTIME;
} else if (clock == CLOCK_MONOTONIC) {
clock_id = __WASI_CLOCKID_MONOTONIC;
} else {
return EINVAL;
}
__wasi_timestamp_t t;
auto errno = __wasi_clock_time_get(clock_id, 1, &t);
if (errno != 0) {
return EINVAL;
}
if (clock == CLOCK_MONOTONIC) {
t += mono_clock_offset;
}
ts->tv_sec = t / NSECS_PER_SEC;
ts->tv_nsec = t % NSECS_PER_SEC;
return 0;
}
void wizen() {
std::string args;
std::getline(std::cin, args);
auto config_parser = starling::ConfigParser();
config_parser.apply_env()->apply_args(args);
auto config = config_parser.take();
config->pre_initialize = true;
ENGINE = new api::Engine(std::move(config));
ENGINE->finish_pre_initialization();
// Ensure that the monotonic clock is always increasing, even across multiple resumptions.
__wasi_timestamp_t t;
MOZ_RELEASE_ASSERT(!__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 1, &t));
if (mono_clock_offset < t) {
mono_clock_offset = t;
}
}
WIZER_INIT(wizen);
extern "C" void __wasm_call_ctors();
/**
* The main entry function for the runtime.
*
* The runtime will be initialized with a configuration derived in the following way:
* 1. If a command line is provided, it will be parsed and used.
* 2. Otherwise, the env var `STARLINGMONKEY_CONFIG` will be split into a command line and used.
* 3. Otherwise, a default configuration is used. In particular, the runtime will attempt to
* load the file `./index.js` and run it as the top-level module script.
*/
extern "C" bool exports_wasi_cli_run_run() {
__wasm_call_ctors();
auto arg_strings = host_api::environment_get_arguments();
std::vector<std::string_view> args;
for (auto& arg : arg_strings) args.push_back(arg);
auto config_parser = starling::ConfigParser();
config_parser.apply_env()->apply_args(args);
ENGINE = new api::Engine(config_parser.take());
return true;
}
/**
* Initialize the runtime with the configuration provided via an environment variable.
*
* This initializer checks for the environment variable `STARLINGMONKEY_CONFIG` and parses
* it as a command line arguments string. The variable not being set is treated as an empty
* command line.
*/
extern "C" bool init_from_environment() {
__wasm_call_ctors();
auto config_parser = starling::ConfigParser();
config_parser.apply_env();
ENGINE = new api::Engine(config_parser.take());
return true;
}