| 58 | |
| 59 | |
| 60 | static bool ExecuteScript() |
| 61 | { |
| 62 | // Create a context in which the script will be executed. |
| 63 | |
| 64 | // Several contexts may exist in parallel, holding the execution |
| 65 | // of various scripts in the same engine. At the moment contexts are not |
| 66 | // thread safe though so you should make sure that only one executes |
| 67 | // at a time. An execution can be suspended to allow another |
| 68 | // context to execute. |
| 69 | |
| 70 | asIScriptContext *ctx = engine->CreateContext(); |
| 71 | if( ctx == 0 ) |
| 72 | { |
| 73 | PRINTF("%s: Failed to create a context\n", TESTNAME); |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | // Prepare the context for execution |
| 78 | |
| 79 | // When a context has finished executing the context can be reused by calling |
| 80 | // PrepareContext on it again. If the same stack size is used as the last time |
| 81 | // there will not be any new allocation thus saving some time. |
| 82 | |
| 83 | int r = ctx->Prepare(engine->GetModule(0)->GetFunctionByName("main")); |
| 84 | if( r < 0 ) |
| 85 | { |
| 86 | PRINTF("%s: Failed to prepare context\n", TESTNAME); |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | // If the script function takes any parameters we need to |
| 91 | // copy them to the context's stack by using SetArguments() |
| 92 | |
| 93 | // Execute script |
| 94 | |
| 95 | r = ctx->Execute(); |
| 96 | if( r < 0 ) |
| 97 | { |
| 98 | PRINTF("%s: Unexpected error during script execution\n", TESTNAME); |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | if( r == asEXECUTION_FINISHED ) |
| 103 | { |
| 104 | // If the script function is returning any |
| 105 | // data we can get it with GetReturnValue() |
| 106 | float retVal = ctx->GetReturnFloat(); |
| 107 | |
| 108 | if (retVal == 7.826446f) |
| 109 | r = 0; |
| 110 | else |
| 111 | PRINTF("%s: Script didn't return the correct value. Returned: %f, expected: %f\n", TESTNAME, retVal, 7.826446f); |
| 112 | } |
| 113 | else if( r == asEXECUTION_SUSPENDED ) |
| 114 | { |
| 115 | PRINTF("%s: Execution was suspended.\n", TESTNAME); |
| 116 | |
| 117 | // In this case we can call Execute again to continue |
no test coverage detected