bool CLuaArguments::CallGlobal ( CLuaMain* pLuaMain, const char* szFunction, CLuaArguments * returnValues ) const { assert ( pLuaMain ); assert ( szFunction ); TIMEUS startTime = GetTimeUs (); // Add the function name to the stack and get the event from the table lua_State* luaVM = pLuaMain->GetVirtualMachine (); assert ( luaVM ); LUA_CHECKSTACK ( luaVM, 1 ); int luaStackPointer = lua_gettop ( luaVM ); lua_pushstring ( luaVM, szFunction ); lua_gettable ( luaVM, LUA_GLOBALSINDEX ); // Push our arguments onto the stack PushArguments ( luaVM ); // Call the function with our arguments pLuaMain->ResetInstructionCount (); int iret = 0; try { iret = lua_pcall ( luaVM, m_Arguments.size (), LUA_MULTRET, 0 ); } catch ( ... ) { return false; } if ( iret == LUA_ERRRUN || iret == LUA_ERRMEM ) { std::string strRes = ConformResourcePath ( lua_tostring( luaVM, -1 ) ); g_pGame->GetScriptDebugging()->LogError ( luaVM, "%s", strRes.c_str () ); // cleanup the stack while ( lua_gettop ( luaVM ) - luaStackPointer > 0 ) lua_pop ( luaVM, 1 ); return false; // the function call failed } else { int iReturns = lua_gettop ( luaVM ) - luaStackPointer; if ( returnValues != NULL ) { for ( int i = - iReturns; i <= -1; i++ ) { returnValues->ReadArgument ( luaVM, i ); } } // cleanup the stack while ( lua_gettop ( luaVM ) - luaStackPointer > 0 ) lua_pop ( luaVM, 1 ); } CPerfStatLuaTiming::GetSingleton ()->UpdateLuaTiming ( pLuaMain, szFunction, GetTimeUs() - startTime ); return true; }
bool CLuaArguments::CallGlobal ( CLuaMain* pLuaMain, const char* szFunction, CLuaArguments * returnValues ) const { assert ( pLuaMain ); assert ( szFunction ); // Add the function name to the stack and get the event from the table lua_State* luaVM = pLuaMain->GetVirtualMachine (); assert ( luaVM ); int luaStackPointer = lua_gettop ( luaVM ); lua_pushstring ( luaVM, szFunction ); lua_gettable ( luaVM, LUA_GLOBALSINDEX ); // Push our arguments onto the stack PushArguments ( luaVM ); // Call the function with our arguments pLuaMain->ResetInstructionCount (); int iret = 0; try { iret = lua_pcall ( luaVM, m_Arguments.size (), LUA_MULTRET, 0 ); } catch ( ... ) { return false; } if ( iret == LUA_ERRRUN || iret == LUA_ERRMEM ) { const char* szRes = lua_tostring( luaVM, -1 ); g_pGame->GetScriptDebugging()->LogError ( luaVM, "%s", szRes ); return false; // the function call failed } else if ( returnValues != NULL ) { int iReturns = lua_gettop ( luaVM ) - luaStackPointer; for ( int i = - iReturns; i <= -1; i++ ) { returnValues->ReadArgument ( luaVM, i ); } for ( int h = 0; h < iReturns; h++) { lua_pop ( luaVM, 1 ); } } return true; }
bool CLuaArguments::Call ( CLuaMain* pLuaMain, int iLuaFunction, CLuaArguments * returnValues ) const { assert ( pLuaMain ); // Add the function name to the stack and get the event from the table lua_State* luaVM = pLuaMain->GetVirtualMachine (); assert ( luaVM ); LUA_CHECKSTACK ( luaVM, 1 ); int luaStackPointer = lua_gettop ( luaVM ); lua_getref ( luaVM, iLuaFunction ); // Push our arguments onto the stack PushArguments ( luaVM ); // Call the function with our arguments pLuaMain->ResetInstructionCount (); int iret = lua_pcall ( luaVM, m_Arguments.size (), LUA_MULTRET, 0 ); if ( iret == LUA_ERRRUN || iret == LUA_ERRMEM ) { const char* szRes = lua_tostring( luaVM, -1 ); g_pGame->GetScriptDebugging()->LogError ( luaVM, "%s", szRes ); // cleanup the stack while ( lua_gettop ( luaVM ) - luaStackPointer > 0 ) lua_pop ( luaVM, 1 ); return false; // the function call failed } else { int iReturns = lua_gettop ( luaVM ) - luaStackPointer; if ( returnValues != NULL ) { for ( int i = - iReturns; i <= -1; i++ ) { returnValues->ReadArgument ( luaVM, i ); } } // cleanup the stack while ( lua_gettop ( luaVM ) - luaStackPointer > 0 ) lua_pop ( luaVM, 1 ); } return true; }
bool CLuaArguments::Call ( lua_State* luaVM, const char* szFunction ) const { assert ( szFunction ); // Add the function name to the stack and get the event from the table assert ( luaVM ); lua_pushstring ( luaVM, szFunction ); lua_gettable ( luaVM, LUA_GLOBALSINDEX ); // Push our arguments onto the stack PushArguments ( luaVM ); int iret = lua_pcall ( luaVM, m_Arguments.size (), 0, 0 ) ; if ( iret == LUA_ERRRUN || iret == LUA_ERRMEM ) { const char* szRes = lua_tostring( luaVM, -1 ); return false; // the function call failed } return true; }
static void TestArgFormatter(JSContext* jscontext, JSObject* glob, nsIXPConnect* xpc) { JSBool ok = JS_TRUE; const char* a_in = "some string"; nsCOMPtr<nsITestXPCFoo> b_in = new nsTestXPCFoo(); nsCOMPtr<nsIWritableVariant> c_in = do_CreateInstance("@mozilla.org/variant;1"); static NS_NAMED_LITERAL_STRING(d_in, "foo bar"); const char* e_in = "another meaningless chunck of text"; JSBool a_match; nsCOMPtr<nsISupports> b_out; nsCOMPtr<nsIVariant> c_out; nsAutoString d_out; JSBool e_match; nsCOMPtr<nsITestXPCFoo> specified; PRInt32 val; printf("ArgumentFormatter test: "); if(!b_in || !c_in || NS_FAILED(c_in->SetAsInt32(5))) { printf(" failed to construct test objects -- FAILED!\n"); return; } do { JSAutoRequest ar(jscontext); // Prepare an array of arguments for JS_ConvertArguments jsval argv[5]; js::AutoArrayRooter tvr(jscontext, JS_ARRAY_LENGTH(argv), argv); if (!PushArguments(jscontext, 5, argv, "s %ip %iv %is s", a_in, &NS_GET_IID(nsITestXPCFoo2), b_in.get(), c_in.get(), static_cast<const nsAString*>(&d_in), e_in)) { printf(" could not convert from native to JS -- FAILED!\n"); return; } JSString *a_out, *e_out; ok = JS_ConvertArguments(jscontext, 5, argv, "S %ip %iv %is S", &a_out, static_cast<nsISupports**>(getter_AddRefs(b_out)), static_cast<nsIVariant**>(getter_AddRefs(c_out)), static_cast<nsAString*>(&d_out), &e_out); TAF_CHECK(ok, " could not convert from JS to native -- FAILED!\n"); TAF_CHECK(b_out, " JS to native for %%ip returned NULL -- FAILED!\n"); specified = do_QueryInterface(b_out); TAF_CHECK(specified, " could not QI value JS to native returned -- FAILED!\n"); ok = specified.get() == b_in.get(); TAF_CHECK(ok, " JS to native returned wrong value -- FAILED!\n"); TAF_CHECK(c_out, " JS to native for %%iv returned NULL -- FAILED!\n"); TAF_CHECK(NS_SUCCEEDED(c_out->GetAsInt32(&val)) && val == 5, " JS to native for %%iv holds wrong value -- FAILED!\n"); TAF_CHECK(d_in.Equals(d_out), " JS to native for %%is returned the wrong value -- FAILED!\n"); TAF_CHECK(JS_StringEqualsAscii(jscontext, a_out, a_in, &a_match), " oom -- FAILED!\n"); TAF_CHECK(JS_StringEqualsAscii(jscontext, e_out, e_in, &e_match), " oom -- FAILED!\n"); } while (0); if (!ok) return; if(a_match && e_match) printf("passed\n"); else printf(" conversion OK, but surrounding was mangled -- FAILED!\n"); }
bool CLuaArguments::Call ( CLuaMain* pLuaMain, const CLuaFunctionRef& iLuaFunction, CLuaArguments * returnValues ) const { assert ( pLuaMain ); TIMEUS startTime = GetTimeUs (); // Add the function name to the stack and get the event from the table lua_State* luaVM = pLuaMain->GetVirtualMachine (); assert ( luaVM ); LUA_CHECKSTACK ( luaVM, 1 ); int luaStackPointer = lua_gettop ( luaVM ); lua_getref ( luaVM, iLuaFunction.ToInt () ); // Push our arguments onto the stack PushArguments ( luaVM ); // Call the function with our arguments pLuaMain->ResetInstructionCount (); int iret = lua_pcall ( luaVM, m_Arguments.size (), LUA_MULTRET, 0 ); if ( iret == LUA_ERRRUN || iret == LUA_ERRMEM ) { SString strRes = ConformResourcePath ( lua_tostring( luaVM, -1 ) ); vector <SString> vecSplit; strRes.Split ( ":", vecSplit ); if ( vecSplit.size ( ) >= 3 ) { SString strFile = vecSplit[0]; int iLine = atoi ( vecSplit[1].c_str ( ) ); SString strMsg = vecSplit[2].substr ( 1 ); g_pGame->GetScriptDebugging()->LogError ( strFile, iLine, strMsg ); } else g_pGame->GetScriptDebugging()->LogError ( luaVM, "%s", strRes.c_str () ); // cleanup the stack while ( lua_gettop ( luaVM ) - luaStackPointer > 0 ) lua_pop ( luaVM, 1 ); return false; // the function call failed } else { int iReturns = lua_gettop ( luaVM ) - luaStackPointer; if ( returnValues != NULL ) { for ( int i = - iReturns; i <= -1; i++ ) { returnValues->ReadArgument ( luaVM, i ); } } // cleanup the stack while ( lua_gettop ( luaVM ) - luaStackPointer > 0 ) lua_pop ( luaVM, 1 ); } CPerfStatLuaTiming::GetSingleton ()->UpdateLuaTiming ( pLuaMain, pLuaMain->GetFunctionTag ( iLuaFunction.m_iFunction ), GetTimeUs() - startTime ); return true; }