File: as_scriptfunction.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (1563 lines) | stat: -rw-r--r-- 48,901 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
/*
   AngelCode Scripting Library
   Copyright (c) 2003-2023 Andreas Jonsson

   This software is provided 'as-is', without any express or implied
   warranty. In no event will the authors be held liable for any
   damages arising from the use of this software.

   Permission is granted to anyone to use this software for any
   purpose, including commercial applications, and to alter it and
   redistribute it freely, subject to the following restrictions:

   1. The origin of this software must not be misrepresented; you
	  must not claim that you wrote the original software. If you use
	  this software in a product, an acknowledgment in the product
	  documentation would be appreciated but is not required.

   2. Altered source versions must be plainly marked as such, and
	  must not be misrepresented as being the original software.

   3. This notice may not be removed or altered from any source
	  distribution.

   The original version of this library can be located at:
   http://www.angelcode.com/angelscript/

   Andreas Jonsson
   andreas@angelcode.com
*/


//
// as_scriptfunction.cpp
//
// A container for a compiled script function
//



#include "as_config.h"
#include "as_scriptfunction.h"
#include "as_tokendef.h"
#include "as_scriptengine.h"
#include "as_callfunc.h"
#include "as_bytecode.h"
#include "as_texts.h"
#include "as_scriptnode.h"
#include "as_builder.h"
#include "as_scriptcode.h"

#include "common/algorithm.h"

BEGIN_AS_NAMESPACE

#if defined(AS_MAX_PORTABILITY) || defined(AS_NO_CLASS_METHODS)

static void ScriptFunction_AddRef_Generic(asIScriptGeneric *gen) {
	asCScriptFunction *self = (asCScriptFunction *)gen->GetObject();
	self->AddRef();
}

static void ScriptFunction_Release_Generic(asIScriptGeneric *gen) {
	asCScriptFunction *self = (asCScriptFunction *)gen->GetObject();
	self->Release();
}

static void ScriptFunction_GetRefCount_Generic(asIScriptGeneric *gen) {
	asCScriptFunction *self = (asCScriptFunction *)gen->GetObject();
	*(int *)gen->GetAddressOfReturnLocation() = self->GetRefCount();
}

static void ScriptFunction_SetFlag_Generic(asIScriptGeneric *gen) {
	asCScriptFunction *self = (asCScriptFunction *)gen->GetObject();
	self->SetFlag();
}

static void ScriptFunction_GetFlag_Generic(asIScriptGeneric *gen) {
	asCScriptFunction *self = (asCScriptFunction *)gen->GetObject();
	*(bool *)gen->GetAddressOfReturnLocation() = self->GetFlag();
}

static void ScriptFunction_EnumReferences_Generic(asIScriptGeneric *gen) {
	asCScriptFunction *self = (asCScriptFunction *)gen->GetObject();
	asIScriptEngine *engine = *(asIScriptEngine **)gen->GetAddressOfArg(0);
	self->EnumReferences(engine);
}

static void ScriptFunction_ReleaseAllHandles_Generic(asIScriptGeneric *gen) {
	asCScriptFunction *self = (asCScriptFunction *)gen->GetObject();
	asIScriptEngine *engine = *(asIScriptEngine **)gen->GetAddressOfArg(0);
	self->ReleaseAllHandles(engine);
}

#ifdef AS_MAX_PORTABILITY
static void ScriptFunction_CreateDelegate_Generic(asIScriptGeneric *gen) {
	asCScriptFunction *func = (asCScriptFunction *)gen->GetArgAddress(0);
	void *obj = gen->GetArgAddress(1);
	gen->SetReturnAddress(CreateDelegate(func, obj));
}
#endif

// TODO: operator==
/*static void ScriptFunction_opEquals_Generic(asIScriptGeneric *gen)
{
    asCScriptFunction *funcSelf = (asCScriptFunction*)gen->GetObject();
    asCScriptFunction *funcOther = (asCScriptFunction*)gen->GetArgAddress(0);
    *(bool*)gen->GetAddressOfReturnLocation() = *funcSelf == *funcOther;
}
*/

#endif


void RegisterScriptFunction(asCScriptEngine *engine) {
	// Register the gc behaviours for the script functions
	int r = 0;
	UNUSED_VAR(r); // It is only used in debug mode
	engine->functionBehaviours.engine = engine;
	engine->functionBehaviours.flags = asOBJ_REF | asOBJ_GC;
	engine->functionBehaviours.name = "$func";
#ifndef AS_MAX_PORTABILITY
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ADDREF, "void f()", asMETHOD(asCScriptFunction, AddRef), asCALL_THISCALL, 0);
	asASSERT(r >= 0);
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASE, "void f()", asMETHOD(asCScriptFunction, Release), asCALL_THISCALL, 0);
	asASSERT(r >= 0);
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asMETHOD(asCScriptFunction, GetRefCount), asCALL_THISCALL, 0);
	asASSERT(r >= 0);
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_SETGCFLAG, "void f()", asMETHOD(asCScriptFunction, SetFlag), asCALL_THISCALL, 0);
	asASSERT(r >= 0);
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asMETHOD(asCScriptFunction, GetFlag), asCALL_THISCALL, 0);
	asASSERT(r >= 0);
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asMETHOD(asCScriptFunction, EnumReferences), asCALL_THISCALL, 0);
	asASSERT(r >= 0);
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asMETHOD(asCScriptFunction, ReleaseAllHandles), asCALL_THISCALL, 0);
	asASSERT(r >= 0);
	// TODO: Need some way to allow the arg type to adapt when the funcdefs are instantiated
//	r = engine->RegisterMethodToObjectType(&engine->functionBehaviours, "bool opEquals(const int &in)", asMETHOD(asCScriptFunction,operator==), asCALL_THISCALL); asASSERT( r >= 0 );
#else
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ADDREF, "void f()", asFUNCTION(ScriptFunction_AddRef_Generic), asCALL_GENERIC, 0);
	asASSERT(r >= 0);
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASE, "void f()", asFUNCTION(ScriptFunction_Release_Generic), asCALL_GENERIC, 0);
	asASSERT(r >= 0);
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETREFCOUNT, "int f()", asFUNCTION(ScriptFunction_GetRefCount_Generic), asCALL_GENERIC, 0);
	asASSERT(r >= 0);
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_SETGCFLAG, "void f()", asFUNCTION(ScriptFunction_SetFlag_Generic), asCALL_GENERIC, 0);
	asASSERT(r >= 0);
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_GETGCFLAG, "bool f()", asFUNCTION(ScriptFunction_GetFlag_Generic), asCALL_GENERIC, 0);
	asASSERT(r >= 0);
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_ENUMREFS, "void f(int&in)", asFUNCTION(ScriptFunction_EnumReferences_Generic), asCALL_GENERIC, 0);
	asASSERT(r >= 0);
	r = engine->RegisterBehaviourToObjectType(&engine->functionBehaviours, asBEHAVE_RELEASEREFS, "void f(int&in)", asFUNCTION(ScriptFunction_ReleaseAllHandles_Generic), asCALL_GENERIC, 0);
	asASSERT(r >= 0);
//	r = engine->RegisterMethodToObjectType(&engine->functionBehaviours, "bool opEquals(const int &in)", asFUNCTION(ScriptFunction_opEquals_Generic), asCALL_GENERIC); asASSERT( r >= 0 );
#endif

	// Register the builtin function for creating delegates
	// This function returns a handle to the delegate, but since the type is not known at this time it is
	// registered to return a void then the return type is changed manually to the builtin function type
	// The name of the function is an invalid identifier so it cannot be invoked accidentally from the script
#ifndef AS_MAX_PORTABILITY
	r = engine->RegisterGlobalFunction("void f(int &in, int &in)", asFUNCTION(CreateDelegate), asCALL_CDECL);
	asASSERT(r >= 0);
#else
	r = engine->RegisterGlobalFunction("void f(int &in, int &in)", asFUNCTION(ScriptFunction_CreateDelegate_Generic), asCALL_GENERIC);
	asASSERT(r >= 0);
#endif

	// Rename the function so that it cannot be called manually by the script
	int idx = engine->registeredGlobalFuncs.GetIndex(engine->scriptFunctions[r]);
	engine->registeredGlobalFuncs.Erase(idx);
	engine->scriptFunctions[r]->name = DELEGATE_FACTORY;
	engine->registeredGlobalFuncs.Put(engine->scriptFunctions[r]);

	// Change the return type so the VM will know the function really returns a handle
	engine->scriptFunctions[r]->returnType = asCDataType::CreateType(&engine->functionBehaviours, false);
	engine->scriptFunctions[r]->returnType.MakeHandle(true);
}

asCScriptFunction *CreateDelegate(asCScriptFunction *func, void *obj) {
	if (func == 0 || obj == 0) {
		// TODO: delegate: Should set script exception
		return 0;
	}

	// Create an instance of a asCScriptFunction with the type asFUNC_DELEGATE
	// The delegate shouldn't have a function id and is not added to the engine->scriptFunctions
	asCScriptFunction *delegate = asNEW(asCScriptFunction)(static_cast<asCScriptEngine *>(func->GetEngine()), 0, asFUNC_DELEGATE);
	if (delegate)
		delegate->MakeDelegate(func, obj);

	return delegate;
}

// internal
void asCScriptFunction::MakeDelegate(asCScriptFunction *func, void *obj) {
	// Increase the reference of the function and object
	func->AddRef();
	funcForDelegate = func;

	func->GetEngine()->AddRefScriptObject(obj, func->GetObjectType());
	objForDelegate = obj;

	// The return type and parameters are copied from the delegated method to this object
	// TODO: optimize: Do we really need to copy? Whenever requested the delegate can simply return the delegated methods' info directly
	parameterTypes = func->parameterTypes;
	returnType     = func->returnType;
	inOutFlags     = func->inOutFlags;

	// The delegate doesn't own the parameters as it will only forward them to the real method
	// so the exception handler must not clean up the parameters for the delegate
	dontCleanUpOnException = true;
}

// interface
void *asCScriptFunction::GetAuxiliary() const {
	if (sysFuncIntf)
		return sysFuncIntf->auxiliary;

	return 0;
}

// interface
void *asCScriptFunction::GetDelegateObject() const {
	return objForDelegate;
}

// interface
asITypeInfo *asCScriptFunction::GetDelegateObjectType() const {
	if (objForDelegate == 0 || funcForDelegate == 0)
		return 0;

	return funcForDelegate->objectType;
}

// interface
asIScriptFunction *asCScriptFunction::GetDelegateFunction() const {
	return funcForDelegate;
}

// TODO: operator==
/*
// internal
bool asCScriptFunction::operator==(const asCScriptFunction &other) const
{
    if( this == &other ) return true;

    if( this->funcType == asFUNC_DELEGATE && other.funcType == asFUNC_DELEGATE )
    {
        if( this->objForDelegate == other.objForDelegate &&
            this->funcForDelegate == other.funcForDelegate )
            return true;
    }

    return false;
}
*/

// internal
int asCScriptFunction::RegisterListPattern(const char *decl, asCScriptNode *listNodes) {
	if (listNodes == 0)
		return asINVALID_ARG;

	// Build the representation of the list pattern from the script nodes
	asSListPatternNode *node;
	listPattern = asNEW(asSListPatternNode)(asLPT_START);
	node = listPattern;

	// Recursively parse the child
	int r = ParseListPattern(node, decl, listNodes);

	node->next = asNEW(asSListPatternNode)(asLPT_END);

	return r;
}

// internal
int asCScriptFunction::ParseListPattern(asSListPatternNode *&target, const char *decl, asCScriptNode *listNodes) {
	asSListPatternNode *node = target;

	listNodes = listNodes->firstChild;
	while (listNodes) {
		if (listNodes->nodeType == snIdentifier) {
			asCString token(&decl[listNodes->tokenPos], listNodes->tokenLength);
			if (token == "repeat") {
				node->next = asNEW(asSListPatternNode)(asLPT_REPEAT);
				node = node->next;
			} else if (token == "repeat_same") {
				// TODO: list: Should make sure this is a sub-list
				node->next = asNEW(asSListPatternNode)(asLPT_REPEAT_SAME);
				node = node->next;
			} else {
				// Shouldn't happen as the parser already reported the error
				asASSERT(false);
			}
		} else if (listNodes->nodeType == snDataType) {
			asCDataType dt;
			asCBuilder builder(engine, 0);
			asCScriptCode code;
			code.SetCode("", decl, 0, false);

			// For list factory we get the object type from the return type, for list constructor we get it from the object type directly
			dt = builder.CreateDataTypeFromNode(listNodes, &code, engine->defaultNamespace, false, objectType ? objectType : CastToObjectType(returnType.GetTypeInfo()));

			node->next = asNEW(asSListPatternDataTypeNode)(dt);
			node = node->next;
		} else if (listNodes->nodeType == snListPattern) {
			node->next = asNEW(asSListPatternNode)(asLPT_START);
			node = node->next;

			// Recursively parse the child
			int r = ParseListPattern(node, decl, listNodes);
			if (r < 0)
				return r;

			node->next = asNEW(asSListPatternNode)(asLPT_END);
			node = node->next;
		} else {
			// Unexpected token in the list, the parser shouldn't have allowed
			asASSERT(false);
			return -1;
		}

		listNodes = listNodes->next;
	}

	target = node;
	return 0;
}

// internal
asCScriptFunction::asCScriptFunction(asCScriptEngine *_engine, asCModule *mod, asEFuncType _funcType) {
	funcType               = _funcType;
	if (funcType == asFUNC_DELEGATE) {
		// Delegates behave like object instances, rather than script code
		externalRefCount.set(1);
		internalRefCount.set(0);
	} else {
		internalRefCount.set(1);
		externalRefCount.set(0);
	}

	this->engine           = _engine;
	this->scriptData       = 0;
	module                 = mod;
	objectType             = 0;
	name                   = "";
	sysFuncIntf            = 0;
	signatureId            = 0;
	dontCleanUpOnException = false;
	vfTableIdx             = -1;
	gcFlag                 = false;
	userData               = 0;
	id                     = 0;
	accessMask             = 0xFFFFFFFF;
	nameSpace              = engine->nameSpaces[0];
	objForDelegate         = 0;
	funcForDelegate        = 0;
	listPattern            = 0;
	funcdefType            = 0;

	if (funcType == asFUNC_SCRIPT)
		AllocateScriptFunctionData();

	// Notify the GC of delegates
	if (funcType == asFUNC_DELEGATE)
		engine->gc.AddScriptObjectToGC(this, &engine->functionBehaviours);
}

void asCScriptFunction::AllocateScriptFunctionData() {
	if (scriptData) return;

	scriptData = asNEW(ScriptFunctionData);

	scriptData->stackNeeded      = 0;
	scriptData->variableSpace    = 0;
	scriptData->scriptSectionIdx = -1;
	scriptData->declaredAt       = 0;
	scriptData->jitFunction      = 0;
}

void asCScriptFunction::DeallocateScriptFunctionData() {
	if (!scriptData) return;

	for (asUINT n = 0; n < scriptData->variables.GetLength(); n++)
		asDELETE(scriptData->variables[n], asSScriptVariable);
	scriptData->variables.SetLength(0);

	asDELETE(scriptData, ScriptFunctionData);
	scriptData = 0;
}

// internal
asCScriptFunction::~asCScriptFunction() {
	// Dummy functions that are allocated on the stack are not reference counted
	asASSERT(funcType == asFUNC_DUMMY    ||
	         (externalRefCount.get() == 0 && internalRefCount.get() == 0));

	// Remove the script function from the engine's scriptFunctions array here
	// Don't remove it before, because there may still be functions referring to it
	// by index until now. If it was removed in DestroyInternal, those would not
	// be able to release the refcount, thus causing memory leak.
	if (engine && id != 0 && funcType != asFUNC_DUMMY)
		engine->RemoveScriptFunction(this);

	// If the engine pointer is 0, then DestroyInternal has already been called and there is nothing more to do
	if (engine == 0) return;

	DestroyInternal();

	// Finally set the engine pointer to 0 because it must not be accessed again
	engine = 0;
}

// internal
void asCScriptFunction::DestroyHalfCreated() {
	asASSERT(externalRefCount.get() == 0 && internalRefCount.get() == 1);

	// Set the funcType to dummy so the destructor won't complain
	funcType = asFUNC_DUMMY;

	// If the bytecode exist remove it before destroying, otherwise it
	// will fail when the destructor releases the references as the bytecode
	// is not fully constructed.
	if (scriptData)
		scriptData->byteCode.SetLength(0);

	asDELETE(this, asCScriptFunction);
}

// internal
void asCScriptFunction::DestroyInternal() {
	// Clean up user data
	for (asUINT n = 0; n < userData.GetLength(); n += 2) {
		if (userData[n + 1]) {
			for (asUINT c = 0; c < engine->cleanFunctionFuncs.GetLength(); c++)
				if (engine->cleanFunctionFuncs[c].type == userData[n])
					engine->cleanFunctionFuncs[c].cleanFunc(this);
		}
	}
	userData.SetLength(0);

	// Release all references the function holds to other objects
	ReleaseReferences();
	parameterTypes.SetLength(0);
	returnType = asCDataType::CreatePrimitive(ttVoid, false);

	for (asUINT p = 0; p < defaultArgs.GetLength(); p++)
		if (defaultArgs[p])
			asDELETE(defaultArgs[p], asCString);
	defaultArgs.SetLength(0);

	if (sysFuncIntf)
		asDELETE(sysFuncIntf, asSSystemFunctionInterface);
	sysFuncIntf = 0;

	if (objectType) {
		objectType->ReleaseInternal();
		objectType = 0;
	}

	DeallocateScriptFunctionData();

	// Deallocate list pattern data
	while (listPattern) {
		asSListPatternNode *n = listPattern->next;
		asDELETE(listPattern, asSListPatternNode);
		listPattern = n;
	}
}

// interface
int asCScriptFunction::GetId() const {
	return id;
}

// interface
int asCScriptFunction::AddRef() const {
	gcFlag = false;
	return externalRefCount.atomicInc();
}

// interface
int asCScriptFunction::Release() const {
	gcFlag = false;
	int r = externalRefCount.atomicDec();
	if (r == 0 &&
	        funcType != asFUNC_DUMMY) {   // Dummy functions are allocated on the stack and cannot be deleted
		// There are no more external references, if there are also no
		// internal references then it is time to delete the function
		if (internalRefCount.get() == 0) {
			// If there are no internal references, then no module is owning the function
			// For example if the function was dynamically compiled without adding it to the scope of the module
			asASSERT(module == 0);

			asDELETE(const_cast<asCScriptFunction *>(this), asCScriptFunction);
		}
	}

	return r;
}

// internal
int asCScriptFunction::AddRefInternal() {
	return internalRefCount.atomicInc();
}

// internal
int asCScriptFunction::ReleaseInternal() {
	int r = internalRefCount.atomicDec();
	if (r == 0 &&
	        funcType != asFUNC_DUMMY) {
		// There are no more internal references, if there are also no
		// external references then it is time to delete the function
		if (externalRefCount.get() == 0) {
			asDELETE(const_cast<asCScriptFunction *>(this), asCScriptFunction);
		}
	}

	return r;
}

// interface
int asCScriptFunction::GetTypeId() const {
	// This const cast is ok, the object won't be modified
	asCDataType dt = asCDataType::CreateType(engine->FindMatchingFuncdef(const_cast<asCScriptFunction *>(this), 0), false);
	return engine->GetTypeIdFromDataType(dt);
}

// interface
bool asCScriptFunction::IsCompatibleWithTypeId(int typeId) const {
	asCDataType dt = engine->GetDataTypeFromTypeId(typeId);

	// Make sure the type is a function
	if (!dt.IsFuncdef())
		return false;

	asCScriptFunction *func = CastToFuncdefType(dt.GetTypeInfo())->funcdef;
	if (!IsSignatureExceptNameEqual(func))
		return false;

	// If this is a class method, then only return true if the object type is the same
	if (objectType != func->objectType)
		return false;

	return true;
}

// interface
const char *asCScriptFunction::GetModuleName() const {
	if (module)
		return module->GetName();

	return 0;
}

// interface
asIScriptModule *asCScriptFunction::GetModule() const {
	return module;
}

// interface
asITypeInfo *asCScriptFunction::GetObjectType() const {
	return objectType;
}

// interface
const char *asCScriptFunction::GetObjectName() const {
	if (objectType)
		return objectType->GetName();

	return 0;
}

// interface
const char *asCScriptFunction::GetName() const {
	return name.AddressOf();
}

// interface
const char *asCScriptFunction::GetNamespace() const {
	if (nameSpace)
		return nameSpace->name.AddressOf();

	return 0;
}

// interface
bool asCScriptFunction::IsReadOnly() const {
	return traits.GetTrait(asTRAIT_CONST);
}

// interface
bool asCScriptFunction::IsPrivate() const {
	return traits.GetTrait(asTRAIT_PRIVATE);
}

// interface
bool asCScriptFunction::IsProtected() const {
	return traits.GetTrait(asTRAIT_PROTECTED);
}

// internal
int asCScriptFunction::GetSpaceNeededForArguments() {
	// We need to check the size for each type
	int s = 0;
	for (asUINT n = 0; n < parameterTypes.GetLength(); n++)
		s += parameterTypes[n].GetSizeOnStackDWords();

	return s;
}

// internal
int asCScriptFunction::GetSpaceNeededForReturnValue() {
	return returnType.GetSizeOnStackDWords();
}

// internal
bool asCScriptFunction::DoesReturnOnStack() const {
	if (returnType.GetTypeInfo() &&
	        (returnType.GetTypeInfo()->flags & asOBJ_VALUE) &&
	        !returnType.IsReference())
		return true;

	return false;
}

// internal
asCString asCScriptFunction::GetDeclarationStr(bool includeObjectName, bool includeNamespace, bool includeParamNames) const {
	asCString str;

	// TODO: default arg: Make the declaration with the default args an option

	// Don't add the return type for constructors and destructors
	if (!(returnType.GetTokenType() == ttVoid &&
	        objectType &&
	        (name == objectType->name || (name.GetLength() > 0 && name[0] == '~') ||
	         name == "$beh0" || name == "$beh2"))) {
		str = returnType.Format(nameSpace, includeNamespace);
		str += " ";
	}
	if (objectType && includeObjectName) {
		if (includeNamespace && objectType->nameSpace->name != "")
			str += objectType->nameSpace->name + "::";

		if (objectType->name != "")
			str += objectType->name + "::";
		else
			str += "_unnamed_type_::";
	} else if (funcdefType && funcdefType->parentClass && includeObjectName) {
		if (includeNamespace && funcdefType->parentClass->nameSpace->name != "")
			str += funcdefType->parentClass->nameSpace->name + "::";

		if (funcdefType->parentClass->name != "")
			str += funcdefType->parentClass->name + "::";
		else
			str += "_unnamed_type_::";
	} else if (includeNamespace && nameSpace->name != "" && !objectType) {
		str += nameSpace->name + "::";
	}
	if (name == "")
		str += "_unnamed_function_(";
	else if (name.SubString(0, 4) == "$beh" && name.GetLength() == 5) {
		if (name[4] == '0' + asBEHAVE_CONSTRUCT)
			str += objectType->name + "(";
		else if (name[4] == '0' + asBEHAVE_FACTORY)
			str += returnType.GetTypeInfo()->name + "(";
		else if (name[4] == '0' + asBEHAVE_DESTRUCT)
			str += "~" + objectType->name + "(";
		else
			str += name + "(";
	} else
		str += name + "(";

	if (parameterTypes.GetLength() > 0) {
		asUINT n;
		for (n = 0; n < parameterTypes.GetLength() - 1; n++) {
			str += parameterTypes[n].Format(nameSpace, includeNamespace);
			if (parameterTypes[n].IsReference() && inOutFlags.GetLength() > n) {
				if (inOutFlags[n] == asTM_INREF) str += "in";
				else if (inOutFlags[n] == asTM_OUTREF) str += "out";
				else if (inOutFlags[n] == asTM_INOUTREF) str += "inout";
			}

			if (includeParamNames && n < parameterNames.GetLength() && parameterNames[n].GetLength() != 0) {
				str += " ";
				str += parameterNames[n];
			}

			if (defaultArgs.GetLength() > n && defaultArgs[n]) {
				asCString tmp;
				tmp.Format(" = %s", defaultArgs[n]->AddressOf());
				str += tmp;
			}

			str += ", ";
		}

		// Add the last parameter
		str += parameterTypes[n].Format(nameSpace, includeNamespace);
		if (parameterTypes[n].IsReference() && inOutFlags.GetLength() > n) {
			if (inOutFlags[n] == asTM_INREF) str += "in";
			else if (inOutFlags[n] == asTM_OUTREF) str += "out";
			else if (inOutFlags[n] == asTM_INOUTREF) str += "inout";
		}

		if (includeParamNames && n < parameterNames.GetLength() && parameterNames[n].GetLength() != 0) {
			str += " ";
			str += parameterNames[n];
		}

		if (defaultArgs.GetLength() > n && defaultArgs[n]) {
			asCString tmp;
			tmp.Format(" = %s", defaultArgs[n]->AddressOf());
			str += tmp;
		}
	}

	str += ")";

	if (IsReadOnly())
		str += " const";

	// Add the declaration of the list pattern
	if (listPattern) {
		asSListPatternNode *n = listPattern;
		bool first = true;
		while (n) {
			if (n->type == asLPT_START) {
				str += " {";
				first = true;
			} else if (n->type == asLPT_END) {
				str += " }";
				first = false;
			} else if (n->type == asLPT_REPEAT)
				str += " repeat";
			else if (n->type == asLPT_REPEAT_SAME)
				str += " repeat_same";
			else if (n->type == asLPT_TYPE) {
				if (first) {
					str += " ";
					first = false;
				} else
					str += ", ";
				str += reinterpret_cast<asSListPatternDataTypeNode *>(n)->dataType.Format(nameSpace, includeNamespace);
			}

			n = n->next;
		}
	}

	return str;
}

// interface
int asCScriptFunction::FindNextLineWithCode(int line) const {
	if (scriptData == 0) return -1;
	if (scriptData->lineNumbers.GetLength() == 0) return -1;

	// The line numbers for constructors are not in order due to the way
	// class members can be initialized directly in the declaration
	if (objectType && objectType->name == name) {
		// Sort all line numbers before looking for the next
		asCArray<int> lineNbrs;
		for (asUINT n = 1; n < scriptData->lineNumbers.GetLength(); n += 2)
			lineNbrs.PushLast(scriptData->lineNumbers[n] & 0xFFFFF);

		Common::sort(&lineNbrs[0], &lineNbrs[0] + lineNbrs.GetLength());

		if (line < lineNbrs[0] && line < (scriptData->declaredAt & 0xFFFFF)) return -1;
		if (line > lineNbrs[lineNbrs.GetLength() - 1]) return -1;

		// Find the line with code on or right after the input line
		// TODO: optimize: Do binary search
		for (asUINT n = 0; n < lineNbrs.GetLength(); n++)
			if (line <= lineNbrs[n])
				return lineNbrs[n];
	} else {
		// Check if given line is outside function
		if (line < (scriptData->declaredAt & 0xFFFFF)) return -1;
		if (line > (scriptData->lineNumbers[scriptData->lineNumbers.GetLength() - 1] & 0xFFFFF)) return -1;

		// Find the line with code on or right after the input line
		// TODO: optimize: Do binary search instead
		for (asUINT n = 1; n < scriptData->lineNumbers.GetLength(); n += 2) {
			if (line <= (scriptData->lineNumbers[n] & 0xFFFFF))
				return (scriptData->lineNumbers[n] & 0xFFFFF);
		}
	}

	return -1;
}

// internal
int asCScriptFunction::GetLineNumber(int programPosition, int *sectionIdx) {
	asASSERT(scriptData);

	if (sectionIdx) *sectionIdx = scriptData->scriptSectionIdx;
	if (scriptData->lineNumbers.GetLength() == 0) return 0;

	if (sectionIdx) {
		// Find the correct section index if the function is compiled from multiple sections
		// This array will be empty most of the time so we don't need a sofisticated algorithm to search it
		for (asUINT n = 0; n < scriptData->sectionIdxs.GetLength(); n += 2) {
			if (scriptData->sectionIdxs[n] <= programPosition)
				*sectionIdx = scriptData->sectionIdxs[n + 1];
		}
	}

	// Do a binary search in the buffer
	int max = (int)scriptData->lineNumbers.GetLength() / 2 - 1;
	int min = 0;
	int i = max / 2;

	for (;;) {
		if (scriptData->lineNumbers[i * 2] < programPosition) {
			// Have we found the largest number < programPosition?
			if (max == i) return scriptData->lineNumbers[i * 2 + 1];
			if (scriptData->lineNumbers[i * 2 + 2] > programPosition) return scriptData->lineNumbers[i * 2 + 1];

			min = i + 1;
			i = (max + min) / 2;
		} else if (scriptData->lineNumbers[i * 2] > programPosition) {
			// Have we found the smallest number > programPosition?
			if (min == i) return scriptData->lineNumbers[i * 2 + 1];

			max = i - 1;
			i = (max + min) / 2;
		} else {
			// We found the exact position
			return scriptData->lineNumbers[i * 2 + 1];
		}
	}
}

// interface
asEFuncType asCScriptFunction::GetFuncType() const {
	return funcType;
}

// interface
asUINT asCScriptFunction::GetVarCount() const {
	if (scriptData)
		return asUINT(scriptData->variables.GetLength());
	return 0;
}

// interface
int asCScriptFunction::GetVar(asUINT index, const char **out_name, int *out_typeId) const {
	if (scriptData == 0)
		return asNOT_SUPPORTED;
	if (index >= scriptData->variables.GetLength())
		return asINVALID_ARG;

	if (out_name)
		*out_name = scriptData->variables[index]->name.AddressOf();
	if (out_typeId)
		*out_typeId = engine->GetTypeIdFromDataType(scriptData->variables[index]->type);

	return asSUCCESS;
}

// interface
const char *asCScriptFunction::GetVarDecl(asUINT index, bool includeNamespace) const {
	if (scriptData == 0 || index >= scriptData->variables.GetLength())
		return 0;

	asCString *tempString = &asCThreadManager::GetLocalData()->string;
	*tempString = scriptData->variables[index]->type.Format(nameSpace, includeNamespace);
	*tempString += " " + scriptData->variables[index]->name;

	return tempString->AddressOf();
}

// internal
void asCScriptFunction::AddVariable(asCString &in_name, asCDataType &in_type, int in_stackOffset) {
	asASSERT(scriptData);
	asSScriptVariable *var = asNEW(asSScriptVariable);
	if (var == 0) {
		// Out of memory
		return;
	}
	var->name                 = in_name;
	var->type                 = in_type;
	var->stackOffset          = in_stackOffset;
	var->declaredAtProgramPos = 0;
	scriptData->variables.PushLast(var);
}

// internal
asCTypeInfo *asCScriptFunction::GetTypeInfoOfLocalVar(short varOffset) {
	asASSERT(scriptData);

	for (asUINT n = 0; n < scriptData->objVariablePos.GetLength(); n++) {
		if (scriptData->objVariablePos[n] == varOffset)
			return scriptData->objVariableTypes[n];
	}

	return 0;
}

// internal
void asCScriptFunction::ComputeSignatureId() {
	// This function will compute the signatureId based on the
	// function name, return type, and parameter types. The object
	// type for methods is not used, so that class methods and
	// interface methods match each other.
	for (asUINT n = 0; n < engine->signatureIds.GetLength(); n++) {
		if (!IsSignatureEqual(engine->signatureIds[n])) continue;

		// We don't need to increment the reference counter here, because
		// asCScriptEngine::FreeScriptFunctionId will maintain the signature
		// id as the function is freed.
		signatureId = engine->signatureIds[n]->signatureId;
		return;
	}

	signatureId = id;
	engine->signatureIds.PushLast(this);
}

// internal
bool asCScriptFunction::IsSignatureEqual(const asCScriptFunction *func) const {
	if (name != func->name || !IsSignatureExceptNameEqual(func)) return false;

	return true;
}

// internal
bool asCScriptFunction::IsSignatureExceptNameEqual(const asCScriptFunction *func) const {
	return IsSignatureExceptNameEqual(func->returnType, func->parameterTypes, func->inOutFlags, func->objectType, func->IsReadOnly());
}

// internal
bool asCScriptFunction::IsSignatureExceptNameEqual(const asCDataType &retType, const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &paramInOut, const asCObjectType *objType, bool readOnly) const {
	if (this->returnType != retType) return false;

	return IsSignatureExceptNameAndReturnTypeEqual(paramTypes, paramInOut, objType, readOnly);
}

// internal
bool asCScriptFunction::IsSignatureExceptNameAndObjectTypeEqual(const asCScriptFunction *func) const {
	return IsSignatureExceptNameEqual(func->returnType, func->parameterTypes, func->inOutFlags, objectType, IsReadOnly());
}

// internal
bool asCScriptFunction::IsSignatureExceptNameAndReturnTypeEqual(const asCScriptFunction *func) const {
	return IsSignatureExceptNameAndReturnTypeEqual(func->parameterTypes, func->inOutFlags, func->objectType, func->IsReadOnly());
}

// internal
bool asCScriptFunction::IsSignatureExceptNameAndReturnTypeEqual(const asCArray<asCDataType> &paramTypes, const asCArray<asETypeModifiers> &paramInOut, const asCObjectType *objType, bool readOnly) const {
	if (this->IsReadOnly()      != readOnly) return false;
	if ((this->objectType != 0) != (objType != 0)) return false;
	if (this->inOutFlags        != paramInOut) return false;
	if (this->parameterTypes    != paramTypes) return false;

	return true;
}

// internal
void asCScriptFunction::AddReferences() {
	// This array will be used to make sure we only add the reference to the same resource once
	// This is especially important for global variables, as it expects the initialization function
	// to hold only one reference to the variable. However, if the variable is initialized through
	// the default constructor followed by the assignment operator we will have two references to
	// the variable in the function.
	asCArray<void *> ptrs;

	// Only count references if there is any bytecode
	if (scriptData && scriptData->byteCode.GetLength()) {
		if (returnType.GetTypeInfo()) {
			returnType.GetTypeInfo()->AddRefInternal();

			asCConfigGroup *group = engine->FindConfigGroupForTypeInfo(returnType.GetTypeInfo());
			if (group != 0) group->AddRef();
		}

		for (asUINT p = 0; p < parameterTypes.GetLength(); p++)
			if (parameterTypes[p].GetTypeInfo()) {
				parameterTypes[p].GetTypeInfo()->AddRefInternal();

				asCConfigGroup *group = engine->FindConfigGroupForTypeInfo(parameterTypes[p].GetTypeInfo());
				if (group != 0) group->AddRef();
			}

		for (asUINT v = 0; v < scriptData->objVariableTypes.GetLength(); v++)
			if (scriptData->objVariableTypes[v]) { // The null handle is also stored, but it doesn't have an object type
				scriptData->objVariableTypes[v]->AddRefInternal();

				asCConfigGroup *group = engine->FindConfigGroupForTypeInfo(scriptData->objVariableTypes[v]);
				if (group != 0) group->AddRef();
			}

		// Go through the byte code and add references to all resources used by the function
		asCArray<asDWORD> &bc = scriptData->byteCode;
		for (asUINT n = 0; n < bc.GetLength(); n += asBCTypeSize[asBCInfo[*(asBYTE *)&bc[n]].type]) {
			switch (*(asBYTE *)&bc[n]) {
			// Object types
			case asBC_OBJTYPE:
			case asBC_FREE:
			case asBC_REFCPY:
			case asBC_RefCpyV: {
				asCObjectType *objType = (asCObjectType *)asBC_PTRARG(&bc[n]);
				asASSERT(objType);
				if (objType)
					objType->AddRefInternal();
			}
			break;

			// Object type and function
			case asBC_ALLOC: {
				asCObjectType *objType = (asCObjectType *)asBC_PTRARG(&bc[n]);
				asASSERT(objType);
				if (objType)
					objType->AddRefInternal();

				int funcId = asBC_INTARG(&bc[n] + AS_PTR_SIZE);
				if (funcId)
					engine->scriptFunctions[funcId]->AddRefInternal();
			}
			break;

			// Global variables
			case asBC_PGA:
			case asBC_PshGPtr:
			case asBC_LDG:
			case asBC_PshG4:
			case asBC_LdGRdR4:
			case asBC_CpyGtoV4:
			case asBC_CpyVtoG4:
			case asBC_SetG4:
				// Need to increase the reference for each global variable
			{
				void *gvarPtr = (void *)asBC_PTRARG(&bc[n]);
				if (!gvarPtr) break;
				asCGlobalProperty *prop = GetPropertyByGlobalVarPtr(gvarPtr);

				if (!prop) {
					// The pointer is a string constant. In order to make sure the correct resource
					// management is maintained we request a new string constant here, so the compiler
					// or bytecode loader can release its copy afterwards.
					asCString str;
					asUINT length;
					int r = engine->stringFactory->GetRawStringData(gvarPtr, 0, &length);
					if (r >= 0) {
						str.SetLength(length);
						engine->stringFactory->GetRawStringData(gvarPtr, str.AddressOf(), &length);

						// Get a new pointer (depending on the string factory implementation it may actually be the same)
						gvarPtr = const_cast<void *>(engine->stringFactory->GetStringConstant(str.AddressOf(), length));
						asBC_PTRARG(&bc[n]) = (asPWORD)gvarPtr;
					}

					// If we get an error from the string factory there is not
					// anything we can do about it, except report a message.
					// TODO: NEWSTRING: Write a message and then exit gracefully
					asASSERT(r >= 0);
					break;
				}

				// Only addref the properties once
				if (!ptrs.Exists(gvarPtr)) {
					prop->AddRef();
					ptrs.PushLast(gvarPtr);
				}

				asCConfigGroup *group = engine->FindConfigGroupForGlobalVar(prop->id);
				if (group != 0) group->AddRef();
			}
			break;

			// System functions
			case asBC_CALLSYS: {
				int funcId = asBC_INTARG(&bc[n]);
				asCConfigGroup *group = engine->FindConfigGroupForFunction(funcId);
				if (group != 0) group->AddRef();

				asASSERT(funcId > 0);
				if (funcId > 0)
					engine->scriptFunctions[funcId]->AddRefInternal();
			}
			break;

			// Functions
			case asBC_CALL:
			case asBC_CALLINTF: {
				int funcId = asBC_INTARG(&bc[n]);
				asASSERT(funcId > 0);
				if (funcId > 0)
					engine->scriptFunctions[funcId]->AddRefInternal();
			}
			break;

			// Function pointers
			case asBC_FuncPtr: {
				asCScriptFunction *func = (asCScriptFunction *)asBC_PTRARG(&bc[n]);
				asASSERT(func);
				if (func)
					func->AddRefInternal();
			}
			break;
			}
		}
	}
}

// internal
void asCScriptFunction::ReleaseReferences() {
	asCArray<void *> ptrs;

	// Only count references if there is any bytecode
	if (scriptData && scriptData->byteCode.GetLength()) {
		if (returnType.GetTypeInfo()) {
			returnType.GetTypeInfo()->ReleaseInternal();

			asCConfigGroup *group = engine->FindConfigGroupForTypeInfo(returnType.GetTypeInfo());
			if (group != 0) group->Release();
		}

		for (asUINT p = 0; p < parameterTypes.GetLength(); p++)
			if (parameterTypes[p].GetTypeInfo()) {
				parameterTypes[p].GetTypeInfo()->ReleaseInternal();

				asCConfigGroup *group = engine->FindConfigGroupForTypeInfo(parameterTypes[p].GetTypeInfo());
				if (group != 0) group->Release();
			}

		for (asUINT v = 0; v < scriptData->objVariableTypes.GetLength(); v++)
			if (scriptData->objVariableTypes[v]) { // The null handle is also stored, but it doesn't have an object type
				scriptData->objVariableTypes[v]->ReleaseInternal();

				asCConfigGroup *group = engine->FindConfigGroupForTypeInfo(scriptData->objVariableTypes[v]);
				if (group != 0) group->Release();
			}

		// Go through the byte code and release references to all resources used by the function
		asCArray<asDWORD> &bc = scriptData->byteCode;
		for (asUINT n = 0; n < bc.GetLength(); n += asBCTypeSize[asBCInfo[*(asBYTE *)&bc[n]].type]) {
			switch (*(asBYTE *)&bc[n]) {
			// Object types
			case asBC_OBJTYPE:
			case asBC_FREE:
			case asBC_REFCPY:
			case asBC_RefCpyV: {
				asCObjectType *objType = (asCObjectType *)asBC_PTRARG(&bc[n]);
				if (objType)
					objType->ReleaseInternal();
			}
			break;

			// Object type and function
			case asBC_ALLOC: {
				asCObjectType *objType = (asCObjectType *)asBC_PTRARG(&bc[n]);
				if (objType)
					objType->ReleaseInternal();

				int funcId = asBC_INTARG(&bc[n] + AS_PTR_SIZE);
				if (funcId > 0) {
					asCScriptFunction *fptr = engine->scriptFunctions[funcId];
					if (fptr)
						fptr->ReleaseInternal();

					// The engine may have been forced to destroy the function internals early
					// and this may will make it impossible to find the function by id anymore.
					// This should only happen if the engine is released while the application
					// is still keeping functions alive.
					// TODO: Fix this possible memory leak
				}
			}
			break;

			// Global variables
			case asBC_PGA:
			case asBC_PshGPtr:
			case asBC_LDG:
			case asBC_PshG4:
			case asBC_LdGRdR4:
			case asBC_CpyGtoV4:
			case asBC_CpyVtoG4:
			case asBC_SetG4:
				// Need to increase the reference for each global variable
			{
				void *gvarPtr = (void *)asBC_PTRARG(&bc[n]);
				if (!gvarPtr) break;
				asCGlobalProperty *prop = GetPropertyByGlobalVarPtr(gvarPtr);

				if (!prop) {
					// The pointer is a string constant, so it needs to be released by the string factory
					int r = engine->stringFactory->ReleaseStringConstant(gvarPtr);
					UNUSED_VAR(r);

					// If we get an error from the string factory there is not
					// anything we can do about it, except report a message.
					// TODO: Write a message showing that the string couldn't be
					//       released. Include the first 10 characters and the length
					//       to make it easier to identify which string it was
					asASSERT(r >= 0);
					break;
				}

				// Only release the properties once
				if (!ptrs.Exists(gvarPtr)) {
					prop->Release();
					ptrs.PushLast(gvarPtr);
				}

				asCConfigGroup *group = engine->FindConfigGroupForGlobalVar(prop->id);
				if (group != 0) group->Release();
			}
			break;

			// System functions
			case asBC_CALLSYS: {
				int funcId = asBC_INTARG(&bc[n]);
				asCConfigGroup *group = engine->FindConfigGroupForFunction(funcId);
				if (group != 0) group->Release();

				if (funcId) {
					asCScriptFunction *fptr = engine->scriptFunctions[funcId];
					if (fptr)
						fptr->ReleaseInternal();
				}
			}
			break;

			// Functions
			case asBC_CALL:
			case asBC_CALLINTF: {
				int funcId = asBC_INTARG(&bc[n]);
				if (funcId) {
					asCScriptFunction *fptr = engine->scriptFunctions[funcId];
					if (fptr)
						fptr->ReleaseInternal();

					// The engine may have been forced to destroy the function internals early
					// and this may will make it impossible to find the function by id anymore.
					// This should only happen if the engine is released while the application
					// is still keeping functions alive.
					// TODO: Fix this possible memory leak
				}
			}
			break;

			// Function pointers
			case asBC_FuncPtr: {
				asCScriptFunction *func = (asCScriptFunction *)asBC_PTRARG(&bc[n]);
				if (func)
					func->ReleaseInternal();
			}
			break;
			}
		}

		// Release the jit compiled function
		if (scriptData->jitFunction)
			engine->jitCompiler->ReleaseJITFunction(scriptData->jitFunction);
		scriptData->jitFunction = 0;
	}

	// Delegate
	if (objForDelegate)
		engine->ReleaseScriptObject(objForDelegate, funcForDelegate->GetObjectType());
	objForDelegate = 0;
	if (funcForDelegate)
		funcForDelegate->Release();
	funcForDelegate = 0;
}

// interface
int asCScriptFunction::GetReturnTypeId(asDWORD *flags) const {
	if (flags) {
		if (returnType.IsReference()) {
			*flags = asTM_INOUTREF;
			*flags |= returnType.IsReadOnly() ? asTM_CONST : 0;
		} else
			*flags = asTM_NONE;
	}

	return engine->GetTypeIdFromDataType(returnType);
}

// interface
asUINT asCScriptFunction::GetParamCount() const {
	return (asUINT)parameterTypes.GetLength();
}

// interface
int asCScriptFunction::GetParam(asUINT index, int *out_typeId, asDWORD *out_flags, const char **out_name, const char **out_defaultArg) const {
	if (index >= parameterTypes.GetLength())
		return asINVALID_ARG;

	if (out_typeId)
		*out_typeId = engine->GetTypeIdFromDataType(parameterTypes[index]);

	if (out_flags) {
		*out_flags = inOutFlags[index];
		*out_flags |= parameterTypes[index].IsReadOnly() ? asTM_CONST : 0;
	}

	if (out_name) {
		// The parameter names are not stored if loading from bytecode without debug information
		if (index < parameterNames.GetLength())
			*out_name = parameterNames[index].AddressOf();
		else
			*out_name = 0;
	}

	if (out_defaultArg) {
		if (index < defaultArgs.GetLength() && defaultArgs[index])
			*out_defaultArg = defaultArgs[index]->AddressOf();
		else
			*out_defaultArg = 0;
	}

	return asSUCCESS;
}

// interface
asIScriptEngine *asCScriptFunction::GetEngine() const {
	return engine;
}

// interface
const char *asCScriptFunction::GetDeclaration(bool includeObjectName, bool includeNamespace, bool includeParamNames) const {
	asCString *tempString = &asCThreadManager::GetLocalData()->string;
	*tempString = GetDeclarationStr(includeObjectName, includeNamespace, includeParamNames);
	return tempString->AddressOf();
}

// interface
const char *asCScriptFunction::GetScriptSectionName() const {
	if (scriptData && scriptData->scriptSectionIdx >= 0)
		return engine->scriptSectionNames[scriptData->scriptSectionIdx]->AddressOf();

	return 0;
}

// interface
const char *asCScriptFunction::GetConfigGroup() const {
	asCConfigGroup *group = 0;
	if (funcType != asFUNC_FUNCDEF)
		group = engine->FindConfigGroupForFunction(id);
	else
		group = engine->FindConfigGroupForFuncDef(this->funcdefType);

	if (group == 0)
		return 0;

	return group->groupName.AddressOf();
}

// interface
asDWORD asCScriptFunction::GetAccessMask() const {
	return accessMask;
}

// internal
void asCScriptFunction::JITCompile() {
	if (funcType != asFUNC_SCRIPT)
		return;

	asASSERT(scriptData);

	asIJITCompiler *jit = engine->GetJITCompiler();
	if (!jit)
		return;

	// Make sure the function has been compiled with JitEntry instructions
	// For functions that has JitEntry this will be a quick test
	asUINT length;
	asDWORD *byteCode = GetByteCode(&length);
	asDWORD *end = byteCode + length;
	bool foundJitEntry = false;
	while (byteCode < end) {
		// Determine the instruction
		asEBCInstr op = asEBCInstr(*(asBYTE *)byteCode);
		if (op == asBC_JitEntry) {
			foundJitEntry = true;
			break;
		}

		// Move to next instruction
		byteCode += asBCTypeSize[asBCInfo[op].type];
	}

	if (!foundJitEntry) {
		asCString msg;
		msg.Format(TXT_NO_JIT_IN_FUNC_s, GetDeclaration());
		engine->WriteMessage("", 0, 0, asMSGTYPE_WARNING, msg.AddressOf());
	}

	// Release the previous function, if any
	if (scriptData->jitFunction) {
		engine->jitCompiler->ReleaseJITFunction(scriptData->jitFunction);
		scriptData->jitFunction = 0;
	}

	// Compile for native system
	int r = jit->CompileFunction(this, &scriptData->jitFunction);
	if (r < 0)
		asASSERT(scriptData->jitFunction == 0);
}

// interface
asDWORD *asCScriptFunction::GetByteCode(asUINT *length) {
	if (scriptData == 0) return 0;

	if (length)
		*length = (asUINT)scriptData->byteCode.GetLength();

	if (scriptData->byteCode.GetLength())
		return scriptData->byteCode.AddressOf();

	return 0;
}

// interface
void *asCScriptFunction::SetUserData(void *data, asPWORD type) {
	// As a thread might add a new new user data at the same time as another
	// it is necessary to protect both read and write access to the userData member
	ACQUIREEXCLUSIVE(engine->engineRWLock);

	// It is not intended to store a lot of different types of userdata,
	// so a more complex structure like a associative map would just have
	// more overhead than a simple array.
	for (asUINT n = 0; n < userData.GetLength(); n += 2) {
		if (userData[n] == type) {
			void *oldData = reinterpret_cast<void *>(userData[n + 1]);
			userData[n + 1] = reinterpret_cast<asPWORD>(data);

			RELEASEEXCLUSIVE(engine->engineRWLock);

			return oldData;
		}
	}

	userData.PushLast(type);
	userData.PushLast(reinterpret_cast<asPWORD>(data));

	RELEASEEXCLUSIVE(engine->engineRWLock);

	return 0;
}

// interface
void *asCScriptFunction::GetUserData(asPWORD type) const {
	// There may be multiple threads reading, but when
	// setting the user data nobody must be reading.
	ACQUIRESHARED(engine->engineRWLock);

	for (asUINT n = 0; n < userData.GetLength(); n += 2) {
		if (userData[n] == type) {
			RELEASESHARED(engine->engineRWLock);
			return reinterpret_cast<void *>(userData[n + 1]);
		}
	}

	RELEASESHARED(engine->engineRWLock);

	return 0;
}

// internal
// TODO: cleanup: This method should probably be a member of the engine
asCGlobalProperty *asCScriptFunction::GetPropertyByGlobalVarPtr(void *gvarPtr) {
	asSMapNode<void *, asCGlobalProperty *> *node;
	if (engine->varAddressMap.MoveTo(&node, gvarPtr)) {
		asASSERT(gvarPtr == node->value->GetAddressOfValue());
		return node->value;
	}
	return 0;
}

// internal
int asCScriptFunction::GetRefCount() {
	asASSERT(funcType == asFUNC_DELEGATE);

	return externalRefCount.get();
}

// internal
void asCScriptFunction::SetFlag() {
	gcFlag = true;
}

// internal
bool asCScriptFunction::GetFlag() {
	return gcFlag;
}

// internal
void asCScriptFunction::EnumReferences(asIScriptEngine *) {
	asASSERT(funcType == asFUNC_DELEGATE);

	// Delegate
	if (objForDelegate)
		engine->GCEnumCallback(objForDelegate);
}

// internal
void asCScriptFunction::ReleaseAllHandles(asIScriptEngine *) {
	asASSERT(funcType == asFUNC_DELEGATE);

	// Release paramaters

	// Delegate
	if (objForDelegate)
		engine->ReleaseScriptObject(objForDelegate, funcForDelegate->GetObjectType());
	objForDelegate = 0;
}

// interface
bool asCScriptFunction::IsShared() const {
	// All system functions are shared
	if (funcType == asFUNC_SYSTEM) return true;

	// All class methods for shared classes are also shared
	asASSERT(objectType == 0 || objectType->engine == engine || objectType->engine == 0);
	if (objectType && (objectType->flags & asOBJ_SHARED)) return true;

	// funcdefs that are registered by the application are shared
	if (funcType == asFUNC_FUNCDEF && module == 0) return true;

	// Functions that have been specifically marked as shared are shared
	return traits.GetTrait(asTRAIT_SHARED);
}

// interface
bool asCScriptFunction::IsFinal() const {
	return traits.GetTrait(asTRAIT_FINAL);
}

// interface
bool asCScriptFunction::IsOverride() const {
	return traits.GetTrait(asTRAIT_OVERRIDE);
}

// interface
bool asCScriptFunction::IsExplicit() const {
	return traits.GetTrait(asTRAIT_EXPLICIT);
}

// interface
bool asCScriptFunction::IsProperty() const {
	return traits.GetTrait(asTRAIT_PROPERTY);
}

// internal
bool asCScriptFunction::IsFactory() const {
	if (objectType) return false;

	asCObjectType *type = CastToObjectType(returnType.GetTypeInfo());
	if (type == 0) return false;

	if (type->name != name) return false;

	if (type->nameSpace != nameSpace) return false;

	return true;
}

END_AS_NAMESPACE