code:
/*
   AngelCode Scripting Library
   Copyright (c) 2003-2024 Andreas Jonsson

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

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

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

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

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

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

   Andreas Jonsson
   andreas@angelcode.com
*/

//
// as_restore.h
//
// Functions for saving and restoring module bytecode
// asCRestore was originally written by Dennis Bollyn, dennis@gyrbo.be
// It was later split in two classes asCReader and asCWriter by me

#ifndef AS_RESTORE_H
#define AS_RESTORE_H

#include "as_scriptengine.h"
#include "as_context.h"
#include "as_map.h"

BEGIN_AS_NAMESPACE

class asCReader
{
public:
	asCReader(asCModule *module, asIBinaryStream *stream, asCScriptEngine *engine);

	int Read(bool *wasDebugInfoStripped);

protected:
	asCModule       *module;
	asIBinaryStream *stream;
	asCScriptEngine *engine;
	bool             noDebugInfo;
	bool             error;
	asUINT           bytesRead;

	int                Error(const char *msg);

	int                ReadInner();

	int                ReadData(void *data, asUINT size);
	void               ReadString(asCString *str);
	asCScriptFunction *ReadFunction(bool &isNew, bool addToModule = true, bool addToEngine = true, bool addToGC = true, bool *isExternal = 0);
	void               ReadFunctionSignature(asCScriptFunction *func, asCObjectType **parentClass = 0);
	void               ReadGlobalProperty();
	void               ReadObjectProperty(asCObjectType *ot);
	void               ReadDataType(asCDataType *dt);
	asCTypeInfo       *ReadTypeInfo();
	void               ReadTypeDeclaration(asCTypeInfo *ot, int phase, bool *isExternal = 0);
	void               ReadByteCode(asCScriptFunction *func);
	asWORD             ReadEncodedUInt16();
	asUINT             ReadEncodedUInt();
	int                ReadEncodedInt();
	asQWORD            ReadEncodedUInt64();
	asUINT             SanityCheck(asUINT val, asUINT max);
	int                SanityCheck(int val, asUINT max);

	void ReadUsedTypeIds();
	void ReadUsedFunctions();
	void ReadUsedGlobalProps();
	void ReadUsedStringConstants();
	void ReadUsedObjectProps();

	asCTypeInfo *      FindType(int idx);
	int                FindTypeId(int idx);
	short              FindObjectPropOffset(asWORD index);
	asCScriptFunction *FindFunction(int idx);

	// After loading, each function needs to be translated to update pointers, function ids, etc
	void TranslateFunction(asCScriptFunction *func);
	void CalculateAdjustmentByPos(asCScriptFunction *func);
	int  AdjustStackPosition(int pos);
	int  AdjustGetOffset(int offset, asCScriptFunction *func, asDWORD programPos);
	void CalculateStackNeeded(asCScriptFunction *func);

	// Temporary storage for persisting variable data
	asCArray<int>                usedTypeIds;
	asCArray<asCTypeInfo*>       usedTypes;
	asCArray<asCScriptFunction*> usedFunctions;
	asCArray<void*>              usedGlobalProperties;
	asCArray<void*>              usedStringConstants;

	asCArray<asCScriptFunction*>  savedFunctions;
	asCArray<asCDataType>         savedDataTypes;
	asCArray<asCString>           savedStrings;

	asCArray<int>                 adjustByPos;
	asCArray<int>                 adjustNegativeStackByPos;

	struct SObjProp
	{
		asCObjectType     *objType;
		asCObjectProperty *prop;
	};
	asCArray<SObjProp> usedObjectProperties;

	asCMap<void*,bool>              existingShared;
	asCMap<asCScriptFunction*,bool> dontTranslate;

	// Helper class for adjusting offsets within initialization list buffers
	struct SListAdjuster
	{
		SListAdjuster(asCReader *rd, asDWORD *bc, asCObjectType *ot);
		void AdjustAllocMem();
		int  AdjustOffset(int offset);
		void SetRepeatCount(asUINT rc);
		void SetNextType(int typeId);

		struct SInfo
		{
			asUINT              repeatCount;
			asSListPatternNode *startNode;
		};
		asCArray<SInfo> stack;

		asCReader          *reader;
		asDWORD            *allocMemBC;
		asUINT              maxOffset;
		asCObjectType      *patternType;
		asUINT              repeatCount;
		int                 lastOffset;
		int                 nextOffset;
		asUINT              lastAdjustedOffset;
		asSListPatternNode *patternNode;
		int                 nextTypeId;
	};
	asCArray<SListAdjuster*> listAdjusters;

	// Used by FindObjectPropOffset
	asCObjectProperty* lastCompositeProp;
};

#ifndef AS_NO_COMPILER

class asCWriter
{
public:
	asCWriter(asCModule *module, asIBinaryStream *stream, asCScriptEngine *engine, bool stripDebugInfo);

	int Write();

protected:
	asCModule       *module;
	asIBinaryStream *stream;
	asCScriptEngine *engine;
	bool             stripDebugInfo;
	bool             error;
	asUINT           bytesWritten;

	int              Error(const char *msg);

	int  WriteData(const void *data, asUINT size);

	void WriteString(asCString *str);
	void WriteFunction(asCScriptFunction *func);
	void WriteFunctionSignature(asCScriptFunction *func);
	void WriteGlobalProperty(asCGlobalProperty *prop);
	void WriteObjectProperty(asCObjectProperty *prop);
	void WriteDataType(const asCDataType *dt);
	void WriteTypeInfo(asCTypeInfo *ot);
	void WriteTypeDeclaration(asCTypeInfo *ot, int phase);
	void WriteByteCode(asCScriptFunction *func);
	void WriteEncodedInt64(asINT64 i);

	// Helper functions for storing variable data
	int FindTypeInfoIdx(asCTypeInfo *ti);
	int FindTypeIdIdx(int typeId);
	int FindFunctionIndex(asCScriptFunction *func);
	int FindGlobalPropPtrIndex(void *);
	int FindStringConstantIndex(void *str);
	int FindObjectPropIndex(short offset, int typeId, asDWORD *bc);

	void CalculateAdjustmentByPos(asCScriptFunction *func);
	int  AdjustStackPosition(int pos);
	int  AdjustProgramPosition(int pos);
	int  AdjustGetOffset(int offset, asCScriptFunction *func, asDWORD programPos);

	// Intermediate data used for storing that which isn't constant, function id's, pointers, etc
	void WriteUsedTypeIds();
	void WriteUsedFunctions();
	void WriteUsedGlobalProps();
	void WriteUsedStringConstants();
	void WriteUsedObjectProps();

	// Temporary storage for persisting variable data
	asCArray<int>                usedTypeIds;
	asCArray<asCTypeInfo*>       usedTypes;
	asCArray<asCScriptFunction*> usedFunctions;
	asCArray<void*>              usedGlobalProperties;
	asCArray<void*>              usedStringConstants;
	asCMap<void*, int>           stringToIndexMap;

	asCArray<asCScriptFunction*>  savedFunctions;
	asCArray<asCDataType>         savedDataTypes;
	asCArray<asCString>           savedStrings;
	asCMap<asCString, int>        stringToIdMap;
	asCArray<int>                 adjustStackByPos;
	asCArray<int>                 adjustNegativeStackByPos;
	asCArray<int>                 bytecodeNbrByPos;

	struct SObjProp
	{
		asCObjectType     *objType;
		asCObjectProperty *prop;
	};
	asCArray<SObjProp>           usedObjectProperties;

	// Helper class for adjusting offsets within initialization list buffers
	struct SListAdjuster
	{
		SListAdjuster(asCObjectType *ot);
		int  AdjustOffset(int offset, asCObjectType *listPatternType);
		void SetRepeatCount(asUINT rc);
		void SetNextType(int typeId);

		struct SInfo
		{
			asUINT              repeatCount;
			asSListPatternNode *startNode;
		};
		asCArray<SInfo> stack;

		asCObjectType      *patternType;
		asUINT              repeatCount;
		asSListPatternNode *patternNode;
		asUINT              entries;
		int                 lastOffset;  // Last offset adjusted
		int                 nextOffset;  // next expected offset to be adjusted
		int                 nextTypeId;
	};
	asCArray<SListAdjuster*> listAdjusters;

	// Used by FindObjectPropIndex
	bool lastWasComposite;
};

#endif

END_AS_NAMESPACE

#endif // AS_RESTORE_H

repo_name: Antidote/openrpgeditor

license: mit


code:
/*
   AngelCode Scripting Library
   Copyright (c) 2003-2015 Andreas Jonsson

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

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

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

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

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

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

   Andreas Jonsson
   andreas@angelcode.com
*/

//
// as_scriptcode.cpp
//
// A container class for the script code to be compiled
//

#include "as_config.h"
#include "as_scriptcode.h"

BEGIN_AS_NAMESPACE

asCScriptCode::asCScriptCode()
{
	lineOffset = 0;
	code = 0;
	codeLength = 0;
	sharedCode = false;
}

asCScriptCode::~asCScriptCode()
{
	if( !sharedCode && code )
	{
		asDELETEARRAY(code);
	}
}

int asCScriptCode::SetCode(const char *in_name, const char *in_code, bool in_makeCopy)
{
	return SetCode(in_name, in_code, 0, in_makeCopy);
}

int asCScriptCode::SetCode(const char *in_name, const char *in_code, size_t in_length, bool in_makeCopy)
{
	if( !in_code) return asINVALID_ARG;
	this->name = in_name ? in_name : "";
	if( !sharedCode && code )
		asDELETEARRAY(code);

	if( in_length == 0 )
		in_length = strlen(in_code);
	if( in_makeCopy )
	{
		codeLength = in_length;
		sharedCode = false;
		code = asNEWARRAY(char, in_length);
		if( code == 0 )
			return asOUT_OF_MEMORY;
		memcpy(code, in_code, in_length);
	}
	else
	{
		codeLength = in_length;
		code = const_cast<char*>(in_code);
		sharedCode = true;
	}

	// Find the positions of each line
	linePositions.PushLast(0);
	for( size_t n = 0; n < in_length; n++ )
		if( in_code[n] == '\n' ) linePositions.PushLast(n+1);
	linePositions.PushLast(in_length);

	return asSUCCESS;
}

void asCScriptCode::ConvertPosToRowCol(size_t pos, int *row, int *col)
{
	if( linePositions.GetLength() == 0 )
	{
		if( row ) *row = lineOffset;
		if( col ) *col = 1;
		return;
	}

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

	for(;;)
	{
		if( linePositions[i] < pos )
		{
			// Have we found the largest number < programPosition?
			if( min == i ) break;

			min = i;
			i = (max + min)/2;
		}
		else if( linePositions[i] > pos )
		{
			// Have we found the smallest number > programPoisition?
			if( max == i ) break;

			max = i;
			i = (max + min)/2;
		}
		else
		{
			// We found the exact position
			break;
		}
	}

	if( row ) *row = i + 1 + lineOffset;
	if( col ) *col = (int)(pos - linePositions[i]) + 1;
}

bool asCScriptCode::TokenEquals(size_t pos, size_t len, const char *str)
{
	if( pos + len > codeLength ) return false;
	if( strncmp(code + pos, str, len) == 0 && strlen(str) == len )
		return true;
	return false;
}

END_AS_NAMESPACE


code:
/*
   AngelCode Scripting Library
   Copyright (c) 2003-2011 Andreas Jonsson

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

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

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

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

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

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

   Andreas Jonsson
   andreas@angelcode.com
*/

//
// as_scriptcode.h
//
// A container class for the script code to be compiled
//

#ifndef AS_SCRIPTCODE_H
#define AS_SCRIPTCODE_H

#include "as_array.h"
#include "as_string.h"

BEGIN_AS_NAMESPACE

class asCScriptCode
{
public:
	asCScriptCode();
	~asCScriptCode();

	int SetCode(const char *name, const char *code, bool makeCopy);
	int SetCode(const char *name, const char *code, size_t length, bool makeCopy);

	void ConvertPosToRowCol(size_t pos, int *row, int *col);

	bool TokenEquals(size_t pos, size_t len, const char *str);

	asCString        name;
	char            *code;
	size_t           codeLength;
	bool             sharedCode;
	int              idx;
	int              lineOffset;
	asCArray<size_t> linePositions;
};

END_AS_NAMESPACE

#endif