-
Notifications
You must be signed in to change notification settings - Fork 238
/
xsPlatforms.c
592 lines (525 loc) · 13 KB
/
xsPlatforms.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
* Copyright (c) 2016-2017 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Runtime.
*
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright (C) 2010-2016 Marvell International Ltd.
* Copyright (C) 2002-2010 Kinoma, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "xsAll.h"
#include "xsScript.h"
/* old programming interface defaults to 0 */
#ifndef mxUseDefaultMachinePlatform
#define mxUseDefaultMachinePlatform 0
#endif
#ifndef mxUseDefaultBuildKeys
#define mxUseDefaultBuildKeys 0
#endif
#ifndef mxUseDefaultChunkAllocation
#define mxUseDefaultChunkAllocation 0
#endif
#ifndef mxUseDefaultSlotAllocation
#define mxUseDefaultSlotAllocation 0
#endif
#ifndef mxUseDefaultHostCollection
#define mxUseDefaultHostCollection 0
#endif
#ifndef mxUseDefaultFindModule
#define mxUseDefaultFindModule 0
#endif
#ifndef mxUseDefaultLoadModule
#define mxUseDefaultLoadModule 0
#endif
#ifndef mxUseDefaultParseScript
#define mxUseDefaultParseScript 0
#endif
#ifndef mxUseDefaultQueuePromiseJobs
#define mxUseDefaultQueuePromiseJobs 0
#endif
#ifndef mxUseDefaultDebug
#define mxUseDefaultDebug 0
#endif
#if mxUseDefaultFindModule
static txBoolean fxFindPreparation(txMachine* the, txSlot* realm, txString path, txID* id);
#endif
#ifdef mxParse
#if mxUseDefaultFindModule
static txBoolean fxFindScript(txMachine* the, txString path, txID* id);
#endif
#if mxUseDefaultLoadModule
extern txScript* fxLoadScript(txMachine* the, txString path, txUnsigned flags);
#endif
#endif
#if mxUseDefaultMachinePlatform
void fxCreateMachinePlatform(txMachine* the)
{
}
void fxDeleteMachinePlatform(txMachine* the)
{
}
#endif /* mxUseDefaultMachinePlatform */
#if mxUseDefaultBuildKeys
void fxBuildKeys(txMachine* the)
{
int i;
for (i = 0; i < XS_SYMBOL_ID_COUNT; i++) {
txID id = the->keyIndex;
txSlot* description = fxNewSlot(the);
fxCopyStringC(the, description, gxIDStrings[i]);
the->keyArray[id] = description;
the->keyIndex++;
}
for (; i < XS_ID_COUNT; i++) {
fxID(the, gxIDStrings[i]);
}
}
#endif /* mxUseDefaultBuildKeys */
#if mxUseDefaultChunkAllocation
void* fxAllocateChunks(txMachine* the, txSize theSize)
{
return c_malloc(theSize);
}
void fxFreeChunks(txMachine* the, void* theChunks)
{
c_free(theChunks);
}
#endif /* mxUseDefaultChunkAllocation */
#if mxUseDefaultSlotAllocation
txSlot* fxAllocateSlots(txMachine* the, txSize theCount)
{
return(txSlot*)c_malloc(theCount * sizeof(txSlot));
}
void fxFreeSlots(txMachine* the, void* theSlots)
{
c_free(theSlots);
}
#endif /* mxUseDefaultSlotAllocation */
#if mxUseDefaultHostCollection
void fxMarkHost(txMachine* the, txMarkRoot markRoot)
{
}
void fxSweepHost(txMachine* the)
{
}
#endif /* mxUseDefaultHostCollection */
#if mxUseDefaultFindModule
txID fxFindModule(txMachine* the, txSlot* realm, txID moduleID, txSlot* slot)
{
txPreparation* preparation = the->preparation;
char name[C_PATH_MAX];
char path[C_PATH_MAX];
txBoolean absolute = 0, relative = 0, search = 0;
txInteger dot = 0;
txString slash;
txID id;
if (preparation)
fxToStringBuffer(the, slot, name, sizeof(name) - preparation->baseLength - 4);
else
fxToStringBuffer(the, slot, name, sizeof(name));
if (name[0] == '/') {
absolute = 1;
}
else if ((name[0] == '.') && (name[1] == '/')) {
dot = 1;
relative = 1;
}
else if ((name[0] == '.') && (name[1] == '.') && (name[2] == '/')) {
dot = 2;
relative = 1;
}
#if mxWindows
else if (('A' <= name[0]) && (name[0] <= 'Z') && (name[1] == ':') && (name[2] == '\\')) {
absolute = 1;
}
#endif
else {
relative = 1;
search = 1;
}
#if mxWindows
{
char c;
slash = name;
while ((c = *slash)) {
if (c == '/')
*slash = '\\';
slash++;
}
}
#endif
if (absolute) {
if (preparation) {
c_strcpy(path, preparation->base);
c_strcat(path, name + 1);
c_strcat(path, ".xsb");
if (fxFindPreparation(the, realm, path, &id))
return id;
}
#ifdef mxParse
if (fxFindScript(the, name, &id))
return id;
#endif
}
if (relative && (moduleID != XS_NO_ID)) {
c_strcpy(path, fxGetKeyName(the, moduleID));
slash = c_strrchr(path, mxSeparator);
if (!slash)
return XS_NO_ID;
if (dot == 0)
slash++;
else if (dot == 2) {
*slash = 0;
slash = c_strrchr(path, mxSeparator);
if (!slash)
return XS_NO_ID;
}
if (preparation) {
if (!c_strncmp(path, preparation->base, preparation->baseLength)) {
*slash = 0;
c_strcat(path, name + dot);
c_strcat(path, ".xsb");
if (fxFindPreparation(the, realm, path, &id))
return id;
}
}
#ifdef mxParse
*slash = 0;
c_strcat(path, name + dot);
if (fxFindScript(the, path, &id))
return id;
#else
#ifdef mxDebug
if (!c_strncmp(path, "xsbug://", 8)) {
*slash = 0;
c_strcat(path, name + dot);
return fxNewNameC(the, path);
}
#endif
#endif
}
if (search) {
if (preparation) {
txSlot* slot = mxAvailableModules(realm);
slot = slot->value.reference->next;
while (slot) {
txSlot* key = fxGetKey(the, slot->ID);
if (key && !c_strcmp(key->value.key.string, name))
return slot->value.symbol;
slot = slot->next;
}
}
}
return XS_NO_ID;
}
txBoolean fxFindPreparation(txMachine* the, txSlot* realm, txString path, txID* id)
{
txID result = fxFindName(the, path);
txSlot* slot = mxAvailableModules(realm)->value.reference->next;
while (slot) {
if (slot->value.symbol == result) {
*id = result;
return 1;
}
slot = slot->next;
}
*id = XS_NO_ID;
return 0;
}
#ifdef mxParse
txBoolean fxFindScript(txMachine* the, txString path, txID* id)
{
txString slash;
txString dot;
char real[C_PATH_MAX];
slash = c_strrchr(path, mxSeparator);
if (!slash)
slash = path;
dot = c_strrchr(slash, '.');
if (!dot)
c_strcat(path, ".js");
if (c_realpath(path, real)) {
*id = fxNewNameC(the, real);
return 1;
}
return 0;
}
#endif
#endif /* mxUseDefaultFindModule */
#if mxUseDefaultLoadModule
extern void fxDebugImport(txMachine* the, txString path);
void fxLoadModule(txMachine* the, txSlot* realm, txID moduleID)
{
char path[C_PATH_MAX];
txByte* code;
txSize size;
c_strcpy(path, fxGetKeyName(the, moduleID));
code = fxGetArchiveCode(the, path, &size);
if (code) {
txScript script;
script.callback = NULL;
script.symbolsBuffer = NULL;
script.symbolsSize = 0;
script.codeBuffer = code;
script.codeSize = size;
script.hostsBuffer = NULL;
script.hostsSize = 0;
script.path = path;
script.version[0] = XS_MAJOR_VERSION;
script.version[1] = XS_MINOR_VERSION;
script.version[2] = XS_PATCH_VERSION;
script.version[3] = 0;
fxResolveModule(the, realm, moduleID, &script, C_NULL, C_NULL);
}
else {
txPreparation* preparation = the->preparation;
if (preparation) {
txInteger c = preparation->scriptCount;
txScript* script = preparation->scripts;
while (c > 0) {
if (!c_strcmp(path + preparation->baseLength, script->path)) {
fxResolveModule(the, realm, moduleID, script, C_NULL, C_NULL);
return;
}
c--;
script++;
}
}
}
#ifdef mxParse
{
#ifdef mxDebug
txUnsigned flags = mxDebugFlag;
#else
txUnsigned flags = 0;
#endif
txScript* script = fxLoadScript(the, path, flags);
if (script)
fxResolveModule(the, realm, moduleID, script, C_NULL, C_NULL);
}
#else
#ifdef mxDebug
{
if (!c_strncmp(path, "xsbug://", 8))
fxDebugImport(the, path);
}
#endif
#endif
}
#ifdef mxParse
txScript* fxLoadScript(txMachine* the, txString path, txUnsigned flags)
{
txParser _parser;
txParser* parser = &_parser;
txParserJump jump;
FILE* file = NULL;
txString name = NULL;
char map[C_PATH_MAX];
txScript* script = NULL;
fxInitializeParser(parser, the, the->parserBufferSize, the->parserTableModulo);
parser->firstJump = &jump;
file = fopen(path, "r");
if (c_setjmp(jump.jmp_buf) == 0) {
mxParserThrowElse(file);
parser->path = fxNewParserSymbol(parser, path);
fxParserTree(parser, file, (txGetter)fgetc, flags, &name);
fclose(file);
file = NULL;
if (name) {
txString slash = c_strrchr(path, mxSeparator);
if (slash) *slash = 0;
c_strcat(path, name);
mxParserThrowElse(c_realpath(path, map));
parser->path = fxNewParserSymbol(parser, map);
file = fopen(map, "r");
mxParserThrowElse(file);
fxParserSourceMap(parser, file, (txGetter)fgetc, flags, &name);
fclose(file);
file = NULL;
if (slash) *slash = 0;
c_strcat(path, name);
mxParserThrowElse(c_realpath(path, map));
parser->path = fxNewParserSymbol(parser, map);
}
fxParserHoist(parser);
fxParserBind(parser);
script = fxParserCode(parser);
}
if (file)
fclose(file);
#ifdef mxInstrument
if (the->peakParserSize < parser->total)
the->peakParserSize = parser->total;
#endif
fxTerminateParser(parser);
return script;
}
#endif
#endif /* mxUseDefaultLoadModule */
#if mxUseDefaultParseScript
txScript* fxParseScript(txMachine* the, void* stream, txGetter getter, txUnsigned flags)
{
txParser _parser;
txParser* parser = &_parser;
txParserJump jump;
txScript* script = NULL;
fxInitializeParser(parser, the, the->parserBufferSize, the->parserTableModulo);
parser->firstJump = &jump;
if (c_setjmp(jump.jmp_buf) == 0) {
fxParserTree(parser, stream, getter, flags, NULL);
fxParserHoist(parser);
fxParserBind(parser);
script = fxParserCode(parser);
}
#ifdef mxInstrument
if (the->peakParserSize < parser->total)
the->peakParserSize = parser->total;
#endif
fxTerminateParser(parser);
return script;
}
#endif /* mxUseDefaultParseScript */
#if mxUseDefaultQueuePromiseJobs
void fxQueuePromiseJobs(txMachine* the)
{
mxUnknownError("promise: no queue");
}
#endif /* mxUseDefaultQueuePromiseJobs */
#if mxUseDefaultDebug
void fxAbort(txMachine* the, int status)
{
c_exit(status);
}
#endif /* mxUseDefaultDebug */
#ifdef mxDebug
#if mxUseDefaultDebug
void fxAbort(txMachine* the, int status)
{
mxTry(the) {
if (status == XS_NOT_ENOUGH_MEMORY_EXIT)
mxUnknownError("not enough memory");
else if (status == XS_STACK_OVERFLOW_EXIT)
mxUnknownError("stack overflow");
else if (status == XS_DEAD_STRIP_EXIT)
mxUnknownError("dead strip");
}
mxCatch(the) {
}
c_exit(status);
}
void fxConnect(txMachine* the)
{
}
void fxDisconnect(txMachine* the)
{
}
txBoolean fxIsConnected(txMachine* the)
{
return 0;
}
txBoolean fxIsReadable(txMachine* the)
{
return 0;
}
void fxReceive(txMachine* the)
{
}
void fxSend(txMachine* the, txBoolean more)
{
}
#endif /* mxUseDefaultDebug */
#endif
#if mxWindows || mxMacOSX || mxLinux || mxWasm
uint32_t modMilliseconds()
{
c_timeval tv;
c_gettimeofday(&tv, NULL);
#if (mxWasm || mxWindows)
return (uint32_t)(uint64_t)(((double)(tv.tv_sec) * 1000.0) + ((double)(tv.tv_usec) / 1000.0));
#else
return (uint32_t)(((double)(tv.tv_sec) * 1000.0) + ((double)(tv.tv_usec) / 1000.0));
#endif
}
#endif
#if mxWindows
#if _MSC_VER < 1800
unsigned long c_nan[2]={0xffffffff, 0x7fffffff};
unsigned long c_infinity[2]={0x00000000, 0x7ff00000};
int c_fpclassify(double x)
{
int result = FP_NORMAL;
switch (_fpclass(x)) {
case _FPCLASS_SNAN:
case _FPCLASS_QNAN:
result = FP_NAN;
break;
case _FPCLASS_NINF:
case _FPCLASS_PINF:
result = FP_INFINITE;
break;
case _FPCLASS_NZ:
case _FPCLASS_PZ:
result = FP_ZERO;
break;
case _FPCLASS_ND:
case _FPCLASS_PD:
result = FP_SUBNORMAL;
break;
}
return result;
}
#endif /* _MSC_VER < 1800 */
int c_gettimeofday(c_timeval *tp, struct c_timezone *tzp)
{
struct _timeb tb;
_ftime(&tb);
if (tp != 0) {
tp->tv_sec = (long)tb.time;
tp->tv_usec = tb.millitm * 1000;
}
if (tzp != 0) {
tzp->tz_minuteswest = tb.timezone;
tzp->tz_dsttime = tb.dstflag;
}
return (0);
}
#ifdef mxParse
char *c_realpath(const char *path, char *real)
{
if (_fullpath(real, path, C_PATH_MAX) != NULL) {
DWORD attributes = GetFileAttributes(real);
if (attributes != 0xFFFFFFFF) {
return real;
}
}
return C_NULL;
}
#endif
#endif