/
narwhal.c
323 lines (249 loc) · 8.91 KB
/
narwhal.c
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#define NOTRACE
#include <narwhal.h>
//#ifdef WEBKIT
JSObjectRef JSObjectMakeArray(JSContextRef _context, size_t argc, const JSValueRef argv[], JSValueRef* _exception)
{
JSObjectRef Array = GET_OBJECT(JS_GLOBAL, "Array");
return CALL_AS_CONSTRUCTOR(Array, argc, argv);
}
JSObjectRef JSObjectMakeDate(JSContextRef _context, size_t argc, const JSValueRef argv[], JSValueRef* _exception)
{
JSObjectRef Date = GET_OBJECT(JS_GLOBAL, "Date");
return CALL_AS_CONSTRUCTOR(Date, argc, argv);
}
JSObjectRef JSObjectMakeError(JSContextRef _context, size_t argc, const JSValueRef argv[], JSValueRef* _exception)
{
JSObjectRef Error = GET_OBJECT(JS_GLOBAL, "Error");
return CALL_AS_CONSTRUCTOR(Error, argc, argv);
}
JSObjectRef JSObjectMakeRegExp(JSContextRef _context, size_t argc, const JSValueRef argv[], JSValueRef* _exception)
{
JSObjectRef RegExp = GET_OBJECT(JS_GLOBAL, "RegExp");
return CALL_AS_CONSTRUCTOR(RegExp, argc, argv);
}
//#endif
NarwhalContext narwhal_context;
// Reads a file into a v8 string.
JSStringRef ReadFile(const char* name) {
FILE* file = fopen(name, "rb");
if (file == NULL) return NULL;
fseek(file, 0, SEEK_END);
int size = ftell(file);
rewind(file);
char *chars = (char*)malloc(sizeof(char) * (size + 1));
int i;
for (i = 0; i < size;) {
int read = fread(&chars[i], 1, size - i, file);
i += read;
}
chars[size] = '\0';
fclose(file);
JSStringRef result = JSStringCreateWithUTF8CString(chars);
free(chars);
return result;
}
FUNCTION(NW_print)
{
size_t i;
for (i = 0; i < ARGC; i++) {
JS_Print(ARGV(i));
if (_exception)
return NULL;
}
return JS_undefined;
}
END
FUNCTION(NW_read, ARG_UTF8(path))
{
ARG_COUNT(1);
JSStringRef contents = ReadFile(path);
if (contents) {
JSValueRef result = JSValueMakeString(_context, contents);
JSStringRelease(contents);
if (result)
return result;
}
*_exception = JSValueMakeStringWithUTF8CString(_context, "read() error occurred");
return JS_undefined;
}
END
FUNCTION(NW_isFile, ARG_UTF8(path))
{
ARG_COUNT(1);
struct stat stat_info;
int ret = stat(path, &stat_info);
return JS_bool(ret != -1 && S_ISREG(stat_info.st_mode));
}
END
typedef JSObjectCallAsFunctionCallback factory_t;
typedef const char *(*getModuleName_t)(NarwhalContext *);
FUNCTION(NW_requireNative, ARG_UTF8(topId), ARG_UTF8(path))
{
ARG_COUNT(2)
DEBUG("RequireNative: topId=[%s] path=[%s] \n", topId, path);
void *handle = dlopen(path, RTLD_LOCAL | RTLD_LAZY);
if (handle == NULL) {
printf("dlopen error: %s\n", dlerror());
return JS_null;
}
getModuleName_t narwhalModuleInit = (getModuleName_t)dlsym(handle, "narwhalModuleInit");
if (narwhalModuleInit == NULL) {
fprintf(stderr, "dlsym (getModuleName) error: %s\n", dlerror());
return JS_null;
}
const char *moduleName = narwhalModuleInit(&narwhal_context);
factory_t func = (factory_t)dlsym(handle, moduleName);
if (func == NULL) {
fprintf(stderr, "dlsym (%s) error: %s\n", moduleName, dlerror());
return JS_null;
}
return JS_fn(func);
}
END
JSObjectRef envpToObject(JSGlobalContextRef _context, JSValueRef *_exception, char *envp[])
{
JSObjectRef ENV = JSObjectMake(_context, NULL, NULL);
char *key, *value;
while (key = *(envp++)) {
value = strchr(key, '=') + 1;
*(value - 1) = '\0';
SET_VALUE(ENV, key, JS_str_utf8(value, strlen(value)));
HANDLE_EXCEPTION(true, true);
*(value - 1) = '=';
}
return ENV;
}
JSObjectRef argvToArray(JSGlobalContextRef _context, JSValueRef *_exception, int argc, char *argv[])
{
JSValueRef arguments[argc];
int i;
for (i = 0; i < argc; i++)
arguments[i] = JSValueMakeStringWithUTF8CString(_context, argv[i]);
JSObjectRef ARGS = JSObjectMakeArray(_context, argc, arguments, _exception);
HANDLE_EXCEPTION(true, true);
return ARGS;
}
void* EvaluateREPL(JSContextRef _context, JSStringRef source)
{
static JSStringRef replTag;
if (!replTag)
replTag = JSStringCreateWithUTF8CString("<repl>");
JSValueRef exception = NULL;
JSValueRef *_exception = &exception;
if (JSCheckScriptSyntax(_context, source, 0, 0, _exception) && !(*_exception))
{
JSValueRef value = JSEvaluateScript(_context, source, 0, replTag, 0, _exception);
HANDLE_EXCEPTION(true, false);
if (value && !JSValueIsUndefined(_context, value)) {
JS_Print(value);
HANDLE_EXCEPTION(true, false);
}
}
else
{
HANDLE_EXCEPTION(true, false);
}
}
// The read-eval-execute loop of the shell.
void* RunREPL(JSContextRef _context) {
printf("Narwhal version %s, JavaScriptCore engine\n", NARWHAL_VERSION);
while (true)
{
#ifdef JSCOCOA
NSAutoreleasePool *pool = [NSAutoreleasePool new];
#endif
char *str = readline("> ");
if (str && *str)
add_history(str);
if (!str)
break;
JSStringRef source = JSStringCreateWithUTF8CString(str);
free(str);
LOCK();
EvaluateREPL(_context, source);
JSStringRelease(source);
UNLOCK();
#ifdef JSCOCOA
[pool drain];
#endif
}
printf("\n");
}
JSValueRef narwhal_wrapped(JSGlobalContextRef _context, JSValueRef *_exception, int argc, char *argv[], char *envp[], int runShell)
{
pthread_mutex_t _mutex = PTHREAD_MUTEX_INITIALIZER;
narwhal_context.context = _context;
narwhal_context.mutex = &_mutex;
LOCK();
// TODO: cleanup all this. strcpy, sprintf, etc BAD!
char executable[1024];
char buffer[1024];
// start with the executable name from argv[0]
strcpy(executable, argv[0]);
// follow any symlinks
size_t len;
while ((int)(len = readlink(executable, buffer, sizeof(buffer))) >= 0) {
buffer[len] = '\0';
// make relative to symlink's directory
if (buffer[0] != '/') {
char tmp[1024];
strcpy(tmp, buffer);
sprintf(buffer, "%s/%s", (char *)dirname(executable), tmp);
}
strcpy(executable, buffer);
}
// make absolute
if (executable[0] != '/') {
char tmp[1024], tmp2[1024];
getcwd(tmp, sizeof(tmp));
sprintf(buffer, "%s/%s", tmp, executable);
strcpy(executable, buffer);
}
char NARWHAL_HOME[1024], NARWHAL_ENGINE_HOME[1024];
// try getting NARWHAL_ENGINE_HOME from env variable. fall back to 2nd ancestor of executable path
if (getenv("NARWHAL_ENGINE_HOME"))
strcpy(NARWHAL_ENGINE_HOME, getenv("NARWHAL_ENGINE_HOME"));
else
strcpy(NARWHAL_ENGINE_HOME, (char *)dirname((char *)dirname(executable)));
// try getting NARWHAL_HOME from env variable. fall back to 2nd ancestor of NARWHAL_ENGINE_HOME
if (getenv("NARWHAL_HOME"))
strcpy(NARWHAL_HOME, getenv("NARWHAL_HOME"));
else
strcpy(NARWHAL_HOME, (char *)dirname((char *)dirname(NARWHAL_ENGINE_HOME)));
JSObjectRef ARGS = CALL(argvToArray, argc, argv);
JSObjectRef ENV = CALL(envpToObject, envp);
SET_VALUE(ENV, "NARWHAL_HOME", JS_str_utf8(NARWHAL_HOME, strlen(NARWHAL_HOME)));
SET_VALUE(ENV, "NARWHAL_ENGINE_HOME", JS_str_utf8(NARWHAL_ENGINE_HOME, strlen(NARWHAL_ENGINE_HOME)));
JSObjectRef global = JS_GLOBAL;
SET_VALUE(global, "print", JS_fn(NW_print));
SET_VALUE(global, "isFile", JS_fn(NW_isFile));
SET_VALUE(global, "read", JS_fn(NW_read));
SET_VALUE(global, "requireNative", JS_fn(NW_requireNative));
SET_VALUE(global, "ARGS", ARGS);
SET_VALUE(global, "ENV", ENV);
// Load bootstrap.js
char *bootstrapPathRelative = "/bootstrap.js";
char bootstrapPathFull[strlen(NARWHAL_ENGINE_HOME) + strlen(bootstrapPathRelative) + 1];
snprintf(bootstrapPathFull, sizeof(bootstrapPathFull), "%s%s", NARWHAL_ENGINE_HOME, bootstrapPathRelative);
JSStringRef bootstrapSource = ReadFile(bootstrapPathFull);
if (!bootstrapSource) {
THROW("Error reading bootstrap.js\n");
}
JSStringRef bootstrapTag = JSStringCreateWithUTF8CString(bootstrapPathFull);
JSEvaluateScript(_context, bootstrapSource, 0, bootstrapTag, 0, _exception);
if (*_exception) {
JS_Print(*_exception);
}
JSStringRelease(bootstrapSource);
JSStringRelease(bootstrapTag);
UNLOCK();
if (!*_exception && argc <= 1 && runShell)
RunREPL(_context);
}
int narwhal(JSGlobalContextRef _context, JSValueRef *_exception, int argc, char *argv[], char *envp[], int runShell)
{
narwhal_wrapped(_context, _exception, argc, argv, envp, runShell);
if (*_exception)
return 1;
return 0;
}