-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTinyJS_Functions.cpp
More file actions
161 lines (138 loc) · 5.17 KB
/
TinyJS_Functions.cpp
File metadata and controls
161 lines (138 loc) · 5.17 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
/*
* TinyJS
*
* A single-file Javascript-alike engine
*
* Authored By Gordon Williams <gw@pur3.co.uk>
*
* Copyright (C) 2009 Pur3 Ltd
*
* 42TinyJS
*
* A fork of TinyJS with the goal to makes a more JavaScript/ECMA compliant engine
*
* Authored / Changed By Armin Diedering <armin@diedering.de>
*
* Copyright (C) 2010 ardisoft
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "TinyJS.h"
#include "TinyJS_Functions.h"
#ifdef USING_STD
# include <algorithm>
# include <math.h>
# include <cstdlib>
# include <sstream>
# include <time.h>
#endif
using namespace std;
// ----------------------------------------------- Actual Functions
static void scTrace(const CFunctionsScopePtr &c, void * userdata) {
CTinyJS *js = (CTinyJS*)userdata;
if(c->getArgumentsLength())
c->getArgument(0)->trace();
else
js->getRoot()->trace("root");
}
static void scObjectDump(const CFunctionsScopePtr &c, void *) {
c->getArgument("this")->trace("> ");
}
static void scObjectClone(const CFunctionsScopePtr &c, void *) {
CScriptVarPtr obj = c->getArgument("this");
c->setReturnVar(obj->clone());
}
static void scIntegerValueOf(const CFunctionsScopePtr &c, void *) {
string str = c->getArgument("str")->getString();
int val = 0;
if (str.length()==1)
val = str.operator[](0);
c->setReturnVar(c->newScriptVar(val));
}
static void scJSONStringify(const CFunctionsScopePtr &c, void *) {
string indent = " ", indentString;
c->setReturnVar(c->newScriptVar(c->getArgument("obj")->getParsableString(indentString, indent)));
}
static void scArrayContains(const CFunctionsScopePtr &c, void *data) {
CScriptVarPtr obj = c->getArgument("obj");
CScriptVarPtr arr = c->getArgument("this");
int l = arr->getArrayLength();
CScriptVarPtr equal = c->constScriptVar(Undefined);
for (int i=0;i<l;i++) {
equal = obj->mathsOp(arr->getArrayIndex(i), LEX_EQUAL);
if(equal->getBool()) {
c->setReturnVar(c->constScriptVar(true));
return;
}
}
c->setReturnVar(c->constScriptVar(false));
}
static void scArrayRemove(const CFunctionsScopePtr &c, void *data) {
CScriptVarPtr obj = c->getArgument("obj");
CScriptVarPtr arr = c->getArgument("this");
int i;
vector<int> removedIndices;
int l = arr->getArrayLength();
CScriptVarPtr equal = c->constScriptVar(Undefined);
for (i=0;i<l;i++) {
equal = obj->mathsOp(arr->getArrayIndex(i), LEX_EQUAL);
if(equal->getBool()) {
removedIndices.push_back(i);
}
}
if(removedIndices.size()) {
vector<int>::iterator remove_it = removedIndices.begin();
int next_remove = *remove_it;
int next_insert = *remove_it++;
for (i=next_remove;i<l;i++) {
CScriptVarLink *link = arr->findChild(int2string(i));
if(i == next_remove) {
if(link) arr->removeLink(link);
if(remove_it != removedIndices.end())
next_remove = *remove_it++;
} else {
if(link) {
arr->setArrayIndex(next_insert++, link);
arr->removeLink(link);
}
}
}
}
}
static void scArrayJoin(const CFunctionsScopePtr &c, void *data) {
string sep = c->getArgument("separator")->getString();
CScriptVarPtr arr = c->getArgument("this");
ostringstream sstr;
int l = arr->getArrayLength();
for (int i=0;i<l;i++) {
if (i>0) sstr << sep;
sstr << arr->getArrayIndex(i)->getString();
}
c->setReturnVar(c->newScriptVar(sstr.str()));
}
// ----------------------------------------------- Register Functions
void registerFunctions(CTinyJS *tinyJS) {
tinyJS->addNative("function trace()", scTrace, tinyJS);
tinyJS->addNative("function Object.prototype.dump()", scObjectDump, 0);
tinyJS->addNative("function Object.prototype.clone()", scObjectClone, 0);
tinyJS->addNative("function Integer.valueOf(str)", scIntegerValueOf, 0); // value of a single character
tinyJS->addNative("function JSON.stringify(obj, replacer)", scJSONStringify, 0); // convert to JSON. replacer is ignored at the moment
tinyJS->addNative("function Array.prototype.contains(obj)", scArrayContains, 0);
tinyJS->addNative("function Array.prototype.remove(obj)", scArrayRemove, 0);
tinyJS->addNative("function Array.prototype.join(separator)", scArrayJoin, 0);
}