forked from gfwilliams/tiny-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmvmFunctions.cpp
536 lines (442 loc) · 11.8 KB
/
mvmFunctions.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
/*
* File: mvmFunctions.cpp
* Author: ghernan
*
* Functions which implement Micro virtual machine primitive operations
*
* Created on December 4, 2016, 11:48 PM
*/
#include "ascript_pch.hpp"
#include "mvmFunctions.h"
#include "jsArray.h"
#include "scriptMain.h"
#include "ScriptException.h"
#include "modules.h"
#include <math.h>
#include <string>
using namespace std;
/**
* Creates a new array
* @param pScope
* @return
*/
ASValue mvmNewArray (ExecutionContext* ec)
{
const size_t size = ec->getParam(0).toSizeT();
return JSArray::create(size)->value();
}
/**
* Increments a value by one.
* @param pScope
* @return
*/
ASValue mvmInc (ExecutionContext* ec)
{
return jsDouble(ec->getParam(0).toDouble(ec) + 1);
}
/**
* Decrements a value by one
* @param pScope
* @return
*/
ASValue mvmDec (ExecutionContext* ec)
{
return jsDouble(ec->getParam(0).toDouble(ec) - 1);
}
/**
* Negation: reverses sign of the input.
* @param pScope
* @return
*/
ASValue mvmNegate (ExecutionContext* ec)
{
return jsDouble(- ec->getParam(0).toDouble(ec));
}
/**
* Javascript 'add' operation. Adds numbers or concatenates strings.
* @param pScope
* @return
*/
ASValue mvmAdd (ExecutionContext* ec)
{
ASValue opA = ec->getParam(0);
ASValue opB = ec->getParam(1);
const JSValueTypes typeA = opA.getType();
const JSValueTypes typeB = opB.getType();
if (typeA >= VT_STRING || typeB >= VT_STRING)
return jsString(opA.toString(ec) + opB.toString(ec));
else
return jsDouble(opA.toDouble(ec) + opB.toDouble(ec));
}
/**
* Substract operation.
* @param pScope
* @return
*/
ASValue mvmSub (ExecutionContext* ec)
{
const double valA = ec->getParam(0).toDouble(ec);
const double valB = ec->getParam(1).toDouble(ec);
return jsDouble( valA - valB );
}
/**
* Multiply operation
* @param pScope
* @return
*/
ASValue mvmMultiply (ExecutionContext* ec)
{
const double valA = ec->getParam(0).toDouble(ec);
const double valB = ec->getParam(1).toDouble(ec);
return jsDouble( valA * valB );
}
/**
* Divide operation
* @param pScope
* @return
*/
ASValue mvmDivide (ExecutionContext* ec)
{
const double valA = ec->getParam(0).toDouble(ec);
const double valB = ec->getParam(1).toDouble(ec);
return jsDouble( valA / valB );
}
/**
* Modulus operation
* @param pScope
* @return
*/
ASValue mvmModulus (ExecutionContext* ec)
{
const double valA = ec->getParam(0).toDouble(ec);
const double valB = ec->getParam(1).toDouble(ec);
return jsDouble( fmod(valA, valB) );
}
/**
* Power operation
* @param pScope
* @return
*/
ASValue mvmPower (ExecutionContext* ec)
{
const double valA = ec->getParam(0).toDouble(ec);
const double valB = ec->getParam(1).toDouble(ec);
return jsDouble( pow(valA, valB) );
}
/**
* Binary 'NOT' operation
* @param pScope
* @return
*/
ASValue mvmBinNot (ExecutionContext* ec)
{
const int valA = ec->getParam(0).toInt32();
return jsInt(~valA);
}
/**
* Binary 'AND' operation
* @param pScope
* @return
*/
ASValue mvmBinAnd (ExecutionContext* ec)
{
const int valA = ec->getParam(0).toInt32();
const int valB = ec->getParam(1).toInt32();
return jsInt (valA & valB);
}
/**
* Binary 'OR' operation
* @param pScope
* @return
*/
ASValue mvmBinOr (ExecutionContext* ec)
{
const int valA = ec->getParam(0).toInt32();
const int valB = ec->getParam(1).toInt32();
return jsInt (valA | valB);
}
/**
* Binary 'XOR' (exclusive OR) operation
* @param pScope
* @return
*/
ASValue mvmBinXor (ExecutionContext* ec)
{
const int valA = ec->getParam(0).toInt32();
const int valB = ec->getParam(1).toInt32();
return jsInt (valA ^ valB);
}
/**
* Logical 'NOT' operation.
* @param pScope
* @return
*/
ASValue mvmLogicNot (ExecutionContext* ec)
{
const bool valA = ec->getParam(0).toBoolean(ec);
return jsBool (!valA);
}
/**
* Left shift operation
* @param pScope
* @return
*/
ASValue mvmLshift (ExecutionContext* ec)
{
const int valA = ec->getParam(0).toInt32();
const unsigned valB = unsigned( ec->getParam(1).toInt32() );
return jsInt (valA << valB);
}
/**
* Right shift operation on signed values.
* @param pScope
* @return
*/
ASValue mvmRshift (ExecutionContext* ec)
{
const int valA = ec->getParam(0).toInt32();
const unsigned valB = unsigned( ec->getParam(1).toInt32() );
return jsInt (valA >> valB);
}
/**
* Right shift operation, interpreting the values as unsigned numbers
* @param pScope
* @return
*/
ASValue mvmRshiftu (ExecutionContext* ec)
{
const unsigned valA = unsigned( ec->getParam(0).toInt32() );
const unsigned valB = unsigned( ec->getParam(1).toInt32() );
return jsDouble (double(valA >> valB));
}
/**
* '<' comparison operation
* @param pScope
* @return
*/
ASValue mvmLess (ExecutionContext* ec)
{
ASValue opA = ec->getParam(0);
ASValue opB = ec->getParam(1);
if (opA.isNull() || opB.isNull())
return jsFalse();
else
return jsBool (opA.compare(opB, ec) < 0);
}
/**
* '>' comparison operation
* @param pScope
* @return
*/
ASValue mvmGreater (ExecutionContext* ec)
{
ASValue opA = ec->getParam(0);
ASValue opB = ec->getParam(1);
if (opA.isNull() || opB.isNull())
return jsFalse();
else
return jsBool (opA.compare(opB, ec) > 0);
}
/**
* '<=' comparison operation
* @param pScope
* @return
*/
ASValue mvmLequal (ExecutionContext* ec)
{
ASValue opA = ec->getParam(0);
ASValue opB = ec->getParam(1);
if (opA.isNull() || opB.isNull())
return jsFalse();
else
return jsBool (opA.compare(opB, ec) <= 0);
}
/**
* '<=' comparison operation
* @param pScope
* @return
*/
ASValue mvmGequal (ExecutionContext* ec)
{
ASValue opA = ec->getParam(0);
ASValue opB = ec->getParam(1);
if (opA.isNull() || opB.isNull())
return jsFalse();
else
return jsBool (opA.compare(opB, ec) <= 0);
}
/**
* Equality compare (==)
* @param pScope
* @return
*/
ASValue mvmAreEqual (ExecutionContext* ec)
{
ASValue opA = ec->getParam(0);
ASValue opB = ec->getParam(1);
return jsBool (opA.compare(opB, ec) == 0);
}
ASValue mvmAreTypeEqual (ExecutionContext* ec)
{
ASValue opA = ec->getParam(0);
ASValue opB = ec->getParam(1);
return jsBool (opA.typedCompare(opB, ec) == 0);
}
/**
* Inequality compare (!=)
* @param pScope
* @return
*/
ASValue mvmNotEqual (ExecutionContext* ec)
{
ASValue opA = ec->getParam(0);
ASValue opB = ec->getParam(1);
if (opA.isNull() || opB.isNull())
return jsBool( !(opA.isNull() && opB.isNull()) );
else
return jsBool (opA.compare (opB, ec) != 0);
}
/**
* Type and value equality compare (!==)
* @param pScope
* @return
*/
ASValue mvmNotTypeEqual (ExecutionContext* ec)
{
ASValue opA = ec->getParam(0);
ASValue opB = ec->getParam(1);
return jsBool (opA.typedCompare(opB, ec) != 0);
}
ASValue mvmToString (ExecutionContext* ec)
{
return jsString(ec->getParam(0).toString(ec));
}
ASValue mvmToBoolean (ExecutionContext* ec)
{
return jsBool(ec->getParam(0).toBoolean(ec));
}
ASValue mvmToNumber (ExecutionContext* ec)
{
return jsDouble(ec->getParam(0).toDouble(ec));
}
ASValue mvmIndexedRead (ExecutionContext* ec)
{
auto index = ec->getParam(1);
return ec->getParam(0).getAt(index,ec);
}
ASValue mvmIndexedWrite (ExecutionContext* ec)
{
auto index = ec->getParam(1);
auto value = ec->getParam(2);
return ec->getParam(0).setAt(index, value, ec);
}
ASValue mvmMakeClosure (ExecutionContext* ec)
{
ASSERT (!ec->frames.empty());
const CallFrame& curFrame = ec->frames.back();
const size_t nParams = curFrame.numParams;
ASSERT (nParams >= 2);
ASValue* paramsBegin = &ec->stack[curFrame.paramsIndex];
auto fn = paramsBegin[nParams-1];
if (fn.getType() != VT_FUNCTION)
rtError ("'fn' parameter is not a function");
return JSClosure::create(fn.staticCast<JSFunction>(), paramsBegin, nParams-1)->value();
}
/**
* Gets the iterator of a given object
* @param ec
* @return
*/
ASValue mvmIterator (ExecutionContext* ec)
{
auto sequence = ec->getParam(0);
return sequence.iterator(ec);
}
/**
* Symbol export implementation
* @param ec
* @return
*/
ASValue mvmExportSymbol (ExecutionContext* ec)
{
auto name = ec->getParam(0);
auto env = ec->getParam(1);
if (name.isNull())
rtError ("'export': 'name' parameter cannot be null");
string nameStr = name.toString(ec);
if (nameStr.empty())
rtError ("'export': empty 'name' parameter");
if (env.getType() != VT_OBJECT)
rtError ("'export': Missing 'env' (environment) parameter or it is not an object");
auto envObj = env.staticCast<JSObject>();
envObj->setFieldProperty (nameStr, "export", jsTrue());
return env;
}
/**
* Implements module 'import'
* @param ec
* @return
*/
ASValue mvmImportModule (ExecutionContext* ec)
{
auto path = ec->getParam(0);
auto env = ec->getParam(1);
if (path.isNull())
rtError ("'import': 'path' parameter cannot be null");
string pathStr = path.toString(ec);
if (pathStr.empty())
rtError ("'import': empty path");
if (env.getType() != VT_OBJECT)
rtError ("'import': Missing 'env' (environment) parameter or it is not an object");
//auto envObj = env.staticCast<JSObject>();
pathStr = normalizeModulePath (pathStr, ec);
auto module = findModule (pathStr, ec);
if (module.isNull())
module = loadModule (pathStr, ec);
mixModule (env, module, ec);
return env;
}
/**
* Registers MVM primitive operations to the given scope.
* @param scope
*/
void registerMvmFunctions(Ref<JSObject> scope)
{
addNative0("@newArray", mvmNewArray, scope);
addNative0("@inc", mvmInc, scope);
addNative0("@dec", mvmDec, scope);
addNative0("@negate", mvmNegate, scope);
addNative1("@add", "b", mvmAdd, scope);
addNative1("@sub", "b", mvmSub, scope);
addNative1("@multiply", "b", mvmMultiply, scope);
addNative1("@divide", "b", mvmDivide, scope);
addNative1("@modulus", "b", mvmModulus, scope);
addNative1("@power", "b", mvmPower, scope);
addNative0("@binNot", mvmBinNot, scope);
addNative1("@binAnd", "b", mvmBinAnd, scope);
addNative1("@binOr", "b", mvmBinOr, scope);
addNative1("@binXor", "b", mvmBinXor, scope);
addNative0("@logicNot", mvmLogicNot, scope);
addNative1("@lshift", "b", mvmLshift, scope);
addNative1("@rshift", "b", mvmRshift, scope);
addNative1("@rshiftu", "b", mvmRshiftu, scope);
addNative1("@less", "b", mvmLess, scope);
addNative1("@greater", "b", mvmGreater, scope);
addNative1("@areEqual", "b", mvmAreEqual, scope);
addNative1("@areTypeEqual", "b", mvmAreTypeEqual, scope);
addNative1("@notEqual", "b", mvmNotEqual, scope);
addNative1("@notTypeEqual", "b", mvmNotTypeEqual, scope);
addNative1("@lequal", "b", mvmLequal, scope);
addNative1("@gequal", "b", mvmGequal, scope);
addNative0("@toString", mvmToString, scope);
addNative0("@toBoolean", mvmToBoolean, scope);
addNative0("@toNumber", mvmToNumber, scope);
addNative1("@getAt", "index", mvmIndexedRead, scope);
addNative2("@setAt", "index", "value", mvmIndexedWrite, scope);
addNative2("@makeClosure", "env", "fn", mvmMakeClosure, scope);
addNative1("@iterator", "obj", mvmIterator, scope);
addNative2("@setClassEnv", "env", "cls", JSClass::scSetEnv, scope);
addNative2("@setObjClass", "obj", "cls", JSObject::scSetObjClass, scope);
addNative2("@exportSymbol", "name", "env", mvmExportSymbol, scope);
addNative2("@importModule", "path", "env", mvmImportModule, scope);
}