| 84 | |
| 85 | |
| 86 | int RunApplication() |
| 87 | { |
| 88 | int r; |
| 89 | |
| 90 | // Create the script engine |
| 91 | asIScriptEngine *engine = asCreateScriptEngine(); |
| 92 | if( engine == 0 ) |
| 93 | { |
| 94 | cout << "Failed to create script engine." << endl; |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | // The script compiler will write any compiler messages to the callback. |
| 99 | engine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL); |
| 100 | |
| 101 | // Configure the script engine with all the functions, |
| 102 | // and variables that the script should be able to use. |
| 103 | ConfigureEngine(engine); |
| 104 | |
| 105 | // Compile the script code |
| 106 | r = CompileScript(engine); |
| 107 | if( r < 0 ) |
| 108 | { |
| 109 | engine->Release(); |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | // Create a context that will execute the script. |
| 114 | asIScriptContext *ctx = engine->CreateContext(); |
| 115 | if( ctx == 0 ) |
| 116 | { |
| 117 | cout << "Failed to create the context." << endl; |
| 118 | engine->Release(); |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | // We don't want to allow the script to hang the application, e.g. with an |
| 123 | // infinite loop, so we'll use the line callback function to set a timeout |
| 124 | // that will abort the script after a certain time. Before executing the |
| 125 | // script the timeOut variable will be set to the time when the script must |
| 126 | // stop executing. |
| 127 | DWORD timeOut; |
| 128 | r = ctx->SetLineCallback(asFUNCTION(LineCallback), &timeOut, asCALL_CDECL); |
| 129 | if( r < 0 ) |
| 130 | { |
| 131 | cout << "Failed to set the line callback function." << endl; |
| 132 | ctx->Release(); |
| 133 | engine->Release(); |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | // Find the function for the function we want to execute. |
| 138 | asIScriptFunction *func = engine->GetModule(0)->GetFunctionByDecl("float calc(float, float)"); |
| 139 | if( func == 0 ) |
| 140 | { |
| 141 | cout << "The function 'float calc(float, float)' was not found." << endl; |
| 142 | ctx->Release(); |
| 143 | engine->Release(); |
no test coverage detected