Thanks, I missed manual somehow. But now I got segmentation fault if I register created array as
array<string>@ myArr
but this works:
array<string> myArr
The full code snippet:
#include <angelscript.h>
#include "scriptstdstring/scriptstdstring.h"
#include "scriptbuilder/scriptbuilder.h"
#include "scriptarray/scriptarray.h"
#include <cstdio>
#include <string>
#include <cassert>
CScriptArray *CreateArrayOfStrings(asIScriptEngine* engine)
{
asIObjectType* t = engine->GetObjectTypeByDecl("array<string>");
assert(t);
CScriptArray* arr = CScriptArray::Create(t, 3);
assert(arr);
for( asUINT i = 0; i < arr->GetSize(); i++ )
{
std::string val("test");
arr->SetValue(i, &val);
}
return arr;
}
void writeln(const std::string &msg)
{
fprintf(stderr, "%s\n", msg.c_str());
}
void MessageCallback(const asSMessageInfo *msg, void *param)
{
const char *type = "ERR ";
if( msg->type == asMSGTYPE_WARNING )
type = "WARN";
else if( msg->type == asMSGTYPE_INFORMATION )
type = "INFO";
printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message);
}
int main()
{
asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
int r = engine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL); assert( r >= 0 );
RegisterStdString(engine);
RegisterScriptArray(engine, true);
r = engine->RegisterGlobalFunction("void writeln(const string &in)", asFUNCTION(writeln), asCALL_CDECL); assert( r >= 0 );
CScriptArray* myArr = CreateArrayOfStrings(engine);
r = engine->RegisterGlobalProperty("array<string>@ myArr", myArr); assert(r >= 0);
CScriptBuilder builder;
r = builder.StartNewModule(engine, "MyModule");
if( r < 0 )
{
printf("Unrecoverable error while starting a new module.\n");
return 1;
}
r = builder.AddSectionFromFile("test.as");
if( r < 0 )
{
printf("Please correct the errors in the script and try again.\n");
return 1;
}
r = builder.BuildModule();
if( r < 0 )
{
printf("Please correct the errors in the script and try again.\n");
return 1;
}
asIScriptModule *mod = engine->GetModule("MyModule");
asIScriptFunction *func = mod->GetFunctionByDecl("void main()");
if( func == 0 )
{
printf("The script must have the function 'void main()'. Please add it and try again.\n");
return 1;
}
asIScriptContext *ctx = engine->CreateContext();
ctx->Prepare(func);
r = ctx->Execute();
if( r != asEXECUTION_FINISHED )
{
// The execution didn't complete as expected. Determine what happened.
if( r == asEXECUTION_EXCEPTION )
{
// An exception occurred, let the script writer know what happened so it can be corrected.
printf("An exception '%s' occurred. Please correct the code and try again.\n", ctx->GetExceptionString());
}
}
ctx->Release();
myArr->Release();
engine->Release();
return 0;
}
Script code:
void main()
{
for( uint n = 0; n < myArr.length(); n++ )
writeln(myArr[n]);
}
I got seg fault in
asUINT CScriptArray::GetSize() const
{
return buffer->numElements;
}
Is it intended behavior?
.