-
Notifications
You must be signed in to change notification settings - Fork 9
/
lo.h
233 lines (202 loc) · 8.43 KB
/
lo.h
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#pragma once
#include <v8.h>
#include <libplatform/libplatform.h>
#include <map>
#include <v8-fast-api-calls.h>
#include <unistd.h>
#include <sys/mount.h>
#include <fcntl.h>
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
namespace lo {
// structs for passing typed arrays & strings in and out of v8 fast api calls
struct FastApiTypedArray {
uintptr_t length_;
void* data;
};
struct FastOneByteString {
const char* data;
uint32_t length;
};
struct FastApiArrayBuffer {
void* data;
size_t byte_length;
};
// struct for builtin JS and text files that have been linked into the runtime
struct builtin {
unsigned int size;
const char* source;
};
// enum for types of JS that can be loaded/compiled by v8 platform
enum ScriptType : int {
kScript,
kModule,
kFunction,
};
// enum used for passing options to V8 compiler when initialising modules
enum HostDefinedOptions : int {
kType = 8,
kID = 9,
kLength = 10,
};
// typedef and V8 callback for module registration
typedef void *(*register_plugin)();
using InitializerCallback = void (*)(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> exports);
// enum of v8 fast api parameter and return types
enum FastTypes: int {
i8 = 1, i16 = 2, i32 = 3, u8 = 4, u16 = 5, u32 = 6, empty = 7, f32 = 8,
f64 = 9, u64 = 10, i64 = 11, iSize = 12, uSize = 13, pointer = 14,
buffer = 15, function = 16, u32array = 17, boolean = 18, string = 19
};
// v8 callbacks
// callback for heap limit reached
size_t nearHeapLimitCallback(void* data, size_t current_heap_limit,
size_t initial_heap_limit);
// declare the callback function for loading ES modules
v8::MaybeLocal<v8::Module> OnModuleInstantiate(v8::Local<v8::Context> context,
v8::Local<v8::String> specifier, v8::Local<v8::FixedArray> import_assertions,
v8::Local<v8::Module> referrer);
// helpers for adding properties and methods to JS object templates
void SET_PROP(v8::Isolate *isolate, v8::Local<v8::ObjectTemplate>
recv, const char *name, v8::FunctionCallback getter,
v8::FunctionCallback setter);
void SET_METHOD(v8::Isolate *isolate, v8::Local<v8::ObjectTemplate>
recv, const char *name, v8::FunctionCallback callback);
void SET_MODULE(v8::Isolate *isolate, v8::Local<v8::ObjectTemplate>
recv, const char *name, v8::Local<v8::ObjectTemplate> module);
void SET_VALUE(v8::Isolate *isolate, v8::Local<v8::ObjectTemplate>
recv, const char *name, v8::Local<v8::Value> value);
void SET_FAST_METHOD(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> exports,
const char * name, v8::CFunction* fastCFunc, v8::FunctionCallback slowFunc);
void SET_FAST_PROP(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> exports,
const char * name, v8::CFunction* fastGetter, v8::FunctionCallback slowGetter,
v8::CFunction* fastSetter, v8::FunctionCallback slowSetter);
// internal API - on the lo:: namespace so can be used from other modules
uint64_t hrtime();
void builtins_add (const char* name, const char* source,
unsigned int size);
void modules_add (const char* name, register_plugin plugin_handler);
void Setup(
int* argc,
char** argv,
const char* v8flags,
int v8_threads,
int v8flags_from_commandline);
int CreateIsolate(int argc, char** argv,
const char* main, unsigned int main_len,
const char* js, unsigned int js_len, char* buf, int buflen, int fd,
uint64_t start, const char* globalobj, const char* scriptname,
int cleanup, int onexit, void* startup_data);
int CreateIsolate(int argc, char** argv,
const char* main, unsigned int main_len, uint64_t start,
const char* globalobj, int cleanup, int onexit, void* startup_data);
void PrintStackTrace(v8::Isolate* isolate, const v8::TryCatch& try_catch);
void PromiseRejectCallback(v8::PromiseRejectMessage message);
void FreeMemory(void* buf, size_t length, void* data);
// external JS api - these are bound to the "lo" object on JS global
void Print(const v8::FunctionCallbackInfo<v8::Value> &args);
void Builtin(const v8::FunctionCallbackInfo<v8::Value> &args);
void Builtins(const v8::FunctionCallbackInfo<v8::Value> &args);
void Library(const v8::FunctionCallbackInfo<v8::Value> &args);
void Libraries(const v8::FunctionCallbackInfo<v8::Value> &args);
void LoadModule(const v8::FunctionCallbackInfo<v8::Value> &args);
void EvaluateModule(const v8::FunctionCallbackInfo<v8::Value> &args);
void SetModuleCallbacks(const v8::FunctionCallbackInfo<v8::Value> &args);
void NextTick(const v8::FunctionCallbackInfo<v8::Value> &args);
void RegisterCallback(const v8::FunctionCallbackInfo<v8::Value> &args);
void RunMicroTasks(const v8::FunctionCallbackInfo<v8::Value> &args);
void PumpMessageLoop(const v8::FunctionCallbackInfo<v8::Value> &args);
void Latin1Decode(const v8::FunctionCallbackInfo<v8::Value> &args);
void Utf8Decode(const v8::FunctionCallbackInfo<v8::Value> &args);
void Utf8Encode(const v8::FunctionCallbackInfo<v8::Value> &args);
void RunScript(const v8::FunctionCallbackInfo<v8::Value> &args);
void SetFlags(const v8::FunctionCallbackInfo<v8::Value> &args);
void Arch(const v8::FunctionCallbackInfo<v8::Value> &args);
void Os(const v8::FunctionCallbackInfo<v8::Value> &args);
void Exit(const v8::FunctionCallbackInfo<v8::Value> &args);
void WrapMemory(const v8::FunctionCallbackInfo<v8::Value> &args);
void UnWrapMemory(const v8::FunctionCallbackInfo<v8::Value> &args);
void GetMeta(const v8::FunctionCallbackInfo<v8::Value> &args);
void HeapUsage(const v8::FunctionCallbackInfo<v8::Value> &args);
// fast api methods
void GetAddress(const v8::FunctionCallbackInfo<v8::Value> &args);
void fastGetAddress(void* p, struct FastApiTypedArray* const p_buf,
struct FastApiTypedArray* const p_ret);
void Utf8EncodeInto(const v8::FunctionCallbackInfo<v8::Value> &args);
int32_t fastUtf8EncodeInto (void* p, struct FastOneByteString* const p_str, struct FastApiTypedArray* const p_buf);
void Utf8EncodeIntoPtr(const v8::FunctionCallbackInfo<v8::Value> &args);
int32_t fastUtf8EncodeIntoPtr (void* p, struct FastOneByteString* const p_str, void* p_buf);
void Utf8EncodeIntoAtOffset(const v8::FunctionCallbackInfo<v8::Value> &args);
int32_t fastUtf8EncodeIntoAtOffset (void* p, struct FastOneByteString* const p_str, struct FastApiTypedArray* const p_buf, uint32_t off);
void Utf8Length(const v8::FunctionCallbackInfo<v8::Value> &args);
int32_t fastUtf8Length (void* p, struct FastOneByteString* const p_ret);
void HRTime(const v8::FunctionCallbackInfo<v8::Value> &args);
void fastHRTime (void* p, struct FastApiTypedArray* const p_ret);
void ReadMemory(const v8::FunctionCallbackInfo<v8::Value> &args);
void fastReadMemory (void* p, struct FastApiTypedArray* const p_buf, void* start, uint32_t size);
void ReadMemoryAtOffset(const v8::FunctionCallbackInfo<v8::Value> &args);
void fastReadMemoryAtOffset (void* p, struct FastApiTypedArray* const p_buf, void* start, uint32_t size, uint32_t off);
// fast api properties
void GetErrno(const v8::FunctionCallbackInfo<v8::Value> &args);
int fastGetErrno(void* p);
void SetErrno(const v8::FunctionCallbackInfo<v8::Value> &args);
void fastSetErrno (void* p, int32_t e);
// Module Initialization
void Init(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> target);
}
#ifdef __cplusplus
extern "C"
{
#endif
struct isolate_context {
int rc;
int argc;
int fd;
int buflen;
int cleanup;
int onexit;
unsigned int main_len;
unsigned int js_len;
uint64_t start;
char** argv;
char* main;
char* js;
char* buf;
char* globalobj;
char* scriptname;
void* startup_data;
void* isolate;
};
int lo_create_isolate (int argc, char** argv,
const char* main, unsigned int main_len,
const char* js, unsigned int js_len, char* buf, int buflen, int fd,
uint64_t start, const char* globalobj, const char* scriptname,
int cleanup, int onexit, void* startup_data);
int lo_context_size ();
void lo_create_isolate_context (int argc, char** argv,
const char* main, unsigned int main_len,
const char* js, unsigned int js_len, char* buf, int buflen, int fd,
uint64_t start, const char* globalobj, const char* scriptname,
int cleanup, int onexit, void* startup_data, struct isolate_context* ctx);
void lo_start_isolate (void* ptr);
void lo_destroy_isolate_context (struct isolate_context* ctx);
struct exec_info {
v8::Global<v8::Function> js_fn;
v8::Isolate* isolate;
uint64_t rv;
int nargs;
};
struct callback_state {
volatile int current = 0;
int max_contexts = 0;
exec_info** contexts;
};
void lo_callback (exec_info* info);
void lo_async_callback (exec_info* info, callback_state* state);
void lo_shutdown (int cleanup);
#ifdef __cplusplus
}
#endif