test lua support

This commit is contained in:
2020-07-22 21:11:33 +03:00
parent 20c58f5423
commit 94c09e1131
25 changed files with 7501 additions and 304 deletions

330
main.cpp
View File

@@ -1,309 +1,33 @@
#include "pip.h"
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
/*#include <sys/ioctl.h>
#include <sys/select.h>
void print(PIConfig::Entry*e, PIString indent = "") {
piCout << indent << e->name() << "=" << e->value();
indent += " ";
e->children().forEach([=](PIConfig::Entry*e)->PIConfig::Entry*{print(e, indent); return e;});
#ifdef PIP_LUA
extern "C" {
# include "lua.h"
# include "lauxlib.h"
# include "lualib.h"
}
#include <LuaBridge/LuaBridge.h>
#include "pistring_std.h"
class AsyncIOWatcher: public PIThread {
PIOBJECT_SUBCLASS(AsyncIOWatcher, PIThread)
public:
AsyncIOWatcher() {
pipe_fd[0] = pipe_fd[1] = 0;
if (pipe(pipe_fd) != 0) {
piCoutObj << "Warning: can`t create pipe," << errorString();
} else {
fd_list << pipe_fd[0];
}
piCout << pipe_fd[0] << pipe_fd[1];
fd_list_changed = false;
start();
}
~AsyncIOWatcher() {
stop();
breakSelect();
if (!waitForFinish(2000))
terminate();
if (pipe_fd[0]) ::close(pipe_fd[0]);
if (pipe_fd[1]) ::close(pipe_fd[1]);
}
void add(int fd) {
que_mutex.lock();
fd_list_changed = true;
if (!add_que.contains(fd))
add_que.enqueue(fd);
que_mutex.unlock();
breakSelect();
}
void remove(int fd) {
que_mutex.lock();
fd_list_changed = true;
if (!remove_que.contains(fd))
remove_que.enqueue(fd);
que_mutex.unlock();
breakSelect();
}
private:
virtual void run() {
que_mutex.lock();
if (fd_list_changed) {
for (int i = 0; i < add_que.size_s(); ++i) {
if (!fd_list.contains(add_que[i]))
fd_list << add_que[i];
}
for (int i = 0; i < remove_que.size_s(); ++i) {
fd_list.removeAll(remove_que[i]);
}
add_que.clear();
remove_que.clear();
}
fd_list_changed = false;
que_mutex.unlock();
s_tv.tv_sec = 1;
s_tv.tv_usec = 0;
FD_ZERO(&s_set);
int max_fd = 0;
piForeachC (int fd, fd_list) {
FD_SET(fd, &s_set);
if (max_fd < fd)
max_fd = fd;
}
int ret = select(max_fd + 1, &s_set, 0, 0, 0);
piCout << "select" << ret;
if (ret <= 0) return;
read_buff.resize(1024);
uint ibuff = 0;
piForeachC (int fd, fd_list) {
if (!FD_ISSET(fd, &s_set)) continue;
if (fd == pipe_fd[0]) {
read(fd, &ibuff, sizeof(ibuff));
piCoutObj << "breaked";
continue;
}
int readed = read(fd, read_buff.data(), read_buff.size_s());
piCout << "readed" << fd << readed;
}
}
void breakSelect() {
if (pipe_fd[1])
::write(pipe_fd[1], "\0", 1);
}
PIQueue<int> add_que, remove_que;
PIDeque<int> fd_list;
PIByteArray read_buff;
PIMutex que_mutex;
bool fd_list_changed;
int pipe_fd[2];
fd_set s_set;
struct timeval s_tv;
};
*/
struct A {
double arr[1000];
};
static const char * script
= "-- script.lua \n"
"testString = \"LuaBridge works!\" \n"
"number = 42 \n";
//PIKbdListener kbd(0, 0, false);
// void swap(int & x, int & y) {int t = x; x = y; y = t;}
// void swap2(int & x, int & y) {int t(std::move(x)); x = y; y = t;}
// void swap(uint & x, uint & y) {uint t = x; x = y; y = t;}
// void swap2(uint & x, uint & y) {uint t(std::move(x)); x = y; y = t;}
// void swap(ullong & x, ullong & y) {ullong t = x; x = y; y = t;}
// void swap2(ullong & x, ullong & y) {ullong t{std::move(x)}; x = y; y = t;}
// void swap(llong & x, llong & y) {llong t = x; x = y; y = t;}
// void swap2(llong & x, llong & y) {llong t{std::move(x)}; x = y; y = t;}
// void swap(double & x, double & y) {double t = x; x = y; y = t;}
// void swap2(double & x, double & y) {double t(std::move(x)); x = std::move(y); y = std::move(t);}
// void swap(float & x, float & y) {float t = x; x = y; y = t;}
// void swap2(float & x, float & y) {float t(std::move(x)); x = std::move(y); y = std::move(t);}
// void swap(PIString & x, PIString & y) {PIString t = x; x = y; y = t;}
// void swap2(PIString & x, PIString & y) {PIString t(std::move(x)); x = std::move(y); y = std::move(t);}
// void swap(std::string & x, std::string & y) {std::string t = x; x = y; y = t;}
// void swap2(std::string & x, std::string & y) {std::string t{std::move(x)}; x = std::move(y); y = std::move(t);}
// void swap(A & x, A & y) {A t = x; x = y; y = t;}
// void swap2(A & x, A & y) {A t{std::move(x)}; x = std::move(y); y = std::move(t);}
//void swap(PIObject & x, PIObject & y) {A t = x; x = y; y = t;}
//void swap2(PIObject & x, PIObject & y) {A t(std::move(x)); x = std::move(y); y = std::move(t);}
int main(int argc, char * argv[]) {
// PITimeMeasurer tm;
// int m = 100000;
// int a = 99, b = 77;
// piCout << "int";
// tm.reset();
// for (int i = 0; i < m; ++i) {
// swap(a,b);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// swap2(a,b);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// piSwapBinary(a,b);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// piSwap(a,b);
// }
// piCout << tm.elapsed_s();
// piCout << "size_t";
// size_t ta = 99, tb = 77;
// tm.reset();
// for (int i = 0; i < m; ++i) {
// swap(ta,tb);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// swap2(ta,tb);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// piSwapBinary(ta,tb);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// piSwap(ta,tb);
// }
// piCout << tm.elapsed_s();
// piCout << "ullong";
// ullong lla = 99, llb = 77;
// tm.reset();
// for (int i = 0; i < m; ++i) {
// swap(lla,llb);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// swap2(lla,llb);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// piSwapBinary(lla,llb);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// piSwap(lla,llb);
// }
// piCout << tm.elapsed_s();
// piCout << "double";
// double da = 0.99,db = 77.77;
// tm.reset();
// for (int i = 0; i < m; ++i) {
// swap(da,db);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// swap2(da,db);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// piSwapBinary(da,db);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// piSwap(da,db);
// }
// piCout << tm.elapsed_s();
// piCout << "float";
// float fa = 0.99,fb = 77.77;
// tm.reset();
// for (int i = 0; i < m; ++i) {
// swap(fa,fb);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// swap2(fa,fb);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// piSwapBinary(fa,fb);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// piSwap(fa,fb);
// }
// piCout << tm.elapsed_s();
// piCout << "A";
// A aa,ab;
// tm.reset();
// for (int i = 0; i < m; ++i) {
// swap(aa,ab);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// swap2(aa,ab);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// piSwapBinary(aa,ab);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// piSwap(aa,ab);
// }
// piCout << tm.elapsed_s();
// piCout << "std::string";
// std::string ia = "123456789012345678901vfsdvsd2345678",ib = "qwertyvsdfvvsdvfsuiopqwertyuikolsdfghjklsdfghjk";
// tm.reset();
// for (int i = 0; i < m; ++i) {
// swap(ia,ib);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// swap2(ia,ib);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// ia.swap(ib);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// piSwap(ia,ib);
// }
// piCout << tm.elapsed_s();
// PIString sa = "123456789012345678901vfsdvsd2345678",sb = "qwertyvsdfvvsdvfsuiopqwertyuikolsdfghjklsdfghjk";
// piCout << "PIString";
// tm.reset();
// for (int i = 0; i < m; ++i) {
// swap(sa,sb);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// swap2(sa, sb);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// sa.swap(sb);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// piSwapBinary(sa, sb);
// }
// piCout << tm.elapsed_s(); tm.reset();
// for (int i = 0; i < m; ++i) {
// piSwap(sa, sb);
// }
// piCout << tm.elapsed_s();
using namespace luabridge;
int main() {
lua_State* L = luaL_newstate();
luaL_dostring(L, script);
luaL_openlibs(L);
lua_pcall(L, 0, 0, 0);
LuaRef s = getGlobal(L, "testString");
LuaRef n = getGlobal(L, "number");
std::string luaString = s.cast<std::string>();
int answer = n.cast<int>();
piCout << StdString2PIString(luaString);
piCout << "And here's our number:" << answer;
}
#else
int main() {
return 0;
}
#endif