Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // g++ -std=c++11 test.cpp `pkg-config --cflags lua51` `pkg-config --libs lua51` -o ver-lua && ./ver-lua
- // g++ -std=c++11 test.cpp `pkg-config --cflags luajit` `pkg-config --libs luajit` -o ver-luajit && ./ver-luajit
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <lua.hpp>
- #define PRINT_LUA_VERSION \
- "if not jit then" \
- " print(_VERSION) " \
- "else" \
- " print(jit.version) " \
- "end"
- inline float mystof(const std::string &str) {
- return atof(str.c_str());
- }
- #define stof mystof
- inline std::string ftos(float f) {
- std::ostringstream oss;
- oss << f;
- return oss.str();
- }
- int my_func(lua_State* L)
- {
- float x = (float)lua_tonumber(L, 1);
- std::string str = ftos(x);
- std::cout << "1st param(string):" << str << std::endl;
- lua_settop(L, 0);
- //lua_pushnumber(L, stod(str)); // double
- lua_pushnumber(L, stof(str)); // float
- return 1;
- }
- int main() {
- lua_State* L = luaL_newstate();
- luaL_openlibs(L);
- lua_register(L, "my_func", my_func);
- if (luaL_dostring(L, PRINT_LUA_VERSION)) {
- lua_close(L);
- exit(EXIT_FAILURE);
- }
- if (luaL_dostring(L, "print('function result :' .. my_func(0.1))")) {
- lua_close(L);
- exit(EXIT_FAILURE);
- }
- lua_close(L);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement