Execute the script by calling the main() function
| 376 | |
| 377 | // Execute the script by calling the main() function |
| 378 | int ExecuteScript(asIScriptEngine *engine, const char *scriptFile) |
| 379 | { |
| 380 | asIScriptModule *mod = engine->GetModule("script", asGM_ONLY_IF_EXISTS); |
| 381 | if( !mod ) return -1; |
| 382 | |
| 383 | // Find the main function |
| 384 | asIScriptFunction *func = mod->GetFunctionByDecl("int main()"); |
| 385 | if( func == 0 ) |
| 386 | { |
| 387 | // Try again with "void main()" |
| 388 | func = mod->GetFunctionByDecl("void main()"); |
| 389 | } |
| 390 | |
| 391 | if( func == 0 ) |
| 392 | { |
| 393 | engine->WriteMessage(scriptFile, 0, 0, asMSGTYPE_ERROR, "Cannot find 'int main()' or 'void main()'"); |
| 394 | return -1; |
| 395 | } |
| 396 | |
| 397 | if(g_doDebug) |
| 398 | InitializeDebugger(engine); |
| 399 | |
| 400 | // Once we have the main function, we first need to initialize the global variables |
| 401 | // Since we've set up the request context callback we will be able to debug the |
| 402 | // initialization without passing in a pre-created context |
| 403 | int r = mod->ResetGlobalVars(0); |
| 404 | if( r < 0 ) |
| 405 | { |
| 406 | engine->WriteMessage(scriptFile, 0, 0, asMSGTYPE_ERROR, "Failed while initializing global variables"); |
| 407 | return -1; |
| 408 | } |
| 409 | |
| 410 | // Set up a context to execute the script |
| 411 | // The context manager will request the context from the |
| 412 | // pool, which will automatically attach the debugger |
| 413 | asIScriptContext *ctx = g_ctxMgr->AddContext(engine, func, true); |
| 414 | |
| 415 | // Execute the script until completion |
| 416 | // The script may create co-routines. These will automatically |
| 417 | // be managed by the context manager |
| 418 | while( g_ctxMgr->ExecuteScripts() ); |
| 419 | |
| 420 | // Check if the main script finished normally |
| 421 | r = ctx->GetState(); |
| 422 | if( r != asEXECUTION_FINISHED ) |
| 423 | { |
| 424 | if( r == asEXECUTION_EXCEPTION ) |
| 425 | { |
| 426 | cout << "The script failed with an exception" << endl; |
| 427 | cout << GetExceptionInfo(ctx, true).c_str(); |
| 428 | r = -1; |
| 429 | } |
| 430 | else if( r == asEXECUTION_ABORTED ) |
| 431 | { |
| 432 | cout << "The script was aborted" << endl; |
| 433 | r = -1; |
| 434 | } |
| 435 | else |
no test coverage detected