Update modules to be Lua coroutine compatible (#1423)

This commit is contained in:
Yury Popov 2016-08-02 23:33:05 +03:00 committed by Marcel Stör
parent ff46abe61f
commit a499716cc1
11 changed files with 92 additions and 130 deletions

View File

@ -2,19 +2,14 @@
#include "../lua/lauxlib.h"
#include <c_stdlib.h>
static lua_State *gL;
static const char errfmt[] = "cjson %salloc: out of mem (%d bytes)";
void cjson_mem_setlua (lua_State *L)
{
gL = L;
}
void *cjson_mem_malloc (uint32_t sz)
{
void *p = (void*)c_malloc (sz);
if (!p && gL)
luaL_error (gL, errfmt, "m", sz);
lua_State *L = lua_getstate();
if (!p)
luaL_error (L, errfmt, "m", sz);
return p;
}
@ -22,7 +17,8 @@ void *cjson_mem_malloc (uint32_t sz)
void *cjson_mem_realloc (void *o, uint32_t sz)
{
void *p = (void*)c_realloc (o, sz);
if (!p && gL)
luaL_error (gL, errfmt, "re", sz);
lua_State *L = lua_getstate();
if (!p)
luaL_error (L, errfmt, "re", sz);
return p;
}

View File

@ -3,8 +3,6 @@
#include "../lua/lua.h"
void cjson_mem_setlua (lua_State *L);
void *cjson_mem_malloc (uint32_t sz);
void *cjson_mem_realloc (void *p, uint32_t sz);

View File

@ -155,7 +155,6 @@ typedef struct
typedef struct coap_luser_entry coap_luser_entry;
struct coap_luser_entry{
lua_State *L;
// int ref;
// char name[MAX_SEGMENTS_SIZE+1]; // +1 for string '\0'
const char *name;

View File

@ -37,6 +37,7 @@ static int handle_get_variable(const coap_endpoint_t *ep, coap_rw_buffer_t *scra
const coap_option_t *opt;
uint8_t count;
int n;
lua_State *L = lua_getstate();
if (NULL != (opt = coap_findOptions(inpkt, COAP_OPTION_URI_PATH, &count)))
{
if ((count != ep->path->count ) && (count != ep->path->count + 1)) // +1 for /f/[function], /v/[variable]
@ -58,19 +59,17 @@ static int handle_get_variable(const coap_endpoint_t *ep, coap_rw_buffer_t *scra
NODE_DBG("/v1/v/");
NODE_DBG((char *)h->name);
NODE_DBG(" match.\n");
if(h->L == NULL)
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
if(c_strlen(h->name))
{
n = lua_gettop(h->L);
lua_getglobal(h->L, h->name);
if (!lua_isnumber(h->L, -1) && !lua_isstring(h->L, -1)) {
n = lua_gettop(L);
lua_getglobal(L, h->name);
if (!lua_isnumber(L, -1) && !lua_isstring(L, -1)) {
NODE_DBG ("should be a number or string.\n");
lua_settop(h->L, n);
lua_settop(L, n);
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
} else {
const char *res = lua_tostring(h->L,-1);
lua_settop(h->L, n);
const char *res = lua_tostring(L,-1);
lua_settop(L, n);
return coap_make_response(scratch, outpkt, (const uint8_t *)res, c_strlen(res), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, h->content_type);
}
}
@ -94,6 +93,7 @@ static int handle_post_function(const coap_endpoint_t *ep, coap_rw_buffer_t *scr
const coap_option_t *opt;
uint8_t count;
int n;
lua_State *L = lua_getstate();
if (NULL != (opt = coap_findOptions(inpkt, COAP_OPTION_URI_PATH, &count)))
{
if ((count != ep->path->count ) && (count != ep->path->count + 1)) // +1 for /f/[function], /v/[variable]
@ -116,37 +116,34 @@ static int handle_post_function(const coap_endpoint_t *ep, coap_rw_buffer_t *scr
NODE_DBG((char *)h->name);
NODE_DBG(" match.\n");
if(h->L == NULL)
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
if(c_strlen(h->name))
{
n = lua_gettop(h->L);
lua_getglobal(h->L, h->name);
if (lua_type(h->L, -1) != LUA_TFUNCTION) {
n = lua_gettop(L);
lua_getglobal(L, h->name);
if (lua_type(L, -1) != LUA_TFUNCTION) {
NODE_DBG ("should be a function\n");
lua_settop(h->L, n);
lua_settop(L, n);
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
} else {
lua_pushlstring(h->L, inpkt->payload.p, inpkt->payload.len); // make sure payload.p is filled with '\0' after payload.len, or use lua_pushlstring
lua_call(h->L, 1, 1);
if (!lua_isnil(h->L, -1)){ /* get return? */
if( lua_isstring(h->L, -1) ) // deal with the return string
lua_pushlstring(L, inpkt->payload.p, inpkt->payload.len); // make sure payload.p is filled with '\0' after payload.len, or use lua_pushlstring
lua_call(L, 1, 1);
if (!lua_isnil(L, -1)){ /* get return? */
if( lua_isstring(L, -1) ) // deal with the return string
{
size_t len = 0;
const char *ret = luaL_checklstring( h->L, -1, &len );
const char *ret = luaL_checklstring( L, -1, &len );
if(len > MAX_PAYLOAD_SIZE){
lua_settop(h->L, n);
luaL_error( h->L, "return string:<MAX_PAYLOAD_SIZE" );
lua_settop(L, n);
luaL_error( L, "return string:<MAX_PAYLOAD_SIZE" );
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
}
NODE_DBG((char *)ret);
NODE_DBG("\n");
lua_settop(h->L, n);
lua_settop(L, n);
return coap_make_response(scratch, outpkt, ret, len, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
}
} else {
lua_settop(h->L, n);
lua_settop(L, n);
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
}
}
@ -198,10 +195,10 @@ static int handle_get_id(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, c
return coap_make_response(scratch, outpkt, (const uint8_t *)(&id), sizeof(uint32_t), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
}
coap_luser_entry var_head = {NULL,NULL,NULL,0};
coap_luser_entry var_head = {NULL,NULL,0};
coap_luser_entry *variable_entry = &var_head;
coap_luser_entry func_head = {NULL,NULL,NULL,0};
coap_luser_entry func_head = {NULL,NULL,0};
coap_luser_entry *function_entry = &func_head;
const coap_endpoint_t endpoints[] =

View File

@ -304,7 +304,7 @@ static int bme280_lua_init(lua_State* L) {
static void bme280_readoutdone (void *arg)
{
NODE_DBG("timer out\n");
lua_State *L = arg;
lua_State *L = lua_getstate();
lua_rawgeti (L, LUA_REGISTRYINDEX, lua_connected_readout_ref);
lua_call (L, 0, 0);
luaL_unref (L, LUA_REGISTRYINDEX, lua_connected_readout_ref);

View File

@ -1617,8 +1617,6 @@ static const LUA_REG_TYPE cjson_map[] = {
int luaopen_cjson( lua_State *L )
{
cjson_mem_setlua (L);
/* Initialise number conversions */
// fpconv_init(); // not needed for a specific cpu.
if(-1==cfg_init(&_cfg)){

View File

@ -24,7 +24,6 @@ coap_queue_t *gQueue = NULL;
typedef struct lcoap_userdata
{
lua_State *L;
struct espconn *pesp_conn;
int self_ref;
}lcoap_userdata;
@ -103,7 +102,6 @@ static int coap_create( lua_State* L, const char* mt )
pesp_conn->state = ESPCONN_NONE;
NODE_DBG("UDP server/client is set.\n");
cud->L = L;
pesp_conn->reverse = cud;
NODE_DBG("coap_create is called.\n");
@ -129,7 +127,6 @@ static int coap_delete( lua_State* L, const char* mt )
cud->self_ref = LUA_NOREF;
}
cud->L = NULL;
if(cud->pesp_conn)
{
if(cud->pesp_conn->proto.udp->remote_port || cud->pesp_conn->proto.udp->local_port)
@ -462,10 +459,8 @@ static int coap_regist( lua_State* L, const char* mt, int isvar )
return luaL_error(L, "not enough memory");
h->next = NULL;
h->name = NULL;
h->L = NULL;
}
h->L = L;
h->name = name;
h->content_type = content_type;

View File

@ -34,7 +34,6 @@ static int expose_array(lua_State* L, char *array, unsigned short len);
#define MAX_SOCKET 5
static int socket_num = 0;
static int socket[MAX_SOCKET];
static lua_State *gL = NULL;
static int tcpserver_cb_connect_ref = LUA_NOREF; // for tcp server connected callback
static uint16_t tcp_server_timeover = 30;
@ -65,8 +64,7 @@ static void net_server_disconnected(void *arg) // for tcp server only
lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
if(nud == NULL)
return;
if(gL == NULL)
return;
lua_State *L = lua_getstate();
#if 0
char temp[20] = {0};
c_sprintf(temp, IPSTR, IP2STR( &(pesp_conn->proto.tcp->remote_ip) ) );
@ -78,25 +76,25 @@ static void net_server_disconnected(void *arg) // for tcp server only
#endif
if(nud->cb_disconnect_ref != LUA_NOREF && nud->self_ref != LUA_NOREF)
{
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_disconnect_ref);
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua
lua_call(gL, 1, 0);
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->cb_disconnect_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua
lua_call(L, 1, 0);
}
int i;
lua_gc(gL, LUA_GCSTOP, 0);
lua_gc(L, LUA_GCSTOP, 0);
for(i=0;i<MAX_SOCKET;i++){
if( (LUA_NOREF!=socket[i]) && (socket[i] == nud->self_ref) ){
// found the saved client
nud->pesp_conn->reverse = NULL;
nud->pesp_conn = NULL; // the espconn is made by low level sdk, do not need to free, delete() will not free it.
nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
luaL_unref(gL, LUA_REGISTRYINDEX, socket[i]);
luaL_unref(L, LUA_REGISTRYINDEX, socket[i]);
socket[i] = LUA_NOREF;
socket_num--;
break;
}
}
lua_gc(gL, LUA_GCRESTART, 0);
lua_gc(L, LUA_GCRESTART, 0);
}
static void net_socket_disconnected(void *arg) // tcp only
@ -108,11 +106,12 @@ static void net_socket_disconnected(void *arg) // tcp only
lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
if(nud == NULL)
return;
lua_State *L = lua_getstate();
if(nud->cb_disconnect_ref != LUA_NOREF && nud->self_ref != LUA_NOREF)
{
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_disconnect_ref);
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua
lua_call(gL, 1, 0);
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->cb_disconnect_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua
lua_call(L, 1, 0);
}
if(pesp_conn->proto.tcp)
@ -121,12 +120,12 @@ static void net_socket_disconnected(void *arg) // tcp only
if(nud->pesp_conn)
c_free(nud->pesp_conn);
nud->pesp_conn = NULL; // espconn is already disconnected
lua_gc(gL, LUA_GCSTOP, 0);
lua_gc(L, LUA_GCSTOP, 0);
if(nud->self_ref != LUA_NOREF){
luaL_unref(gL, LUA_REGISTRYINDEX, nud->self_ref);
luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
}
lua_gc(gL, LUA_GCRESTART, 0);
lua_gc(L, LUA_GCRESTART, 0);
}
static void net_server_reconnected(void *arg, sint8_t err)
@ -154,15 +153,16 @@ static void net_socket_received(void *arg, char *pdata, unsigned short len)
return;
if(nud->self_ref == LUA_NOREF)
return;
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_receive_ref);
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(server) to callback func in lua
// expose_array(gL, pdata, len);
lua_State *L = lua_getstate();
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->cb_receive_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(server) to callback func in lua
// expose_array(L, pdata, len);
// *(pdata+len) = 0;
// NODE_DBG(pdata);
// NODE_DBG("\n");
lua_pushlstring(gL, pdata, len);
// lua_pushinteger(gL, len);
lua_call(gL, 2, 0);
lua_pushlstring(L, pdata, len);
// lua_pushinteger(L, len);
lua_call(L, 2, 0);
}
static void net_socket_sent(void *arg)
@ -178,9 +178,10 @@ static void net_socket_sent(void *arg)
return;
if(nud->self_ref == LUA_NOREF)
return;
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_send_ref);
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(server) to callback func in lua
lua_call(gL, 1, 0);
lua_State *L = lua_getstate();
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->cb_send_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(server) to callback func in lua
lua_call(L, 1, 0);
}
static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
@ -205,34 +206,16 @@ static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
NODE_DBG("self_ref null.\n");
return;
}
/* original
if(ipaddr == NULL)
{
NODE_ERR( "DNS Fail!\n" );
goto end;
}
// ipaddr->addr is a uint32_t ip
char ip_str[20];
c_memset(ip_str, 0, sizeof(ip_str));
if(ipaddr->addr != 0)
{
c_sprintf(ip_str, IPSTR, IP2STR(&(ipaddr->addr)));
}
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); // the callback function
//lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(conn) to callback func in lua
lua_pushstring(gL, ip_str); // the ip para
*/
lua_State *L = lua_getstate();
// "enhanced"
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); // the callback function
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(conn) to callback func in lua
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); // the callback function
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(conn) to callback func in lua
if(ipaddr == NULL)
{
NODE_DBG( "DNS Fail!\n" );
lua_pushnil(gL);
lua_pushnil(L);
}else{
// ipaddr->addr is a uint32_t ip
char ip_str[20];
@ -241,21 +224,21 @@ static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
{
c_sprintf(ip_str, IPSTR, IP2STR(&(ipaddr->addr)));
}
lua_pushstring(gL, ip_str); // the ip para
lua_pushstring(L, ip_str); // the ip para
}
// "enhanced" end
lua_call(gL, 2, 0);
lua_call(L, 2, 0);
end:
if((pesp_conn->type == ESPCONN_TCP && pesp_conn->proto.tcp->remote_port == 0)
|| (pesp_conn->type == ESPCONN_UDP && pesp_conn->proto.udp->remote_port == 0) ){
lua_gc(gL, LUA_GCSTOP, 0);
lua_gc(L, LUA_GCSTOP, 0);
if(nud->self_ref != LUA_NOREF){
luaL_unref(gL, LUA_REGISTRYINDEX, nud->self_ref);
luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
}
lua_gc(gL, LUA_GCRESTART, 0);
lua_gc(L, LUA_GCRESTART, 0);
}
}
@ -295,25 +278,24 @@ static void net_server_connected(void *arg) // for tcp only
if(tcpserver_cb_connect_ref == LUA_NOREF)
return;
if(!gL)
return;
lua_State *L = lua_getstate();
lua_rawgeti(gL, LUA_REGISTRYINDEX, tcpserver_cb_connect_ref); // get function
lua_rawgeti(L, LUA_REGISTRYINDEX, tcpserver_cb_connect_ref); // get function
// create a new client object
skt = (lnet_userdata *)lua_newuserdata(gL, sizeof(lnet_userdata));
skt = (lnet_userdata *)lua_newuserdata(L, sizeof(lnet_userdata));
if(!skt){
NODE_ERR("can't newudata\n");
lua_pop(gL, 1);
lua_pop(L, 1);
return;
}
// set its metatable
luaL_getmetatable(gL, "net.socket");
lua_setmetatable(gL, -2);
luaL_getmetatable(L, "net.socket");
lua_setmetatable(L, -2);
// pre-initialize it, in case of errors
skt->self_ref = LUA_NOREF;
lua_pushvalue(gL, -1); // copy the top of stack
skt->self_ref = luaL_ref(gL, LUA_REGISTRYINDEX); // ref to it self, for module api to find the userdata
lua_pushvalue(L, -1); // copy the top of stack
skt->self_ref = luaL_ref(L, LUA_REGISTRYINDEX); // ref to it self, for module api to find the userdata
socket[i] = skt->self_ref; // save to socket array
socket_num++;
skt->cb_connect_ref = LUA_NOREF; // this socket already connected
@ -337,7 +319,7 @@ static void net_server_connected(void *arg) // for tcp only
espconn_regist_reconcb(pesp_conn, net_server_reconnected);
// now socket[i] has the client ref, and stack top has the userdata
lua_call(gL, 1, 0); // function(conn)
lua_call(L, 1, 0); // function(conn)
}
static void net_socket_connected(void *arg)
@ -358,9 +340,10 @@ static void net_socket_connected(void *arg)
return;
if(nud->self_ref == LUA_NOREF)
return;
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_connect_ref);
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua
lua_call(gL, 1, 0);
lua_State *L = lua_getstate();
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->cb_connect_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua
lua_call(L, 1, 0);
}
// Lua: s = net.create(type, secure/timeout, function(conn))
@ -492,8 +475,6 @@ static int net_create( lua_State* L, const char* mt )
pUdpServer = pesp_conn;
}
gL = L; // global L for net module.
// if call back function is specified, call it with para userdata
// luaL_checkanyfunction(L, 2);
if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION){
@ -575,12 +556,12 @@ static int net_delete( lua_State* L, const char* mt )
luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_dns_found_ref);
nud->cb_dns_found_ref = LUA_NOREF;
}
lua_gc(gL, LUA_GCSTOP, 0);
lua_gc(L, LUA_GCSTOP, 0);
if(LUA_NOREF!=nud->self_ref){
luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
nud->self_ref = LUA_NOREF;
}
lua_gc(gL, LUA_GCRESTART, 0);
lua_gc(L, LUA_GCRESTART, 0);
return 0;
}
@ -625,19 +606,18 @@ static void socket_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
if(nud == NULL)
return;
if(gL == NULL)
return;
lua_State *L = lua_getstate();
if(ipaddr == NULL)
{
dns_reconn_count++;
if( dns_reconn_count >= 5 ){
NODE_ERR( "DNS Fail!\n" );
lua_gc(gL, LUA_GCSTOP, 0);
lua_gc(L, LUA_GCSTOP, 0);
if(nud->self_ref != LUA_NOREF){
luaL_unref(gL, LUA_REGISTRYINDEX, nud->self_ref);
luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
}
lua_gc(gL, LUA_GCRESTART, 0);
lua_gc(L, LUA_GCRESTART, 0);
return;
}
NODE_ERR( "DNS retry %d!\n", dns_reconn_count );
@ -1175,8 +1155,8 @@ static int net_dns_static( lua_State* L )
lua_pushinteger(L, UDP); // we are going to create a new dummy UDP socket
lua_call(L,1,1);// after this the stack should have a socket
lua_rawgeti(gL, LUA_REGISTRYINDEX, rdom); // load domain back to the stack
lua_rawgeti(gL, LUA_REGISTRYINDEX, rfunc); // load the callback function back to the stack
lua_rawgeti(L, LUA_REGISTRYINDEX, rdom); // load domain back to the stack
lua_rawgeti(L, LUA_REGISTRYINDEX, rfunc); // load the callback function back to the stack
luaL_unref(L, LUA_REGISTRYINDEX, rdom); //free reference
luaL_unref(L, LUA_REGISTRYINDEX, rfunc); //free reference

View File

@ -333,7 +333,7 @@ void output_redirect(const char *str) {
// return;
// }
if (output_redir_ref == LUA_NOREF || !L) {
if (output_redir_ref == LUA_NOREF) {
uart0_sendStr(str);
return;
}

View File

@ -204,7 +204,7 @@ static void on_recv (void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_a
(void)port;
sntp_dbg("sntp: on_recv\n");
lua_State *L = arg;
lua_State *L = lua_getstate();
if (!state || state->pcb != pcb)
{

View File

@ -8,7 +8,6 @@
#include "c_string.h"
#include "rom.h"
static lua_State *gL = NULL;
static int uart_receive_rf = LUA_NOREF;
bool run_input = true;
bool uart_on_data_cb(const char *buf, size_t len){
@ -16,11 +15,12 @@ bool uart_on_data_cb(const char *buf, size_t len){
return false;
if(uart_receive_rf == LUA_NOREF)
return false;
if(!gL)
lua_State *L = lua_getstate();
if(!L)
return false;
lua_rawgeti(gL, LUA_REGISTRYINDEX, uart_receive_rf);
lua_pushlstring(gL, buf, len);
lua_call(gL, 1, 0);
lua_rawgeti(L, LUA_REGISTRYINDEX, uart_receive_rf);
lua_pushlstring(L, buf, len);
lua_call(L, 1, 0);
return !run_input;
}
@ -75,7 +75,6 @@ static int uart_on( lua_State* L )
}
if(!lua_isnil(L, -1)){
uart_receive_rf = luaL_ref(L, LUA_REGISTRYINDEX);
gL = L;
if(run==0)
run_input = false;
} else {