I'm trying to get the [] operator registered in my application, and AngelScript keeps on crashing on Build(). If I use ints/floats at the index parameter, everything is fine. The problem occurs when I try to use strings. Below is a simple test case for this (note that using a by-value string as the index is on purpose, although I have tried this using references/handles with the same effect). // asTest1.cpp
#include <windows.h>
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
#include "angelscript.h"
#pragma comment(lib, "angelscript.lib")
struct : public asIOutputStream
{
void Write(const char* text)
{
cout << text;
}
}
asOutput;
struct CTestObj
{
CTestObj& operator[](string s);
};
string asStringFactory(asUINT length, const char *text);
void asStringConstructor(string* pthis);
void asStringDestructor(string* pthis);
string& asStringAssignString(string &cp, string *pthis);
bool GetScript(char* filename, char* &code, unsigned int& size);
void main(void)
{
int rc;
char* code;
char* szTestFile;
unsigned int size;
asIScriptEngine* engine;
szTestFile = "asTest1.as";
if (!GetScript(szTestFile, code, size))
return;
engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
engine->SetCommonMessageStream(&asOutput);
rc = engine->RegisterObjectType("string", sizeof(string), asOBJ_CLASS_CDA); assert(rc >= 0);
rc = engine->RegisterStringFactory("string", asFUNCTION(asStringFactory), asCALL_CDECL); assert(rc >= 0);
rc = engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(asStringConstructor), asCALL_CDECL_OBJLAST); assert(rc >= 0);
rc = engine->RegisterObjectBehaviour("string", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(asStringDestructor), asCALL_CDECL_OBJLAST); assert(rc >= 0);
rc = engine->RegisterObjectBehaviour("string", asBEHAVE_ASSIGNMENT, "string& f(string &in)", asFUNCTION(asStringAssignString), asCALL_CDECL_OBJLAST); assert(rc >= 0);
rc = engine->RegisterObjectType("CTestObj", sizeof(CTestObj), asOBJ_CLASS); assert(rc >= 0);
rc = engine->RegisterObjectBehaviour("CTestObj", asBEHAVE_INDEX, "CTestObj& f(string)", asMETHOD(CTestObj, operator[]), asCALL_THISCALL); assert(rc >= 0);
engine->AddScriptSection(0, szTestFile, code, size);
engine->Build(0);
engine->ExecuteString(0, "main()");
engine->Release();
}
string asStringFactory(asUINT length, const char *text)
{
cout << __FUNCTION__ << " length=" << length << " text='" << text << "'\n";
return string(text);
}
void asStringConstructor(string* pthis)
{
cout << __FUNCTION__ << " pthis=" << (int)pthis << endl;
new(pthis)string();
}
void asStringDestructor(string* pthis)
{
cout << __FUNCTION__ << " pthis=" << (int)pthis << endl;
pthis->~string();
}
string& asStringAssignString(string &cp, string *pthis)
{
cout << __FUNCTION__ << " cp=" << cp << " pthis=" << (int)pthis << endl;
return *pthis = cp;
}
bool GetScript(char* filename, char* &code, unsigned int& size)
{
DWORD dummy;
HANDLE hFile = CreateFile(
filename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
size = GetFileSize(hFile, 0);
code = new char[size + 1];
ReadFile(hFile, code, size, &dummy, NULL);
CloseHandle(hFile);
cout << "Script '" << filename << "' is " << size << " bytes\n";
}
else
cout << "Script '" << filename << "' does not exist\n";
return hFile != INVALID_HANDLE_VALUE;
}
CTestObj& CTestObj::operator[](string s)
{
cout << __FUNCTION__ << " s='" << s << "'\n";
return *this;
}
// asTest1.as
void main()
{
CTestObj obj;
obj["test"];
}
Is anyone else successfully using strings as index parameters? -D