-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgauntlet.c
More file actions
143 lines (126 loc) · 3.78 KB
/
gauntlet.c
File metadata and controls
143 lines (126 loc) · 3.78 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
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
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <inttypes.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <time.h>
#include <sys/time.h>
#include <math.h>
#include <fcntl.h>
#include "cutils.h"
#include "mquickjs.h"
static JSValue js_print(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv)
{
int i;
for(i = 0; i < argc; i++) {
if (i != 0) putchar(' ');
if (JS_IsString(ctx, argv[i])) {
JSCStringBuf buf;
const char *str;
size_t len;
str = JS_ToCStringLen(ctx, &len, argv[i], &buf);
fwrite(str, 1, len, stdout);
} else {
JS_PrintValueF(ctx, argv[i], 0);
}
}
putchar('\n');
return JS_UNDEFINED;
}
static JSValue js_gc(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv)
{
JS_GC(ctx);
return JS_UNDEFINED;
}
#if defined(__linux__) || defined(__APPLE__)
static int64_t get_time_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000 + (ts.tv_nsec / 1000000);
}
#else
static int64_t get_time_ms(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (int64_t)tv.tv_sec * 1000 + (tv.tv_usec / 1000);
}
#endif
static JSValue js_date_now(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv)
{
return JS_NewInt64(ctx, get_time_ms());
}
static JSValue js_performance_now(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv)
{
return JS_NewInt64(ctx, get_time_ms());
}
static JSValue js_load(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) { return JS_UNDEFINED; }
static JSValue js_setTimeout(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) { return JS_UNDEFINED; }
static JSValue js_clearTimeout(JSContext *ctx, JSValue *this_val, int argc, JSValue *argv) { return JS_UNDEFINED; }
#include "mqjs_stdlib.h"
static void js_log_func(void *opaque, const void *buf, size_t buf_len)
{
fwrite(buf, 1, buf_len, stdout);
}
int main(int argc, char **argv)
{
size_t mem_size = 32 * 1024;
uint8_t *mem_buf;
JSContext *ctx;
JSValue global_obj, val;
int buf_len;
uint8_t *buf;
const char *filename;
if (argc < 2) {
printf("usage: gauntlet script.js [mem_limit_kb]\n");
return 1;
}
filename = argv[1];
if (argc >= 3) {
mem_size = atoi(argv[2]) * 1024;
}
mem_buf = malloc(mem_size);
ctx = JS_NewContext(mem_buf, mem_size, &js_stdlib);
JS_SetLogFunc(ctx, js_log_func);
FILE *f = fopen(filename, "rb");
if (!f) {
perror(filename);
return 1;
}
fseek(f, 0, SEEK_END);
buf_len = ftell(f);
fseek(f, 0, SEEK_SET);
buf = malloc(buf_len + 1);
fread(buf, 1, buf_len, f);
buf[buf_len] = '\0';
fclose(f);
printf("\033[1;33m--- µGauntlet Initializing (%zu bytes RAM) ---\033[0m\n", mem_size);
val = JS_Eval(ctx, (const char *)buf, buf_len, filename, 0);
if (JS_IsException(val)) {
JSValue ex = JS_GetException(ctx);
printf("\033[1;31m[CRITICAL FAILURE]\033[0m ");
JS_PrintValueF(ctx, ex, JS_DUMP_LONG);
printf("\n");
return 1;
}
// Check if it's a challenge mode (looking for a function named 'solution')
global_obj = JS_GetGlobalObject(ctx);
JSValue solution = JS_GetPropertyStr(ctx, global_obj, "solution");
if (JS_IsFunction(ctx, solution)) {
printf("\033[1;36m[CHALLENGE] Running 'solution' function...\033[0m\n");
// We could run test cases here
// JS_PushArg(ctx, JS_NewString(ctx, "aaabbbccc"));
// JS_PushArg(ctx, solution);
// JS_PushArg(ctx, JS_NULL);
// val = JS_Call(ctx, 1);
// ...
}
JS_FreeContext(ctx);
free(mem_buf);
free(buf);
return 0;
}