2014-10-27 18:43:09 -04:00
|
|
|
/*
|
|
|
|
AngelCode Scripting Library
|
2017-11-20 08:02:33 -05:00
|
|
|
Copyright (c) 2003-2017 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_restore.cpp
|
|
|
|
//
|
|
|
|
// Functions for saving and restoring module bytecode
|
|
|
|
// asCRestore was originally written by Dennis Bollyn, dennis@gyrbo.be
|
|
|
|
|
|
|
|
#include "as_config.h"
|
|
|
|
#include "as_restore.h"
|
|
|
|
#include "as_bytecode.h"
|
|
|
|
#include "as_scriptobject.h"
|
|
|
|
#include "as_texts.h"
|
2016-02-17 23:07:55 -05:00
|
|
|
#include "as_debug.h"
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
BEGIN_AS_NAMESPACE
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Macros for doing endianess agnostic bitmask serialization
|
|
|
|
#define SAVE_TO_BIT(dst, val, bit) ((dst) |= ((val) << (bit)))
|
|
|
|
#define LOAD_FROM_BIT(dst, val, bit) ((dst) = ((val) >> (bit)) & 1)
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
asCReader::asCReader(asCModule* _module, asIBinaryStream* _stream, asCScriptEngine* _engine)
|
|
|
|
: module(_module), stream(_stream), engine(_engine)
|
|
|
|
{
|
|
|
|
error = false;
|
|
|
|
bytesRead = 0;
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
int asCReader::ReadData(void *data, asUINT size)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
asASSERT(size == 1 || size == 2 || size == 4 || size == 8);
|
2017-11-20 08:02:33 -05:00
|
|
|
int ret = 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
#if defined(AS_BIG_ENDIAN)
|
2017-11-20 08:02:33 -05:00
|
|
|
for( asUINT n = 0; ret >= 0 && n < size; n++ )
|
|
|
|
ret = stream->Read(((asBYTE*)data)+n, 1);
|
2014-10-27 18:43:09 -04:00
|
|
|
#else
|
2017-11-20 08:02:33 -05:00
|
|
|
for( int n = size-1; ret >= 0 && n >= 0; n-- )
|
|
|
|
ret = stream->Read(((asBYTE*)data)+n, 1);
|
2014-10-27 18:43:09 -04:00
|
|
|
#endif
|
2017-11-20 08:02:33 -05:00
|
|
|
if (ret < 0)
|
|
|
|
Error(TXT_UNEXPECTED_END_OF_FILE);
|
2014-10-27 18:43:09 -04:00
|
|
|
bytesRead += size;
|
2017-11-20 08:02:33 -05:00
|
|
|
return ret;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int asCReader::Read(bool *wasDebugInfoStripped)
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("asCReader::Read");
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Before starting the load, make sure that
|
2014-10-27 18:43:09 -04:00
|
|
|
// any existing resources have been freed
|
|
|
|
module->InternalReset();
|
|
|
|
|
|
|
|
// Call the inner method to do the actual loading
|
|
|
|
int r = ReadInner();
|
|
|
|
if( r < 0 )
|
|
|
|
{
|
|
|
|
// Something went wrong while loading the bytecode, so we need
|
|
|
|
// to clean-up whatever has been created during the process.
|
|
|
|
|
|
|
|
// Make sure none of the loaded functions attempt to release
|
|
|
|
// references that have not yet been increased
|
|
|
|
asUINT i;
|
|
|
|
for( i = 0; i < module->scriptFunctions.GetLength(); i++ )
|
|
|
|
if( !dontTranslate.MoveTo(0, module->scriptFunctions[i]) )
|
|
|
|
if( module->scriptFunctions[i]->scriptData )
|
|
|
|
module->scriptFunctions[i]->scriptData->byteCode.SetLength(0);
|
|
|
|
|
|
|
|
asCSymbolTable<asCGlobalProperty>::iterator it = module->scriptGlobals.List();
|
|
|
|
for( ; it; it++ )
|
|
|
|
if( (*it)->GetInitFunc() )
|
|
|
|
if( (*it)->GetInitFunc()->scriptData )
|
|
|
|
(*it)->GetInitFunc()->scriptData->byteCode.SetLength(0);
|
|
|
|
|
|
|
|
module->InternalReset();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Init system functions properly
|
|
|
|
engine->PrepareEngine();
|
|
|
|
|
|
|
|
// Initialize the global variables (unless requested not to)
|
|
|
|
if( engine->ep.initGlobalVarsAfterBuild )
|
|
|
|
r = module->ResetGlobalVars(0);
|
|
|
|
|
|
|
|
if( wasDebugInfoStripped )
|
|
|
|
*wasDebugInfoStripped = noDebugInfo;
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Clean up the loaded string constants
|
|
|
|
for (asUINT n = 0; n < usedStringConstants.GetLength(); n++)
|
|
|
|
engine->stringFactory->ReleaseStringConstant(usedStringConstants[n]);
|
|
|
|
usedStringConstants.SetLength(0);
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int asCReader::Error(const char *msg)
|
|
|
|
{
|
|
|
|
// Don't write if it has already been reported an error earlier
|
|
|
|
if( !error )
|
|
|
|
{
|
|
|
|
asCString str;
|
|
|
|
str.Format(msg, bytesRead);
|
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
return asERROR;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
int asCReader::ReadInner()
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("asCReader::ReadInner");
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// This function will load each entity one by one from the stream.
|
2017-11-20 08:02:33 -05:00
|
|
|
// If any error occurs, it will return to the caller who is
|
2014-10-27 18:43:09 -04:00
|
|
|
// responsible for cleaning up the partially loaded entities.
|
|
|
|
|
|
|
|
engine->deferValidationOfTemplateTypes = true;
|
|
|
|
|
|
|
|
unsigned long i, count;
|
|
|
|
asCScriptFunction* func;
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Read the flag as 1 byte even on platforms with 4byte booleans
|
|
|
|
noDebugInfo = ReadEncodedUInt() ? VALUE_OF_BOOLEAN_TRUE : 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Read enums
|
|
|
|
count = ReadEncodedUInt();
|
|
|
|
module->enumTypes.Allocate(count, false);
|
|
|
|
for( i = 0; i < count && !error; i++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCEnumType *et = asNEW(asCEnumType)(engine);
|
|
|
|
if( et == 0 )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
error = true;
|
|
|
|
return asOUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
bool isExternal = false;
|
|
|
|
ReadTypeDeclaration(et, 1, &isExternal);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// If the type is shared then we should use the original if it exists
|
|
|
|
bool sharedExists = false;
|
2017-11-20 08:02:33 -05:00
|
|
|
if( et->IsShared() )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
for( asUINT n = 0; n < engine->sharedScriptTypes.GetLength(); n++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCTypeInfo *t = engine->sharedScriptTypes[n];
|
|
|
|
if( t &&
|
2014-10-27 18:43:09 -04:00
|
|
|
t->IsShared() &&
|
2017-11-20 08:02:33 -05:00
|
|
|
t->name == et->name &&
|
|
|
|
t->nameSpace == et->nameSpace &&
|
2014-10-27 18:43:09 -04:00
|
|
|
(t->flags & asOBJ_ENUM) )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asDELETE(et, asCEnumType);
|
|
|
|
et = CastToEnumType(t);
|
2014-10-27 18:43:09 -04:00
|
|
|
sharedExists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if (isExternal && !sharedExists)
|
|
|
|
{
|
|
|
|
asCString msg;
|
|
|
|
msg.Format(TXT_EXTERNAL_SHARED_s_NOT_FOUND, et->name.AddressOf());
|
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, msg.AddressOf());
|
|
|
|
asDELETE(et, asCEnumType);
|
|
|
|
error = true;
|
|
|
|
return asERROR;
|
|
|
|
}
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
if( sharedExists )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
existingShared.Insert(et, true);
|
|
|
|
et->AddRefInternal();
|
2015-05-11 19:40:43 -04:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
else
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
if( et->IsShared() )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
engine->sharedScriptTypes.PushLast(et);
|
|
|
|
et->AddRefInternal();
|
2015-05-11 19:40:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set this module as the owner
|
2017-11-20 08:02:33 -05:00
|
|
|
et->module = module;
|
2015-05-11 19:40:43 -04:00
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
module->enumTypes.PushLast(et);
|
|
|
|
|
|
|
|
if (isExternal)
|
|
|
|
module->externalTypes.PushLast(et);
|
|
|
|
|
|
|
|
ReadTypeDeclaration(et, 2);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if( error ) return asERROR;
|
|
|
|
|
|
|
|
// classTypes[]
|
|
|
|
// First restore the structure names, then the properties
|
|
|
|
count = ReadEncodedUInt();
|
|
|
|
module->classTypes.Allocate(count, false);
|
|
|
|
for( i = 0; i < count && !error; ++i )
|
|
|
|
{
|
|
|
|
asCObjectType *ot = asNEW(asCObjectType)(engine);
|
|
|
|
if( ot == 0 )
|
|
|
|
{
|
|
|
|
error = true;
|
|
|
|
return asOUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
bool isExternal = false;
|
|
|
|
ReadTypeDeclaration(ot, 1, &isExternal);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// If the type is shared, then we should use the original if it exists
|
|
|
|
bool sharedExists = false;
|
|
|
|
if( ot->IsShared() )
|
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
for( asUINT n = 0; n < engine->sharedScriptTypes.GetLength(); n++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCTypeInfo *ti = engine->sharedScriptTypes[n];
|
|
|
|
asCObjectType *t = CastToObjectType(ti);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( t &&
|
|
|
|
t->IsShared() &&
|
|
|
|
t->name == ot->name &&
|
|
|
|
t->nameSpace == ot->nameSpace &&
|
|
|
|
t->IsInterface() == ot->IsInterface() )
|
|
|
|
{
|
|
|
|
asDELETE(ot, asCObjectType);
|
2017-11-20 08:02:33 -05:00
|
|
|
ot = CastToObjectType(t);
|
2014-10-27 18:43:09 -04:00
|
|
|
sharedExists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if (isExternal && !sharedExists)
|
|
|
|
{
|
|
|
|
asCString msg;
|
|
|
|
msg.Format(TXT_EXTERNAL_SHARED_s_NOT_FOUND, ot->name.AddressOf());
|
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, msg.AddressOf());
|
|
|
|
asDELETE(ot, asCObjectType);
|
|
|
|
error = true;
|
|
|
|
return asERROR;
|
|
|
|
}
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
if( sharedExists )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2014-10-27 18:43:09 -04:00
|
|
|
existingShared.Insert(ot, true);
|
2015-05-11 19:40:43 -04:00
|
|
|
ot->AddRefInternal();
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
else
|
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
if( ot->IsShared() )
|
|
|
|
{
|
|
|
|
engine->sharedScriptTypes.PushLast(ot);
|
|
|
|
ot->AddRefInternal();
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Set this module as the owner
|
|
|
|
ot->module = module;
|
|
|
|
}
|
|
|
|
module->classTypes.PushLast(ot);
|
2017-11-20 08:02:33 -05:00
|
|
|
|
|
|
|
if (isExternal)
|
|
|
|
module->externalTypes.PushLast(ot);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if( error ) return asERROR;
|
|
|
|
|
|
|
|
// Read func defs
|
|
|
|
count = ReadEncodedUInt();
|
|
|
|
module->funcDefs.Allocate(count, false);
|
|
|
|
for( i = 0; i < count && !error; i++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
bool isNew, isExternal;
|
|
|
|
asCScriptFunction *funcDef = ReadFunction(isNew, false, true, true, &isExternal);
|
|
|
|
if(funcDef)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
funcDef->module = module;
|
|
|
|
|
|
|
|
asCFuncdefType *fdt = funcDef->funcdefType;
|
|
|
|
fdt->module = module;
|
|
|
|
|
|
|
|
module->funcDefs.PushLast(fdt);
|
|
|
|
engine->funcDefs.PushLast(fdt);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// TODO: clean up: This is also done by the builder. It should probably be moved to a method in the module
|
|
|
|
// Check if there is another identical funcdef from another module and if so reuse that instead
|
2017-11-20 08:02:33 -05:00
|
|
|
if(funcDef->IsShared())
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
for( asUINT n = 0; n < engine->funcDefs.GetLength(); n++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCFuncdefType *f2 = engine->funcDefs[n];
|
|
|
|
if( f2 == 0 || fdt == f2 )
|
2016-02-17 23:07:55 -05:00
|
|
|
continue;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if( !f2->funcdef->IsShared() )
|
2016-02-17 23:07:55 -05:00
|
|
|
continue;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if( f2->name == fdt->name &&
|
|
|
|
f2->nameSpace == fdt->nameSpace &&
|
|
|
|
f2->parentClass == fdt->parentClass &&
|
|
|
|
f2->funcdef->IsSignatureExceptNameEqual(funcDef) )
|
2016-02-17 23:07:55 -05:00
|
|
|
{
|
|
|
|
// Replace our funcdef for the existing one
|
2017-11-20 08:02:33 -05:00
|
|
|
module->funcDefs[module->funcDefs.IndexOf(fdt)] = f2;
|
2016-02-17 23:07:55 -05:00
|
|
|
f2->AddRefInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if (isExternal)
|
|
|
|
module->externalTypes.PushLast(f2);
|
|
|
|
|
|
|
|
engine->funcDefs.RemoveValue(fdt);
|
|
|
|
|
|
|
|
savedFunctions[savedFunctions.IndexOf(funcDef)] = f2->funcdef;
|
2016-02-17 23:07:55 -05:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if (fdt->parentClass)
|
|
|
|
{
|
|
|
|
// The real funcdef should already be in the object
|
|
|
|
asASSERT(fdt->parentClass->childFuncDefs.IndexOf(f2) >= 0);
|
|
|
|
|
|
|
|
fdt->parentClass = 0;
|
|
|
|
}
|
2016-02-17 23:07:55 -05:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
fdt->ReleaseInternal();
|
|
|
|
funcDef = 0;
|
2016-02-17 23:07:55 -05:00
|
|
|
break;
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
|
|
|
|
// Add the funcdef to the parentClass if this is a child funcdef
|
|
|
|
if (funcDef && fdt->parentClass)
|
|
|
|
fdt->parentClass->childFuncDefs.PushLast(fdt);
|
|
|
|
|
|
|
|
// Check if an external shared funcdef was really found
|
|
|
|
if (isExternal && funcDef)
|
|
|
|
{
|
|
|
|
asCString msg;
|
|
|
|
msg.Format(TXT_EXTERNAL_SHARED_s_NOT_FOUND, funcDef->name.AddressOf());
|
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, msg.AddressOf());
|
|
|
|
error = true;
|
|
|
|
return asERROR;
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read interface methods
|
|
|
|
for( i = 0; i < module->classTypes.GetLength() && !error; i++ )
|
|
|
|
{
|
|
|
|
if( module->classTypes[i]->IsInterface() )
|
2017-11-20 08:02:33 -05:00
|
|
|
ReadTypeDeclaration(module->classTypes[i], 2);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read class methods and behaviours
|
|
|
|
for( i = 0; i < module->classTypes.GetLength() && !error; ++i )
|
|
|
|
{
|
|
|
|
if( !module->classTypes[i]->IsInterface() )
|
2017-11-20 08:02:33 -05:00
|
|
|
ReadTypeDeclaration(module->classTypes[i], 2);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read class properties
|
|
|
|
for( i = 0; i < module->classTypes.GetLength() && !error; ++i )
|
|
|
|
{
|
|
|
|
if( !module->classTypes[i]->IsInterface() )
|
2017-11-20 08:02:33 -05:00
|
|
|
ReadTypeDeclaration(module->classTypes[i], 3);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if( error ) return asERROR;
|
|
|
|
|
|
|
|
// Read typedefs
|
|
|
|
count = ReadEncodedUInt();
|
|
|
|
module->typeDefs.Allocate(count, false);
|
|
|
|
for( i = 0; i < count && !error; i++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCTypedefType *td = asNEW(asCTypedefType)(engine);
|
|
|
|
if( td == 0 )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
error = true;
|
|
|
|
return asOUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
bool isExternal = false;
|
|
|
|
ReadTypeDeclaration(td, 1, &isExternal);
|
|
|
|
td->module = module;
|
|
|
|
module->typeDefs.PushLast(td);
|
|
|
|
ReadTypeDeclaration(td, 2);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if( error ) return asERROR;
|
|
|
|
|
|
|
|
// scriptGlobals[]
|
|
|
|
count = ReadEncodedUInt();
|
|
|
|
if( count && engine->ep.disallowGlobalVars )
|
|
|
|
{
|
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, TXT_GLOBAL_VARS_NOT_ALLOWED);
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
module->scriptGlobals.Allocate(count, false);
|
2017-11-20 08:02:33 -05:00
|
|
|
for( i = 0; i < count && !error; ++i )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
ReadGlobalProperty();
|
|
|
|
}
|
|
|
|
|
|
|
|
// scriptFunctions[]
|
|
|
|
count = ReadEncodedUInt();
|
2017-11-20 08:02:33 -05:00
|
|
|
for( i = 0; i < count && !error; ++i )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
size_t len = module->scriptFunctions.GetLength();
|
2017-11-20 08:02:33 -05:00
|
|
|
bool isNew, isExternal;
|
|
|
|
func = ReadFunction(isNew, true, true, true, &isExternal);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( func == 0 )
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
break;
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Is the function shared and was it created now?
|
2017-11-20 08:02:33 -05:00
|
|
|
if( func->IsShared() && len != module->scriptFunctions.GetLength() )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
// If the function already existed in another module, then
|
|
|
|
// we need to replace it with previously existing one
|
|
|
|
for( asUINT n = 0; n < engine->scriptFunctions.GetLength() && !error; n++ )
|
|
|
|
{
|
|
|
|
asCScriptFunction *realFunc = engine->scriptFunctions[n];
|
|
|
|
if( realFunc &&
|
|
|
|
realFunc != func &&
|
|
|
|
realFunc->IsShared() &&
|
|
|
|
realFunc->IsSignatureEqual(func) )
|
|
|
|
{
|
|
|
|
// Replace the recently created function with the pre-existing function
|
|
|
|
module->scriptFunctions[module->scriptFunctions.GetLength()-1] = realFunc;
|
2015-05-11 19:40:43 -04:00
|
|
|
realFunc->AddRefInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
savedFunctions[savedFunctions.GetLength()-1] = realFunc;
|
2015-05-11 19:40:43 -04:00
|
|
|
engine->RemoveScriptFunction(func);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Insert the function in the dontTranslate array
|
|
|
|
dontTranslate.Insert(realFunc, true);
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if (isExternal)
|
|
|
|
module->externalFunctions.PushLast(realFunc);
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Release the function, but make sure nothing else is released
|
|
|
|
func->id = 0;
|
2017-11-20 08:02:33 -05:00
|
|
|
if( func->scriptData )
|
|
|
|
func->scriptData->byteCode.SetLength(0);
|
2015-05-11 19:40:43 -04:00
|
|
|
func->ReleaseInternal();
|
2017-11-20 08:02:33 -05:00
|
|
|
func = 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
|
|
|
|
// Check if an external shared func was really found
|
|
|
|
if (isExternal && func)
|
|
|
|
{
|
|
|
|
asCString msg;
|
|
|
|
msg.Format(TXT_EXTERNAL_SHARED_s_NOT_FOUND, func->name.AddressOf());
|
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, msg.AddressOf());
|
|
|
|
error = true;
|
|
|
|
return asERROR;
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// globalFunctions[]
|
|
|
|
count = ReadEncodedUInt();
|
|
|
|
for( i = 0; i < count && !error; ++i )
|
|
|
|
{
|
|
|
|
bool isNew;
|
|
|
|
func = ReadFunction(isNew, false, false);
|
|
|
|
if( func )
|
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
// All the global functions were already loaded while loading the scriptFunctions, here
|
|
|
|
// we're just re-reading the references to know which goes into the globalFunctions array
|
|
|
|
asASSERT( !isNew );
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
module->globalFunctions.Put(func);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( error ) return asERROR;
|
|
|
|
|
|
|
|
// bindInformations[]
|
|
|
|
count = ReadEncodedUInt();
|
|
|
|
module->bindInformations.Allocate(count, false);
|
|
|
|
for( i = 0; i < count && !error; ++i )
|
|
|
|
{
|
|
|
|
sBindInfo *info = asNEW(sBindInfo);
|
|
|
|
if( info == 0 )
|
|
|
|
{
|
|
|
|
error = true;
|
|
|
|
return asOUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isNew;
|
|
|
|
info->importedFunctionSignature = ReadFunction(isNew, false, false);
|
|
|
|
if( info->importedFunctionSignature == 0 )
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( engine->freeImportedFunctionIdxs.GetLength() )
|
|
|
|
{
|
|
|
|
int id = engine->freeImportedFunctionIdxs.PopLast();
|
|
|
|
info->importedFunctionSignature->id = int(FUNC_IMPORTED + id);
|
|
|
|
engine->importedFunctions[id] = info;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info->importedFunctionSignature->id = int(FUNC_IMPORTED + engine->importedFunctions.GetLength());
|
|
|
|
engine->importedFunctions.PushLast(info);
|
|
|
|
}
|
|
|
|
ReadString(&info->importFromModule);
|
|
|
|
info->boundFunctionId = -1;
|
|
|
|
module->bindInformations.PushLast(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( error ) return asERROR;
|
|
|
|
|
|
|
|
// usedTypes[]
|
|
|
|
count = ReadEncodedUInt();
|
|
|
|
usedTypes.Allocate(count, false);
|
|
|
|
for( i = 0; i < count && !error; ++i )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCTypeInfo *ti = ReadTypeInfo();
|
|
|
|
usedTypes.PushLast(ti);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// usedTypeIds[]
|
|
|
|
if( !error )
|
|
|
|
ReadUsedTypeIds();
|
|
|
|
|
|
|
|
// usedFunctions[]
|
|
|
|
if( !error )
|
|
|
|
ReadUsedFunctions();
|
|
|
|
|
|
|
|
// usedGlobalProperties[]
|
|
|
|
if( !error )
|
|
|
|
ReadUsedGlobalProps();
|
|
|
|
|
|
|
|
// usedStringConstants[]
|
2017-11-20 08:02:33 -05:00
|
|
|
if( !error )
|
2014-10-27 18:43:09 -04:00
|
|
|
ReadUsedStringConstants();
|
|
|
|
|
|
|
|
// usedObjectProperties
|
|
|
|
if( !error )
|
|
|
|
ReadUsedObjectProps();
|
|
|
|
|
|
|
|
// Validate the template types
|
|
|
|
if( !error )
|
|
|
|
{
|
|
|
|
for( i = 0; i < usedTypes.GetLength() && !error; i++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *ot = CastToObjectType(usedTypes[i]);
|
|
|
|
if( !ot ||
|
|
|
|
!(ot->flags & asOBJ_TEMPLATE) ||
|
|
|
|
!ot->beh.templateCallback )
|
2014-10-27 18:43:09 -04:00
|
|
|
continue;
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
bool dontGarbageCollect = false;
|
2017-11-20 08:02:33 -05:00
|
|
|
asCScriptFunction *callback = engine->scriptFunctions[ot->beh.templateCallback];
|
|
|
|
if( !engine->CallGlobalFunctionRetBool(ot, &dontGarbageCollect, callback->sysFuncIntf, callback) )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCString sub = ot->templateSubTypes[0].Format(ot->nameSpace);
|
|
|
|
for( asUINT n = 1; n < ot->templateSubTypes.GetLength(); n++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
sub += ",";
|
2017-11-20 08:02:33 -05:00
|
|
|
sub += ot->templateSubTypes[n].Format(ot->nameSpace);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
asCString str;
|
2017-11-20 08:02:33 -05:00
|
|
|
str.Format(TXT_INSTANCING_INVLD_TMPL_TYPE_s_s, ot->name.AddressOf(), sub.AddressOf());
|
2014-10-27 18:43:09 -04:00
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
2015-05-11 19:40:43 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// If the callback said this template instance won't be garbage collected then remove the flag
|
|
|
|
if( dontGarbageCollect )
|
2017-11-20 08:02:33 -05:00
|
|
|
ot->flags &= ~asOBJ_GC;
|
2015-05-11 19:40:43 -04:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
engine->deferValidationOfTemplateTypes = false;
|
|
|
|
|
|
|
|
if( error ) return asERROR;
|
|
|
|
|
|
|
|
// Update the loaded bytecode to point to the correct types, property offsets,
|
|
|
|
// function ids, etc. This is basically a linking stage.
|
|
|
|
for( i = 0; i < module->scriptFunctions.GetLength() && !error; i++ )
|
|
|
|
if( module->scriptFunctions[i]->funcType == asFUNC_SCRIPT )
|
|
|
|
TranslateFunction(module->scriptFunctions[i]);
|
|
|
|
|
|
|
|
asCSymbolTable<asCGlobalProperty>::iterator globIt = module->scriptGlobals.List();
|
|
|
|
while( globIt && !error )
|
|
|
|
{
|
|
|
|
asCScriptFunction *initFunc = (*globIt)->GetInitFunc();
|
|
|
|
if( initFunc )
|
|
|
|
TranslateFunction(initFunc);
|
|
|
|
globIt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( error ) return asERROR;
|
|
|
|
|
|
|
|
// Add references for all functions (except for the pre-existing shared code)
|
|
|
|
for( i = 0; i < module->scriptFunctions.GetLength(); i++ )
|
|
|
|
if( !dontTranslate.MoveTo(0, module->scriptFunctions[i]) )
|
|
|
|
module->scriptFunctions[i]->AddReferences();
|
|
|
|
|
|
|
|
globIt = module->scriptGlobals.List();
|
|
|
|
while( globIt )
|
|
|
|
{
|
|
|
|
asCScriptFunction *initFunc = (*globIt)->GetInitFunc();
|
|
|
|
if( initFunc )
|
|
|
|
initFunc->AddReferences();
|
|
|
|
globIt++;
|
|
|
|
}
|
|
|
|
return error ? asERROR : asSUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCReader::ReadUsedStringConstants()
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("asCReader::ReadUsedStringConstants");
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
asCString str;
|
|
|
|
|
|
|
|
asUINT count;
|
|
|
|
count = ReadEncodedUInt();
|
2017-11-20 08:02:33 -05:00
|
|
|
|
|
|
|
if (count > 0 && engine->stringFactory == 0)
|
|
|
|
{
|
|
|
|
Error(TXT_STRINGS_NOT_RECOGNIZED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
usedStringConstants.Allocate(count, false);
|
2017-11-20 08:02:33 -05:00
|
|
|
for( asUINT i = 0; i < count; ++i )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
ReadString(&str);
|
2017-11-20 08:02:33 -05:00
|
|
|
usedStringConstants.PushLast(const_cast<void*>(engine->stringFactory->GetStringConstant(str.AddressOf(), (asUINT)str.GetLength())));
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCReader::ReadUsedFunctions()
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("asCReader::ReadUsedFunctions");
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
asUINT count;
|
|
|
|
count = ReadEncodedUInt();
|
|
|
|
usedFunctions.SetLength(count);
|
2015-05-11 19:40:43 -04:00
|
|
|
if( usedFunctions.GetLength() != count )
|
|
|
|
{
|
|
|
|
// Out of memory
|
|
|
|
error = true;
|
|
|
|
return;
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
memset(usedFunctions.AddressOf(), 0, sizeof(asCScriptFunction *)*count);
|
|
|
|
|
|
|
|
for( asUINT n = 0; n < usedFunctions.GetLength(); n++ )
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
|
|
|
// Read the data to be able to uniquely identify the function
|
|
|
|
|
|
|
|
// Is the function from the module or the application?
|
|
|
|
ReadData(&c, 1);
|
|
|
|
|
|
|
|
if( c == 'n' )
|
|
|
|
{
|
|
|
|
// Null function pointer
|
|
|
|
usedFunctions[n] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asCScriptFunction func(engine, c == 'm' ? module : 0, asFUNC_DUMMY);
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *parentClass = 0;
|
|
|
|
ReadFunctionSignature(&func, &parentClass);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( error )
|
|
|
|
{
|
|
|
|
func.funcType = asFUNC_DUMMY;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the correct function
|
|
|
|
if( c == 'm' )
|
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
if( func.funcType == asFUNC_IMPORTED )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
for( asUINT i = 0; i < module->bindInformations.GetLength(); i++ )
|
|
|
|
{
|
|
|
|
asCScriptFunction *f = module->bindInformations[i]->importedFunctionSignature;
|
2016-02-17 23:07:55 -05:00
|
|
|
if( func.objectType != f->objectType ||
|
2017-11-20 08:02:33 -05:00
|
|
|
func.funcType != f->funcType ||
|
2016-02-17 23:07:55 -05:00
|
|
|
func.nameSpace != f->nameSpace ||
|
|
|
|
!func.IsSignatureEqual(f) )
|
2015-05-11 19:40:43 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
usedFunctions[n] = f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-02-17 23:07:55 -05:00
|
|
|
else if( func.funcType == asFUNC_FUNCDEF )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
const asCArray<asCFuncdefType *> &funcs = module->funcDefs;
|
2016-02-17 23:07:55 -05:00
|
|
|
for( asUINT i = 0; i < funcs.GetLength(); i++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCScriptFunction *f = funcs[i]->funcdef;
|
|
|
|
if( f == 0 ||
|
|
|
|
func.name != f->name ||
|
|
|
|
!func.IsSignatureExceptNameAndObjectTypeEqual(f) ||
|
|
|
|
funcs[i]->parentClass != parentClass )
|
2016-02-17 23:07:55 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
asASSERT( f->objectType == 0 );
|
|
|
|
|
|
|
|
usedFunctions[n] = f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-11 19:40:43 -04:00
|
|
|
else
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
// TODO: optimize: Global functions should be searched for in module->globalFunctions
|
|
|
|
// TODO: optimize: funcdefs should be searched for in module->funcDefs
|
|
|
|
// TODO: optimize: object methods should be searched for directly in the object type
|
2015-05-11 19:40:43 -04:00
|
|
|
for( asUINT i = 0; i < module->scriptFunctions.GetLength(); i++ )
|
|
|
|
{
|
|
|
|
asCScriptFunction *f = module->scriptFunctions[i];
|
2016-02-17 23:07:55 -05:00
|
|
|
if( func.objectType != f->objectType ||
|
2017-11-20 08:02:33 -05:00
|
|
|
func.funcType != f->funcType ||
|
2016-02-17 23:07:55 -05:00
|
|
|
func.nameSpace != f->nameSpace ||
|
|
|
|
!func.IsSignatureEqual(f) )
|
2015-05-11 19:40:43 -04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
usedFunctions[n] = f;
|
|
|
|
break;
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
else if (c == 's')
|
|
|
|
{
|
|
|
|
// Look for shared entities in the engine, as they may not necessarily be part
|
|
|
|
// of the scope of the module if they have been inhereted from other modules.
|
|
|
|
if (func.funcType == asFUNC_FUNCDEF)
|
|
|
|
{
|
|
|
|
const asCArray<asCFuncdefType *> &funcs = engine->funcDefs;
|
|
|
|
for (asUINT i = 0; i < funcs.GetLength(); i++)
|
|
|
|
{
|
|
|
|
asCScriptFunction *f = funcs[i]->funcdef;
|
|
|
|
if (f == 0 ||
|
|
|
|
func.name != f->name ||
|
|
|
|
!func.IsSignatureExceptNameAndObjectTypeEqual(f) ||
|
|
|
|
funcs[i]->parentClass != parentClass)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
asASSERT(f->objectType == 0);
|
|
|
|
|
|
|
|
usedFunctions[n] = f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (asUINT i = 0; i < engine->scriptFunctions.GetLength(); i++)
|
|
|
|
{
|
|
|
|
asCScriptFunction *f = engine->scriptFunctions[i];
|
|
|
|
if (f == 0 || !f->IsShared() ||
|
|
|
|
func.objectType != f->objectType ||
|
|
|
|
func.funcType != f->funcType ||
|
|
|
|
func.nameSpace != f->nameSpace ||
|
|
|
|
!func.IsSignatureEqual(f))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
usedFunctions[n] = f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
else
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asASSERT(c == 'a');
|
|
|
|
|
2016-02-17 23:07:55 -05:00
|
|
|
if( func.funcType == asFUNC_FUNCDEF )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
// This is a funcdef (registered or shared)
|
2017-11-20 08:02:33 -05:00
|
|
|
const asCArray<asCFuncdefType *> &funcs = engine->funcDefs;
|
2016-02-17 23:07:55 -05:00
|
|
|
for( asUINT i = 0; i < funcs.GetLength(); i++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCScriptFunction *f = funcs[i]->funcdef;
|
|
|
|
if( f == 0 || func.name != f->name || !func.IsSignatureExceptNameAndObjectTypeEqual(f) || funcs[i]->parentClass != parentClass )
|
2016-02-17 23:07:55 -05:00
|
|
|
continue;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2016-02-17 23:07:55 -05:00
|
|
|
asASSERT( f->objectType == 0 );
|
|
|
|
|
|
|
|
usedFunctions[n] = f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( func.name[0] == '$' )
|
|
|
|
{
|
|
|
|
// This is a special function
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if( func.name == "$beh0" && func.objectType )
|
2016-02-17 23:07:55 -05:00
|
|
|
{
|
|
|
|
// This is a class constructor, so we can search directly in the object type's constructors
|
|
|
|
for( asUINT i = 0; i < func.objectType->beh.constructors.GetLength(); i++ )
|
|
|
|
{
|
|
|
|
asCScriptFunction *f = engine->scriptFunctions[func.objectType->beh.constructors[i]];
|
|
|
|
if( f == 0 ||
|
|
|
|
!func.IsSignatureExceptNameAndObjectTypeEqual(f) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
usedFunctions[n] = f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( func.name == "$fact" || func.name == "$beh3" )
|
|
|
|
{
|
|
|
|
// This is a factory (or stub), so look for the function in the return type's factories
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *objType = CastToObjectType(func.returnType.GetTypeInfo());
|
2016-02-17 23:07:55 -05:00
|
|
|
if( objType )
|
|
|
|
{
|
|
|
|
for( asUINT i = 0; i < objType->beh.factories.GetLength(); i++ )
|
|
|
|
{
|
|
|
|
asCScriptFunction *f = engine->scriptFunctions[objType->beh.factories[i]];
|
|
|
|
if( f == 0 ||
|
|
|
|
!func.IsSignatureExceptNameAndObjectTypeEqual(f) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
usedFunctions[n] = f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( func.name == "$list" )
|
|
|
|
{
|
|
|
|
// listFactory is used for both factory is global and returns a handle and constructor that is a method
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *objType = func.objectType ? func.objectType : CastToObjectType(func.returnType.GetTypeInfo());
|
2016-02-17 23:07:55 -05:00
|
|
|
if( objType )
|
|
|
|
{
|
|
|
|
asCScriptFunction *f = engine->scriptFunctions[objType->beh.listFactory];
|
|
|
|
if( f && func.IsSignatureExceptNameAndObjectTypeEqual(f) )
|
|
|
|
usedFunctions[n] = f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( func.name == "$beh2" )
|
|
|
|
{
|
|
|
|
// This is a destructor, so check the object type's destructor
|
|
|
|
asCObjectType *objType = func.objectType;
|
|
|
|
if( objType )
|
|
|
|
{
|
|
|
|
asCScriptFunction *f = engine->scriptFunctions[objType->beh.destruct];
|
|
|
|
if( f && func.IsSignatureExceptNameAndObjectTypeEqual(f) )
|
|
|
|
usedFunctions[n] = f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( func.name == "$beh4" )
|
|
|
|
{
|
|
|
|
// This is a list factory, so check the return type's list factory
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *objType = CastToObjectType(func.returnType.GetTypeInfo());
|
2016-02-17 23:07:55 -05:00
|
|
|
if( objType )
|
|
|
|
{
|
|
|
|
asCScriptFunction *f = engine->scriptFunctions[objType->beh.listFactory];
|
|
|
|
if( f && func.IsSignatureExceptNameAndObjectTypeEqual(f) )
|
|
|
|
usedFunctions[n] = f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( func.name == "$dlgte" )
|
|
|
|
{
|
|
|
|
// This is the delegate factory
|
|
|
|
asCScriptFunction *f = engine->registeredGlobalFuncs.GetFirst(engine->nameSpaces[0], DELEGATE_FACTORY);
|
|
|
|
asASSERT( f && func.IsSignatureEqual(f) );
|
|
|
|
usedFunctions[n] = f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( func.objectType == 0 )
|
|
|
|
{
|
|
|
|
// This is a global function
|
|
|
|
const asCArray<asUINT> &funcs = engine->registeredGlobalFuncs.GetIndexes(func.nameSpace, func.name);
|
|
|
|
for( asUINT i = 0; i < funcs.GetLength(); i++ )
|
|
|
|
{
|
|
|
|
asCScriptFunction *f = engine->registeredGlobalFuncs.Get(funcs[i]);
|
|
|
|
if( f == 0 ||
|
|
|
|
!func.IsSignatureExceptNameAndObjectTypeEqual(f) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
usedFunctions[n] = f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( func.objectType )
|
|
|
|
{
|
|
|
|
// It is a class member, so we can search directly in the object type's members
|
|
|
|
// TODO: virtual function is different that implemented method
|
|
|
|
for( asUINT i = 0; i < func.objectType->methods.GetLength(); i++ )
|
|
|
|
{
|
|
|
|
asCScriptFunction *f = engine->scriptFunctions[func.objectType->methods[i]];
|
|
|
|
if( f == 0 ||
|
|
|
|
!func.IsSignatureEqual(f) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
usedFunctions[n] = f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( usedFunctions[n] == 0 )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// TODO: clean up: This part of the code should never happen. All functions should
|
|
|
|
// be found in the above logic. The only valid reason to come here
|
2016-02-17 23:07:55 -05:00
|
|
|
// is if the bytecode is wrong and the function doesn't exist anyway.
|
2017-11-20 08:02:33 -05:00
|
|
|
// This loop is kept temporarily until we can be certain all scenarios
|
2016-02-17 23:07:55 -05:00
|
|
|
// are covered.
|
|
|
|
for( asUINT i = 0; i < engine->scriptFunctions.GetLength(); i++ )
|
|
|
|
{
|
|
|
|
asCScriptFunction *f = engine->scriptFunctions[i];
|
|
|
|
if( f == 0 ||
|
|
|
|
func.objectType != f->objectType ||
|
|
|
|
func.nameSpace != f->nameSpace ||
|
|
|
|
!func.IsSignatureEqual(f) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
usedFunctions[n] = f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No function is expected to be found
|
|
|
|
asASSERT(usedFunctions[n] == 0);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the type to dummy so it won't try to release the id
|
|
|
|
func.funcType = asFUNC_DUMMY;
|
|
|
|
|
|
|
|
if( usedFunctions[n] == 0 )
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
void asCReader::ReadFunctionSignature(asCScriptFunction *func, asCObjectType **parentClass)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
asUINT i, count;
|
|
|
|
asCDataType dt;
|
|
|
|
int num;
|
|
|
|
|
|
|
|
ReadString(&func->name);
|
|
|
|
if( func->name == DELEGATE_FACTORY )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// It's not necessary to read anymore, everything is known
|
2014-10-27 18:43:09 -04:00
|
|
|
asCScriptFunction *f = engine->registeredGlobalFuncs.GetFirst(engine->nameSpaces[0], DELEGATE_FACTORY);
|
|
|
|
asASSERT( f );
|
|
|
|
func->returnType = f->returnType;
|
|
|
|
func->parameterTypes = f->parameterTypes;
|
|
|
|
func->inOutFlags = f->inOutFlags;
|
|
|
|
func->funcType = f->funcType;
|
|
|
|
func->defaultArgs = f->defaultArgs;
|
|
|
|
func->nameSpace = f->nameSpace;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadDataType(&func->returnType);
|
|
|
|
|
|
|
|
count = ReadEncodedUInt();
|
|
|
|
if( count > 256 )
|
|
|
|
{
|
|
|
|
// Too many arguments, must be something wrong in the file
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
func->parameterTypes.Allocate(count, false);
|
2017-11-20 08:02:33 -05:00
|
|
|
for( i = 0; i < count; ++i )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
ReadDataType(&dt);
|
|
|
|
func->parameterTypes.PushLast(dt);
|
|
|
|
}
|
|
|
|
|
|
|
|
func->inOutFlags.SetLength(func->parameterTypes.GetLength());
|
2015-05-11 19:40:43 -04:00
|
|
|
if( func->inOutFlags.GetLength() != func->parameterTypes.GetLength() )
|
|
|
|
{
|
|
|
|
// Out of memory
|
|
|
|
error = true;
|
|
|
|
return;
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
memset(func->inOutFlags.AddressOf(), 0, sizeof(asETypeModifiers)*func->inOutFlags.GetLength());
|
|
|
|
count = ReadEncodedUInt();
|
|
|
|
if( count > func->parameterTypes.GetLength() )
|
|
|
|
{
|
|
|
|
// Cannot be more than the number of arguments
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for( i = 0; i < count; ++i )
|
|
|
|
{
|
|
|
|
num = ReadEncodedUInt();
|
|
|
|
func->inOutFlags[i] = static_cast<asETypeModifiers>(num);
|
|
|
|
}
|
|
|
|
|
|
|
|
func->funcType = (asEFuncType)ReadEncodedUInt();
|
|
|
|
|
|
|
|
// Read the default args, from last to first
|
|
|
|
count = ReadEncodedUInt();
|
|
|
|
if( count > func->parameterTypes.GetLength() )
|
|
|
|
{
|
|
|
|
// Cannot be more than the number of arguments
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if( count )
|
|
|
|
{
|
|
|
|
func->defaultArgs.SetLength(func->parameterTypes.GetLength());
|
2015-05-11 19:40:43 -04:00
|
|
|
if( func->defaultArgs.GetLength() != func->parameterTypes.GetLength() )
|
|
|
|
{
|
|
|
|
// Out of memory
|
|
|
|
error = true;
|
|
|
|
return;
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
memset(func->defaultArgs.AddressOf(), 0, sizeof(asCString*)*func->defaultArgs.GetLength());
|
|
|
|
for( i = 0; i < count; i++ )
|
|
|
|
{
|
|
|
|
asCString *str = asNEW(asCString);
|
|
|
|
if( str == 0 )
|
|
|
|
{
|
|
|
|
// Out of memory
|
|
|
|
error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
func->defaultArgs[func->defaultArgs.GetLength()-1-i] = str;
|
|
|
|
ReadString(str);
|
|
|
|
}
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
|
|
|
|
func->objectType = CastToObjectType(ReadTypeInfo());
|
2014-10-27 18:43:09 -04:00
|
|
|
if( func->objectType )
|
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
func->objectType->AddRefInternal();
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
asBYTE b;
|
|
|
|
ReadData(&b, 1);
|
2017-11-20 08:02:33 -05:00
|
|
|
func->SetReadOnly((b & 1) ? true : false);
|
|
|
|
func->SetPrivate((b & 2) ? true : false);
|
|
|
|
func->SetProtected((b & 4) ? true : false);
|
2014-10-27 18:43:09 -04:00
|
|
|
func->nameSpace = engine->nameSpaces[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
if (func->funcType == asFUNC_FUNCDEF)
|
|
|
|
{
|
|
|
|
asBYTE b;
|
|
|
|
ReadData(&b, 1);
|
|
|
|
if (b == 'n')
|
|
|
|
{
|
|
|
|
asCString ns;
|
|
|
|
ReadString(&ns);
|
|
|
|
func->nameSpace = engine->AddNameSpace(ns.AddressOf());
|
|
|
|
}
|
|
|
|
else if (b == 'o')
|
|
|
|
{
|
|
|
|
func->nameSpace = 0;
|
|
|
|
if (parentClass)
|
|
|
|
*parentClass = CastToObjectType(ReadTypeInfo());
|
|
|
|
else
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asCString ns;
|
|
|
|
ReadString(&ns);
|
|
|
|
func->nameSpace = engine->AddNameSpace(ns.AddressOf());
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
asCScriptFunction *asCReader::ReadFunction(bool &isNew, bool addToModule, bool addToEngine, bool addToGC, bool *isExternal)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
isNew = false;
|
2017-11-20 08:02:33 -05:00
|
|
|
if (isExternal) *isExternal = false;
|
2014-10-27 18:43:09 -04:00
|
|
|
if( error ) return 0;
|
|
|
|
|
|
|
|
char c;
|
|
|
|
ReadData(&c, 1);
|
|
|
|
|
|
|
|
if( c == '\0' )
|
|
|
|
{
|
|
|
|
// There is no function, so return a null pointer
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( c == 'r' )
|
|
|
|
{
|
|
|
|
// This is a reference to a previously saved function
|
|
|
|
asUINT index = ReadEncodedUInt();
|
|
|
|
if( index < savedFunctions.GetLength() )
|
|
|
|
return savedFunctions[index];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the new function
|
|
|
|
isNew = true;
|
2015-05-11 19:40:43 -04:00
|
|
|
asCScriptFunction *func = asNEW(asCScriptFunction)(engine,0,asFUNC_DUMMY);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( func == 0 )
|
|
|
|
{
|
|
|
|
// Out of memory
|
|
|
|
error = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
savedFunctions.PushLast(func);
|
|
|
|
|
|
|
|
int i, count;
|
|
|
|
asCDataType dt;
|
|
|
|
int num;
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *parentClass = 0;
|
|
|
|
ReadFunctionSignature(func, &parentClass);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( error )
|
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
func->DestroyHalfCreated();
|
2014-10-27 18:43:09 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( func->funcType == asFUNC_SCRIPT )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
char bits;
|
|
|
|
ReadData(&bits, 1);
|
|
|
|
func->SetShared((bits & 1) ? true : false);
|
|
|
|
func->dontCleanUpOnException = (bits & 2) ? true : false;
|
|
|
|
if ((bits & 4) && isExternal)
|
|
|
|
*isExternal = true;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// for external shared functions the rest is not needed
|
|
|
|
if (!(bits & 4))
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
func->AllocateScriptFunctionData();
|
|
|
|
if (func->scriptData == 0)
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
|
|
|
// Out of memory
|
|
|
|
error = true;
|
|
|
|
func->DestroyHalfCreated();
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if (addToGC && !addToModule)
|
|
|
|
engine->gc.AddScriptObjectToGC(func, &engine->functionBehaviours);
|
|
|
|
|
|
|
|
ReadByteCode(func);
|
|
|
|
|
|
|
|
func->scriptData->variableSpace = ReadEncodedUInt();
|
|
|
|
|
|
|
|
count = ReadEncodedUInt();
|
|
|
|
func->scriptData->objVariablePos.Allocate(count, false);
|
|
|
|
func->scriptData->objVariableTypes.Allocate(count, false);
|
|
|
|
for (i = 0; i < count; ++i)
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
func->scriptData->objVariableTypes.PushLast(ReadTypeInfo());
|
|
|
|
num = ReadEncodedUInt();
|
|
|
|
func->scriptData->objVariablePos.PushLast(num);
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
// No need to continue (the error has already been reported before)
|
|
|
|
func->DestroyHalfCreated();
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-11 19:40:43 -04:00
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
if (count > 0)
|
|
|
|
func->scriptData->objVariablesOnHeap = ReadEncodedUInt();
|
|
|
|
else
|
|
|
|
func->scriptData->objVariablesOnHeap = 0;
|
|
|
|
|
|
|
|
int length = ReadEncodedUInt();
|
|
|
|
func->scriptData->objVariableInfo.SetLength(length);
|
|
|
|
for (i = 0; i < length; ++i)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
func->scriptData->objVariableInfo[i].programPos = ReadEncodedUInt();
|
|
|
|
func->scriptData->objVariableInfo[i].variableOffset = ReadEncodedUInt();
|
|
|
|
asEObjVarInfoOption option = (asEObjVarInfoOption)ReadEncodedUInt();
|
|
|
|
func->scriptData->objVariableInfo[i].option = option;
|
|
|
|
if (option != asOBJ_INIT && option != asOBJ_UNINIT && option != asBLOCK_BEGIN && option != asBLOCK_END)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
error = true;
|
|
|
|
func->DestroyHalfCreated();
|
|
|
|
return 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if (!noDebugInfo)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
length = ReadEncodedUInt();
|
|
|
|
func->scriptData->lineNumbers.SetLength(length);
|
|
|
|
if (int(func->scriptData->lineNumbers.GetLength()) != length)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
// Out of memory
|
|
|
|
error = true;
|
2015-05-11 19:40:43 -04:00
|
|
|
func->DestroyHalfCreated();
|
2014-10-27 18:43:09 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
for (i = 0; i < length; ++i)
|
|
|
|
func->scriptData->lineNumbers[i] = ReadEncodedUInt();
|
2015-05-11 19:40:43 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Read the array of script sections
|
|
|
|
length = ReadEncodedUInt();
|
|
|
|
func->scriptData->sectionIdxs.SetLength(length);
|
|
|
|
if (int(func->scriptData->sectionIdxs.GetLength()) != length)
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// Out of memory
|
|
|
|
error = true;
|
2015-05-11 19:40:43 -04:00
|
|
|
func->DestroyHalfCreated();
|
|
|
|
return 0;
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
for (i = 0; i < length; ++i)
|
|
|
|
{
|
|
|
|
if ((i & 1) == 0)
|
|
|
|
func->scriptData->sectionIdxs[i] = ReadEncodedUInt();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asCString str;
|
|
|
|
ReadString(&str);
|
|
|
|
func->scriptData->sectionIdxs[i] = engine->GetScriptSectionNameIndex(str.AddressOf());
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Read the variable information
|
|
|
|
if (!noDebugInfo)
|
|
|
|
{
|
|
|
|
length = ReadEncodedUInt();
|
|
|
|
func->scriptData->variables.Allocate(length, false);
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
asSScriptVariable *var = asNEW(asSScriptVariable);
|
|
|
|
if (var == 0)
|
|
|
|
{
|
|
|
|
// Out of memory
|
|
|
|
error = true;
|
|
|
|
func->DestroyHalfCreated();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
func->scriptData->variables.PushLast(var);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
var->declaredAtProgramPos = ReadEncodedUInt();
|
|
|
|
var->stackOffset = ReadEncodedUInt();
|
|
|
|
ReadString(&var->name);
|
|
|
|
ReadDataType(&var->type);
|
2015-05-11 19:40:43 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
// No need to continue (the error has already been reported before)
|
|
|
|
func->DestroyHalfCreated();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read script section name
|
|
|
|
if (!noDebugInfo)
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCString name;
|
|
|
|
ReadString(&name);
|
|
|
|
func->scriptData->scriptSectionIdx = engine->GetScriptSectionNameIndex(name.AddressOf());
|
|
|
|
func->scriptData->declaredAt = ReadEncodedUInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read parameter names
|
|
|
|
if (!noDebugInfo)
|
|
|
|
{
|
|
|
|
asUINT countParam = asUINT(ReadEncodedUInt64());
|
|
|
|
if (countParam > func->parameterTypes.GetLength())
|
|
|
|
{
|
|
|
|
error = true;
|
|
|
|
func->DestroyHalfCreated();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
func->parameterNames.SetLength(countParam);
|
|
|
|
for (asUINT n = 0; n < countParam; n++)
|
|
|
|
ReadString(&func->parameterNames[n]);
|
2015-05-11 19:40:43 -04:00
|
|
|
}
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( func->funcType == asFUNC_VIRTUAL || func->funcType == asFUNC_INTERFACE )
|
|
|
|
{
|
|
|
|
func->vfTableIdx = ReadEncodedUInt();
|
|
|
|
}
|
2016-02-17 23:07:55 -05:00
|
|
|
else if( func->funcType == asFUNC_FUNCDEF )
|
|
|
|
{
|
|
|
|
asBYTE bits;
|
|
|
|
ReadData(&bits, 1);
|
2017-11-20 08:02:33 -05:00
|
|
|
if( bits & 1 )
|
|
|
|
func->SetShared(true);
|
|
|
|
if ((bits & 2) && isExternal)
|
|
|
|
*isExternal = true;
|
|
|
|
|
|
|
|
// The asCFuncdefType constructor adds itself to the func->funcdefType member
|
|
|
|
asCFuncdefType *fdt = asNEW(asCFuncdefType)(engine, func);
|
|
|
|
fdt->parentClass = parentClass;
|
2016-02-17 23:07:55 -05:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
if( addToModule )
|
|
|
|
{
|
|
|
|
// The refCount is already 1
|
|
|
|
module->scriptFunctions.PushLast(func);
|
2015-05-11 19:40:43 -04:00
|
|
|
func->module = module;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
if( addToEngine )
|
|
|
|
{
|
|
|
|
func->id = engine->GetNextScriptFunctionId();
|
2015-05-11 19:40:43 -04:00
|
|
|
engine->AddScriptFunction(func);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
if( func->objectType )
|
|
|
|
func->ComputeSignatureId();
|
|
|
|
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
void asCReader::ReadTypeDeclaration(asCTypeInfo *type, int phase, bool *isExternal)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
if( phase == 1 )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asASSERT(isExternal);
|
|
|
|
if (isExternal)
|
|
|
|
*isExternal = false;
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Read the initial attributes
|
2017-11-20 08:02:33 -05:00
|
|
|
ReadString(&type->name);
|
|
|
|
ReadData(&type->flags, 4);
|
|
|
|
type->size = ReadEncodedUInt();
|
2014-10-27 18:43:09 -04:00
|
|
|
asCString ns;
|
|
|
|
ReadString(&ns);
|
2017-11-20 08:02:33 -05:00
|
|
|
type->nameSpace = engine->AddNameSpace(ns.AddressOf());
|
|
|
|
|
|
|
|
// Verify that the flags match the asCTypeInfo
|
|
|
|
if ((CastToEnumType(type) && !(type->flags & asOBJ_ENUM)) ||
|
|
|
|
(CastToFuncdefType(type) && !(type->flags & asOBJ_FUNCDEF)) ||
|
|
|
|
(CastToObjectType(type) && !(type->flags & (asOBJ_REF | asOBJ_VALUE))))
|
|
|
|
{
|
|
|
|
error = true;
|
|
|
|
return;
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Reset the size of script classes, since it will be recalculated as properties are added
|
2017-11-20 08:02:33 -05:00
|
|
|
if( (type->flags & asOBJ_SCRIPT_OBJECT) && type->size != 0 )
|
|
|
|
type->size = sizeof(asCScriptObject);
|
|
|
|
|
|
|
|
asCObjectType *ot = CastToObjectType(type);
|
|
|
|
if (ot)
|
|
|
|
{
|
|
|
|
// Use the default script class behaviours
|
|
|
|
ot->beh = engine->scriptTypeBehaviours.beh;
|
|
|
|
ot->beh.construct = 0;
|
|
|
|
ot->beh.factory = 0;
|
|
|
|
ot->beh.constructors.PopLast(); // These will be read from the file
|
|
|
|
ot->beh.factories.PopLast(); // These will be read from the file
|
|
|
|
engine->scriptFunctions[ot->beh.addref]->AddRefInternal();
|
|
|
|
engine->scriptFunctions[ot->beh.release]->AddRefInternal();
|
|
|
|
engine->scriptFunctions[ot->beh.gcEnumReferences]->AddRefInternal();
|
|
|
|
engine->scriptFunctions[ot->beh.gcGetFlag]->AddRefInternal();
|
|
|
|
engine->scriptFunctions[ot->beh.gcGetRefCount]->AddRefInternal();
|
|
|
|
engine->scriptFunctions[ot->beh.gcReleaseAllReferences]->AddRefInternal();
|
|
|
|
engine->scriptFunctions[ot->beh.gcSetFlag]->AddRefInternal();
|
|
|
|
engine->scriptFunctions[ot->beh.copy]->AddRefInternal();
|
|
|
|
// TODO: weak: Should not do this if the class has been declared with 'noweak'
|
|
|
|
engine->scriptFunctions[ot->beh.getWeakRefFlag]->AddRefInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
// external shared flag
|
|
|
|
if (type->flags & asOBJ_SHARED)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
ReadData(&c, 1);
|
|
|
|
if (c == 'e')
|
|
|
|
*isExternal = true;
|
|
|
|
else if (c != ' ')
|
|
|
|
{
|
|
|
|
error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( phase == 2 )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// external shared types doesn't store this
|
|
|
|
if ((type->flags & asOBJ_SHARED) && module->externalTypes.IndexOf(type) >= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( type->flags & asOBJ_ENUM )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCEnumType *t = CastToEnumType(type);
|
2014-10-27 18:43:09 -04:00
|
|
|
int count = ReadEncodedUInt();
|
2017-11-20 08:02:33 -05:00
|
|
|
bool sharedExists = existingShared.MoveTo(0, type);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( !sharedExists )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
t->enumValues.Allocate(count, false);
|
2014-10-27 18:43:09 -04:00
|
|
|
for( int n = 0; n < count; n++ )
|
|
|
|
{
|
|
|
|
asSEnumValue *e = asNEW(asSEnumValue);
|
|
|
|
if( e == 0 )
|
|
|
|
{
|
|
|
|
// Out of memory
|
|
|
|
error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ReadString(&e->name);
|
|
|
|
ReadData(&e->value, 4); // TODO: Should be encoded
|
2017-11-20 08:02:33 -05:00
|
|
|
t->enumValues.PushLast(e);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Verify that the enum values exists in the original
|
|
|
|
asCString name;
|
|
|
|
int value;
|
|
|
|
for( int n = 0; n < count; n++ )
|
|
|
|
{
|
|
|
|
ReadString(&name);
|
|
|
|
ReadData(&value, 4); // TODO: Should be encoded
|
|
|
|
bool found = false;
|
2017-11-20 08:02:33 -05:00
|
|
|
for( asUINT e = 0; e < t->enumValues.GetLength(); e++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
if( t->enumValues[e]->name == name &&
|
|
|
|
t->enumValues[e]->value == value )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( !found )
|
|
|
|
{
|
|
|
|
asCString str;
|
2017-11-20 08:02:33 -05:00
|
|
|
str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, type->GetName());
|
2014-10-27 18:43:09 -04:00
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
else if( type->flags & asOBJ_TYPEDEF )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCTypedefType *td = CastToTypedefType(type);
|
|
|
|
asASSERT(td);
|
2014-10-27 18:43:09 -04:00
|
|
|
eTokenType t = (eTokenType)ReadEncodedUInt();
|
2017-11-20 08:02:33 -05:00
|
|
|
td->aliasForType = asCDataType::CreatePrimitive(t, false);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *ot = CastToObjectType(type);
|
|
|
|
asASSERT(ot);
|
|
|
|
|
|
|
|
// If the type is shared and pre-existing, we should just
|
|
|
|
// validate that the loaded methods match the original
|
|
|
|
bool sharedExists = existingShared.MoveTo(0, type);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( sharedExists )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *dt = CastToObjectType(ReadTypeInfo());
|
2014-10-27 18:43:09 -04:00
|
|
|
if( ot->derivedFrom != dt )
|
|
|
|
{
|
|
|
|
asCString str;
|
2017-11-20 08:02:33 -05:00
|
|
|
str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, type->GetName());
|
2014-10-27 18:43:09 -04:00
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
ot->derivedFrom = CastToObjectType(ReadTypeInfo());
|
2014-10-27 18:43:09 -04:00
|
|
|
if( ot->derivedFrom )
|
2015-05-11 19:40:43 -04:00
|
|
|
ot->derivedFrom->AddRefInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// interfaces[] / interfaceVFTOffsets[]
|
|
|
|
int size = ReadEncodedUInt();
|
|
|
|
if( sharedExists )
|
|
|
|
{
|
|
|
|
for( int n = 0; n < size; n++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *intf = CastToObjectType(ReadTypeInfo());
|
2014-10-27 18:43:09 -04:00
|
|
|
ReadEncodedUInt();
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if( !type->Implements(intf) )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
asCString str;
|
2017-11-20 08:02:33 -05:00
|
|
|
str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, type->GetName());
|
2014-10-27 18:43:09 -04:00
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ot->interfaces.Allocate(size, false);
|
|
|
|
ot->interfaceVFTOffsets.Allocate(size, false);
|
|
|
|
for( int n = 0; n < size; n++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *intf = CastToObjectType(ReadTypeInfo());
|
2014-10-27 18:43:09 -04:00
|
|
|
ot->interfaces.PushLast(intf);
|
|
|
|
|
|
|
|
asUINT offset = ReadEncodedUInt();
|
|
|
|
ot->interfaceVFTOffsets.PushLast(offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// behaviours
|
2017-11-20 08:02:33 -05:00
|
|
|
if( !ot->IsInterface() && type->flags != asOBJ_TYPEDEF && type->flags != asOBJ_ENUM )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
bool isNew;
|
|
|
|
asCScriptFunction *func = ReadFunction(isNew, !sharedExists, !sharedExists, !sharedExists);
|
|
|
|
if( sharedExists )
|
|
|
|
{
|
|
|
|
// Find the real function in the object, and update the savedFunctions array
|
|
|
|
asCScriptFunction *realFunc = engine->GetScriptFunction(ot->beh.destruct);
|
|
|
|
if( (realFunc == 0 && func == 0) || realFunc->IsSignatureEqual(func) )
|
|
|
|
{
|
|
|
|
// If the function is not the last, then the substitution has already occurred before
|
|
|
|
if( func && savedFunctions[savedFunctions.GetLength()-1] == func )
|
|
|
|
savedFunctions[savedFunctions.GetLength()-1] = realFunc;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asCString str;
|
2017-11-20 08:02:33 -05:00
|
|
|
str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, type->GetName());
|
2014-10-27 18:43:09 -04:00
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
if( func )
|
|
|
|
{
|
|
|
|
if( isNew )
|
|
|
|
{
|
|
|
|
// Destroy the function without releasing any references
|
|
|
|
func->id = 0;
|
|
|
|
func->scriptData->byteCode.SetLength(0);
|
2015-05-11 19:40:43 -04:00
|
|
|
func->ReleaseInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
module->scriptFunctions.PushLast(realFunc);
|
2015-05-11 19:40:43 -04:00
|
|
|
realFunc->AddRefInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
dontTranslate.Insert(realFunc, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( func )
|
|
|
|
{
|
|
|
|
ot->beh.destruct = func->id;
|
2015-05-11 19:40:43 -04:00
|
|
|
func->AddRefInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ot->beh.destruct = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = ReadEncodedUInt();
|
|
|
|
for( int n = 0; n < size; n++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
func = ReadFunction(isNew, !sharedExists, !sharedExists, !sharedExists);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( func )
|
|
|
|
{
|
|
|
|
if( sharedExists )
|
|
|
|
{
|
|
|
|
// Find the real function in the object, and update the savedFunctions array
|
|
|
|
bool found = false;
|
2017-11-20 08:02:33 -05:00
|
|
|
for( asUINT f = 0; f < ot->beh.constructors.GetLength(); f++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCScriptFunction *realFunc = engine->GetScriptFunction(ot->beh.constructors[f]);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( realFunc->IsSignatureEqual(func) )
|
|
|
|
{
|
|
|
|
// If the function is not the last, then the substitution has already occurred before
|
|
|
|
if( savedFunctions[savedFunctions.GetLength()-1] == func )
|
|
|
|
savedFunctions[savedFunctions.GetLength()-1] = realFunc;
|
|
|
|
found = true;
|
|
|
|
module->scriptFunctions.PushLast(realFunc);
|
2015-05-11 19:40:43 -04:00
|
|
|
realFunc->AddRefInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
dontTranslate.Insert(realFunc, true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( !found )
|
|
|
|
{
|
|
|
|
asCString str;
|
2017-11-20 08:02:33 -05:00
|
|
|
str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, type->GetName());
|
2014-10-27 18:43:09 -04:00
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
if( isNew )
|
|
|
|
{
|
|
|
|
// Destroy the function without releasing any references
|
|
|
|
func->id = 0;
|
|
|
|
func->scriptData->byteCode.SetLength(0);
|
2015-05-11 19:40:43 -04:00
|
|
|
func->ReleaseInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ot->beh.constructors.PushLast(func->id);
|
2015-05-11 19:40:43 -04:00
|
|
|
func->AddRefInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
if( func->parameterTypes.GetLength() == 0 )
|
|
|
|
ot->beh.construct = func->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
|
|
|
|
func = ReadFunction(isNew, !sharedExists, !sharedExists, !sharedExists);
|
|
|
|
if( func )
|
|
|
|
{
|
|
|
|
if( sharedExists )
|
|
|
|
{
|
|
|
|
// Find the real function in the object, and update the savedFunctions array
|
|
|
|
bool found = false;
|
2017-11-20 08:02:33 -05:00
|
|
|
for( asUINT f = 0; f < ot->beh.factories.GetLength(); f++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCScriptFunction *realFunc = engine->GetScriptFunction(ot->beh.factories[f]);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( realFunc->IsSignatureEqual(func) )
|
|
|
|
{
|
|
|
|
// If the function is not the last, then the substitution has already occurred before
|
|
|
|
if( savedFunctions[savedFunctions.GetLength()-1] == func )
|
|
|
|
savedFunctions[savedFunctions.GetLength()-1] = realFunc;
|
|
|
|
found = true;
|
|
|
|
module->scriptFunctions.PushLast(realFunc);
|
2015-05-11 19:40:43 -04:00
|
|
|
realFunc->AddRefInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
dontTranslate.Insert(realFunc, true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( !found )
|
|
|
|
{
|
|
|
|
asCString str;
|
2017-11-20 08:02:33 -05:00
|
|
|
str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, type->GetName());
|
2014-10-27 18:43:09 -04:00
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
if( isNew )
|
|
|
|
{
|
|
|
|
// Destroy the function without releasing any references
|
|
|
|
func->id = 0;
|
|
|
|
func->scriptData->byteCode.SetLength(0);
|
2015-05-11 19:40:43 -04:00
|
|
|
func->ReleaseInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ot->beh.factories.PushLast(func->id);
|
2015-05-11 19:40:43 -04:00
|
|
|
func->AddRefInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
if( func->parameterTypes.GetLength() == 0 )
|
|
|
|
ot->beh.factory = func->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// methods[]
|
|
|
|
size = ReadEncodedUInt();
|
|
|
|
int n;
|
2017-11-20 08:02:33 -05:00
|
|
|
for( n = 0; n < size; n++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
bool isNew;
|
|
|
|
asCScriptFunction *func = ReadFunction(isNew, !sharedExists, !sharedExists, !sharedExists);
|
|
|
|
if( func )
|
|
|
|
{
|
|
|
|
if( sharedExists )
|
|
|
|
{
|
|
|
|
// Find the real function in the object, and update the savedFunctions array
|
|
|
|
bool found = false;
|
2017-11-20 08:02:33 -05:00
|
|
|
for( asUINT f = 0; f < ot->methods.GetLength(); f++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCScriptFunction *realFunc = engine->GetScriptFunction(ot->methods[f]);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( realFunc->IsSignatureEqual(func) )
|
|
|
|
{
|
|
|
|
// If the function is not the last, then the substitution has already occurred before
|
|
|
|
if( savedFunctions[savedFunctions.GetLength()-1] == func )
|
|
|
|
savedFunctions[savedFunctions.GetLength()-1] = realFunc;
|
|
|
|
found = true;
|
|
|
|
module->scriptFunctions.PushLast(realFunc);
|
2015-05-11 19:40:43 -04:00
|
|
|
realFunc->AddRefInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
dontTranslate.Insert(realFunc, true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( !found )
|
|
|
|
{
|
|
|
|
asCString str;
|
2017-11-20 08:02:33 -05:00
|
|
|
str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, type->GetName());
|
2014-10-27 18:43:09 -04:00
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
if( isNew )
|
|
|
|
{
|
|
|
|
// Destroy the function without releasing any references
|
|
|
|
func->id = 0;
|
|
|
|
if( func->scriptData )
|
|
|
|
func->scriptData->byteCode.SetLength(0);
|
2015-05-11 19:40:43 -04:00
|
|
|
func->ReleaseInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If the method is the assignment operator we need to replace the default implementation
|
|
|
|
if( func->name == "opAssign" && func->parameterTypes.GetLength() == 1 &&
|
2017-11-20 08:02:33 -05:00
|
|
|
func->parameterTypes[0].GetTypeInfo() == func->objectType &&
|
2014-10-27 18:43:09 -04:00
|
|
|
(func->inOutFlags[0] & asTM_INREF) )
|
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
engine->scriptFunctions[ot->beh.copy]->ReleaseInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
ot->beh.copy = func->id;
|
2015-05-11 19:40:43 -04:00
|
|
|
func->AddRefInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
ot->methods.PushLast(func->id);
|
2015-05-11 19:40:43 -04:00
|
|
|
func->AddRefInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// virtualFunctionTable[]
|
|
|
|
size = ReadEncodedUInt();
|
|
|
|
for( n = 0; n < size; n++ )
|
|
|
|
{
|
|
|
|
bool isNew;
|
|
|
|
asCScriptFunction *func = ReadFunction(isNew, !sharedExists, !sharedExists, !sharedExists);
|
|
|
|
if( func )
|
|
|
|
{
|
|
|
|
if( sharedExists )
|
|
|
|
{
|
|
|
|
// Find the real function in the object, and update the savedFunctions array
|
|
|
|
bool found = false;
|
2017-11-20 08:02:33 -05:00
|
|
|
for( asUINT f = 0; f < ot->virtualFunctionTable.GetLength(); f++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCScriptFunction *realFunc = ot->virtualFunctionTable[f];
|
2014-10-27 18:43:09 -04:00
|
|
|
if( realFunc->IsSignatureEqual(func) )
|
|
|
|
{
|
|
|
|
// If the function is not the last, then the substitution has already occurred before
|
|
|
|
if( savedFunctions[savedFunctions.GetLength()-1] == func )
|
|
|
|
savedFunctions[savedFunctions.GetLength()-1] = realFunc;
|
|
|
|
found = true;
|
|
|
|
module->scriptFunctions.PushLast(realFunc);
|
2015-05-11 19:40:43 -04:00
|
|
|
realFunc->AddRefInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
dontTranslate.Insert(realFunc, true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( !found )
|
|
|
|
{
|
|
|
|
asCString str;
|
2017-11-20 08:02:33 -05:00
|
|
|
str.Format(TXT_SHARED_s_DOESNT_MATCH_ORIGINAL, type->GetName());
|
2014-10-27 18:43:09 -04:00
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
if( isNew )
|
|
|
|
{
|
|
|
|
// Destroy the function without releasing any references
|
|
|
|
func->id = 0;
|
|
|
|
if( func->scriptData )
|
|
|
|
func->scriptData->byteCode.SetLength(0);
|
2015-05-11 19:40:43 -04:00
|
|
|
func->ReleaseInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ot->virtualFunctionTable.PushLast(func);
|
2015-05-11 19:40:43 -04:00
|
|
|
func->AddRefInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( phase == 3 )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// external shared types doesn't store this
|
|
|
|
if ((type->flags & asOBJ_SHARED) && module->externalTypes.IndexOf(type) >= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
asCObjectType *ot = CastToObjectType(type);
|
|
|
|
|
|
|
|
// This is only done for object types
|
|
|
|
asASSERT(ot);
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// properties[]
|
|
|
|
asUINT size = ReadEncodedUInt();
|
|
|
|
for( asUINT n = 0; n < size; n++ )
|
|
|
|
ReadObjectProperty(ot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
asWORD asCReader::ReadEncodedUInt16()
|
|
|
|
{
|
|
|
|
asDWORD dw = ReadEncodedUInt();
|
|
|
|
if( (dw>>16) != 0 && (dw>>16) != 0xFFFF )
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
|
|
|
|
return asWORD(dw & 0xFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
asUINT asCReader::ReadEncodedUInt()
|
|
|
|
{
|
|
|
|
asQWORD qw = ReadEncodedUInt64();
|
|
|
|
if( (qw>>32) != 0 && (qw>>32) != 0xFFFFFFFF )
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
|
|
|
|
return asUINT(qw & 0xFFFFFFFFu);
|
|
|
|
}
|
|
|
|
|
|
|
|
asQWORD asCReader::ReadEncodedUInt64()
|
|
|
|
{
|
|
|
|
asQWORD i = 0;
|
|
|
|
asBYTE b;
|
|
|
|
ReadData(&b, 1);
|
|
|
|
bool isNegative = ( b & 0x80 ) ? true : false;
|
|
|
|
b &= 0x7F;
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
if( (b & 0x7F) == 0x7F )
|
|
|
|
{
|
|
|
|
ReadData(&b, 1); i = asQWORD(b) << 56;
|
|
|
|
ReadData(&b, 1); i += asQWORD(b) << 48;
|
|
|
|
ReadData(&b, 1); i += asQWORD(b) << 40;
|
|
|
|
ReadData(&b, 1); i += asQWORD(b) << 32;
|
|
|
|
ReadData(&b, 1); i += asUINT(b) << 24;
|
|
|
|
ReadData(&b, 1); i += asUINT(b) << 16;
|
|
|
|
ReadData(&b, 1); i += asUINT(b) << 8;
|
|
|
|
ReadData(&b, 1); i += b;
|
|
|
|
}
|
|
|
|
else if( (b & 0x7E) == 0x7E )
|
|
|
|
{
|
|
|
|
i = asQWORD(b & 0x01) << 48;
|
|
|
|
ReadData(&b, 1); i += asQWORD(b) << 40;
|
|
|
|
ReadData(&b, 1); i += asQWORD(b) << 32;
|
|
|
|
ReadData(&b, 1); i += asUINT(b) << 24;
|
|
|
|
ReadData(&b, 1); i += asUINT(b) << 16;
|
|
|
|
ReadData(&b, 1); i += asUINT(b) << 8;
|
|
|
|
ReadData(&b, 1); i += b;
|
2017-11-20 08:02:33 -05:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
else if( (b & 0x7C) == 0x7C )
|
|
|
|
{
|
|
|
|
i = asQWORD(b & 0x03) << 40;
|
|
|
|
ReadData(&b, 1); i += asQWORD(b) << 32;
|
|
|
|
ReadData(&b, 1); i += asUINT(b) << 24;
|
|
|
|
ReadData(&b, 1); i += asUINT(b) << 16;
|
|
|
|
ReadData(&b, 1); i += asUINT(b) << 8;
|
|
|
|
ReadData(&b, 1); i += b;
|
2017-11-20 08:02:33 -05:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
else if( (b & 0x78) == 0x78 )
|
|
|
|
{
|
|
|
|
i = asQWORD(b & 0x07) << 32;
|
|
|
|
ReadData(&b, 1); i += asUINT(b) << 24;
|
|
|
|
ReadData(&b, 1); i += asUINT(b) << 16;
|
|
|
|
ReadData(&b, 1); i += asUINT(b) << 8;
|
|
|
|
ReadData(&b, 1); i += b;
|
2017-11-20 08:02:33 -05:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
else if( (b & 0x70) == 0x70 )
|
|
|
|
{
|
|
|
|
i = asUINT(b & 0x0F) << 24;
|
|
|
|
ReadData(&b, 1); i += asUINT(b) << 16;
|
|
|
|
ReadData(&b, 1); i += asUINT(b) << 8;
|
|
|
|
ReadData(&b, 1); i += b;
|
|
|
|
}
|
|
|
|
else if( (b & 0x60) == 0x60 )
|
|
|
|
{
|
|
|
|
i = asUINT(b & 0x1F) << 16;
|
|
|
|
ReadData(&b, 1); i += asUINT(b) << 8;
|
|
|
|
ReadData(&b, 1); i += b;
|
|
|
|
}
|
|
|
|
else if( (b & 0x40) == 0x40 )
|
|
|
|
{
|
|
|
|
i = asUINT(b & 0x3F) << 8;
|
|
|
|
ReadData(&b, 1); i += b;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i = b;
|
|
|
|
}
|
|
|
|
if( isNegative )
|
|
|
|
i = (asQWORD)(-asINT64(i));
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
void asCReader::ReadString(asCString* str)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
asUINT len = ReadEncodedUInt();
|
|
|
|
if( len & 1 )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
asUINT idx = len/2;
|
|
|
|
if( idx < savedStrings.GetLength() )
|
|
|
|
*str = savedStrings[idx];
|
|
|
|
else
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
2016-02-17 23:07:55 -05:00
|
|
|
else if( len > 0 )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
len /= 2;
|
2014-10-27 18:43:09 -04:00
|
|
|
str->SetLength(len);
|
2017-11-20 08:02:33 -05:00
|
|
|
int r = stream->Read(str->AddressOf(), len);
|
|
|
|
if (r < 0)
|
|
|
|
Error(TXT_UNEXPECTED_END_OF_FILE);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
savedStrings.PushLast(*str);
|
|
|
|
}
|
|
|
|
else
|
2016-02-17 23:07:55 -05:00
|
|
|
str->SetLength(0);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
void asCReader::ReadGlobalProperty()
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
asCString name;
|
|
|
|
asCDataType type;
|
|
|
|
|
|
|
|
ReadString(&name);
|
|
|
|
|
|
|
|
asCString ns;
|
|
|
|
ReadString(&ns);
|
|
|
|
asSNameSpace *nameSpace = engine->AddNameSpace(ns.AddressOf());
|
|
|
|
|
|
|
|
ReadDataType(&type);
|
|
|
|
|
|
|
|
asCGlobalProperty *prop = module->AllocateGlobalProperty(name.AddressOf(), type, nameSpace);
|
|
|
|
|
|
|
|
// Read the initialization function
|
2015-05-11 19:40:43 -04:00
|
|
|
bool isNew;
|
2017-11-20 08:02:33 -05:00
|
|
|
// Do not add the function to the GC at this time. It will
|
2015-05-11 19:40:43 -04:00
|
|
|
// only be added to the GC when the module releases the property
|
|
|
|
asCScriptFunction *func = ReadFunction(isNew, false, true, false);
|
|
|
|
if( func )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
// Make sure the function knows it is owned by the module
|
|
|
|
func->module = module;
|
|
|
|
|
|
|
|
prop->SetInitFunc(func);
|
|
|
|
func->ReleaseInternal();
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
void asCReader::ReadObjectProperty(asCObjectType *ot)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
asCString name;
|
|
|
|
ReadString(&name);
|
|
|
|
asCDataType dt;
|
|
|
|
ReadDataType(&dt);
|
2015-05-11 19:40:43 -04:00
|
|
|
int flags = ReadEncodedUInt();
|
|
|
|
bool isPrivate = (flags & 1) ? true : false;
|
|
|
|
bool isProtected = (flags & 2) ? true : false;
|
|
|
|
bool isInherited = (flags & 4) ? true : false;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// TODO: shared: If the type is shared and pre-existing, we should just
|
|
|
|
// validate that the loaded methods match the original
|
2014-10-27 18:43:09 -04:00
|
|
|
if( !existingShared.MoveTo(0, ot) )
|
2015-05-11 19:40:43 -04:00
|
|
|
ot->AddPropertyToClass(name, dt, isPrivate, isProtected, isInherited);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
void asCReader::ReadDataType(asCDataType *dt)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
// Check if this is a previously used type
|
2017-11-20 08:02:33 -05:00
|
|
|
asUINT idx = ReadEncodedUInt();
|
|
|
|
if( idx != 0 )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
// Get the datatype from the cache
|
2017-11-20 08:02:33 -05:00
|
|
|
*dt = savedDataTypes[idx-1];
|
2014-10-27 18:43:09 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
// Read the type definition
|
|
|
|
eTokenType tokenType = (eTokenType)ReadEncodedUInt();
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Reserve a spot in the savedDataTypes
|
2015-05-11 19:40:43 -04:00
|
|
|
asUINT saveSlot = savedDataTypes.GetLength();
|
2014-10-27 18:43:09 -04:00
|
|
|
savedDataTypes.PushLast(asCDataType());
|
|
|
|
|
|
|
|
// Read the datatype for the first time
|
2017-11-20 08:02:33 -05:00
|
|
|
asCTypeInfo *ti = 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
if( tokenType == ttIdentifier )
|
2017-11-20 08:02:33 -05:00
|
|
|
ti = ReadTypeInfo();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Read type flags as a bitmask
|
|
|
|
// Endian-safe code
|
|
|
|
bool isObjectHandle, isHandleToConst, isReference, isReadOnly;
|
|
|
|
char b = 0;
|
|
|
|
ReadData(&b, 1);
|
|
|
|
LOAD_FROM_BIT(isObjectHandle, b, 0);
|
|
|
|
LOAD_FROM_BIT(isHandleToConst, b, 1);
|
|
|
|
LOAD_FROM_BIT(isReference, b, 2);
|
|
|
|
LOAD_FROM_BIT(isReadOnly, b, 3);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if( tokenType == ttIdentifier )
|
|
|
|
*dt = asCDataType::CreateType(ti, false);
|
2014-10-27 18:43:09 -04:00
|
|
|
else
|
|
|
|
*dt = asCDataType::CreatePrimitive(tokenType, false);
|
2017-11-20 08:02:33 -05:00
|
|
|
if( isObjectHandle )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
dt->MakeReadOnly(isHandleToConst ? true : false);
|
|
|
|
|
|
|
|
// Here we must allow a scoped type to be a handle
|
2014-10-27 18:43:09 -04:00
|
|
|
// e.g. if the datatype is for a system function
|
|
|
|
dt->MakeHandle(true, true);
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
dt->MakeReadOnly(isReadOnly ? true : false);
|
|
|
|
dt->MakeReference(isReference ? true : false);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Update the previously saved slot
|
|
|
|
savedDataTypes[saveSlot] = *dt;
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
asCTypeInfo* asCReader::ReadTypeInfo()
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCTypeInfo *ot = 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
char ch;
|
|
|
|
ReadData(&ch, 1);
|
|
|
|
if( ch == 'a' )
|
|
|
|
{
|
|
|
|
// Read the name of the template type
|
2015-05-11 19:40:43 -04:00
|
|
|
asCString typeName, ns;
|
2014-10-27 18:43:09 -04:00
|
|
|
ReadString(&typeName);
|
2015-05-11 19:40:43 -04:00
|
|
|
ReadString(&ns);
|
|
|
|
asSNameSpace *nameSpace = engine->AddNameSpace(ns.AddressOf());
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
asCTypeInfo *tmp = engine->GetRegisteredType(typeName.AddressOf(), nameSpace);
|
|
|
|
asCObjectType *tmpl = CastToObjectType(tmp);
|
2014-10-27 18:43:09 -04:00
|
|
|
if( tmpl == 0 )
|
|
|
|
{
|
|
|
|
asCString str;
|
|
|
|
str.Format(TXT_TEMPLATE_TYPE_s_DOESNT_EXIST, typeName.AddressOf());
|
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
asUINT numSubTypes = ReadEncodedUInt();
|
|
|
|
asCArray<asCDataType> subTypes;
|
|
|
|
for( asUINT n = 0; n < numSubTypes; n++ )
|
|
|
|
{
|
|
|
|
ReadData(&ch, 1);
|
|
|
|
if( ch == 's' )
|
|
|
|
{
|
|
|
|
asCDataType dt;
|
|
|
|
ReadDataType(&dt);
|
|
|
|
subTypes.PushLast(dt);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eTokenType tokenType = (eTokenType)ReadEncodedUInt();
|
|
|
|
asCDataType dt = asCDataType::CreatePrimitive(tokenType, false);
|
|
|
|
subTypes.PushLast(dt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the actual template if the subtypes are the template's dummy types
|
|
|
|
if( tmpl->templateSubTypes == subTypes )
|
|
|
|
ot = tmpl;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Get the template instance type based on the loaded subtypes
|
2015-05-11 19:40:43 -04:00
|
|
|
ot = engine->GetTemplateInstanceType(tmpl, subTypes, module);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ot == 0 )
|
|
|
|
{
|
|
|
|
// Show all subtypes in error message
|
2015-05-11 19:40:43 -04:00
|
|
|
asCString sub = subTypes[0].Format(nameSpace);
|
2014-10-27 18:43:09 -04:00
|
|
|
for( asUINT n = 1; n < subTypes.GetLength(); n++ )
|
|
|
|
{
|
|
|
|
sub += ",";
|
2015-05-11 19:40:43 -04:00
|
|
|
sub += subTypes[n].Format(nameSpace);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
asCString str;
|
|
|
|
str.Format(TXT_INSTANCING_INVLD_TMPL_TYPE_s_s, typeName.AddressOf(), sub.AddressOf());
|
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( ch == 'l' )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *st = CastToObjectType(ReadTypeInfo());
|
2014-10-27 18:43:09 -04:00
|
|
|
if( st == 0 || st->beh.listFactory == 0 )
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ot = engine->GetListPatternType(st->beh.listFactory);
|
|
|
|
}
|
|
|
|
else if( ch == 's' )
|
|
|
|
{
|
|
|
|
// Read the name of the template subtype
|
|
|
|
asCString typeName;
|
|
|
|
ReadString(&typeName);
|
|
|
|
|
|
|
|
// Find the template subtype
|
|
|
|
ot = 0;
|
|
|
|
for( asUINT n = 0; n < engine->templateSubTypes.GetLength(); n++ )
|
|
|
|
{
|
|
|
|
if( engine->templateSubTypes[n] && engine->templateSubTypes[n]->name == typeName )
|
|
|
|
{
|
|
|
|
ot = engine->templateSubTypes[n];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ot == 0 )
|
|
|
|
{
|
|
|
|
asCString str;
|
|
|
|
str.Format(TXT_TEMPLATE_SUBTYPE_s_DOESNT_EXIST, typeName.AddressOf());
|
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( ch == 'o' )
|
|
|
|
{
|
|
|
|
// Read the object type name
|
|
|
|
asCString typeName, ns;
|
|
|
|
ReadString(&typeName);
|
|
|
|
ReadString(&ns);
|
|
|
|
asSNameSpace *nameSpace = engine->AddNameSpace(ns.AddressOf());
|
|
|
|
|
2016-02-17 23:07:55 -05:00
|
|
|
if( typeName.GetLength() && typeName != "$obj" && typeName != "$func" )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
// Find the object type
|
2017-11-20 08:02:33 -05:00
|
|
|
ot = module->GetType(typeName.AddressOf(), nameSpace);
|
|
|
|
if (!ot)
|
|
|
|
ot = engine->GetRegisteredType(typeName.AddressOf(), nameSpace);
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
if( ot == 0 )
|
|
|
|
{
|
|
|
|
asCString str;
|
|
|
|
str.Format(TXT_OBJECT_TYPE_s_DOESNT_EXIST, typeName.AddressOf());
|
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2016-02-17 23:07:55 -05:00
|
|
|
else if( typeName == "$obj" )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
ot = &engine->scriptTypeBehaviours;
|
|
|
|
}
|
2016-02-17 23:07:55 -05:00
|
|
|
else if( typeName == "$func" )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
ot = &engine->functionBehaviours;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
asASSERT( false );
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
else if (ch == 'c')
|
|
|
|
{
|
|
|
|
// Read the object type name
|
|
|
|
asCString typeName, ns;
|
|
|
|
ReadString(&typeName);
|
|
|
|
|
|
|
|
// Read the parent class
|
|
|
|
asCObjectType *parentClass = CastToObjectType(ReadTypeInfo());
|
|
|
|
if (parentClass == 0)
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the child type in the parentClass
|
|
|
|
for (asUINT n = 0; n < parentClass->childFuncDefs.GetLength(); n++)
|
|
|
|
{
|
|
|
|
if (parentClass->childFuncDefs[n]->name == typeName)
|
|
|
|
ot = parentClass->childFuncDefs[n];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ot == 0)
|
|
|
|
{
|
|
|
|
asCString str;
|
|
|
|
str.Format(TXT_OBJECT_TYPE_s_DOESNT_EXIST, typeName.AddressOf());
|
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// No object type
|
2015-05-11 19:40:43 -04:00
|
|
|
asASSERT( ch == '\0' || error );
|
2014-10-27 18:43:09 -04:00
|
|
|
ot = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ot;
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCReader::ReadByteCode(asCScriptFunction *func)
|
|
|
|
{
|
|
|
|
asASSERT( func->scriptData );
|
|
|
|
|
|
|
|
// Read number of instructions
|
|
|
|
asUINT total, numInstructions;
|
|
|
|
total = numInstructions = ReadEncodedUInt();
|
|
|
|
|
|
|
|
// Reserve some space for the instructions
|
|
|
|
func->scriptData->byteCode.AllocateNoConstruct(numInstructions, false);
|
|
|
|
|
|
|
|
asUINT pos = 0;
|
|
|
|
while( numInstructions )
|
|
|
|
{
|
|
|
|
asBYTE b;
|
|
|
|
ReadData(&b, 1);
|
|
|
|
|
|
|
|
// Allocate the space for the instruction
|
|
|
|
asUINT len = asBCTypeSize[asBCInfo[b].type];
|
|
|
|
asUINT newSize = asUINT(func->scriptData->byteCode.GetLength()) + len;
|
|
|
|
if( func->scriptData->byteCode.GetCapacity() < newSize )
|
|
|
|
{
|
|
|
|
// Determine the average size of the loaded instructions and re-estimate the final size
|
|
|
|
asUINT size = asUINT(float(newSize) / (total - numInstructions) * total) + 1;
|
|
|
|
func->scriptData->byteCode.AllocateNoConstruct(size, true);
|
|
|
|
}
|
|
|
|
if( !func->scriptData->byteCode.SetLengthNoConstruct(newSize) )
|
|
|
|
{
|
|
|
|
// Out of memory
|
|
|
|
error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
asDWORD *bc = func->scriptData->byteCode.AddressOf() + pos;
|
|
|
|
pos += len;
|
|
|
|
|
|
|
|
switch( asBCInfo[b].type )
|
|
|
|
{
|
|
|
|
case asBCTYPE_NO_ARG:
|
|
|
|
{
|
|
|
|
*(asBYTE*)(bc) = b;
|
|
|
|
bc++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_W_ARG:
|
|
|
|
case asBCTYPE_wW_ARG:
|
|
|
|
case asBCTYPE_rW_ARG:
|
|
|
|
{
|
|
|
|
*(asBYTE*)(bc) = b;
|
|
|
|
|
|
|
|
// Read the argument
|
|
|
|
asWORD w = ReadEncodedUInt16();
|
|
|
|
*(((asWORD*)bc)+1) = w;
|
|
|
|
|
|
|
|
bc++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_rW_DW_ARG:
|
|
|
|
case asBCTYPE_wW_DW_ARG:
|
|
|
|
case asBCTYPE_W_DW_ARG:
|
|
|
|
{
|
|
|
|
*(asBYTE*)(bc) = b;
|
|
|
|
|
|
|
|
// Read the word argument
|
|
|
|
asWORD w = ReadEncodedUInt16();
|
|
|
|
*(((asWORD*)bc)+1) = w;
|
|
|
|
bc++;
|
|
|
|
|
|
|
|
// Read the dword argument
|
|
|
|
*bc++ = ReadEncodedUInt();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_DW_ARG:
|
|
|
|
{
|
|
|
|
*(asBYTE*)(bc) = b;
|
|
|
|
bc++;
|
|
|
|
|
|
|
|
// Read the argument
|
|
|
|
*bc++ = ReadEncodedUInt();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_DW_DW_ARG:
|
|
|
|
{
|
|
|
|
*(asBYTE*)(bc) = b;
|
|
|
|
bc++;
|
|
|
|
|
|
|
|
// Read the first argument
|
|
|
|
*bc++ = ReadEncodedUInt();
|
|
|
|
|
|
|
|
// Read the second argument
|
|
|
|
*bc++ = ReadEncodedUInt();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_wW_rW_rW_ARG:
|
|
|
|
{
|
|
|
|
*(asBYTE*)(bc) = b;
|
|
|
|
|
|
|
|
// Read the first argument
|
|
|
|
asWORD w = ReadEncodedUInt16();
|
|
|
|
*(((asWORD*)bc)+1) = w;
|
|
|
|
bc++;
|
|
|
|
|
|
|
|
// Read the second argument
|
|
|
|
w = ReadEncodedUInt16();
|
|
|
|
*(asWORD*)bc = w;
|
|
|
|
|
|
|
|
// Read the third argument
|
|
|
|
w = ReadEncodedUInt16();
|
|
|
|
*(((asWORD*)bc)+1) = w;
|
|
|
|
|
|
|
|
bc++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_wW_rW_ARG:
|
|
|
|
case asBCTYPE_rW_rW_ARG:
|
|
|
|
case asBCTYPE_wW_W_ARG:
|
|
|
|
{
|
|
|
|
*(asBYTE*)(bc) = b;
|
|
|
|
|
|
|
|
// Read the first argument
|
|
|
|
asWORD w = ReadEncodedUInt16();
|
|
|
|
*(((asWORD*)bc)+1) = w;
|
|
|
|
bc++;
|
|
|
|
|
|
|
|
// Read the second argument
|
|
|
|
w = ReadEncodedUInt16();
|
|
|
|
*(asWORD*)bc = w;
|
|
|
|
|
|
|
|
bc++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_wW_rW_DW_ARG:
|
|
|
|
case asBCTYPE_rW_W_DW_ARG:
|
|
|
|
{
|
|
|
|
*(asBYTE*)(bc) = b;
|
|
|
|
|
|
|
|
// Read the first argument
|
|
|
|
asWORD w = ReadEncodedUInt16();
|
|
|
|
*(((asWORD*)bc)+1) = w;
|
|
|
|
bc++;
|
|
|
|
|
|
|
|
// Read the second argument
|
|
|
|
w = ReadEncodedUInt16();
|
|
|
|
*(asWORD*)bc = w;
|
|
|
|
bc++;
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Read the third argument
|
|
|
|
asDWORD dw = ReadEncodedUInt();
|
|
|
|
*bc++ = dw;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_QW_ARG:
|
|
|
|
{
|
|
|
|
*(asBYTE*)(bc) = b;
|
|
|
|
bc++;
|
|
|
|
|
|
|
|
// Read the argument
|
|
|
|
asQWORD qw = ReadEncodedUInt64();
|
|
|
|
*(asQWORD*)bc = qw;
|
|
|
|
bc += 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_QW_DW_ARG:
|
|
|
|
{
|
|
|
|
*(asBYTE*)(bc) = b;
|
|
|
|
bc++;
|
|
|
|
|
|
|
|
// Read the first argument
|
|
|
|
asQWORD qw = ReadEncodedUInt64();
|
|
|
|
*(asQWORD*)bc = qw;
|
|
|
|
bc += 2;
|
|
|
|
|
|
|
|
// Read the second argument
|
|
|
|
asDWORD dw = ReadEncodedUInt();
|
|
|
|
*bc++ = dw;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_rW_QW_ARG:
|
|
|
|
case asBCTYPE_wW_QW_ARG:
|
|
|
|
{
|
|
|
|
*(asBYTE*)(bc) = b;
|
|
|
|
|
|
|
|
// Read the first argument
|
|
|
|
asWORD w = ReadEncodedUInt16();
|
|
|
|
*(((asWORD*)bc)+1) = w;
|
|
|
|
bc++;
|
|
|
|
|
|
|
|
// Read the argument
|
|
|
|
asQWORD qw = ReadEncodedUInt64();
|
|
|
|
*(asQWORD*)bc = qw;
|
|
|
|
bc += 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_rW_DW_DW_ARG:
|
|
|
|
{
|
|
|
|
*(asBYTE*)(bc) = b;
|
|
|
|
|
|
|
|
// Read the 1st argument
|
|
|
|
asWORD w = ReadEncodedUInt16();
|
|
|
|
*(((asWORD*)bc)+1) = w;
|
|
|
|
bc++;
|
|
|
|
|
|
|
|
// Read the 2nd argument
|
|
|
|
*bc++ = ReadEncodedUInt();
|
|
|
|
|
|
|
|
// Read the 3rd argument
|
|
|
|
*bc++ = ReadEncodedUInt();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
// This should never happen
|
|
|
|
asASSERT(false);
|
|
|
|
|
|
|
|
// Read the next 3 bytes
|
|
|
|
asDWORD c; asBYTE t;
|
|
|
|
#if defined(AS_BIG_ENDIAN)
|
|
|
|
c = b << 24;
|
|
|
|
ReadData(&t, 1); c += t << 16;
|
|
|
|
ReadData(&t, 1); c += t << 8;
|
|
|
|
ReadData(&t, 1); c += t;
|
|
|
|
#else
|
|
|
|
c = b;
|
|
|
|
ReadData(&t, 1); c += t << 8;
|
|
|
|
ReadData(&t, 1); c += t << 16;
|
|
|
|
ReadData(&t, 1); c += t << 24;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
*bc++ = c;
|
|
|
|
c = *(asBYTE*)&c;
|
|
|
|
|
|
|
|
// Read the bc as is
|
|
|
|
for( int n = 1; n < asBCTypeSize[asBCInfo[c].type]; n++ )
|
|
|
|
ReadData(&*bc++, 4);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
numInstructions--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Correct the final size in case we over-estimated it
|
|
|
|
func->scriptData->byteCode.SetLengthNoConstruct(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCReader::ReadUsedTypeIds()
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("asCReader::ReadUsedTypeIds");
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
asUINT count = ReadEncodedUInt();
|
|
|
|
usedTypeIds.Allocate(count, false);
|
|
|
|
for( asUINT n = 0; n < count; n++ )
|
|
|
|
{
|
|
|
|
asCDataType dt;
|
|
|
|
ReadDataType(&dt);
|
|
|
|
usedTypeIds.PushLast(engine->GetTypeIdFromDataType(dt));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCReader::ReadUsedGlobalProps()
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("asCReader::ReadUsedGlobalProps");
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
int c = ReadEncodedUInt();
|
|
|
|
|
|
|
|
usedGlobalProperties.Allocate(c, false);
|
|
|
|
|
|
|
|
for( int n = 0; n < c; n++ )
|
|
|
|
{
|
|
|
|
asCString name, ns;
|
|
|
|
asCDataType type;
|
|
|
|
char moduleProp;
|
|
|
|
|
|
|
|
ReadString(&name);
|
|
|
|
ReadString(&ns);
|
|
|
|
ReadDataType(&type);
|
|
|
|
ReadData(&moduleProp, 1);
|
|
|
|
|
|
|
|
asSNameSpace *nameSpace = engine->AddNameSpace(ns.AddressOf());
|
|
|
|
|
|
|
|
// Find the real property
|
|
|
|
asCGlobalProperty *globProp = 0;
|
|
|
|
if( moduleProp )
|
|
|
|
globProp = module->scriptGlobals.GetFirst(nameSpace, name);
|
|
|
|
else
|
|
|
|
globProp = engine->registeredGlobalProps.GetFirst(nameSpace, name);
|
|
|
|
|
|
|
|
void *prop = 0;
|
|
|
|
if( globProp && globProp->type == type )
|
|
|
|
prop = globProp->GetAddressOfValue();
|
|
|
|
|
|
|
|
usedGlobalProperties.PushLast(prop);
|
|
|
|
|
|
|
|
if( prop == 0 )
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCReader::ReadUsedObjectProps()
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("asCReader::ReadUsedObjectProps");
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
asUINT c = ReadEncodedUInt();
|
|
|
|
|
|
|
|
usedObjectProperties.SetLength(c);
|
|
|
|
for( asUINT n = 0; n < c; n++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *objType = CastToObjectType(ReadTypeInfo());
|
2014-10-27 18:43:09 -04:00
|
|
|
if( objType == 0 )
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
asCString name;
|
|
|
|
ReadString(&name);
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Find the property
|
2014-10-27 18:43:09 -04:00
|
|
|
bool found = false;
|
|
|
|
for( asUINT p = 0; p < objType->properties.GetLength(); p++ )
|
|
|
|
{
|
|
|
|
if( objType->properties[p]->name == name )
|
|
|
|
{
|
|
|
|
usedObjectProperties[n].objType = objType;
|
2017-11-20 08:02:33 -05:00
|
|
|
usedObjectProperties[n].prop = objType->properties[p];
|
2014-10-27 18:43:09 -04:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !found )
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
short asCReader::FindObjectPropOffset(asWORD index)
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
static asCObjectProperty *lastCompositeProp = 0;
|
|
|
|
if (lastCompositeProp)
|
|
|
|
{
|
|
|
|
if (index != 0)
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
short offset = (short)lastCompositeProp->byteOffset;
|
|
|
|
lastCompositeProp = 0;
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
if( index >= usedObjectProperties.GetLength() )
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if (usedObjectProperties[index].prop->compositeOffset || usedObjectProperties[index].prop->isCompositeIndirect)
|
|
|
|
{
|
|
|
|
lastCompositeProp = usedObjectProperties[index].prop;
|
|
|
|
return (short)lastCompositeProp->compositeOffset;
|
|
|
|
}
|
|
|
|
return (short)usedObjectProperties[index].prop->byteOffset;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
asCScriptFunction *asCReader::FindFunction(int idx)
|
|
|
|
{
|
|
|
|
if( idx >= 0 && idx < (int)usedFunctions.GetLength() )
|
|
|
|
return usedFunctions[idx];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCReader::TranslateFunction(asCScriptFunction *func)
|
|
|
|
{
|
|
|
|
// Skip this if the function is part of an pre-existing shared object
|
|
|
|
if( dontTranslate.MoveTo(0, func) ) return;
|
|
|
|
|
|
|
|
asASSERT( func->scriptData );
|
|
|
|
|
|
|
|
// Pre-compute the size of each instruction in order to translate jump offsets
|
|
|
|
asUINT n;
|
|
|
|
asDWORD *bc = func->scriptData->byteCode.AddressOf();
|
2015-05-11 19:40:43 -04:00
|
|
|
asUINT bcLength = (asUINT)func->scriptData->byteCode.GetLength();
|
|
|
|
asCArray<asUINT> bcSizes(bcLength);
|
|
|
|
asCArray<asUINT> instructionNbrToPos(bcLength);
|
|
|
|
for( n = 0; n < bcLength; )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
int c = *(asBYTE*)&bc[n];
|
|
|
|
asUINT size = asBCTypeSize[asBCInfo[c].type];
|
|
|
|
if( size == 0 )
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bcSizes.PushLast(size);
|
|
|
|
instructionNbrToPos.PushLast(n);
|
|
|
|
n += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
asUINT bcNum = 0;
|
2015-05-11 19:40:43 -04:00
|
|
|
for( n = 0; n < bcLength; bcNum++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
int c = *(asBYTE*)&bc[n];
|
2017-11-20 08:02:33 -05:00
|
|
|
if( c == asBC_REFCPY ||
|
2014-10-27 18:43:09 -04:00
|
|
|
c == asBC_RefCpyV ||
|
|
|
|
c == asBC_OBJTYPE )
|
|
|
|
{
|
|
|
|
// Translate the index to the true object type
|
|
|
|
asPWORD *ot = (asPWORD*)&bc[n+1];
|
2017-11-20 08:02:33 -05:00
|
|
|
*(asCObjectType**)ot = CastToObjectType(FindType(int(*ot)));
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_TYPEID ||
|
|
|
|
c == asBC_Cast )
|
|
|
|
{
|
|
|
|
// Translate the index to the type id
|
|
|
|
int *tid = (int*)&bc[n+1];
|
|
|
|
*tid = FindTypeId(*tid);
|
|
|
|
}
|
|
|
|
else if( c == asBC_ADDSi ||
|
|
|
|
c == asBC_LoadThisR )
|
|
|
|
{
|
|
|
|
// Translate the index to the type id
|
|
|
|
int *tid = (int*)&bc[n+1];
|
|
|
|
*tid = FindTypeId(*tid);
|
|
|
|
|
|
|
|
// Translate the prop index into the property offset
|
|
|
|
*(((short*)&bc[n])+1) = FindObjectPropOffset(*(((short*)&bc[n])+1));
|
|
|
|
}
|
|
|
|
else if( c == asBC_LoadRObjR ||
|
|
|
|
c == asBC_LoadVObjR )
|
|
|
|
{
|
|
|
|
// Translate the index to the type id
|
|
|
|
int *tid = (int*)&bc[n+2];
|
|
|
|
*tid = FindTypeId(*tid);
|
|
|
|
|
|
|
|
asCObjectType *ot = engine->GetObjectTypeFromTypeId(*tid);
|
|
|
|
if( ot && (ot->flags & asOBJ_LIST_PATTERN) )
|
|
|
|
{
|
|
|
|
// List patterns have a different way of adjusting the offsets
|
|
|
|
SListAdjuster *listAdj = listAdjusters[listAdjusters.GetLength()-1];
|
2015-05-11 19:40:43 -04:00
|
|
|
*(((short*)&bc[n])+2) = (short)listAdj->AdjustOffset(*(((short*)&bc[n])+2));
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Translate the prop index into the property offset
|
|
|
|
*(((short*)&bc[n])+2) = FindObjectPropOffset(*(((short*)&bc[n])+2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( c == asBC_COPY )
|
|
|
|
{
|
|
|
|
// Translate the index to the type id
|
|
|
|
int *tid = (int*)&bc[n+1];
|
|
|
|
*tid = FindTypeId(*tid);
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// COPY is used to copy POD types that don't have the opAssign method. It is
|
2014-10-27 18:43:09 -04:00
|
|
|
// also used to copy references to scoped types during variable initializations.
|
|
|
|
// Update the number of dwords to copy as it may be different on the target platform
|
|
|
|
if( (*tid) & asTYPEID_OBJHANDLE )
|
|
|
|
{
|
|
|
|
// It is the actual reference that is being copied, not the object itself
|
|
|
|
asBC_SWORDARG0(&bc[n]) = AS_PTR_SIZE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asCDataType dt = engine->GetDataTypeFromTypeId(*tid);
|
|
|
|
if( !dt.IsValid() )
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
asBC_SWORDARG0(&bc[n]) = (short)dt.GetSizeInMemoryDWords();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( c == asBC_RET )
|
|
|
|
{
|
|
|
|
// Determine the correct amount of DWORDs to pop
|
|
|
|
asWORD dw = (asWORD)func->GetSpaceNeededForArguments();
|
|
|
|
if( func->DoesReturnOnStack() ) dw += AS_PTR_SIZE;
|
|
|
|
if( func->objectType ) dw += AS_PTR_SIZE;
|
|
|
|
asBC_WORDARG0(&bc[n]) = dw;
|
|
|
|
}
|
|
|
|
else if( c == asBC_CALL ||
|
|
|
|
c == asBC_CALLINTF ||
|
2016-02-17 23:07:55 -05:00
|
|
|
c == asBC_CALLSYS ||
|
|
|
|
c == asBC_Thiscall1 )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
// Translate the index to the func id
|
|
|
|
int *fid = (int*)&bc[n+1];
|
|
|
|
asCScriptFunction *f = FindFunction(*fid);
|
|
|
|
if( f )
|
|
|
|
*fid = f->id;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( c == asBC_FuncPtr )
|
|
|
|
{
|
|
|
|
// Translate the index to the func pointer
|
|
|
|
asPWORD *fid = (asPWORD*)&bc[n+1];
|
2017-11-20 08:02:33 -05:00
|
|
|
*fid = (asPWORD)FindFunction(int(*fid));
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_ALLOC )
|
|
|
|
{
|
|
|
|
// Translate the index to the true object type
|
|
|
|
asPWORD *arg = (asPWORD*)&bc[n+1];
|
2017-11-20 08:02:33 -05:00
|
|
|
*(asCObjectType**)arg = CastToObjectType(FindType(int(*arg)));
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// The constructor function id must be translated, unless it is zero
|
|
|
|
int *fid = (int*)&bc[n+1+AS_PTR_SIZE];
|
|
|
|
if( *fid != 0 )
|
|
|
|
{
|
|
|
|
// Subtract 1 from the id, as it was incremented during the writing
|
|
|
|
asCScriptFunction *f = FindFunction(*fid-1);
|
|
|
|
if( f )
|
|
|
|
*fid = f->id;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( c == asBC_STR )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_CALLBND )
|
|
|
|
{
|
|
|
|
// Translate the function id
|
|
|
|
asUINT *fid = (asUINT*)&bc[n+1];
|
|
|
|
if( *fid < module->bindInformations.GetLength() )
|
|
|
|
{
|
|
|
|
sBindInfo *bi = module->bindInformations[*fid];
|
|
|
|
if( bi )
|
|
|
|
*fid = bi->importedFunctionSignature->id;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( c == asBC_PGA ||
|
|
|
|
c == asBC_PshGPtr ||
|
|
|
|
c == asBC_LDG ||
|
|
|
|
c == asBC_PshG4 ||
|
|
|
|
c == asBC_LdGRdR4 ||
|
|
|
|
c == asBC_CpyGtoV4 ||
|
|
|
|
c == asBC_CpyVtoG4 ||
|
|
|
|
c == asBC_SetG4 )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// Translate the index to pointer
|
|
|
|
asPWORD *index = (asPWORD*)&bc[n + 1];
|
|
|
|
if ((*index & 1))
|
|
|
|
{
|
|
|
|
if ((asUINT(*index)>>1) < usedGlobalProperties.GetLength())
|
|
|
|
*(void**)index = usedGlobalProperties[asUINT(*index)>>1];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
else
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// Only PGA and PshGPtr can hold string constants
|
|
|
|
asASSERT(c == asBC_PGA || c == asBC_PshGPtr);
|
|
|
|
|
|
|
|
if ((asUINT(*index)>>1) < usedStringConstants.GetLength())
|
|
|
|
*(void**)index = usedStringConstants[asUINT(*index)>>1];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return;
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( c == asBC_JMP ||
|
|
|
|
c == asBC_JZ ||
|
|
|
|
c == asBC_JNZ ||
|
|
|
|
c == asBC_JLowZ ||
|
|
|
|
c == asBC_JLowNZ ||
|
|
|
|
c == asBC_JS ||
|
|
|
|
c == asBC_JNS ||
|
|
|
|
c == asBC_JP ||
|
|
|
|
c == asBC_JNP ) // The JMPP instruction doesn't need modification
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// Get the offset
|
2014-10-27 18:43:09 -04:00
|
|
|
int offset = int(bc[n+1]);
|
|
|
|
|
|
|
|
// Count the instruction sizes to the destination instruction
|
|
|
|
int size = 0;
|
|
|
|
if( offset >= 0 )
|
|
|
|
// If moving ahead, then start from next instruction
|
|
|
|
for( asUINT num = bcNum+1; offset-- > 0; num++ )
|
|
|
|
size += bcSizes[num];
|
|
|
|
else
|
|
|
|
// If moving backwards, then start at current instruction
|
|
|
|
for( asUINT num = bcNum; offset++ < 0; num-- )
|
|
|
|
size -= bcSizes[num];
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// The size is dword offset
|
2014-10-27 18:43:09 -04:00
|
|
|
bc[n+1] = size;
|
|
|
|
}
|
|
|
|
else if( c == asBC_AllocMem )
|
|
|
|
{
|
|
|
|
// The size of the allocated memory is only known after all the elements has been seen.
|
2017-11-20 08:02:33 -05:00
|
|
|
// This helper class will collect this information and adjust the size when the
|
2014-10-27 18:43:09 -04:00
|
|
|
// corresponding asBC_FREE is encountered
|
|
|
|
|
|
|
|
// The adjuster also needs to know the list type so it can know the type of the elements
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *ot = CastToObjectType(func->GetTypeInfoOfLocalVar(asBC_SWORDARG0(&bc[n])));
|
2015-05-11 19:40:43 -04:00
|
|
|
listAdjusters.PushLast(asNEW(SListAdjuster)(this, &bc[n], ot));
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_FREE )
|
|
|
|
{
|
|
|
|
// Translate the index to the true object type
|
|
|
|
asPWORD *pot = (asPWORD*)&bc[n+1];
|
2017-11-20 08:02:33 -05:00
|
|
|
*(asCObjectType**)pot = CastToObjectType(FindType(int(*pot)));
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
asCObjectType *ot = *(asCObjectType**)pot;
|
|
|
|
if( ot && (ot->flags & asOBJ_LIST_PATTERN) )
|
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
if( listAdjusters.GetLength() == 0 )
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Finalize the adjustment of the list buffer that was initiated with asBC_AllocMem
|
|
|
|
SListAdjuster *list = listAdjusters.PopLast();
|
|
|
|
list->AdjustAllocMem();
|
|
|
|
asDELETE(list, SListAdjuster);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( c == asBC_SetListSize )
|
|
|
|
{
|
|
|
|
// Adjust the offset in the list where the size is informed
|
|
|
|
SListAdjuster *listAdj = listAdjusters[listAdjusters.GetLength()-1];
|
2015-05-11 19:40:43 -04:00
|
|
|
bc[n+1] = listAdj->AdjustOffset(bc[n+1]);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Inform the list adjuster how many values will be repeated
|
|
|
|
listAdj->SetRepeatCount(bc[n+2]);
|
|
|
|
}
|
|
|
|
else if( c == asBC_PshListElmnt )
|
|
|
|
{
|
|
|
|
// Adjust the offset in the list where the size is informed
|
|
|
|
SListAdjuster *listAdj = listAdjusters[listAdjusters.GetLength()-1];
|
2015-05-11 19:40:43 -04:00
|
|
|
bc[n+1] = listAdj->AdjustOffset(bc[n+1]);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_SetListType )
|
|
|
|
{
|
|
|
|
// Adjust the offset in the list where the typeid is informed
|
|
|
|
SListAdjuster *listAdj = listAdjusters[listAdjusters.GetLength()-1];
|
2015-05-11 19:40:43 -04:00
|
|
|
bc[n+1] = listAdj->AdjustOffset(bc[n+1]);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Translate the type id
|
|
|
|
bc[n+2] = FindTypeId(bc[n+2]);
|
|
|
|
|
|
|
|
// Inform the list adjuster the type id of the next element
|
|
|
|
listAdj->SetNextType(bc[n+2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
n += asBCTypeSize[asBCInfo[c].type];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the stack adjustments
|
|
|
|
CalculateAdjustmentByPos(func);
|
|
|
|
|
|
|
|
// Adjust all variable positions in the bytecode
|
|
|
|
bc = func->scriptData->byteCode.AddressOf();
|
2015-05-11 19:40:43 -04:00
|
|
|
for( n = 0; n < bcLength; )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
int c = *(asBYTE*)&bc[n];
|
|
|
|
switch( asBCInfo[c].type )
|
|
|
|
{
|
|
|
|
case asBCTYPE_wW_ARG:
|
|
|
|
case asBCTYPE_rW_DW_ARG:
|
|
|
|
case asBCTYPE_wW_QW_ARG:
|
|
|
|
case asBCTYPE_rW_ARG:
|
|
|
|
case asBCTYPE_wW_DW_ARG:
|
|
|
|
case asBCTYPE_wW_W_ARG:
|
|
|
|
case asBCTYPE_rW_QW_ARG:
|
|
|
|
case asBCTYPE_rW_W_DW_ARG:
|
|
|
|
case asBCTYPE_rW_DW_DW_ARG:
|
|
|
|
{
|
|
|
|
asBC_SWORDARG0(&bc[n]) = (short)AdjustStackPosition(asBC_SWORDARG0(&bc[n]));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case asBCTYPE_wW_rW_ARG:
|
|
|
|
case asBCTYPE_wW_rW_DW_ARG:
|
|
|
|
case asBCTYPE_rW_rW_ARG:
|
|
|
|
{
|
|
|
|
asBC_SWORDARG0(&bc[n]) = (short)AdjustStackPosition(asBC_SWORDARG0(&bc[n]));
|
|
|
|
asBC_SWORDARG1(&bc[n]) = (short)AdjustStackPosition(asBC_SWORDARG1(&bc[n]));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case asBCTYPE_wW_rW_rW_ARG:
|
|
|
|
{
|
|
|
|
asBC_SWORDARG0(&bc[n]) = (short)AdjustStackPosition(asBC_SWORDARG0(&bc[n]));
|
|
|
|
asBC_SWORDARG1(&bc[n]) = (short)AdjustStackPosition(asBC_SWORDARG1(&bc[n]));
|
|
|
|
asBC_SWORDARG2(&bc[n]) = (short)AdjustStackPosition(asBC_SWORDARG2(&bc[n]));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// The other types don't treat variables so won't be modified
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
n += asBCTypeSize[asBCInfo[c].type];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust the space needed for local variables
|
|
|
|
func->scriptData->variableSpace = AdjustStackPosition(func->scriptData->variableSpace);
|
|
|
|
|
|
|
|
// Adjust the variable information. This will be used during the adjustment below
|
|
|
|
for( n = 0; n < func->scriptData->variables.GetLength(); n++ )
|
|
|
|
{
|
|
|
|
func->scriptData->variables[n]->declaredAtProgramPos = instructionNbrToPos[func->scriptData->variables[n]->declaredAtProgramPos];
|
|
|
|
func->scriptData->variables[n]->stackOffset = AdjustStackPosition(func->scriptData->variables[n]->stackOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// objVariablePos
|
|
|
|
for( n = 0; n < func->scriptData->objVariablePos.GetLength(); n++ )
|
|
|
|
func->scriptData->objVariablePos[n] = AdjustStackPosition(func->scriptData->objVariablePos[n]);
|
|
|
|
|
|
|
|
// Adjust the get offsets. This must be done in the second iteration because
|
2017-11-20 08:02:33 -05:00
|
|
|
// it relies on the function ids and variable position already being correct in the
|
2014-10-27 18:43:09 -04:00
|
|
|
// bytecodes that come after the GET instructions.
|
|
|
|
// TODO: optimize: Instead of doing a full extra loop. We can push the GET instructions
|
|
|
|
// on a stack, and then when a call instruction is found update all of them.
|
2017-11-20 08:02:33 -05:00
|
|
|
// This will also make the AdjustGetOffset() function quicker as it can
|
2014-10-27 18:43:09 -04:00
|
|
|
// receive the called function directly instead of having to search for it.
|
|
|
|
bc = func->scriptData->byteCode.AddressOf();
|
2015-05-11 19:40:43 -04:00
|
|
|
for( n = 0; n < bcLength; )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
int c = *(asBYTE*)&bc[n];
|
|
|
|
|
|
|
|
if( c == asBC_GETREF ||
|
|
|
|
c == asBC_GETOBJ ||
|
2017-11-20 08:02:33 -05:00
|
|
|
c == asBC_GETOBJREF ||
|
|
|
|
c == asBC_ChkNullS )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
asBC_WORDARG0(&bc[n]) = (asWORD)AdjustGetOffset(asBC_WORDARG0(&bc[n]), func, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
n += asBCTypeSize[asBCInfo[c].type];
|
|
|
|
}
|
|
|
|
|
|
|
|
for( n = 0; n < func->scriptData->objVariableInfo.GetLength(); n++ )
|
|
|
|
{
|
|
|
|
// The program position must be adjusted as it is stored in number of instructions
|
|
|
|
func->scriptData->objVariableInfo[n].programPos = instructionNbrToPos[func->scriptData->objVariableInfo[n].programPos];
|
|
|
|
func->scriptData->objVariableInfo[n].variableOffset = AdjustStackPosition(func->scriptData->objVariableInfo[n].variableOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The program position (every even number) needs to be adjusted
|
2017-11-20 08:02:33 -05:00
|
|
|
// for the line numbers to be in number of dwords instead of number of instructions
|
2014-10-27 18:43:09 -04:00
|
|
|
for( n = 0; n < func->scriptData->lineNumbers.GetLength(); n += 2 )
|
|
|
|
func->scriptData->lineNumbers[n] = instructionNbrToPos[func->scriptData->lineNumbers[n]];
|
|
|
|
for( n = 0; n < func->scriptData->sectionIdxs.GetLength(); n += 2 )
|
|
|
|
func->scriptData->sectionIdxs[n] = instructionNbrToPos[func->scriptData->sectionIdxs[n]];
|
|
|
|
|
|
|
|
CalculateStackNeeded(func);
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
asCReader::SListAdjuster::SListAdjuster(asCReader *rd, asDWORD *bc, asCObjectType *listType) :
|
2015-05-11 19:40:43 -04:00
|
|
|
reader(rd), allocMemBC(bc), maxOffset(0), patternType(listType), repeatCount(0), lastOffset(-1), nextOffset(0), nextTypeId(-1)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
asASSERT( patternType && (patternType->flags & asOBJ_LIST_PATTERN) );
|
|
|
|
|
|
|
|
// Find the first expected value in the list
|
|
|
|
asSListPatternNode *node = patternType->engine->scriptFunctions[patternType->templateSubTypes[0].GetBehaviour()->listFactory]->listPattern;
|
|
|
|
asASSERT( node && node->type == asLPT_START );
|
|
|
|
patternNode = node->next;
|
|
|
|
}
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
int asCReader::SListAdjuster::AdjustOffset(int offset)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
if( offset < lastOffset )
|
|
|
|
{
|
|
|
|
reader->Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// If it is the same offset being accessed again, just return the same adjusted value
|
|
|
|
if( lastOffset == offset )
|
|
|
|
return lastAdjustedOffset;
|
|
|
|
|
|
|
|
lastOffset = offset;
|
|
|
|
lastAdjustedOffset = maxOffset;
|
|
|
|
|
|
|
|
// What is being expected at this position?
|
2015-05-11 19:40:43 -04:00
|
|
|
if( patternNode->type == asLPT_REPEAT || patternNode->type == asLPT_REPEAT_SAME )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
// Align the offset to 4 bytes boundary
|
|
|
|
if( maxOffset & 0x3 )
|
|
|
|
{
|
|
|
|
maxOffset += 4 - (maxOffset & 0x3);
|
|
|
|
lastAdjustedOffset = maxOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't move the patternNode yet because the caller must make a call to SetRepeatCount too
|
|
|
|
maxOffset += 4;
|
2015-05-11 19:40:43 -04:00
|
|
|
nextOffset = offset+1;
|
2014-10-27 18:43:09 -04:00
|
|
|
return lastAdjustedOffset;
|
|
|
|
}
|
|
|
|
else if( patternNode->type == asLPT_TYPE )
|
|
|
|
{
|
|
|
|
const asCDataType &dt = reinterpret_cast<asSListPatternDataTypeNode*>(patternNode)->dataType;
|
|
|
|
if( dt.GetTokenType() == ttQuestion )
|
|
|
|
{
|
|
|
|
if( nextTypeId != -1 )
|
|
|
|
{
|
|
|
|
if( repeatCount > 0 )
|
|
|
|
repeatCount--;
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
asCDataType nextdt = patternType->engine->GetDataTypeFromTypeId(nextTypeId);
|
2014-10-27 18:43:09 -04:00
|
|
|
asUINT size;
|
2017-11-20 08:02:33 -05:00
|
|
|
if(nextdt.IsObjectHandle() || (nextdt.GetTypeInfo() && (nextdt.GetTypeInfo()->flags & asOBJ_REF)) )
|
2014-10-27 18:43:09 -04:00
|
|
|
size = AS_PTR_SIZE*4;
|
|
|
|
else
|
2017-11-20 08:02:33 -05:00
|
|
|
size = nextdt.GetSizeInMemoryBytes();
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Align the offset to 4 bytes boundary
|
|
|
|
if( size >= 4 && (maxOffset & 0x3) )
|
|
|
|
{
|
|
|
|
maxOffset += 4 - (maxOffset & 0x3);
|
|
|
|
lastAdjustedOffset = maxOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only move the patternNode if we're not expecting any more repeated entries
|
|
|
|
if( repeatCount == 0 )
|
|
|
|
patternNode = patternNode->next;
|
|
|
|
|
|
|
|
nextTypeId = -1;
|
|
|
|
|
|
|
|
maxOffset += size;
|
2015-05-11 19:40:43 -04:00
|
|
|
nextOffset = offset+1;
|
2014-10-27 18:43:09 -04:00
|
|
|
return lastAdjustedOffset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Align the offset to 4 bytes boundary
|
|
|
|
if( maxOffset & 0x3 )
|
|
|
|
{
|
|
|
|
maxOffset += 4 - (maxOffset & 0x3);
|
|
|
|
lastAdjustedOffset = maxOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first adjustment is for the typeId
|
|
|
|
maxOffset += 4;
|
2015-05-11 19:40:43 -04:00
|
|
|
nextOffset = offset+1;
|
2014-10-27 18:43:09 -04:00
|
|
|
return lastAdjustedOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Determine the size of the element
|
|
|
|
asUINT size;
|
2017-11-20 08:02:33 -05:00
|
|
|
if( dt.IsObjectHandle() || (dt.GetTypeInfo() && (dt.GetTypeInfo()->flags & asOBJ_REF)) )
|
2014-10-27 18:43:09 -04:00
|
|
|
size = AS_PTR_SIZE*4;
|
|
|
|
else
|
|
|
|
size = dt.GetSizeInMemoryBytes();
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
// If values are skipped, the offset needs to be incremented
|
|
|
|
while( nextOffset <= offset )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
if( repeatCount > 0 )
|
|
|
|
repeatCount--;
|
|
|
|
|
|
|
|
// Align the offset to 4 bytes boundary
|
|
|
|
if( size >= 4 && (maxOffset & 0x3) )
|
|
|
|
maxOffset += 4 - (maxOffset & 0x3);
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
lastAdjustedOffset = maxOffset;
|
2015-05-11 19:40:43 -04:00
|
|
|
nextOffset += 1;
|
|
|
|
maxOffset += size;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only move the patternNode if we're not expecting any more repeated entries
|
|
|
|
if( repeatCount == 0 )
|
|
|
|
patternNode = patternNode->next;
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
nextOffset = offset+1;
|
2014-10-27 18:43:09 -04:00
|
|
|
return lastAdjustedOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( patternNode->type == asLPT_START )
|
|
|
|
{
|
|
|
|
if( repeatCount > 0 )
|
|
|
|
repeatCount--;
|
|
|
|
SInfo info = {repeatCount, patternNode};
|
|
|
|
stack.PushLast(info);
|
|
|
|
|
|
|
|
repeatCount = 0;
|
|
|
|
patternNode = patternNode->next;
|
|
|
|
|
|
|
|
lastOffset--;
|
2015-05-11 19:40:43 -04:00
|
|
|
return AdjustOffset(offset);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( patternNode->type == asLPT_END )
|
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
if( stack.GetLength() == 0 )
|
|
|
|
{
|
|
|
|
reader->Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
SInfo info = stack.PopLast();
|
|
|
|
repeatCount = info.repeatCount;
|
|
|
|
if( repeatCount )
|
|
|
|
patternNode = info.startNode;
|
|
|
|
else
|
|
|
|
patternNode = patternNode->next;
|
|
|
|
|
|
|
|
lastOffset--;
|
2015-05-11 19:40:43 -04:00
|
|
|
return AdjustOffset(offset);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Something is wrong with the pattern list declaration
|
2015-05-11 19:40:43 -04:00
|
|
|
reader->Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
UNREACHABLE_RETURN;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void asCReader::SListAdjuster::SetRepeatCount(asUINT rc)
|
|
|
|
{
|
|
|
|
// Make sure the list is expecting a repeat at this location
|
2015-05-11 19:40:43 -04:00
|
|
|
asASSERT( patternNode->type == asLPT_REPEAT || patternNode->type == asLPT_REPEAT_SAME );
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Now move to the next patternNode
|
|
|
|
patternNode = patternNode->next;
|
|
|
|
|
|
|
|
repeatCount = rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCReader::SListAdjuster::AdjustAllocMem()
|
|
|
|
{
|
|
|
|
allocMemBC[1] = maxOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCReader::SListAdjuster::SetNextType(int typeId)
|
|
|
|
{
|
|
|
|
asASSERT( nextTypeId == -1 );
|
|
|
|
|
|
|
|
nextTypeId = typeId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCReader::CalculateStackNeeded(asCScriptFunction *func)
|
|
|
|
{
|
|
|
|
asASSERT( func->scriptData );
|
|
|
|
|
|
|
|
int largestStackUsed = 0;
|
|
|
|
|
|
|
|
// Clear the known stack size for each bytecode
|
|
|
|
asCArray<int> stackSize;
|
|
|
|
stackSize.SetLength(func->scriptData->byteCode.GetLength());
|
|
|
|
memset(&stackSize[0], -1, stackSize.GetLength()*4);
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Add the first instruction to the list of unchecked code
|
2014-10-27 18:43:09 -04:00
|
|
|
// paths and set the stack size at that instruction to variableSpace
|
|
|
|
asCArray<asUINT> paths;
|
|
|
|
paths.PushLast(0);
|
|
|
|
stackSize[0] = func->scriptData->variableSpace;
|
|
|
|
|
|
|
|
// Go through each of the code paths
|
|
|
|
for( asUINT p = 0; p < paths.GetLength(); ++p )
|
|
|
|
{
|
|
|
|
asUINT pos = paths[p];
|
|
|
|
int currStackSize = stackSize[pos];
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
asBYTE bc = *(asBYTE*)&func->scriptData->byteCode[pos];
|
|
|
|
if( bc == asBC_RET )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Determine the change in stack size for this instruction
|
|
|
|
int stackInc = asBCInfo[bc].stackInc;
|
|
|
|
if( stackInc == 0xFFFF )
|
|
|
|
{
|
|
|
|
// Determine the true delta from the instruction arguments
|
|
|
|
if( bc == asBC_CALL ||
|
2016-02-17 23:07:55 -05:00
|
|
|
bc == asBC_CALLSYS ||
|
|
|
|
bc == asBC_Thiscall1 ||
|
2014-10-27 18:43:09 -04:00
|
|
|
bc == asBC_CALLBND ||
|
|
|
|
bc == asBC_ALLOC ||
|
|
|
|
bc == asBC_CALLINTF ||
|
|
|
|
bc == asBC_CallPtr )
|
|
|
|
{
|
|
|
|
asCScriptFunction *called = GetCalledFunction(func, pos);
|
|
|
|
if( called )
|
|
|
|
{
|
|
|
|
stackInc = -called->GetSpaceNeededForArguments();
|
|
|
|
if( called->objectType )
|
|
|
|
stackInc -= AS_PTR_SIZE;
|
|
|
|
if( called->DoesReturnOnStack() )
|
|
|
|
stackInc -= AS_PTR_SIZE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// It is an allocation for an object without a constructor
|
|
|
|
asASSERT( bc == asBC_ALLOC );
|
|
|
|
stackInc = -AS_PTR_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
currStackSize += stackInc;
|
|
|
|
asASSERT( currStackSize >= 0 );
|
|
|
|
|
|
|
|
if( currStackSize > largestStackUsed )
|
|
|
|
largestStackUsed = currStackSize;
|
|
|
|
|
|
|
|
if( bc == asBC_JMP )
|
|
|
|
{
|
|
|
|
// Find the label that we should jump to
|
|
|
|
int offset = asBC_INTARG(&func->scriptData->byteCode[pos]);
|
|
|
|
pos += 2 + offset;
|
|
|
|
|
|
|
|
// Add the destination as a new path
|
|
|
|
if( stackSize[pos] == -1 )
|
|
|
|
{
|
|
|
|
stackSize[pos] = currStackSize;
|
|
|
|
paths.PushLast(pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
asASSERT(stackSize[pos] == currStackSize);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if( bc == asBC_JZ || bc == asBC_JNZ ||
|
|
|
|
bc == asBC_JLowZ || bc == asBC_JLowNZ ||
|
|
|
|
bc == asBC_JS || bc == asBC_JNS ||
|
|
|
|
bc == asBC_JP || bc == asBC_JNP )
|
|
|
|
{
|
|
|
|
// Find the label that is being jumped to
|
|
|
|
int offset = asBC_INTARG(&func->scriptData->byteCode[pos]);
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Add both paths to the code paths
|
|
|
|
pos += 2;
|
|
|
|
if( stackSize[pos] == -1 )
|
|
|
|
{
|
|
|
|
stackSize[pos] = currStackSize;
|
|
|
|
paths.PushLast(pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
asASSERT(stackSize[pos] == currStackSize);
|
|
|
|
|
|
|
|
pos += offset;
|
|
|
|
if( stackSize[pos] == -1 )
|
|
|
|
{
|
|
|
|
stackSize[pos] = currStackSize;
|
|
|
|
paths.PushLast(pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
asASSERT(stackSize[pos] == currStackSize);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if( bc == asBC_JMPP )
|
|
|
|
{
|
|
|
|
pos++;
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Add all subsequent JMP instructions to the path
|
|
|
|
while( *(asBYTE*)&func->scriptData->byteCode[pos] == asBC_JMP )
|
|
|
|
{
|
|
|
|
if( stackSize[pos] == -1 )
|
|
|
|
{
|
|
|
|
stackSize[pos] = currStackSize;
|
|
|
|
paths.PushLast(pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
asASSERT(stackSize[pos] == currStackSize);
|
|
|
|
pos += 2;
|
2017-11-20 08:02:33 -05:00
|
|
|
}
|
|
|
|
continue;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Add next instruction to the paths
|
|
|
|
pos += asBCTypeSize[asBCInfo[bc].type];
|
|
|
|
if( stackSize[pos] == -1 )
|
|
|
|
{
|
|
|
|
stackSize[pos] = currStackSize;
|
|
|
|
paths.PushLast(pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
asASSERT(stackSize[pos] == currStackSize);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func->scriptData->stackNeeded = largestStackUsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCReader::CalculateAdjustmentByPos(asCScriptFunction *func)
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// Adjust the offset of all negative variables (parameters) as
|
2014-10-27 18:43:09 -04:00
|
|
|
// all pointers have been stored as having a size of 1 dword
|
|
|
|
asUINT n;
|
|
|
|
asCArray<int> adjustments;
|
|
|
|
asUINT offset = 0;
|
|
|
|
if( func->objectType )
|
|
|
|
{
|
|
|
|
adjustments.PushLast(offset);
|
|
|
|
adjustments.PushLast(1-AS_PTR_SIZE);
|
|
|
|
offset += 1;
|
|
|
|
}
|
|
|
|
if( func->DoesReturnOnStack() )
|
|
|
|
{
|
|
|
|
adjustments.PushLast(offset);
|
|
|
|
adjustments.PushLast(1-AS_PTR_SIZE);
|
|
|
|
offset += 1;
|
|
|
|
}
|
|
|
|
for( n = 0; n < func->parameterTypes.GetLength(); n++ )
|
|
|
|
{
|
|
|
|
if( !func->parameterTypes[n].IsPrimitive() ||
|
|
|
|
func->parameterTypes[n].IsReference() )
|
|
|
|
{
|
|
|
|
adjustments.PushLast(offset);
|
|
|
|
adjustments.PushLast(1-AS_PTR_SIZE);
|
|
|
|
offset += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asASSERT( func->parameterTypes[n].IsPrimitive() );
|
|
|
|
offset += func->parameterTypes[n].GetSizeOnStackDWords();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build look-up table with the adjustments for each stack position
|
|
|
|
adjustNegativeStackByPos.SetLength(offset);
|
|
|
|
memset(adjustNegativeStackByPos.AddressOf(), 0, adjustNegativeStackByPos.GetLength()*sizeof(int));
|
|
|
|
for( n = 0; n < adjustments.GetLength(); n+=2 )
|
|
|
|
{
|
|
|
|
int pos = adjustments[n];
|
|
|
|
int adjust = adjustments[n+1];
|
|
|
|
|
|
|
|
for( asUINT i = pos+1; i < adjustNegativeStackByPos.GetLength(); i++ )
|
|
|
|
adjustNegativeStackByPos[i] += adjust;
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// The bytecode has been stored as if all object variables take up only 1 dword.
|
2014-10-27 18:43:09 -04:00
|
|
|
// It is necessary to adjust to the size according to the current platform.
|
|
|
|
adjustments.SetLength(0);
|
|
|
|
int highestPos = 0;
|
|
|
|
for( n = 0; n < func->scriptData->objVariableTypes.GetLength(); n++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// Determine the size the variable currently occupies on the stack
|
|
|
|
int size = AS_PTR_SIZE;
|
|
|
|
|
|
|
|
// objVariableTypes is null if the type is a null pointer
|
|
|
|
if( func->scriptData->objVariableTypes[n] &&
|
|
|
|
(func->scriptData->objVariableTypes[n]->GetFlags() & asOBJ_VALUE) &&
|
|
|
|
n >= func->scriptData->objVariablesOnHeap )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
size = func->scriptData->objVariableTypes[n]->GetSize();
|
|
|
|
if( size < 4 )
|
|
|
|
size = 1;
|
|
|
|
else
|
|
|
|
size /= 4;
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Check if type has a different size than stored
|
|
|
|
if( size > 1 )
|
|
|
|
{
|
|
|
|
if( func->scriptData->objVariablePos[n] > highestPos )
|
|
|
|
highestPos = func->scriptData->objVariablePos[n];
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
adjustments.PushLast(func->scriptData->objVariablePos[n]);
|
|
|
|
adjustments.PushLast(size-1);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count position 0 too
|
|
|
|
adjustByPos.SetLength(highestPos+1);
|
|
|
|
memset(adjustByPos.AddressOf(), 0, adjustByPos.GetLength()*sizeof(int));
|
|
|
|
|
|
|
|
// Build look-up table with the adjustments for each stack position
|
|
|
|
for( n = 0; n < adjustments.GetLength(); n+=2 )
|
|
|
|
{
|
|
|
|
int pos = adjustments[n];
|
|
|
|
int adjust = adjustments[n+1];
|
|
|
|
|
|
|
|
for( asUINT i = pos; i < adjustByPos.GetLength(); i++ )
|
|
|
|
adjustByPos[i] += adjust;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int asCReader::AdjustStackPosition(int pos)
|
|
|
|
{
|
|
|
|
if( pos >= (int)adjustByPos.GetLength() )
|
|
|
|
{
|
|
|
|
// It can be higher for primitives allocated on top of highest object variable
|
|
|
|
if( adjustByPos.GetLength() )
|
|
|
|
pos += (short)adjustByPos[adjustByPos.GetLength()-1];
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
else if( pos >= 0 )
|
2014-10-27 18:43:09 -04:00
|
|
|
pos += (short)adjustByPos[pos];
|
|
|
|
else if( -pos >= (int)adjustNegativeStackByPos.GetLength() )
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
else
|
|
|
|
pos += (short)adjustNegativeStackByPos[-pos];
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
asCScriptFunction *asCReader::GetCalledFunction(asCScriptFunction *func, asDWORD programPos)
|
|
|
|
{
|
|
|
|
asBYTE bc = *(asBYTE*)&func->scriptData->byteCode[programPos];
|
|
|
|
|
|
|
|
if( bc == asBC_CALL ||
|
|
|
|
bc == asBC_CALLSYS ||
|
2016-02-17 23:07:55 -05:00
|
|
|
bc == asBC_Thiscall1 ||
|
2014-10-27 18:43:09 -04:00
|
|
|
bc == asBC_CALLINTF )
|
|
|
|
{
|
|
|
|
// Find the function from the function id in bytecode
|
|
|
|
int funcId = asBC_INTARG(&func->scriptData->byteCode[programPos]);
|
|
|
|
return engine->scriptFunctions[funcId];
|
|
|
|
}
|
|
|
|
else if( bc == asBC_ALLOC )
|
|
|
|
{
|
|
|
|
// Find the function from the function id in the bytecode
|
|
|
|
int funcId = asBC_INTARG(&func->scriptData->byteCode[programPos+AS_PTR_SIZE]);
|
|
|
|
return engine->scriptFunctions[funcId];
|
|
|
|
}
|
|
|
|
else if( bc == asBC_CALLBND )
|
|
|
|
{
|
|
|
|
// Find the function from the engine's bind array
|
|
|
|
int funcId = asBC_INTARG(&func->scriptData->byteCode[programPos]);
|
|
|
|
return engine->importedFunctions[funcId & ~FUNC_IMPORTED]->importedFunctionSignature;
|
|
|
|
}
|
|
|
|
else if( bc == asBC_CallPtr )
|
|
|
|
{
|
|
|
|
asUINT v;
|
|
|
|
int var = asBC_SWORDARG0(&func->scriptData->byteCode[programPos]);
|
|
|
|
|
|
|
|
// Find the funcdef from the local variable
|
|
|
|
for( v = 0; v < func->scriptData->objVariablePos.GetLength(); v++ )
|
|
|
|
if( func->scriptData->objVariablePos[v] == var )
|
2017-11-20 08:02:33 -05:00
|
|
|
return CastToFuncdefType(func->scriptData->objVariableTypes[v])->funcdef;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Look in parameters
|
|
|
|
int paramPos = 0;
|
|
|
|
if( func->objectType )
|
|
|
|
paramPos -= AS_PTR_SIZE;
|
|
|
|
if( func->DoesReturnOnStack() )
|
|
|
|
paramPos -= AS_PTR_SIZE;
|
|
|
|
for( v = 0; v < func->parameterTypes.GetLength(); v++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
if (var == paramPos)
|
|
|
|
{
|
|
|
|
if (func->parameterTypes[v].IsFuncdef())
|
|
|
|
return CastToFuncdefType(func->parameterTypes[v].GetTypeInfo())->funcdef;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
paramPos -= func->parameterTypes[v].GetSizeOnStackDWords();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int asCReader::AdjustGetOffset(int offset, asCScriptFunction *func, asDWORD programPos)
|
|
|
|
{
|
|
|
|
// TODO: optimize: multiple instructions for the same function doesn't need to look for the function everytime
|
|
|
|
// the function can remember where it found the function and check if the programPos is still valid
|
|
|
|
|
|
|
|
// Get offset 0 doesn't need adjustment
|
|
|
|
if( offset == 0 ) return 0;
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
bool bcAlloc = false;
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Find out which function that will be called
|
|
|
|
asCScriptFunction *calledFunc = 0;
|
2016-02-17 23:07:55 -05:00
|
|
|
int stackDelta = 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
for( asUINT n = programPos; func->scriptData->byteCode.GetLength(); )
|
|
|
|
{
|
|
|
|
asBYTE bc = *(asBYTE*)&func->scriptData->byteCode[n];
|
|
|
|
if( bc == asBC_CALL ||
|
|
|
|
bc == asBC_CALLSYS ||
|
2016-02-17 23:07:55 -05:00
|
|
|
bc == asBC_Thiscall1 ||
|
2017-11-20 08:02:33 -05:00
|
|
|
bc == asBC_CALLINTF ||
|
2014-10-27 18:43:09 -04:00
|
|
|
bc == asBC_ALLOC ||
|
|
|
|
bc == asBC_CALLBND ||
|
|
|
|
bc == asBC_CallPtr )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// The alloc instruction allocates the object memory
|
|
|
|
// so it doesn't take the this pointer as input
|
|
|
|
if (bc == asBC_ALLOC)
|
|
|
|
bcAlloc = true;
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
calledFunc = GetCalledFunction(func, n);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if( bc == asBC_REFCPY ||
|
|
|
|
bc == asBC_COPY )
|
|
|
|
{
|
|
|
|
// In this case we know there is only 1 pointer on the stack above
|
|
|
|
asASSERT( offset == 1 );
|
|
|
|
return offset - (1 - AS_PTR_SIZE);
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Keep track of the stack size between the
|
2016-02-17 23:07:55 -05:00
|
|
|
// instruction that needs to be adjusted and the call
|
|
|
|
stackDelta += asBCInfo[bc].stackInc;
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
n += asBCTypeSize[asBCInfo[bc].type];
|
|
|
|
}
|
|
|
|
|
|
|
|
if( calledFunc == 0 )
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Count the number of pointers pushed on the stack above the
|
2014-10-27 18:43:09 -04:00
|
|
|
// current offset, and then adjust the offset accordingly
|
|
|
|
asUINT numPtrs = 0;
|
2016-02-17 23:07:55 -05:00
|
|
|
int currOffset = -stackDelta;
|
2017-11-20 08:02:33 -05:00
|
|
|
if( offset > currOffset && calledFunc->GetObjectType() && !bcAlloc )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
currOffset++;
|
2016-02-17 23:07:55 -05:00
|
|
|
if( currOffset > 0 )
|
|
|
|
numPtrs++;
|
|
|
|
#if AS_PTR_SIZE == 2
|
2017-11-20 08:02:33 -05:00
|
|
|
// For 64bit platforms it is necessary to increment the currOffset by one more
|
2016-02-17 23:07:55 -05:00
|
|
|
// DWORD since the stackDelta was counting the full 64bit size of the pointer
|
|
|
|
else if( stackDelta )
|
|
|
|
currOffset++;
|
|
|
|
#endif
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
if( offset > currOffset && calledFunc->DoesReturnOnStack() )
|
|
|
|
{
|
|
|
|
currOffset++;
|
2016-02-17 23:07:55 -05:00
|
|
|
if( currOffset > 0 )
|
|
|
|
numPtrs++;
|
|
|
|
#if AS_PTR_SIZE == 2
|
2017-11-20 08:02:33 -05:00
|
|
|
// For 64bit platforms it is necessary to increment the currOffset by one more
|
2016-02-17 23:07:55 -05:00
|
|
|
// DWORD since the stackDelta was counting the full 64bit size of the pointer
|
|
|
|
else if( stackDelta )
|
|
|
|
currOffset++;
|
|
|
|
#endif
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
for( asUINT p = 0; p < calledFunc->parameterTypes.GetLength(); p++ )
|
|
|
|
{
|
|
|
|
if( offset <= currOffset ) break;
|
|
|
|
|
|
|
|
if( !calledFunc->parameterTypes[p].IsPrimitive() ||
|
|
|
|
calledFunc->parameterTypes[p].IsReference() )
|
|
|
|
{
|
|
|
|
currOffset++;
|
2016-02-17 23:07:55 -05:00
|
|
|
if( currOffset > 0 )
|
|
|
|
numPtrs++;
|
|
|
|
#if AS_PTR_SIZE == 2
|
2017-11-20 08:02:33 -05:00
|
|
|
// For 64bit platforms it is necessary to increment the currOffset by one more
|
2016-02-17 23:07:55 -05:00
|
|
|
// DWORD since the stackDelta was counting the full 64bit size of the pointer
|
|
|
|
else if( stackDelta )
|
|
|
|
currOffset++;
|
|
|
|
#endif
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// The variable arg ? has an additiona 32bit integer with the typeid
|
|
|
|
if( calledFunc->parameterTypes[p].IsAnyType() )
|
|
|
|
currOffset += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Enums or built-in primitives are passed by value
|
|
|
|
asASSERT( calledFunc->parameterTypes[p].IsPrimitive() );
|
|
|
|
currOffset += calledFunc->parameterTypes[p].GetSizeOnStackDWords();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return offset - numPtrs * (1 - AS_PTR_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int asCReader::FindTypeId(int idx)
|
|
|
|
{
|
|
|
|
if( idx >= 0 && idx < (int)usedTypeIds.GetLength() )
|
|
|
|
return usedTypeIds[idx];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
asCTypeInfo *asCReader::FindType(int idx)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
if( idx < 0 || idx >= (int)usedTypes.GetLength() )
|
|
|
|
{
|
|
|
|
Error(TXT_INVALID_BYTECODE_d);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return usedTypes[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef AS_NO_COMPILER
|
|
|
|
|
|
|
|
asCWriter::asCWriter(asCModule* _module, asIBinaryStream* _stream, asCScriptEngine* _engine, bool _stripDebug)
|
2017-11-20 08:02:33 -05:00
|
|
|
: module(_module), stream(_stream), engine(_engine), stripDebugInfo(_stripDebug), error(false), bytesWritten(0)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
int asCWriter::Error(const char *msg)
|
|
|
|
{
|
|
|
|
// Don't write if it has already been reported an error earlier
|
|
|
|
if (!error)
|
|
|
|
{
|
|
|
|
asCString str;
|
|
|
|
str.Format(msg, bytesWritten);
|
|
|
|
engine->WriteMessage("", 0, 0, asMSGTYPE_ERROR, str.AddressOf());
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return asERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int asCWriter::WriteData(const void *data, asUINT size)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
asASSERT(size == 1 || size == 2 || size == 4 || size == 8);
|
2017-11-20 08:02:33 -05:00
|
|
|
int ret = 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
#if defined(AS_BIG_ENDIAN)
|
2017-11-20 08:02:33 -05:00
|
|
|
for( asUINT n = 0; ret >= 0 && n < size; n++ )
|
|
|
|
ret = stream->Write(((asBYTE*)data)+n, 1);
|
2014-10-27 18:43:09 -04:00
|
|
|
#else
|
2017-11-20 08:02:33 -05:00
|
|
|
for( int n = size-1; ret >= 0 && n >= 0; n-- )
|
|
|
|
ret = stream->Write(((asBYTE*)data)+n, 1);
|
2014-10-27 18:43:09 -04:00
|
|
|
#endif
|
2017-11-20 08:02:33 -05:00
|
|
|
if (ret < 0)
|
|
|
|
Error(TXT_UNEXPECTED_END_OF_FILE);
|
|
|
|
bytesWritten += size;
|
|
|
|
return ret;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
int asCWriter::Write()
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("asCWriter::Write");
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
unsigned long i, count;
|
|
|
|
|
|
|
|
// Store everything in the same order that the builder parses scripts
|
|
|
|
|
|
|
|
// TODO: Should be possible to skip saving the enum values. They are usually not needed after the script is compiled anyway
|
|
|
|
// TODO: Should be possible to skip saving the typedefs. They are usually not needed after the script is compiled anyway
|
|
|
|
// TODO: Should be possible to skip saving constants. They are usually not needed after the script is compiled anyway
|
2017-11-20 08:02:33 -05:00
|
|
|
|
|
|
|
// Write the flag as 1byte even on platforms with 4byte booleans
|
|
|
|
WriteEncodedInt64(stripDebugInfo ? 1 : 0);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Store enums
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("store enums");
|
|
|
|
|
|
|
|
count = (asUINT)module->enumTypes.GetLength();
|
|
|
|
WriteEncodedInt64(count);
|
|
|
|
for( i = 0; i < count; i++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteTypeDeclaration(module->enumTypes[i], 1);
|
|
|
|
WriteTypeDeclaration(module->enumTypes[i], 2);
|
2016-02-17 23:07:55 -05:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store type declarations first
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("type declarations");
|
|
|
|
|
|
|
|
count = (asUINT)module->classTypes.GetLength();
|
|
|
|
WriteEncodedInt64(count);
|
|
|
|
for( i = 0; i < count; i++ )
|
|
|
|
{
|
|
|
|
// Store only the name of the class/interface types
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteTypeDeclaration(module->classTypes[i], 1);
|
2016-02-17 23:07:55 -05:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store func defs
|
2016-02-17 23:07:55 -05:00
|
|
|
{
|
|
|
|
TimeIt("func defs");
|
|
|
|
|
|
|
|
count = (asUINT)module->funcDefs.GetLength();
|
|
|
|
WriteEncodedInt64(count);
|
|
|
|
for( i = 0; i < count; i++ )
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteFunction(module->funcDefs[i]->funcdef);
|
2016-02-17 23:07:55 -05:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Now store all interface methods
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("interface methods");
|
|
|
|
|
|
|
|
count = (asUINT)module->classTypes.GetLength();
|
|
|
|
for( i = 0; i < count; i++ )
|
|
|
|
{
|
|
|
|
if( module->classTypes[i]->IsInterface() )
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteTypeDeclaration(module->classTypes[i], 2);
|
2016-02-17 23:07:55 -05:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then store the class methods and behaviours
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("class methods and behaviours");
|
|
|
|
|
|
|
|
for( i = 0; i < count; ++i )
|
|
|
|
{
|
|
|
|
if( !module->classTypes[i]->IsInterface() )
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteTypeDeclaration(module->classTypes[i], 2);
|
2016-02-17 23:07:55 -05:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then store the class properties
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("class properties");
|
|
|
|
|
|
|
|
for( i = 0; i < count; ++i )
|
|
|
|
{
|
|
|
|
if( !module->classTypes[i]->IsInterface() )
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteTypeDeclaration(module->classTypes[i], 3);
|
2016-02-17 23:07:55 -05:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store typedefs
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("type defs");
|
|
|
|
|
|
|
|
count = (asUINT)module->typeDefs.GetLength();
|
|
|
|
WriteEncodedInt64(count);
|
|
|
|
for( i = 0; i < count; i++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteTypeDeclaration(module->typeDefs[i], 1);
|
|
|
|
WriteTypeDeclaration(module->typeDefs[i], 2);
|
2016-02-17 23:07:55 -05:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// scriptGlobals[]
|
2016-02-17 23:07:55 -05:00
|
|
|
{
|
|
|
|
TimeIt("script globals");
|
|
|
|
|
|
|
|
count = (asUINT)module->scriptGlobals.GetSize();
|
|
|
|
WriteEncodedInt64(count);
|
|
|
|
asCSymbolTable<asCGlobalProperty>::iterator it = module->scriptGlobals.List();
|
|
|
|
for( ; it; it++ )
|
|
|
|
WriteGlobalProperty(*it);
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// scriptFunctions[]
|
2016-02-17 23:07:55 -05:00
|
|
|
{
|
|
|
|
TimeIt("scriptFunctions");
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
for( i = 0; i < module->scriptFunctions.GetLength(); i++ )
|
|
|
|
if( module->scriptFunctions[i]->objectType == 0 )
|
|
|
|
count++;
|
|
|
|
WriteEncodedInt64(count);
|
|
|
|
for( i = 0; i < module->scriptFunctions.GetLength(); ++i )
|
|
|
|
if( module->scriptFunctions[i]->objectType == 0 )
|
|
|
|
WriteFunction(module->scriptFunctions[i]);
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// globalFunctions[]
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("globalFunctions");
|
|
|
|
|
|
|
|
count = (int)module->globalFunctions.GetSize();
|
|
|
|
asCSymbolTable<asCScriptFunction>::iterator funcIt = module->globalFunctions.List();
|
|
|
|
WriteEncodedInt64(count);
|
|
|
|
while( funcIt )
|
|
|
|
{
|
|
|
|
WriteFunction(*funcIt);
|
|
|
|
funcIt++;
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// bindInformations[]
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("bindInformations");
|
|
|
|
|
|
|
|
count = (asUINT)module->bindInformations.GetLength();
|
|
|
|
WriteEncodedInt64(count);
|
|
|
|
for( i = 0; i < count; ++i )
|
|
|
|
{
|
|
|
|
WriteFunction(module->bindInformations[i]->importedFunctionSignature);
|
|
|
|
WriteString(&module->bindInformations[i]->importFromModule);
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// usedTypes[]
|
2016-02-17 23:07:55 -05:00
|
|
|
{
|
|
|
|
TimeIt("usedTypes");
|
|
|
|
|
|
|
|
count = (asUINT)usedTypes.GetLength();
|
|
|
|
WriteEncodedInt64(count);
|
|
|
|
for( i = 0; i < count; ++i )
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteTypeInfo(usedTypes[i]);
|
2016-02-17 23:07:55 -05:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// usedTypeIds[]
|
|
|
|
WriteUsedTypeIds();
|
|
|
|
|
|
|
|
// usedFunctions[]
|
|
|
|
WriteUsedFunctions();
|
|
|
|
|
|
|
|
// usedGlobalProperties[]
|
|
|
|
WriteUsedGlobalProps();
|
|
|
|
|
|
|
|
// usedStringConstants[]
|
|
|
|
WriteUsedStringConstants();
|
|
|
|
|
|
|
|
// usedObjectProperties[]
|
|
|
|
WriteUsedObjectProps();
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
return error ? asERROR : asSUCCESS;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
int asCWriter::FindStringConstantIndex(void *str)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asSMapNode<void*, int> *cursor = 0;
|
|
|
|
if (stringToIndexMap.MoveTo(&cursor, str))
|
2014-10-27 18:43:09 -04:00
|
|
|
return cursor->value;
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
usedStringConstants.PushLast(str);
|
2014-10-27 18:43:09 -04:00
|
|
|
int index = int(usedStringConstants.GetLength() - 1);
|
2017-11-20 08:02:33 -05:00
|
|
|
stringToIndexMap.Insert(str, index);
|
2014-10-27 18:43:09 -04:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCWriter::WriteUsedStringConstants()
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("asCWriter::WriteUsedStringConstants");
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
asUINT count = (asUINT)usedStringConstants.GetLength();
|
|
|
|
WriteEncodedInt64(count);
|
2017-11-20 08:02:33 -05:00
|
|
|
|
|
|
|
asCString str;
|
|
|
|
for (asUINT i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
asUINT length;
|
|
|
|
engine->stringFactory->GetRawStringData(usedStringConstants[i], 0, &length);
|
|
|
|
str.SetLength(length);
|
|
|
|
engine->stringFactory->GetRawStringData(usedStringConstants[i], str.AddressOf(), &length);
|
|
|
|
WriteString(&str);
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void asCWriter::WriteUsedFunctions()
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("asCWriter::WriteUsedFunctions");
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
asUINT count = (asUINT)usedFunctions.GetLength();
|
|
|
|
WriteEncodedInt64(count);
|
|
|
|
|
|
|
|
for( asUINT n = 0; n < usedFunctions.GetLength(); n++ )
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
|
|
|
// Write enough data to be able to uniquely identify the function upon load
|
2017-11-20 08:02:33 -05:00
|
|
|
asCScriptFunction *func = usedFunctions[n];
|
|
|
|
if(func)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
// Is the function from the module or the application?
|
2017-11-20 08:02:33 -05:00
|
|
|
c = func->module ? 'm' : 'a';
|
|
|
|
|
|
|
|
// Functions and methods that are shared and not owned by the module can be
|
|
|
|
// stored as 's' to tell the reader that these are received from other modules.
|
|
|
|
if (c == 'm' && func->IsShared() && module->scriptFunctions.IndexOf(func) < 0 )
|
|
|
|
c = 's';
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteData(&c, 1);
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteFunctionSignature(func);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// null function pointer
|
2017-11-20 08:02:33 -05:00
|
|
|
c = 'n';
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteData(&c, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCWriter::WriteFunctionSignature(asCScriptFunction *func)
|
|
|
|
{
|
|
|
|
asUINT i, count;
|
|
|
|
|
|
|
|
WriteString(&func->name);
|
|
|
|
if( func->name == DELEGATE_FACTORY )
|
|
|
|
{
|
|
|
|
// It's not necessary to write anything else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteDataType(&func->returnType);
|
|
|
|
|
|
|
|
count = (asUINT)func->parameterTypes.GetLength();
|
|
|
|
WriteEncodedInt64(count);
|
2017-11-20 08:02:33 -05:00
|
|
|
for( i = 0; i < count; ++i )
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteDataType(&func->parameterTypes[i]);
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Only write the inout flags if any of them are set
|
|
|
|
count = 0;
|
|
|
|
for( i = asUINT(func->inOutFlags.GetLength()); i > 0; i-- )
|
|
|
|
if( func->inOutFlags[i-1] != asTM_NONE )
|
|
|
|
{
|
|
|
|
count = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
WriteEncodedInt64(count);
|
|
|
|
for( i = 0; i < count; ++i )
|
|
|
|
WriteEncodedInt64(func->inOutFlags[i]);
|
|
|
|
|
|
|
|
WriteEncodedInt64(func->funcType);
|
|
|
|
|
|
|
|
// Write the default args, from last to first
|
|
|
|
count = 0;
|
|
|
|
for( i = (asUINT)func->defaultArgs.GetLength(); i-- > 0; )
|
|
|
|
if( func->defaultArgs[i] )
|
|
|
|
count++;
|
|
|
|
WriteEncodedInt64(count);
|
|
|
|
for( i = (asUINT)func->defaultArgs.GetLength(); i-- > 0; )
|
|
|
|
if( func->defaultArgs[i] )
|
|
|
|
WriteString(func->defaultArgs[i]);
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteTypeInfo(func->objectType);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
if( func->objectType )
|
|
|
|
{
|
|
|
|
asBYTE b = 0;
|
2017-11-20 08:02:33 -05:00
|
|
|
b += func->IsReadOnly() ? 1 : 0;
|
|
|
|
b += func->IsPrivate() ? 2 : 0;
|
|
|
|
b += func->IsProtected() ? 4 : 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteData(&b, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
if (func->funcType == asFUNC_FUNCDEF)
|
|
|
|
{
|
|
|
|
if (func->nameSpace)
|
|
|
|
{
|
|
|
|
// This funcdef was declared as global entity
|
|
|
|
asBYTE b = 'n';
|
|
|
|
WriteData(&b, 1);
|
|
|
|
WriteString(&func->nameSpace->name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This funcdef was declared as class member
|
|
|
|
asBYTE b = 'o';
|
|
|
|
WriteData(&b, 1);
|
|
|
|
WriteTypeInfo(func->funcdefType->parentClass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
WriteString(&func->nameSpace->name);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
void asCWriter::WriteFunction(asCScriptFunction* func)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
|
|
|
// If there is no function, then store a null char
|
|
|
|
if( func == 0 )
|
|
|
|
{
|
|
|
|
c = '\0';
|
|
|
|
WriteData(&c, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First check if the function has been saved already
|
|
|
|
for( asUINT f = 0; f < savedFunctions.GetLength(); f++ )
|
|
|
|
{
|
|
|
|
if( savedFunctions[f] == func )
|
|
|
|
{
|
|
|
|
c = 'r';
|
|
|
|
WriteData(&c, 1);
|
|
|
|
WriteEncodedInt64(f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep a reference to the function in the list
|
|
|
|
savedFunctions.PushLast(func);
|
|
|
|
|
|
|
|
c = 'f';
|
|
|
|
WriteData(&c, 1);
|
|
|
|
|
|
|
|
asUINT i, count;
|
|
|
|
|
|
|
|
WriteFunctionSignature(func);
|
|
|
|
|
|
|
|
if( func->funcType == asFUNC_SCRIPT )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
char bits = 0;
|
|
|
|
bits += func->IsShared() ? 1 : 0;
|
|
|
|
bits += func->dontCleanUpOnException ? 2 : 0;
|
|
|
|
if (module->externalFunctions.IndexOf(func) >= 0)
|
|
|
|
bits += 4;
|
|
|
|
WriteData(&bits, 1);
|
|
|
|
|
|
|
|
// For external shared functions the rest is not needed
|
|
|
|
if (bits & 4)
|
|
|
|
return;
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Calculate the adjustment by position lookup table
|
|
|
|
CalculateAdjustmentByPos(func);
|
|
|
|
|
|
|
|
WriteByteCode(func);
|
|
|
|
|
|
|
|
asDWORD varSpace = AdjustStackPosition(func->scriptData->variableSpace);
|
|
|
|
WriteEncodedInt64(varSpace);
|
|
|
|
|
|
|
|
count = (asUINT)func->scriptData->objVariablePos.GetLength();
|
|
|
|
WriteEncodedInt64(count);
|
|
|
|
for( i = 0; i < count; ++i )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteTypeInfo(func->scriptData->objVariableTypes[i]);
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(AdjustStackPosition(func->scriptData->objVariablePos[i]));
|
|
|
|
}
|
|
|
|
if( count > 0 )
|
|
|
|
WriteEncodedInt64(func->scriptData->objVariablesOnHeap);
|
|
|
|
|
|
|
|
WriteEncodedInt64((asUINT)func->scriptData->objVariableInfo.GetLength());
|
|
|
|
for( i = 0; i < func->scriptData->objVariableInfo.GetLength(); ++i )
|
|
|
|
{
|
|
|
|
// The program position must be adjusted to be in number of instructions
|
|
|
|
WriteEncodedInt64(bytecodeNbrByPos[func->scriptData->objVariableInfo[i].programPos]);
|
|
|
|
WriteEncodedInt64(AdjustStackPosition(func->scriptData->objVariableInfo[i].variableOffset));
|
|
|
|
WriteEncodedInt64(func->scriptData->objVariableInfo[i].option);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The program position (every even number) needs to be adjusted
|
|
|
|
// to be in number of instructions instead of DWORD offset
|
|
|
|
if( !stripDebugInfo )
|
|
|
|
{
|
|
|
|
asUINT length = (asUINT)func->scriptData->lineNumbers.GetLength();
|
|
|
|
WriteEncodedInt64(length);
|
|
|
|
for( i = 0; i < length; ++i )
|
|
|
|
{
|
|
|
|
if( (i & 1) == 0 )
|
|
|
|
WriteEncodedInt64(bytecodeNbrByPos[func->scriptData->lineNumbers[i]]);
|
|
|
|
else
|
|
|
|
WriteEncodedInt64(func->scriptData->lineNumbers[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the array of script sections
|
|
|
|
length = (asUINT)func->scriptData->sectionIdxs.GetLength();
|
|
|
|
WriteEncodedInt64(length);
|
|
|
|
for( i = 0; i < length; ++i )
|
|
|
|
{
|
|
|
|
if( (i & 1) == 0 )
|
|
|
|
WriteEncodedInt64(bytecodeNbrByPos[func->scriptData->sectionIdxs[i]]);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( func->scriptData->sectionIdxs[i] >= 0 )
|
|
|
|
WriteString(engine->scriptSectionNames[func->scriptData->sectionIdxs[i]]);
|
|
|
|
else
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
c = 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteData(&c, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the variable information
|
|
|
|
if( !stripDebugInfo )
|
|
|
|
{
|
|
|
|
WriteEncodedInt64((asUINT)func->scriptData->variables.GetLength());
|
|
|
|
for( i = 0; i < func->scriptData->variables.GetLength(); i++ )
|
|
|
|
{
|
|
|
|
// The program position must be adjusted to be in number of instructions
|
|
|
|
WriteEncodedInt64(bytecodeNbrByPos[func->scriptData->variables[i]->declaredAtProgramPos]);
|
|
|
|
// The stack position must be adjusted according to the pointer sizes
|
|
|
|
WriteEncodedInt64(AdjustStackPosition(func->scriptData->variables[i]->stackOffset));
|
|
|
|
WriteString(&func->scriptData->variables[i]->name);
|
|
|
|
WriteDataType(&func->scriptData->variables[i]->type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store script section name
|
|
|
|
if( !stripDebugInfo )
|
|
|
|
{
|
|
|
|
if( func->scriptData->scriptSectionIdx >= 0 )
|
|
|
|
WriteString(engine->scriptSectionNames[func->scriptData->scriptSectionIdx]);
|
|
|
|
else
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
c = 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteData(&c, 1);
|
|
|
|
}
|
|
|
|
WriteEncodedInt64(func->scriptData->declaredAt);
|
|
|
|
}
|
2015-05-11 19:40:43 -04:00
|
|
|
|
|
|
|
// Store the parameter names
|
|
|
|
if( !stripDebugInfo )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
count = asUINT(func->parameterNames.GetLength());
|
2015-05-11 19:40:43 -04:00
|
|
|
WriteEncodedInt64(count);
|
|
|
|
for( asUINT n = 0; n < count; n++ )
|
|
|
|
WriteString(&func->parameterNames[n]);
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( func->funcType == asFUNC_VIRTUAL || func->funcType == asFUNC_INTERFACE )
|
|
|
|
{
|
|
|
|
// TODO: Do we really need to store this? It can probably be reconstructed by the reader
|
|
|
|
WriteEncodedInt64(func->vfTableIdx);
|
|
|
|
}
|
2016-02-17 23:07:55 -05:00
|
|
|
else if( func->funcType == asFUNC_FUNCDEF )
|
|
|
|
{
|
|
|
|
char bits = 0;
|
2017-11-20 08:02:33 -05:00
|
|
|
bits += func->IsShared() ? 1 : 0;
|
|
|
|
if (module->externalTypes.IndexOf(func->funcdefType) >= 0)
|
|
|
|
bits += 2;
|
2016-02-17 23:07:55 -05:00
|
|
|
WriteData(&bits,1);
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
void asCWriter::WriteTypeDeclaration(asCTypeInfo *type, int phase)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
if( phase == 1 )
|
|
|
|
{
|
|
|
|
// name
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteString(&type->name);
|
2014-10-27 18:43:09 -04:00
|
|
|
// flags
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteData(&type->flags, 4);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// size
|
2017-11-20 08:02:33 -05:00
|
|
|
// TODO: Do we really need to store this? The reader should be able to
|
2014-10-27 18:43:09 -04:00
|
|
|
// determine the correct size from the object type's flags
|
2017-11-20 08:02:33 -05:00
|
|
|
if( (type->flags & asOBJ_SCRIPT_OBJECT) && type->size > 0 )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// The size for script objects may vary from platform to platform so
|
2014-10-27 18:43:09 -04:00
|
|
|
// only store 1 to diferentiate from interfaces that have size 0.
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteEncodedInt64(1);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Enums, typedefs, and interfaces have fixed sizes independently
|
|
|
|
// of platform so it is safe to serialize the size directly.
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteEncodedInt64(type->size);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// namespace
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteString(&type->nameSpace->name);
|
|
|
|
|
|
|
|
// external shared flag
|
|
|
|
if ((type->flags & asOBJ_SHARED))
|
|
|
|
{
|
|
|
|
char c = ' ';
|
|
|
|
if (module->externalTypes.IndexOf(type) >= 0)
|
|
|
|
c = 'e';
|
|
|
|
WriteData(&c, 1);
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( phase == 2 )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// external shared types doesn't need to save this
|
|
|
|
if ((type->flags & asOBJ_SHARED) && module->externalTypes.IndexOf(type) >= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(type->flags & asOBJ_ENUM )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
// enumValues[]
|
2017-11-20 08:02:33 -05:00
|
|
|
asCEnumType *t = CastToEnumType(type);
|
|
|
|
int size = (int)t->enumValues.GetLength();
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(size);
|
|
|
|
|
|
|
|
for( int n = 0; n < size; n++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteString(&t->enumValues[n]->name);
|
|
|
|
WriteData(&t->enumValues[n]->value, 4);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
else if(type->flags & asOBJ_TYPEDEF )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCTypedefType *td = CastToTypedefType(type);
|
|
|
|
eTokenType t = td->aliasForType.GetTokenType();
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(t);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *t = CastToObjectType(type);
|
|
|
|
WriteTypeInfo(t->derivedFrom);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// interfaces[] / interfaceVFTOffsets[]
|
|
|
|
// TOOD: Is it really necessary to store the VFTOffsets? Can't the reader calculate those?
|
2017-11-20 08:02:33 -05:00
|
|
|
int size = (asUINT)t->interfaces.GetLength();
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(size);
|
|
|
|
asUINT n;
|
2017-11-20 08:02:33 -05:00
|
|
|
asASSERT( t->interfaces.GetLength() == t->interfaceVFTOffsets.GetLength() );
|
|
|
|
for( n = 0; n < t->interfaces.GetLength(); n++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteTypeInfo(t->interfaces[n]);
|
|
|
|
WriteEncodedInt64(t->interfaceVFTOffsets[n]);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// behaviours
|
2017-11-20 08:02:33 -05:00
|
|
|
// TODO: Default behaviours should just be stored as a indicator
|
2014-10-27 18:43:09 -04:00
|
|
|
// to avoid storing the actual function object
|
2017-11-20 08:02:33 -05:00
|
|
|
if( !t->IsInterface() && type->flags != asOBJ_TYPEDEF && type->flags != asOBJ_ENUM )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteFunction(engine->scriptFunctions[t->beh.destruct]);
|
|
|
|
size = (int)t->beh.constructors.GetLength();
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(size);
|
2017-11-20 08:02:33 -05:00
|
|
|
for( n = 0; n < t->beh.constructors.GetLength(); n++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteFunction(engine->scriptFunctions[t->beh.constructors[n]]);
|
|
|
|
WriteFunction(engine->scriptFunctions[t->beh.factories[n]]);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// methods[]
|
|
|
|
// TODO: Avoid storing inherited methods in interfaces, as the reader
|
|
|
|
// can add those directly from the base interface
|
2017-11-20 08:02:33 -05:00
|
|
|
size = (int)t->methods.GetLength();
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(size);
|
2017-11-20 08:02:33 -05:00
|
|
|
for( n = 0; n < t->methods.GetLength(); n++ )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteFunction(engine->scriptFunctions[t->methods[n]]);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// virtualFunctionTable[]
|
|
|
|
// TODO: Is it really necessary to store this? Can't it be easily rebuilt by the reader
|
2017-11-20 08:02:33 -05:00
|
|
|
size = (int)t->virtualFunctionTable.GetLength();
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(size);
|
|
|
|
for( n = 0; n < (asUINT)size; n++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteFunction(t->virtualFunctionTable[n]);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( phase == 3 )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// external shared types doesn't need to save this
|
|
|
|
if ((type->flags & asOBJ_SHARED) && module->externalTypes.IndexOf(type) >= 0)
|
|
|
|
return;
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// properties[]
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *t = CastToObjectType(type);
|
|
|
|
|
|
|
|
// This is only done for object types
|
|
|
|
asASSERT(t);
|
|
|
|
|
|
|
|
asUINT size = (asUINT)t->properties.GetLength();
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(size);
|
2017-11-20 08:02:33 -05:00
|
|
|
for (asUINT n = 0; n < t->properties.GetLength(); n++)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteObjectProperty(t->properties[n]);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCWriter::WriteEncodedInt64(asINT64 i)
|
|
|
|
{
|
|
|
|
asBYTE signBit = ( i & asINT64(1)<<63 ) ? 0x80 : 0;
|
|
|
|
if( signBit ) i = -i;
|
|
|
|
|
|
|
|
asBYTE b;
|
|
|
|
if( i < (1<<6) )
|
|
|
|
{
|
|
|
|
b = (asBYTE)(signBit + i); WriteData(&b, 1);
|
|
|
|
}
|
|
|
|
else if( i < (1<<13) )
|
|
|
|
{
|
|
|
|
b = asBYTE(0x40 + signBit + (i >> 8)); WriteData(&b, 1);
|
|
|
|
b = asBYTE(i & 0xFF); WriteData(&b, 1);
|
|
|
|
}
|
|
|
|
else if( i < (1<<20) )
|
|
|
|
{
|
|
|
|
b = asBYTE(0x60 + signBit + (i >> 16)); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 8) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE(i & 0xFF); WriteData(&b, 1);
|
|
|
|
}
|
|
|
|
else if( i < (1<<27) )
|
|
|
|
{
|
|
|
|
b = asBYTE(0x70 + signBit + (i >> 24)); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 16) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 8) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE(i & 0xFF); WriteData(&b, 1);
|
|
|
|
}
|
|
|
|
else if( i < (asINT64(1)<<34) )
|
|
|
|
{
|
|
|
|
b = asBYTE(0x78 + signBit + (i >> 32)); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 24) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 16) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 8) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE(i & 0xFF); WriteData(&b, 1);
|
|
|
|
}
|
|
|
|
else if( i < (asINT64(1)<<41) )
|
|
|
|
{
|
|
|
|
b = asBYTE(0x7C + signBit + (i >> 40)); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 32) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 24) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 16) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 8) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE(i & 0xFF); WriteData(&b, 1);
|
|
|
|
}
|
|
|
|
else if( i < (asINT64(1)<<48) )
|
|
|
|
{
|
|
|
|
b = asBYTE(0x7E + signBit + (i >> 48)); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 40) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 32) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 24) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 16) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 8) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE(i & 0xFF); WriteData(&b, 1);
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
else
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
b = asBYTE(0x7F + signBit); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 56) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 48) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 40) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 32) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 24) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 16) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE((i >> 8) & 0xFF); WriteData(&b, 1);
|
|
|
|
b = asBYTE(i & 0xFF); WriteData(&b, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
void asCWriter::WriteString(asCString* str)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
// First check if the string hasn't been saved already
|
2017-11-20 08:02:33 -05:00
|
|
|
asSMapNode<asCString, int> *cursor = 0;
|
|
|
|
if (stringToIdMap.MoveTo(&cursor, *str))
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
// Save a reference to the existing string
|
2016-02-17 23:07:55 -05:00
|
|
|
// The lowest bit is set to 1 to indicate a reference
|
|
|
|
WriteEncodedInt64(cursor->value*2+1);
|
2014-10-27 18:43:09 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save a new string
|
2016-02-17 23:07:55 -05:00
|
|
|
// The lowest bit is set to 0 to indicate a new string
|
2014-10-27 18:43:09 -04:00
|
|
|
asUINT len = (asUINT)str->GetLength();
|
2016-02-17 23:07:55 -05:00
|
|
|
WriteEncodedInt64(len*2);
|
|
|
|
|
|
|
|
if( len > 0 )
|
|
|
|
{
|
|
|
|
stream->Write(str->AddressOf(), (asUINT)len);
|
2017-11-20 08:02:33 -05:00
|
|
|
bytesWritten += len;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2016-02-17 23:07:55 -05:00
|
|
|
savedStrings.PushLast(*str);
|
2017-11-20 08:02:33 -05:00
|
|
|
stringToIdMap.Insert(*str, int(savedStrings.GetLength()) - 1);
|
2016-02-17 23:07:55 -05:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
void asCWriter::WriteGlobalProperty(asCGlobalProperty* prop)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// TODO: We might be able to avoid storing the name and type of the global
|
|
|
|
// properties twice if we merge this with the WriteUsedGlobalProperties.
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteString(&prop->name);
|
|
|
|
WriteString(&prop->nameSpace->name);
|
|
|
|
WriteDataType(&prop->type);
|
|
|
|
|
|
|
|
// Store the initialization function
|
2015-05-11 19:40:43 -04:00
|
|
|
WriteFunction(prop->GetInitFunc());
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
void asCWriter::WriteObjectProperty(asCObjectProperty* prop)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
WriteString(&prop->name);
|
|
|
|
WriteDataType(&prop->type);
|
2015-05-11 19:40:43 -04:00
|
|
|
int flags = 0;
|
|
|
|
if( prop->isPrivate ) flags |= 1;
|
|
|
|
if( prop->isProtected ) flags |= 2;
|
|
|
|
if( prop->isInherited ) flags |= 4;
|
|
|
|
WriteEncodedInt64(flags);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
void asCWriter::WriteDataType(const asCDataType *dt)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
// First check if the datatype has already been saved
|
|
|
|
for( asUINT n = 0; n < savedDataTypes.GetLength(); n++ )
|
|
|
|
{
|
|
|
|
if( *dt == savedDataTypes[n] )
|
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
WriteEncodedInt64(n+1);
|
2014-10-27 18:43:09 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
// Indicate a new type with a null byte
|
|
|
|
asUINT c = 0;
|
|
|
|
WriteEncodedInt64(c);
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Save the new datatype
|
|
|
|
savedDataTypes.PushLast(*dt);
|
|
|
|
|
|
|
|
int t = dt->GetTokenType();
|
|
|
|
WriteEncodedInt64(t);
|
|
|
|
if( t == ttIdentifier )
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteTypeInfo(dt->GetTypeInfo());
|
|
|
|
|
|
|
|
// Endianess safe bitmask
|
|
|
|
char bits = 0;
|
|
|
|
SAVE_TO_BIT(bits, dt->IsObjectHandle(), 0);
|
|
|
|
SAVE_TO_BIT(bits, dt->IsHandleToConst(), 1);
|
|
|
|
SAVE_TO_BIT(bits, dt->IsReference(), 2);
|
|
|
|
SAVE_TO_BIT(bits, dt->IsReadOnly(), 3);
|
2015-05-11 19:40:43 -04:00
|
|
|
WriteData(&bits, 1);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
void asCWriter::WriteTypeInfo(asCTypeInfo* ti)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
char ch;
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
if( ti )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
// Check for template instances/specializations
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *ot = CastToObjectType(ti);
|
|
|
|
if( ot && ot->templateSubTypes.GetLength() )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
// Check for list pattern type or template type
|
|
|
|
if( ot->flags & asOBJ_LIST_PATTERN )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
ch = 'l'; // list
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteData(&ch, 1);
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteTypeInfo(ot->templateSubTypes[0].GetTypeInfo());
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
ch = 'a'; // array
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteData(&ch, 1);
|
|
|
|
WriteString(&ot->name);
|
2015-05-11 19:40:43 -04:00
|
|
|
WriteString(&ot->nameSpace->name);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
WriteEncodedInt64(ot->templateSubTypes.GetLength());
|
|
|
|
for( asUINT n = 0; n < ot->templateSubTypes.GetLength(); n++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
if( !ot->templateSubTypes[n].IsPrimitive() || ot->templateSubTypes[n].IsEnumType() )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
ch = 's'; // sub type
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteData(&ch, 1);
|
2016-02-17 23:07:55 -05:00
|
|
|
WriteDataType(&ot->templateSubTypes[n]);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
ch = 't'; // token
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteData(&ch, 1);
|
2016-02-17 23:07:55 -05:00
|
|
|
eTokenType t = ot->templateSubTypes[n].GetTokenType();
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
else if( ti->flags & asOBJ_TEMPLATE_SUBTYPE )
|
|
|
|
{
|
|
|
|
ch = 's'; // sub type
|
|
|
|
WriteData(&ch, 1);
|
|
|
|
WriteString(&ti->name);
|
|
|
|
}
|
|
|
|
else if( !ti->GetParentType() )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
ch = 'o'; // object
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteData(&ch, 1);
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteString(&ti->name);
|
|
|
|
WriteString(&ti->nameSpace->name);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asASSERT(ti->flags & asOBJ_FUNCDEF);
|
|
|
|
|
|
|
|
ch = 'c'; // child type
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteData(&ch, 1);
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteString(&ti->name);
|
|
|
|
WriteTypeInfo(CastToFuncdefType(ti)->parentClass);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ch = '\0';
|
|
|
|
WriteData(&ch, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCWriter::CalculateAdjustmentByPos(asCScriptFunction *func)
|
|
|
|
{
|
|
|
|
// Adjust the offset of all negative variables (parameters) so all pointers will have a size of 1 dword
|
|
|
|
asUINT n;
|
|
|
|
asCArray<int> adjustments;
|
|
|
|
asUINT offset = 0;
|
|
|
|
if( func->objectType )
|
|
|
|
{
|
|
|
|
adjustments.PushLast(offset);
|
|
|
|
adjustments.PushLast(1-AS_PTR_SIZE);
|
|
|
|
offset += AS_PTR_SIZE;
|
|
|
|
}
|
|
|
|
if( func->DoesReturnOnStack() )
|
|
|
|
{
|
|
|
|
adjustments.PushLast(offset);
|
|
|
|
adjustments.PushLast(1-AS_PTR_SIZE);
|
|
|
|
offset += AS_PTR_SIZE;
|
|
|
|
}
|
|
|
|
for( n = 0; n < func->parameterTypes.GetLength(); n++ )
|
|
|
|
{
|
|
|
|
if( !func->parameterTypes[n].IsPrimitive() ||
|
|
|
|
func->parameterTypes[n].IsReference() )
|
|
|
|
{
|
|
|
|
adjustments.PushLast(offset);
|
|
|
|
adjustments.PushLast(1-AS_PTR_SIZE);
|
|
|
|
offset += AS_PTR_SIZE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asASSERT( func->parameterTypes[n].IsPrimitive() );
|
|
|
|
offset += func->parameterTypes[n].GetSizeOnStackDWords();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build look-up table with the adjustments for each stack position
|
|
|
|
adjustNegativeStackByPos.SetLength(offset);
|
|
|
|
memset(adjustNegativeStackByPos.AddressOf(), 0, adjustNegativeStackByPos.GetLength()*sizeof(int));
|
|
|
|
for( n = 0; n < adjustments.GetLength(); n+=2 )
|
|
|
|
{
|
|
|
|
int pos = adjustments[n];
|
|
|
|
int adjust = adjustments[n+1];
|
|
|
|
|
|
|
|
for( asUINT i = pos+1; i < adjustNegativeStackByPos.GetLength(); i++ )
|
|
|
|
adjustNegativeStackByPos[i] += adjust;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust the offset of all positive variables so that all object types and handles have a size of 1 dword
|
|
|
|
// This is similar to how the adjustment is done in the asCReader::TranslateFunction, only the reverse
|
|
|
|
adjustments.SetLength(0);
|
|
|
|
for( n = 0; n < func->scriptData->objVariableTypes.GetLength(); n++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// Determine the size the variable currently occupies on the stack
|
|
|
|
int size = AS_PTR_SIZE;
|
|
|
|
|
|
|
|
// objVariableTypes is null if the variable type is a null pointer
|
|
|
|
if( func->scriptData->objVariableTypes[n] &&
|
|
|
|
(func->scriptData->objVariableTypes[n]->GetFlags() & asOBJ_VALUE) &&
|
|
|
|
n >= func->scriptData->objVariablesOnHeap )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
size = func->scriptData->objVariableTypes[n]->GetSize();
|
|
|
|
if( size < 4 )
|
|
|
|
size = 1;
|
|
|
|
else
|
|
|
|
size /= 4;
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// If larger than 1 dword, adjust the offsets accordingly
|
|
|
|
if (size > 1)
|
|
|
|
{
|
|
|
|
// How much needs to be adjusted?
|
|
|
|
adjustments.PushLast(func->scriptData->objVariablePos[n]);
|
|
|
|
adjustments.PushLast(-(size - 1));
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build look-up table with the adjustments for each stack position
|
|
|
|
adjustStackByPos.SetLength(func->scriptData->stackNeeded);
|
|
|
|
memset(adjustStackByPos.AddressOf(), 0, adjustStackByPos.GetLength()*sizeof(int));
|
|
|
|
for( n = 0; n < adjustments.GetLength(); n+=2 )
|
|
|
|
{
|
|
|
|
int pos = adjustments[n];
|
|
|
|
int adjust = adjustments[n+1];
|
|
|
|
|
|
|
|
for( asUINT i = pos; i < adjustStackByPos.GetLength(); i++ )
|
|
|
|
adjustStackByPos[i] += adjust;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the sequence number of each bytecode instruction in order to update the jump offsets
|
2015-05-11 19:40:43 -04:00
|
|
|
asUINT length = func->scriptData->byteCode.GetLength();
|
2014-10-27 18:43:09 -04:00
|
|
|
asDWORD *bc = func->scriptData->byteCode.AddressOf();
|
|
|
|
bytecodeNbrByPos.SetLength(length);
|
|
|
|
asUINT num;
|
|
|
|
for( offset = 0, num = 0; offset < length; )
|
|
|
|
{
|
|
|
|
bytecodeNbrByPos[offset] = num;
|
|
|
|
offset += asBCTypeSize[asBCInfo[*(asBYTE*)(bc+offset)].type];
|
|
|
|
num++;
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
// The last instruction is always a BC_RET. This make it possible to query
|
2014-10-27 18:43:09 -04:00
|
|
|
// the number of instructions by checking the last entry in bytecodeNbrByPos
|
|
|
|
asASSERT(*(asBYTE*)(bc+length-1) == asBC_RET);
|
|
|
|
}
|
|
|
|
|
|
|
|
int asCWriter::AdjustStackPosition(int pos)
|
|
|
|
{
|
|
|
|
if( pos >= (int)adjustStackByPos.GetLength() )
|
|
|
|
{
|
|
|
|
// This happens for example if the function only have temporary variables
|
|
|
|
// The adjustByPos can also be empty if the function doesn't have any variables at all, but receive a handle by parameter
|
|
|
|
if( adjustStackByPos.GetLength() > 0 )
|
|
|
|
pos += adjustStackByPos[adjustStackByPos.GetLength()-1];
|
|
|
|
}
|
|
|
|
else if( pos >= 0 )
|
|
|
|
pos += adjustStackByPos[pos];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asASSERT( -pos < (int)adjustNegativeStackByPos.GetLength() );
|
|
|
|
pos -= (short)adjustNegativeStackByPos[-pos];
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
int asCWriter::AdjustGetOffset(int offset, asCScriptFunction *func, asDWORD programPos)
|
|
|
|
{
|
|
|
|
// TODO: optimize: multiple instructions for the same function doesn't need to look for the function everytime
|
|
|
|
// the function can remember where it found the function and check if the programPos is still valid
|
|
|
|
|
|
|
|
// Get offset 0 doesn't need adjustment
|
|
|
|
if( offset == 0 ) return 0;
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
bool bcAlloc = false;
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Find out which function that will be called
|
|
|
|
asCScriptFunction *calledFunc = 0;
|
2016-02-17 23:07:55 -05:00
|
|
|
int stackDelta = 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
for( asUINT n = programPos; n < func->scriptData->byteCode.GetLength(); )
|
|
|
|
{
|
|
|
|
asBYTE bc = *(asBYTE*)&func->scriptData->byteCode[n];
|
|
|
|
if( bc == asBC_CALL ||
|
|
|
|
bc == asBC_CALLSYS ||
|
2016-02-17 23:07:55 -05:00
|
|
|
bc == asBC_Thiscall1 ||
|
2014-10-27 18:43:09 -04:00
|
|
|
bc == asBC_CALLINTF )
|
|
|
|
{
|
|
|
|
// Find the function from the function id in bytecode
|
|
|
|
int funcId = asBC_INTARG(&func->scriptData->byteCode[n]);
|
|
|
|
calledFunc = engine->scriptFunctions[funcId];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if( bc == asBC_ALLOC )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// The alloc instruction doesn't take the object pointer on the stack,
|
|
|
|
// as the memory will be allocated by the instruction itself
|
|
|
|
bcAlloc = true;
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Find the function from the function id in the bytecode
|
|
|
|
int funcId = asBC_INTARG(&func->scriptData->byteCode[n+AS_PTR_SIZE]);
|
|
|
|
calledFunc = engine->scriptFunctions[funcId];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if( bc == asBC_CALLBND )
|
|
|
|
{
|
|
|
|
// Find the function from the engine's bind array
|
|
|
|
int funcId = asBC_INTARG(&func->scriptData->byteCode[n]);
|
|
|
|
calledFunc = engine->importedFunctions[funcId & ~FUNC_IMPORTED]->importedFunctionSignature;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if( bc == asBC_CallPtr )
|
|
|
|
{
|
|
|
|
int var = asBC_SWORDARG0(&func->scriptData->byteCode[n]);
|
|
|
|
asUINT v;
|
|
|
|
// Find the funcdef from the local variable
|
|
|
|
for( v = 0; v < func->scriptData->objVariablePos.GetLength(); v++ )
|
|
|
|
{
|
|
|
|
if( func->scriptData->objVariablePos[v] == var )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
calledFunc = CastToFuncdefType(func->scriptData->objVariableTypes[v])->funcdef;
|
2014-10-27 18:43:09 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( !calledFunc )
|
|
|
|
{
|
|
|
|
// Look in parameters
|
|
|
|
int paramPos = 0;
|
|
|
|
if( func->objectType )
|
|
|
|
paramPos -= AS_PTR_SIZE;
|
|
|
|
if( func->DoesReturnOnStack() )
|
|
|
|
paramPos -= AS_PTR_SIZE;
|
|
|
|
for( v = 0; v < func->parameterTypes.GetLength(); v++ )
|
|
|
|
{
|
|
|
|
if( var == paramPos )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
calledFunc = CastToFuncdefType(func->parameterTypes[v].GetTypeInfo())->funcdef;
|
2014-10-27 18:43:09 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
paramPos -= func->parameterTypes[v].GetSizeOnStackDWords();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if( bc == asBC_REFCPY ||
|
|
|
|
bc == asBC_COPY )
|
|
|
|
{
|
|
|
|
// In this case we know there is only 1 pointer on the stack above
|
|
|
|
asASSERT( offset == AS_PTR_SIZE );
|
|
|
|
return offset + (1 - AS_PTR_SIZE);
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Keep track of the stack size between the
|
2016-02-17 23:07:55 -05:00
|
|
|
// instruction that needs to be adjusted and the call
|
|
|
|
stackDelta += asBCInfo[bc].stackInc;
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
n += asBCTypeSize[asBCInfo[bc].type];
|
|
|
|
}
|
|
|
|
|
|
|
|
asASSERT( calledFunc );
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Count the number of pointers pushed on the stack above the
|
2014-10-27 18:43:09 -04:00
|
|
|
// current offset, and then adjust the offset accordingly
|
|
|
|
asUINT numPtrs = 0;
|
2016-02-17 23:07:55 -05:00
|
|
|
int currOffset = -stackDelta;
|
2017-11-20 08:02:33 -05:00
|
|
|
if( offset > currOffset && calledFunc->GetObjectType() && !bcAlloc )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
currOffset += AS_PTR_SIZE;
|
2016-02-17 23:07:55 -05:00
|
|
|
if( currOffset > 0 )
|
|
|
|
numPtrs++;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
if( offset > currOffset && calledFunc->DoesReturnOnStack() )
|
|
|
|
{
|
|
|
|
currOffset += AS_PTR_SIZE;
|
2016-02-17 23:07:55 -05:00
|
|
|
if( currOffset > 0 )
|
|
|
|
numPtrs++;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
for( asUINT p = 0; p < calledFunc->parameterTypes.GetLength(); p++ )
|
|
|
|
{
|
|
|
|
if( offset <= currOffset ) break;
|
|
|
|
|
|
|
|
if( !calledFunc->parameterTypes[p].IsPrimitive() ||
|
|
|
|
calledFunc->parameterTypes[p].IsReference() )
|
|
|
|
{
|
|
|
|
// objects and references are passed by pointer
|
|
|
|
currOffset += AS_PTR_SIZE;
|
2016-02-17 23:07:55 -05:00
|
|
|
if( currOffset > 0 )
|
|
|
|
numPtrs++;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// The variable arg ? has an additional 32bit int with the typeid
|
|
|
|
if( calledFunc->parameterTypes[p].IsAnyType() )
|
|
|
|
currOffset += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// built-in primitives or enums are passed by value
|
|
|
|
asASSERT( calledFunc->parameterTypes[p].IsPrimitive() );
|
|
|
|
currOffset += calledFunc->parameterTypes[p].GetSizeOnStackDWords();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The get offset must match one of the parameter offsets
|
|
|
|
asASSERT( offset == currOffset );
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
return offset + numPtrs * (1 - AS_PTR_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCWriter::WriteByteCode(asCScriptFunction *func)
|
|
|
|
{
|
|
|
|
asDWORD *bc = func->scriptData->byteCode.AddressOf();
|
|
|
|
size_t length = func->scriptData->byteCode.GetLength();
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// The length cannot be stored, because it is platform dependent,
|
2014-10-27 18:43:09 -04:00
|
|
|
// instead we store the number of instructions
|
|
|
|
asUINT count = bytecodeNbrByPos[bytecodeNbrByPos.GetLength()-1] + 1;
|
|
|
|
WriteEncodedInt64(count);
|
|
|
|
|
|
|
|
asDWORD *startBC = bc;
|
|
|
|
while( length )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asDWORD tmpBC[4]; // The biggest instructions take up 4 DWORDs
|
2014-10-27 18:43:09 -04:00
|
|
|
asDWORD c = *(asBYTE*)bc;
|
|
|
|
|
|
|
|
// Copy the instruction to a temp buffer so we can work on it before saving
|
2017-11-20 08:02:33 -05:00
|
|
|
memcpy(tmpBC, bc, asBCTypeSize[asBCInfo[c].type]*sizeof(asDWORD));
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
if( c == asBC_ALLOC ) // PTR_DW_ARG
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// Translate the object type
|
|
|
|
asCObjectType *ot = *(asCObjectType**)(tmpBC+1);
|
|
|
|
*(asPWORD*)(tmpBC+1) = FindTypeInfoIdx(ot);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Translate the constructor func id, unless it is 0
|
2017-11-20 08:02:33 -05:00
|
|
|
if( *(int*)&tmpBC[1+AS_PTR_SIZE] != 0 )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
// Increment 1 to the translated function id, as 0 will be reserved for no function
|
2017-11-20 08:02:33 -05:00
|
|
|
*(int*)&tmpBC[1+AS_PTR_SIZE] = 1+FindFunctionIndex(engine->scriptFunctions[*(int*)&tmpBC[1+AS_PTR_SIZE]]);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( c == asBC_REFCPY || // PTR_ARG
|
|
|
|
c == asBC_RefCpyV || // wW_PTR_ARG
|
|
|
|
c == asBC_OBJTYPE ) // PTR_ARG
|
|
|
|
{
|
|
|
|
// Translate object type pointers into indices
|
2017-11-20 08:02:33 -05:00
|
|
|
*(asPWORD*)(tmpBC+1) = FindTypeInfoIdx(*(asCObjectType**)(tmpBC+1));
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_JitEntry ) // PTR_ARG
|
|
|
|
{
|
|
|
|
// We don't store the JIT argument
|
2017-11-20 08:02:33 -05:00
|
|
|
*(asPWORD*)(tmpBC+1) = 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_TYPEID || // DW_ARG
|
|
|
|
c == asBC_Cast ) // DW_ARG
|
|
|
|
{
|
|
|
|
// Translate type ids into indices
|
2017-11-20 08:02:33 -05:00
|
|
|
*(int*)(tmpBC+1) = FindTypeIdIdx(*(int*)(tmpBC+1));
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_ADDSi || // W_DW_ARG
|
|
|
|
c == asBC_LoadThisR ) // W_DW_ARG
|
|
|
|
{
|
|
|
|
// Translate property offsets into indices
|
2017-11-20 08:02:33 -05:00
|
|
|
*(((short*)tmpBC)+1) = (short)FindObjectPropIndex(*(((short*)tmpBC)+1), *(int*)(tmpBC+1), bc);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Translate type ids into indices
|
2017-11-20 08:02:33 -05:00
|
|
|
*(int*)(tmpBC+1) = FindTypeIdIdx(*(int*)(tmpBC+1));
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_LoadRObjR || // rW_W_DW_ARG
|
|
|
|
c == asBC_LoadVObjR ) // rW_W_DW_ARG
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *ot = engine->GetObjectTypeFromTypeId(*(int*)(tmpBC+2));
|
2014-10-27 18:43:09 -04:00
|
|
|
if( ot->flags & asOBJ_LIST_PATTERN )
|
|
|
|
{
|
|
|
|
// List patterns have a different way of translating the offsets
|
|
|
|
SListAdjuster *listAdj = listAdjusters[listAdjusters.GetLength()-1];
|
2017-11-20 08:02:33 -05:00
|
|
|
*(((short*)tmpBC)+2) = (short)listAdj->AdjustOffset(*(((short*)tmpBC)+2), ot);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Translate property offsets into indices
|
2017-11-20 08:02:33 -05:00
|
|
|
*(((short*)tmpBC)+2) = (short)FindObjectPropIndex(*(((short*)tmpBC)+2), *(int*)(tmpBC+2), bc);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Translate type ids into indices
|
2017-11-20 08:02:33 -05:00
|
|
|
*(int*)(tmpBC+2) = FindTypeIdIdx(*(int*)(tmpBC+2));
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_COPY ) // W_DW_ARG
|
|
|
|
{
|
|
|
|
// Translate type ids into indices
|
2017-11-20 08:02:33 -05:00
|
|
|
*(int*)(tmpBC+1) = FindTypeIdIdx(*(int*)(tmpBC+1));
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Update the WORDARG0 to 0, as this will be recalculated on the target platform
|
2017-11-20 08:02:33 -05:00
|
|
|
asBC_WORDARG0(tmpBC) = 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_RET ) // W_ARG
|
|
|
|
{
|
|
|
|
// Save with arg 0, as this will be recalculated on the target platform
|
2017-11-20 08:02:33 -05:00
|
|
|
asBC_WORDARG0(tmpBC) = 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_CALL || // DW_ARG
|
|
|
|
c == asBC_CALLINTF || // DW_ARG
|
2016-02-17 23:07:55 -05:00
|
|
|
c == asBC_CALLSYS || // DW_ARG
|
|
|
|
c == asBC_Thiscall1 ) // DW_ARG
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
// Translate the function id
|
2017-11-20 08:02:33 -05:00
|
|
|
*(int*)(tmpBC+1) = FindFunctionIndex(engine->scriptFunctions[*(int*)(tmpBC+1)]);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_FuncPtr ) // PTR_ARG
|
|
|
|
{
|
|
|
|
// Translate the function pointer
|
2017-11-20 08:02:33 -05:00
|
|
|
*(asPWORD*)(tmpBC+1) = FindFunctionIndex(*(asCScriptFunction**)(tmpBC+1));
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_CALLBND ) // DW_ARG
|
|
|
|
{
|
|
|
|
// Translate the function id
|
2017-11-20 08:02:33 -05:00
|
|
|
int funcId = tmpBC[1];
|
2014-10-27 18:43:09 -04:00
|
|
|
for( asUINT n = 0; n < module->bindInformations.GetLength(); n++ )
|
|
|
|
if( module->bindInformations[n]->importedFunctionSignature->id == funcId )
|
|
|
|
{
|
|
|
|
funcId = n;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
tmpBC[1] = funcId;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_PGA || // PTR_ARG
|
2017-11-20 08:02:33 -05:00
|
|
|
c == asBC_PshGPtr || // PTR_ARG
|
2014-10-27 18:43:09 -04:00
|
|
|
c == asBC_LDG || // PTR_ARG
|
|
|
|
c == asBC_PshG4 || // PTR_ARG
|
|
|
|
c == asBC_LdGRdR4 || // wW_PTR_ARG
|
|
|
|
c == asBC_CpyGtoV4 || // wW_PTR_ARG
|
|
|
|
c == asBC_CpyVtoG4 || // rW_PTR_ARG
|
|
|
|
c == asBC_SetG4 ) // PTR_DW_ARG
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// Check if the address is a global property or a string constant
|
|
|
|
void *ptr = *(void**)(tmpBC + 1);
|
|
|
|
if (engine->varAddressMap.MoveTo(0, ptr))
|
|
|
|
{
|
|
|
|
// Translate global variable pointers into indices
|
|
|
|
// Flag the first bit to signal global property
|
|
|
|
*(asPWORD*)(tmpBC + 1) = (FindGlobalPropPtrIndex(*(void**)(tmpBC + 1)) << 1) + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Only PGA and PshGPtr can hold string constants
|
|
|
|
asASSERT(c == asBC_PGA || c == asBC_PshGPtr);
|
|
|
|
|
|
|
|
// Translate string constants into indices
|
|
|
|
// Leave the first bit clear to signal string constant
|
|
|
|
*(asPWORD*)(tmpBC + 1) = FindStringConstantIndex(*(void**)(tmpBC + 1)) << 1;
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_JMP || // DW_ARG
|
|
|
|
c == asBC_JZ ||
|
|
|
|
c == asBC_JNZ ||
|
|
|
|
c == asBC_JLowZ ||
|
|
|
|
c == asBC_JLowNZ ||
|
|
|
|
c == asBC_JS ||
|
|
|
|
c == asBC_JNS ||
|
|
|
|
c == asBC_JP ||
|
|
|
|
c == asBC_JNP ) // The JMPP instruction doesn't need modification
|
|
|
|
{
|
|
|
|
// Get the DWORD offset from arg
|
2017-11-20 08:02:33 -05:00
|
|
|
int offset = *(int*)(tmpBC+1);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Determine instruction number for next instruction and destination
|
2015-05-11 19:40:43 -04:00
|
|
|
int bcSeqNum = bytecodeNbrByPos[asUINT(bc - startBC)] + 1;
|
2014-10-27 18:43:09 -04:00
|
|
|
asDWORD *targetBC = bc + 2 + offset;
|
2015-05-11 19:40:43 -04:00
|
|
|
int targetBcSeqNum = bytecodeNbrByPos[asUINT(targetBC - startBC)];
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Set the offset in number of instructions
|
2017-11-20 08:02:33 -05:00
|
|
|
*(int*)(tmpBC+1) = targetBcSeqNum - bcSeqNum;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_GETOBJ || // W_ARG
|
|
|
|
c == asBC_GETOBJREF ||
|
2017-11-20 08:02:33 -05:00
|
|
|
c == asBC_GETREF ||
|
|
|
|
c == asBC_ChkNullS )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
// Adjust the offset according to the function call that comes after
|
2017-11-20 08:02:33 -05:00
|
|
|
asBC_WORDARG0(tmpBC) = (asWORD)AdjustGetOffset(asBC_WORDARG0(tmpBC), func, asDWORD(bc - startBC));
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_AllocMem )
|
|
|
|
{
|
|
|
|
// It's not necessary to store the size of the list buffer, as it will be recalculated in the reader
|
2017-11-20 08:02:33 -05:00
|
|
|
asBC_DWORDARG(tmpBC) = 0;
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Determine the type of the list pattern from the variable
|
2017-11-20 08:02:33 -05:00
|
|
|
short var = asBC_WORDARG0(tmpBC);
|
|
|
|
asCObjectType *ot = CastToObjectType(func->GetTypeInfoOfLocalVar(var));
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Create this helper object to adjust the offset of the elements accessed in the buffer
|
|
|
|
listAdjusters.PushLast(asNEW(SListAdjuster)(ot));
|
|
|
|
}
|
|
|
|
else if( c == asBC_FREE ) // wW_PTR_ARG
|
|
|
|
{
|
|
|
|
// Translate object type pointers into indices
|
2017-11-20 08:02:33 -05:00
|
|
|
asCObjectType *ot = *(asCObjectType**)(tmpBC+1);
|
|
|
|
*(asPWORD*)(tmpBC+1) = FindTypeInfoIdx(ot);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Pop and destroy the list adjuster helper that was created with asBC_AllocMem
|
|
|
|
if( ot && (ot->flags & asOBJ_LIST_PATTERN) )
|
|
|
|
{
|
|
|
|
SListAdjuster *list = listAdjusters.PopLast();
|
|
|
|
asDELETE(list, SListAdjuster);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( c == asBC_SetListSize )
|
|
|
|
{
|
|
|
|
// Adjust the offset in the initialization list
|
|
|
|
SListAdjuster *listAdj = listAdjusters[listAdjusters.GetLength()-1];
|
2017-11-20 08:02:33 -05:00
|
|
|
tmpBC[1] = listAdj->AdjustOffset(tmpBC[1], listAdj->patternType);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Tell the adjuster how many repeated values there are
|
2017-11-20 08:02:33 -05:00
|
|
|
listAdj->SetRepeatCount(tmpBC[2]);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_PshListElmnt ) // W_DW_ARG
|
|
|
|
{
|
|
|
|
// Adjust the offset in the initialization list
|
|
|
|
SListAdjuster *listAdj = listAdjusters[listAdjusters.GetLength()-1];
|
2017-11-20 08:02:33 -05:00
|
|
|
tmpBC[1] = listAdj->AdjustOffset(tmpBC[1], listAdj->patternType);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
else if( c == asBC_SetListType )
|
|
|
|
{
|
|
|
|
// Adjust the offset in the initialization list
|
|
|
|
SListAdjuster *listAdj = listAdjusters[listAdjusters.GetLength()-1];
|
2017-11-20 08:02:33 -05:00
|
|
|
tmpBC[1] = listAdj->AdjustOffset(tmpBC[1], listAdj->patternType);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Inform the adjuster of the type id of the next element
|
2017-11-20 08:02:33 -05:00
|
|
|
listAdj->SetNextType(tmpBC[2]);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Translate the type id
|
2017-11-20 08:02:33 -05:00
|
|
|
tmpBC[2] = FindTypeIdIdx(tmpBC[2]);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
// Adjust the variable offsets
|
|
|
|
switch( asBCInfo[c].type )
|
|
|
|
{
|
|
|
|
case asBCTYPE_wW_ARG:
|
|
|
|
case asBCTYPE_rW_DW_ARG:
|
|
|
|
case asBCTYPE_wW_QW_ARG:
|
|
|
|
case asBCTYPE_rW_ARG:
|
|
|
|
case asBCTYPE_wW_DW_ARG:
|
|
|
|
case asBCTYPE_wW_W_ARG:
|
|
|
|
case asBCTYPE_rW_QW_ARG:
|
|
|
|
case asBCTYPE_rW_W_DW_ARG:
|
|
|
|
case asBCTYPE_rW_DW_DW_ARG:
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asBC_SWORDARG0(tmpBC) = (short)AdjustStackPosition(asBC_SWORDARG0(tmpBC));
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case asBCTYPE_wW_rW_ARG:
|
|
|
|
case asBCTYPE_wW_rW_DW_ARG:
|
|
|
|
case asBCTYPE_rW_rW_ARG:
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asBC_SWORDARG0(tmpBC) = (short)AdjustStackPosition(asBC_SWORDARG0(tmpBC));
|
|
|
|
asBC_SWORDARG1(tmpBC) = (short)AdjustStackPosition(asBC_SWORDARG1(tmpBC));
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case asBCTYPE_wW_rW_rW_ARG:
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
asBC_SWORDARG0(tmpBC) = (short)AdjustStackPosition(asBC_SWORDARG0(tmpBC));
|
|
|
|
asBC_SWORDARG1(tmpBC) = (short)AdjustStackPosition(asBC_SWORDARG1(tmpBC));
|
|
|
|
asBC_SWORDARG2(tmpBC) = (short)AdjustStackPosition(asBC_SWORDARG2(tmpBC));
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// The other types don't treat variables so won't be modified
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// TODO: bytecode: Must make sure that floats and doubles are always stored the same way regardless of platform.
|
2014-10-27 18:43:09 -04:00
|
|
|
// Some platforms may not use the IEEE 754 standard, in which case it is necessary to encode the values
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Now store the instruction in the smallest possible way
|
|
|
|
switch( asBCInfo[c].type )
|
|
|
|
{
|
|
|
|
case asBCTYPE_NO_ARG:
|
|
|
|
{
|
|
|
|
// Just write 1 byte
|
|
|
|
asBYTE b = (asBYTE)c;
|
|
|
|
WriteData(&b, 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_W_ARG:
|
|
|
|
case asBCTYPE_wW_ARG:
|
|
|
|
case asBCTYPE_rW_ARG:
|
|
|
|
{
|
|
|
|
// Write the instruction code
|
|
|
|
asBYTE b = (asBYTE)c;
|
|
|
|
WriteData(&b, 1);
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
// Write the argument
|
2017-11-20 08:02:33 -05:00
|
|
|
short w = *(((short*)tmpBC)+1);
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(w);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_rW_DW_ARG:
|
|
|
|
case asBCTYPE_wW_DW_ARG:
|
|
|
|
case asBCTYPE_W_DW_ARG:
|
|
|
|
{
|
|
|
|
// Write the instruction code
|
|
|
|
asBYTE b = (asBYTE)c;
|
|
|
|
WriteData(&b, 1);
|
|
|
|
|
|
|
|
// Write the word argument
|
2017-11-20 08:02:33 -05:00
|
|
|
short w = *(((short*)tmpBC)+1);
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(w);
|
|
|
|
|
|
|
|
// Write the dword argument
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteEncodedInt64((int)tmpBC[1]);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_DW_ARG:
|
|
|
|
{
|
|
|
|
// Write the instruction code
|
|
|
|
asBYTE b = (asBYTE)c;
|
|
|
|
WriteData(&b, 1);
|
|
|
|
|
|
|
|
// Write the argument
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteEncodedInt64((int)tmpBC[1]);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_DW_DW_ARG:
|
|
|
|
{
|
|
|
|
// Write the instruction code
|
|
|
|
asBYTE b = (asBYTE)c;
|
|
|
|
WriteData(&b, 1);
|
|
|
|
|
|
|
|
// Write the dword argument
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteEncodedInt64((int)tmpBC[1]);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Write the dword argument
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteEncodedInt64((int)tmpBC[2]);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_wW_rW_rW_ARG:
|
|
|
|
{
|
|
|
|
// Write the instruction code
|
|
|
|
asBYTE b = (asBYTE)c;
|
|
|
|
WriteData(&b, 1);
|
|
|
|
|
|
|
|
// Write the first argument
|
2017-11-20 08:02:33 -05:00
|
|
|
short w = *(((short*)tmpBC)+1);
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(w);
|
|
|
|
|
|
|
|
// Write the second argument
|
2017-11-20 08:02:33 -05:00
|
|
|
w = *(((short*)tmpBC)+2);
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(w);
|
|
|
|
|
|
|
|
// Write the third argument
|
2017-11-20 08:02:33 -05:00
|
|
|
w = *(((short*)tmpBC)+3);
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(w);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_wW_rW_ARG:
|
|
|
|
case asBCTYPE_rW_rW_ARG:
|
|
|
|
case asBCTYPE_wW_W_ARG:
|
|
|
|
{
|
|
|
|
// Write the instruction code
|
|
|
|
asBYTE b = (asBYTE)c;
|
|
|
|
WriteData(&b, 1);
|
|
|
|
|
|
|
|
// Write the first argument
|
2017-11-20 08:02:33 -05:00
|
|
|
short w = *(((short*)tmpBC)+1);
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(w);
|
|
|
|
|
|
|
|
// Write the second argument
|
2017-11-20 08:02:33 -05:00
|
|
|
w = *(((short*)tmpBC)+2);
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(w);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_wW_rW_DW_ARG:
|
|
|
|
case asBCTYPE_rW_W_DW_ARG:
|
|
|
|
{
|
|
|
|
// Write the instruction code
|
|
|
|
asBYTE b = (asBYTE)c;
|
|
|
|
WriteData(&b, 1);
|
|
|
|
|
|
|
|
// Write the first argument
|
2017-11-20 08:02:33 -05:00
|
|
|
short w = *(((short*)tmpBC)+1);
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(w);
|
|
|
|
|
|
|
|
// Write the second argument
|
2017-11-20 08:02:33 -05:00
|
|
|
w = *(((short*)tmpBC)+2);
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(w);
|
|
|
|
|
|
|
|
// Write the third argument
|
2017-11-20 08:02:33 -05:00
|
|
|
int dw = tmpBC[2];
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(dw);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_QW_ARG:
|
|
|
|
{
|
|
|
|
// Write the instruction code
|
|
|
|
asBYTE b = (asBYTE)c;
|
|
|
|
WriteData(&b, 1);
|
|
|
|
|
|
|
|
// Write the argument
|
2017-11-20 08:02:33 -05:00
|
|
|
asQWORD qw = *(asQWORD*)&tmpBC[1];
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(qw);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_QW_DW_ARG:
|
|
|
|
{
|
|
|
|
// Write the instruction code
|
|
|
|
asBYTE b = (asBYTE)c;
|
|
|
|
WriteData(&b, 1);
|
|
|
|
|
|
|
|
// Write the argument
|
2017-11-20 08:02:33 -05:00
|
|
|
asQWORD qw = *(asQWORD*)&tmpBC[1];
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(qw);
|
|
|
|
|
|
|
|
// Write the second argument
|
2017-11-20 08:02:33 -05:00
|
|
|
int dw = tmpBC[3];
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(dw);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_rW_QW_ARG:
|
|
|
|
case asBCTYPE_wW_QW_ARG:
|
|
|
|
{
|
|
|
|
// Write the instruction code
|
|
|
|
asBYTE b = (asBYTE)c;
|
|
|
|
WriteData(&b, 1);
|
|
|
|
|
|
|
|
// Write the first argument
|
2017-11-20 08:02:33 -05:00
|
|
|
short w = *(((short*)tmpBC)+1);
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(w);
|
|
|
|
|
|
|
|
// Write the argument
|
2017-11-20 08:02:33 -05:00
|
|
|
asQWORD qw = *(asQWORD*)&tmpBC[1];
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(qw);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case asBCTYPE_rW_DW_DW_ARG:
|
|
|
|
{
|
|
|
|
// Write the instruction code
|
|
|
|
asBYTE b = (asBYTE)c;
|
|
|
|
WriteData(&b, 1);
|
|
|
|
|
|
|
|
// Write the short argument
|
2017-11-20 08:02:33 -05:00
|
|
|
short w = *(((short*)tmpBC)+1);
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteEncodedInt64(w);
|
|
|
|
|
|
|
|
// Write the dword argument
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteEncodedInt64((int)tmpBC[1]);
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Write the dword argument
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteEncodedInt64((int)tmpBC[2]);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
// This should never happen
|
|
|
|
asASSERT(false);
|
|
|
|
|
|
|
|
// Store the bc as is
|
|
|
|
for( int n = 0; n < asBCTypeSize[asBCInfo[c].type]; n++ )
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteData(&tmpBC[n], 4);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move to the next instruction
|
|
|
|
bc += asBCTypeSize[asBCInfo[c].type];
|
|
|
|
length -= asBCTypeSize[asBCInfo[c].type];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
asCWriter::SListAdjuster::SListAdjuster(asCObjectType *ot) : patternType(ot), repeatCount(0), entries(0), lastOffset(-1), nextOffset(0), nextTypeId(-1)
|
2017-11-20 08:02:33 -05:00
|
|
|
{
|
|
|
|
asASSERT( ot && (ot->flags & asOBJ_LIST_PATTERN) );
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Find the first expected value in the list
|
|
|
|
asSListPatternNode *node = ot->engine->scriptFunctions[patternType->templateSubTypes[0].GetBehaviour()->listFactory]->listPattern;
|
|
|
|
asASSERT( node && node->type == asLPT_START );
|
|
|
|
patternNode = node->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
int asCWriter::SListAdjuster::AdjustOffset(int offset, asCObjectType *listPatternType)
|
|
|
|
{
|
|
|
|
// TODO: cleanup: The listPatternType parameter is not needed
|
|
|
|
asASSERT( patternType == listPatternType );
|
|
|
|
UNUSED_VAR(listPatternType);
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
asASSERT( offset >= lastOffset );
|
|
|
|
|
|
|
|
// If it is the same offset being accessed again, just return the same adjusted value
|
|
|
|
if( offset == lastOffset )
|
|
|
|
return entries-1;
|
|
|
|
|
2015-05-11 19:40:43 -04:00
|
|
|
asASSERT( offset >= nextOffset );
|
|
|
|
|
|
|
|
// Update last offset for next call
|
2014-10-27 18:43:09 -04:00
|
|
|
lastOffset = offset;
|
|
|
|
|
|
|
|
// What is being expected at this position?
|
2015-05-11 19:40:43 -04:00
|
|
|
if( patternNode->type == asLPT_REPEAT || patternNode->type == asLPT_REPEAT_SAME )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
// Don't move the patternNode yet because the caller must make a call to SetRepeatCount too
|
2015-05-11 19:40:43 -04:00
|
|
|
nextOffset = offset + 4;
|
2014-10-27 18:43:09 -04:00
|
|
|
return entries++;
|
|
|
|
}
|
|
|
|
else if( patternNode->type == asLPT_TYPE )
|
|
|
|
{
|
|
|
|
const asCDataType &dt = reinterpret_cast<asSListPatternDataTypeNode*>(patternNode)->dataType;
|
|
|
|
if( dt.GetTokenType() == ttQuestion )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// The bytecode need to inform the type that will
|
|
|
|
// come next and then adjust that position too before
|
2014-10-27 18:43:09 -04:00
|
|
|
// we can move to the next node
|
|
|
|
if( nextTypeId != -1 )
|
|
|
|
{
|
2015-05-11 19:40:43 -04:00
|
|
|
nextOffset = offset + 4;
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
if( repeatCount > 0 )
|
|
|
|
repeatCount--;
|
|
|
|
|
|
|
|
// Only move the patternNode if we're not expecting any more repeated entries
|
|
|
|
if( repeatCount == 0 )
|
|
|
|
patternNode = patternNode->next;
|
|
|
|
|
|
|
|
nextTypeId = -1;
|
|
|
|
}
|
|
|
|
}
|
2017-11-20 08:02:33 -05:00
|
|
|
else
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
if( repeatCount > 0 )
|
2015-05-11 19:40:43 -04:00
|
|
|
{
|
|
|
|
// Was any value skipped?
|
|
|
|
asUINT size;
|
2017-11-20 08:02:33 -05:00
|
|
|
if( dt.IsObjectHandle() || (dt.GetTypeInfo() && (dt.GetTypeInfo()->flags & asOBJ_REF)) )
|
2015-05-11 19:40:43 -04:00
|
|
|
size = AS_PTR_SIZE*4;
|
|
|
|
else
|
|
|
|
size = dt.GetSizeInMemoryBytes();
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
while( nextOffset <= offset )
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
nextOffset += size;
|
|
|
|
|
|
|
|
// Align the offset on 4 byte boundaries
|
|
|
|
if( size >= 4 && (nextOffset & 0x3) )
|
|
|
|
nextOffset += 4 - (nextOffset & 0x3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( --count > 0 )
|
|
|
|
{
|
|
|
|
// Skip these values
|
|
|
|
repeatCount -= count;
|
|
|
|
entries += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
nextOffset = offset + size;
|
2014-10-27 18:43:09 -04:00
|
|
|
repeatCount--;
|
2015-05-11 19:40:43 -04:00
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Only move the patternNode if we're not expecting any more repeated entries
|
|
|
|
if( repeatCount == 0 )
|
|
|
|
patternNode = patternNode->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries++;
|
|
|
|
}
|
|
|
|
else if( patternNode->type == asLPT_START )
|
|
|
|
{
|
|
|
|
if( repeatCount > 0 )
|
|
|
|
repeatCount--;
|
|
|
|
SInfo info = {repeatCount, patternNode};
|
|
|
|
stack.PushLast(info);
|
|
|
|
|
|
|
|
repeatCount = 0;
|
|
|
|
patternNode = patternNode->next;
|
|
|
|
|
|
|
|
lastOffset--;
|
|
|
|
return AdjustOffset(offset, listPatternType);
|
|
|
|
}
|
|
|
|
else if( patternNode->type == asLPT_END )
|
|
|
|
{
|
|
|
|
SInfo info = stack.PopLast();
|
|
|
|
repeatCount = info.repeatCount;
|
|
|
|
if( repeatCount )
|
|
|
|
patternNode = info.startNode;
|
|
|
|
else
|
|
|
|
patternNode = patternNode->next;
|
|
|
|
|
|
|
|
lastOffset--;
|
|
|
|
return AdjustOffset(offset, listPatternType);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Something is wrong with the pattern list declaration
|
|
|
|
asASSERT( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCWriter::SListAdjuster::SetRepeatCount(asUINT rc)
|
|
|
|
{
|
|
|
|
// Make sure the list is expecting a repeat at this location
|
2015-05-11 19:40:43 -04:00
|
|
|
asASSERT( patternNode->type == asLPT_REPEAT || patternNode->type == asLPT_REPEAT_SAME );
|
2014-10-27 18:43:09 -04:00
|
|
|
|
|
|
|
// Now move to the next patternNode
|
|
|
|
patternNode = patternNode->next;
|
|
|
|
|
|
|
|
repeatCount = rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCWriter::SListAdjuster::SetNextType(int typeId)
|
|
|
|
{
|
|
|
|
// Make sure the list is expecting a type at this location
|
2017-11-20 08:02:33 -05:00
|
|
|
asASSERT( patternNode->type == asLPT_TYPE &&
|
2014-10-27 18:43:09 -04:00
|
|
|
reinterpret_cast<asSListPatternDataTypeNode*>(patternNode)->dataType.GetTokenType() == ttQuestion );
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Inform the type id for the next adjustment
|
2014-10-27 18:43:09 -04:00
|
|
|
nextTypeId = typeId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCWriter::WriteUsedTypeIds()
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("asCWriter::WriteUsedTypeIds");
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
asUINT count = (asUINT)usedTypeIds.GetLength();
|
|
|
|
WriteEncodedInt64(count);
|
|
|
|
for( asUINT n = 0; n < count; n++ )
|
|
|
|
{
|
|
|
|
asCDataType dt = engine->GetDataTypeFromTypeId(usedTypeIds[n]);
|
|
|
|
WriteDataType(&dt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int asCWriter::FindGlobalPropPtrIndex(void *ptr)
|
|
|
|
{
|
|
|
|
int i = usedGlobalProperties.IndexOf(ptr);
|
|
|
|
if( i >= 0 ) return i;
|
|
|
|
|
|
|
|
usedGlobalProperties.PushLast(ptr);
|
|
|
|
return (int)usedGlobalProperties.GetLength()-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCWriter::WriteUsedGlobalProps()
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("asCWriter::WriteUsedGlobalProps");
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
int c = (int)usedGlobalProperties.GetLength();
|
|
|
|
WriteEncodedInt64(c);
|
|
|
|
|
|
|
|
for( int n = 0; n < c; n++ )
|
|
|
|
{
|
|
|
|
asPWORD *p = (asPWORD*)usedGlobalProperties[n];
|
2017-11-20 08:02:33 -05:00
|
|
|
|
2016-02-17 23:07:55 -05:00
|
|
|
// Find the property descriptor from the address
|
2014-10-27 18:43:09 -04:00
|
|
|
asCGlobalProperty *prop = 0;
|
2016-02-17 23:07:55 -05:00
|
|
|
asSMapNode<void*, asCGlobalProperty*> *cursor;
|
|
|
|
if( engine->varAddressMap.MoveTo(&cursor, p) )
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
prop = engine->varAddressMap.GetValue(cursor);
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
asASSERT(prop);
|
|
|
|
|
|
|
|
// Store the name and type of the property so we can find it again on loading
|
|
|
|
WriteString(&prop->name);
|
|
|
|
WriteString(&prop->nameSpace->name);
|
|
|
|
WriteDataType(&prop->type);
|
|
|
|
|
|
|
|
// Also store whether the property is a module property or a registered property
|
2016-02-17 23:07:55 -05:00
|
|
|
char moduleProp = 0;
|
|
|
|
if( prop->realAddress == 0 )
|
|
|
|
moduleProp = 1;
|
2014-10-27 18:43:09 -04:00
|
|
|
WriteData(&moduleProp, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void asCWriter::WriteUsedObjectProps()
|
|
|
|
{
|
2016-02-17 23:07:55 -05:00
|
|
|
TimeIt("asCWriter::WriteUsedObjectProps");
|
|
|
|
|
2014-10-27 18:43:09 -04:00
|
|
|
int c = (int)usedObjectProperties.GetLength();
|
|
|
|
WriteEncodedInt64(c);
|
|
|
|
|
|
|
|
for( asUINT n = 0; n < usedObjectProperties.GetLength(); n++ )
|
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
WriteTypeInfo(usedObjectProperties[n].objType);
|
|
|
|
WriteString(&usedObjectProperties[n].prop->name);
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 18:43:09 -04:00
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
int asCWriter::FindObjectPropIndex(short offset, int typeId, asDWORD *bc)
|
|
|
|
{
|
|
|
|
// If the last property was a composite property, then just return 0, because it won't be translated
|
|
|
|
static bool lastWasComposite = false;
|
|
|
|
if (lastWasComposite)
|
|
|
|
{
|
|
|
|
lastWasComposite = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
asCObjectType *objType = engine->GetObjectTypeFromTypeId(typeId);
|
|
|
|
asCObjectProperty *objProp = 0;
|
|
|
|
|
|
|
|
// Look for composite properties first
|
|
|
|
for (asUINT n = 0; objProp == 0 && n < objType->properties.GetLength(); n++)
|
|
|
|
{
|
|
|
|
// TODO: Composite: Perhaps it would be better to add metadata to the bytecode instruction to give the exact property.
|
|
|
|
// That would also allow me to remove the typeId from the bytecode instruction itself
|
|
|
|
// Or perhaps a new bytecode instruction all together for accessing composite properties
|
|
|
|
// One that would do both offsets and indirection in a single go.
|
|
|
|
// TODO: Composite: Need to be able to handle instructions replaced in bytecode optimizations too
|
|
|
|
if (objType->properties[n]->compositeOffset == offset)
|
|
|
|
{
|
|
|
|
// This is a potential composite property. Need to check the following instructions to be sure
|
|
|
|
objProp = objType->properties[n];
|
|
|
|
asDWORD *bcTemp = bc;
|
|
|
|
bcTemp += asBCTypeSize[asBCInfo[*(asBYTE*)bcTemp].type];
|
|
|
|
if (objProp->isCompositeIndirect)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
2017-11-20 08:02:33 -05:00
|
|
|
// The next instruction would be a asBC_RDSPtr
|
|
|
|
if ((*(asBYTE*)bcTemp) != asBC_RDSPtr)
|
|
|
|
{
|
|
|
|
objProp = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bcTemp += asBCTypeSize[asBCInfo[*(asBYTE*)bcTemp].type];
|
|
|
|
}
|
|
|
|
// The next instruction would be asBC_ADDSi
|
|
|
|
if ((*(asBYTE*)bcTemp) != asBC_ADDSi)
|
|
|
|
{
|
|
|
|
objProp = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Make sure the offset is the expected one
|
|
|
|
if (*(((short*)bcTemp) + 1) != objProp->byteOffset)
|
|
|
|
{
|
|
|
|
objProp = 0;
|
|
|
|
continue;
|
2014-10-27 18:43:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// If none of the composite properties matched, then look for ordinary property
|
|
|
|
for (asUINT n = 0; objProp == 0 && n < objType->properties.GetLength(); n++)
|
|
|
|
{
|
|
|
|
if (objType->properties[n]->byteOffset == offset && !(objType->properties[n]->compositeOffset || objType->properties[n]->isCompositeIndirect))
|
|
|
|
objProp = objType->properties[n];
|
|
|
|
}
|
|
|
|
|
|
|
|
asASSERT(objProp);
|
|
|
|
|
|
|
|
// Remember if this is a composite property as the next call will then be for the same property
|
|
|
|
if (objProp->compositeOffset || objProp->isCompositeIndirect)
|
|
|
|
lastWasComposite = true;
|
|
|
|
|
|
|
|
// Now check if the same property has already been accessed
|
2014-10-27 18:43:09 -04:00
|
|
|
for( asUINT n = 0; n < usedObjectProperties.GetLength(); n++ )
|
|
|
|
{
|
|
|
|
if( usedObjectProperties[n].objType == objType &&
|
2017-11-20 08:02:33 -05:00
|
|
|
usedObjectProperties[n].prop == objProp )
|
2014-10-27 18:43:09 -04:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
// Insert the new property
|
|
|
|
SObjProp prop = {objType, objProp};
|
2014-10-27 18:43:09 -04:00
|
|
|
usedObjectProperties.PushLast(prop);
|
|
|
|
return (int)usedObjectProperties.GetLength() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int asCWriter::FindFunctionIndex(asCScriptFunction *func)
|
|
|
|
{
|
|
|
|
for( asUINT n = 0; n < usedFunctions.GetLength(); n++ )
|
|
|
|
{
|
|
|
|
if( usedFunctions[n] == func )
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
usedFunctions.PushLast(func);
|
|
|
|
return (int)usedFunctions.GetLength() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int asCWriter::FindTypeIdIdx(int typeId)
|
|
|
|
{
|
|
|
|
asUINT n;
|
|
|
|
for( n = 0; n < usedTypeIds.GetLength(); n++ )
|
|
|
|
{
|
|
|
|
if( usedTypeIds[n] == typeId )
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
usedTypeIds.PushLast(typeId);
|
|
|
|
return (int)usedTypeIds.GetLength() - 1;
|
|
|
|
}
|
|
|
|
|
2017-11-20 08:02:33 -05:00
|
|
|
int asCWriter::FindTypeInfoIdx(asCTypeInfo *obj)
|
2014-10-27 18:43:09 -04:00
|
|
|
{
|
|
|
|
asUINT n;
|
|
|
|
for( n = 0; n < usedTypes.GetLength(); n++ )
|
|
|
|
{
|
|
|
|
if( usedTypes[n] == obj )
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
usedTypes.PushLast(obj);
|
|
|
|
return (int)usedTypes.GetLength() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // AS_NO_COMPILER
|
|
|
|
|
|
|
|
END_AS_NAMESPACE
|
|
|
|
|