2014-10-27 18:43:09 -04:00
|
|
|
/*
|
|
|
|
AngelCode Scripting Library
|
2021-11-04 16:43:10 -04:00
|
|
|
Copyright (c) 2003-2021 Andreas Jonsson
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any
|
2014-10-27 18:43:09 -04:00
|
|
|
damages arising from the use of this software.
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
Permission is granted to anyone to use this software for any
|
|
|
|
purpose, including commercial applications, and to alter it and
|
2014-10-27 18:43:09 -04:00
|
|
|
redistribute it freely, subject to the following restrictions:
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
1. The origin of this software must not be misrepresented; you
|
2014-10-27 18:43:09 -04:00
|
|
|
must not claim that you wrote the original software. If you use
|
2017-11-20 08:02:33 -05:00
|
|
|
this software in a product, an acknowledgment in the product
|
2014-10-27 18:43:09 -04:00
|
|
|
documentation would be appreciated but is not required.
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
2. Altered source versions must be plainly marked as such, and
|
2014-10-27 18:43:09 -04:00
|
|
|
must not be misrepresented as being the original software.
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
3. This notice may not be removed or altered from any source
|
2014-10-27 18:43:09 -04:00
|
|
|
distribution.
|
|
|
|
|
|
|
|
The original version of this library can be located at:
|
|
|
|
http://www.angelcode.com/angelscript/
|
|
|
|
|
|
|
|
Andreas Jonsson
|
|
|
|
andreas@angelcode.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// as_module.cpp
|
|
|
|
//
|
|
|
|
// A class that holds a script module
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "as_config.h"
|
|
|
|
#include "as_module.h"
|
|
|
|
#include "as_builder.h"
|
|
|
|
#include "as_context.h"
|
|
|
|
#include "as_texts.h"
|
|
|
|
#include "as_debug.h"
|
|
|
|
#include "as_restore.h"
|
|
|
|
|
|
|
|
BEGIN_AS_NAMESPACE
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// internal
|
|
|
|
asCModule::asCModule(const char *name, asCScriptEngine *engine)
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_name = name;
|
|
|
|
m_engine = engine;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
m_userData = 0;
|
|
|
|
m_builder = 0;
|
|
|
|
m_isGlobalVarInitialized = false;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
m_accessMask = 1;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
m_defaultNamespace = engine->nameSpaces[0];
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// internal
|
|
|
|
asCModule::~asCModule()
|
|
|
|
{
|
|
|
|
InternalReset();
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// The builder is not removed by InternalReset because it holds the script
|
2015-05-11 19:40:43 -04:00
|
|
|
// sections that will be built, so we need to explictly remove it now if it exists
|
2021-11-04 16:43:10 -04:00
|
|
|
if( m_builder )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asDELETE(m_builder,asCBuilder);
|
|
|
|
m_builder = 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
if( m_engine )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
// Clean the user data
|
2021-11-04 16:43:10 -04:00
|
|
|
for( asUINT n = 0; n < m_userData.GetLength(); n += 2 )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
if( m_userData[n+1] )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
for( asUINT c = 0; c < m_engine->cleanModuleFuncs.GetLength(); c++ )
|
|
|
|
if( m_engine->cleanModuleFuncs[c].type == m_userData[n] )
|
|
|
|
m_engine->cleanModuleFuncs[c].cleanFunc(this);
|
2015-05-11 19:40:43 -04:00
|
|
|
}
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Remove the module from the engine
|
2021-11-04 16:43:10 -04:00
|
|
|
ACQUIREEXCLUSIVE(m_engine->engineRWLock);
|
2015-05-11 19:40:43 -04:00
|
|
|
// The module must have been discarded before it is deleted
|
2021-11-04 16:43:10 -04:00
|
|
|
asASSERT( !m_engine->scriptModules.Exists(this) );
|
|
|
|
m_engine->discardedModules.RemoveValue(this);
|
|
|
|
RELEASEEXCLUSIVE(m_engine->engineRWLock);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
void asCModule::Discard()
|
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
// Reset the global variables already so that no object in the global variables keep the module alive forever.
|
|
|
|
// If any live object tries to access the global variables during clean up they will fail with a script exception,
|
|
|
|
// so the application must keep that in mind before discarding a module.
|
|
|
|
CallExit();
|
|
|
|
|
|
|
|
// Keep a local copy of the engine pointer, because once the module is moved do the discarded
|
|
|
|
// pile, it is possible that another thread might discard it while we are still in here. So no
|
|
|
|
// further access to members may be done after that
|
2021-11-04 16:43:10 -04:00
|
|
|
asCScriptEngine *engine = m_engine;
|
2015-05-11 19:40:43 -04:00
|
|
|
|
|
|
|
// Instead of deleting the module immediately, move it to the discarded pile
|
2017-11-20 08:02:33 -05:00
|
|
|
// This will turn it invisible to the application, yet keep it alive until all
|
2015-05-11 19:40:43 -04:00
|
|
|
// external references to its entities have been released.
|
|
|
|
ACQUIREEXCLUSIVE(engine->engineRWLock);
|
2021-11-04 16:43:10 -04:00
|
|
|
if( engine->lastModule == this )
|
|
|
|
engine->lastModule = 0;
|
|
|
|
engine->scriptModules.RemoveValue(this);
|
|
|
|
engine->discardedModules.PushLast(this);
|
|
|
|
RELEASEEXCLUSIVE(engine->engineRWLock);
|
2015-05-11 19:40:43 -04:00
|
|
|
|
|
|
|
// Allow the engine to go over the list of discarded modules to see what can be cleaned up at this moment.
|
|
|
|
// Don't do this if the engine is already shutting down, as it will be done explicitly by the engine itself with error reporting
|
2021-11-04 16:43:10 -04:00
|
|
|
if( !engine->shuttingDown )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
if( engine->ep.autoGarbageCollect )
|
|
|
|
engine->GarbageCollect();
|
2015-05-11 19:40:43 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// GarbageCollect calls DeleteDiscardedModules, so no need
|
|
|
|
// to call it again if we already called GarbageCollect
|
2021-11-04 16:43:10 -04:00
|
|
|
engine->DeleteDiscardedModules();
|
2015-05-11 19:40:43 -04:00
|
|
|
}
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
2015-05-11 19:40:43 -04:00
|
|
|
void *asCModule::SetUserData(void *data, asPWORD type)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
// 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
|
2021-11-04 16:43:10 -04:00
|
|
|
ACQUIREEXCLUSIVE(m_engine->engineRWLock);
|
2015-05-11 19:40:43 -04:00
|
|
|
|
|
|
|
// 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.
|
2021-11-04 16:43:10 -04:00
|
|
|
for( asUINT n = 0; n < m_userData.GetLength(); n += 2 )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
if( m_userData[n] == type )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
void *oldData = reinterpret_cast<void*>(m_userData[n+1]);
|
|
|
|
m_userData[n+1] = reinterpret_cast<asPWORD>(data);
|
2015-05-11 19:40:43 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
RELEASEEXCLUSIVE(m_engine->engineRWLock);
|
2015-05-11 19:40:43 -04:00
|
|
|
|
|
|
|
return oldData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
m_userData.PushLast(type);
|
|
|
|
m_userData.PushLast(reinterpret_cast<asPWORD>(data));
|
2015-05-11 19:40:43 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
RELEASEEXCLUSIVE(m_engine->engineRWLock);
|
2015-05-11 19:40:43 -04:00
|
|
|
|
|
|
|
return 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
2015-05-11 19:40:43 -04:00
|
|
|
void *asCModule::GetUserData(asPWORD type) const
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
// There may be multiple threads reading, but when
|
|
|
|
// setting the user data nobody must be reading.
|
2021-11-04 16:43:10 -04:00
|
|
|
ACQUIRESHARED(m_engine->engineRWLock);
|
2015-05-11 19:40:43 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
for( asUINT n = 0; n < m_userData.GetLength(); n += 2 )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
if( m_userData[n] == type )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
void *ud = reinterpret_cast<void*>(m_userData[n+1]);
|
|
|
|
RELEASESHARED(m_engine->engineRWLock);
|
2015-05-11 19:40:43 -04:00
|
|
|
return ud;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
RELEASESHARED(m_engine->engineRWLock);
|
2015-05-11 19:40:43 -04:00
|
|
|
|
|
|
|
return 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
asIScriptEngine *asCModule::GetEngine() const
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
return m_engine;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
2017-11-20 08:02:33 -05:00
|
|
|
void asCModule::SetName(const char *in_name)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_name = in_name;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
const char *asCModule::GetName() const
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
return m_name.AddressOf();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
const char *asCModule::GetDefaultNamespace() const
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
return m_defaultNamespace->name.AddressOf();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
int asCModule::SetDefaultNamespace(const char *nameSpace)
|
|
|
|
{
|
|
|
|
// TODO: cleanup: This function is similar to asCScriptEngine::SetDefaultNamespace. Can we reuse the code?
|
|
|
|
if( nameSpace == 0 )
|
|
|
|
return asINVALID_ARG;
|
|
|
|
|
|
|
|
asCString ns = nameSpace;
|
|
|
|
if( ns != "" )
|
|
|
|
{
|
|
|
|
// Make sure the namespace is composed of alternating identifier and ::
|
|
|
|
size_t pos = 0;
|
|
|
|
bool expectIdentifier = true;
|
|
|
|
size_t len;
|
|
|
|
eTokenType t = ttIdentifier;
|
|
|
|
|
|
|
|
for( ; pos < ns.GetLength(); pos += len )
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
t = m_engine->tok.GetToken(ns.AddressOf() + pos, ns.GetLength() - pos, &len);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( (expectIdentifier && t != ttIdentifier) || (!expectIdentifier && t != ttScope) )
|
|
|
|
return asINVALID_DECLARATION;
|
|
|
|
|
|
|
|
expectIdentifier = !expectIdentifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the namespace ends with :: then strip it off
|
|
|
|
if( t == ttScope )
|
|
|
|
ns.SetLength(ns.GetLength()-2);
|
|
|
|
}
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
m_defaultNamespace = m_engine->AddNameSpace(ns.AddressOf());
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
2017-11-20 08:02:33 -05:00
|
|
|
int asCModule::AddScriptSection(const char *in_name, const char *in_code, size_t in_codeLength, int in_lineOffset)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
#ifdef AS_NO_COMPILER
|
2017-11-20 08:02:33 -05:00
|
|
|
UNUSED_VAR(in_name);
|
|
|
|
UNUSED_VAR(in_code);
|
|
|
|
UNUSED_VAR(in_codeLength);
|
|
|
|
UNUSED_VAR(in_lineOffset);
|
2014-10-27 18:43:09 -04:00
|
|
|
return asNOT_SUPPORTED;
|
|
|
|
#else
|
2021-11-04 16:43:10 -04:00
|
|
|
if( !m_builder )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_builder = asNEW(asCBuilder)(m_engine, this);
|
|
|
|
if( m_builder == 0 )
|
2014-10-27 18:43:09 -04:00
|
|
|
return asOUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
return m_builder->AddCode(in_name, in_code, (int)in_codeLength, in_lineOffset, (int)m_engine->GetScriptSectionNameIndex(in_name ? in_name : ""), m_engine->ep.copyScriptSections);
|
2014-10-27 18:43:09 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal
|
|
|
|
void asCModule::JITCompile()
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asIJITCompiler *jit = m_engine->GetJITCompiler();
|
2014-10-27 18:43:09 -04:00
|
|
|
if( !jit )
|
|
|
|
return;
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
for (unsigned int i = 0; i < m_scriptFunctions.GetLength(); i++)
|
|
|
|
m_scriptFunctions[i]->JITCompile();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
int asCModule::Build()
|
|
|
|
{
|
|
|
|
#ifdef AS_NO_COMPILER
|
|
|
|
return asNOT_SUPPORTED;
|
|
|
|
#else
|
|
|
|
TimeIt("asCModule::Build");
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Don't allow the module to be rebuilt if there are still
|
2015-05-11 19:40:43 -04:00
|
|
|
// external references that will need the previous code
|
2016-02-17 23:07:55 -05:00
|
|
|
// TODO: interface: The asIScriptModule must have a method for querying if the module is used
|
2015-05-11 19:40:43 -04:00
|
|
|
if( HasExternalReferences(false) )
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_MODULE_IS_IN_USE);
|
2015-05-11 19:40:43 -04:00
|
|
|
return asMODULE_IS_IN_USE;
|
|
|
|
}
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Only one thread may build at one time
|
|
|
|
// TODO: It should be possible to have multiple threads perform compilations
|
2021-11-04 16:43:10 -04:00
|
|
|
int r = m_engine->RequestBuild();
|
2014-10-27 18:43:09 -04:00
|
|
|
if( r < 0 )
|
|
|
|
return r;
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->PrepareEngine();
|
|
|
|
if( m_engine->configFailed )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_INVALID_CONFIGURATION);
|
|
|
|
m_engine->BuildCompleted();
|
2014-10-27 18:43:09 -04:00
|
|
|
return asINVALID_CONFIGURATION;
|
|
|
|
}
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
InternalReset();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
if( !m_builder )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->BuildCompleted();
|
2014-10-27 18:43:09 -04:00
|
|
|
return asSUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the script
|
2021-11-04 16:43:10 -04:00
|
|
|
r = m_builder->Build();
|
|
|
|
asDELETE(m_builder,asCBuilder);
|
|
|
|
m_builder = 0;
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
if( r < 0 )
|
|
|
|
{
|
|
|
|
// Reset module again
|
|
|
|
InternalReset();
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->BuildCompleted();
|
2014-10-27 18:43:09 -04:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
JITCompile();
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->PrepareEngine();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
#ifdef AS_DEBUG
|
|
|
|
// Verify that there are no unwanted gaps in the scriptFunctions array.
|
2021-11-04 16:43:10 -04:00
|
|
|
for( asUINT n = 1; n < m_engine->scriptFunctions.GetLength(); n++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
int id = n;
|
2021-11-04 16:43:10 -04:00
|
|
|
if( m_engine->scriptFunctions[n] == 0 && !m_engine->freeScriptFunctionIds.Exists(id) )
|
2014-10-27 18:43:09 -04:00
|
|
|
asASSERT( false );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->BuildCompleted();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Initialize global variables
|
2021-11-04 16:43:10 -04:00
|
|
|
if( r >= 0 && m_engine->ep.initGlobalVarsAfterBuild )
|
2014-10-27 18:43:09 -04:00
|
|
|
r = ResetGlobalVars(0);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
int asCModule::ResetGlobalVars(asIScriptContext *ctx)
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
if( m_isGlobalVarInitialized )
|
2014-10-27 18:43:09 -04:00
|
|
|
CallExit();
|
|
|
|
|
|
|
|
return CallInit(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
asIScriptFunction *asCModule::GetFunctionByIndex(asUINT index) const
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
return const_cast<asCScriptFunction*>(m_globalFunctions.Get(index));
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// internal
|
|
|
|
int asCModule::CallInit(asIScriptContext *myCtx)
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
if( m_isGlobalVarInitialized )
|
2014-10-27 18:43:09 -04:00
|
|
|
return asERROR;
|
|
|
|
|
|
|
|
// Each global variable needs to be cleared individually
|
2021-11-04 16:43:10 -04:00
|
|
|
asCSymbolTableIterator<asCGlobalProperty> it = m_scriptGlobals.List();
|
2014-10-27 18:43:09 -04:00
|
|
|
while( it )
|
|
|
|
{
|
|
|
|
asCGlobalProperty *desc = *it;
|
|
|
|
memset(desc->GetAddressOfValue(), 0, sizeof(asDWORD)*desc->type.GetSizeOnStackDWords());
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the init function for each of the global variables
|
|
|
|
asIScriptContext *ctx = myCtx;
|
|
|
|
int r = asEXECUTION_FINISHED;
|
2021-11-04 16:43:10 -04:00
|
|
|
it = m_scriptGlobals.List();
|
2014-10-27 18:43:09 -04:00
|
|
|
while( it && r == asEXECUTION_FINISHED )
|
|
|
|
{
|
|
|
|
asCGlobalProperty *desc = *it;
|
|
|
|
it++;
|
|
|
|
if( desc->GetInitFunc() )
|
|
|
|
{
|
|
|
|
if( ctx == 0 )
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
ctx = m_engine->RequestContext();
|
2015-05-11 19:40:43 -04:00
|
|
|
if( ctx == 0 )
|
2014-10-27 18:43:09 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
r = InitGlobalProp(desc, ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ctx && !myCtx )
|
|
|
|
{
|
|
|
|
m_engine->ReturnContext(ctx);
|
|
|
|
ctx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Even if the initialization failed we need to set the
|
|
|
|
// flag that the variables have been initialized, otherwise
|
|
|
|
// the module won't free those variables that really were
|
|
|
|
// initialized.
|
|
|
|
m_isGlobalVarInitialized = true;
|
|
|
|
|
|
|
|
if( r != asEXECUTION_FINISHED )
|
|
|
|
return asINIT_GLOBAL_VARS_FAILED;
|
|
|
|
|
|
|
|
return asSUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal
|
|
|
|
// This function assumes the memory for the global property is already cleared
|
|
|
|
int asCModule::InitGlobalProp(asCGlobalProperty *prop, asIScriptContext *myCtx)
|
|
|
|
{
|
|
|
|
// Call the init function for each of the global variables
|
|
|
|
asIScriptContext *ctx = myCtx;
|
|
|
|
int r = asEXECUTION_FINISHED;
|
|
|
|
if( prop->GetInitFunc() )
|
|
|
|
{
|
|
|
|
if( ctx == 0 )
|
|
|
|
{
|
|
|
|
ctx = m_engine->RequestContext();
|
|
|
|
if( ctx == 0 )
|
|
|
|
return asERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = ctx->Prepare(prop->GetInitFunc());
|
|
|
|
if( r >= 0 )
|
|
|
|
{
|
|
|
|
r = ctx->Execute();
|
|
|
|
if( r != asEXECUTION_FINISHED )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asCString msg;
|
|
|
|
msg.Format(TXT_FAILED_TO_INITIALIZE_s, prop->name.AddressOf());
|
|
|
|
asCScriptFunction *func = prop->GetInitFunc();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->WriteMessage(func->scriptData->scriptSectionIdx >= 0 ? m_engine->scriptSectionNames[func->scriptData->scriptSectionIdx]->AddressOf() : "",
|
|
|
|
func->GetLineNumber(0, 0) & 0xFFFFF,
|
|
|
|
func->GetLineNumber(0, 0) >> 20,
|
|
|
|
asMSGTYPE_ERROR,
|
|
|
|
msg.AddressOf());
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
if( r == asEXECUTION_EXCEPTION )
|
|
|
|
{
|
|
|
|
const asIScriptFunction *function = ctx->GetExceptionFunction();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
msg.Format(TXT_EXCEPTION_s_IN_s, ctx->GetExceptionString(), function->GetDeclaration());
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->WriteMessage(function->GetScriptSectionName(),
|
|
|
|
ctx->GetExceptionLineNumber(),
|
|
|
|
0,
|
|
|
|
asMSGTYPE_INFORMATION,
|
|
|
|
msg.AddressOf());
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ctx && !myCtx )
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->ReturnContext(ctx);
|
2014-10-27 18:43:09 -04:00
|
|
|
ctx = 0;
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Even if the initialization failed we need to set the
|
2014-10-27 18:43:09 -04:00
|
|
|
// flag that the variables have been initialized, otherwise
|
2017-11-20 08:02:33 -05:00
|
|
|
// the module won't free those variables that really were
|
2014-10-27 18:43:09 -04:00
|
|
|
// initialized.
|
2021-11-04 16:43:10 -04:00
|
|
|
m_isGlobalVarInitialized = true;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
if( r != asEXECUTION_FINISHED )
|
|
|
|
return asINIT_GLOBAL_VARS_FAILED;
|
|
|
|
|
|
|
|
return asSUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal
|
2017-11-20 08:02:33 -05:00
|
|
|
void asCModule::UninitializeGlobalProp(asCGlobalProperty *prop)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
if (prop == 0)
|
|
|
|
return;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if (prop->type.IsObject())
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
void **obj = (void**)prop->GetAddressOfValue();
|
|
|
|
if (*obj)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *ot = CastToObjectType(prop->type.GetTypeInfo());
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if (ot->flags & asOBJ_REF)
|
|
|
|
{
|
|
|
|
asASSERT((ot->flags & asOBJ_NOCOUNT) || ot->beh.release);
|
|
|
|
if (ot->beh.release)
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->CallObjectMethod(*obj, ot->beh.release);
|
2017-11-20 08:02:33 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ot->beh.destruct)
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->CallObjectMethod(*obj, ot->beh.destruct);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->CallFree(*obj);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
|
|
|
|
// Set the address to 0 as someone might try to access the variable afterwards
|
|
|
|
*obj = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (prop->type.IsFuncdef())
|
|
|
|
{
|
|
|
|
asCScriptFunction **func = (asCScriptFunction**)prop->GetAddressOfValue();
|
|
|
|
if (*func)
|
|
|
|
{
|
|
|
|
(*func)->Release();
|
|
|
|
*func = 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal
|
|
|
|
void asCModule::CallExit()
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
if( !m_isGlobalVarInitialized ) return;
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
asCSymbolTableIterator<asCGlobalProperty> it = m_scriptGlobals.List();
|
2017-11-20 08:02:33 -05:00
|
|
|
while( it )
|
|
|
|
{
|
|
|
|
UninitializeGlobalProp(*it);
|
2014-10-27 18:43:09 -04:00
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
m_isGlobalVarInitialized = false;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
// internal
|
|
|
|
bool asCModule::HasExternalReferences(bool shuttingDown)
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
// Check all entities in the module for any external references.
|
2015-05-11 19:40:43 -04:00
|
|
|
// If there are any external references the module cannot be deleted yet.
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
asCSymbolTableIterator<asCGlobalProperty> it = m_scriptGlobals.List();
|
2017-11-20 08:02:33 -05:00
|
|
|
while (it)
|
|
|
|
{
|
|
|
|
asCGlobalProperty *desc = *it;
|
|
|
|
if (desc->GetInitFunc() && desc->GetInitFunc()->externalRefCount.get())
|
|
|
|
{
|
|
|
|
if( !shuttingDown )
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asCString msg;
|
2021-11-04 16:43:10 -04:00
|
|
|
msg.Format(TXT_EXTRNL_REF_TO_MODULE_s, m_name.AddressOf());
|
|
|
|
m_engine->WriteMessage("", 0, 0, asMSGTYPE_WARNING, msg.AddressOf());
|
2017-11-20 08:02:33 -05:00
|
|
|
|
|
|
|
// TODO: Use a better error message
|
|
|
|
asCString tmpName = "init " + desc->name;
|
|
|
|
msg.Format(TXT_PREV_FUNC_IS_NAMED_s_TYPE_IS_d, tmpName.AddressOf(), desc->GetInitFunc()->GetFuncType());
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->WriteMessage("", 0, 0, asMSGTYPE_INFORMATION, msg.AddressOf());
|
2017-11-20 08:02:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
for (asUINT n = 0; n < m_scriptFunctions.GetLength(); n++)
|
|
|
|
{
|
|
|
|
asCScriptFunction *func = m_scriptFunctions[n];
|
|
|
|
if (func && func->externalRefCount.get())
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
// If the func is shared and can be moved to another module then this is not a reason to keep the module alive
|
|
|
|
if (func->IsShared() && m_engine->FindNewOwnerForSharedFunc(func, this) != this)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!shuttingDown)
|
2015-05-11 19:40:43 -04:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asCString msg;
|
2021-11-04 16:43:10 -04:00
|
|
|
msg.Format(TXT_EXTRNL_REF_TO_MODULE_s, m_name.AddressOf());
|
|
|
|
m_engine->WriteMessage("", 0, 0, asMSGTYPE_WARNING, msg.AddressOf());
|
2015-05-11 19:40:43 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
msg.Format(TXT_PREV_FUNC_IS_NAMED_s_TYPE_IS_d, m_scriptFunctions[n]->GetName(), m_scriptFunctions[n]->GetFuncType());
|
|
|
|
m_engine->WriteMessage("", 0, 0, asMSGTYPE_INFORMATION, msg.AddressOf());
|
2015-05-11 19:40:43 -04:00
|
|
|
}
|
|
|
|
}
|
2021-11-04 16:43:10 -04:00
|
|
|
}
|
2015-05-11 19:40:43 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
for (asUINT n = 0; n < m_classTypes.GetLength(); n++)
|
|
|
|
{
|
|
|
|
asCObjectType *obj = m_classTypes[n];
|
|
|
|
if (obj && obj->externalRefCount.get())
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
// If the obj is shared and can be moved to another module then this is not a reason to keep the module alive
|
|
|
|
if (obj->IsShared() && m_engine->FindNewOwnerForSharedType(obj, this) != this)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!shuttingDown)
|
2015-05-11 19:40:43 -04:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asCString msg;
|
2021-11-04 16:43:10 -04:00
|
|
|
msg.Format(TXT_EXTRNL_REF_TO_MODULE_s, m_name.AddressOf());
|
|
|
|
m_engine->WriteMessage("", 0, 0, asMSGTYPE_WARNING, msg.AddressOf());
|
2015-05-11 19:40:43 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
msg.Format(TXT_PREV_TYPE_IS_NAMED_s, m_classTypes[n]->GetName());
|
|
|
|
m_engine->WriteMessage("", 0, 0, asMSGTYPE_INFORMATION, msg.AddressOf());
|
2015-05-11 19:40:43 -04:00
|
|
|
}
|
|
|
|
}
|
2021-11-04 16:43:10 -04:00
|
|
|
}
|
2015-05-11 19:40:43 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
for (asUINT n = 0; n < m_funcDefs.GetLength(); n++)
|
|
|
|
{
|
|
|
|
asCFuncdefType *func = m_funcDefs[n];
|
|
|
|
if (func && func->externalRefCount.get())
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
// If the funcdef is shared and can be moved to another module then this is not a reason to keep the module alive
|
|
|
|
if (func->IsShared() && m_engine->FindNewOwnerForSharedType(func, this) != this)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!shuttingDown)
|
2015-05-11 19:40:43 -04:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asCString msg;
|
2021-11-04 16:43:10 -04:00
|
|
|
msg.Format(TXT_EXTRNL_REF_TO_MODULE_s, m_name.AddressOf());
|
|
|
|
m_engine->WriteMessage("", 0, 0, asMSGTYPE_WARNING, msg.AddressOf());
|
2015-05-11 19:40:43 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
msg.Format(TXT_PREV_FUNC_IS_NAMED_s_TYPE_IS_d, m_funcDefs[n]->GetName(), m_funcDefs[n]->funcdef->GetFuncType());
|
|
|
|
m_engine->WriteMessage("", 0, 0, asMSGTYPE_INFORMATION, msg.AddressOf());
|
2015-05-11 19:40:43 -04:00
|
|
|
}
|
|
|
|
}
|
2021-11-04 16:43:10 -04:00
|
|
|
}
|
2015-05-11 19:40:43 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
for (asUINT n = 0; n < m_templateInstances.GetLength(); n++)
|
|
|
|
{
|
|
|
|
asCObjectType *obj = m_templateInstances[n];
|
|
|
|
if (obj && obj->externalRefCount.get())
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
// If the template can be moved to another module then this is not a reason to keep the module alive
|
|
|
|
if (obj->IsShared() && m_engine->FindNewOwnerForSharedType(obj, this) != this)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!shuttingDown)
|
2015-05-11 19:40:43 -04:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asCString msg;
|
2021-11-04 16:43:10 -04:00
|
|
|
msg.Format(TXT_EXTRNL_REF_TO_MODULE_s, m_name.AddressOf());
|
|
|
|
m_engine->WriteMessage("", 0, 0, asMSGTYPE_WARNING, msg.AddressOf());
|
2015-05-11 19:40:43 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
msg.Format(TXT_PREV_TYPE_IS_NAMED_s, m_templateInstances[n]->GetName());
|
|
|
|
m_engine->WriteMessage("", 0, 0, asMSGTYPE_INFORMATION, msg.AddressOf());
|
2015-05-11 19:40:43 -04:00
|
|
|
}
|
|
|
|
}
|
2021-11-04 16:43:10 -04:00
|
|
|
}
|
2015-05-11 19:40:43 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// internal
|
|
|
|
void asCModule::InternalReset()
|
|
|
|
{
|
|
|
|
CallExit();
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
asUINT n;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
// Remove all global functions
|
2021-11-04 16:43:10 -04:00
|
|
|
m_globalFunctions.Clear();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Destroy the internals of the global properties here, but do not yet remove them from the
|
2015-05-11 19:40:43 -04:00
|
|
|
// engine, because functions need the engine's varAddressMap to get to the property. If the
|
|
|
|
// property is removed already, it may leak as the refCount doesn't reach 0.
|
2021-11-04 16:43:10 -04:00
|
|
|
asCSymbolTableIterator<asCGlobalProperty> globIt = m_scriptGlobals.List();
|
2014-10-27 18:43:09 -04:00
|
|
|
while( globIt )
|
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
(*globIt)->DestroyInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
globIt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnbindAllImportedFunctions();
|
|
|
|
|
|
|
|
// Free bind information
|
2021-11-04 16:43:10 -04:00
|
|
|
for( n = 0; n < m_bindInformations.GetLength(); n++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
if( m_bindInformations[n] )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_bindInformations[n]->importedFunctionSignature->ReleaseInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
asDELETE(m_bindInformations[n], sBindInfo);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
2021-11-04 16:43:10 -04:00
|
|
|
m_bindInformations.SetLength(0);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Free declared types, including classes, typedefs, and enums
|
2021-11-04 16:43:10 -04:00
|
|
|
for( n = 0; n < m_templateInstances.GetLength(); n++ )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asCObjectType *type = m_templateInstances[n];
|
|
|
|
if( m_engine->FindNewOwnerForSharedType(type, this) != this )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
|
|
|
// The type is owned by another module, just release our reference
|
|
|
|
type->ReleaseInternal();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Orphan the template instance
|
|
|
|
type->module = 0;
|
|
|
|
|
|
|
|
// No other module is holding the template type
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->RemoveTemplateInstanceType(type);
|
2015-05-11 19:40:43 -04:00
|
|
|
type->ReleaseInternal();
|
|
|
|
}
|
2021-11-04 16:43:10 -04:00
|
|
|
m_templateInstances.SetLength(0);
|
|
|
|
for( n = 0; n < m_classTypes.GetLength(); n++ )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asCObjectType *type = m_classTypes[n];
|
2015-05-11 19:40:43 -04:00
|
|
|
if( type->IsShared() )
|
|
|
|
{
|
|
|
|
// The type is shared, so transfer ownership to another module that also uses it
|
2021-11-04 16:43:10 -04:00
|
|
|
if( m_engine->FindNewOwnerForSharedType(type, this) != this )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
|
|
|
// The type is owned by another module, just release our reference
|
|
|
|
type->ReleaseInternal();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The type should be destroyed now
|
|
|
|
type->DestroyInternal();
|
|
|
|
|
|
|
|
// Remove the type from the engine
|
|
|
|
if( type->IsShared() )
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->sharedScriptTypes.RemoveValue(type);
|
2015-05-11 19:40:43 -04:00
|
|
|
type->ReleaseInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release it from the module
|
|
|
|
type->module = 0;
|
|
|
|
type->ReleaseInternal();
|
|
|
|
}
|
2021-11-04 16:43:10 -04:00
|
|
|
m_classTypes.SetLength(0);
|
|
|
|
for( n = 0; n < m_enumTypes.GetLength(); n++ )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asCEnumType *type = m_enumTypes[n];
|
2015-05-11 19:40:43 -04:00
|
|
|
if( type->IsShared() )
|
|
|
|
{
|
|
|
|
// The type is shared, so transfer ownership to another module that also uses it
|
2021-11-04 16:43:10 -04:00
|
|
|
if( m_engine->FindNewOwnerForSharedType(type, this) != this )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
|
|
|
// The type is owned by another module, just release our reference
|
|
|
|
type->ReleaseInternal();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the type from the engine
|
|
|
|
if( type->IsShared() )
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->sharedScriptTypes.RemoveValue(type);
|
2015-05-11 19:40:43 -04:00
|
|
|
type->ReleaseInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release it from the module
|
|
|
|
type->module = 0;
|
|
|
|
type->ReleaseInternal();
|
|
|
|
}
|
2021-11-04 16:43:10 -04:00
|
|
|
m_enumTypes.SetLength(0);
|
|
|
|
for( n = 0; n < m_typeDefs.GetLength(); n++ )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asCTypedefType *type = m_typeDefs[n];
|
2015-05-11 19:40:43 -04:00
|
|
|
|
|
|
|
// The type should be destroyed now
|
|
|
|
type->DestroyInternal();
|
|
|
|
|
|
|
|
// Release it from the module
|
|
|
|
type->module = 0;
|
|
|
|
type->ReleaseInternal();
|
|
|
|
}
|
2021-11-04 16:43:10 -04:00
|
|
|
m_typeDefs.SetLength(0);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Free funcdefs
|
2021-11-04 16:43:10 -04:00
|
|
|
for( n = 0; n < m_funcDefs.GetLength(); n++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asCFuncdefType *func = m_funcDefs[n];
|
2017-11-20 08:02:33 -05:00
|
|
|
asASSERT(func);
|
|
|
|
if( func->funcdef && func->funcdef->IsShared() )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// The funcdef is shared, so transfer ownership to another module that also uses it
|
2021-11-04 16:43:10 -04:00
|
|
|
if( m_engine->FindNewOwnerForSharedType(func, this) != this )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// The funcdef is owned by another module, just release our reference
|
2015-05-11 19:40:43 -04:00
|
|
|
func->ReleaseInternal();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func->DestroyInternal();
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->RemoveFuncdef(func);
|
2015-05-11 19:40:43 -04:00
|
|
|
func->module = 0;
|
|
|
|
func->ReleaseInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
2021-11-04 16:43:10 -04:00
|
|
|
m_funcDefs.SetLength(0);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
// Then release the functions
|
2021-11-04 16:43:10 -04:00
|
|
|
for( n = 0; n < m_scriptFunctions.GetLength(); n++ )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asCScriptFunction *func = m_scriptFunctions[n];
|
2015-05-11 19:40:43 -04:00
|
|
|
if( func->IsShared() )
|
|
|
|
{
|
|
|
|
// The func is shared, so transfer ownership to another module that also uses it
|
2021-11-04 16:43:10 -04:00
|
|
|
if( m_engine->FindNewOwnerForSharedFunc(func, this) != this )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
|
|
|
// The func is owned by another module, just release our reference
|
|
|
|
func->ReleaseInternal();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func->DestroyInternal();
|
|
|
|
func->module = 0;
|
|
|
|
func->ReleaseInternal();
|
|
|
|
}
|
2021-11-04 16:43:10 -04:00
|
|
|
m_scriptFunctions.SetLength(0);
|
2015-05-11 19:40:43 -04:00
|
|
|
|
|
|
|
// Now remove and release the global properties as there are no more references to them
|
2021-11-04 16:43:10 -04:00
|
|
|
globIt = m_scriptGlobals.List();
|
2015-05-11 19:40:43 -04:00
|
|
|
while( globIt )
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->RemoveGlobalProperty(*globIt);
|
2015-05-11 19:40:43 -04:00
|
|
|
asASSERT( (*globIt)->refCount.get() == 1 );
|
|
|
|
(*globIt)->Release();
|
|
|
|
globIt++;
|
|
|
|
}
|
2021-11-04 16:43:10 -04:00
|
|
|
m_scriptGlobals.Clear();
|
|
|
|
|
|
|
|
// Clear the type lookup
|
|
|
|
// The references were already released as the types were removed from the respective arrays
|
|
|
|
m_typeLookup.EraseAll();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
asASSERT( IsEmpty() );
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
2017-11-20 08:02:33 -05:00
|
|
|
asIScriptFunction *asCModule::GetFunctionByName(const char *in_name) const
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asCString name;
|
|
|
|
asSNameSpace *ns = 0;
|
|
|
|
if( m_engine->DetermineNameAndNamespace(in_name, m_defaultNamespace, name, ns) < 0 )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Search recursively in the given namespace, moving up to parent namespace until the function is found
|
2015-05-11 19:40:43 -04:00
|
|
|
while( ns )
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
const asCArray<unsigned int> &idxs = m_globalFunctions.GetIndexes(ns, name);
|
2015-05-11 19:40:43 -04:00
|
|
|
if( idxs.GetLength() != 1 )
|
|
|
|
return 0;
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
const asIScriptFunction *func = m_globalFunctions.Get(idxs[0]);
|
2015-05-11 19:40:43 -04:00
|
|
|
if( func )
|
|
|
|
return const_cast<asIScriptFunction*>(func);
|
|
|
|
|
|
|
|
// Recursively search parent namespaces
|
2021-11-04 16:43:10 -04:00
|
|
|
ns = m_engine->GetParentNameSpace(ns);
|
2015-05-11 19:40:43 -04:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
return 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
asUINT asCModule::GetImportedFunctionCount() const
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
return (asUINT)m_bindInformations.GetLength();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
int asCModule::GetImportedFunctionIndexByDecl(const char *decl) const
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asCBuilder bld(m_engine, const_cast<asCModule*>(this));
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Don't write parser errors to the message callback
|
|
|
|
bld.silent = true;
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
asCScriptFunction func(m_engine, const_cast<asCModule*>(this), asFUNC_DUMMY);
|
|
|
|
bld.ParseFunctionDeclaration(0, decl, &func, false, 0, 0, m_defaultNamespace);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// TODO: optimize: Improve linear search
|
|
|
|
// Search script functions for matching interface
|
|
|
|
int id = -1;
|
2021-11-04 16:43:10 -04:00
|
|
|
for( asUINT n = 0; n < m_bindInformations.GetLength(); ++n )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
if( func.name == m_bindInformations[n]->importedFunctionSignature->name &&
|
|
|
|
func.returnType == m_bindInformations[n]->importedFunctionSignature->returnType &&
|
|
|
|
func.parameterTypes.GetLength() == m_bindInformations[n]->importedFunctionSignature->parameterTypes.GetLength() )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
bool match = true;
|
|
|
|
for( asUINT p = 0; p < func.parameterTypes.GetLength(); ++p )
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
if( func.parameterTypes[p] != m_bindInformations[n]->importedFunctionSignature->parameterTypes[p] )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( match )
|
|
|
|
{
|
|
|
|
if( id == -1 )
|
|
|
|
id = n;
|
|
|
|
else
|
|
|
|
return asMULTIPLE_FUNCTIONS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( id == -1 ) return asNO_FUNCTION;
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
asUINT asCModule::GetFunctionCount() const
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
return (asUINT)m_globalFunctions.GetSize();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
asIScriptFunction *asCModule::GetFunctionByDecl(const char *decl) const
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asCBuilder bld(m_engine, const_cast<asCModule*>(this));
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Don't write parser errors to the message callback
|
|
|
|
bld.silent = true;
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
asCScriptFunction func(m_engine, const_cast<asCModule*>(this), asFUNC_DUMMY);
|
|
|
|
int r = bld.ParseFunctionDeclaration(0, decl, &func, false, 0, 0, m_defaultNamespace);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( r < 0 )
|
|
|
|
{
|
|
|
|
// Invalid declaration
|
|
|
|
// TODO: Write error to message stream
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the defaultNamespace implicitly unless an explicit namespace has been provided
|
2021-11-04 16:43:10 -04:00
|
|
|
asSNameSpace *ns = func.nameSpace == m_engine->nameSpaces[0] ? m_defaultNamespace : func.nameSpace;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Search script functions for matching interface
|
2015-05-11 19:40:43 -04:00
|
|
|
while( ns )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
asIScriptFunction *f = 0;
|
2021-11-04 16:43:10 -04:00
|
|
|
const asCArray<unsigned int> &idxs = m_globalFunctions.GetIndexes(ns, func.name);
|
2015-05-11 19:40:43 -04:00
|
|
|
for( unsigned int n = 0; n < idxs.GetLength(); n++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
const asCScriptFunction *funcPtr = m_globalFunctions.Get(idxs[n]);
|
2015-05-11 19:40:43 -04:00
|
|
|
if( funcPtr->objectType == 0 &&
|
|
|
|
func.returnType == funcPtr->returnType &&
|
|
|
|
func.parameterTypes.GetLength() == funcPtr->parameterTypes.GetLength()
|
|
|
|
)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
bool match = true;
|
|
|
|
for( asUINT p = 0; p < func.parameterTypes.GetLength(); ++p )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
if( func.parameterTypes[p] != funcPtr->parameterTypes[p] )
|
|
|
|
{
|
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
if( match )
|
|
|
|
{
|
|
|
|
if( f == 0 )
|
|
|
|
f = const_cast<asCScriptFunction*>(funcPtr);
|
|
|
|
else
|
|
|
|
// Multiple functions
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-11 19:40:43 -04:00
|
|
|
|
|
|
|
if( f )
|
|
|
|
return f;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Search for matching functions in the parent namespace
|
2021-11-04 16:43:10 -04:00
|
|
|
ns = m_engine->GetParentNameSpace(ns);
|
2015-05-11 19:40:43 -04:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
return 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
asUINT asCModule::GetGlobalVarCount() const
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
return (asUINT)m_scriptGlobals.GetSize();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
2017-11-20 08:02:33 -05:00
|
|
|
int asCModule::GetGlobalVarIndexByName(const char *in_name) const
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asCString name;
|
|
|
|
asSNameSpace *ns = 0;
|
|
|
|
if( m_engine->DetermineNameAndNamespace(in_name, m_defaultNamespace, name, ns) < 0 )
|
|
|
|
return asINVALID_ARG;
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Find the global var id
|
2015-05-11 19:40:43 -04:00
|
|
|
while( ns )
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
int id = m_scriptGlobals.GetFirstIndex(ns, name);
|
2015-05-11 19:40:43 -04:00
|
|
|
if( id >= 0 ) return id;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
// Recursively search parent namespaces
|
2021-11-04 16:43:10 -04:00
|
|
|
ns = m_engine->GetParentNameSpace(ns);
|
2015-05-11 19:40:43 -04:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
return asNO_GLOBAL_VAR;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
int asCModule::RemoveGlobalVar(asUINT index)
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asCGlobalProperty *prop = m_scriptGlobals.Get(index);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( !prop )
|
|
|
|
return asINVALID_ARG;
|
2015-05-11 19:40:43 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// If the global variables have already been initialized
|
|
|
|
// then uninitialize the variable before it is removed
|
2021-11-04 16:43:10 -04:00
|
|
|
if (m_isGlobalVarInitialized)
|
2017-11-20 08:02:33 -05:00
|
|
|
UninitializeGlobalProp(prop);
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
// Destroy the internal of the global variable (removes the initialization function)
|
|
|
|
prop->DestroyInternal();
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Check if the module is the only one referring to the property, if so remove it from the engine too
|
2015-05-11 19:40:43 -04:00
|
|
|
// If the property is not removed now, it will be removed later when the module is discarded
|
|
|
|
if( prop->refCount.get() == 2 )
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->RemoveGlobalProperty(prop);
|
2015-05-11 19:40:43 -04:00
|
|
|
|
|
|
|
// Remove the global variable from the module
|
2021-11-04 16:43:10 -04:00
|
|
|
m_scriptGlobals.Erase(index);
|
2017-11-20 08:02:33 -05:00
|
|
|
prop->Release();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
int asCModule::GetGlobalVarIndexByDecl(const char *decl) const
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asCBuilder bld(m_engine, const_cast<asCModule*>(this));
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Don't write parser errors to the message callback
|
|
|
|
bld.silent = true;
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
asCString declName;
|
2014-10-27 18:43:09 -04:00
|
|
|
asSNameSpace *nameSpace;
|
|
|
|
asCDataType dt;
|
2021-11-04 16:43:10 -04:00
|
|
|
int r = bld.ParseVariableDeclaration(decl, m_defaultNamespace, declName, nameSpace, dt);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( r < 0 )
|
|
|
|
return r;
|
|
|
|
|
|
|
|
// Search global variables for a match
|
2015-05-11 19:40:43 -04:00
|
|
|
while( nameSpace )
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
int id = m_scriptGlobals.GetFirstIndex(nameSpace, declName, asCCompGlobPropType(dt));
|
2015-05-11 19:40:43 -04:00
|
|
|
if( id != -1 )
|
|
|
|
return id;
|
|
|
|
|
|
|
|
// Recursively search parent namespace
|
2021-11-04 16:43:10 -04:00
|
|
|
nameSpace = m_engine->GetParentNameSpace(nameSpace);
|
2015-05-11 19:40:43 -04:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
return asNO_GLOBAL_VAR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
void *asCModule::GetAddressOfGlobalVar(asUINT index)
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asCGlobalProperty *prop = m_scriptGlobals.Get(index);
|
2017-11-20 08:02:33 -05:00
|
|
|
if( !prop )
|
2014-10-27 18:43:09 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// For object variables it's necessary to dereference the pointer to get the address of the value
|
|
|
|
if( prop->type.IsObject() &&
|
|
|
|
!prop->type.IsObjectHandle() )
|
|
|
|
return *(void**)(prop->GetAddressOfValue());
|
|
|
|
|
|
|
|
return (void*)(prop->GetAddressOfValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
const char *asCModule::GetGlobalVarDeclaration(asUINT index, bool includeNamespace) const
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
const asCGlobalProperty *prop = m_scriptGlobals.Get(index);
|
2014-10-27 18:43:09 -04:00
|
|
|
if (!prop) return 0;
|
|
|
|
|
|
|
|
asCString *tempString = &asCThreadManager::GetLocalData()->string;
|
2021-11-04 16:43:10 -04:00
|
|
|
*tempString = prop->type.Format(m_defaultNamespace);
|
2014-10-27 18:43:09 -04:00
|
|
|
*tempString += " ";
|
2015-05-11 19:40:43 -04:00
|
|
|
if( includeNamespace && prop->nameSpace->name != "" )
|
2014-10-27 18:43:09 -04:00
|
|
|
*tempString += prop->nameSpace->name + "::";
|
|
|
|
*tempString += prop->name;
|
|
|
|
|
|
|
|
return tempString->AddressOf();
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
2017-11-20 08:02:33 -05:00
|
|
|
int asCModule::GetGlobalVar(asUINT index, const char **out_name, const char **out_nameSpace, int *out_typeId, bool *out_isConst) const
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
const asCGlobalProperty *prop = m_scriptGlobals.Get(index);
|
|
|
|
if (!prop) return asINVALID_ARG;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if( out_name )
|
|
|
|
*out_name = prop->name.AddressOf();
|
|
|
|
if( out_nameSpace )
|
|
|
|
*out_nameSpace = prop->nameSpace->name.AddressOf();
|
|
|
|
if( out_typeId )
|
2021-11-04 16:43:10 -04:00
|
|
|
*out_typeId = m_engine->GetTypeIdFromDataType(prop->type);
|
2017-11-20 08:02:33 -05:00
|
|
|
if( out_isConst )
|
|
|
|
*out_isConst = prop->type.IsReadOnly();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
return asSUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
asUINT asCModule::GetObjectTypeCount() const
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
return (asUINT)m_classTypes.GetLength();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// interface
|
|
|
|
asITypeInfo *asCModule::GetObjectTypeByIndex(asUINT index) const
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
if( index >= m_classTypes.GetLength() )
|
2014-10-27 18:43:09 -04:00
|
|
|
return 0;
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
return m_classTypes[index];
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
2017-11-20 08:02:33 -05:00
|
|
|
asITypeInfo *asCModule::GetTypeInfoByName(const char *in_name) const
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asCString name;
|
|
|
|
asSNameSpace *ns = 0;
|
|
|
|
if( m_engine->DetermineNameAndNamespace(in_name, m_defaultNamespace, name, ns) < 0 )
|
|
|
|
return 0;
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
while (ns)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asITypeInfo* info = GetType(name, ns);
|
|
|
|
if(info)
|
2017-11-20 08:02:33 -05:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
return info;
|
2017-11-20 08:02:33 -05:00
|
|
|
}
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
// Recursively search parent namespace
|
2021-11-04 16:43:10 -04:00
|
|
|
ns = m_engine->GetParentNameSpace(ns);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
int asCModule::GetTypeIdByDecl(const char *decl) const
|
|
|
|
{
|
|
|
|
asCDataType dt;
|
|
|
|
|
|
|
|
// This const cast is safe since we know the engine won't be modified
|
2021-11-04 16:43:10 -04:00
|
|
|
asCBuilder bld(m_engine, const_cast<asCModule*>(this));
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Don't write parser errors to the message callback
|
|
|
|
bld.silent = true;
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
int r = bld.ParseDataType(decl, &dt, m_defaultNamespace);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( r < 0 )
|
|
|
|
return asINVALID_TYPE;
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
return m_engine->GetTypeIdFromDataType(dt);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
// interface
|
2017-11-20 08:02:33 -05:00
|
|
|
asITypeInfo *asCModule::GetTypeInfoByDecl(const char *decl) const
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
|
|
|
asCDataType dt;
|
|
|
|
|
|
|
|
// This const cast is safe since we know the engine won't be modified
|
2021-11-04 16:43:10 -04:00
|
|
|
asCBuilder bld(m_engine, const_cast<asCModule*>(this));
|
2015-05-11 19:40:43 -04:00
|
|
|
|
|
|
|
// Don't write parser errors to the message callback
|
|
|
|
bld.silent = true;
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
int r = bld.ParseDataType(decl, &dt, m_defaultNamespace);
|
2017-11-20 08:02:33 -05:00
|
|
|
if (r < 0)
|
2015-05-11 19:40:43 -04:00
|
|
|
return 0;
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
return dt.GetTypeInfo();
|
2015-05-11 19:40:43 -04:00
|
|
|
}
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// interface
|
|
|
|
asUINT asCModule::GetEnumCount() const
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
return m_enumTypes.GetLength();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
2017-11-20 08:02:33 -05:00
|
|
|
asITypeInfo *asCModule::GetEnumByIndex(asUINT index) const
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
if( index >= m_enumTypes.GetLength() )
|
2014-10-27 18:43:09 -04:00
|
|
|
return 0;
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
return m_enumTypes[index];
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
asUINT asCModule::GetTypedefCount() const
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
return (asUINT)m_typeDefs.GetLength();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
2017-11-20 08:02:33 -05:00
|
|
|
asITypeInfo *asCModule::GetTypedefByIndex(asUINT index) const
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
if( index >= m_typeDefs.GetLength() )
|
2014-10-27 18:43:09 -04:00
|
|
|
return 0;
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
return m_typeDefs[index];
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// internal
|
|
|
|
int asCModule::GetNextImportedFunctionId()
|
|
|
|
{
|
|
|
|
// TODO: multithread: This will break if one thread if freeing a module, while another is being compiled
|
2021-11-04 16:43:10 -04:00
|
|
|
if( m_engine->freeImportedFunctionIdxs.GetLength() )
|
|
|
|
return FUNC_IMPORTED | (asUINT)m_engine->freeImportedFunctionIdxs[m_engine->freeImportedFunctionIdxs.GetLength()-1];
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
return FUNC_IMPORTED | (asUINT)m_engine->importedFunctions.GetLength();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef AS_NO_COMPILER
|
|
|
|
// internal
|
2017-11-20 08:02:33 -05:00
|
|
|
int asCModule::AddScriptFunction(int sectionIdx, int declaredAt, int id, const asCString &funcName, const asCDataType &returnType, const asCArray<asCDataType> ¶ms, const asCArray<asCString> ¶mNames, const asCArray<asETypeModifiers> &inOutFlags, const asCArray<asCString *> &defaultArgs, bool isInterface, asCObjectType *objType, bool isGlobalFunction, asSFunctionTraits funcTraits, asSNameSpace *ns)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
asASSERT(id >= 0);
|
|
|
|
|
|
|
|
// Store the function information
|
2021-11-04 16:43:10 -04:00
|
|
|
asCScriptFunction *func = asNEW(asCScriptFunction)(m_engine, this, isInterface ? asFUNC_INTERFACE : asFUNC_SCRIPT);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( func == 0 )
|
|
|
|
{
|
|
|
|
// Free the default args
|
|
|
|
for( asUINT n = 0; n < defaultArgs.GetLength(); n++ )
|
|
|
|
if( defaultArgs[n] )
|
|
|
|
asDELETE(defaultArgs[n], asCString);
|
|
|
|
|
|
|
|
return asOUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ns == 0 )
|
2021-11-04 16:43:10 -04:00
|
|
|
ns = m_engine->nameSpaces[0];
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// All methods of shared objects are also shared
|
|
|
|
if( objType && objType->IsShared() )
|
2017-11-20 08:02:33 -05:00
|
|
|
funcTraits.SetTrait(asTRAIT_SHARED, true);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
func->name = funcName;
|
2014-10-27 18:43:09 -04:00
|
|
|
func->nameSpace = ns;
|
|
|
|
func->id = id;
|
|
|
|
func->returnType = returnType;
|
|
|
|
if( func->funcType == asFUNC_SCRIPT )
|
|
|
|
{
|
|
|
|
func->scriptData->scriptSectionIdx = sectionIdx;
|
|
|
|
func->scriptData->declaredAt = declaredAt;
|
|
|
|
}
|
|
|
|
func->parameterTypes = params;
|
2015-05-11 19:40:43 -04:00
|
|
|
func->parameterNames = paramNames;
|
2014-10-27 18:43:09 -04:00
|
|
|
func->inOutFlags = inOutFlags;
|
|
|
|
func->defaultArgs = defaultArgs;
|
|
|
|
func->objectType = objType;
|
2015-05-11 19:40:43 -04:00
|
|
|
if( objType )
|
|
|
|
objType->AddRefInternal();
|
2017-11-20 08:02:33 -05:00
|
|
|
func->traits = funcTraits;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
asASSERT( params.GetLength() == inOutFlags.GetLength() && params.GetLength() == defaultArgs.GetLength() );
|
|
|
|
|
|
|
|
// Verify that we are not assigning either the final or override specifier(s) if we are registering a non-member function
|
2017-11-20 08:02:33 -05:00
|
|
|
asASSERT( !(!objType && funcTraits.GetTrait(asTRAIT_FINAL)) );
|
|
|
|
asASSERT( !(!objType && funcTraits.GetTrait(asTRAIT_OVERRIDE)) );
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
// The internal ref count was already set by the constructor
|
2021-11-04 16:43:10 -04:00
|
|
|
m_scriptFunctions.PushLast(func);
|
|
|
|
m_engine->AddScriptFunction(func);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Compute the signature id
|
|
|
|
if( objType )
|
|
|
|
func->ComputeSignatureId();
|
|
|
|
|
|
|
|
// Add reference
|
|
|
|
if( isGlobalFunction )
|
2021-11-04 16:43:10 -04:00
|
|
|
m_globalFunctions.Put(func);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// internal
|
2014-10-27 18:43:09 -04:00
|
|
|
int asCModule::AddScriptFunction(asCScriptFunction *func)
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_scriptFunctions.PushLast(func);
|
2015-05-11 19:40:43 -04:00
|
|
|
func->AddRefInternal();
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->AddScriptFunction(func);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2016-02-17 23:07:55 -05:00
|
|
|
// If the function that is being added is an already compiled shared function
|
|
|
|
// then it is necessary to look for anonymous functions that may be declared
|
|
|
|
// within it and add those as well
|
2017-11-20 08:02:33 -05:00
|
|
|
if( func->IsShared() && func->funcType == asFUNC_SCRIPT )
|
2016-02-17 23:07:55 -05:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// Loop through the byte code and check all the
|
2016-02-17 23:07:55 -05:00
|
|
|
// asBC_FuncPtr instructions for anonymous functions
|
|
|
|
asDWORD *bc = func->scriptData->byteCode.AddressOf();
|
|
|
|
asUINT bcLength = (asUINT)func->scriptData->byteCode.GetLength();
|
|
|
|
for( asUINT n = 0; n < bcLength; )
|
|
|
|
{
|
|
|
|
int c = *(asBYTE*)&bc[n];
|
|
|
|
if( c == asBC_FuncPtr )
|
|
|
|
{
|
|
|
|
asCScriptFunction *f = reinterpret_cast<asCScriptFunction*>(asBC_PTRARG(&bc[n]));
|
|
|
|
// Anonymous functions start with $
|
|
|
|
// There are never two equal anonymous functions so it is not necessary to look for duplicates
|
|
|
|
if( f && f->name[0] == '$' )
|
|
|
|
{
|
|
|
|
AddScriptFunction(f);
|
2021-11-04 16:43:10 -04:00
|
|
|
m_globalFunctions.Put(f);
|
2016-02-17 23:07:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
n += asBCTypeSize[asBCInfo[c].type];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal
|
2021-11-04 16:43:10 -04:00
|
|
|
int asCModule::AddImportedFunction(int id, const asCString &funcName, const asCDataType &returnType, const asCArray<asCDataType> ¶ms, const asCArray<asETypeModifiers> &inOutFlags, const asCArray<asCString *> &defaultArgs, asSFunctionTraits funcTraits, asSNameSpace *ns, const asCString &moduleName)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
asASSERT(id >= 0);
|
|
|
|
|
|
|
|
// Store the function information
|
2021-11-04 16:43:10 -04:00
|
|
|
asCScriptFunction *func = asNEW(asCScriptFunction)(m_engine, this, asFUNC_IMPORTED);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( func == 0 )
|
|
|
|
{
|
|
|
|
// Free the default args
|
|
|
|
for( asUINT n = 0; n < defaultArgs.GetLength(); n++ )
|
|
|
|
if( defaultArgs[n] )
|
|
|
|
asDELETE(defaultArgs[n], asCString);
|
|
|
|
|
|
|
|
return asOUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
func->name = funcName;
|
2014-10-27 18:43:09 -04:00
|
|
|
func->id = id;
|
|
|
|
func->returnType = returnType;
|
|
|
|
func->nameSpace = ns;
|
|
|
|
func->parameterTypes = params;
|
|
|
|
func->inOutFlags = inOutFlags;
|
|
|
|
func->defaultArgs = defaultArgs;
|
|
|
|
func->objectType = 0;
|
2021-11-04 16:43:10 -04:00
|
|
|
func->traits = funcTraits;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
sBindInfo *info = asNEW(sBindInfo);
|
|
|
|
if( info == 0 )
|
|
|
|
{
|
|
|
|
asDELETE(func, asCScriptFunction);
|
|
|
|
return asOUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->importedFunctionSignature = func;
|
|
|
|
info->boundFunctionId = -1;
|
|
|
|
info->importFromModule = moduleName;
|
2021-11-04 16:43:10 -04:00
|
|
|
m_bindInformations.PushLast(info);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Add the info to the array in the engine
|
2021-11-04 16:43:10 -04:00
|
|
|
if( m_engine->freeImportedFunctionIdxs.GetLength() )
|
|
|
|
m_engine->importedFunctions[m_engine->freeImportedFunctionIdxs.PopLast()] = info;
|
2014-10-27 18:43:09 -04:00
|
|
|
else
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->importedFunctions.PushLast(info);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// internal
|
|
|
|
asCScriptFunction *asCModule::GetImportedFunction(int index) const
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
return m_bindInformations[index]->importedFunctionSignature;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
int asCModule::BindImportedFunction(asUINT index, asIScriptFunction *func)
|
|
|
|
{
|
|
|
|
// First unbind the old function
|
|
|
|
int r = UnbindImportedFunction(index);
|
|
|
|
if( r < 0 ) return r;
|
|
|
|
|
|
|
|
// Must verify that the interfaces are equal
|
|
|
|
asCScriptFunction *dst = GetImportedFunction(index);
|
|
|
|
if( dst == 0 ) return asNO_FUNCTION;
|
|
|
|
|
|
|
|
if( func == 0 )
|
|
|
|
return asINVALID_ARG;
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
asCScriptFunction *src = m_engine->GetScriptFunction(func->GetId());
|
2017-11-20 08:02:33 -05:00
|
|
|
if( src == 0 )
|
2014-10-27 18:43:09 -04:00
|
|
|
return asNO_FUNCTION;
|
|
|
|
|
|
|
|
// Verify return type
|
|
|
|
if( dst->returnType != src->returnType )
|
|
|
|
return asINVALID_INTERFACE;
|
|
|
|
|
|
|
|
if( dst->parameterTypes.GetLength() != src->parameterTypes.GetLength() )
|
|
|
|
return asINVALID_INTERFACE;
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
for( asUINT n = 0; n < dst->parameterTypes.GetLength(); ++n )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
if( dst->parameterTypes[n] != src->parameterTypes[n] )
|
|
|
|
return asINVALID_INTERFACE;
|
|
|
|
}
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
m_bindInformations[index]->boundFunctionId = src->GetId();
|
2015-05-11 19:40:43 -04:00
|
|
|
src->AddRefInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
return asSUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
int asCModule::UnbindImportedFunction(asUINT index)
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
if( index >= m_bindInformations.GetLength() )
|
2014-10-27 18:43:09 -04:00
|
|
|
return asINVALID_ARG;
|
|
|
|
|
|
|
|
// Remove reference to old module
|
2021-11-04 16:43:10 -04:00
|
|
|
if( m_bindInformations[index] )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
int oldFuncID = m_bindInformations[index]->boundFunctionId;
|
2014-10-27 18:43:09 -04:00
|
|
|
if( oldFuncID != -1 )
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_bindInformations[index]->boundFunctionId = -1;
|
|
|
|
m_engine->scriptFunctions[oldFuncID]->ReleaseInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return asSUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
const char *asCModule::GetImportedFunctionDeclaration(asUINT index) const
|
|
|
|
{
|
|
|
|
asCScriptFunction *func = GetImportedFunction(index);
|
|
|
|
if( func == 0 ) return 0;
|
|
|
|
|
|
|
|
asCString *tempString = &asCThreadManager::GetLocalData()->string;
|
2021-11-04 16:43:10 -04:00
|
|
|
// TODO: Allow the application to decide if the parameter name should be included or not (requires change in the interface)
|
|
|
|
*tempString = func->GetDeclarationStr(true, true, false);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
return tempString->AddressOf();
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
const char *asCModule::GetImportedFunctionSourceModule(asUINT index) const
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
if( index >= m_bindInformations.GetLength() )
|
2014-10-27 18:43:09 -04:00
|
|
|
return 0;
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
return m_bindInformations[index]->importFromModule.AddressOf();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// inteface
|
|
|
|
int asCModule::BindAllImportedFunctions()
|
|
|
|
{
|
|
|
|
bool notAllFunctionsWereBound = false;
|
|
|
|
|
|
|
|
// Bind imported functions
|
|
|
|
int c = GetImportedFunctionCount();
|
|
|
|
for( int n = 0; n < c; ++n )
|
|
|
|
{
|
|
|
|
asCScriptFunction *importFunc = GetImportedFunction(n);
|
|
|
|
if( importFunc == 0 ) return asERROR;
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
asCString str = importFunc->GetDeclarationStr(false, true);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Get module name from where the function should be imported
|
|
|
|
const char *moduleName = GetImportedFunctionSourceModule(n);
|
|
|
|
if( moduleName == 0 ) return asERROR;
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
asCModule *srcMod = m_engine->GetModule(moduleName, false);
|
2014-10-27 18:43:09 -04:00
|
|
|
asIScriptFunction *func = 0;
|
|
|
|
if( srcMod )
|
|
|
|
func = srcMod->GetFunctionByDecl(str.AddressOf());
|
|
|
|
|
|
|
|
if( func == 0 )
|
|
|
|
notAllFunctionsWereBound = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( BindImportedFunction(n, func) < 0 )
|
|
|
|
notAllFunctionsWereBound = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( notAllFunctionsWereBound )
|
|
|
|
return asCANT_BIND_ALL_FUNCTIONS;
|
|
|
|
|
|
|
|
return asSUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
int asCModule::UnbindAllImportedFunctions()
|
|
|
|
{
|
|
|
|
asUINT c = GetImportedFunctionCount();
|
|
|
|
for( asUINT n = 0; n < c; ++n )
|
|
|
|
UnbindImportedFunction(n);
|
|
|
|
|
|
|
|
return asSUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal
|
2021-11-04 16:43:10 -04:00
|
|
|
void asCModule::AddClassType(asCObjectType* type)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_classTypes.PushLast(type);
|
|
|
|
m_typeLookup.Insert(asSNameSpaceNamePair(type->nameSpace, type->name), type);
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
// internal
|
|
|
|
void asCModule::AddEnumType(asCEnumType* type)
|
|
|
|
{
|
|
|
|
m_enumTypes.PushLast(type);
|
|
|
|
m_typeLookup.Insert(asSNameSpaceNamePair(type->nameSpace, type->name), type);
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
// internal
|
|
|
|
void asCModule::AddTypeDef(asCTypedefType* type)
|
|
|
|
{
|
|
|
|
m_typeDefs.PushLast(type);
|
|
|
|
m_typeLookup.Insert(asSNameSpaceNamePair(type->nameSpace, type->name), type);
|
2017-11-20 08:02:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// internal
|
2021-11-04 16:43:10 -04:00
|
|
|
void asCModule::AddFuncDef(asCFuncdefType* type)
|
2017-11-20 08:02:33 -05:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_funcDefs.PushLast(type);
|
|
|
|
m_typeLookup.Insert(asSNameSpaceNamePair(type->nameSpace, type->name), type);
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
// internal
|
|
|
|
void asCModule::ReplaceFuncDef(asCFuncdefType* type, asCFuncdefType* newType)
|
|
|
|
{
|
|
|
|
int i = m_funcDefs.IndexOf(type);
|
|
|
|
if( i >= 0 )
|
|
|
|
{
|
|
|
|
m_funcDefs[i] = newType;
|
|
|
|
|
|
|
|
// Replace it in the lookup map too
|
|
|
|
asSMapNode<asSNameSpaceNamePair, asCTypeInfo*>* result = 0;
|
|
|
|
if(m_typeLookup.MoveTo(&result, asSNameSpaceNamePair(type->nameSpace, type->name)))
|
|
|
|
{
|
|
|
|
asASSERT( result->value == type );
|
|
|
|
result->value = newType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal
|
|
|
|
asCTypeInfo *asCModule::GetType(const asCString &type, asSNameSpace *ns) const
|
|
|
|
{
|
|
|
|
asSMapNode<asSNameSpaceNamePair, asCTypeInfo*>* result = 0;
|
|
|
|
if(m_typeLookup.MoveTo(&result, asSNameSpaceNamePair(ns, type)))
|
|
|
|
{
|
|
|
|
return result->value;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
// internal
|
|
|
|
asCObjectType *asCModule::GetObjectType(const char *type, asSNameSpace *ns) const
|
|
|
|
{
|
|
|
|
asSMapNode<asSNameSpaceNamePair, asCTypeInfo*>* result = 0;
|
|
|
|
if(m_typeLookup.MoveTo(&result, asSNameSpaceNamePair(ns, type)))
|
|
|
|
{
|
|
|
|
return CastToObjectType(result->value);
|
|
|
|
}
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal
|
2017-11-20 08:02:33 -05:00
|
|
|
asCGlobalProperty *asCModule::AllocateGlobalProperty(const char *propName, const asCDataType &dt, asSNameSpace *ns)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asCGlobalProperty *prop = m_engine->AllocateGlobalProperty();
|
2017-11-20 08:02:33 -05:00
|
|
|
prop->name = propName;
|
2014-10-27 18:43:09 -04:00
|
|
|
prop->nameSpace = ns;
|
|
|
|
|
|
|
|
// Allocate the memory for this property based on its type
|
|
|
|
prop->type = dt;
|
|
|
|
prop->AllocateMemory();
|
|
|
|
|
|
|
|
// Make an entry in the address to variable map
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->varAddressMap.Insert(prop->GetAddressOfValue(), prop);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
// Store the variable in the module scope
|
2021-11-04 16:43:10 -04:00
|
|
|
m_scriptGlobals.Put(prop);
|
2015-05-11 19:40:43 -04:00
|
|
|
prop->AddRef();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
return prop;
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal
|
|
|
|
bool asCModule::IsEmpty() const
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
if( m_scriptFunctions.GetLength() ) return false;
|
|
|
|
if( m_globalFunctions.GetSize() ) return false;
|
|
|
|
if( m_bindInformations.GetLength() ) return false;
|
|
|
|
if( m_scriptGlobals.GetSize() ) return false;
|
|
|
|
if( m_classTypes.GetLength() ) return false;
|
|
|
|
if( m_enumTypes.GetLength() ) return false;
|
|
|
|
if( m_typeDefs.GetLength() ) return false;
|
|
|
|
if( m_funcDefs.GetLength() ) return false;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
int asCModule::SaveByteCode(asIBinaryStream *out, bool stripDebugInfo) const
|
|
|
|
{
|
|
|
|
#ifdef AS_NO_COMPILER
|
|
|
|
UNUSED_VAR(out);
|
|
|
|
UNUSED_VAR(stripDebugInfo);
|
|
|
|
return asNOT_SUPPORTED;
|
|
|
|
#else
|
|
|
|
if( out == 0 ) return asINVALID_ARG;
|
|
|
|
|
|
|
|
// Make sure there is actually something to save
|
|
|
|
if( IsEmpty() )
|
|
|
|
return asERROR;
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
asCWriter write(const_cast<asCModule*>(this), out, m_engine, stripDebugInfo);
|
2014-10-27 18:43:09 -04:00
|
|
|
return write.Write();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
int asCModule::LoadByteCode(asIBinaryStream *in, bool *wasDebugInfoStripped)
|
|
|
|
{
|
|
|
|
if( in == 0 ) return asINVALID_ARG;
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Don't allow the module to be rebuilt if there are still
|
2015-05-11 19:40:43 -04:00
|
|
|
// external references that will need the previous code
|
|
|
|
if( HasExternalReferences(false) )
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_MODULE_IS_IN_USE);
|
2015-05-11 19:40:43 -04:00
|
|
|
return asMODULE_IS_IN_USE;
|
|
|
|
}
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Only permit loading bytecode if no other thread is currently compiling
|
|
|
|
// TODO: It should be possible to have multiple threads perform compilations
|
2021-11-04 16:43:10 -04:00
|
|
|
int r = m_engine->RequestBuild();
|
2014-10-27 18:43:09 -04:00
|
|
|
if( r < 0 )
|
|
|
|
return r;
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
asCReader read(this, in, m_engine);
|
2014-10-27 18:43:09 -04:00
|
|
|
r = read.Read(wasDebugInfoStripped);
|
2017-11-20 08:02:33 -05:00
|
|
|
if (r < 0)
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->BuildCompleted();
|
2017-11-20 08:02:33 -05:00
|
|
|
return r;
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
JITCompile();
|
|
|
|
|
|
|
|
#ifdef AS_DEBUG
|
|
|
|
// Verify that there are no unwanted gaps in the scriptFunctions array.
|
2021-11-04 16:43:10 -04:00
|
|
|
for( asUINT n = 1; n < m_engine->scriptFunctions.GetLength(); n++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
int id = n;
|
2021-11-04 16:43:10 -04:00
|
|
|
if( m_engine->scriptFunctions[n] == 0 && !m_engine->freeScriptFunctionIds.Exists(id) )
|
2014-10-27 18:43:09 -04:00
|
|
|
asASSERT( false );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->BuildCompleted();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
int asCModule::CompileGlobalVar(const char *sectionName, const char *code, int lineOffset)
|
|
|
|
{
|
|
|
|
#ifdef AS_NO_COMPILER
|
|
|
|
UNUSED_VAR(sectionName);
|
|
|
|
UNUSED_VAR(code);
|
|
|
|
UNUSED_VAR(lineOffset);
|
|
|
|
return asNOT_SUPPORTED;
|
|
|
|
#else
|
|
|
|
// Validate arguments
|
|
|
|
if( code == 0 )
|
|
|
|
return asINVALID_ARG;
|
|
|
|
|
|
|
|
// Only one thread may build at one time
|
|
|
|
// TODO: It should be possible to have multiple threads perform compilations
|
2021-11-04 16:43:10 -04:00
|
|
|
int r = m_engine->RequestBuild();
|
2014-10-27 18:43:09 -04:00
|
|
|
if( r < 0 )
|
|
|
|
return r;
|
|
|
|
|
|
|
|
// Prepare the engine
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->PrepareEngine();
|
|
|
|
if( m_engine->configFailed )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_INVALID_CONFIGURATION);
|
|
|
|
m_engine->BuildCompleted();
|
2014-10-27 18:43:09 -04:00
|
|
|
return asINVALID_CONFIGURATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the global variable and add it to the module scope
|
2021-11-04 16:43:10 -04:00
|
|
|
asCBuilder varBuilder(m_engine, this);
|
2014-10-27 18:43:09 -04:00
|
|
|
asCString str = code;
|
2017-11-20 08:02:33 -05:00
|
|
|
r = varBuilder.CompileGlobalVar(sectionName, str.AddressOf(), lineOffset);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->BuildCompleted();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Initialize the variable
|
2021-11-04 16:43:10 -04:00
|
|
|
if( r >= 0 )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// Clear the memory
|
2021-11-04 16:43:10 -04:00
|
|
|
asCGlobalProperty *prop = m_scriptGlobals.GetLast();
|
2014-10-27 18:43:09 -04:00
|
|
|
if( prop )
|
|
|
|
{
|
|
|
|
memset(prop->GetAddressOfValue(), 0, sizeof(asDWORD)*prop->type.GetSizeOnStackDWords());
|
2021-11-04 16:43:10 -04:00
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
if( prop && m_engine->ep.initGlobalVarsAfterBuild )
|
|
|
|
{
|
|
|
|
// Flag that there are initialized global variables
|
|
|
|
m_isGlobalVarInitialized = true;
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
r = InitGlobalProp(prop, 0);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
2021-11-04 16:43:10 -04:00
|
|
|
int asCModule::CompileFunction(const char* sectionName, const char* code, int lineOffset, asDWORD compileFlags, asIScriptFunction** outFunc)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// Make sure the outFunc is null if the function fails, so the
|
2014-10-27 18:43:09 -04:00
|
|
|
// application doesn't attempt to release a non-existent function
|
2021-11-04 16:43:10 -04:00
|
|
|
if (outFunc)
|
2014-10-27 18:43:09 -04:00
|
|
|
*outFunc = 0;
|
|
|
|
|
|
|
|
#ifdef AS_NO_COMPILER
|
|
|
|
UNUSED_VAR(sectionName);
|
|
|
|
UNUSED_VAR(code);
|
|
|
|
UNUSED_VAR(lineOffset);
|
|
|
|
UNUSED_VAR(compileFlags);
|
|
|
|
return asNOT_SUPPORTED;
|
|
|
|
#else
|
|
|
|
// Validate arguments
|
2021-11-04 16:43:10 -04:00
|
|
|
if (code == 0 ||
|
|
|
|
(compileFlags != 0 && compileFlags != asCOMP_ADD_TO_MODULE))
|
2014-10-27 18:43:09 -04:00
|
|
|
return asINVALID_ARG;
|
|
|
|
|
|
|
|
// Only one thread may build at one time
|
|
|
|
// TODO: It should be possible to have multiple threads perform compilations
|
2021-11-04 16:43:10 -04:00
|
|
|
int r = m_engine->RequestBuild();
|
|
|
|
if (r < 0)
|
2014-10-27 18:43:09 -04:00
|
|
|
return r;
|
|
|
|
|
|
|
|
// Prepare the engine
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->PrepareEngine();
|
|
|
|
if (m_engine->configFailed)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_INVALID_CONFIGURATION);
|
|
|
|
m_engine->BuildCompleted();
|
2014-10-27 18:43:09 -04:00
|
|
|
return asINVALID_CONFIGURATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the single function
|
2021-11-04 16:43:10 -04:00
|
|
|
asCBuilder funcBuilder(m_engine, this);
|
2014-10-27 18:43:09 -04:00
|
|
|
asCString str = code;
|
2021-11-04 16:43:10 -04:00
|
|
|
asCScriptFunction* func = 0;
|
2017-11-20 08:02:33 -05:00
|
|
|
r = funcBuilder.CompileFunction(sectionName, str.AddressOf(), lineOffset, compileFlags, &func);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
if (r >= 0)
|
|
|
|
{
|
|
|
|
// Invoke the JIT compiler if it has been set
|
|
|
|
asIJITCompiler* jit = m_engine->GetJITCompiler();
|
|
|
|
if (jit)
|
|
|
|
{
|
|
|
|
func->JITCompile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_engine->BuildCompleted();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
if( r >= 0 && outFunc && func )
|
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
// Return the function to the caller and add an external reference
|
2014-10-27 18:43:09 -04:00
|
|
|
*outFunc = func;
|
|
|
|
func->AddRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release our reference to the function
|
|
|
|
if( func )
|
2015-05-11 19:40:43 -04:00
|
|
|
func->ReleaseInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
return r;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// interface
|
|
|
|
int asCModule::RemoveFunction(asIScriptFunction *func)
|
|
|
|
{
|
|
|
|
// Find the global function
|
|
|
|
asCScriptFunction *f = static_cast<asCScriptFunction*>(func);
|
2021-11-04 16:43:10 -04:00
|
|
|
int idx = m_globalFunctions.GetIndex(f);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( idx >= 0 )
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
m_globalFunctions.Erase(idx);
|
|
|
|
m_scriptFunctions.RemoveValue(f);
|
2015-05-11 19:40:43 -04:00
|
|
|
f->ReleaseInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return asNO_FUNCTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef AS_NO_COMPILER
|
|
|
|
// internal
|
2017-11-20 08:02:33 -05:00
|
|
|
int asCModule::AddFuncDef(const asCString &funcName, asSNameSpace *ns, asCObjectType *parent)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// namespace and parent are mutually exclusive
|
|
|
|
asASSERT((ns == 0 && parent) || (ns && parent == 0));
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
asCScriptFunction *func = asNEW(asCScriptFunction)(m_engine, 0, asFUNC_FUNCDEF);
|
2017-11-20 08:02:33 -05:00
|
|
|
if (func == 0)
|
2014-10-27 18:43:09 -04:00
|
|
|
return asOUT_OF_MEMORY;
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
func->name = funcName;
|
2014-10-27 18:43:09 -04:00
|
|
|
func->nameSpace = ns;
|
2016-02-17 23:07:55 -05:00
|
|
|
func->module = this;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
asCFuncdefType *fdt = asNEW(asCFuncdefType)(m_engine, func);
|
|
|
|
AddFuncDef(fdt); // The constructor set the refcount to 1
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
m_engine->funcDefs.PushLast(fdt); // doesn't increase refcount
|
|
|
|
func->id = m_engine->GetNextScriptFunctionId();
|
|
|
|
m_engine->AddScriptFunction(func);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if (parent)
|
|
|
|
{
|
|
|
|
parent->childFuncDefs.PushLast(fdt);
|
|
|
|
fdt->parentClass = parent;
|
|
|
|
}
|
|
|
|
|
2021-11-04 16:43:10 -04:00
|
|
|
return (int)m_funcDefs.GetLength()-1;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// interface
|
|
|
|
asDWORD asCModule::SetAccessMask(asDWORD mask)
|
|
|
|
{
|
2021-11-04 16:43:10 -04:00
|
|
|
asDWORD old = m_accessMask;
|
|
|
|
m_accessMask = mask;
|
2014-10-27 18:43:09 -04:00
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
END_AS_NAMESPACE
|
|
|
|
|