forked from qcwgithub/qjsbmozillajswrap
/
storage.cpp
555 lines (500 loc) · 12.1 KB
/
storage.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
#include "mozjswrap.h"
// objMap
///////////////////////////////////////////////////////////////////////////////////////////////////
// objMap::OBJMAP objMap::mMap;
// OBJID objMap::lastID = 1; // starts from 1, 0 means nothing
//
// void objMap::trace(JSTracer *trc)
// {
// char sz[16] = {'o','m',0};
// OBJMAPIT it = mMap.begin();
// for (; it != mMap.end(); it++)
// {
// itoa(it->first, &sz[2], 10);
// JS::Heap<JSObject*>* p = &it->second.obj;
// if (!objRoot::containsValue((JSObject*)(*p)))
// JS_CallHeapObjectTracer(trc, p, sz);
// }
// }
//
// OBJID objMap::add(JS::HandleObject jsObj)
// {
// OBJID id = lastID++;
// mMap[id] = stHeapObj(jsObj);
// return id;
// }
//
// OBJID objMap::jsObj2ID(JS::HandleObject jsObj, bool autoAdd)
// {
// if (jsObj == 0)
// {
// return 0;
// }
// OBJMAPIT it = mMap.begin();
// for (; it != mMap.end(); it++)
// {
// if (it->second.obj == jsObj)
// {
// return it->first;
// }
// }
// if (autoAdd)
// {
// return add(jsObj);
// }
// return 0;
// }
//
// JSObject* objMap::id2JSObj(OBJID id)
// {
// OBJMAPIT it = mMap.find(id);
// if (it != mMap.end())
// {
// return it->second.obj;
// }
// return 0;
// }
//
// bool objMap::remove(OBJID id)
// {
// OBJMAPIT it = mMap.find(id);
// if (it != mMap.end())
// {
// mMap.erase(it);
// return true;
// }
// return false;
// }
// valueArr
///////////////////////////////////////////////////////////////////////////////////////////////////
//
// for
// 1) Array
// 2) Function Arguments
//
int* valueArr::arr = 0;
int valueArr::Capacity = 0;
int valueArr::lastIndex = -1;
int* valueArr::makeSureArrHeapObj(int index)
{
int size = index + 1;
int& S = Capacity;
if (arr == 0 || S < size)
{
int oldS = S;
if (S == 0)
S = 8;
while (S < size)
S *= 2;
// copy data
int* _arr = new int[S];
if (index > 0)
{
int N = min(oldS - 1, index);
for (int i = 0; i <= N; i++)
_arr[i] = arr[i];
}
if (arr)
{
delete[] arr;
}
arr = _arr;
}
return arr;
}
void valueArr::add(int arrIndex, MAPID iMap)
{
if (arrIndex == 0)
{
Assert(lastIndex == -1);
}
lastIndex = arrIndex;
int* arr = makeSureArrHeapObj(arrIndex);
arr[arrIndex] = iMap;
}
void valueArr::clear(bool bClear)
{
if (bClear)
{
int& index = lastIndex;
if (index >= 0)
{
Assert(index <= Capacity - 1);
int N = min(index, Capacity - 1);
for (int i = 0; i <= N; i++)
{
valueMap::removeByID(arr[i], false);
}
}
}
lastIndex = -1;
}
// objRoot
///////////////////////////////////////////////////////////////////////////////////////////////////
// std::map<OBJID, JSObject**> objRoot::mapObjectRoot;
// bool objRoot::add( OBJID id )
// {
// if (mapObjectRoot.find(id) != mapObjectRoot.end())
// {
// return true;
// }
// JSObject* jsObj = objMap::id2JSObj(id);
// if (jsObj == 0)
// return false;
// JSObject** pjsObj = new JSObject*;
// *pjsObj = jsObj;
// bool ret = JS_AddObjectRoot(g_cx, pjsObj);
// mapObjectRoot[id] = pjsObj;
// return ret;
// }
//
// bool objRoot::remove( OBJID id )
// {
// std::map<OBJID, JSObject**>::iterator it = mapObjectRoot.find(id);
// if (it != mapObjectRoot.end())
// {
// JSObject** pjsObj = it->second;
// JS_RemoveObjectRoot(g_cx, pjsObj);
// delete pjsObj;
// mapObjectRoot.erase(it);
// return true;
// }
// return false;
// }
//
// bool objRoot::containsValue(JSObject* obj)
// {
// std::map<OBJID, JSObject**>::iterator it = mapObjectRoot.begin();
// for (; it != mapObjectRoot.end(); it++)
// {
// if (*(it->second) == obj)
// return true;
// }
// return false;
//
// }
valueMap::VALUEMAP valueMap::mMap;
valueMap::VMAP valueMap::VMap;
list<int> valueMap::lstFree;
int valueMap::index = 1;
bool valueMap::tracing = false;
std::list<int> valueMap::LstTempID;
int startMapID = 0;
int endMapID = 0;
//
// trace protects objects from being collected
//
//
void valueMap::trace(JSTracer *trc)
{
Assert(!valueMap::tracing);
valueMap::tracing = true;
//VMap.clear();
VMAP::iterator vit;
uint64_t Old, New;
//char sz[16] = {'v','m',0};
VALUEMAPIT it = mMap.begin();
for (; it != mMap.end(); it++)
{
//itoa(it->first, &sz[2], 10);
//stHeapValue* p = it->second;
stHeapValue* p = &it->second;
if (p->bTrace || p->bTempTrace || p->refCount > 0)
{
Old = p->heapValue.get().asRawBits();
//
// Set name to 0 is OK? it seems OK. I'm not sure.
//
// NOTICE: It seems 'name' here will cause memory leak if it's not 0
//
//
JS_CallHeapValueTracer(trc, &p->heapValue, 0 /* name */);
New = p->heapValue.get().asRawBits();
if (New != Old)
{
//
// Will it get here?
// Not sure
//
vit = VMap.find(Old);
Assert(vit != VMap.end());
VMap.erase(vit);
VMap.insert(VMAP::value_type(New, it->first));
}
}
}
// for (it = mMap.begin(); it != mMap.end(); it++)
// {
// VMap.insert(VMAP::value_type(it->second.heapValue.get().asRawBits(), it->first));
// }
valueMap::tracing = false;
}
MAPID valueMap::add(JS::HandleValue val, int mark)
{
int ret = containsValue(val);
if (ret != 0)
{
return ret;
}
//stHeapValue* p = new stHeapValue(val);
stHeapValue p(val);
p.mark = (char)mark;
int J = 0;
/*list<int>::iterator itBegin = lstFree.begin();
if (itBegin != lstFree.end())
{
J = *itBegin;
lstFree.erase(itBegin);
}
else*/
// 2015.Nov.3rd
// 先改成不回收利用
// 脚本的成员函数如 Update 如果 ID 是使用回收来的,当时可能还存在于 idFunRet
// 再次 callFunctionValue 时会把这个 ID 给移除掉
// 可能是导致 调用到其他脚本的 Update,错乱了
{
J = valueMap::index++;
}
Assert(!valueMap::tracing);
Assert(mMap.find(J) == mMap.end());
// 1)
mMap.insert(VALUEMAP::value_type(J, p));
//mMap[valueMap::index] = p;
// 2)
VMap.insert(VMAP::value_type(p.heapValue.get().asRawBits(), J));
return J;
}
MAPID valueMap::addFunction(JS::HandleValue val)
{
JS::RootedValue ns(g_cx);
if (!JS_ConvertValue(g_cx, val, JSTYPE_FUNCTION, &ns))
return 0;
return add(ns, 8);
}
MAPID valueMap::containsValue(JS::Value v)
{
// VALUEMAPIT it = mMap.begin();
// for (; it != mMap.end(); it++)
// {
// if (v == it->second.heapValue.get())
// return it->first;
//
// }
VMAP::iterator vit = VMap.find(v.asRawBits());
if (vit != VMap.end())
{
VALUEMAPIT it = mMap.find(vit->second);
Assert(it != mMap.end());
return it->first;
}
return 0;
}
int valueMap::incRefCount(MAPID id)
{
VALUEMAPIT it = mMap.find(id);
if (it != mMap.end())
{
stHeapValue& hv = it->second;
hv.refCount++;
return hv.refCount;
}
else
{
if (!shutingDown)
Assert(false, "valueMap::incRefCount fail");
return -1;
}
}
int valueMap::decRefCount(MAPID id)
{
VALUEMAPIT it = mMap.find(id);
if (it != mMap.end())
{
stHeapValue& hv = it->second;
hv.refCount--;
Assert(hv.refCount >= 0);
int ret = hv.refCount;
if (hv.refCount <= 0)
{
removeByID(id, false);
}
return ret;
}
else
{
if (!shutingDown)
Assert(false, "valueMap::decRefCount fail");
return -1;
}
}
bool valueMap::isTraced(MAPID id)
{
VALUEMAPIT it = mMap.find(id);
if (it != mMap.end())
{
stHeapValue& hv = it->second;
return hv.bTrace;
}
return false;
}
bool valueMap::setTrace(MAPID id, bool trace)
{
VALUEMAPIT it = mMap.find(id);
if (it != mMap.end())
{
stHeapValue& hv = it->second;
hv.bTrace = trace;
return true;
}
Assert(false, "valueMap::setTrace fail");
return false;
}
bool valueMap::setTempTrace(MAPID id, bool tempTrace)
{
VALUEMAPIT it = mMap.find(id);
if (it != mMap.end())
{
it->second.bTempTrace = tempTrace;
return true;
}
Assert(false, "valueMap::setTempTrace fail");
return false;
}
bool valueMap::setHasFinalizeOp(MAPID id, bool has)
{
VALUEMAPIT it = mMap.find(id);
if (it != mMap.end())
{
it->second.hasFinalizeOp = has;
return true;
}
Assert(false, "valueMap::setHasFinalizeOp fail");
return false;
}
bool valueMap::clear()
{
// VALUEMAPIT it = mMap.begin();
// for (; it != mMap.end(); it++)
// {
// delete it->second;
// }
mMap.clear();
VMap.clear();
LstTempID.clear();
/*
IMPORTANT
DO NOT reset index
In UnityEditor, it may Init JS engine, shut it down, init it, shut it down,... many times
removeByID may be called from C#'s destructor, the destructor may remove a ID belonging to last 'round'
so we let index always increase
*/
// index = 1;
lstFree.clear();
return 0;
}
bool valueMap::removeByID( MAPID i, bool bForce )
{
VALUEMAPIT it = mMap.find(i);
if (it != mMap.end())
{
stHeapValue* p = &it->second;
p->bTempTrace = false;
Assert(!(p->bTrace && p->hasFinalizeOp), "trace and finalize are both true!!!");
if (bForce || (!p->bTrace && !p->hasFinalizeOp && p->refCount <= 0))
{
Assert(!valueMap::tracing);
// 0)
//lstFree.push_back(it->first);
// 1)
VMAP::iterator vit = VMap.find(it->second.heapValue.get().asRawBits());
Assert(vit != VMap.end());
VMap.erase(vit);
// 2)
mMap.erase(it);
}
return true;
}
return false;
}
bool valueMap::getVal(MAPID id, JS::MutableHandleValue pVal)
{
if (id == 0)
return false;
VALUEMAPIT it = mMap.find(id);
if (it != mMap.end())
{
pVal.set(it->second.heapValue.get());
return true;
}
return false;
}
MAPID valueMap::getID(const JS::Value& val, bool autoAdd)
{
// VALUEMAPIT it = mMap.begin();
// for (; it != mMap.end(); it++)
// {
// if (it->second.heapValue.get() == val)
// return it->first;
// }
if (autoAdd)
{
JS::RootedValue rval(g_cx, val);
return valueMap::add(rval, 10);
}
else
{
return containsValue(val);
}
return 0;
}
void valueMap::_clearTempIDs()
{
std::list<int>::iterator it = LstTempID.begin();
for (; it != LstTempID.end(); it++)
{
removeByID(*it, false);
}
LstTempID.clear();
// bool bStat = false;
// if (bStat)
// {
// std::map<int, int> m;
// for(VALUEMAPIT it = mMap.begin(); it != mMap.end(); it++)
// {
// m[it->second.mark] ++;
// }
// bStat = false;
// }
}
// VALUEMAPIT it = mMap.find(iMap);
// if (it != mMap.end())
// {
// // JS::RootedValue val(g_cx, it->second->heapValue.get());
// valueArr::add(iArr, iMap);
// return true;
// }
// return false;
//
// std::map<int, JS::Value*> valueRoot::mMap;
// int valueRoot::index = 1;
// int valueRoot::add(JS::HandleValue val)
// {
// JS::Value* pVal = new JS::Value;
// *pVal = val;
// bool ret = JS_AddValueRoot(g_cx, pVal);
// mMap[index] = pVal;
// return index++;
// }
// bool valueRoot::remove(int i)
// {
// std::map<int, JS::Value*>::iterator it = mMap.find(i);
// if (it != mMap.end())
// {
// JS::Value* pVal = it->second;
// JS_RemoveValueRoot(g_cx, pVal);
// delete pVal;
// mMap.erase(it);
// return true;
// }
// return false;
// }