Advertisement
Guest User

angelscript opcast Bug?

a guest
Jan 31st, 2023
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.55 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <string.h> // strstr()
  4. #include <assert.h>
  5. #include <math.h>
  6. #include <angelscript.h>
  7. #include "../../../add_on/scriptstdstring/scriptstdstring.h"
  8. #include "../../../add_on/scripthelper/scripthelper.h"
  9. #include "../../../add_on/scriptarray/scriptarray.h"
  10. #include "../source/as_scriptengine.h"
  11.  
  12. using namespace std;
  13.  
  14. void ExecString(asIScriptEngine* engine, const string& arg);
  15.  
  16. void ConfigureEngine(asIScriptEngine* engine);
  17.  
  18. static asCScriptEngine* GetEngine()
  19. {
  20.     static asCScriptEngine* eng = (asCScriptEngine*)asCreateScriptEngine();
  21.     return eng;
  22. }
  23. int main(int argc, char** argv)
  24. {
  25.     // Create the script engine
  26.     asIScriptEngine* engine = GetEngine();
  27.     if (engine == 0)
  28.     {
  29.         cout << "Failed to create script engine." << endl;
  30.         return -1;
  31.     }
  32.  
  33.     ConfigureEngine(engine);
  34.  
  35.     // Print some useful information and start the input loop
  36.     cout << "Sample console using AngelScript " << asGetLibraryVersion() << " to perform scripted tasks." << endl;
  37.     cout << "Type 'help' for more information." << endl;
  38.  
  39.  
  40.     // Shut down the engine
  41.     engine->ShutDownAndRelease();
  42.  
  43.     return 0;
  44. }
  45.  
  46.  
  47. void print(const string& s)
  48. {
  49.     cout << s << "\n" << endl;
  50. }
  51.  
  52. void MessageCallback(const asSMessageInfo* msg, void* param)
  53. {
  54.     const char* type = "ERR ";
  55.     if (msg->type == asMSGTYPE_WARNING)
  56.         type = "WARN";
  57.     else if (msg->type == asMSGTYPE_INFORMATION)
  58.         type = "INFO";
  59.  
  60.     cout << msg->section << " (" << msg->row << ", " << msg->col << ") : " << type << " : " << msg->message << endl;
  61. }
  62.  
  63. // Function implementation with native calling convention
  64. void PrintString(const string& str)
  65. {
  66.     cout << str << "\n";
  67. }
  68.  
  69.  
  70.  
  71. class testA
  72. {
  73. };
  74.  
  75. testA* testA_Factory()
  76. {
  77.     return new testA();
  78. }
  79.  
  80. class testB : public testA
  81. {
  82. };
  83.  
  84. testB* testB_Factory()
  85. {
  86.     return new testB();
  87. }
  88.  
  89. testA* testA_opImplCast_testB(testB* Object)
  90. {
  91.     PrintString("testA opImplCast(testB@)");
  92.     return Object;
  93. }
  94.  
  95.  
  96. void testA_Opcast(testA* Object, void** OutAddress, int TypeId)
  97. {
  98.     if (!(TypeId & asTYPEID_OBJHANDLE))
  99.     {
  100.         PrintString("!(TypeId & asTYPEID_OBJHANDLE)");
  101.         return;
  102.     }
  103.     asIScriptEngine* engine = GetEngine();
  104.     asCObjectType* ScriptType = (asCObjectType*)engine->GetTypeInfoById(TypeId);
  105.  
  106.     if (ScriptType->GetFlags() & asOBJ_VALUE)
  107.     {
  108.         PrintString("(ScriptType->GetFlags() & asOBJ_VALUE)");
  109.         return;
  110.     }
  111.     if (ScriptType->GetFlags() & asOBJ_POD)
  112.     {
  113.         PrintString("(ScriptType->GetFlags() & asOBJ_POD)");
  114.         return;
  115.     }
  116.     PrintString("testA_Opcast");
  117.     *(testA**)OutAddress = Object;
  118. }
  119.  
  120. void testCasting()
  121. {
  122.     auto* engine = GetEngine();
  123.  
  124.     std::string testfuncstr =
  125.         "print(\"testing testCasting\");          \n"
  126.         "testB tb = testB();                       \n"
  127.         "testB @ta = tb;                           \n"
  128.         "if(ta is null) print(\"ta is null\");       \n"
  129.         "testA @tc = ta;                       \n" //testA opImplCast(testB@):
  130.         "if(tc is null) print(\"tc is null\");       \n"
  131.         "testB @tab = cast<testB>(tc);              \n"  //testA_Opcast
  132.         "if(tab is null) print(\"tab is null\");       \n" // tab is not null... ??
  133.         ;
  134.     ExecString(engine, testfuncstr);
  135. }
  136.  
  137. void ConfigureEngine(asIScriptEngine* engine)
  138. {
  139.     int r;
  140.  
  141.     engine->SetEngineProperty(asEP_ALLOW_UNSAFE_REFERENCES, 1);
  142.     engine->SetEngineProperty(asEP_USE_CHARACTER_LITERALS, 1);
  143.     engine->SetEngineProperty(asEP_ALLOW_MULTILINE_STRINGS, 1);
  144.     engine->SetEngineProperty(asEP_SCRIPT_SCANNER, 1);
  145.     engine->SetEngineProperty(asEP_OPTIMIZE_BYTECODE, 1);
  146.     engine->SetEngineProperty(asEP_AUTO_GARBAGE_COLLECT, 0);
  147.     engine->SetEngineProperty(asEP_ALTER_SYNTAX_NAMED_ARGS, 1);
  148.     engine->SetEngineProperty(asEP_DISALLOW_VALUE_ASSIGN_FOR_REF_TYPE, 1);
  149.     engine->SetEngineProperty(asEP_REQUIRE_ENUM_SCOPE, 1);
  150.     engine->SetEngineProperty(asEP_ALWAYS_IMPL_DEFAULT_CONSTRUCT, 1);
  151.     engine->SetEngineProperty(asEP_ALLOW_IMPLICIT_HANDLE_TYPES, 1);
  152.     // Tell the engine to output any error messages to printf
  153.     engine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL);
  154.  
  155.     // Register the script string type
  156.     // Look at the implementation for this function for more information
  157.     // on how to register a custom string type, and other object types.
  158.     RegisterStdString(engine);
  159.  
  160.     // Register the script array type
  161.     RegisterScriptArray(engine, false);
  162.  
  163.     r = engine->RegisterGlobalFunction("void print(const string &in)", asFUNCTION(print), asCALL_CDECL); assert(r >= 0);
  164.  
  165.     r = engine->RegisterObjectType("testA", 0, asOBJ_REF | asOBJ_NOCOUNT | asOBJ_IMPLICIT_HANDLE); assert(r >= 0);
  166.     r = engine->RegisterObjectBehaviour("testA", asBEHAVE_FACTORY, "testA @f()", asFUNCTION(testA_Factory), asCALL_CDECL); assert(r >= 0);
  167.     r = engine->RegisterObjectMethod("testA", "void opCast(?&out)", asFUNCTION(testA_Opcast), asCALL_CDECL_OBJFIRST); assert(r >= 0);
  168.     r = engine->RegisterObjectType("testB", 0, asOBJ_REF | asOBJ_NOCOUNT | asOBJ_IMPLICIT_HANDLE); assert(r >= 0);
  169.     r = engine->RegisterObjectBehaviour("testB", asBEHAVE_FACTORY, "testB @f()", asFUNCTION(testB_Factory), asCALL_CDECL); assert(r >= 0);
  170.     r = engine->RegisterObjectMethod("testB", "testA@ opImplCast() const", asFUNCTION(testA_opImplCast_testB), asCALL_CDECL_OBJFIRST); assert(r >= 0);
  171.  
  172.     testCasting();
  173.  
  174.  
  175.     // Do not output anything else to printf
  176.     engine->ClearMessageCallback();
  177. }
  178.  
  179. void ExecString(asIScriptEngine* engine, const string& arg)
  180. {
  181.     int r = ExecuteString(engine, arg.c_str(), engine->GetModule("console", asGM_CREATE_IF_NOT_EXISTS));
  182.     if (r < 0)
  183.         cout << "Invalid script statement. " << endl;
  184.     else if (r == asEXECUTION_EXCEPTION)
  185.         cout << "A script exception was raised." << endl;
  186. }
  187.  
  188.  
Tags: angelscript
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement