Passing Lua variable to C

I embed Lua in a C ++ application, and I want to provide a print () function (or maybe just override the Lua print function) so that I can pass variables of simple data types (strings, booleans and numbers) to my C ++ as strings.

So what I want to do is have C ++ - a function that I export to Lua called my_print ()

Then I can call my_print () in the Lua block as follows:

a = 22/7
b = false
c = 42
my_print('The value of variable a is: ' .. a)
my_print('b: ' .. b)
my_print('c is: ' .. c)

Each time my_print () is called, it passes the C ++ string to the C ++ application. I looked at the Lua C API, I suspect that I will have to use lua_gettop (L), lua_type (), etc.

A small snippet of how to start writing a C / C ++ function that can be exported to Lua and used as described above will be greatly appreciated.

+3
3

print Lua? lmathlib.c. . http://www.lua.org/source/5.1/. etc/min.c .

+4

http://www.lua.org/source/5.1/lbaselib.c.html#luaB_tostring

, . lhf, __tostring, min.c - . , , Lua oo.

+1

, , my_print gets ( lua_lstring), ... false . tostring(var) . string.format.

0
source

Source: https://habr.com/ru/post/1792377/


All Articles