forked from qcwgithub/qjsbmozillajswrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callstack.cpp
481 lines (428 loc) · 12.5 KB
/
callstack.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
#include "mozjswrap.h"
//
// global variables
//
//
MAPID idFunRet = 0; // after callFunctionValue, javascript return value
MAPID idSave = 0; // saving some id to valueMap
JSCallStack jsCallStack;
#define ArgVal(i) (jsCallStack.currStack->args->get(i))
#define ArgIndex (jsCallStack.currStack->argIndex)
#define ActualArgc (jsCallStack.currStack->actualArgc)
#define ArgRval (jsCallStack.currStack->rval)
CSEntry csEntry = 0;
bool JSCall(JSContext *cx, unsigned argc, JS::Value *vp)
{
AJSCallStack aStack;
aStack.rval = vp;
aStack.argc = argc;
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
aStack.args = &args;
int op = args.get(0).toInt32();
int slot = args.get(1).toInt32();
int index = args.get(2).toInt32();
bool isStatic = args.get(3).toBoolean();
//
// Calculate actual parameter count
//
aStack.actualArgc = argc;
while (aStack.actualArgc > 0 && args.get(aStack.actualArgc - 1).isUndefined())
{
aStack.actualArgc--;
}
aStack.actualArgc = argc;
aStack.argIndex = 4;
jsCallStack.Push(aStack);
bool ret = (_TRUE == csEntry(op, slot, index, (isStatic ? 1 : 0), aStack.actualArgc - aStack.argIndex));
valueMap::_clearTempIDs();
jsCallStack.Pop();
return ret;
}
int getArgIndex()
{
return ArgIndex;
}
void setArgIndex(int i)
{
if (i >= 0 && i < ActualArgc)
{
ArgIndex = i;
}
}
MOZ_API int incArgIndex()
{
return ArgIndex++;
}
JS::Value getVal(eGetType e, bool bIncIndex)
{
switch (e)
{
case GetArg:
{
int i = ArgIndex;
if (bIncIndex) ArgIndex++;
return ArgVal(i);
}
break;
case GetArgRef:
{
int i = ArgIndex;
if (bIncIndex) ArgIndex++;
JS::RootedValue val(g_cx, ArgVal(i));
JS::RootedObject jsObj(g_cx, &val.toObject());
JS::RootedValue v(g_cx);
bool suc = JS_GetProperty(g_cx, jsObj, "Value", &v);
Assert(suc);
return v;
}
break;
case GetJSFunRet:
{
JS::RootedValue val(g_cx);
valueMap::getVal(idFunRet, &val);
return val;
}
break;
case GetSaveAndRemove:
default:
{
JS::RootedValue val(g_cx);
valueMap::getVal(idSave, &val);
if (bIncIndex) // 2015.Sep.7
valueMap::removeByID(idSave, false);
return val;
}
break;
}
}
enum eValueTag
{
tagUNDEFINED = 1 << 0,
tagNULL = 1 << 1,
tagINT32 = 1 << 2,
tagDOUBLE = 1 << 3,
tagBOOLEAN = 1 << 4,
tagSTRING = 1 << 5,
tagNUMBER = 1 << 6,
tagOBJECT = 1 << 7,
};
MOZ_API unsigned int getTag(eGetType e)
{
const JS::Value& val = getVal(e, false);
unsigned int ret = 0;
{
if (val.isUndefined()) ret |= tagUNDEFINED;
if (val.isNull()) ret |= tagNULL;
if (val.isInt32()) ret |= tagINT32;
if (val.isDouble()) ret |= tagDOUBLE;
if (val.isBoolean()) ret |= tagBOOLEAN;
if (val.isString()) ret |= tagSTRING;
if (val.isNumber()) ret |= tagNUMBER;
if (val.isObject()) ret |= tagOBJECT;
}
return ret;
}
template<class T>
T val2Number(JS::HandleValue val)
{
if (val.isDouble())
return (T)val.toDouble();
else
return (T)val.toInt32();
}
//extern jschar* getMarshalStringFromJSString(JSContext* cx, JSString* jsStr);
char* val2String(JS::HandleValue val)
{
JS::RootedString jsStr(g_cx, val.toString());
if (jsStr == 0)
return "";
else
return getMarshalStringFromJSString(g_cx, jsStr);
}
template<class T>
T getNumber(eGetType e)
{
JS::RootedValue val(g_cx, getVal(e, true));
return val2Number<T>(val);
}
short getChar (eGetType e) { return getNumber<short>(e); }
char getSByte (eGetType e) { return getNumber<char>(e); }
unsigned char getByte (eGetType e) { return getNumber<unsigned char>(e); }
short getInt16 (eGetType e) { return getNumber<short>(e); }
unsigned short getUInt16 (eGetType e) { return getNumber<unsigned short>(e); }
int getInt32 (eGetType e) { return getNumber<int>(e); }
unsigned int getUInt32 (eGetType e) { return getNumber<unsigned int>(e); }
long long getInt64 (eGetType e) { return getNumber<long long>(e); }
unsigned long long getUInt64 (eGetType e) { return getNumber<unsigned long long>(e); }
int getEnum (eGetType e) { return getNumber<int>(e); }
float getSingle (eGetType e) { return getNumber<float>(e); }
double getDouble (eGetType e) { return getNumber<double>(e); }
long long getIntPtr (eGetType e) { return getNumber<long long>(e); }
_BOOL getBoolean(eGetType e)
{
return (getVal(e, true).toBoolean() ? _TRUE : _FALSE);
}
char* getString(eGetType e)
{
JS::RootedValue val(g_cx, getVal(e, true));
return val2String(val);
}
float arrFloat[3];
void getVector2(eGetType e)
{
JS::RootedValue val(g_cx, getVal(e, true));
Assert(val.isObject());
JS::RootedObject obj(g_cx, &val.toObject());
JS::RootedValue v(g_cx);
bool suc = false;
suc = JS_GetProperty(g_cx, obj, "x", &v);
Assert(suc);
arrFloat[0] = val2Number<float>(v);
suc = JS_GetProperty(g_cx, obj, "y", &v);
Assert(suc);
arrFloat[1] = val2Number<float>(v);
}
void getVector3(eGetType e)
{
JS::RootedValue val(g_cx, getVal(e, true));
Assert(val.isObject());
JS::RootedObject obj(g_cx, &val.toObject());
JS::RootedValue v(g_cx);
bool suc = false;
suc = JS_GetProperty(g_cx, obj, "x", &v);
Assert(suc);
arrFloat[0] = val2Number<float>(v);
suc = JS_GetProperty(g_cx, obj, "y", &v);
Assert(suc);
arrFloat[1] = val2Number<float>(v);
suc = JS_GetProperty(g_cx, obj, "z", &v);
Assert(suc);
arrFloat[2] = val2Number<float>(v);
}
float getObjX()
{
return arrFloat[0];
}
float getObjY()
{
return arrFloat[1];
}
float getObjZ()
{
return arrFloat[2];
}
int getObject(eGetType e)
{
JS::RootedValue val(g_cx, ::getVal(e, true));
if (val.isObject())
{
MAPID id = valueMap::getID(val, true);
valueMap::_addTempID(id);
return id;
}
return 0;
}
MOZ_API _BOOL isFunction(eGetType e)
{
JS::RootedValue val(g_cx, getVal(e, false));
if (val.isObject())
{
return (JS_ObjectIsFunction(g_cx, &val.toObject()) ? _TRUE : _FALSE);
}
return _FALSE;
}
//bool valFullNameIs(JS::HandleValue val, const char* name)
//{
// if (val.isObject())
// {
// JS::RootedObject obj(g_cx, &val.toObject());
// JS::RootedValue v(g_cx);
// JS_GetProperty(g_cx, obj, "_fullname", &v);
// if (v.isString())
// {
// // TODO fix memory leak
// JS::RootedString jsStr(g_cx, v.toString());
// const char* str = JS_EncodeStringToUTF8(g_cx, jsStr);
// bool ret = (strcmp(str, name) == 0);
// JS_free(g_cx, (void*)str);
// return ret;
// }
// }
// return false;
//}
MOZ_API _BOOL isVector2( eGetType e )
{
JS::RootedValue val(g_cx, getVal(e, false));
if (val.isObject())
{
JS::RootedObject obj(g_cx, &val.toObject());
JS::RootedValue v(g_cx);
JS_GetProperty(g_cx, obj, "$isv2", &v);
if (v.isBoolean())
return v.toBoolean() ? _TRUE : _FALSE;
}
return _FALSE;
/*JS::RootedValue val(g_cx, getVal(e, false));
return (valFullNameIs(val, "UnityEngine.Vector2") ? _TRUE : _FALSE);*/
}
MOZ_API _BOOL isVector3( eGetType e )
{
JS::RootedValue val(g_cx, getVal(e, false));
if (val.isObject())
{
JS::RootedObject obj(g_cx, &val.toObject());
JS::RootedValue v(g_cx);
JS_GetProperty(g_cx, obj, "$isv3", &v);
if (v.isBoolean())
return v.toBoolean() ? _TRUE : _FALSE;
}
return _FALSE;
/*JS::RootedValue val(g_cx, getVal(e, false));
return (valFullNameIs(val, "UnityEngine.Vector3") ? _TRUE : _FALSE);*/
}
MOZ_API int getFunction(eGetType e)
{
JS::RootedValue val(g_cx, getVal(e, true));
return valueMap::addFunction(val);
}
void setVal(eSetType e, JS::HandleValue val)
{
switch (e)
{
case SetSaveAndTempTrace:
{
idSave = valueMap::add(val, 7);
valueMap::setTempTrace(idSave, true);
}
break;
case SetRval:
JS_SET_RVAL(g_cx, ArgRval, val);
break;
case SetArgRef:
default:
{
int i = ArgIndex;
JS::RootedValue v(g_cx, ArgVal(i));
JS::RootedObject jsObj(g_cx, &v.toObject());
JS::RootedValue mfval(g_cx, val);
JS_SetProperty(g_cx, jsObj, "Value", mfval);
}
break;
}
}
template<class T>
void setNumberI(eSetType e, T value)
{
JS::RootedValue val(g_cx, INT_TO_JSVAL((int)value));
setVal(e, val);
}
template<class T>
void setNumberF(eSetType e, T value)
{
JS::RootedValue val(g_cx, DOUBLE_TO_JSVAL((double)value));
setVal(e, val);
}
void setUndefined(eSetType e)
{
JS::RootedValue val(g_cx);
val.setUndefined();
setVal(e, val);
}
void setChar (eSetType e, short v) { return setNumberI<short>(e, v); }
void setSByte (eSetType e, char v) { return setNumberI<char>(e, v); }
void setByte (eSetType e, unsigned char v) { return setNumberI<unsigned char>(e, v); }
void setInt16 (eSetType e, short v) { return setNumberI<short>(e, v); }
void setUInt16 (eSetType e, unsigned short v) { return setNumberI<unsigned short>(e, v); }
void setInt32 (eSetType e, int v) { return setNumberI<int>(e, v); }
void setUInt32 (eSetType e, unsigned int v) { return setNumberI<unsigned int>(e, v); }
void setInt64 (eSetType e, long long v) { return setNumberF<long long>(e, v); }
void setUInt64 (eSetType e, unsigned long long v) { return setNumberF<unsigned long long>(e, v); }
void setEnum (eSetType e, int v) { return setNumberI<int>(e, v); }
void setSingle (eSetType e, float v) { return setNumberF<float>(e, v); }
void setDouble (eSetType e, double v) { return setNumberF<double>(e, v); }
void setIntPtr (eSetType e, long long v) { return setNumberF<long long>(e, v); }
void setBoolean(eSetType e, _BOOL v)
{
JS::RootedValue val(g_cx, BOOLEAN_TO_JSVAL(v == _TRUE));
setVal(e, val);
}
void setString(eSetType e, const jschar* value)
{
JS::RootedString jsString(g_cx, JS_NewUCStringCopyZ(g_cx, value));
JS::RootedValue val(g_cx, STRING_TO_JSVAL(jsString));
if (value == 0)
val.setUndefined();
setVal(e, val);
}
void setVector2(eSetType e, float x, float y)
{
JS::RootedObject jsObj(g_cx, _createJSClassObject("UnityEngine.Vector2", g_bUseCacheForStruct));
if (jsObj)
{
JS::RootedValue val(g_cx);
val.setDouble(x); JS_SetProperty(g_cx, jsObj, "x", val);
val.setDouble(y); JS_SetProperty(g_cx, jsObj, "y", val);
/*JS::RootedString jsString(g_cx, JS_NewStringCopyZ(g_cx, "UnityEngine.Vector2"));
val.setString(jsString);
JS_SetProperty(g_cx, jsObj, "_fullname", val);*/
val.setBoolean(true);
JS_SetProperty(g_cx, jsObj, "$isv2", val);
val.setObject(*jsObj);
setVal(e, val);
}
}
void setVector3(eSetType e, float x, float y, float z)
{
JS::RootedObject jsObj(g_cx, _createJSClassObject("UnityEngine.Vector3", g_bUseCacheForStruct));
if (jsObj)
{
JS::RootedValue val(g_cx);
val.setDouble(x); JS_SetProperty(g_cx, jsObj, "x", val);
val.setDouble(y); JS_SetProperty(g_cx, jsObj, "y", val);
val.setDouble(z); JS_SetProperty(g_cx, jsObj, "z", val);
/*JS::RootedString jsString(g_cx, JS_NewStringCopyZ(g_cx, "UnityEngine.Vector3"));
val.setString(jsString);
JS_SetProperty(g_cx, jsObj, "_fullname", val);*/
val.setBoolean(true);
JS_SetProperty(g_cx, jsObj, "$isv3", val);
val.setObject(*jsObj);
setVal(e, val);
}
}
void setObject(eSetType e, int id)
{
JS::RootedValue val(g_cx);
if (id == 0 || !valueMap::getVal(id, &val))
{
val.setUndefined();
}
setVal(e, val);
}
void setFunction(eSetType e, int funID)
{
setObject(e, funID);
// JS::RootedValue val(g_cx);
// if (!valueMap::getVal(funID, &val))
// {
// val.setUndefined();
// }
//
// setVal(e, _v);
}
void setArray(eSetType e, int count, _BOOL bClear)
{
JSObject* _t = JS_NewArrayObject(g_cx, count);
JS::RootedObject arrObj(g_cx, _t);
for (int i = 0; i < count; i++)
{
JS::RootedValue val(g_cx);
bool b = valueMap::getVal(valueArr::arr[i], &val);
Assert(b);
JS_SetElement(g_cx, arrObj, i, val);
}
// clear value array
valueArr::clear(bClear == _TRUE);
JS::RootedValue val(g_cx);
val.setObject(*arrObj);
setVal(e, val);
}