forked from gfwilliams/tiny-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasObjects.cpp
725 lines (594 loc) · 16.3 KB
/
asObjects.cpp
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/*
* File: asObjects.cpp
* Author: ghernan
*
* Support for object oriented programming in AsyncScript.
* Contains the runtime objects for AsyncScript classes and objects
*
* Created on January 3, 2017, 11:16 AM
*/
#include "ascript_pch.hpp"
#include "asObjects.h"
#include "scriptMain.h"
#include "microVM.h"
#include "jsArray.h"
#include "ScriptException.h"
using namespace std;
Ref<JSClass> createObjectClass();
Ref<JSClass> JSObject::DefaultClass = createObjectClass();
// JSClass
//
//////////////////////////////////////////////////
/**
* Constructor.
* @param name
* @param parent
* @param members
* @param constructorFn
*/
JSClass::JSClass(const std::string& name,
Ref<JSClass> parent,
const VarMap& members,
Ref<JSFunction> constructorFn) :
m_name(name), m_members(members), m_parent(parent), m_constructor(constructorFn->value())
{
}
Ref<JSClass> JSClass::create (const std::string& name,
Ref<JSClass> parent,
const VarMap& members,
Ref<JSFunction> constructorFn)
{
return refFromNew(new JSClass(name, parent, members, constructorFn));
}
Ref<JSClass> JSClass::createNative (const std::string& name,
Ref<JSClass> parent,
const VarMap& members,
const StringVector& params,
JSNativeFn nativeFn)
{
auto native = JSFunction::createNative("@constructor", params, nativeFn);
return refFromNew(new JSClass(name, parent, members, native));
}
/**
* Gets all fields of the class
* @param inherited Include inherited fields
* @return
*/
StringSet JSClass::getFields(bool inherited)const
{
StringSet result;
if (inherited && m_parent.notNull())
result = m_parent->getFields();
m_members.forEach ([&result](const string& name, ASValue){
result.insert(name);
});
return result;
}
/**
* Reads a field from a class
* @param key
* @return
*/
ASValue JSClass::readField(const std::string& key)const
{
ASValue val;
if (m_members.tryGetValue(key, &val))
return val;
else if (m_parent.notNull())
return m_parent->readField (key);
else
return jsNull();
}
/**
* Gets the parameters of a class constructor
* @return
*/
const StringVector& JSClass::getParams()const
{
const static StringVector empty;
Ref<JSFunction> fn;
if (m_constructor.getType() == VT_CLOSURE)
fn = m_constructor.staticCast<JSClosure>()->getFunction();
else if (m_constructor.getType() == VT_FUNCTION)
fn = m_constructor.staticCast<JSFunction>();
else
return empty;
return fn->getParams();
}
/**
* Sets the environment object which will be used in constructor.
* @param ec
* @return
*/
ASValue JSClass::scSetEnv(ExecutionContext* ec)
{
auto env = ec->getParam(0);
auto cls = ec->getParam(1);
if (cls.getType() != VT_CLASS)
rtError ("@setClassEnv: Second parameter shall be a class");
auto clsPtr = cls.staticCast<JSClass>();
auto fnVal = clsPtr->getConstructor();
Ref<JSFunction> fn;
if (fnVal.getType() == VT_CLOSURE)
fn = fnVal.staticCast<JSClosure>()->getFunction();
else if (fnVal.getType() == VT_FUNCTION)
fn = fnVal.staticCast<JSFunction>();
else
rtError ("Constructor of class '%s' is not a function", clsPtr->getName().c_str());
ASValue closureValues[2] = {env, cls};
auto closure = JSClosure::create(fn, closureValues, 2);
clsPtr->m_constructor = closure->value();
//Closures for member functions
auto newMembers = clsPtr->m_members.map ([&env](const string& name, ASValue value)->ASValue{
if (value.getType() == VT_FUNCTION)
return JSClosure::create(value.staticCast<JSFunction>(), &env, 1)->value();
else
return value;
});
clsPtr->m_members = newMembers;
return cls;
}
// JSObject
//
//////////////////////////////////////////////////
JSObject::JSObject(Ref<JSClass> cls, JSMutability mutability) :
m_cls (cls), m_mutability(mutability)
{
ASSERT(cls.notNull());
}
JSObject::~JSObject()
{
//printf ("Destroying object: %s\n", this->getJSON(0).c_str());
}
/**
* Creates an empty object, with default class
* @return
*/
Ref<JSObject> JSObject::create()
{
return refFromNew(new JSObject(DefaultClass, MT_MUTABLE));
}
/**
* Creates an empty JSON object
* @return
*/
Ref<JSObject> JSObject::create(Ref<JSClass> cls)
{
return refFromNew(new JSObject(cls, MT_MUTABLE));
}
/**
* Creates a frozen copy of an object
*/
ASValue JSObject::freeze()
{
if (getMutability() == MT_MUTABLE)
return clone (false)->value();
else
return value();
}
/**
* Creates a 'deep-frozen' copy of the object, by making a frozen copy of the object
* and all referenced objects which are not 'deep-frozen' recursively.
* @param transformed
* @return
*/
ASValue JSObject::deepFreeze(ASValue::ValuesMap& transformed)
{
auto me = value();
if (m_mutability == MT_DEEPFROZEN)
return me;
auto it = transformed.find(me);
if (it != transformed.end())
return it->second;
//Clone object
auto newObject = JSObject::create(m_cls);
transformed[me] = newObject->value();
m_members.forEach([newObject, this, &transformed](const string& name, ASValue val){
auto newValue = val.deepFreeze(transformed);
bool isConst = m_members.isConst(name);
newObject->writeField(name, newValue, isConst);
});
newObject->m_mutability = MT_DEEPFROZEN;
return newObject->value();
}
/**
* Creates a mutable copy of an object
* @param forceClone
* @return
*/
ASValue JSObject::unFreeze(bool forceClone)
{
if (forceClone || getMutability() != MT_MUTABLE)
return clone (true)->value();
else
return value();
}
/**
* Transforms the object into an immutable object. The transformation is made in
* place, no copy is performed.
* Watch-out for side-effects.
*/
void JSObject::setFrozen()
{
m_mutability = selectMutability(*this, false);
}
/**
* Gets a vector with all object keys
* @return
*/
std::vector <ASValue > JSObject::getKeys()const
{
std::vector <ASValue > result;
m_members.forEach([&result](const string& name, ASValue){
result.push_back(jsString(name));
});
return result;
}
/**
* Gets a vector with all object keys (string format)
* @return
*/
StringSet JSObject::getFields(bool inherited)const
{
StringSet result;
if (inherited)
result = m_cls->getFields(true);
m_members.forEach([&result](const string& name, ASValue){
result.insert(name);
});
return result;
}
/**
* Handles array access operator reads
* @param index
* @return
*/
ASValue JSObject::getAt(ASValue index, ExecutionContext* ec)
{
ASValue fn = jsNull();
if (ec != NULL)
fn = readField("getAt");
if (!fn.isNull())
return callMemberFn(fn, index, ec);
else
return readField(index.toString(ec));
}
/**
* Handles array access operator writes
* @param index
* @return
*/
ASValue JSObject::setAt(ASValue index, ASValue value, ExecutionContext* ec)
{
ASValue fn = jsNull();
if (ec != NULL)
fn = readField("setAt");
if (!fn.isNull())
return callMemberFn(fn, index, value, ec);
else
return writeField(index.toString(ec), value, false);
}
/**
* Returns an iterator to walk object contents.
* It is script-overridable. The default implementation returns a
* single item iterator.
* @return
*/
ASValue JSObject::iterator(ExecutionContext* ec)const
{
ASValue fn = jsNull();
if (ec != NULL)
fn = readField("iterator");
if (!fn.isNull())
return callMemberFn(fn, ec);
else
return singleItemIterator(const_cast<JSObject*>(this)->value());
}
/**
* Compares two objects.
* Default implementation compares addresses.
*
* @param b
* @param ec
* @return
*/
double JSObject::compare(const ASValue& b, ExecutionContext* ec)const
{
ASValue fn = jsNull();
if (ec != NULL)
fn = readField("compare");
if (!fn.isNull())
return callMemberFn(fn, b, ec).toDouble(ec);
else if (b.getType() != VT_OBJECT)
return VT_OBJECT - b.getType();
else
return double (this - b.staticCast<JSObject>().getPointer());
}
/**
* Copy constructor.
* @param src Reference to the source object
* @param _mutable
*/
JSObject::JSObject(const JSObject& src, bool _mutable)
: m_members (src.m_members)
, m_cls (src.m_cls)
, m_mutability (selectMutability(src, _mutable))
{
}
/**
* Creates a copy of the object
* @param _mutable Controls if the copy will be mutable or not. In case of not
* being mutable, it will be 'frozen' or 'deepFrozen' depending on the mutability
* state of the object members.
* @return
*/
Ref<JSObject> JSObject::clone (bool _mutable)
{
return refFromNew(new JSObject(*this, _mutable));
}
/**
* Changes the object class.
* Used by generated constructors.
* @param ec
* @return
*/
ASValue JSObject::scSetObjClass(ExecutionContext* ec)
{
ASValue objVal = ec->getParam(0);
ASValue clsVal = ec->getParam(1);
if (objVal.getType() != VT_OBJECT)
rtError ("@setObjClass: Not an object");
if (clsVal.getType() != VT_CLASS)
rtError ("@setObjClass: Parameter is not a class");
auto objPtr = objVal.staticCast<JSObject>();
auto clsPtr = clsVal.staticCast<JSClass>();
objPtr->m_cls = clsPtr;
return objVal;
}
/**
* Chooses the appropriate mutability state for the new object on a clone operation
* @param src
* @param _mutable
* @return
*/
JSMutability JSObject::selectMutability(const JSObject& src, bool _mutable)
{
if (_mutable)
return MT_MUTABLE;
else
{
bool onlyFrozen = src.m_members.any([](const string&, ASValue val){
return val.getMutability() != MT_DEEPFROZEN;
});
return onlyFrozen ? MT_FROZEN : MT_DEEPFROZEN;
}
}
/**
* Calls a member function defined in the script code.
* @param function
* @return
*/
ASValue JSObject::callMemberFn (ASValue function, ExecutionContext* ec)const
{
ASValue me = const_cast<JSObject*>(this)->value();
ec->push(me);
ec->push(function);
mvmExecCall(1, ec);
return ec->pop();
}
/**
* Calls a member function defined in the script code.
* @param function
* @return
*/
ASValue JSObject::callMemberFn (ASValue function, ASValue p1, ExecutionContext* ec)const
{
ASValue me = const_cast<JSObject*>(this)->value();
ec->push(p1);
ec->push(me);
ec->push(function);
mvmExecCall(1, ec);
return ec->pop();
}
/**
* Calls a member function defined in the script code.
* @param function
* @return
*/
ASValue JSObject::callMemberFn (ASValue function, ASValue p1, ASValue p2, ExecutionContext* ec)const
{
ASValue me = const_cast<JSObject*>(this)->value();
ec->push(p1);
ec->push(p2);
ec->push(me);
ec->push(function);
mvmExecCall(2, ec);
return ec->pop();
}
bool JSObject::isWritable(const std::string& key)const
{
//TODO: Check class fields
if (getMutability() != MT_MUTABLE)
return false;
else
return !m_members.isConst(key);
}
/**
* String representation of an object. Calls 'toString' script function, if defined.
* @return
*/
std::string JSObject::toString(ExecutionContext* ec)const
{
ASValue fn = jsNull();
if (ec != NULL)
fn = readField("toString");
if (!fn.isNull())
return callMemberFn(fn, ec).toString(ec);
else
return std::string("[Object of ") + m_cls->toString() + "]";
}
/**
* Transforms the object into a boolean value. Calls 'toBoolean' script function, if defined.
* @return
*/
bool JSObject::toBoolean(ExecutionContext* ec)const
{
ASValue fn = jsNull();
if (ec != NULL)
fn = readField("toBoolean");
if (!fn.isNull())
return callMemberFn(fn, ec).toBoolean(ec);
else
return true;
}
/**
* Transforms the object into a 'double' value. Calls 'toNumber' script function, if defined.
* @return
*/
double JSObject::toDouble(ExecutionContext* ec)const
{
ASValue fn = jsNull();
if (ec != NULL)
fn = readField("toNumber");
if (!fn.isNull())
return callMemberFn(fn, ec).toDouble(ec);
else
return getNaN();
}
/**
* Reads a field of the object. If it does not exist, it returns 'null'
* @param key
* @return
*/
ASValue JSObject::readField(const std::string& key)const
{
ASValue val;
if (m_members.tryGetValue(key, &val))
return val;
else
return m_cls->readField (key);
}
/**
* Sets the value of a member, or creates it if not already present.
* @param name
* @param value
* @param isConst
* @return
*/
ASValue JSObject::writeField(const std::string& key,
ASValue value,
bool isConst)
{
if (!isWritable(key))
return readField(key);
return m_members.varWrite (key, value, isConst);
}
/**
* Deletes a field from the object
* @param key
* @return
*/
ASValue JSObject::deleteField(const std::string& key)
{
if (!isWritable(key))
return readField(key);
return m_members.varDelete(key);
}
/**
* Generates a JSON representation of the object
* @return JSON string
*/
std::string JSObject::getJSON(int indent)
{
ostringstream output;
bool first = true;
//{"x":2}
output << "{";
m_members.forEach ([&](const string& name, ASValue val){
string childJSON = val.getJSON(indent+1);
if (!childJSON.empty())
{
if (!first)
output << ",";
else
first = false;
output << "\n" << indentText(indent+1) << "\"" << name << "\":";
output << childJSON;
}
});
if (!first)
output << "\n" << indentText(indent) << "}";
else
output << "}";
return output.str();
}
/**
* Sets a property of a field.
* @param field
* @param propName
* @param value
* @return
*/
ASValue JSObject::setFieldProperty (const std::string& field, const std::string& propName, ASValue value)
{
return m_members.setProperty (field, propName, value);
}
/**
* Gets a field property.
* If the field has not this property, or does not exist at all, it returns null.
* @param field
* @param propName
* @return
*/
ASValue JSObject::getFieldProperty (const std::string& field, const std::string& propName)const
{
return m_members.getProperty (field, propName);
}
ASValue scObjectFreeze(ExecutionContext* ec)
{
auto obj = ec->getThis();
return obj.freeze();
}
ASValue scObjectDeepFreeze(ExecutionContext* ec)
{
auto obj = ec->getThis();
return obj.deepFreeze();
}
ASValue scObjectUnfreeze(ExecutionContext* ec)
{
auto obj = ec->getThis();
auto forceClone = ec->getParam(0);
return obj.unFreeze(forceClone.toBoolean(ec));
}
ASValue scObjectIsFrozen(ExecutionContext* ec)
{
auto obj = ec->getThis();
return jsBool (!obj.isMutable());
}
ASValue scObjectIsDeepFrozen(ExecutionContext* ec)
{
auto obj = ec->getThis();
return jsBool (obj.getMutability() == MT_DEEPFROZEN);
}
ASValue scObjectConstructor(ExecutionContext* ec)
{
return JSObject::create()->value();
}
/**
* Creates the class object for the 'Object' class
* @return
*/
Ref<JSClass> createObjectClass()
{
VarMap members;
addNative("function freeze()", scObjectFreeze, members);
addNative("function deepFreeze()", scObjectDeepFreeze, members);
addNative("function unfreeze(forceClone)", scObjectUnfreeze, members);
addNative("function isFrozen(forceClone)", scObjectIsFrozen, members);
addNative("function isDeepFrozen(forceClone)", scObjectIsDeepFrozen, members);
return JSClass::createNative("Object",
JSObject::DefaultClass,
members,
StringVector(),
scObjectConstructor);
}