forked from qcwgithub/qjsbmozillajswrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mozjswrap.cpp
1062 lines (918 loc) · 25.6 KB
/
mozjswrap.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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// mozjswrap.cpp
// author: answerwinner
// desc: wraps for SpiderMonkey api
#include "mozjswrap.h"
#ifdef ENABLE_JS_DEBUG
#include "debugger/jsdebugger.h"
#include "global_define.h"
#include "js/OldDebugAPI.h"
#endif
//
// memory for marshaling
//
void* marshalMemory = 0;
int marshalLength = 0;
void* getMarshalMemory(int len)
{
if (marshalMemory == 0 || marshalLength < len)
{
if (marshalMemory)
free(marshalMemory);
marshalMemory = malloc(len);
marshalLength = len;
}
return marshalMemory;
}
char* getMarshalStringFromJSString(JSContext* cx, JS::HandleString jsStr)
{
char* ori = JS_EncodeStringToUTF8(cx, jsStr);
int length = strlen(ori);
char* rt = (char*)::getMarshalMemory(length + 1);
rt[length] = 0;
memcpy(rt, ori, length);
JS_free(cx, (void*)ori);
return rt;
}
JSRuntime* g_rt = 0;
JSContext* g_cx = 0;
//JSObject* g_global = 0;
mozilla::Maybe<JS::PersistentRootedObject> g_global;
OnObjCollected onObjCollected = 0;
bool shutingDown = false;
void sc_finalize(JSFreeOp* freeOp, JSObject* obj)
{
int id = (int)(long long)JS_GetPrivate(obj);
bool bRemoved = valueMap::removeByID(id, true);
if (!shutingDown) Assert(bRemoved, "finalize remove fail");
// notify C#
onObjCollected(id);
}
static JSClass global_class =
{
"global", JSCLASS_GLOBAL_FLAGS,
JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, NULL,
0,0,0,JS_GlobalObjectTraceHook,
//{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
static JSClass debug_global_class =
{
"global", JSCLASS_GLOBAL_FLAGS,
JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, NULL,
0,0,0,JS_GlobalObjectTraceHook,
//{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
static JSClass normal_class =
{
"qiucw_n", 0,
JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, NULL,
0,0,0,0,
//{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
static JSClass class_with_finalizer =
{
"qiucw_f", JSCLASS_HAS_PRIVATE,
JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, sc_finalize,
0,0,0,0,
//{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
int getErroReportLineNo(JSErrorReport* report)
{
return report->lineno;
}
const char* getErroReportFileName(JSErrorReport* report)
{
if (!report->filename)
return "no_file_name";
else
return report->filename;
}
#ifdef ENABLE_JS_DEBUG
////////////////////////////////////////////////////////////////////////////////////
// debugger api
void enableDebugger(const char** src_searchpath, int nums, int port){
jsdebugger::getInstance()->Start(g_cx, &debug_global_class, src_searchpath, nums, port);
}
void updateDebugger(){
jsdebugger::getInstance()->update(1.0f);
}
void cleanupDebugger(){
jsdebugger::Clean();
}
// bool Jsh_RunScript(JSContext* cx, JSObject* global, const char* script_file)
// {
// return jsdebugger::getInstance()->runScript(script_file, global, cx);
// }
//
// void Jsh_CompileScript(JSContext* cx, JSObject* global, const char* script_file)
// {
// return jsdebugger::getInstance()->compileScript(script_file, global, cx);
// }
#else
////////////////////////////////////////////////////////////////////////////////////
// debugger api
void enableDebugger(const char** src_searchpath, int nums, int port) {}
void updateDebugger() {}
void cleanupDebugger() {}
// bool Jsh_RunScript(JSContext* cx, JSObject* global, const char* script_file) { return false;}
// void Jsh_CompileScript(JSContext* cx, JSObject* global, const char* script_file) {}
//MOZ_API void JSh_DumpBacktrace( JSContext* cx ) {}
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////
variableLengthArray<char> SplitUtil::sArr;
// TODO cache?
JSObject* GetJSTableByName(char* name)
{
SplitUtil split(name, ".");
const char* n = split.next();
if (!n) return 0;
JS::RootedObject obj(g_cx, g_global.ref().get());
JS::RootedValue val(g_cx);
while (n)
{
JS_GetProperty(g_cx, obj, n, &val);
if (val.isObject())
{
obj = &val.toObject();
n = split.next();
}
else
{
Assert(false, "GetJSTableByName fail");
return 0;
}
}
return obj;
}
JSObject* getObjCtorPrototype(JS::HandleObject obj)
{
JS::RootedValue val(g_cx);
JS_GetProperty(g_cx, obj, "ctor", &val);
if (val.isObject())
{
JS::RootedObject rO(g_cx, &val.toObject());
JS_GetProperty(g_cx, rO, "prototype", &val);
if (val.isObject())
{
return &val.toObject();
}
}
Assert(false, "getObjCtorPrototype fail");
return 0;
}
/*
why do we need to attach finalizeOp on object?
there are C# objects stored, we have to tell C# to release those objects when js object collected
there are now 2 places calling this function:
1) C# object constructor
example: UnityEngine_GameObjectGenerated.GameObject_GameObject1
when constructor is called, there is already a js object in hand, so, yes, add a finalizeOp to it
2) JSDataExchangeMgr.setObject
it will call createJSClassObject
*/
void attachFinalizerObject(MAPID id)
{
if (valueMap::getHasFinalizeOp(id))
return;
valueMap::setHasFinalizeOp(id, true);
MGETOBJ0(id, obj);
//
// obj.__just_for_finalize = fObj
//
JS::RootedObject fObj(g_cx, JS_NewObject(g_cx, &class_with_finalizer, JS::NullPtr(), JS::NullPtr()));
JS_SetPrivate(fObj, (void*)id);
JS::RootedValue val(g_cx);
val.setObject(*fObj);
JS_DefineProperty(g_cx, obj, "__just_for_finalize", val, 0/* getter */, 0/* setter */, 0/* attr */);
}
/*
几个操作说明,他们达到相同的目的
A. JS: new GameObject.ctor()
-------------------------------------
1) C#: GameObject_GameObject1
2) C#: 对传进来的 _this 调用 attachFinalizerObject
3) C: 添加 jsID -> csObj 的对应关系
B. 当C#中某个操作需要返回一个对象给JS时:createJSClassObject
-------------------------------------
1) C#: JSDataExchangeMgr.setObject
2) C#: 调用 createJSClassObject("GameObject")
3) C: 创建一个普通对象 jsObj,设置 proto = GameObject.ctor.prototype
4) C: 对 jsObj 设置 attachFinalizerObject
C. 当 JSSerizlizer 需要生成一个对象时:newJSClassObject
-------------------------------------
1) 调用 newJSClassObject("GameObject")
2) 接下去 A 的步骤
*/
void jsb_saveStackObj(JS::HandleObject jsObj)
{
//JS::RootedString jsString(g_cx, JS_NewStringCopyZ(g_cx, name));
//JS::RootedValue valName(g_cx);
//valName.setString(jsString);
JS::RootedValue valObj(g_cx);
valObj.setObject(*(jsObj.get()));
JS::Value arr[] = { valObj };
JS::RootedValue _rval(g_cx);
JS::RootedObject roGlobal(g_cx, g_global.ref().get());
bool suc = JS_CallFunctionName(g_cx, roGlobal, "jsb_saveStackObj", JS::HandleValueArray::fromMarkedLocation(1, arr), &_rval);
Assert(suc);
}
JSObject* jsb_getStackObj(char* name)
{
JS::RootedString jsString(g_cx, JS_NewStringCopyZ(g_cx, name));
JS::RootedValue val(g_cx);
val.setString(jsString);
JS::RootedValue _rval(g_cx);
JS::RootedObject roGlobal(g_cx, g_global.ref().get());
bool suc = JS_CallFunctionName(g_cx, roGlobal, "jsb_getStackObj", JS::HandleValueArray::fromMarkedLocation(1, val.address()), &_rval);
Assert(suc);
if (suc && _rval.isObject())
{
return _rval.toObjectOrNull();
}
return 0;
}
//
// 创建一个JS类对象
// 返回jsObj
//
//
// 假设 name = GameObject
// 操作:
// 新增一个JS对象,将其 proto 设置为 GameObject.ctor.prototype
// 并设置 finalizer
// 并把这个对象存起来 返回ID
//
// 注意:这个操作有别于 new GameObject.ctor(),new GameObject.ctor() 是会调用到C#去创建 C#对象,我们这里只是单纯的JS对象
//
JSObject* _createJSClassObject(char* name, _BOOL useCache)
{
if (useCache)
{
JSObject* jsObj = jsb_getStackObj(name);
if (jsObj)
return jsObj;
}
JS::RootedObject _t(g_cx);
if (_t = GetJSTableByName(name))
{
JS::RootedObject tableObj(g_cx, _t);
JS::RootedObject prototypeObj(g_cx, getObjCtorPrototype(tableObj));
JSObject* jsObj = JS_NewObject(g_cx, &normal_class, prototypeObj/* proto */, JS::NullPtr()/* parentProto */);
if (useCache)
{
JS::RootedObject roObj(g_cx, jsObj);
jsb_saveStackObj(roObj);
}
return jsObj;
}
return 0;
}
// to create a C# object,绝对不是 Vector2,Vector3
MOZ_API MAPID createJSClassObject(char* name, _BOOL useCache)
{
JS::RootedObject jsObj(g_cx, _createJSClassObject(name, useCache));
if (jsObj)
{
JS::Value _v;
_v.setObject(*jsObj);
JS::RootedValue val(g_cx, _v);
// 目前对于结构体,_createJSClassObject 有可能返回已经存在的对象
// 这样的话,不可以再 attachFinalizerObject!否则出错
int id = valueMap::getID(val, false);
if (id == 0)
{
id = valueMap::add(val, 1);
attachFinalizerObject(id);
}
return id;
}
return 0;
}
// 如果说 JS ctor不会调用到C# 那么 createXXX newXXX 结果一样
// 这个只有 JSComponent,JSSerializer 会调用,不需要使用缓存
MOZ_API int newJSClassObject(const char* name)
{
JS::RootedString jsString(g_cx, JS_NewStringCopyZ(g_cx, name));
JS::RootedValue val(g_cx);
val.setString(jsString);
JS::RootedValue _rval(g_cx);
JS::RootedObject roGlobal(g_cx, g_global.ref().get());
if (JS_CallFunctionName(g_cx, roGlobal, "jsb_CallObjectCtor", JS::HandleValueArray::fromMarkedLocation(1, val.address()), &_rval))
{
//JS::RootedValue rval(g_cx, _rval);
if (_rval.isObject())
{
return valueMap::add(_rval, 2);
// not need to add finalizeOp here
// if it's pure js object, .. OK
// if it's C# object, attachFinalizerObject will be called from C#
// attachFinalizerObject(id);
}
}
return 0;
}
// MOZ_API bool IsJSClassObjectFunctionExist(OBJID objID, const char* functionName)
// {
// JS::RootedObject obj(g_cx, objMap::id2JSObj(objID));
// if (obj == 0)
// return false;
//
// JS::RootedValue val(g_cx);
// JS_GetProperty(g_cx, obj, functionName, &val);
//
// if (val.isNullOrUndefined())
// return false;
//
// if (!JS_ConvertValue(g_cx, val, JSTYPE_FUNCTION, &val))
// return false;
// return true;
// }
void registerCS(JSNative req)
{
JS::RootedObject CS_obj(g_cx, JS_NewObject(g_cx, &normal_class, JS::NullPtr(), JS::NullPtr()));
JS::RootedValue val(g_cx);
val.setObject(*CS_obj);
JS::RootedObject roGlobal(g_cx, g_global.ref().get());
JS_DefineProperty(g_cx, roGlobal, "CS", val, 0/* getter */, 0/* setter */, 0/* attr */);
JS_DefineFunction(g_cx, CS_obj, "Call", JSCall, 0/* narg */, 0);
JS_DefineFunction(g_cx, CS_obj, "require", req, 0/* narg */, 0);
ppCSObj = new JS::Heap<JSObject*>();
*(ppCSObj) = CS_obj;
//*ppCSObj = CS_obj;
JS::AddObjectRoot(g_cx, ppCSObj);
}
void myJSTraceDataOp(JSTracer *trc, void *data)
{
valueMap::trace(trc);
}
// bool js_print(JSContext *cx, unsigned argc, JS::Value *vp)
// {
// JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
// if (args.get(0).isString())
// {
// JS::RootedString jsStr(cx, args.get(0).toString());
// const char* _buffer = JS_EncodeStringToUTF8(cx, jsStr);
// if (_buffer)
// {
// printf("C: %s\n", _buffer);
// JS_free(cx, (void*)_buffer);
// }
// }
// return true;
// }
// Just a wrapper around JSPrincipals that allows static construction.
class CCJSPrincipals : public JSPrincipals
{
public:
explicit CCJSPrincipals(int rc = 0)
: JSPrincipals()
{
refcount = rc;
}
};
static CCJSPrincipals shellTrustedPrincipals(1);
static bool
CheckObjectAccess(JSContext *cx)
{
return true;
}
static JSSecurityCallbacks securityCallbacks = {
CheckObjectAccess,
NULL
};
bool GCing = false;
int gcCount = 0;
void MyJSGCCallback(JSRuntime *rt, JSGCStatus status, void *data)
{
if (status == JSGC_BEGIN)
{
gcCount++;
GCing = true;
// valueMap::clearVMap();
}
else if (status == JSGC_END)
{
GCing = false;
// valueMap::rebuildVMap();
}
}
int minorGCCount = 0;
//void MyNurseryCollectCallback(JSRuntime *rt, JS::gcreason::Reason, int status /* 0 - start 1 - end*/)
//{
// if (status == 0)
// {
// minorGCCount++;
// valueMap::clearVMap();
// }
// else
// valueMap::rebuildVMap();
//}
//
// return 0: success
//
JSCompartment* oldCompartment = 0;
JSErrorReporter oldErrorReporter = 0;
bool g_firstInit = true;
MOZ_API int InitJSEngine(JSErrorReporter er,
CSEntry entry,
JSNative req,
OnObjCollected onObjCollected,
JSNative print)
{
JSRuntime*& rt = g_rt;
JSContext*& cx = g_cx;
if (g_firstInit)
{
g_firstInit = false;
// JS_SetNurseryCollectCallback(MyNurseryCollectCallback);
if (!JS_Init())
{
return 1;
}
rt = JS_NewRuntime(8 * 1024 * 1024
#ifdef SPIDERMONKEY31
, JS_NO_HELPER_THREADS
#endif
);
JS_SetGCParameter(rt, JSGC_MAX_BYTES, 0xffffffff);
JS_SetTrustedPrincipals(rt, &shellTrustedPrincipals);
JS_SetSecurityCallbacks(rt, &securityCallbacks);
JS_SetNativeStackQuota(rt, 500000, 0, 0);
JS_SetGCCallback(rt, MyJSGCCallback, 0);
}
gcCount = 0;
minorGCCount = 0;
cx = JS_NewContext(rt, 8192);
/*JS::RuntimeOptionsRef(rt).setIon(true);
JS::RuntimeOptionsRef(rt).setBaseline(true);*/
// Set error reporter
oldErrorReporter = JS_SetErrorReporter(cx, er);
JS::CompartmentOptions options;
options.setVersion(JSVERSION_LATEST);
g_global.construct(cx);
g_global.ref() = JS_NewGlobalObject(cx, &global_class, 0/*principals*/, JS::DontFireOnNewGlobalHook, options);
//JSObject*& global = g_global;
JS::RootedObject roGlobal(g_cx, g_global.ref().get());
oldCompartment = JS_EnterCompartment(cx, g_global.ref().get());
if (!JS_InitStandardClasses(cx, roGlobal))
{
return 2;
}
JS_InitReflect(cx, roGlobal);
csEntry = entry;
::onObjCollected = onObjCollected;
JS_AddExtraGCRootsTracer(rt, myJSTraceDataOp, 0/* data */);
//ppCSObj = 0;
registerCS(req);
JS_DefineFunction(cx, roGlobal, "print", print, 0/* narg */, 0);
//JS_SetGCCallback(rt, jsGCCallback, 0/* user data */);
shutingDown = false;
startMapID = valueMap::getIndex();
Assert(endMapID == 0 || startMapID == endMapID);
return 0;
}
MOZ_API void ShutdownJSEngine(_BOOL bCleanup)
{
endMapID = valueMap::getIndex();
// Reset
{
shutingDown = true;
if (ppCSObj)
{
JS::RemoveObjectRoot(g_cx, ppCSObj);
delete ppCSObj;
ppCSObj = 0;
}
idErrorEntry = 0;
//JS_SetErrorReporter(g_cx, oldErrorReporter);
JS_RemoveExtraGCRootsTracer(g_rt, myJSTraceDataOp, 0);
idFunRet = 0;
idSave = 0;
valueMap::clear();
valueArr::clear(false);
JS_LeaveCompartment(g_cx, oldCompartment);
g_global.destroy();
JS_DestroyContext(g_cx);
JS_GC(g_rt);
}
if (bCleanup == _TRUE)
{
JS_DestroyRuntime(g_rt);
JS_ShutDown();
g_cx = 0;
g_rt = 0;
}
}
MOZ_API void getProperty(MAPID id, const char* name)
{
MGETOBJ0(id, jsObj);
if (jsObj == 0)
{
// no error
// defauting to global object
jsObj = g_global.ref().get();
}
JS::RootedValue val(g_cx);
if (!JS_GetProperty(g_cx, jsObj, name, &val))
{
Assert(false, "JS_GetProperty fail");
return;
}
idSave = valueMap::add(val, 3);
}
MOZ_API void setProperty( MAPID id, const char* name, MAPID valueID )
{
MGETOBJ0(id, jsObj);
if (jsObj == 0)
{
// no error
// defauting to global object
jsObj = g_global.ref().get();
}
JS::RootedValue valValue(g_cx);
if (!valueMap::getVal(valueID, &valValue))
{
return;
}
JS_SetProperty(g_cx, jsObj, name, valValue);
}
MOZ_API void getElement(MAPID id, int i)
{
MGETOBJ0(id, obj);
JS::RootedValue val(g_cx);
if (!JS_GetElement(g_cx, obj, i, &val))
{
Assert(false, "JS_GetElement fail");
return;
}
idSave = valueMap::add(val, 3);
//return _TRUE;
}
MOZ_API int getArrayLength(MAPID id)
{
JS::RootedValue valObj(g_cx);
if (!valueMap::getVal(id, &valObj))
return false;
JS::RootedObject jsObj(g_cx, &valObj.toObject());
uint32_t len = 0;
JS_GetArrayLength(g_cx, jsObj, &len);
return (int)len;
}
MOZ_API void gc()
{
JS_GC(g_rt);
}
MOZ_API void MaybeGC()
{
JS_MaybeGC(g_cx);
}
MOZ_API int GetGCCount()
{
return gcCount;
}
MOZ_API int GetMinorGCCount()
{
return minorGCCount;
}
MOZ_API void moveSaveID2Arr( int arrIndex )
{
valueArr::add(arrIndex, idSave);
}
MOZ_API MAPID getSaveID( )
{
return idSave;
}
MOZ_API void removeByID( MAPID id )
{
valueMap::removeByID(id, false);
}
MOZ_API void moveID2Arr(int id, int arrIndex)
{
valueArr::add(arrIndex, id);
}
JS::Heap<JSObject*>* ppCSObj = 0;
MAPID idErrorEntry = 0;
_BOOL initErrorHandler()
{
Assert(idErrorEntry == 0);
JS::RootedObject CS_obj(g_cx, *ppCSObj);
JS::RootedValue val(g_cx);
JS_GetProperty(g_cx, CS_obj, "jsFunctionEntry", &val);
if (val.isObject() &&
JS_ObjectIsFunction(g_cx, &val.toObject()))
{
JS::RootedValue fval(g_cx);
JS_ConvertValue(g_cx, val, JSTYPE_FUNCTION, &fval);
idErrorEntry = valueMap::add(fval, 5);
return _TRUE;
}
return _FALSE;
}
#include <set>
class funArgArrayMgr
{
static std::set<JS::Value*> setFree;
static std::set<JS::Value*> setUsed;
static const int LENGTH = 16; // array length
static bool inited;
static int maxLength;
static JS::Value* newArray()
{
return new JS::Value[LENGTH];
}
// static void init()
// {
// if (inited)
// {
// return;
// }
// for (int i = 0; i < 16; i++)
// {
// setFree.insert(newArray());
// }
// inited = true;
// }
static void reset()
{
if (inited)
{
setFree.insert(setUsed.begin(), setUsed.end());
setUsed.clear();
}
}
public:
static JS::Value* get(int length)
{
// if (!inited)
// {
// init();
// }
if (length > maxLength)
maxLength = length;
if (length <= LENGTH)
{
std::set<JS::Value*>::iterator it = setFree.begin();
if (it != setFree.end())
{
JS::Value* p = *it;
setFree.erase(it);
setUsed.insert(p);
return p;
}
else
{
JS::Value* p = newArray();
setUsed.insert(p);
return p;
}
}
else
{
return new JS::Value[length];
}
}
static void giveBack(JS::Value* arr)
{
std::set<JS::Value*>::iterator it = setUsed.find(arr);
if (it != setUsed.end())
{
setUsed.erase(it);
setFree.insert(arr);
}
else
{
delete[] arr;
}
}
};
std::set<JS::Value*> funArgArrayMgr::setFree;
std::set<JS::Value*> funArgArrayMgr::setUsed;
bool funArgArrayMgr::inited = false;
int funArgArrayMgr::maxLength = 0;
int globalId = -1;
bool callFunctionValueRemoveArr = true;
MOZ_API void setCallFunctionValueRemoveArr(_BOOL bRemove)
{
callFunctionValueRemoveArr = (bRemove == 1);
}
MOZ_API void callFunctionValue(MAPID jsObjID, MAPID funID, int argCount)
{
MGETOBJ0(jsObjID, jsObj);
if (jsObj == 0)
{
// no error
// defauting to global object
jsObj = g_global.ref().get();
}
JS::RootedValue fval(g_cx);
if (!valueMap::getVal(funID, &fval))
{
Assert(false, "callFunctionValue: get function val fail");
return;
}
JS::RootedValue retVal(g_cx);
bool ret;
if (idErrorEntry > 0)
{
JS::Value* arr = funArgArrayMgr::get(argCount + 2);
arr[0].setObjectOrNull(jsObj);
arr[1] = fval;
JS::RootedValue ele(g_cx);
for (int i = 0; i < argCount; i++)
{
bool b = valueMap::getVal(valueArr::arr[i], &ele);
Assert(b);
arr[i + 2] = ele;
}
valueArr::clear(callFunctionValueRemoveArr);
callFunctionValueRemoveArr = true;
JS::RootedValue entryVal(g_cx);
valueMap::getVal(idErrorEntry, &entryVal);
//JSAutoCompartment ac(g_cx, ppCSObj);
JS::RootedObject roCSObj(g_cx, *ppCSObj);
ret = JS_CallFunctionValue(g_cx, roCSObj, entryVal, JS::HandleValueArray::fromMarkedLocation(argCount + 2, arr), &retVal);
Assert(ret);
funArgArrayMgr::giveBack(arr);
}
else
{
if (argCount == 0)
{
ret = JS_CallFunctionValue(g_cx, jsObj, fval, JS::HandleValueArray::empty(), &retVal);
}
else
{
JS::Value* arr = funArgArrayMgr::get(argCount);
JS::RootedValue ele(g_cx);
for (int i = 0; i < argCount; i++)
{
valueMap::getVal(valueArr::arr[i], &ele);
arr[i] = ele;
}
valueArr::clear(callFunctionValueRemoveArr);
callFunctionValueRemoveArr = true;
ret = JS_CallFunctionValue(g_cx, jsObj, fval, JS::HandleValueArray::fromMarkedLocation(argCount, arr), &retVal);
funArgArrayMgr::giveBack(arr);
}
}
// clear value array
valueArr::clear(true);
valueMap::removeByID(idFunRet, false);
idFunRet = valueMap::add(retVal, 6);
return;
}
MOZ_API int incRefCount(MAPID id)
{
return valueMap::incRefCount(id);
}
MOZ_API int decRefCount(MAPID id)
{
return valueMap::decRefCount(id);
}
MOZ_API void setTrace(MAPID id, _BOOL bTrace)
{
valueMap::setTrace(id, (bTrace == _TRUE));
}
MOZ_API _BOOL isTraced(MAPID id)
{
if (valueMap::isTraced(id))
return _TRUE;
else
return _FALSE;
}
MOZ_API void setTempTrace(MAPID id, bool bTempTrace)
{
valueMap::setTempTrace(id, bTempTrace);
}
MOZ_API _BOOL evaluate( const char* ascii, int length, const char* filename )
{
JS::CompileOptions options(g_cx);
options.setVersion(JSVERSION_LATEST);
options.setUTF8(true);
options.setFileAndLine(filename, 1);
JS::RootedObject roGlobal(g_cx, g_global.ref().get());
#ifdef SPIDERMONKEY31
JS::RootedScript jsScript(g_cx, JS_CompileScript(g_cx, roGlobal, ascii, length, options));
#else // V33
JS::RootedScript jsScript(g_cx);
JS_CompileScript(g_cx, roGlobal, ascii, length, options, &jsScript);
#endif
if (jsScript == 0)
{
Assert(false, "JS_CompileScript fail");
return _FALSE;
}
JS::RootedValue val(g_cx);
if (!JS_ExecuteScript(g_cx, roGlobal, jsScript, &val))
{
Assert(false, "JS_ExecuteScript fail");
return _FALSE;
}
// TODO add script root
//JS_AddNamedScriptRoot(g_cx, &jsScript, filename);
return _TRUE;
}
MOZ_API _BOOL evaluate_jsc( const char* ascii, int length, const char* filename )
{
JS::CompileOptions options(g_cx);
options.setVersion(JSVERSION_LATEST);
options.setUTF8(true);
options.setFileAndLine(filename, 1);
JS::RootedObject roGlobal(g_cx, g_global.ref().get());
#ifdef SPIDERMONKEY31
JS::RootedScript jsScript(g_cx, JS_DecodeScript(g_cx, ascii, length, NULL));
#else // V33
JS::RootedScript jsScript(g_cx, JS_DecodeScript(g_cx, ascii, length, NULL));
#endif
if (jsScript == 0)
{
Assert(false, "JS_DecodeScript fail");
return _FALSE;
}
JS::RootedValue val(g_cx);
if (!JS_ExecuteScript(g_cx, roGlobal, jsScript, &val))
{
Assert(false, "JS_ExecuteScript fail");
return _FALSE;
}
// TODO add script root
//JS_AddNamedScriptRoot(g_cx, &jsScript, filename);
return _TRUE;
}
const char* getArgString(jsval* vp, int i)
{
JS::RootedValue val(g_cx, JS_ARGV(g_cx, vp)[i]);
Assert(val.isString());
//JSString* jsStr = val.toString();
JS::RootedString jsStr(g_cx, val.toString());
if (jsStr)
{
return getMarshalStringFromJSString(g_cx, jsStr);
}
return 0;
}
MOZ_API MAPID getObjFunction(MAPID id, const char* fname)
{
// JS::RootedValue objVal(g_cx);
// if (!valueMap::getVal(id, &objVal) || !objVal.isObject())
// return 0;
//
// JS::RootedObject obj(g_cx, &objVal.toObject());