forked from arnaudsj/ChatScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.cpp
More file actions
218 lines (205 loc) · 5.89 KB
/
javascript.cpp
File metadata and controls
218 lines (205 loc) · 5.89 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
#ifndef DISCARDJAVASCRIPT
#include "duktape/duktape.h"
//#include "duktape/duktape.cpp"
duk_context *ctxPermanent = NULL;
duk_context *ctxTransient = NULL;
duk_context *ctx;
#endif
#include "common1.h"
#ifndef WIN32
#define stricmp strcasecmp
#define strnicmp strncasecmp
#endif
void ChangeSpecial(char* buffer);
// there are 2 contexts: a permanently resident one for the system and a transient one per volley.
// ONE instance per server... so all routines are shared until gone. Need to distinguish "inits" from calls.
// OR runtime per user, always init and call.
FunctionResult RunJavaScript(char* definition, char* buffer, unsigned int args)
{ // Javascript permanent {call void name type type} eval ...code...
char* defstart = definition;
bool inited = (*definition++ == '.');
*buffer = 0;
#ifndef DISCARDJAVASCRIPT
char context[MAX_WORD_SIZE];
definition = ReadCompiledWord(definition,context);
if (!stricmp(context,"permanent"))
{
if (!ctxPermanent)
{
ctxPermanent = duk_create_heap_default();
if (!ctxPermanent) return FAILRULE_BIT; // unable to init it
}
ctx = ctxPermanent;
}
else
{
if (!ctxTransient)
{
ctxTransient = duk_create_heap_default();
if (!ctxTransient) return FAILRULE_BIT; // unable to init it
}
ctx = ctxTransient;
}
char word[MAX_WORD_SIZE];
definition = ReadCompiledWord(definition,word); // is it a call? call test string int
char name[MAX_WORD_SIZE];
*name = 0;
char returnType[MAX_WORD_SIZE];
char* callbase = NULL;
char* code = definition;
if (!stricmp(word,"call")) // skip over the description for now
{
definition = ReadCompiledWord(definition,returnType);
definition = ReadCompiledWord(definition,name);
ReadCompiledWord(definition,word); // arg type there?
callbase = definition; // start of arguments list
code = definition;
while (!stricmp(word,"string") || !stricmp(word,"int") || !stricmp(word,"float"))
{
code = definition; // start of arguments list
definition = ReadCompiledWord(definition,word);
}
}
// compile requirements - execute the code definition if not inited
bool compile = false;
if (!stricmp(word,"compile"))
{
code = ReadCompiledWord(code,word);
compile = true; // compile vs eval
if (!*code) return FAILRULE_BIT; // require some code
}
else if (!stricmp(word,"eval"))
{
if (!*code) return FAILRULE_BIT; // require some code
}
else if (!*name) return FAILRULE_BIT; // need to define someting, be it a compile or a call
char* terminator = strchr(code,'`');
*terminator = 0; // hide this from javascript
if (*code && !inited) // code was supplied, handle it if not yet executed
{
*defstart = '.';
char file[SMALL_WORD_SIZE];
ReadCompiledWord(code,file);
if (!stricmp(file,"file")) // read files
{
while ((code = ReadCompiledWord(code,file)) != NULL && !stricmp(file,"file"))
{
code = ReadCompiledWord(code,file); // name
char* name = file;
if (file[0] == '"')
{
size_t len = strlen(file);
file[len-1] = 0;
++name;
}
if (!compile) duk_eval_file(ctx, name);
else duk_compile_file(ctx, 0, name);
duk_pop(ctx);
}
}
else
{
if (!compile) duk_eval_string(ctx, (const char *)code); // execute the definition
else duk_compile_string(ctx, 0, (const char *)code); // compile the definition
duk_pop(ctx); /* pop result/error */
}
}
// now do a call, if one should be done
if (*name)
{
duk_push_global_object(ctx);
int found = (int) duk_get_prop_string(ctx, -1 /*index*/, name); // find the function
if (!found)
{
duk_pop(ctx); // discard context
*terminator = '`';
return FAILRULE_BIT;
}
unsigned int index = 0;
FunctionResult result = NOPROBLEM_BIT;
while (index < args)
{
char type[MAX_WORD_SIZE];
*buffer = 0;
if ((result = JavascriptArgEval(index,buffer)) != NOPROBLEM_BIT)
{
for (unsigned int i = 0; i < index; ++i) duk_pop(ctx); // discard saved args
duk_pop(ctx); // discard context
*terminator = '`';
return FAILRULE_BIT;
}
callbase = ReadCompiledWord(callbase,type);
if (!stricmp(type,"string"))
{
duk_push_string(ctx, buffer);
}
else if (!stricmp(type,"int" ))
{
if (!(*buffer >= '0' && *buffer <= '9') && *buffer != '-' && *buffer != '+')
{
result = FAILRULE_BIT;
break;
}
duk_push_int(ctx, atoi(buffer));
}
else if (!stricmp(type,"float" ))
{
if (!(*buffer >= '0' && *buffer <= '9') && *buffer != '.')
{
result = FAILRULE_BIT;
break;
}
duk_push_number(ctx, (double)atof(buffer));
}
else break;
++index;
}
*buffer = 0;
if (result != NOPROBLEM_BIT) // abandon the call
{
for (unsigned int i = 0; i < index; ++i) duk_pop(ctx); // discard saved args
duk_pop(ctx); // discard context
*terminator = '`';
return FAILRULE_BIT;
}
if (duk_pcall(ctx, args) != 0) // call failed
{
printf("Javascript Error: %s\r\n", duk_safe_to_string(ctx, -1));
duk_pop(ctx);
*terminator = '`';
return FAILRULE_BIT;
}
else
{
if (!stricmp(returnType,"string"))
{
strcpy(buffer,duk_safe_to_string(ctx, -1)); // assumes there is a return string!
if (strchr(buffer,'\n') || strchr(buffer,'\r') || strchr(buffer,'\t')) ChangeSpecial(buffer); // do not allow special characters in string
}
else if (!stricmp(returnType,"int")) strcpy(buffer,duk_safe_to_string(ctx, -1)); // assumes there is a return string!
else if (!stricmp(returnType,"float")) strcpy(buffer,duk_safe_to_string(ctx, -1)); // assumes there is a return string!
duk_pop(ctx);
*terminator = '`';
return NOPROBLEM_BIT;
}
}
*terminator = '`';
return NOPROBLEM_BIT;
#else
return FAILRULE_BIT; // if javascript not compiled into engine
#endif
}
void DeletePermanentJavaScript()
{
#ifndef DISCARDJAVASCRIPT
duk_destroy_heap(ctxPermanent);
ctxPermanent = NULL;
#endif
}
void DeleteTransientJavaScript()
{
#ifndef DISCARDJAVASCRIPT
duk_destroy_heap(ctxTransient);
ctxTransient = NULL;
#endif
}