PageRenderTime 76ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/xbmc/visualizations/Vortex/angelscript/angelscript/source/as_scriptengine.cpp

http://github.com/xbmc/xbmc
C++ | 4005 lines | 2955 code | 632 blank | 418 comment | 874 complexity | 865bdcb2382977594909b1cc468f8b12 MD5 | raw file
Possible License(s): GPL-3.0, CC-BY-SA-3.0, LGPL-2.0, 0BSD, Unlicense, GPL-2.0, AGPL-1.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2009 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. andreas@angelcode.com
  22. */
  23. //
  24. // as_scriptengine.cpp
  25. //
  26. // The implementation of the script engine interface
  27. //
  28. #include <stdlib.h>
  29. #include "as_config.h"
  30. #include "as_scriptengine.h"
  31. #include "as_builder.h"
  32. #include "as_context.h"
  33. #include "as_string_util.h"
  34. #include "as_tokenizer.h"
  35. #include "as_texts.h"
  36. #include "as_module.h"
  37. #include "as_callfunc.h"
  38. #include "as_arrayobject.h"
  39. #include "as_generic.h"
  40. #include "as_scriptobject.h"
  41. #include "as_compiler.h"
  42. BEGIN_AS_NAMESPACE
  43. extern "C"
  44. {
  45. AS_API const char * asGetLibraryVersion()
  46. {
  47. #ifdef _DEBUG
  48. return ANGELSCRIPT_VERSION_STRING " DEBUG";
  49. #else
  50. return ANGELSCRIPT_VERSION_STRING;
  51. #endif
  52. }
  53. AS_API const char * asGetLibraryOptions()
  54. {
  55. const char *string = " "
  56. // Options
  57. #ifdef AS_MAX_PORTABILITY
  58. "AS_MAX_PORTABILITY "
  59. #endif
  60. #ifdef AS_DEBUG
  61. "AS_DEBUG "
  62. #endif
  63. #ifdef AS_NO_CLASS_METHODS
  64. "AS_NO_CLASS_METHODS "
  65. #endif
  66. #ifdef AS_USE_DOUBLE_AS_FLOAT
  67. "AS_USE_DOUBLE_AS_FLOAT "
  68. #endif
  69. #ifdef AS_64BIT_PTR
  70. "AS_64BIT_PTR "
  71. #endif
  72. #ifdef AS_NO_THREADS
  73. "AS_NO_THREADS "
  74. #endif
  75. #ifdef AS_NO_ATOMIC
  76. "AS_NO_ATOMIC "
  77. #endif
  78. // Target system
  79. #ifdef AS_WIN
  80. "AS_WIN "
  81. #endif
  82. #ifdef AS_LINUX
  83. "AS_LINUX "
  84. #endif
  85. #ifdef AS_MAC
  86. "AS_MAC "
  87. #endif
  88. #ifdef AS_BSD
  89. "AS_BSD "
  90. #endif
  91. #ifdef AS_XBOX
  92. "AS_XBOX "
  93. #endif
  94. #ifdef AS_XBOX360
  95. "AS_XBOX360 "
  96. #endif
  97. #ifdef AS_PSP
  98. "AS_PSP "
  99. #endif
  100. #ifdef AS_PS2
  101. "AS_PS2 "
  102. #endif
  103. #ifdef AS_PS3
  104. "AS_PS3 "
  105. #endif
  106. #ifdef AS_DC
  107. "AS_DC "
  108. #endif
  109. #ifdef AS_GC
  110. "AS_GC "
  111. #endif
  112. #ifdef AS_WII
  113. "AS_WII "
  114. #endif
  115. #ifdef AS_IPHONE
  116. "AS_IPHONE "
  117. #endif
  118. #ifdef AS_ANDROID
  119. "AS_ANDROID "
  120. #endif
  121. // CPU family
  122. #ifdef AS_PPC
  123. "AS_PPC "
  124. #endif
  125. #ifdef AS_PPC_64
  126. "AS_PPC_64 "
  127. #endif
  128. #ifdef AS_X86
  129. "AS_X86 "
  130. #endif
  131. #ifdef AS_MIPS
  132. "AS_MIPS "
  133. #endif
  134. #ifdef AS_SH4
  135. "AS_SH4 "
  136. #endif
  137. #ifdef AS_XENON
  138. "AS_XENON "
  139. #endif
  140. #ifdef AS_ARM
  141. "AS_ARM "
  142. #endif
  143. ;
  144. return string;
  145. }
  146. AS_API asIScriptEngine *asCreateScriptEngine(asDWORD version)
  147. {
  148. // Verify the version that the application expects
  149. if( (version/10000) != (ANGELSCRIPT_VERSION/10000) )
  150. return 0;
  151. if( (version/100)%100 != (ANGELSCRIPT_VERSION/100)%100 )
  152. return 0;
  153. if( (version%100) > (ANGELSCRIPT_VERSION%100) )
  154. return 0;
  155. // Verify the size of the types
  156. asASSERT( sizeof(asBYTE) == 1 );
  157. asASSERT( sizeof(asWORD) == 2 );
  158. asASSERT( sizeof(asDWORD) == 4 );
  159. asASSERT( sizeof(asQWORD) == 8 );
  160. asASSERT( sizeof(asPWORD) == sizeof(void*) );
  161. // Verify the boolean type
  162. asASSERT( sizeof(bool) == AS_SIZEOF_BOOL );
  163. asASSERT( true == VALUE_OF_BOOLEAN_TRUE );
  164. // Verify endianess
  165. #ifdef AS_BIG_ENDIAN
  166. asASSERT( *(asDWORD*)"\x00\x01\x02\x03" == 0x00010203 );
  167. asASSERT( *(asQWORD*)"\x00\x01\x02\x03\x04\x05\x06\x07" == I64(0x0001020304050607) );
  168. #else
  169. asASSERT( *(asDWORD*)"\x00\x01\x02\x03" == 0x03020100 );
  170. asASSERT( *(asQWORD*)"\x00\x01\x02\x03\x04\x05\x06\x07" == I64(0x0706050403020100) );
  171. #endif
  172. return asNEW(asCScriptEngine)();
  173. }
  174. int asCScriptEngine::SetEngineProperty(asEEngineProp property, asPWORD value)
  175. {
  176. switch( property )
  177. {
  178. case asEP_ALLOW_UNSAFE_REFERENCES:
  179. ep.allowUnsafeReferences = value ? true : false;
  180. break;
  181. case asEP_OPTIMIZE_BYTECODE:
  182. ep.optimizeByteCode = value ? true : false;
  183. break;
  184. case asEP_COPY_SCRIPT_SECTIONS:
  185. ep.copyScriptSections = value ? true : false;
  186. break;
  187. case asEP_MAX_STACK_SIZE:
  188. // The size is given in bytes, but we only store dwords
  189. ep.maximumContextStackSize = (int)value/4;
  190. if( initialContextStackSize > ep.maximumContextStackSize )
  191. initialContextStackSize = ep.maximumContextStackSize;
  192. break;
  193. case asEP_USE_CHARACTER_LITERALS:
  194. ep.useCharacterLiterals = value ? true : false;
  195. break;
  196. case asEP_ALLOW_MULTILINE_STRINGS:
  197. ep.allowMultilineStrings = value ? true : false;
  198. break;
  199. case asEP_ALLOW_IMPLICIT_HANDLE_TYPES:
  200. ep.allowImplicitHandleTypes = value ? true : false;
  201. break;
  202. case asEP_BUILD_WITHOUT_LINE_CUES:
  203. ep.buildWithoutLineCues = value ? true : false;
  204. break;
  205. case asEP_INIT_GLOBAL_VARS_AFTER_BUILD:
  206. ep.initGlobalVarsAfterBuild = value ? true : false;
  207. break;
  208. case asEP_REQUIRE_ENUM_SCOPE:
  209. ep.requireEnumScope = value ? true : false;
  210. break;
  211. case asEP_SCRIPT_SCANNER:
  212. if( value <= 1 )
  213. ep.scanner = (int)value;
  214. else
  215. return asINVALID_ARG;
  216. break;
  217. case asEP_INCLUDE_JIT_INSTRUCTIONS:
  218. ep.includeJitInstructions = value ? true : false;
  219. break;
  220. case asEP_STRING_ENCODING:
  221. if( value <= 1 )
  222. ep.stringEncoding = (int)value;
  223. else
  224. return asINVALID_ARG;
  225. break;
  226. default:
  227. return asINVALID_ARG;
  228. }
  229. return asSUCCESS;
  230. }
  231. asPWORD asCScriptEngine::GetEngineProperty(asEEngineProp property)
  232. {
  233. switch( property )
  234. {
  235. case asEP_ALLOW_UNSAFE_REFERENCES:
  236. return ep.allowUnsafeReferences;
  237. case asEP_OPTIMIZE_BYTECODE:
  238. return ep.optimizeByteCode;
  239. case asEP_COPY_SCRIPT_SECTIONS:
  240. return ep.copyScriptSections;
  241. case asEP_MAX_STACK_SIZE:
  242. return ep.maximumContextStackSize*4;
  243. case asEP_USE_CHARACTER_LITERALS:
  244. return ep.useCharacterLiterals;
  245. case asEP_ALLOW_MULTILINE_STRINGS:
  246. return ep.allowMultilineStrings;
  247. case asEP_ALLOW_IMPLICIT_HANDLE_TYPES:
  248. return ep.allowImplicitHandleTypes;
  249. case asEP_BUILD_WITHOUT_LINE_CUES:
  250. return ep.buildWithoutLineCues;
  251. case asEP_INIT_GLOBAL_VARS_AFTER_BUILD:
  252. return ep.initGlobalVarsAfterBuild;
  253. case asEP_REQUIRE_ENUM_SCOPE:
  254. return ep.requireEnumScope;
  255. case asEP_SCRIPT_SCANNER:
  256. return ep.scanner;
  257. case asEP_INCLUDE_JIT_INSTRUCTIONS:
  258. return ep.includeJitInstructions;
  259. case asEP_STRING_ENCODING:
  260. return ep.stringEncoding;
  261. }
  262. return 0;
  263. }
  264. } // extern "C"
  265. asCScriptEngine::asCScriptEngine()
  266. {
  267. // Instanciate the thread manager
  268. if( threadManager == 0 )
  269. threadManager = asNEW(asCThreadManager);
  270. else
  271. threadManager->AddRef();
  272. // Engine properties
  273. ep.allowUnsafeReferences = false;
  274. ep.optimizeByteCode = true;
  275. ep.copyScriptSections = true;
  276. ep.maximumContextStackSize = 0; // no limit
  277. ep.useCharacterLiterals = false;
  278. ep.allowMultilineStrings = false;
  279. ep.allowImplicitHandleTypes = false;
  280. ep.buildWithoutLineCues = false;
  281. ep.initGlobalVarsAfterBuild = true;
  282. ep.requireEnumScope = false;
  283. ep.scanner = 1; // utf8. 0 = ascii
  284. ep.includeJitInstructions = false;
  285. ep.stringEncoding = 0; // utf8. 1 = utf16
  286. gc.engine = this;
  287. refCount.set(1);
  288. stringFactory = 0;
  289. configFailed = false;
  290. isPrepared = false;
  291. isBuilding = false;
  292. lastModule = 0;
  293. userData = 0;
  294. initialContextStackSize = 1024; // 1 KB
  295. typeIdSeqNbr = 0;
  296. currentGroup = &defaultGroup;
  297. msgCallback = 0;
  298. jitCompiler = 0;
  299. // Reserve function id 0 for no function
  300. scriptFunctions.PushLast(0);
  301. // Make sure typeId for the built-in primitives are defined according to asETypeIdFlags
  302. int id;
  303. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttVoid, false)); asASSERT( id == asTYPEID_VOID );
  304. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttBool, false)); asASSERT( id == asTYPEID_BOOL );
  305. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttInt8, false)); asASSERT( id == asTYPEID_INT8 );
  306. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttInt16, false)); asASSERT( id == asTYPEID_INT16 );
  307. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttInt, false)); asASSERT( id == asTYPEID_INT32 );
  308. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttInt64, false)); asASSERT( id == asTYPEID_INT64 );
  309. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttUInt8, false)); asASSERT( id == asTYPEID_UINT8 );
  310. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttUInt16, false)); asASSERT( id == asTYPEID_UINT16 );
  311. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttUInt, false)); asASSERT( id == asTYPEID_UINT32 );
  312. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttUInt64, false)); asASSERT( id == asTYPEID_UINT64 );
  313. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttFloat, false)); asASSERT( id == asTYPEID_FLOAT );
  314. id = GetTypeIdFromDataType(asCDataType::CreatePrimitive(ttDouble, false)); asASSERT( id == asTYPEID_DOUBLE );
  315. defaultArrayObjectType = 0;
  316. RegisterArrayObject(this);
  317. RegisterScriptObject(this);
  318. RegisterScriptFunction(this);
  319. RegisterObjectTypeGCBehaviours(this);
  320. }
  321. asCScriptEngine::~asCScriptEngine()
  322. {
  323. asASSERT(refCount.get() == 0);
  324. asUINT n;
  325. // The modules must be deleted first, as they may use
  326. // object types from the config groups
  327. for( n = (asUINT)scriptModules.GetLength(); n-- > 0; )
  328. {
  329. if( scriptModules[n] )
  330. {
  331. asDELETE(scriptModules[n],asCModule);
  332. }
  333. }
  334. scriptModules.SetLength(0);
  335. GarbageCollect(asGC_FULL_CYCLE);
  336. // Delete the functions for template types that may references object types
  337. for( n = 0; n < templateTypes.GetLength(); n++ )
  338. {
  339. if( templateTypes[n] )
  340. {
  341. asUINT f;
  342. // Delete the factory stubs first
  343. for( f = 0; f < templateTypes[n]->beh.factories.GetLength(); f++ )
  344. {
  345. scriptFunctions[templateTypes[n]->beh.factories[f]]->Release();
  346. }
  347. templateTypes[n]->beh.factories.Allocate(0, false);
  348. // Delete the specialized functions
  349. for( f = 1; f < templateTypes[n]->beh.operators.GetLength(); f += 2 )
  350. {
  351. if( scriptFunctions[templateTypes[n]->beh.operators[f]]->objectType == templateTypes[n] )
  352. {
  353. scriptFunctions[templateTypes[n]->beh.operators[f]]->Release();
  354. templateTypes[n]->beh.operators[f] = 0;
  355. }
  356. }
  357. }
  358. }
  359. // Do one more garbage collect to free gc objects that were global variables
  360. GarbageCollect(asGC_FULL_CYCLE);
  361. FreeUnusedGlobalProperties();
  362. ClearUnusedTypes();
  363. // Break all relationship between remaining class types and functions
  364. for( n = 0; n < classTypes.GetLength(); n++ )
  365. {
  366. if( classTypes[n] )
  367. classTypes[n]->ReleaseAllFunctions();
  368. if( classTypes[n]->derivedFrom )
  369. {
  370. classTypes[n]->derivedFrom->Release();
  371. classTypes[n]->derivedFrom = 0;
  372. }
  373. }
  374. GarbageCollect(asGC_FULL_CYCLE);
  375. FreeUnusedGlobalProperties();
  376. ClearUnusedTypes();
  377. asSMapNode<int,asCDataType*> *cursor = 0;
  378. while( mapTypeIdToDataType.MoveFirst(&cursor) )
  379. {
  380. asDELETE(mapTypeIdToDataType.GetValue(cursor),asCDataType);
  381. mapTypeIdToDataType.Erase(cursor);
  382. }
  383. defaultGroup.RemoveConfiguration(this);
  384. while( configGroups.GetLength() )
  385. {
  386. // Delete config groups in the right order
  387. asCConfigGroup *grp = configGroups.PopLast();
  388. if( grp )
  389. {
  390. asDELETE(grp,asCConfigGroup);
  391. }
  392. }
  393. for( n = 0; n < registeredGlobalProps.GetLength(); n++ )
  394. {
  395. if( registeredGlobalProps[n] )
  396. {
  397. asDELETE(registeredGlobalProps[n],asCGlobalProperty);
  398. }
  399. }
  400. registeredGlobalProps.SetLength(0);
  401. FreeUnusedGlobalProperties();
  402. for( n = 0; n < templateTypes.GetLength(); n++ )
  403. {
  404. if( templateTypes[n] )
  405. {
  406. // Clear the sub type before deleting the template type so that the sub type isn't freed to soon
  407. templateTypes[n]->templateSubType = asCDataType::CreateNullHandle();
  408. asDELETE(templateTypes[n],asCObjectType);
  409. }
  410. }
  411. templateTypes.SetLength(0);
  412. for( n = 0; n < objectTypes.GetLength(); n++ )
  413. {
  414. if( objectTypes[n] )
  415. {
  416. // Clear the sub type before deleting the template type so that the sub type isn't freed to soon
  417. objectTypes[n]->templateSubType = asCDataType::CreateNullHandle();
  418. asDELETE(objectTypes[n],asCObjectType);
  419. }
  420. }
  421. objectTypes.SetLength(0);
  422. for( n = 0; n < templateSubTypes.GetLength(); n++ )
  423. {
  424. if( templateSubTypes[n] )
  425. {
  426. asDELETE(templateSubTypes[n], asCObjectType);
  427. }
  428. }
  429. templateSubTypes.SetLength(0);
  430. registeredTypeDefs.SetLength(0);
  431. registeredEnums.SetLength(0);
  432. registeredObjTypes.SetLength(0);
  433. for( n = 0; n < registeredGlobalFuncs.GetLength(); n++ )
  434. {
  435. if( registeredGlobalFuncs[n] )
  436. registeredGlobalFuncs[n]->Release();
  437. }
  438. registeredGlobalFuncs.SetLength(0);
  439. scriptTypeBehaviours.ReleaseAllFunctions();
  440. functionBehaviours.ReleaseAllFunctions();
  441. objectTypeBehaviours.ReleaseAllFunctions();
  442. // Free string constants
  443. for( n = 0; n < stringConstants.GetLength(); n++ )
  444. {
  445. asDELETE(stringConstants[n],asCString);
  446. }
  447. stringConstants.SetLength(0);
  448. // Free the script section names
  449. for( n = 0; n < scriptSectionNames.GetLength(); n++ )
  450. {
  451. asDELETE(scriptSectionNames[n],asCString);
  452. }
  453. scriptSectionNames.SetLength(0);
  454. // Release the thread manager
  455. threadManager->Release();
  456. }
  457. // interface
  458. int asCScriptEngine::AddRef()
  459. {
  460. return refCount.atomicInc();
  461. }
  462. // interface
  463. int asCScriptEngine::Release()
  464. {
  465. int r = refCount.atomicDec();
  466. if( r == 0 )
  467. {
  468. asDELETE(this,asCScriptEngine);
  469. return 0;
  470. }
  471. return r;
  472. }
  473. // interface
  474. void *asCScriptEngine::SetUserData(void *data)
  475. {
  476. void *old = userData;
  477. userData = data;
  478. return old;
  479. }
  480. // interface
  481. void *asCScriptEngine::GetUserData()
  482. {
  483. return userData;
  484. }
  485. // interface
  486. int asCScriptEngine::SetMessageCallback(const asSFuncPtr &callback, void *obj, asDWORD callConv)
  487. {
  488. msgCallback = true;
  489. msgCallbackObj = obj;
  490. bool isObj = false;
  491. if( (unsigned)callConv == asCALL_GENERIC )
  492. {
  493. msgCallback = false;
  494. return asNOT_SUPPORTED;
  495. }
  496. if( (unsigned)callConv >= asCALL_THISCALL )
  497. {
  498. isObj = true;
  499. if( obj == 0 )
  500. {
  501. msgCallback = false;
  502. return asINVALID_ARG;
  503. }
  504. }
  505. int r = DetectCallingConvention(isObj, callback, callConv, &msgCallbackFunc);
  506. if( r < 0 ) msgCallback = false;
  507. return r;
  508. }
  509. // interface
  510. int asCScriptEngine::ClearMessageCallback()
  511. {
  512. msgCallback = false;
  513. return 0;
  514. }
  515. // interface
  516. int asCScriptEngine::WriteMessage(const char *section, int row, int col, asEMsgType type, const char *message)
  517. {
  518. // Validate input parameters
  519. if( section == 0 ||
  520. message == 0 )
  521. return asINVALID_ARG;
  522. // If there is no callback then there's nothing to do
  523. if( !msgCallback )
  524. return 0;
  525. asSMessageInfo msg;
  526. msg.section = section;
  527. msg.row = row;
  528. msg.col = col;
  529. msg.type = type;
  530. msg.message = message;
  531. if( msgCallbackFunc.callConv < ICC_THISCALL )
  532. CallGlobalFunction(&msg, msgCallbackObj, &msgCallbackFunc, 0);
  533. else
  534. CallObjectMethod(msgCallbackObj, &msg, &msgCallbackFunc, 0);
  535. return 0;
  536. }
  537. int asCScriptEngine::SetJITCompiler(asIJITCompiler *compiler)
  538. {
  539. jitCompiler = compiler;
  540. return asSUCCESS;
  541. }
  542. asIJITCompiler *asCScriptEngine::GetJITCompiler()
  543. {
  544. return jitCompiler;
  545. }
  546. // interface
  547. asETokenClass asCScriptEngine::ParseToken(const char *string, size_t stringLength, int *tokenLength)
  548. {
  549. if( stringLength == 0 )
  550. stringLength = strlen(string);
  551. size_t len;
  552. asCTokenizer t;
  553. asETokenClass tc;
  554. t.GetToken(string, stringLength, &len, &tc);
  555. if( tokenLength )
  556. *tokenLength = (int)len;
  557. return tc;
  558. }
  559. // interface
  560. asIScriptModule *asCScriptEngine::GetModule(const char *module, asEGMFlags flag)
  561. {
  562. asCModule *mod = GetModule(module, false);
  563. if( flag == asGM_ALWAYS_CREATE )
  564. {
  565. if( mod != 0 )
  566. {
  567. asDELETE(mod, asCModule);
  568. }
  569. return GetModule(module, true);
  570. }
  571. if( mod == 0 && flag == asGM_CREATE_IF_NOT_EXISTS )
  572. {
  573. return GetModule(module, true);
  574. }
  575. return mod;
  576. }
  577. // interface
  578. int asCScriptEngine::DiscardModule(const char *module)
  579. {
  580. asCModule *mod = GetModule(module, false);
  581. if( mod == 0 ) return asNO_MODULE;
  582. asDELETE(mod, asCModule);
  583. FreeUnusedGlobalProperties();
  584. ClearUnusedTypes();
  585. return 0;
  586. }
  587. void asCScriptEngine::ClearUnusedTypes()
  588. {
  589. // Build a list of all types to check for
  590. asCArray<asCObjectType*> types;
  591. types = classTypes;
  592. types.Concatenate(templateInstanceTypes);
  593. // Go through all modules
  594. asUINT n;
  595. for( n = 0; n < scriptModules.GetLength() && types.GetLength(); n++ )
  596. {
  597. asCModule *mod = scriptModules[n];
  598. if( mod )
  599. {
  600. // Functions/Methods/Globals are handled after this
  601. // Go through all type declarations
  602. asUINT m;
  603. for( m = 0; m < mod->classTypes.GetLength() && types.GetLength(); m++ )
  604. RemoveTypeAndRelatedFromList(types, mod->classTypes[m]);
  605. for( m = 0; m < mod->enumTypes.GetLength() && types.GetLength(); m++ )
  606. RemoveTypeAndRelatedFromList(types, mod->enumTypes[m]);
  607. for( m = 0; m < mod->typeDefs.GetLength() && types.GetLength(); m++ )
  608. RemoveTypeAndRelatedFromList(types, mod->typeDefs[m]);
  609. }
  610. }
  611. // Go through all function parameters and remove used types
  612. for( n = 0; n < scriptFunctions.GetLength() && types.GetLength(); n++ )
  613. {
  614. asCScriptFunction *func = scriptFunctions[n];
  615. if( func )
  616. {
  617. // Ignore factory stubs
  618. if( func->name == "factstub" )
  619. continue;
  620. asCObjectType *ot = func->returnType.GetObjectType();
  621. if( ot != 0 && ot != func->objectType )
  622. if( func->name != ot->name )
  623. RemoveTypeAndRelatedFromList(types, ot);
  624. for( asUINT p = 0; p < func->parameterTypes.GetLength(); p++ )
  625. {
  626. ot = func->parameterTypes[p].GetObjectType();
  627. if( ot != 0 && ot != func->objectType )
  628. if( func->name != ot->name )
  629. RemoveTypeAndRelatedFromList(types, ot);
  630. }
  631. }
  632. }
  633. // Go through all global properties
  634. for( n = 0; n < globalProperties.GetLength() && types.GetLength(); n++ )
  635. {
  636. if( globalProperties[n] && globalProperties[n]->type.GetObjectType() )
  637. RemoveTypeAndRelatedFromList(types, globalProperties[n]->type.GetObjectType());
  638. }
  639. // All that remains in the list after this can be discarded, since they are no longer used
  640. for(;;)
  641. {
  642. bool didClearTemplateInstanceType = false;
  643. for( n = 0; n < types.GetLength(); n++ )
  644. {
  645. // Template types and script classes will have two references for each factory stub
  646. int refCount = ((types[n]->flags & asOBJ_TEMPLATE) || (types[n]->flags & asOBJ_SCRIPT_OBJECT)) ? 2*(int)types[n]->beh.factories.GetLength() : 0;
  647. if( types[n]->GetRefCount() == refCount )
  648. {
  649. if( types[n]->flags & asOBJ_TEMPLATE )
  650. {
  651. didClearTemplateInstanceType = true;
  652. RemoveTemplateInstanceType(types[n]);
  653. }
  654. else
  655. {
  656. RemoveFromTypeIdMap(types[n]);
  657. asDELETE(types[n],asCObjectType);
  658. int i = classTypes.IndexOf(types[n]);
  659. if( i == (signed)classTypes.GetLength() - 1 )
  660. classTypes.PopLast();
  661. else
  662. classTypes[i] = classTypes.PopLast();
  663. }
  664. // Remove the type from the array
  665. if( n < types.GetLength() - 1 )
  666. types[n] = types.PopLast();
  667. else
  668. types.PopLast();
  669. n--;
  670. }
  671. }
  672. if( didClearTemplateInstanceType == false )
  673. break;
  674. }
  675. }
  676. void asCScriptEngine::RemoveTypeAndRelatedFromList(asCArray<asCObjectType*> &types, asCObjectType *ot)
  677. {
  678. // Remove the type from the list
  679. int i = types.IndexOf(ot);
  680. if( i == -1 ) return;
  681. if( i == (signed)types.GetLength() - 1 )
  682. types.PopLast();
  683. else
  684. types[i] = types.PopLast();
  685. // If the type is an template type, then remove all sub types as well
  686. if( ot->templateSubType.GetObjectType() )
  687. {
  688. while( ot->templateSubType.GetObjectType() )
  689. {
  690. ot = ot->templateSubType.GetObjectType();
  691. RemoveTypeAndRelatedFromList(types, ot);
  692. }
  693. return;
  694. }
  695. // If the type is a class, then remove all properties types as well
  696. if( ot->properties.GetLength() )
  697. {
  698. for( asUINT n = 0; n < ot->properties.GetLength(); n++ )
  699. RemoveTypeAndRelatedFromList(types, ot->properties[n]->type.GetObjectType());
  700. }
  701. }
  702. // internal
  703. int asCScriptEngine::GetFactoryIdByDecl(const asCObjectType *ot, const char *decl)
  704. {
  705. asCModule *mod = 0;
  706. // Is this a script class?
  707. if( ot->flags & asOBJ_SCRIPT_OBJECT && ot->size > 0 )
  708. mod = scriptFunctions[ot->beh.factory]->module;
  709. asCBuilder bld(this, mod);
  710. asCScriptFunction func(this, mod,-1);
  711. int r = bld.ParseFunctionDeclaration(0, decl, &func, false);
  712. if( r < 0 )
  713. return asINVALID_DECLARATION;
  714. // Search for matching factory function
  715. int id = -1;
  716. for( size_t n = 0; n < ot->beh.factories.GetLength(); n++ )
  717. {
  718. asCScriptFunction *f = scriptFunctions[ot->beh.factories[n]];
  719. if( f->IsSignatureEqual(&func) )
  720. {
  721. id = ot->beh.factories[n];
  722. break;
  723. }
  724. }
  725. if( id == -1 ) return asNO_FUNCTION;
  726. return id;
  727. }
  728. // internal
  729. int asCScriptEngine::GetMethodIdByDecl(const asCObjectType *ot, const char *decl, asCModule *mod)
  730. {
  731. asCBuilder bld(this, mod);
  732. asCScriptFunction func(this, mod, -1);
  733. int r = bld.ParseFunctionDeclaration(0, decl, &func, false);
  734. if( r < 0 )
  735. return asINVALID_DECLARATION;
  736. // Set the object type so that the signature can be properly compared
  737. // This cast is OK, it will only be used for comparison
  738. func.objectType = const_cast<asCObjectType*>(ot);
  739. // Search script functions for matching interface
  740. int id = -1;
  741. for( size_t n = 0; n < ot->methods.GetLength(); ++n )
  742. {
  743. if( func.IsSignatureEqual(scriptFunctions[ot->methods[n]]) )
  744. {
  745. if( id == -1 )
  746. id = ot->methods[n];
  747. else
  748. return asMULTIPLE_FUNCTIONS;
  749. }
  750. }
  751. if( id == -1 ) return asNO_FUNCTION;
  752. return id;
  753. }
  754. // Internal
  755. asCString asCScriptEngine::GetFunctionDeclaration(int funcID)
  756. {
  757. asCString str;
  758. asCScriptFunction *func = GetScriptFunction(funcID);
  759. if( func )
  760. str = func->GetDeclarationStr();
  761. return str;
  762. }
  763. asCScriptFunction *asCScriptEngine::GetScriptFunction(int funcId)
  764. {
  765. if( funcId < 0 || funcId >= (int)scriptFunctions.GetLength() )
  766. return 0;
  767. return scriptFunctions[funcId];
  768. }
  769. asIScriptContext *asCScriptEngine::CreateContext()
  770. {
  771. asIScriptContext *ctx = 0;
  772. CreateContext(&ctx, false);
  773. return ctx;
  774. }
  775. int asCScriptEngine::CreateContext(asIScriptContext **context, bool isInternal)
  776. {
  777. *context = asNEW(asCContext)(this, !isInternal);
  778. // We need to make sure the engine has been
  779. // prepared before any context is executed
  780. PrepareEngine();
  781. return 0;
  782. }
  783. int asCScriptEngine::RegisterObjectProperty(const char *obj, const char *declaration, int byteOffset)
  784. {
  785. int r;
  786. asCDataType dt;
  787. asCBuilder bld(this, 0);
  788. r = bld.ParseDataType(obj, &dt);
  789. if( r < 0 )
  790. return ConfigError(r);
  791. // Verify that the correct config group is used
  792. if( currentGroup->FindType(dt.GetObjectType()->name.AddressOf()) == 0 )
  793. return ConfigError(asWRONG_CONFIG_GROUP);
  794. asCDataType type;
  795. asCString name;
  796. if( (r = bld.VerifyProperty(&dt, declaration, name, type)) < 0 )
  797. return ConfigError(r);
  798. // Store the property info
  799. if( dt.GetObjectType() == 0 )
  800. return ConfigError(asINVALID_OBJECT);
  801. asCObjectProperty *prop = asNEW(asCObjectProperty);
  802. prop->name = name;
  803. prop->type = type;
  804. prop->byteOffset = byteOffset;
  805. dt.GetObjectType()->properties.PushLast(prop);
  806. currentGroup->RefConfigGroup(FindConfigGroupForObjectType(type.GetObjectType()));
  807. return asSUCCESS;
  808. }
  809. int asCScriptEngine::RegisterInterface(const char *name)
  810. {
  811. if( name == 0 ) return ConfigError(asINVALID_NAME);
  812. // Verify if the name has been registered as a type already
  813. asUINT n;
  814. for( n = 0; n < objectTypes.GetLength(); n++ )
  815. {
  816. if( objectTypes[n] && objectTypes[n]->name == name )
  817. return asALREADY_REGISTERED;
  818. }
  819. // Use builder to parse the datatype
  820. asCDataType dt;
  821. asCBuilder bld(this, 0);
  822. bool oldMsgCallback = msgCallback; msgCallback = false;
  823. int r = bld.ParseDataType(name, &dt);
  824. msgCallback = oldMsgCallback;
  825. if( r >= 0 ) return ConfigError(asERROR);
  826. // Make sure the name is not a reserved keyword
  827. asCTokenizer t;
  828. size_t tokenLen;
  829. int token = t.GetToken(name, strlen(name), &tokenLen);
  830. if( token != ttIdentifier || strlen(name) != tokenLen )
  831. return ConfigError(asINVALID_NAME);
  832. r = bld.CheckNameConflict(name, 0, 0);
  833. if( r < 0 )
  834. return ConfigError(asNAME_TAKEN);
  835. // Don't have to check against members of object
  836. // types as they are allowed to use the names
  837. // Register the object type for the interface
  838. asCObjectType *st = asNEW(asCObjectType)(this);
  839. st->flags = asOBJ_REF | asOBJ_SCRIPT_OBJECT;
  840. st->size = 0; // Cannot be instanciated
  841. st->name = name;
  842. // Use the default script class behaviours
  843. st->beh.factory = 0;
  844. st->beh.addref = scriptTypeBehaviours.beh.addref;
  845. scriptFunctions[st->beh.addref]->AddRef();
  846. st->beh.release = scriptTypeBehaviours.beh.release;
  847. scriptFunctions[st->beh.release]->AddRef();
  848. st->beh.copy = 0;
  849. objectTypes.PushLast(st);
  850. registeredObjTypes.PushLast(st);
  851. currentGroup->objTypes.PushLast(st);
  852. return asSUCCESS;
  853. }
  854. int asCScriptEngine::RegisterInterfaceMethod(const char *intf, const char *declaration)
  855. {
  856. // Verify that the correct config group is set.
  857. if( currentGroup->FindType(intf) == 0 )
  858. return ConfigError(asWRONG_CONFIG_GROUP);
  859. asCDataType dt;
  860. asCBuilder bld(this, 0);
  861. int r = bld.ParseDataType(intf, &dt);
  862. if( r < 0 )
  863. return ConfigError(r);
  864. asCScriptFunction *func = asNEW(asCScriptFunction)(this, 0, asFUNC_INTERFACE);
  865. func->objectType = dt.GetObjectType();
  866. r = bld.ParseFunctionDeclaration(func->objectType, declaration, func, false);
  867. if( r < 0 )
  868. {
  869. asDELETE(func,asCScriptFunction);
  870. return ConfigError(asINVALID_DECLARATION);
  871. }
  872. // Check name conflicts
  873. r = bld.CheckNameConflictMember(dt, func->name.AddressOf(), 0, 0);
  874. if( r < 0 )
  875. {
  876. asDELETE(func,asCScriptFunction);
  877. return ConfigError(asNAME_TAKEN);
  878. }
  879. func->id = GetNextScriptFunctionId();
  880. SetScriptFunction(func);
  881. func->objectType->methods.PushLast(func->id);
  882. // The refCount was already set to 1
  883. func->ComputeSignatureId();
  884. // If parameter type from other groups are used, add references
  885. // TODO: The code for adding references to config groups is repeated in a lot of places
  886. if( func->returnType.GetObjectType() )
  887. {
  888. asCConfigGroup *group = FindConfigGroupForObjectType(func->returnType.GetObjectType());
  889. currentGroup->RefConfigGroup(group);
  890. }
  891. for( asUINT n = 0; n < func->parameterTypes.GetLength(); n++ )
  892. {
  893. if( func->parameterTypes[n].GetObjectType() )
  894. {
  895. asCConfigGroup *group = FindConfigGroupForObjectType(func->parameterTypes[n].GetObjectType());
  896. currentGroup->RefConfigGroup(group);
  897. }
  898. }
  899. // Return function id as success
  900. return func->id;
  901. }
  902. int asCScriptEngine::RegisterObjectType(const char *name, int byteSize, asDWORD flags)
  903. {
  904. int r;
  905. isPrepared = false;
  906. // Verify flags
  907. // Must have either asOBJ_REF or asOBJ_VALUE
  908. if( flags & asOBJ_REF )
  909. {
  910. // Can optionally have the asOBJ_GC, asOBJ_NOHANDLE, asOBJ_SCOPED, or asOBJ_TEMPLATE flag set, but nothing else
  911. if( flags & ~(asOBJ_REF | asOBJ_GC | asOBJ_NOHANDLE | asOBJ_SCOPED | asOBJ_TEMPLATE) )
  912. return ConfigError(asINVALID_ARG);
  913. // flags are exclusive
  914. if( (flags & asOBJ_GC) && (flags & (asOBJ_NOHANDLE|asOBJ_SCOPED)) )
  915. return ConfigError(asINVALID_ARG);
  916. if( (flags & asOBJ_NOHANDLE) && (flags & (asOBJ_GC|asOBJ_SCOPED)) )
  917. return ConfigError(asINVALID_ARG);
  918. if( (flags & asOBJ_SCOPED) && (flags & (asOBJ_GC|asOBJ_NOHANDLE)) )
  919. return ConfigError(asINVALID_ARG);
  920. }
  921. else if( flags & asOBJ_VALUE )
  922. {
  923. // Cannot use reference flags
  924. // TODO: template: Should be possible to register a value type as template type
  925. if( flags & (asOBJ_REF | asOBJ_GC | asOBJ_SCOPED) )
  926. return ConfigError(asINVALID_ARG);
  927. // If the app type is given, we must validate the flags
  928. if( flags & asOBJ_APP_CLASS )
  929. {
  930. // Must not set the primitive or float flag
  931. if( flags & (asOBJ_APP_PRIMITIVE |
  932. asOBJ_APP_FLOAT) )
  933. return ConfigError(asINVALID_ARG);
  934. }
  935. else if( flags & asOBJ_APP_PRIMITIVE )
  936. {
  937. // Must not set the class flags nor the float flag
  938. if( flags & (asOBJ_APP_CLASS |
  939. asOBJ_APP_CLASS_CONSTRUCTOR |
  940. asOBJ_APP_CLASS_DESTRUCTOR |
  941. asOBJ_APP_CLASS_ASSIGNMENT |
  942. asOBJ_APP_FLOAT) )
  943. return ConfigError(asINVALID_ARG);
  944. }
  945. else if( flags & asOBJ_APP_FLOAT )
  946. {
  947. // Must not set the class flags nor the primitive flag
  948. if( flags & (asOBJ_APP_CLASS |
  949. asOBJ_APP_CLASS_CONSTRUCTOR |
  950. asOBJ_APP_CLASS_DESTRUCTOR |
  951. asOBJ_APP_CLASS_ASSIGNMENT |
  952. asOBJ_APP_PRIMITIVE) )
  953. return ConfigError(asINVALID_ARG);
  954. }
  955. else if( flags & (asOBJ_APP_CLASS_CONSTRUCTOR |
  956. asOBJ_APP_CLASS_DESTRUCTOR |
  957. asOBJ_APP_CLASS_ASSIGNMENT) )
  958. {
  959. // Must not set the class properties, without the class flag
  960. return ConfigError(asINVALID_ARG);
  961. }
  962. }
  963. else
  964. return ConfigError(asINVALID_ARG);
  965. // Don't allow anything else than the defined flags
  966. if( flags - (flags & asOBJ_MASK_VALID_FLAGS) )
  967. return ConfigError(asINVALID_ARG);
  968. // Value types must have a defined size
  969. if( (flags & asOBJ_VALUE) && byteSize == 0 )
  970. {
  971. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_VALUE_TYPE_MUST_HAVE_SIZE);
  972. return ConfigError(asINVALID_ARG);
  973. }
  974. // Verify type name
  975. if( name == 0 )
  976. return ConfigError(asINVALID_NAME);
  977. asCString typeName;
  978. asCBuilder bld(this, 0);
  979. if( flags & asOBJ_TEMPLATE )
  980. {
  981. asCString subtypeName;
  982. r = bld.ParseTemplateDecl(name, &typeName, &subtypeName);
  983. if( r < 0 )
  984. return r;
  985. // Verify that the template name hasn't been registered as a type already
  986. asUINT n;
  987. for( n = 0; n < objectTypes.GetLength(); n++ )
  988. {
  989. if( objectTypes[n] && objectTypes[n]->name == typeName )
  990. return asALREADY_REGISTERED;
  991. }
  992. asCObjectType *type = asNEW(asCObjectType)(this);
  993. type->name = typeName;
  994. type->size = byteSize;
  995. type->flags = flags;
  996. // Store it in the object types
  997. objectTypes.PushLast(type);
  998. // Define a template subtype
  999. asCObjectType *subtype = 0;
  1000. for( n = 0; n < templateSubTypes.GetLength(); n++ )
  1001. {
  1002. if( templateSubTypes[n]->name == subtypeName )
  1003. {
  1004. subtype = templateSubTypes[n];
  1005. break;
  1006. }
  1007. }
  1008. if( subtype == 0 )
  1009. {
  1010. // Create the new subtype if not already existing
  1011. subtype = asNEW(asCObjectType)(this);
  1012. subtype->name = subtypeName;
  1013. subtype->size = 0;
  1014. subtype->flags = asOBJ_TEMPLATE_SUBTYPE;
  1015. templateSubTypes.PushLast(subtype);
  1016. subtype->AddRef();
  1017. }
  1018. type->templateSubType = asCDataType::CreateObject(subtype, false);
  1019. subtype->AddRef();
  1020. currentGroup->objTypes.PushLast(type);
  1021. if( defaultArrayObjectType == 0 )
  1022. {
  1023. // TODO: The default array object type should be defined by the application
  1024. // The default array object type is registered by the engine itself
  1025. defaultArrayObjectType = type;
  1026. type->AddRef();
  1027. }
  1028. else
  1029. {
  1030. registeredObjTypes.PushLast(type);
  1031. }
  1032. }
  1033. else
  1034. {
  1035. typeName = name;
  1036. // Verify if the name has been registered as a type already
  1037. asUINT n;
  1038. for( n = 0; n < objectTypes.GetLength(); n++ )
  1039. {
  1040. if( objectTypes[n] && objectTypes[n]->name == typeName )
  1041. return asALREADY_REGISTERED;
  1042. }
  1043. for( n = 0; n < templateTypes.GetLength(); n++ )
  1044. {
  1045. if( templateTypes[n] && templateTypes[n]->name == typeName )
  1046. return asALREADY_REGISTERED;
  1047. }
  1048. // Verify the most recently created template instance type
  1049. asCObjectType *mostRecentTemplateInstanceType = 0;
  1050. if( templateInstanceTypes.GetLength() )
  1051. mostRecentTemplateInstanceType = templateInstanceTypes[templateInstanceTypes.GetLength()-1];
  1052. // Use builder to parse the datatype
  1053. asCDataType dt;
  1054. bool oldMsgCallback = msgCallback; msgCallback = false;
  1055. r = bld.ParseDataType(name, &dt);
  1056. msgCallback = oldMsgCallback;
  1057. // If the builder fails, then the type name
  1058. // is new and it should be registered
  1059. if( r < 0 )
  1060. {
  1061. // Make sure the name is not a reserved keyword
  1062. asCTokenizer t;
  1063. size_t tokenLen;
  1064. int token = t.GetToken(name, typeName.GetLength(), &tokenLen);
  1065. if( token != ttIdentifier || typeName.GetLength() != tokenLen )
  1066. return ConfigError(asINVALID_NAME);
  1067. int r = bld.CheckNameConflict(name, 0, 0);
  1068. if( r < 0 )
  1069. return ConfigError(asNAME_TAKEN);
  1070. // Don't have to check against members of object
  1071. // types as they are allowed to use the names
  1072. // Put the data type in the list
  1073. asCObjectType *type = asNEW(asCObjectType)(this);
  1074. type->name = typeName;
  1075. type->size = byteSize;
  1076. type->flags = flags;
  1077. objectTypes.PushLast(type);
  1078. registeredObjTypes.PushLast(type);
  1079. currentGroup->objTypes.PushLast(type);
  1080. }
  1081. else
  1082. {
  1083. // The application is registering a template specialization so we
  1084. // need to replace the template instance type with the new type.
  1085. // TODO: Template: We don't require the lower dimensions to be registered first for registered template types
  1086. // int[][] must not be allowed to be registered
  1087. // if int[] hasn't been registered first
  1088. if( dt.GetSubType().IsTemplate() )
  1089. return ConfigError(asLOWER_ARRAY_DIMENSION_NOT_REGISTERED);
  1090. if( dt.IsReadOnly() ||
  1091. dt.IsReference() )
  1092. return ConfigError(asINVALID_TYPE);
  1093. // Was the template instance type created before?
  1094. if( templateInstanceTypes[templateInstanceTypes.GetLength()-1] == mostRecentTemplateInstanceType ||
  1095. mostRecentTemplateInstanceType == dt.GetObjectType() )
  1096. // TODO: Should have a better error message
  1097. return ConfigError(asNOT_SUPPORTED);
  1098. // TODO: Add this again. The type is used by the factory stubs so we need to discount that
  1099. // Is the template instance type already being used?
  1100. // if( dt.GetObjectType()->GetRefCount() > 1 )
  1101. // return ConfigError(asNOT_SUPPORTED);
  1102. // Put the data type in the list
  1103. asCObjectType *type = asNEW(asCObjectType)(this);
  1104. type->name = dt.GetObjectType()->name;
  1105. type->templateSubType = dt.GetSubType();
  1106. if( type->templateSubType.GetObjectType() ) type->templateSubType.GetObjectType()->AddRef();
  1107. type->size = byteSize;
  1108. type->flags = flags;
  1109. templateTypes.PushLast(type);
  1110. currentGroup->objTypes.PushLast(type);
  1111. // Remove the template instance type, which will no longer be used.
  1112. RemoveTemplateInstanceType(dt.GetObjectType());
  1113. }
  1114. }
  1115. return asSUCCESS;
  1116. }
  1117. // interface
  1118. int asCScriptEngine::RegisterObjectBehaviour(const char *datatype, asEBehaviours behaviour, const char *decl, const asSFuncPtr &funcPointer, asDWORD callConv)
  1119. {
  1120. if( datatype == 0 ) return ConfigError(asINVALID_ARG);
  1121. // Determine the object type
  1122. asCBuilder bld(this, 0);
  1123. asCDataType type;
  1124. int r = bld.ParseDataType(datatype, &type);
  1125. if( r < 0 )
  1126. return ConfigError(r);
  1127. if( type.GetObjectType() == 0 )
  1128. return ConfigError(asINVALID_TYPE);
  1129. if( type.IsReadOnly() || type.IsReference() )
  1130. return ConfigError(asINVALID_TYPE);
  1131. return RegisterBehaviourToObjectType(type.GetObjectType(), behaviour, decl, funcPointer, callConv);
  1132. }
  1133. // internal
  1134. int asCScriptEngine::RegisterBehaviourToObjectType(asCObjectType *objectType, asEBehaviours behaviour, const char *decl, const asSFuncPtr &funcPointer, asDWORD callConv)
  1135. {
  1136. asSSystemFunctionInterface internal;
  1137. if( behaviour == asBEHAVE_FACTORY ||
  1138. behaviour == asBEHAVE_TEMPLATE_CALLBACK )
  1139. {
  1140. #ifdef AS_MAX_PORTABILITY
  1141. if( callConv != asCALL_GENERIC )
  1142. return ConfigError(asNOT_SUPPORTED);
  1143. #endif
  1144. int r = DetectCallingConvention(false, funcPointer, callConv, &internal);
  1145. if( r < 0 )
  1146. return ConfigError(r);
  1147. }
  1148. else
  1149. {
  1150. #ifdef AS_MAX_PORTABILITY
  1151. if( callConv != asCALL_GENERIC )
  1152. return ConfigError(asNOT_SUPPORTED);
  1153. #else
  1154. if( callConv != asCALL_THISCALL &&
  1155. callConv != asCALL_CDECL_OBJLAST &&
  1156. callConv != asCALL_CDECL_OBJFIRST &&
  1157. callConv != asCALL_GENERIC )
  1158. return ConfigError(asNOT_SUPPORTED);
  1159. #endif
  1160. int r = DetectCallingConvention(true, funcPointer, callConv, &internal);
  1161. if( r < 0 )
  1162. return ConfigError(r);
  1163. }
  1164. isPrepared = false;
  1165. asSTypeBehaviour *beh = &objectType->beh;
  1166. // Verify function declaration
  1167. asCScriptFunction func(this, 0, -1);
  1168. asCBuilder bld(this, 0);
  1169. int r = bld.ParseFunctionDeclaration(objectType, decl, &func, true, &internal.paramAutoHandles, &internal.returnAutoHandle);
  1170. if( r < 0 )
  1171. return ConfigError(asINVALID_DECLARATION);
  1172. func.name.Format("_beh_%d_", behaviour);
  1173. if( behaviour != asBEHAVE_FACTORY )
  1174. func.objectType = objectType;
  1175. // Check if the method restricts that use of the template to value types or reference types
  1176. if( objectType->flags & asOBJ_TEMPLATE )
  1177. {
  1178. if( func.returnType.GetObjectType() == objectType->templateSubType.GetObjectType() )
  1179. {
  1180. if( func.returnType.IsObjectHandle() )
  1181. objectType->acceptValueSubType = false;
  1182. else if( !func.returnType.IsReference() )
  1183. objectType->acceptRefSubType = false;
  1184. }
  1185. for( asUINT n = 0; n < func.parameterTypes.GetLength(); n++ )
  1186. {
  1187. if( func.parameterTypes[n].GetObjectType() == objectType->templateSubType.GetObjectType() )
  1188. {
  1189. // TODO: If unsafe references are allowed, then inout references allow value types
  1190. if( func.parameterTypes[n].IsObjectHandle() || (func.parameterTypes[n].IsReference() && func.inOutFlags[n] == asTM_INOUTREF) )
  1191. objectType->acceptValueSubType = false;
  1192. else if( !func.parameterTypes[n].IsReference() )
  1193. objectType->acceptRefSubType = false;
  1194. }
  1195. }
  1196. }
  1197. if( behaviour == asBEHAVE_CONSTRUCT )
  1198. {
  1199. // TODO: Add asBEHAVE_IMPLICIT_CONSTRUCT
  1200. // Verify that the return type is void
  1201. if( func.returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  1202. return ConfigError(asINVALID_DECLARATION);
  1203. if( objectType->flags & asOBJ_SCRIPT_OBJECT )
  1204. {
  1205. // The script object is a special case
  1206. asASSERT(func.parameterTypes.GetLength() == 1);
  1207. beh->construct = AddBehaviourFunction(func, internal);
  1208. beh->factory = beh->construct;
  1209. scriptFunctions[beh->factory]->AddRef();
  1210. beh->constructors.PushLast(beh->construct);
  1211. beh->factories.PushLast(beh->factory);
  1212. func.id = beh->construct;
  1213. }
  1214. else
  1215. {
  1216. // Verify that it is a value type
  1217. if( !(func.objectType->flags & asOBJ_VALUE) )
  1218. {
  1219. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1220. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1221. }
  1222. // Implicit constructors must take one and only one parameter
  1223. /* if( behaviour == asBEHAVE_IMPLICIT_CONSTRUCT &&
  1224. func.parameterTypes.GetLength() != 1 )
  1225. return ConfigError(asINVALID_DECLARATION);
  1226. */
  1227. // TODO: Verify that the same constructor hasn't been registered already
  1228. // Store all constructors in a list
  1229. if( func.parameterTypes.GetLength() == 0 )
  1230. {
  1231. func.id = beh->construct = AddBehaviourFunction(func, internal);
  1232. beh->constructors.PushLast(beh->construct);
  1233. }
  1234. else
  1235. {
  1236. func.id = AddBehaviourFunction(func, internal);
  1237. beh->constructors.PushLast(func.id);
  1238. /*
  1239. if( behaviour == asBEHAVE_IMPLICIT_CONSTRUCT )
  1240. {
  1241. beh->operators.PushLast(behaviour);
  1242. beh->operators.PushLast(func.id);
  1243. }
  1244. */ }
  1245. }
  1246. }
  1247. else if( behaviour == asBEHAVE_DESTRUCT )
  1248. {
  1249. // Must be a value type
  1250. if( !(func.objectType->flags & asOBJ_VALUE) )
  1251. {
  1252. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1253. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1254. }
  1255. if( beh->destruct )
  1256. return ConfigError(asALREADY_REGISTERED);
  1257. // Verify that the return type is void
  1258. if( func.returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  1259. return ConfigError(asINVALID_DECLARATION);
  1260. // Verify that there are no parameters
  1261. if( func.parameterTypes.GetLength() > 0 )
  1262. return ConfigError(asINVALID_DECLARATION);
  1263. func.id = beh->destruct = AddBehaviourFunction(func, internal);
  1264. }
  1265. else if( behaviour == asBEHAVE_FACTORY )
  1266. {
  1267. // TODO: Add asBEHAVE_IMPLICIT_FACTORY
  1268. // Must be a ref type and must not have asOBJ_NOHANDLE
  1269. if( !(objectType->flags & asOBJ_REF) || (objectType->flags & asOBJ_NOHANDLE) )
  1270. {
  1271. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1272. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1273. }
  1274. // Verify that the return type is a handle to the type
  1275. if( func.returnType != asCDataType::CreateObjectHandle(objectType, false) )
  1276. return ConfigError(asINVALID_DECLARATION);
  1277. // Implicit factories must take one and only one parameter
  1278. /* if( behaviour == asBEHAVE_IMPLICIT_FACTORY &&
  1279. func.parameterTypes.GetLength() != 1 )
  1280. return ConfigError(asINVALID_DECLARATION);
  1281. */
  1282. // TODO: Verify that the same factory function hasn't been registered already
  1283. // The templates take a hidden parameter with the object type
  1284. if( (objectType->flags & asOBJ_TEMPLATE) &&
  1285. (func.parameterTypes.GetLength() == 0 ||
  1286. !func.parameterTypes[0].IsReference()) )
  1287. {
  1288. return ConfigError(asINVALID_DECLARATION);
  1289. }
  1290. // Store all factory functions in a list
  1291. if( (func.parameterTypes.GetLength() == 0) ||
  1292. (func.parameterTypes.GetLength() == 1 && (objectType->flags & asOBJ_TEMPLATE)) )
  1293. {
  1294. func.id = beh->factory = AddBehaviourFunction(func, internal);
  1295. beh->factories.PushLast(beh->factory);
  1296. }
  1297. else
  1298. {
  1299. func.id = AddBehaviourFunction(func, internal);
  1300. beh->factories.PushLast(func.id);
  1301. /*
  1302. if( behaviour == asBEHAVE_IMPLICIT_FACTORY )
  1303. {
  1304. beh->operators.PushLast(behaviour);
  1305. beh->operators.PushLast(func.id);
  1306. }
  1307. */ }
  1308. }
  1309. else if( behaviour == asBEHAVE_ADDREF )
  1310. {
  1311. // Must be a ref type and must not have asOBJ_NOHANDLE, nor asOBJ_SCOPED
  1312. if( !(func.objectType->flags & asOBJ_REF) ||
  1313. (func.objectType->flags & asOBJ_NOHANDLE) ||
  1314. (func.objectType->flags & asOBJ_SCOPED) )
  1315. {
  1316. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1317. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1318. }
  1319. if( beh->addref )
  1320. return ConfigError(asALREADY_REGISTERED);
  1321. // Verify that the return type is void
  1322. if( func.returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  1323. return ConfigError(asINVALID_DECLARATION);
  1324. // Verify that there are no parameters
  1325. if( func.parameterTypes.GetLength() > 0 )
  1326. return ConfigError(asINVALID_DECLARATION);
  1327. func.id = beh->addref = AddBehaviourFunction(func, internal);
  1328. }
  1329. else if( behaviour == asBEHAVE_RELEASE )
  1330. {
  1331. // Must be a ref type and must not have asOBJ_NOHANDLE
  1332. if( !(func.objectType->flags & asOBJ_REF) || (func.objectType->flags & asOBJ_NOHANDLE) )
  1333. {
  1334. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1335. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1336. }
  1337. if( beh->release )
  1338. return ConfigError(asALREADY_REGISTERED);
  1339. // Verify that the return type is void
  1340. if( func.returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  1341. return ConfigError(asINVALID_DECLARATION);
  1342. // Verify that there are no parameters
  1343. if( func.parameterTypes.GetLength() > 0 )
  1344. return ConfigError(asINVALID_DECLARATION);
  1345. func.id = beh->release = AddBehaviourFunction(func, internal);
  1346. }
  1347. else if( behaviour == asBEHAVE_TEMPLATE_CALLBACK )
  1348. {
  1349. // Must be a template type
  1350. if( !(func.objectType->flags & asOBJ_TEMPLATE) )
  1351. {
  1352. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1353. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1354. }
  1355. if( beh->templateCallback )
  1356. return ConfigError(asALREADY_REGISTERED);
  1357. // Verify that the return type is bool
  1358. if( func.returnType != asCDataType::CreatePrimitive(ttBool, false) )
  1359. return ConfigError(asINVALID_DECLARATION);
  1360. // Verify that there is one parameters
  1361. if( func.parameterTypes.GetLength() != 1 )
  1362. return ConfigError(asINVALID_DECLARATION);
  1363. func.id = beh->templateCallback = AddBehaviourFunction(func, internal);
  1364. }
  1365. else if( behaviour == asBEHAVE_INDEX )
  1366. {
  1367. // Verify that the var type is not used
  1368. if( VerifyVarTypeNotInFunction(&func) < 0 )
  1369. return ConfigError(asINVALID_DECLARATION);
  1370. // Verify that there is only one parameter
  1371. if( func.parameterTypes.GetLength() != 1 )
  1372. return ConfigError(asINVALID_DECLARATION);
  1373. // Verify that the return type is not void
  1374. if( func.returnType.GetTokenType() == ttVoid )
  1375. return ConfigError(asINVALID_DECLARATION);
  1376. // TODO: Verify that the operator hasn't been registered already
  1377. beh->operators.PushLast(behaviour);
  1378. func.id = AddBehaviourFunction(func, internal);
  1379. beh->operators.PushLast(func.id);
  1380. }
  1381. else if( behaviour >= asBEHAVE_FIRST_GC &&
  1382. behaviour <= asBEHAVE_LAST_GC )
  1383. {
  1384. // Only allow GC behaviours for types registered to be garbage collected
  1385. if( !(func.objectType->flags & asOBJ_GC) )
  1386. {
  1387. WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_ILLEGAL_BEHAVIOUR_FOR_TYPE);
  1388. return ConfigError(asILLEGAL_BEHAVIOUR_FOR_TYPE);
  1389. }
  1390. // Verify parameter count
  1391. if( (behaviour == asBEHAVE_GETREFCOUNT ||
  1392. behaviour == asBEHAVE_SETGCFLAG ||
  1393. behaviour == asBEHAVE_GETGCFLAG) &&
  1394. func.parameterTypes.GetLength() != 0 )
  1395. return ConfigError(asINVALID_DECLARATION);
  1396. if( (behaviour == asBEHAVE_ENUMREFS ||
  1397. behaviour == asBEHAVE_RELEASEREFS) &&
  1398. func.parameterTypes.GetLength() != 1 )
  1399. return ConfigError(asINVALID_DECLARATION);
  1400. // Verify return type
  1401. if( behaviour == asBEHAVE_GETREFCOUNT &&
  1402. func.returnType != asCDataType::CreatePrimitive(ttInt, false) )
  1403. return ConfigError(asINVALID_DECLARATION);
  1404. if( behaviour == asBEHAVE_GETGCFLAG &&
  1405. func.returnType != asCDataType::CreatePrimitive(ttBool, false) )
  1406. return ConfigError(asINVALID_DECLARATION);
  1407. if( (behaviour == asBEHAVE_SETGCFLAG ||
  1408. behaviour == asBEHAVE_ENUMREFS ||
  1409. behaviour == asBEHAVE_RELEASEREFS) &&
  1410. func.returnType != asCDataType::CreatePrimitive(ttVoid, false) )
  1411. return ConfigError(asINVALID_DECLARATION);
  1412. if( behaviour == asBEHAVE_GETREFCOUNT )
  1413. func.id = beh->gcGetRefCount = AddBehaviourFunction(func, internal);
  1414. else if( behaviour == asBEHAVE_SETGCFLAG )
  1415. func.id = beh->gcSetFlag = AddBehaviourFunction(func, internal);
  1416. else if( behaviour == asBEHAVE_GETGCFLAG )
  1417. func.id = beh->gcGetFlag = AddBehaviourFunction(func, internal);
  1418. else if( behaviour == asBEHAVE_ENUMREFS )
  1419. func.id = beh->gcEnumReferences = AddBehaviourFunction(func, internal);
  1420. else if( behaviour == asBEHAVE_RELEASEREFS )
  1421. func.id = beh->gcReleaseAllReferences = AddBehaviourFunction(func, internal);
  1422. }
  1423. else if( behaviour == asBEHAVE_IMPLICIT_VALUE_CAST ||
  1424. behaviour == asBEHAVE_VALUE_CAST )
  1425. {
  1426. // Verify parameter count
  1427. if( func.parameterTypes.GetLength() != 0 )
  1428. return ConfigError(asINVALID_DECLARATION);
  1429. // Verify return type
  1430. if( func.returnType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttBool, false)) )
  1431. return ConfigError(asNOT_SUPPORTED);
  1432. if( func.returnType.IsEqualExceptRefAndConst(asCDataType::CreatePrimitive(ttVoid, false)) )
  1433. return ConfigError(asINVALID_DECLARATION);
  1434. // TODO: verify that the same cast is not registered already (const or non-const is treated the same for the return type)
  1435. beh->operators.PushLast(behaviour);
  1436. func.id = AddBehaviourFunction(func, internal);
  1437. beh->operators.PushLast(func.id);
  1438. }
  1439. else if( behaviour == asBEHAVE_REF_CAST ||
  1440. behaviour == asBEHAVE_IMPLICIT_REF_CAST )
  1441. {
  1442. // Verify parameter count
  1443. if( func.parameterTypes.GetLength() != 0 )
  1444. return ConfigError(asINVALID_DECLARATION);
  1445. // Verify return type
  1446. if( !func.returnType.IsObjectHandle() )
  1447. return ConfigError(asINVALID_DECLARATION);
  1448. // TODO: verify that the same cast is not registered already (cosnt or non-const is treated the same for the return type)
  1449. beh->operators.PushLast(behaviour);
  1450. func.id = AddBehaviourFunction(func, internal);
  1451. beh->operators.PushLast(func.id);
  1452. }
  1453. else
  1454. {
  1455. asASSERT(false);
  1456. return ConfigError(asINVALID_ARG);
  1457. }
  1458. // Return function id as success
  1459. return func.id;
  1460. }
  1461. int asCScriptEngine::VerifyVarTypeNotInFunction(asCScriptFunction *func)
  1462. {
  1463. // Don't allow var type in this function
  1464. if( func->returnType.GetTokenType() == ttQuestion )
  1465. return asINVALID_DECLARATION;
  1466. for( unsigned int n = 0; n < func->parameterTypes.GetLength(); n++ )
  1467. if( func->parameterTypes[n].GetTokenType() == ttQuestion )
  1468. return asINVALID_DECLARATION;
  1469. return 0;
  1470. }
  1471. int asCScriptEngine::AddBehaviourFunction(asCScriptFunction &func, asSSystemFunctionInterface &internal)
  1472. {
  1473. asUINT n;
  1474. int id = GetNextScriptFunctionId();
  1475. asSSystemFunctionInterface *newInterface = asNEW(asSSystemFunctionInterface);
  1476. newInterface->func = internal.func;
  1477. newInterface->baseOffset = internal.baseOffset;
  1478. newInterface->callConv = internal.callConv;
  1479. newInterface->scriptReturnSize = internal.scriptReturnSize;
  1480. newInterface->hostReturnInMemory = internal.hostReturnInMemory;
  1481. newInterface->hostReturnFloat = internal.hostReturnFloat;
  1482. newInterface->hostReturnSize = internal.hostReturnSize;
  1483. newInterface->paramSize = internal.paramSize;
  1484. newInterface->takesObjByVal = internal.takesObjByVal;
  1485. newInterface->paramAutoHandles = internal.paramAutoHandles;
  1486. newInterface->returnAutoHandle = internal.returnAutoHandle;
  1487. newInterface->hasAutoHandles = internal.hasAutoHandles;
  1488. asCScriptFunction *f = asNEW(asCScriptFunction)(this, 0, asFUNC_SYSTEM);
  1489. asASSERT(func.name != "" && func.name != "f");
  1490. f->name = func.name;
  1491. f->sysFuncIntf = newInterface;
  1492. f->returnType = func.returnType;
  1493. f->objectType = func.objectType;
  1494. f->id = id;
  1495. f->isReadOnly = func.isReadOnly;
  1496. for( n = 0; n < func.parameterTypes.GetLength(); n++ )
  1497. {
  1498. f->parameterTypes.PushLast(func.parameterTypes[n]);
  1499. f->inOutFlags.PushLast(func.inOutFlags[n]);
  1500. }
  1501. SetScriptFunction(f);
  1502. // If parameter type from other groups are used, add references
  1503. if( f->returnType.GetObjectType() )
  1504. {
  1505. asCConfigGroup *group = FindConfigGroupForObjectType(f->returnType.GetObjectType());
  1506. currentGroup->RefConfigGroup(group);
  1507. }
  1508. for( n = 0; n < f->parameterTypes.GetLength(); n++ )
  1509. {
  1510. if( f->parameterTypes[n].GetObjectType() )
  1511. {
  1512. asCConfigGroup *group = FindConfigGroupForObjectType(f->parameterTypes[n].GetObjectType());
  1513. currentGroup->RefConfigGroup(group);
  1514. }
  1515. }
  1516. return id;
  1517. }
  1518. // interface
  1519. int asCScriptEngine::RegisterGlobalProperty(const char *declaration, void *pointer)
  1520. {
  1521. asCDataType type;
  1522. asCString name;
  1523. int r;
  1524. asCBuilder bld(t

Large files files are truncated, but you can click here to view the full file