lua return string c++ - c++

There is a function on Lua:
STRING getClassesList()
It returns string as it is to get on c++:
This does not work:
Const char * ClassesStr;
Lua_getglobal (L, "getClassesList");
Lua_pcall (L, 1, 1, 0);
ClassesStr = lua_tostring (L, 1);
stack: 'readQuikAgent' 'attempt to call a table value'
The function is designed to obtain a list of class codes sent from the server during the communication session. The class codes in the list are separated by a comma ",". At the end of the received line, the symbol "," is always appended.
Call format:
STRING getClassesList ()
Example:
List = getClassesList ()
As a result of the above line of code, the list variable contains a string of the form:
OPTEXP, USDRUB, PSOPT, PSFUT, SPBFUT

'attempt to call table value' means that the item at the top of the lua stack is a table not a function.
So the result of the getglobal was a table, and that can not be called.

It works (thank you siffiejoe):
lua_pcall(L, 0, 1, 0);
ClassesStr = lua_tostring(L, -1);

Related

Get table from c++ lua reference index in lua

first sorry for the weired title :(
I am using lua as a scripting languge for my game and I have a problem:
I am creating lua table instances and storing them in indices "ref_idx " like this:
lua_newtable(L);
lua_getglobal(L, "TestTable"); // create instance of table "TestTable"
lua_pcall(L, 0, 1, 0);
ref_idx = luaL_ref(L, LUA_REGISTRYINDEX);
and when i want to call a method of one of them I use :
lua_rawgeti(L, LUA_REGISTRYINDEX, ref_idx);
lua_getfield(L, -1, "testMethod");
lua_pushvalue(L, -2);
lua_pcall(L, 1, 0, 0));
lua_pop(L, 0);
when I am done with the reference :
luaL_unref(L, LUA_REGISTRYINDEX, ref_idx);
Every thing works fine, the problem is I want to use these reference in lua directly something like :
function onUpdate()
local ref = GetReferenceFromC++() -- returns ref_idx
ref:testMethod()
end
Any help or a way to do it?
(sorry for my english, I am working on it)
I used debug.getregistry()[ref]:testMethod()
and it works, Thank you #EgorSkriptunoff for your quick response.

LuaBridge Indescriptive errors

I use LuaJit and LuaBridge to compile lua code and execute it.
When the lua code throws an error, it's really hard to debug - I don't know what line it happened or what caused it.
This is an example:
[string "1"]:0: bad argument #1 to 'info' (string expected, got table)
in the code:
"function foo(bar)\n"
" logger:info(bar.moo);"
How do I translate [string "1"] to the matching code line?
Can I get a stacktrace?
The lua code is "compiled" with doing string.dump on the text code, and then loading it with luaL_loadbuffer.
Psuedo code that loads and calls the lua function:
int result = luaL_loadstring(L, script.c_str());
auto script_function = luabridge::LuaRef::fromStack(L);
auto string_module = luabridge::getGlobal(L, "string");
auto string_dump = string_module["dump"];
auto code = luabridge::LuaRef_cast<std::string>(string_dump(script_function, strip));
luaL_loadbuffer(L, code.data(), code.size(), std::to_string(current_id++).c_str());
lua_pcall(L, 0, LUA_MULTRET, -1);
auto func_to_call = luabridge::getGlobal(L, "foo");
func_to_call(&bar);
[string "1"] comes from the chunk name you provided here: std::to_string(current_id++).c_str()
Since it doesn't start with # it's treated as the string that was loaded, hence the [string ""] syntax. If you want the error message to treat it as a filename, put # in front of the filename.
And I suspect that the reason the line number is 0 because you told Lua to delete the line numbers, by setting "strip" to true.

Using sqlite3_bind_XXX inside a loop

I want to do multiple parameterized inserts with SQLite in my code. For this :
I have a single prepare statement outside of my loop as :
error = sqlite3_prepare(connection, insert_sql, strlen(insert_sql), &stmt, NULL);
I want inserts within a loop as:
while ( not reached end of datafile ) {
// Insert into server table
sqlite3_bind_int(stmt, 1, id1);
sqlite3_bind_double(stmt, 2, latitude);
sqlite3_bind_double(stmt, 3, longitude);
sqlite3_step(stmt);
}
The API docs for the function : https://www.sqlite.org/c3ref/bind_blob.html
mention that :
sqlite3_step() has been called more recently than sqlite3_reset(), then the call will return SQLITE_MISUSE
Bindings are not cleared by the sqlite3_reset() routine
If any sqlite3_bind_() routine is passed a prepared statement that has
been finalized, the result is undefined and probably harmful.
I am really confused as to how do I do repeated inserts with parameterized query in SQLite?
Just call sqlite3_reset() after sqlite3_step().

ADODC retrieve indivisuals values into EditBox

Is there a method to retrieve individual values from ADODC into an edit box
I tried the following way :-
m_edit1=m_adodc1.GetRecordset().GetField().GetItem("table1_names");
but got an error "binary '=' no conversion available .....
as I remember
You can retrieve the value of a field with the Value property, you then need to call the SetWindowText() member function pof the edit control, like:
m_edit1.SetWindowText(m_adodc1.GetRecordset().GetField().GetItem("table1_names").Value());
EDIT: the value needs to be converted using the _bstr_t class:
LPTSTR lpValue = (LPTSTR)(_bstr_t) m_adodc1.GetRecordset().GetField().GetItem("table1_names").Value();
m_edit1.SetWindowText(lpValue);
ADODC or nothing ;)
however check the follwing :-
i made it ,,, partially !!
to inform you about my mdb file :-
its name (inventory.mdb)
it has one table (Stocks)
the fields are (StockName,StockID,...)
now i can extract values from only the "StockName" which its string values
by the following code in button click :-
m_ado.SetRecordSource ("SELECT * FROM Stocks");
m_ado.Refresh ();
C_Recordset m_Record = m_ado.GetRecordset ();// this line can be omitted !
COleVariant var1;
var1.vt = VT_I2;
var1.iVal = 1;
COleVariant value = m_ado.GetRecordset().GetFields().GetItem(var1).GetValue ();
m_edit = value.bstrVal;
UpdateData (FALSE);
how to extract the other values such as "StockID" which its integer or any other fields ?

Instantiating Lua objects through the C API

I am trying to do some OO functionality in Lua via the C API. In my Lua script I have the following:
Parser = {}
Parser.__index = Parser
function Parser:create(url)
local self = {}
print ("creating")
self.baseUrl = url
setmetatable(self, Parser)
return self
end
function Parser:Foo()
print ("baseUrl: " .. self.baseUrl)
end
p = Parser:create("http://www.google.com")
p:Foo()
If I run this from the command line, it works fine and I see the following output:
creating
baseUrl: http://www.google.com
Now, if I comment out the last two lines and try the following through the C API
// <load the state and lua file>
lua_getglobal(L, "Parser");
lua_getfield(L, -1, "create");
lua_pushstring(L, "http://www.google.com");
if (lua_pcall(L, 1, 1, 0) != 0)
{
// handle error
}
This works. I see "creating" in my standard output as expected. As I understand it, the new Parser object is now on top of the stack. If I them immediately try the following:
lua_getfield(L, -1, "Foo");
if (lua_pcall(L, 0, 0, 0) != 0)
{
logger()->error("-- %1", lua_tostring(L, -1));
}
I get the following error: attempt to index local 'self' (a nil value)
Can anyone tell me what I'm doing wrong and how to get the function to run as expected?
Thank you!
The definition function Parser:Foo() ... end is equivalent to:
Parser.Foo = function(self)
print ("baseUrl: " .. self.baseUrl)
end
That is -- Foo is a function that takes one argument. When you call lua_pcall(L, 0, 0, 0) you are passing 0 arguments. Change it to lua_pcall(L, 1, 0, 0) and everything should work. (You will have to also change the pcall to create to correctly pass 2 arguments rather than 1).