Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7639 views
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
5
#include "mujs.h"
6
7
#define PS1 "> "
8
9
static void jsB_gc(js_State *J)
10
{
11
int report = js_toboolean(J, 1);
12
js_gc(J, report);
13
js_pushundefined(J);
14
}
15
16
static void jsB_load(js_State *J)
17
{
18
const char *filename = js_tostring(J, 1);
19
int rv = js_dofile(J, filename);
20
js_pushboolean(J, !rv);
21
}
22
23
static void jsB_print(js_State *J)
24
{
25
unsigned int i, top = js_gettop(J);
26
for (i = 1; i < top; ++i) {
27
const char *s = js_tostring(J, i);
28
if (i > 1) putchar(' ');
29
fputs(s, stdout);
30
}
31
putchar('\n');
32
js_pushundefined(J);
33
}
34
35
static void jsB_write(js_State *J)
36
{
37
unsigned int i, top = js_gettop(J);
38
for (i = 1; i < top; ++i) {
39
const char *s = js_tostring(J, i);
40
if (i > 1) putchar(' ');
41
fputs(s, stdout);
42
}
43
js_pushundefined(J);
44
}
45
46
static void jsB_read(js_State *J)
47
{
48
const char *filename = js_tostring(J, 1);
49
FILE *f;
50
char *s;
51
int n, t;
52
53
f = fopen(filename, "rb");
54
if (!f) {
55
js_error(J, "cannot open file: '%s'", filename);
56
}
57
58
if (fseek(f, 0, SEEK_END) < 0) {
59
fclose(f);
60
js_error(J, "cannot seek in file: '%s'", filename);
61
}
62
63
n = ftell(f);
64
if (n < 0) {
65
fclose(f);
66
js_error(J, "cannot tell in file: '%s'", filename);
67
}
68
69
if (fseek(f, 0, SEEK_SET) < 0) {
70
fclose(f);
71
js_error(J, "cannot seek in file: '%s'", filename);
72
}
73
74
s = malloc(n + 1);
75
if (!s) {
76
fclose(f);
77
js_error(J, "cannot allocate storage for file contents: '%s'", filename);
78
}
79
80
t = fread(s, 1, n, f);
81
if (t != n) {
82
free(s);
83
fclose(f);
84
js_error(J, "cannot read data from file: '%s'", filename);
85
}
86
s[n] = 0;
87
88
js_pushstring(J, s);
89
free(s);
90
fclose(f);
91
}
92
93
static void jsB_readline(js_State *J)
94
{
95
char line[256];
96
int n;
97
if (!fgets(line, sizeof line, stdin))
98
js_error(J, "cannot read line from stdin");
99
n = strlen(line);
100
if (n > 0 && line[n-1] == '\n')
101
line[n-1] = 0;
102
js_pushstring(J, line);
103
}
104
105
static void jsB_quit(js_State *J)
106
{
107
exit(js_tonumber(J, 1));
108
}
109
110
static const char *require_js =
111
"function require(name) {\n"
112
"var cache = require.cache;\n"
113
"if (name in cache) return cache[name];\n"
114
"var exports = {};\n"
115
"cache[name] = exports;\n"
116
"Function('exports', read(name+'.js'))(exports);\n"
117
"return exports;\n"
118
"}\n"
119
"require.cache = Object.create(null);\n"
120
;
121
122
int
123
main(int argc, char **argv)
124
{
125
char line[256];
126
js_State *J;
127
int i;
128
129
J = js_newstate(NULL, NULL, JS_STRICT);
130
131
js_newcfunction(J, jsB_gc, "gc", 0);
132
js_setglobal(J, "gc");
133
134
js_newcfunction(J, jsB_load, "load", 1);
135
js_setglobal(J, "load");
136
137
js_newcfunction(J, jsB_print, "print", 1);
138
js_setglobal(J, "print");
139
140
js_newcfunction(J, jsB_write, "write", 0);
141
js_setglobal(J, "write");
142
143
js_newcfunction(J, jsB_read, "read", 1);
144
js_setglobal(J, "read");
145
146
js_newcfunction(J, jsB_readline, "readline", 0);
147
js_setglobal(J, "readline");
148
149
js_newcfunction(J, jsB_quit, "quit", 1);
150
js_setglobal(J, "quit");
151
152
js_dostring(J, require_js, 0);
153
154
if (argc > 1) {
155
for (i = 1; i < argc; ++i) {
156
if (js_dofile(J, argv[i]))
157
return 1;
158
js_gc(J, 0);
159
}
160
} else {
161
fputs(PS1, stdout);
162
while (fgets(line, sizeof line, stdin)) {
163
js_dostring(J, line, 1);
164
fputs(PS1, stdout);
165
}
166
putchar('\n');
167
js_gc(J, 1);
168
}
169
170
js_freestate(J);
171
172
return 0;
173
}
174
175