| 15 | } |
| 16 | |
| 17 | bool TestBStr() |
| 18 | { |
| 19 | RET_ON_MAX_PORT |
| 20 | |
| 21 | bool fail = false; |
| 22 | COutStream out; |
| 23 | |
| 24 | asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION); |
| 25 | engine->SetMessageCallback(asMETHOD(COutStream, Callback), &out, asCALL_THISCALL); |
| 26 | RegisterBStr(engine); |
| 27 | |
| 28 | engine->RegisterGlobalFunction("bstr NewString(int)", asFUNCTION(NewString), asCALL_CDECL); |
| 29 | engine->RegisterGlobalFunction("void assert(bool)", asFUNCTION(Assert), asCALL_GENERIC); |
| 30 | |
| 31 | int r = ExecuteString(engine, "bstr s = NewString(10)"); |
| 32 | if( r < 0 ) |
| 33 | { |
| 34 | PRINTF("%s: ExecuteString() failed\n", TESTNAME); |
| 35 | TEST_FAILED; |
| 36 | } |
| 37 | else if( r != asEXECUTION_FINISHED ) |
| 38 | { |
| 39 | PRINTF("%s: ExecuteString() returned %d\n", TESTNAME, r); |
| 40 | TEST_FAILED; |
| 41 | } |
| 42 | |
| 43 | // Test passing bstr strings to a script function |
| 44 | asIScriptModule *mod = engine->GetModule(0, asGM_ALWAYS_CREATE); |
| 45 | const char *script = |
| 46 | "void test(bstr a, bstr b) \n" |
| 47 | "{ \n" |
| 48 | " assert(a == \"a\"); \n" |
| 49 | " assert(b == \"b\"); \n" |
| 50 | "} \n"; |
| 51 | mod->AddScriptSection("script", script); |
| 52 | r = mod->Build(); |
| 53 | if( r < 0 ) |
| 54 | { |
| 55 | TEST_FAILED; |
| 56 | } |
| 57 | |
| 58 | asIScriptFunction *func = mod->GetFunctionByIndex(0); |
| 59 | asIScriptContext *ctx = engine->CreateContext(); |
| 60 | ctx->Prepare(func); |
| 61 | |
| 62 | // Create the object and initialize it, then give |
| 63 | // the pointer directly to the script engine. |
| 64 | // The script engine will free the object. |
| 65 | asBSTR *a = (asBSTR*)engine->CreateScriptObject(engine->GetTypeInfoByName("bstr")); |
| 66 | *a = asBStrAlloc(1); |
| 67 | strcpy((char*)*a, "a"); |
| 68 | *(asBSTR**)ctx->GetAddressOfArg(0) = a; |
| 69 | |
| 70 | // Create a local instance and have the script engine copy it. |
| 71 | // The application must free its copy of the object. |
| 72 | asBSTR b = asBStrAlloc(1); |
| 73 | strcpy((char*)b, "b"); |
| 74 | ctx->SetArgObject(1, &b); |
no test coverage detected