The same C++ code and script give me different result between Windows+MSVC and Linux+GCC.
#include <angelscript.h>
#include <iostream>
class my_int
{
public:
my_int() = default;
my_int(const my_int&) = default;
my_int& operator=(const my_int&) = default;
int value = 0;
my_int& operator++()
{
++value;
return *this;
}
my_int operator++(int)
{
my_int tmp(*this);
++*this;
return tmp;
}
};
void output(int val)
{
std::cout << val << std::endl;
}
int main(int argc, char** argv)
{
asIScriptEngine* engine = asCreateScriptEngine();
engine->RegisterObjectType(
"my_int", sizeof(my_int), asOBJ_VALUE | asOBJ_APP_CLASS_CDK
);
engine->RegisterObjectBehaviour(
"my_int",
asBEHAVE_CONSTRUCT,
"void f()",
asFunctionPtr(+[](void* mem)
{
new(mem) my_int();
}),
asCALL_CDECL_OBJLAST
);
engine->RegisterObjectBehaviour(
"my_int",
asBEHAVE_CONSTRUCT,
"void f(const my_int&in)",
asFunctionPtr(+[](void* mem, const my_int& other)
{
new(mem) my_int(other);
}),
asCALL_CDECL_OBJFIRST
);
engine->RegisterObjectBehaviour(
"my_int",
asBEHAVE_DESTRUCT,
"void f()",
asFunctionPtr(+[](void* mem)
{
// empty
}),
asCALL_CDECL_OBJLAST
);
engine->RegisterObjectMethod(
"my_int",
"my_int& opPreInc()",
asMETHODPR(my_int, operator++, (), my_int&),
asCALL_THISCALL
);
engine->RegisterObjectMethod(
"my_int",
"my_int opPostInc()",
asMETHODPR(my_int, operator++, (int), my_int),
asCALL_THISCALL
);
engine->RegisterObjectProperty(
"my_int",
"int value",
asOFFSET(my_int, value)
);
engine->RegisterGlobalFunction(
"void output(int val)",
asFUNCTION(output),
asCALL_CDECL
);
asIScriptModule* m = engine->GetModule("test", asGM_ALWAYS_CREATE);
m->AddScriptSection(
"test.as",
"void f()"
"{"
"my_int i;"
"output(i.value);"
"++i;"
"output(i.value);"
"my_int tmp = i++;"
"output(tmp.value);"
"output(i.value);"
"}"
);
m->Build();
std::cout << asGetLibraryVersion() << std::endl;
asIScriptFunction* f = m->GetFunctionByName("f");
asIScriptContext* ctx = engine->CreateContext();
ctx->Prepare(f);
ctx->Execute();
ctx->Release();
engine->ShutDownAndRelease();
return 0;
}
The output of Windows+MSVC, which is the expected output
0
1
1
2
But the output of Linux+GCC is obviously wrong. The third line of output keeps changing among every execution. Here is an example output
0
1
-266470689
1
The architecture of testing environment is both x64. The version of AngelScript is 2.37.0.
But if I registered the opPostInc
using asCALL_GENERIC
, output of both platforms give expected result.
// The wrapper
void opPostInc_gen(asIScriptGeneric* gen)
{
my_int* this_ = (my_int*)gen->GetObject();
my_int result = (*this_)++;
gen->SetReturnObject(&result);
}
// in main()
engine->RegisterObjectMethod(
"my_int",
"my_int opPostInc()",
asFUNCTION(opPostInc_gen),
asCALL_GENERIC
);