-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjscpprt.cpp
More file actions
662 lines (596 loc) · 12.5 KB
/
jscpprt.cpp
File metadata and controls
662 lines (596 loc) · 12.5 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
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#include "windows.h"
#include <stdio.h>
#include "jscpprt.h"
#include <exception>
#include <limits>
#include <math.h>
#include <time.h>
#include <sys\timeb.h>
value_ global_(new obj_);
value_ true_(true);
value_ false_(false);
value_ undefined; // the prototypical undefined variable
static value_ NaN(std::numeric_limits<double>::quiet_NaN());
char *pzAppTitle_;
class TypeError : public exception {
public:
virtual const char* what() const throw() { return "TypeError"; }
};
class incomp_operand : public exception {
public:
virtual const char *what() const throw() { return "incompatible operand"; }
};
class bad_alloc : public exception {
public:
virtual const char *what() const throw() { return "memory allocation failed"; }
};
class not_imp : public exception {
public:
virtual const char *what() const throw() { return "unimplemented feature"; }
};
/////////////////////////////////////////////////////////////////////
// Objects
obj_::~obj_()
{
prop* p = props;
while (p) {
prop* p2 = p->next;
delete p;
p = p2;
}
}
value_ obj_::dot(const char* id)
// search parents, always return a value
{
prop* p = props;
while (p && p->id!=id) {
p = p->next;
}
if (p) {
return p->value;
}
// TODO: search parents!
return undefined;
}
value_ obj_::at(value_ x)
{
const char* id = x;
prop* p = props;
while (p && strcmp(p->id,id)) {
p = p->next;
}
if (p) {
return p->value;
}
// TODO: search up the prototype chain!
return undefined;
}
value_& obj_::dotref(const char* id)
// search this obj only, add new prop if necessary
// assumes id has been made address-unique!
{
prop* p = props;
while (p && p->id!=id) {
p = p->next;
}
if (p) {
return p->value;
}
p = new prop;
if (!p) {
throw bad_alloc();
}
p->id = id;
p->value = undefined;
p->next = props;
props = p;
return p->value;
}
value_& obj_::atref(value_ x)
// index into this object
{
// convert to string rep:
const char* id = x;
prop* p = props;
while (p && strcmp(p->id,id)!=0) {
p = p->next;
}
if (p) {
return p->value;
}
p = new prop;
if (!p) {
throw bad_alloc();
}
p->id = strdup(id);
p->value = undefined;
p->next = props;
props = p;
return p->value;
}
/////////////////////////////////////////////////////////////////////
// Functions
value_::value_(func_* pfunc)
{
t = TFUNC;
v.f = pfunc;
pfunc->atref("length") = pfunc->length;
}
/////////////////////////////////////////////////////////////////////
// Arrays
// constructor body
class Array_class_ : public func_ {
public:
Array_class_() { length = 0; }
virtual value_ call(value_ this_, int nargs, ...)
{
this_ = value_(new array_());
return this_;
}
};
// constructor
value_ Array(new Array_class_);
value_ MakeArray_(int len, ...)
{
array_* a = new array_();
if (len) {
value_* arg = (value_*)(&len+1);
for (int i = 0; i < len; i++) {
a->atref(i) = arg[i];
}
}
return a;
}
const char* array_::toString(void)
{
if (flags & VALMAP) {
// not implemented yet
throw not_imp();
}
value_* vector = (value_*)pdata;
if (0==len) {
return "";
}
if (1==len) {
// single element, just render that
return vector[0].toString();
}
// multi-element array, build up by concatenation
value_ s = vector[0].toString();
for (int i = 1; i < len; i++) {
s += ",";
s += vector[i].toString();
}
return s;
}
/////////////////////////////////////////////////////////////////////
// Date
class date
{
public:
static double now(void);
};
double date::now(void)
{
struct timeb t;
ftime(&t);
return t.time*1000.0+t.millitm;
}
class getTime_class_ : public func_ {
public:
getTime_class_() { length = 0; }
virtual value_ call(value_ this_, int nargs, ...)
{
return this_.dot("[[value]]");
}
};
static value_ getTime(new getTime_class_);
class Date_class_ : public func_ {
public:
Date_class_() { length=0; }
virtual value_ call(value_ this_, int nargs, ...)
{
// TODO: move this into prototype:
this_.dotref("getTime") = getTime;
if (nargs==0) {
// "set to the current time (UTC)"
this_.dotref("[[value]]") = date::now();
} else {
// TODO: handle 1,2,...7 args
throw not_imp();
}
return this_;
}
};
// constructor
value_ Date(new Date_class_);
/////////////////////////////////////////////////////////////////////
// Standard functions
class alert_class_ : public func_ {
public:
alert_class_() { length=1; }
virtual value_ call(value_ this_, int nargs, ...)
{
value_ msg;
if (nargs > 0) {
msg = ((value_*)(&nargs+1))[0];
}
MessageBox(NULL, msg.toString(), pzAppTitle_, MB_ICONEXCLAMATION | MB_OK);
return undefined;
}
};
value_ alert(new alert_class_);
class Object_class_ : public func_ {
public:
Object_class_() { length=0; }
virtual value_ call(value_ this_, int nargs, ...)
{
return this_;
}
};
value_ Object(new Object_class_);
/////////////////////////////////////////////////////////////////////
// value_ methods
value_ value_::dot(const char* id)
{
if (t==TOBJ) {
return v.o->dot(id);
}
// TODO: support properties of primitive values
throw incomp_operand();
} // dot
value_& value_::dotref(const char* id)
{
if (t==TOBJ) {
return v.o->dotref(id);
}
// TODO: support properties of primitive values
throw incomp_operand();
} // dotref
value_ value_::eltcall(value_ x, int nargs, ...)
{
value_ base = *this;
if (base.t < TOBJ) {
base = base.toObject();
}
value_ m = base.at(x);
if (m.t != TFUNC) {
throw TypeError();
}
func_* func = m.v.f;
#define arg0 ((value_*)(&nargs+1))[0]
#define arg1 ((value_*)(&nargs+1))[1]
#define arg2 ((value_*)(&nargs+1))[2]
#define arg3 ((value_*)(&nargs+1))[3]
#define arg4 ((value_*)(&nargs+1))[4]
switch (nargs) {
case 0:
return func->call(*this, nargs);
case 1:
return func->call(*this, nargs, arg0);
case 2:
return func->call(*this, nargs, arg0, arg1);
case 3:
return func->call(*this, nargs, arg0,arg1,arg2);
case 4:
return func->call(*this, nargs, arg0,arg1,arg2,arg3);
case 5:
return func->call(*this, nargs, arg0,arg1,arg2,arg3,arg4);
default:
throw incomp_operand();
} // switch
}
value_ value_::dotcall(const char* id, int nargs, ...)
{
if (t<TOBJ) {
// not an object
throw incomp_operand();
}
// get the function member
value_ m = v.o->dot(id);
if (m.t != TFUNC) {
throw incomp_operand();
}
func_* func = m.v.f;
#define arg0 ((value_*)(&nargs+1))[0]
#define arg1 ((value_*)(&nargs+1))[1]
#define arg2 ((value_*)(&nargs+1))[2]
#define arg3 ((value_*)(&nargs+1))[3]
#define arg4 ((value_*)(&nargs+1))[4]
switch (nargs) {
case 0:
return func->call(*this, nargs);
case 1:
return func->call(*this, nargs, arg0);
case 2:
return func->call(*this, nargs, arg0, arg1);
case 3:
return func->call(*this, nargs, arg0,arg1,arg2);
case 4:
return func->call(*this, nargs, arg0,arg1,arg2,arg3);
case 5:
return func->call(*this, nargs, arg0,arg1,arg2,arg3,arg4);
default:
throw incomp_operand();
} // switch
}
value_ value_::at(value_ x)
{
if (t==TOBJ) {
return v.o->at(x);
}
// TODO: support properties of primitive values
throw incomp_operand();
} // dot
value_& value_::atref(value_ x)
{
if (t==TOBJ) {
return v.o->atref(x);
}
// TODO: support properties of primitive values
throw incomp_operand();
} // dotref
value_ value_::toPrimitive(void) const
{
// TODO: implement this!
throw incomp_operand();
}
value_ value_::toObject(void) const
{
// TODO: real implementation of toObject!
if (t < TOBJ) {
throw TypeError();
}
return *this;
} // toObject
const char *value_::toString(void) const
{
if (t==TSTR) {
return v.s;
}
if (t==TNUM) {
if (_finite(v.d)) {
char buf[32];
//TODO: implement ECMAScript spec:
if (v.d==(long)v.d) {
sprintf(buf, "%ld", (long)v.d);
} else {
sprintf(buf, "%g", v.d);
}
return strdup(buf);
}
if (_isnan(v.d)) {
return "NaN";
}
// leaves an infinity
return "-Infinity" + (v.d > 0);
}
if (t==TBOOL) {
return v.d ? "true" : "false";
}
if (t==TUNDEF) {
return "undefined";
}
if (t==TNULL) {
return "null";
}
if (t==TARRAY) {
return ((array_*)v.o)->toString();
}
return toPrimitive().toString();
} // toString
bool value_::toBool(void) const
{
if (t==TBOOL || t==TNUM) {
// TODO: What if it's a NaN? Supposed to return false.
return v.d != 0;
}
if (t==TSTR) {
// empty string is false, all others are true.
return v.s[0] != 0;
}
if (t>=TOBJ) {
// object, function, array
return true;
}
// that leaves undefined or null, which are false
return false;
}
long value_::toInt32(void) const
{
double r;
if (t==TNUM || t==TBOOL) {
r = v.d;
} else {
r = toNumber();
}
if (!_finite(r)) {
return 0;
}
return _copysign(floor(fabs(r)),r);
}
double value_::toNumber(void) const
{
if (t==TNUM || t==TBOOL) {
return v.d;
}
if (t==TUNDEF) {
return NaN;
}
if (t==TNULL) {
return 0.0;
}
if (t==TSTR) {
double f;
char c;
if (1==sscanf(v.s, "%g %c", &f, &c)) {
return f;
}
return NaN;
}
throw incomp_operand();
}
func_* value_::toFunc(void) const
{
if (t != TFUNC) {
throw TypeError();
}
return v.f;
}
value_ value_::typeof(void) const
{
switch (t) {
case TUNDEF:
return "undefined";
case TBOOL:
return "boolean";
case TSTR:
return "string";
case TNUM:
return "number";
case TFUNC:
return "function";
default:
return "object";
} // switch
}
// inc/dec
value_ postinc_(value_& v)
// increment v but return it's previous value
{
double d = v.toNumber();
v = d+1;
return d;
}
// identity
value_ identical_(value_& a, value_& b)
{
if (a.t != b.t) {
return false;
}
switch (a.t) {
case value_::TUNDEF:
case value_::TNULL:
return true;
case value_::TNUM:
case value_::TBOOL:
// use IEEE equality.
// Note that NaN is never equal to anything, and
// +0=-0 and vice-versa.
return a.v.d==b.v.d;
case value_::TSTR:
// interesting - for strings, use lexical equality
return 0==strcmp(a.v.s,b.v.s);
default:
// object, array, function:
break;
} // switch
// they must point to the same object.
return a.v.o==b.v.o;
}
// assignment
value_& value_::operator+=(const value_& b)
{
if (t==TNUM) {
v.d += (double)b;
} else if (t==TSTR) {
const char *sb = b;
int blen = strlen(sb);
int len = strlen(v.s); // our current length
char *s = (char*)malloc(len+blen+1);
if (!s) {
throw bad_alloc();
}
memcpy(s, v.s, len);
memcpy(s+len, sb, blen+1);
v.s = s;
} else {
throw incomp_operand();
}
return *this;
}
// arithmetic operators
//
// unary -
value_ value_::operator-(void) const
{
if (t==value_::TNUM) {
return value_(-v.d);
}
throw incomp_operand();
}
// binary -
value_ value_::operator-(const value_& b) const
{
if (t==value_::TNUM) {
return value_(v.d - (double)b);
}
throw incomp_operand();
}
// binary +
value_ value_::operator+(const value_& b) const
{
if (t==value_::TSTR) {
const char *sb = (const char *)b;
int alen = strlen(v.s);
int blen = strlen(sb);
char *s = (char*)malloc(alen+blen+1);
memcpy(s, v.s, alen);
memcpy(s+alen, sb, blen+1);
return value_(s);
}
if (t==value_::TNUM) {
return value_(v.d+(double)b);
}
throw incomp_operand();
}
// binary *
value_ value_::operator*(const value_& b) const
{
if (t==value_::TNUM) {
return value_(v.d * (double)b);
}
throw incomp_operand();
}
// division (binary /)
value_ value_::operator/(const value_& b) const
{
if (t==value_::TNUM) {
return value_(v.d / (double)b);
}
throw incomp_operand();
}
// remainder (binary %)
value_ value_::operator%(const value_& b) const
{
if (t==value_::TNUM) {
return value_(fmod(v.d, (double)b));
}
throw incomp_operand();
}
// equality
bool value_::operator==(const value_&b)
{
if (this==&b) {
return true;
}
if (t==value_::TNUM && b.t==value_::TNUM) {
return v.d==b.v.d;
}
if (t==value_::TSTR && b.t==value_::TSTR) {
return 0==strcmp(v.s,b.v.s);
}
// chicken out for now. TODO: operand alignment.
throw incomp_operand();
}
// relation <
bool value_::operator<(const value_& b) const
{
if (t==value_::TSTR) {
return strcmp(v.s, b) < 0;
}
if (t==value_::TNUM) {
return v.d < (double)b;
}
throw incomp_operand();
}