Commit dba57fa0 authored by Johny Mattsson's avatar Johny Mattsson
Browse files

Merge branch 'dev-esp32-idf4-lua53' into dev-esp32-idf4

parents 3a6961cc 8e5ce49d
...@@ -119,7 +119,7 @@ static void event_connected(lua_State* L, mqtt_context_t* mqtt_context, esp_mqtt ...@@ -119,7 +119,7 @@ static void event_connected(lua_State* L, mqtt_context_t* mqtt_context, esp_mqtt
luaX_push_weak_ref(L, mqtt_context->self); // push a reference to the client (first parameter) luaX_push_weak_ref(L, mqtt_context->self); // push a reference to the client (first parameter)
ESP_LOGD(TAG, "CB:connect: calling registered one-shot connect callback"); ESP_LOGD(TAG, "CB:connect: calling registered one-shot connect callback");
int res = lua_pcall(L, 1, 0, 0); //call the connect callback: function(client) int res = luaL_pcallx(L, 1, 0); //call the connect callback: function(client)
if (res != 0) if (res != 0)
ESP_LOGD(TAG, "CB:connect: Error when calling one-shot connect callback - (%d) %s", res, luaL_checkstring(L, -1)); ESP_LOGD(TAG, "CB:connect: Error when calling one-shot connect callback - (%d) %s", res, luaL_checkstring(L, -1));
...@@ -133,7 +133,7 @@ static void event_connected(lua_State* L, mqtt_context_t* mqtt_context, esp_mqtt ...@@ -133,7 +133,7 @@ static void event_connected(lua_State* L, mqtt_context_t* mqtt_context, esp_mqtt
ESP_LOGD(TAG, "CB:connect: calling registered standard connect callback"); ESP_LOGD(TAG, "CB:connect: calling registered standard connect callback");
lua_rawgeti(L, LUA_REGISTRYINDEX, mqtt_context->on_connect_cb); // push the callback function reference to the stack lua_rawgeti(L, LUA_REGISTRYINDEX, mqtt_context->on_connect_cb); // push the callback function reference to the stack
luaX_push_weak_ref(L, mqtt_context->self); // push a reference to the client (first parameter) luaX_push_weak_ref(L, mqtt_context->self); // push a reference to the client (first parameter)
int res = lua_pcall(L, 1, 0, 0); //call the connect callback: function(client) int res = luaL_pcallx(L, 1, 0); //call the connect callback: function(client)
if (res != 0) if (res != 0)
ESP_LOGD(TAG, "CB:connect: Error when calling connect callback - (%d) %s", res, luaL_checkstring(L, -1)); ESP_LOGD(TAG, "CB:connect: Error when calling connect callback - (%d) %s", res, luaL_checkstring(L, -1));
} }
...@@ -156,7 +156,7 @@ static void event_disconnected(lua_State* L, mqtt_context_t* mqtt_context, esp_m ...@@ -156,7 +156,7 @@ static void event_disconnected(lua_State* L, mqtt_context_t* mqtt_context, esp_m
lua_pushinteger(L, -6); // esp sdk mqtt lib does not provide reason codes. Push "-6" to be backward compatible with ESP8266 API lua_pushinteger(L, -6); // esp sdk mqtt lib does not provide reason codes. Push "-6" to be backward compatible with ESP8266 API
ESP_LOGD(TAG, "CB:disconnect: calling registered one-shot disconnect callback"); ESP_LOGD(TAG, "CB:disconnect: calling registered one-shot disconnect callback");
int res = lua_pcall(L, 2, 0, 0); //call the disconnect callback with 2 parameters: function(client, reason) int res = luaL_pcallx(L, 2, 0); //call the disconnect callback with 2 parameters: function(client, reason)
if (res != 0) if (res != 0)
ESP_LOGD(TAG, "CB:disconnect: Error when calling one-shot disconnect callback - (%d) %s", res, luaL_checkstring(L, -1)); ESP_LOGD(TAG, "CB:disconnect: Error when calling one-shot disconnect callback - (%d) %s", res, luaL_checkstring(L, -1));
...@@ -170,7 +170,7 @@ static void event_disconnected(lua_State* L, mqtt_context_t* mqtt_context, esp_m ...@@ -170,7 +170,7 @@ static void event_disconnected(lua_State* L, mqtt_context_t* mqtt_context, esp_m
ESP_LOGD(TAG, "CB:disconnect: calling registered standard on_offline_cb callback"); ESP_LOGD(TAG, "CB:disconnect: calling registered standard on_offline_cb callback");
lua_rawgeti(L, LUA_REGISTRYINDEX, mqtt_context->on_offline_cb); // push the callback function reference to the stack lua_rawgeti(L, LUA_REGISTRYINDEX, mqtt_context->on_offline_cb); // push the callback function reference to the stack
luaX_push_weak_ref(L, mqtt_context->self); // push a reference to the client (first parameter) luaX_push_weak_ref(L, mqtt_context->self); // push a reference to the client (first parameter)
int res = lua_pcall(L, 1, 0, 0); //call the offline callback: function(client) int res = luaL_pcallx(L, 1, 0); //call the offline callback: function(client)
if (res != 0) if (res != 0)
ESP_LOGD(TAG, "CB:disconnect: Error when calling offline callback - (%d) %s", res, luaL_checkstring(L, -1)); ESP_LOGD(TAG, "CB:disconnect: Error when calling offline callback - (%d) %s", res, luaL_checkstring(L, -1));
} }
...@@ -184,7 +184,7 @@ static void event_subscribed(lua_State* L, mqtt_context_t* mqtt_context, esp_mqt ...@@ -184,7 +184,7 @@ static void event_subscribed(lua_State* L, mqtt_context_t* mqtt_context, esp_mqt
lua_rawgeti(L, LUA_REGISTRYINDEX, mqtt_context->subscribed_ok_cb); // push the function reference on the stack lua_rawgeti(L, LUA_REGISTRYINDEX, mqtt_context->subscribed_ok_cb); // push the function reference on the stack
luaX_push_weak_ref(L, mqtt_context->self); // push the client object on the stack luaX_push_weak_ref(L, mqtt_context->self); // push the client object on the stack
luaX_unset_ref(L, &mqtt_context->subscribed_ok_cb); // forget the callback since it is one-shot luaX_unset_ref(L, &mqtt_context->subscribed_ok_cb); // forget the callback since it is one-shot
int res = lua_pcall(L, 1, 0, 0); //call the connect callback with one parameter: function(client) int res = luaL_pcallx(L, 1, 0); //call the connect callback with one parameter: function(client)
if (res != 0) if (res != 0)
ESP_LOGD(TAG, "CB:subscribe: Error when calling one-shot subscribe callback - (%d) %s", res, luaL_checkstring(L, -1)); ESP_LOGD(TAG, "CB:subscribe: Error when calling one-shot subscribe callback - (%d) %s", res, luaL_checkstring(L, -1));
} }
...@@ -197,7 +197,7 @@ static void event_published(lua_State* L, mqtt_context_t* mqtt_context, esp_mqtt ...@@ -197,7 +197,7 @@ static void event_published(lua_State* L, mqtt_context_t* mqtt_context, esp_mqtt
lua_rawgeti(L, LUA_REGISTRYINDEX, mqtt_context->published_ok_cb); // push the callback function reference to the stack lua_rawgeti(L, LUA_REGISTRYINDEX, mqtt_context->published_ok_cb); // push the callback function reference to the stack
luaX_push_weak_ref(L, mqtt_context->self); // push the client reference to the stack luaX_push_weak_ref(L, mqtt_context->self); // push the client reference to the stack
luaX_unset_ref(L, &mqtt_context->published_ok_cb); // forget this callback since it is one-shot luaX_unset_ref(L, &mqtt_context->published_ok_cb); // forget this callback since it is one-shot
int res = lua_pcall(L, 1, 0, 0); //call the connect callback with 1 parameter: function(client) int res = luaL_pcallx(L, 1, 0); //call the connect callback with 1 parameter: function(client)
if (res != 0) if (res != 0)
ESP_LOGD(TAG, "CB:publish: Error when calling one-shot publish callback - (%d) %s", res, luaL_checkstring(L, -1)); ESP_LOGD(TAG, "CB:publish: Error when calling one-shot publish callback - (%d) %s", res, luaL_checkstring(L, -1));
} }
...@@ -210,7 +210,7 @@ static void event_unsubscribed(lua_State* L, mqtt_context_t* mqtt_context, esp_m ...@@ -210,7 +210,7 @@ static void event_unsubscribed(lua_State* L, mqtt_context_t* mqtt_context, esp_m
lua_rawgeti(L, LUA_REGISTRYINDEX, mqtt_context->unsubscribed_ok_cb); // push callback function reference on the stack lua_rawgeti(L, LUA_REGISTRYINDEX, mqtt_context->unsubscribed_ok_cb); // push callback function reference on the stack
luaX_push_weak_ref(L, mqtt_context->self); // push a reference to the client luaX_push_weak_ref(L, mqtt_context->self); // push a reference to the client
luaX_unset_ref(L, &mqtt_context->unsubscribed_ok_cb); // forget callback as it is one-shot luaX_unset_ref(L, &mqtt_context->unsubscribed_ok_cb); // forget callback as it is one-shot
int res = lua_pcall(L, 1, 0, 0); //call the connect callback with one parameter: function(client) int res = luaL_pcallx(L, 1, 0); //call the connect callback with one parameter: function(client)
if (res != 0) if (res != 0)
ESP_LOGD(TAG, "CB:unsubscribe: Error when calling one-shot unsubscribe callback - (%d) %s", res, luaL_checkstring(L, -1)); ESP_LOGD(TAG, "CB:unsubscribe: Error when calling one-shot unsubscribe callback - (%d) %s", res, luaL_checkstring(L, -1));
} }
...@@ -227,7 +227,7 @@ static void event_data_received(lua_State* L, mqtt_context_t* mqtt_context, esp_ ...@@ -227,7 +227,7 @@ static void event_data_received(lua_State* L, mqtt_context_t* mqtt_context, esp_
lua_pushlstring(L, event->data, event->data_len); lua_pushlstring(L, event->data, event->data_len);
numArg++; numArg++;
} }
int res = lua_pcall(L, numArg, 0, 0); //call the messagecallback int res = luaL_pcallx(L, numArg, 0); //call the messagecallback
if (res != 0) if (res != 0)
ESP_LOGD(TAG, "CB:data: Error when calling message callback - (%d) %s", res, luaL_checkstring(L, -1)); ESP_LOGD(TAG, "CB:data: Error when calling message callback - (%d) %s", res, luaL_checkstring(L, -1));
} }
...@@ -635,7 +635,9 @@ static int mqtt_new(lua_State* L) { ...@@ -635,7 +635,9 @@ static int mqtt_new(lua_State* L) {
} }
// map client methods to functions: // map client methods to functions:
LROT_BEGIN(mqtt_metatable) LROT_BEGIN(mqtt_metatable, NULL, 0)
LROT_FUNCENTRY(__gc, mqtt_delete)
LROT_TABENTRY(__index, mqtt_metatable)
LROT_FUNCENTRY(connect, mqtt_connect) LROT_FUNCENTRY(connect, mqtt_connect)
LROT_FUNCENTRY(close, mqtt_close) LROT_FUNCENTRY(close, mqtt_close)
LROT_FUNCENTRY(lwt, mqtt_lwt) LROT_FUNCENTRY(lwt, mqtt_lwt)
...@@ -643,17 +645,15 @@ LROT_BEGIN(mqtt_metatable) ...@@ -643,17 +645,15 @@ LROT_BEGIN(mqtt_metatable)
LROT_FUNCENTRY(subscribe, mqtt_subscribe) LROT_FUNCENTRY(subscribe, mqtt_subscribe)
LROT_FUNCENTRY(unsubscribe, mqtt_unsubscribe) LROT_FUNCENTRY(unsubscribe, mqtt_unsubscribe)
LROT_FUNCENTRY(on, mqtt_on) LROT_FUNCENTRY(on, mqtt_on)
LROT_FUNCENTRY(__gc, mqtt_delete)
LROT_TABENTRY(__index, mqtt_metatable)
LROT_END(mqtt_metatable, NULL, 0) LROT_END(mqtt_metatable, NULL, 0)
// Module function map // Module function map
LROT_BEGIN(mqtt) LROT_BEGIN(mqtt, NULL, 0)
LROT_FUNCENTRY(Client, mqtt_new) LROT_FUNCENTRY(Client, mqtt_new)
LROT_END(mqtt, NULL, 0) LROT_END(mqtt, NULL, 0)
int luaopen_mqtt(lua_State* L) { int luaopen_mqtt(lua_State* L) {
luaL_rometatable(L, MQTT_METATABLE, (void*)mqtt_metatable_map); // create metatable for mqtt luaL_rometatable(L, MQTT_METATABLE, LROT_TABLEREF(mqtt_metatable)); // create metatable for mqtt
return 0; return 0;
} }
......
...@@ -479,7 +479,7 @@ static int net_listen( lua_State *L ) { ...@@ -479,7 +479,7 @@ static int net_listen( lua_State *L ) {
if (!ipaddr_aton(domain, &addr)) if (!ipaddr_aton(domain, &addr))
return luaL_error(L, "invalid IP address"); return luaL_error(L, "invalid IP address");
if (ud->type == TYPE_TCP_SERVER) { if (ud->type == TYPE_TCP_SERVER) {
if (lua_isfunction(L, stack) || lua_islightfunction(L, stack)) { if (lua_isfunction(L, stack)) {
lua_pushvalue(L, stack++); lua_pushvalue(L, stack++);
luaL_unref(L, LUA_REGISTRYINDEX, ud->server.cb_accept_ref); luaL_unref(L, LUA_REGISTRYINDEX, ud->server.cb_accept_ref);
ud->server.cb_accept_ref = luaL_ref(L, LUA_REGISTRYINDEX); ud->server.cb_accept_ref = luaL_ref(L, LUA_REGISTRYINDEX);
...@@ -586,7 +586,7 @@ static int net_on( lua_State *L ) { ...@@ -586,7 +586,7 @@ static int net_on( lua_State *L ) {
} }
if (refptr == NULL) if (refptr == NULL)
return luaL_error(L, "invalid callback name"); return luaL_error(L, "invalid callback name");
if (lua_isfunction(L, 3) || lua_islightfunction(L, 3)) { if (lua_isfunction(L, 3)) {
lua_pushvalue(L, 3); lua_pushvalue(L, 3);
luaL_unref(L, LUA_REGISTRYINDEX, *refptr); luaL_unref(L, LUA_REGISTRYINDEX, *refptr);
*refptr = luaL_ref(L, LUA_REGISTRYINDEX); *refptr = luaL_ref(L, LUA_REGISTRYINDEX);
...@@ -620,7 +620,7 @@ static int net_send( lua_State *L ) { ...@@ -620,7 +620,7 @@ static int net_send( lua_State *L ) {
data = luaL_checklstring(L, stack++, &datalen); data = luaL_checklstring(L, stack++, &datalen);
if (!data || datalen == 0) return luaL_error(L, "no data to send"); if (!data || datalen == 0) return luaL_error(L, "no data to send");
ud->client.num_send = datalen; ud->client.num_send = datalen;
if (lua_isfunction(L, stack) || lua_islightfunction(L, stack)) { if (lua_isfunction(L, stack)) {
lua_pushvalue(L, stack++); lua_pushvalue(L, stack++);
luaL_unref(L, LUA_REGISTRYINDEX, ud->client.cb_sent_ref); luaL_unref(L, LUA_REGISTRYINDEX, ud->client.cb_sent_ref);
ud->client.cb_sent_ref = luaL_ref(L, LUA_REGISTRYINDEX); ud->client.cb_sent_ref = luaL_ref(L, LUA_REGISTRYINDEX);
...@@ -684,7 +684,7 @@ static int net_dns( lua_State *L ) { ...@@ -684,7 +684,7 @@ static int net_dns( lua_State *L ) {
const char *domain = luaL_checklstring(L, 2, &dl); const char *domain = luaL_checklstring(L, 2, &dl);
if (!domain) if (!domain)
return luaL_error(L, "no domain specified"); return luaL_error(L, "no domain specified");
if (lua_isfunction(L, 3) || lua_islightfunction(L, 3)) { if (lua_isfunction(L, 3)) {
luaL_unref(L, LUA_REGISTRYINDEX, ud->client.cb_dns_ref); luaL_unref(L, LUA_REGISTRYINDEX, ud->client.cb_dns_ref);
lua_pushvalue(L, 3); lua_pushvalue(L, 3);
ud->client.cb_dns_ref = luaL_ref(L, LUA_REGISTRYINDEX); ud->client.cb_dns_ref = luaL_ref(L, LUA_REGISTRYINDEX);
...@@ -890,7 +890,7 @@ static int net_dns_static( lua_State* L ) { ...@@ -890,7 +890,7 @@ static int net_dns_static( lua_State* L ) {
return luaL_error(L, "domain name too long"); return luaL_error(L, "domain name too long");
} }
luaL_checkanyfunction(L, 2); luaL_checkfunction(L, 2);
lua_settop(L, 2); lua_settop(L, 2);
dns_event_t *ev = luaM_new(L, dns_event_t); dns_event_t *ev = luaM_new(L, dns_event_t);
...@@ -968,7 +968,7 @@ static void ldnsfound_cb (lua_State *L, lnet_userdata *ud, ip_addr_t *addr) { ...@@ -968,7 +968,7 @@ static void ldnsfound_cb (lua_State *L, lnet_userdata *ud, ip_addr_t *addr) {
} else { } else {
lua_pushnil(L); lua_pushnil(L);
} }
lua_call(L, 2, 0); luaL_pcallx(L, 2, 0);
} }
ud->client.wait_dns --; ud->client.wait_dns --;
if (ud->netconn && ud->type == TYPE_TCP_CLIENT && !ud->client.connecting) { if (ud->netconn && ud->type == TYPE_TCP_CLIENT && !ud->client.connecting) {
...@@ -997,7 +997,7 @@ static void ldnsstatic_cb (lua_State *L, int cb_ref, ip_addr_t *addr) { ...@@ -997,7 +997,7 @@ static void ldnsstatic_cb (lua_State *L, int cb_ref, ip_addr_t *addr) {
} else { } else {
lua_pushnil(L); lua_pushnil(L);
} }
lua_call(L, 1, 0); luaL_pcallx(L, 1, 0);
} }
...@@ -1005,7 +1005,7 @@ static void lconnected_cb (lua_State *L, lnet_userdata *ud) { ...@@ -1005,7 +1005,7 @@ static void lconnected_cb (lua_State *L, lnet_userdata *ud) {
if (ud->self_ref != LUA_NOREF && ud->client.cb_connect_ref != LUA_NOREF) { if (ud->self_ref != LUA_NOREF && ud->client.cb_connect_ref != LUA_NOREF) {
lua_rawgeti(L, LUA_REGISTRYINDEX, ud->client.cb_connect_ref); lua_rawgeti(L, LUA_REGISTRYINDEX, ud->client.cb_connect_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, ud->self_ref); lua_rawgeti(L, LUA_REGISTRYINDEX, ud->self_ref);
lua_call(L, 1, 0); luaL_pcallx(L, 1, 0);
} }
} }
...@@ -1041,7 +1041,7 @@ static void lrecv_cb (lua_State *L, lnet_userdata *ud) { ...@@ -1041,7 +1041,7 @@ static void lrecv_cb (lua_State *L, lnet_userdata *ud) {
lua_pushinteger(L, port); lua_pushinteger(L, port);
lua_pushstring(L, iptmp); lua_pushstring(L, iptmp);
} }
lua_call(L, num_args, 0); luaL_pcallx(L, num_args, 0);
} }
} while (netbuf_next(p) != -1); } while (netbuf_next(p) != -1);
...@@ -1069,7 +1069,7 @@ static void laccept_cb (lua_State *L, lnet_userdata *ud) { ...@@ -1069,7 +1069,7 @@ static void laccept_cb (lua_State *L, lnet_userdata *ud) {
nud->netconn->pcb.tcp->keep_cnt = 1; nud->netconn->pcb.tcp->keep_cnt = 1;
} else } else
luaL_error(L, "cannot accept new server socket connection"); luaL_error(L, "cannot accept new server socket connection");
lua_call(L, 1, 0); luaL_pcallx(L, 1, 0);
} }
...@@ -1077,7 +1077,7 @@ static void lsent_cb (lua_State *L, lnet_userdata *ud) { ...@@ -1077,7 +1077,7 @@ static void lsent_cb (lua_State *L, lnet_userdata *ud) {
if (ud->client.cb_sent_ref != LUA_NOREF) { if (ud->client.cb_sent_ref != LUA_NOREF) {
lua_rawgeti(L, LUA_REGISTRYINDEX, ud->client.cb_sent_ref); lua_rawgeti(L, LUA_REGISTRYINDEX, ud->client.cb_sent_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, ud->self_ref); lua_rawgeti(L, LUA_REGISTRYINDEX, ud->self_ref);
lua_call(L, 1, 0); luaL_pcallx(L, 1, 0);
} }
} }
...@@ -1094,7 +1094,7 @@ static void lerr_cb (lua_State *L, lnet_userdata *ud, err_t err) ...@@ -1094,7 +1094,7 @@ static void lerr_cb (lua_State *L, lnet_userdata *ud, err_t err)
lua_rawgeti(L, LUA_REGISTRYINDEX, ref); lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, ud->self_ref); lua_rawgeti(L, LUA_REGISTRYINDEX, ud->self_ref);
lua_pushinteger(L, err); lua_pushinteger(L, err);
lua_call(L, 2, 0); luaL_pcallx(L, 2, 0);
} }
if (ud->client.wait_dns == 0) { if (ud->client.wait_dns == 0) {
lua_gc(L, LUA_GCSTOP, 0); lua_gc(L, LUA_GCSTOP, 0);
...@@ -1108,15 +1108,17 @@ static void lerr_cb (lua_State *L, lnet_userdata *ud, err_t err) ...@@ -1108,15 +1108,17 @@ static void lerr_cb (lua_State *L, lnet_userdata *ud, err_t err)
// --- Tables // --- Tables
// Module function map // Module function map
LROT_BEGIN(net_tcpserver) LROT_BEGIN(net_tcpserver, NULL, 0)
LROT_FUNCENTRY( __gc, net_delete )
LROT_TABENTRY ( __index, net_tcpserver )
LROT_FUNCENTRY( listen, net_listen ) LROT_FUNCENTRY( listen, net_listen )
LROT_FUNCENTRY( getaddr, net_getaddr ) LROT_FUNCENTRY( getaddr, net_getaddr )
LROT_FUNCENTRY( close, net_close ) LROT_FUNCENTRY( close, net_close )
LROT_FUNCENTRY( __gc, net_delete )
LROT_TABENTRY ( __index, net_tcpserver )
LROT_END(net_tcpserver, NULL, 0) LROT_END(net_tcpserver, NULL, 0)
LROT_BEGIN(net_tcpsocket) LROT_BEGIN(net_tcpsocket, NULL, 0)
LROT_FUNCENTRY( __gc, net_delete )
LROT_TABENTRY ( __index, net_tcpsocket )
LROT_FUNCENTRY( connect, net_connect ) LROT_FUNCENTRY( connect, net_connect )
LROT_FUNCENTRY( close, net_close ) LROT_FUNCENTRY( close, net_close )
LROT_FUNCENTRY( on, net_on ) LROT_FUNCENTRY( on, net_on )
...@@ -1124,28 +1126,27 @@ LROT_BEGIN(net_tcpsocket) ...@@ -1124,28 +1126,27 @@ LROT_BEGIN(net_tcpsocket)
LROT_FUNCENTRY( dns, net_dns ) LROT_FUNCENTRY( dns, net_dns )
LROT_FUNCENTRY( getpeer, net_getpeer ) LROT_FUNCENTRY( getpeer, net_getpeer )
LROT_FUNCENTRY( getaddr, net_getaddr ) LROT_FUNCENTRY( getaddr, net_getaddr )
LROT_FUNCENTRY( __gc, net_delete )
LROT_TABENTRY ( __index, net_tcpsocket )
LROT_END(net_tcpsocket, NULL, 0) LROT_END(net_tcpsocket, NULL, 0)
LROT_BEGIN(net_udpsocket) LROT_BEGIN(net_udpsocket, NULL, 0)
LROT_FUNCENTRY( __gc, net_delete )
LROT_TABENTRY ( __index, net_udpsocket )
LROT_FUNCENTRY( listen, net_listen ) LROT_FUNCENTRY( listen, net_listen )
LROT_FUNCENTRY( close, net_close ) LROT_FUNCENTRY( close, net_close )
LROT_FUNCENTRY( on, net_on ) LROT_FUNCENTRY( on, net_on )
LROT_FUNCENTRY( send, net_send ) LROT_FUNCENTRY( send, net_send )
LROT_FUNCENTRY( dns, net_dns ) LROT_FUNCENTRY( dns, net_dns )
LROT_FUNCENTRY( getaddr, net_getaddr ) LROT_FUNCENTRY( getaddr, net_getaddr )
LROT_FUNCENTRY( __gc, net_delete )
LROT_TABENTRY ( __index, net_udpsocket )
LROT_END(net_udpsocket, NULL, 0) LROT_END(net_udpsocket, NULL, 0)
LROT_BEGIN(net_dns) LROT_BEGIN(net_dns, NULL, 0)
LROT_FUNCENTRY( setdnsserver, net_setdnsserver ) LROT_FUNCENTRY( setdnsserver, net_setdnsserver )
LROT_FUNCENTRY( getdnsserver, net_getdnsserver ) LROT_FUNCENTRY( getdnsserver, net_getdnsserver )
LROT_FUNCENTRY( resolve, net_dns_static ) LROT_FUNCENTRY( resolve, net_dns_static )
LROT_END(net_dns, NULL, 0) LROT_END(net_dns, NULL, 0)
LROT_BEGIN(net) LROT_BEGIN(net, NULL, 0)
LROT_TABENTRY ( __metatable, net )
LROT_FUNCENTRY( createServer, net_createServer ) LROT_FUNCENTRY( createServer, net_createServer )
LROT_FUNCENTRY( createConnection, net_createConnection ) LROT_FUNCENTRY( createConnection, net_createConnection )
LROT_FUNCENTRY( createUDPSocket, net_createUDPSocket ) LROT_FUNCENTRY( createUDPSocket, net_createUDPSocket )
...@@ -1154,15 +1155,14 @@ LROT_BEGIN(net) ...@@ -1154,15 +1155,14 @@ LROT_BEGIN(net)
LROT_TABENTRY ( dns, net_dns ) LROT_TABENTRY ( dns, net_dns )
LROT_NUMENTRY ( TCP, TYPE_TCP ) LROT_NUMENTRY ( TCP, TYPE_TCP )
LROT_NUMENTRY ( UDP, TYPE_UDP ) LROT_NUMENTRY ( UDP, TYPE_UDP )
LROT_TABENTRY ( __metatable, net )
LROT_END(net, NULL, 0) LROT_END(net, NULL, 0)
int luaopen_net( lua_State *L ) { int luaopen_net( lua_State *L ) {
igmp_init(); igmp_init();
luaL_rometatable(L, NET_TABLE_TCP_SERVER, (void *)net_tcpserver_map); luaL_rometatable(L, NET_TABLE_TCP_SERVER, LROT_TABLEREF(net_tcpserver));
luaL_rometatable(L, NET_TABLE_TCP_CLIENT, (void *)net_tcpsocket_map); luaL_rometatable(L, NET_TABLE_TCP_CLIENT, LROT_TABLEREF(net_tcpsocket));
luaL_rometatable(L, NET_TABLE_UDP_SOCKET, (void *)net_udpsocket_map); luaL_rometatable(L, NET_TABLE_UDP_SOCKET, LROT_TABLEREF(net_udpsocket));
net_handler = task_get_id(handle_net_event); net_handler = task_get_id(handle_net_event);
dns_handler = task_get_id(handle_dns_event); dns_handler = task_get_id(handle_dns_event);
......
#include "module.h" #include "module.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "common.h" #include "common.h"
#include "legc.h"
#include "lundump.h" #include "lundump.h"
#include "platform.h" #include "platform.h"
#include "task/task.h" #include "task/task.h"
...@@ -14,7 +13,7 @@ ...@@ -14,7 +13,7 @@
#include "ldebug.h" #include "ldebug.h"
#include "esp_vfs.h" #include "esp_vfs.h"
#include "lnodeaux.h" #include "lnodeaux.h"
#include "lflash.h" #include "lpanic.h"
#include "rom/rtc.h" #include "rom/rtc.h"
// Lua: node.bootreason() // Lua: node.bootreason()
...@@ -57,7 +56,7 @@ static int node_bootreason( lua_State *L) ...@@ -57,7 +56,7 @@ static int node_bootreason( lua_State *L)
// 21: EXT_CPU_RESET = 14, /**<14, for APP CPU, reseted by PRO CPU*/ // 21: EXT_CPU_RESET = 14, /**<14, for APP CPU, reseted by PRO CPU*/
// 22: RTCWDT_BROWN_OUT_RESET = 15, /**<15, Reset when the vdd voltage is not stable*/ // 22: RTCWDT_BROWN_OUT_RESET = 15, /**<15, Reset when the vdd voltage is not stable*/
// 23: RTCWDT_RTC_RESET = 16 /**<16, RTC Watch dog reset digital core and rtc module*/` // 23: RTCWDT_RTC_RESET = 16 /**<16, RTC Watch dog reset digital core and rtc module*/`
#if defined(CONFIG_IDF_TARGET_ESP32C3) #if !defined(CONFIG_IDF_TARGET_ESP32)
# define SW_CPU_RESET RTC_SW_CPU_RESET # define SW_CPU_RESET RTC_SW_CPU_RESET
# define SW_RESET RTC_SW_SYS_RESET # define SW_RESET RTC_SW_SYS_RESET
#endif #endif
...@@ -68,24 +67,32 @@ static int node_bootreason( lua_State *L) ...@@ -68,24 +67,32 @@ static int node_bootreason( lua_State *L)
case SW_RESET: case SW_RESET:
rawinfo = 2; break; rawinfo = 2; break;
case NO_MEAN: case NO_MEAN:
#if !defined(CONFIG_IDF_TARGET_ESP32C3) #if defined(CONFIG_IDF_TARGET_ESP32)
case EXT_CPU_RESET: case EXT_CPU_RESET:
#endif #endif
case DEEPSLEEP_RESET: case DEEPSLEEP_RESET:
#if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32C3)
case SDIO_RESET: case SDIO_RESET:
#endif
#if defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3)
case GLITCH_RTC_RESET:
#endif
#if defined(CONFIG_IDF_TARGET_ESP32S3)
case EFUSE_RESET:
#endif
case TG0WDT_SYS_RESET: case TG0WDT_SYS_RESET:
case TG1WDT_SYS_RESET: case TG1WDT_SYS_RESET:
case INTRUSION_RESET: case INTRUSION_RESET:
case RTCWDT_BROWN_OUT_RESET: case RTCWDT_BROWN_OUT_RESET:
case RTCWDT_RTC_RESET: case RTCWDT_RTC_RESET:
rawinfo = 3; break; rawinfo = 3; break;
#if defined(CONFIG_IDF_TARGET_ESP32C3) #if defined(CONFIG_IDF_TARGET_ESP32)
case OWDT_RESET:
case TGWDT_CPU_RESET:
#else
case TG0WDT_CPU_RESET: case TG0WDT_CPU_RESET:
case TG1WDT_CPU_RESET: case TG1WDT_CPU_RESET:
case SUPER_WDT_RESET: case SUPER_WDT_RESET:
#else
case OWDT_RESET:
case TGWDT_CPU_RESET:
#endif #endif
case RTCWDT_CPU_RESET: case RTCWDT_CPU_RESET:
case RTCWDT_SYS_RESET: case RTCWDT_SYS_RESET:
...@@ -100,7 +107,7 @@ static int node_bootreason( lua_State *L) ...@@ -100,7 +107,7 @@ static int node_bootreason( lua_State *L)
return 2; return 2;
} }
#if !defined(CONFIG_IDF_TARGET_ESP32C3) #if defined(CONFIG_IDF_TARGET_ESP32)
// Lua: node.chipid() // Lua: node.chipid()
static int node_chipid( lua_State *L ) static int node_chipid( lua_State *L )
{ {
...@@ -169,7 +176,7 @@ static void node_sleep_disable_wakeup_sources (lua_State *L) ...@@ -169,7 +176,7 @@ static void node_sleep_disable_wakeup_sources (lua_State *L)
static int node_sleep (lua_State *L) static int node_sleep (lua_State *L)
{ {
lua_settop(L, 1); lua_settop(L, 1);
luaL_checkanytable(L, 1); luaL_checktable(L, 1);
node_sleep_disable_wakeup_sources(L); node_sleep_disable_wakeup_sources(L);
// uart options: uart = num|{num, num, ...} // uart options: uart = num|{num, num, ...}
...@@ -342,24 +349,13 @@ static int node_dsleep (lua_State *L) ...@@ -342,24 +349,13 @@ static int node_dsleep (lua_State *L)
} }
extern lua_Load gLoad;
extern bool user_process_input(bool force);
// Lua: input("string") // Lua: input("string")
static int node_input( lua_State* L ) static int node_input( lua_State* L )
{ {
size_t l = 0; size_t l = 0;
const char *s = luaL_checklstring(L, 1, &l); const char *s = luaL_checklstring(L, 1, &l);
if (s != NULL && l > 0 && l < LUA_MAXINPUT - 1) if (l > 0 && l < LUA_MAXINPUT - 1)
{ lua_input_string(s, l);
lua_Load *load = &gLoad;
if (load->line_position == 0) {
memcpy(load->line, s, l);
load->line[l + 1] = '\0';
load->line_position = strlen(load->line) + 1;
load->done = 1;
user_process_input(true);
}
}
return 0; return 0;
} }
...@@ -383,7 +379,7 @@ ssize_t redir_write(int fd, const void *data, size_t size) { ...@@ -383,7 +379,7 @@ ssize_t redir_write(int fd, const void *data, size_t size) {
lua_State *L = lua_getstate(); lua_State *L = lua_getstate();
lua_rawgeti(L, LUA_REGISTRYINDEX, output_redir); // push function reference lua_rawgeti(L, LUA_REGISTRYINDEX, output_redir); // push function reference
lua_pushlstring(L, (char *)data, size); // push data lua_pushlstring(L, (char *)data, size); // push data
lua_pcall(L, 1, 0, 0); // invoke callback luaL_pcallx(L, 1, 0); // invoke callback
} }
return size; return size;
} }
...@@ -395,7 +391,7 @@ int redir_open(const char *path, int flags, int mode) { ...@@ -395,7 +391,7 @@ int redir_open(const char *path, int flags, int mode) {
// Lua: node.output(func, serial_debug) // Lua: node.output(func, serial_debug)
static int node_output(lua_State *L) { static int node_output(lua_State *L) {
if (lua_type(L, 1) == LUA_TFUNCTION || lua_type(L, 1) == LUA_TLIGHTFUNCTION) { if (lua_isfunction(L, 1)) {
if (output_redir == LUA_NOREF) { if (output_redir == LUA_NOREF) {
// create an instance of a virtual filesystem so we can use fopen // create an instance of a virtual filesystem so we can use fopen
esp_vfs_t redir_fs = { esp_vfs_t redir_fs = {
...@@ -451,7 +447,7 @@ int redir_vprintf(const char *fmt, va_list ap) ...@@ -451,7 +447,7 @@ int redir_vprintf(const char *fmt, va_list ap)
lua_State *L = lua_getstate(); lua_State *L = lua_getstate();
lua_rawgeti(L, LUA_REGISTRYINDEX, os_output_redir); // push function reference lua_rawgeti(L, LUA_REGISTRYINDEX, os_output_redir); // push function reference
lua_pushlstring(L, (char *)data, size); // push data lua_pushlstring(L, (char *)data, size); // push data
lua_pcall(L, 1, 0, 0); // invoke callback luaL_pcallx(L, 1, 0); // invoke callback
} }
return size; return size;
} }
...@@ -459,7 +455,7 @@ int redir_vprintf(const char *fmt, va_list ap) ...@@ -459,7 +455,7 @@ int redir_vprintf(const char *fmt, va_list ap)
// Lua: node.output(func, serial_debug) // Lua: node.output(func, serial_debug)
static int node_osoutput(lua_State *L) { static int node_osoutput(lua_State *L) {
if (lua_type(L, 1) == LUA_TFUNCTION || lua_type(L, 1) == LUA_TLIGHTFUNCTION) { if (lua_isfunction(L, 1)) {
if (os_output_redir == LUA_NOREF) { if (os_output_redir == LUA_NOREF) {
// register our log redirect first time this is invoked // register our log redirect first time this is invoked
oldvprintf = esp_log_set_vprintf(redir_vprintf); oldvprintf = esp_log_set_vprintf(redir_vprintf);
...@@ -477,6 +473,7 @@ static int node_osoutput(lua_State *L) { ...@@ -477,6 +473,7 @@ static int node_osoutput(lua_State *L) {
return 0; return 0;
} }
/* node.stripdebug([level[, function]]).  /* node.stripdebug([level[, function]]). 
* level: 1 don't discard debug * level: 1 don't discard debug
* 2 discard Local and Upvalue debug info * 2 discard Local and Upvalue debug info
...@@ -487,58 +484,38 @@ static int node_osoutput(lua_State *L) { ...@@ -487,58 +484,38 @@ static int node_osoutput(lua_State *L) {
* The function returns an estimated integer count of the bytes stripped. * The function returns an estimated integer count of the bytes stripped.
*/ */
static int node_stripdebug (lua_State *L) { static int node_stripdebug (lua_State *L) {
int level;
if (L->top == L->base) {
lua_pushlightuserdata(L, &luaG_stripdebug );
lua_gettable(L, LUA_REGISTRYINDEX);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_pushinteger(L, CONFIG_LUA_OPTIMIZE_DEBUG);
}
return 1;
}
level = luaL_checkint(L, 1); int n = lua_gettop(L);
if ((level <= 0) || (level > 3)) luaL_argerror(L, 1, "must in range 1-3"); int strip = 0;
if (L->top == L->base + 1) { lua_settop(L, 2);
/* Store the default level in the registry if no function parameter */ if (!lua_isnil(L, 1)) {
lua_pushlightuserdata(L, &luaG_stripdebug); strip = lua_tointeger(L, 1);
lua_pushinteger(L, level); luaL_argcheck(L, strip > 0 && strip < 4, 1, "Invalid strip level");
lua_settable(L, LUA_REGISTRYINDEX);
lua_settop(L,0);
return 0;
} }
if (level == 1) { if (lua_isnumber(L, 2)) {
lua_settop(L,0); /* Use debug interface to replace stack level by corresponding function */
lua_pushinteger(L, 0); int scope = luaL_checkinteger(L, 2);
return 1;
}
if (!lua_isfunction(L, 2)) {
int scope = luaL_checkint(L, 2);
if (scope > 0) { if (scope > 0) {
/* if the function parameter is a +ve integer then climb to find function */
lua_Debug ar; lua_Debug ar;
lua_pop(L, 1); /* pop level as getinfo will replace it by the function */ lua_pop(L, 1);
if (lua_getstack(L, scope, &ar)) { if (lua_getstack(L, scope, &ar)) {
lua_getinfo(L, "f", &ar); lua_getinfo(L, "f", &ar); /* put function at [2] (ToS) */
} }
} }
} }
if(!lua_isfunction(L, 2) || lua_iscfunction(L, -1)) luaL_argerror(L, 2, "must be a Lua Function"); int isfunc = lua_isfunction(L, 2);
// lua_lock(L); luaL_argcheck(L, n < 2 || isfunc, 2, "not a valid function");
Proto *f = clvalue(L->base + 1)->l.p;
// lua_unlock(L); /* return result of lua_stripdebug, adding 1 if this is get/set level) */
lua_settop(L,0); lua_pushinteger(L, lua_stripdebug(L, strip - 1) + (isfunc ? 0 : 1));
lua_pushinteger(L, luaG_stripdebug(L, f, level, 1));
return 1; return 1;
} }
#if defined(CONFIG_LUA_VERSION_51)
// Lua: node.egc.setmode( mode, [param]) // Lua: node.egc.setmode( mode, [param])
// where the mode is one of the node.egc constants NOT_ACTIVE , ON_ALLOC_FAILURE, // where the mode is one of the node.egc constants NOT_ACTIVE , ON_ALLOC_FAILURE,
// ON_MEM_LIMIT, ALWAYS. In the case of ON_MEM_LIMIT an integer parameter is reqired // ON_MEM_LIMIT, ALWAYS. In the case of ON_MEM_LIMIT an integer parameter is reqired
...@@ -550,9 +527,10 @@ static int node_egc_setmode(lua_State* L) { ...@@ -550,9 +527,10 @@ static int node_egc_setmode(lua_State* L) {
luaL_argcheck(L, mode <= (EGC_ON_ALLOC_FAILURE | EGC_ON_MEM_LIMIT | EGC_ALWAYS), 1, "invalid mode"); luaL_argcheck(L, mode <= (EGC_ON_ALLOC_FAILURE | EGC_ON_MEM_LIMIT | EGC_ALWAYS), 1, "invalid mode");
luaL_argcheck(L, !(mode & EGC_ON_MEM_LIMIT) || limit>0, 1, "limit must be non-zero"); luaL_argcheck(L, !(mode & EGC_ON_MEM_LIMIT) || limit>0, 1, "limit must be non-zero");
legc_set_mode( L, mode, limit ); lua_setegcmode( L, mode, limit );
return 0; return 0;
} }
#endif
static int writer(lua_State* L, const void* p, size_t size, void* u) static int writer(lua_State* L, const void* p, size_t size, void* u)
...@@ -593,7 +571,7 @@ static int node_compile( lua_State* L ) ...@@ -593,7 +571,7 @@ static int node_compile( lua_State* L )
output[strlen(output) - 1] = '\0'; output[strlen(output) - 1] = '\0';
NODE_DBG(output); NODE_DBG(output);
NODE_DBG("\n"); NODE_DBG("\n");
if (luaL_loadfsfile(L, fname) != 0) { if (luaL_loadfile(L, fname) != 0) {
luaM_free( L, output ); luaM_free( L, output );
return luaL_error(L, lua_tostring(L, -1)); return luaL_error(L, lua_tostring(L, -1));
} }
...@@ -643,7 +621,7 @@ static void do_node_task (task_param_t task_fn_ref, task_prio_t prio) ...@@ -643,7 +621,7 @@ static void do_node_task (task_param_t task_fn_ref, task_prio_t prio)
lua_rawgeti(L, LUA_REGISTRYINDEX, (int)task_fn_ref); lua_rawgeti(L, LUA_REGISTRYINDEX, (int)task_fn_ref);
luaL_unref(L, LUA_REGISTRYINDEX, (int)task_fn_ref); luaL_unref(L, LUA_REGISTRYINDEX, (int)task_fn_ref);
lua_pushinteger(L, prio); lua_pushinteger(L, prio);
lua_call(L, 1, 0); luaL_pcallx(L, 1, 0);
} }
// Lua: node.task.post([priority],task_cb) -- schedule a task for execution next // Lua: node.task.post([priority],task_cb) -- schedule a task for execution next
...@@ -656,7 +634,7 @@ static int node_task_post( lua_State* L ) ...@@ -656,7 +634,7 @@ static int node_task_post( lua_State* L )
luaL_argcheck(L, priority <= TASK_PRIORITY_HIGH, 1, "invalid priority"); luaL_argcheck(L, priority <= TASK_PRIORITY_HIGH, 1, "invalid priority");
Ltype = lua_type(L, ++n); Ltype = lua_type(L, ++n);
} }
luaL_argcheck(L, Ltype == LUA_TFUNCTION || Ltype == LUA_TLIGHTFUNCTION, n, "invalid function"); luaL_argcheck(L, Ltype == LUA_TFUNCTION, n, "invalid function");
lua_pushvalue(L, n); lua_pushvalue(L, n);
int task_fn_ref = luaL_ref(L, LUA_REGISTRYINDEX); int task_fn_ref = luaL_ref(L, LUA_REGISTRYINDEX);
...@@ -699,16 +677,102 @@ static int node_uptime(lua_State *L) ...@@ -699,16 +677,102 @@ static int node_uptime(lua_State *L)
} }
LROT_BEGIN(node_egc) // Lua: n = node.LFS.reload(lfsimage)
static int node_lfsreload (lua_State *L) {
lua_settop(L, 1);
luaL_lfsreload(L);
return 1;
}
// Lua: n = node.flashreload(lfsimage)
static int node_lfsreload_deprecated (lua_State *L) {
platform_print_deprecation_note("node.flashreload", "soon. Use node.LFS interface instead");
return node_lfsreload (L);
}
// Lua: n = node.flashindex(module)
// Lua: n = node.LFS.get(module)
static int node_lfsindex (lua_State *L) {
lua_settop(L, 1);
luaL_pushlfsmodule(L);
return 1;
}
// Lua: n = node.LFS.list([option])
// Note that option is ignored in this release
static int node_lfslist (lua_State *L) {
lua_settop(L, 1);
luaL_pushlfsmodules(L);
if (lua_istable(L, -1) && lua_getglobal(L, "table") == LUA_TTABLE) {
lua_getfield(L, -1, "sort");
lua_remove(L, -2); /* remove table table */
lua_pushvalue(L, -2); /* dup array of modules ref to ToS */
lua_call(L, 1, 0);
}
return 1;
}
//== node.LFS Table emulator ==============================================//
static void add_int_field( lua_State* L, lua_Integer i, const char *name){
lua_pushinteger(L, i);
lua_setfield(L, -2, name);
}
static void get_lfs_config ( lua_State* L ){
int config[5];
lua_getlfsconfig(L, config);
lua_createtable(L, 0, 4);
add_int_field(L, config[0], "lfs_mapped");
add_int_field(L, config[1], "lfs_base");
add_int_field(L, config[2], "lfs_size");
add_int_field(L, config[3], "lfs_used");
}
static int node_lfs_func (lua_State* L) { /*T[1] = LFS, T[2] = fieldname */
lua_remove(L, 1);
lua_settop(L, 1);
const char *name = lua_tostring(L, 1);
if (!name) {
lua_pushnil(L);
} else if (!strcmp(name, "config")) {
get_lfs_config(L);
} else if (!strcmp(name, "time")) {
luaL_pushlfsdts(L);
} else {
luaL_pushlfsmodule(L);
}
return 1;
}
LROT_BEGIN(node_lfs_meta, NULL, LROT_MASK_INDEX)
LROT_FUNCENTRY( __index, node_lfs_func)
LROT_END(node_lfs_meta, NULL, LROT_MASK_INDEX)
LROT_BEGIN(node_lfs, LROT_TABLEREF(node_lfs_meta), 0)
LROT_FUNCENTRY( list, node_lfslist)
LROT_FUNCENTRY( get, node_lfsindex)
LROT_FUNCENTRY( reload, node_lfsreload )
LROT_END(node_lfs, LROT_TABLEREF(node_lfs_meta), 0)
#if defined(CONFIG_LUA_VERSION_51)
LROT_BEGIN(node_egc, NULL, 0)
LROT_FUNCENTRY( setmode, node_egc_setmode ) LROT_FUNCENTRY( setmode, node_egc_setmode )
LROT_NUMENTRY ( NOT_ACTIVE, EGC_NOT_ACTIVE ) LROT_NUMENTRY ( NOT_ACTIVE, EGC_NOT_ACTIVE )
LROT_NUMENTRY ( ON_ALLOC_FAILURE, EGC_ON_ALLOC_FAILURE ) LROT_NUMENTRY ( ON_ALLOC_FAILURE, EGC_ON_ALLOC_FAILURE )
LROT_NUMENTRY ( ON_MEM_LIMIT, EGC_ON_MEM_LIMIT ) LROT_NUMENTRY ( ON_MEM_LIMIT, EGC_ON_MEM_LIMIT )
LROT_NUMENTRY ( ALWAYS, EGC_ALWAYS ) LROT_NUMENTRY ( ALWAYS, EGC_ALWAYS )
LROT_END(node_egc, NULL, 0) LROT_END(node_egc, NULL, 0)
#endif
LROT_BEGIN(node_task) LROT_BEGIN(node_task, NULL, 0)
LROT_FUNCENTRY( post, node_task_post ) LROT_FUNCENTRY( post, node_task_post )
LROT_NUMENTRY ( LOW_PRIORITY, TASK_PRIORITY_LOW ) LROT_NUMENTRY ( LOW_PRIORITY, TASK_PRIORITY_LOW )
LROT_NUMENTRY ( MEDIUM_PRIORITY, TASK_PRIORITY_MEDIUM ) LROT_NUMENTRY ( MEDIUM_PRIORITY, TASK_PRIORITY_MEDIUM )
...@@ -717,7 +781,7 @@ LROT_END(node_task, NULL, 0) ...@@ -717,7 +781,7 @@ LROT_END(node_task, NULL, 0)
// Wakup reasons // Wakup reasons
LROT_BEGIN(node_wakeup) LROT_BEGIN(node_wakeup, NULL, 0)
LROT_NUMENTRY ( GPIO, ESP_SLEEP_WAKEUP_GPIO ) LROT_NUMENTRY ( GPIO, ESP_SLEEP_WAKEUP_GPIO )
LROT_NUMENTRY ( TIMER, ESP_SLEEP_WAKEUP_TIMER ) LROT_NUMENTRY ( TIMER, ESP_SLEEP_WAKEUP_TIMER )
LROT_NUMENTRY ( TOUCHPAD, ESP_SLEEP_WAKEUP_TOUCHPAD ) LROT_NUMENTRY ( TOUCHPAD, ESP_SLEEP_WAKEUP_TOUCHPAD )
...@@ -725,16 +789,19 @@ LROT_BEGIN(node_wakeup) ...@@ -725,16 +789,19 @@ LROT_BEGIN(node_wakeup)
LROT_NUMENTRY ( ULP, ESP_SLEEP_WAKEUP_ULP ) LROT_NUMENTRY ( ULP, ESP_SLEEP_WAKEUP_ULP )
LROT_END(node_wakeup, NULL, 0) LROT_END(node_wakeup, NULL, 0)
LROT_BEGIN(node) LROT_BEGIN(node, NULL, 0)
LROT_FUNCENTRY( bootreason, node_bootreason ) LROT_FUNCENTRY( bootreason, node_bootreason )
#if !defined(CONFIG_IDF_TARGET_ESP32C3) #if defined(CONFIG_IDF_TARGET_ESP32)
LROT_FUNCENTRY( chipid, node_chipid ) LROT_FUNCENTRY( chipid, node_chipid )
#endif #endif
LROT_FUNCENTRY( compile, node_compile ) LROT_FUNCENTRY( compile, node_compile )
LROT_FUNCENTRY( dsleep, node_dsleep ) LROT_FUNCENTRY( dsleep, node_dsleep )
#if defined(CONFIG_LUA_VERSION_51)
LROT_TABENTRY ( egc, node_egc ) LROT_TABENTRY ( egc, node_egc )
LROT_FUNCENTRY( flashreload,luaN_reload_reboot ) #endif
LROT_FUNCENTRY( flashindex, luaN_index ) LROT_FUNCENTRY( flashreload,node_lfsreload_deprecated )
LROT_FUNCENTRY( flashindex, node_lfsindex )
LROT_TABENTRY( LFS, node_lfs )
LROT_FUNCENTRY( heap, node_heap ) LROT_FUNCENTRY( heap, node_heap )
LROT_FUNCENTRY( input, node_input ) LROT_FUNCENTRY( input, node_input )
LROT_FUNCENTRY( output, node_output ) LROT_FUNCENTRY( output, node_output )
......
...@@ -239,13 +239,13 @@ next: ...@@ -239,13 +239,13 @@ next:
} }
LROT_BEGIN(otaupgrade) LROT_BEGIN(otaupgrade, NULL, 0)
LROT_FUNCENTRY( commence, lotaupgrade_commence ) LROT_FUNCENTRY( commence, lotaupgrade_commence )
LROT_FUNCENTRY( write, lotaupgrade_write ) LROT_FUNCENTRY( write, lotaupgrade_write )
LROT_FUNCENTRY( complete, lotaupgrade_complete ) LROT_FUNCENTRY( complete, lotaupgrade_complete )
LROT_FUNCENTRY( accept, lotaupgrade_accept ) LROT_FUNCENTRY( accept, lotaupgrade_accept )
LROT_FUNCENTRY( rollback, lotaupgrade_rollback ) LROT_FUNCENTRY( rollback, lotaupgrade_rollback )
LROT_FUNCENTRY( info, lotaupgrade_info ) LROT_FUNCENTRY( info, lotaupgrade_info )
LROT_END(otaupgrade, 0, NULL) LROT_END(otaupgrade, NULL, 0)
NODEMCU_MODULE(OTAUPGRADE, "otaupgrade", otaupgrade, NULL); NODEMCU_MODULE(OTAUPGRADE, "otaupgrade", otaupgrade, NULL);
...@@ -326,7 +326,7 @@ static int ow_crc16( lua_State *L ) ...@@ -326,7 +326,7 @@ static int ow_crc16( lua_State *L )
return 1; return 1;
} }
LROT_BEGIN(ow) LROT_BEGIN(ow, NULL, 0)
LROT_FUNCENTRY( setup, ow_setup ) LROT_FUNCENTRY( setup, ow_setup )
LROT_FUNCENTRY( reset, ow_reset ) LROT_FUNCENTRY( reset, ow_reset )
LROT_FUNCENTRY( skip, ow_skip ) LROT_FUNCENTRY( skip, ow_skip )
......
/*
** The pipe algo is somewhat similar to the luaL_Buffer algo, except that it
** uses table to store the LUAL_BUFFERSIZE byte array chunks instead of the
** stack. Writing is always to the last UD in the table and overflow pushes a
** new UD to the end of the table. Reading is always from the first UD in the
** table and underrun removes the first UD to shift a new one into slot 2. (Slot
** 1 of the table is reserved for the pipe reader function with 0 denoting no
** reader.)
**
** Reads and writes may span multiple UD buffers and if the read spans multiple
** UDs then the parts are collected as strings on the Lua stack and then
** concatenated with a lua_concat().
**
** Note that pipe tables also support the undocumented length and tostring
** operators for debugging puposes, so if p is a pipe then #p[i] gives the
** effective length of pipe slot i and printing p[i] gives its contents.
**
** The pipe library also supports the automatic scheduling of a reader task.
** This is declared by including a Lua CB function and an optional prioirty for
** it to execute at in the pipe.create() call. The reader task may or may not
** empty the FIFO (and there is also nothing to stop the task also writing to
** the FIFO. The reader automatically reschedules itself if the pipe contains
** unread content.
**
** The reader tasks may be interleaved with other tasks that write to the pipe
** and others that don't. Any task writing to the pipe will also trigger the
** posting of a read task if one is not already pending. In this way at most
** only one pending reader task is pending, and this prevents overrun of the
** task queueing system.
**
** Implementation Notes:
**
** - The Pipe slot 1 is used to store the Lua CB function reference of the
** reader task. Note that is actually an auxiliary wrapper around the
** supplied Lua CB function, and this wrapper also uses upvals to store
** internal pipe state. The remaining slots are the Userdata buffer chunks.
**
** - This internal state needs to be shared with the pipe_write function, but a
** limitation of Lua 5.1 is that C functions cannot share upvals; to avoid
** this constraint, this function is also denormalised to act as the
** pipe_write function: if Arg1 is the pipe then its a pipe:write() otherwise
** its a CB wrapper.
**
** Also note that the pipe module is used by the Lua VM and therefore the create
** read, and unread methods are exposed as directly callable C functions. (Write
** is available through pipe[1].)
**
** Read the docs/modules/pipe.md documentation for a functional description.
*/
#include "module.h"
#include "lauxlib.h"
#include <string.h>
#include "platform.h"
#include "task/task.h"
#define INVALID_LEN ((unsigned)-1)
typedef struct buffer {
int start, end;
char buf[LUAL_BUFFERSIZE];
} buffer_t;
#define AT_TAIL 0x00
#define AT_HEAD 0x01
#define WRITING 0x02
static buffer_t *checkPipeUD (lua_State *L, int ndx);
static buffer_t *newPipeUD(lua_State *L, int ndx, int n);
static int pipe_write_aux(lua_State *L);
LROT_TABLE(pipe_meta);
/* Validation and utility functions */
// [-0, +0, v]
static buffer_t *checkPipeTable (lua_State *L, int tbl, int flags) {
int m = lua_gettop(L), n = lua_objlen(L, tbl);
if (lua_istable(L, tbl) && lua_getmetatable(L, tbl)) {
lua_pushrotable(L, LROT_TABLEREF(pipe_meta));/* push comparison metatable */
if (lua_rawequal(L, -1, -2)) { /* check these match */
buffer_t *ud;
if (n == 1) {
ud = (flags & WRITING) ? newPipeUD(L, tbl, 2) : NULL;
} else {
int i = flags & AT_HEAD ? 2 : n; /* point to head or tail of T */
lua_rawgeti(L, tbl, i); /* and fetch UD */
ud = checkPipeUD(L, -1);
}
lua_settop(L, m);
return ud; /* and return ptr to buffer_t rec */
}
}
luaL_argerror(L, tbl, "pipe table");
return NULL; /* NORETURN avoid compiler error */
}
static buffer_t *checkPipeUD (lua_State *L, int ndx) { // [-0, +0, v]
luaL_checktype(L, ndx, LUA_TUSERDATA); /* NORETURN on error */
buffer_t *ud = lua_touserdata(L, ndx);
if (ud && lua_getmetatable(L, ndx)) { /* Get UD and its metatable? */
lua_pushrotable(L, LROT_TABLEREF(pipe_meta)); /* push correct metatable */
if (lua_rawequal(L, -1, -2)) { /* Do these match? */
lua_pop(L, 2); /* remove both metatables */
return ud; /* and return ptr to buffer_t rec */
}
}
return NULL;
}
/* Create new buffer chunk at `n` in the table which is at stack `ndx` */
static buffer_t *newPipeUD(lua_State *L, int ndx, int n) { // [-0,+0,-]
buffer_t *ud = (buffer_t *) lua_newuserdata(L, sizeof(buffer_t));
lua_pushrotable(L, LROT_TABLEREF(pipe_meta)); /* push the metatable */
lua_setmetatable(L, -2); /* set UD's metabtable to pipe_meta */
ud->start = ud->end = 0;
lua_rawseti(L, ndx, n); /* T[n] = new UD */
return ud; /* ud points to new T[n] */
}
#define CHAR_DELIM -1
#define CHAR_DELIM_KEEP -2
static char getsize_delim (lua_State *L, int ndx, int *len) { // [-0, +0, v]
char delim;
int n;
if( lua_type( L, ndx ) == LUA_TNUMBER ) {
n = luaL_checkinteger( L, ndx );
luaL_argcheck(L, n > 0, ndx, "invalid chunk size");
delim = '\0';
} else if (lua_isnil(L, ndx)) {
n = CHAR_DELIM;
delim = '\n';
} else {
size_t ldelim;
const char *s = lua_tolstring( L, 2, &ldelim);
n = ldelim == 2 && s[1] == '+' ? CHAR_DELIM_KEEP : CHAR_DELIM ;
luaL_argcheck(L, ldelim + n == 0, ndx, "invalid delimiter");
delim = s[0];
}
if (len)
*len = n;
return delim;
}
/*
** Read CB Initiator AND pipe_write. If arg1 == the pipe, then this is a pipe
** write(); otherwise it is the Lua CB wapper for the task post. This botch
** allows these two functions to share Upvals within the Lua 5.1 VM:
*/
#define UVpipe lua_upvalueindex(1) // The pipe table object
#define UVfunc lua_upvalueindex(2) // The CB's Lua function
#define UVprio lua_upvalueindex(3) // The task priority
#define UVstate lua_upvalueindex(4) // Pipe state;
#define CB_NOT_USED 0
#define CB_ACTIVE 1
#define CB_WRITE_UPDATED 2
#define CB_QUIESCENT 4
/*
** Note that nothing precludes the Lua CB function from itself writing to the
** pipe and in this case this routine will call itself recursively.
**
** The Lua CB itself takes the pipe object as a parameter and returns an
** optional boolean to force or to suppress automatic retasking if needed. If
** omitted, then the default is to repost if the pipe is not empty, otherwise
** the task chain is left to lapse.
*/
static int pipe_write_and_read_poster (lua_State *L) {
int state = lua_tointeger(L, UVstate);
if (lua_rawequal(L, 1, UVpipe)) {
/* arg1 == the pipe, so this was invoked as a pipe_write() */
if (pipe_write_aux(L) && state && !(state & CB_WRITE_UPDATED)) {
/*
* if this resulted in a write and not already in a CB and not already
* toggled the write update then post the task
*/
state |= CB_WRITE_UPDATED;
lua_pushinteger(L, state);
lua_replace(L, UVstate); /* Set CB state write updated flag */
if (state == (CB_QUIESCENT | CB_WRITE_UPDATED)) {
lua_rawgeti(L, 1, 1); /* Get CB ref from pipe[1] */
luaL_posttask(L, (int) lua_tointeger(L, UVprio)); /* and repost task */
}
}
} else if (state != CB_NOT_USED) {
/* invoked by the luaN_taskpost() so call the Lua CB */
int repost; /* can take the values CB_WRITE_UPDATED or 0 */
lua_pushinteger(L, CB_ACTIVE); /* CB state set to active only */
lua_replace(L, UVstate);
lua_pushvalue(L, UVfunc); /* Lua CB function */
lua_pushvalue(L, UVpipe); /* pipe table */
lua_call(L, 1, 1); /* Errors are thrown back to caller */
/*
* On return from the Lua CB, the task is never reposted if the pipe is empty.
* If it is not empty then the Lua CB return status determines when reposting
* occurs:
* - true = repost
* - false = don't repost
* - nil = only repost if there has been a write update.
*/
if (lua_isboolean(L,-1)) {
repost = (lua_toboolean(L, -1) == true &&
lua_objlen(L, UVpipe) > 1) ? CB_WRITE_UPDATED : 0;
} else {
repost = state & CB_WRITE_UPDATED;
}
state = CB_QUIESCENT | repost;
lua_pushinteger(L, state); /* Update the CB state */
lua_replace(L, UVstate);
if (repost) {
lua_rawgeti(L, UVpipe, 1); /* Get CB ref from pipe[1] */
luaL_posttask(L, (int) lua_tointeger(L, UVprio)); /* and repost task */
}
}
return 0;
}
/* Lua callable methods. Since the metatable is linked to both the pipe table */
/* and the userdata entries the __len & __tostring functions must handle both */
// Lua: buf = pipe.create()
int pipe_create(lua_State *L) {
int prio = -1;
lua_settop(L, 2); /* fix stack sze as 2 */
if (!lua_isnil(L, 1)) {
luaL_checktype(L, 1, LUA_TFUNCTION); /* non-nil arg1 must be a function */
if (lua_isnil(L, 2)) {
prio = TASK_PRIORITY_MEDIUM;
} else {
prio = (int) lua_tointeger(L, 2);
luaL_argcheck(L, prio >= TASK_PRIORITY_LOW &&
prio <= TASK_PRIORITY_HIGH, 2,
"invalid priority");
}
}
lua_createtable (L, 1, 0); /* create pipe table */
lua_pushrotable(L, LROT_TABLEREF(pipe_meta));
lua_setmetatable(L, -2); /* set pipe table's metabtable to pipe_meta */
lua_pushvalue(L, -1); /* UV1: pipe object */
lua_pushvalue(L, 1); /* UV2: CB function */
lua_pushinteger(L, prio); /* UV3: task priority */
lua_pushinteger(L, prio == -1 ? CB_NOT_USED : CB_QUIESCENT);
lua_pushcclosure(L, pipe_write_and_read_poster, 4); /* aux func for C task */
lua_rawseti(L, -2, 1); /* and write to T[1] */
return 1; /* return the table */
}
// len = #pipeobj[i]
static int pipe__len (lua_State *L) {
if (lua_istable(L, 1)) {
lua_pushinteger(L, lua_objlen(L, 1));
} else {
buffer_t *ud = checkPipeUD(L, 1);
lua_pushinteger(L, ud->end - ud->start);
}
return 1;
}
//Lua s = pipeUD:tostring()
static int pipe__tostring (lua_State *L) {
if (lua_istable(L, 1)) {
lua_pushfstring(L, "Pipe: %p", lua_topointer(L, 1));
} else {
buffer_t *ud = checkPipeUD(L, 1);
lua_pushlstring(L, ud->buf + ud->start, ud->end - ud->start);
}
return 1;
}
// Lua: rec = p:read(end_or_delim) // also [-2, +1,- ]
int pipe_read(lua_State *L) {
buffer_t *ud = checkPipeTable(L, 1, AT_HEAD);
int i, k=0, n;
lua_settop(L,2);
const char delim = getsize_delim(L, 2, &n);
lua_pop(L,1);
while (ud && n) {
int want, used, avail = ud->end - ud->start;
if (n < 0 /* one of the CHAR_DELIM flags */) {
/* getting a delimited chunk so scan for delimiter */
for (i = ud->start; i < ud->end && ud->buf[i] != delim; i++) {}
/* Can't have i = ud->end and ud->buf[i] == delim so either */
/* we've scanned full buffer avail OR we've hit a delim char */
if (i == ud->end) {
want = used = avail; /* case where we've scanned without a hit */
} else {
want = used = i + 1 - ud->start; /* case where we've hit a delim */
if (n == CHAR_DELIM)
want--;
n = 0; /* force loop exit because delim found */
}
} else {
want = used = (n < avail) ? n : avail;
n -= used;
}
lua_pushlstring(L, ud->buf + ud->start, want); /* part we want */
k++;
ud->start += used;
if (ud->start == ud->end) {
/* shift the pipe array down overwriting T[1] */
int nUD = lua_objlen(L, 1);
for (i = 2; i < nUD; i++) { /* for i = 2, nUD-1 */
lua_rawgeti(L, 1, i+1); lua_rawseti(L, 1, i); /* T[i] = T[i+1] */
}
lua_pushnil(L); lua_rawseti(L, 1, nUD--); /* T[n] = nil */
if (nUD>1) {
lua_rawgeti(L, 1, 2);
ud = checkPipeUD(L, -1);
lua_pop(L, 1);
} else {
ud = NULL;
}
}
}
if (k)
lua_concat(L, k);
else
lua_pushnil(L);
return 1;
}
// Lua: buf:unread(some_string)
int pipe_unread(lua_State *L) {
size_t l = INVALID_LEN;
const char *s = lua_tolstring(L, 2, &l);
if (l==0)
return 0;
luaL_argcheck(L, l != INVALID_LEN, 2, "must be a string");
buffer_t *ud = checkPipeTable(L, 1, AT_HEAD | WRITING);
do {
int used = ud->end - ud->start;
int lrem = LUAL_BUFFERSIZE-used;
if (used == LUAL_BUFFERSIZE) {
/* If the current UD is full insert a new UD at T[2] */
int i, nUD = lua_objlen(L, 1);
for (i = nUD; i > 1; i--) { /* for i = nUD,1,-1 */
lua_rawgeti(L, 1, i); lua_rawseti(L, 1, i+1); /* T[i+1] = T[i] */
}
ud = newPipeUD(L, 1, 2);
used = 0; lrem = LUAL_BUFFERSIZE;
/* Filling leftwards; make this chunk "empty but at the right end" */
ud->start = ud->end = LUAL_BUFFERSIZE;
} else if (ud->start < l) {
/* If the unread can't fit it before the start, shift content to end */
memmove(ud->buf + lrem,
ud->buf + ud->start, used); /* must be memmove not cpy */
ud->start = lrem; ud->end = LUAL_BUFFERSIZE;
}
if (l <= (unsigned )lrem)
break;
/* If we're here then the remaining string is strictly longer than the */
/* remaining buffer space; top off the buffer before looping around again */
l -= lrem;
memcpy(ud->buf, s + l, lrem);
ud->start = 0;
} while(1);
/* Copy any residual tail to the UD buffer.
* Here, ud != NULL and 0 <= l <= ud->start */
ud->start -= l;
memcpy(ud->buf + ud->start, s, l);
return 0;
}
// Lua: buf:write(some_string)
static int pipe_write_aux(lua_State *L) {
size_t l = INVALID_LEN;
const char *s = lua_tolstring(L, 2, &l);
//dbg_printf("pipe write(%u): %s", l, s);
if (l==0)
return false;
luaL_argcheck(L, l != INVALID_LEN, 2, "must be a string");
buffer_t *ud = checkPipeTable(L, 1, AT_TAIL | WRITING);
do {
int used = ud->end - ud->start;
if (used == LUAL_BUFFERSIZE) {
/* If the current UD is full insert a new UD at T[end] */
ud = newPipeUD(L, 1, lua_objlen(L, 1)+1);
used = 0;
} else if (LUAL_BUFFERSIZE - ud->end < l) {
/* If the write can't fit it at the end then shift content to the start */
memmove(ud->buf, ud->buf + ud->start, used); /* must be memmove not cpy */
ud->start = 0; ud->end = used;
}
int lrem = LUAL_BUFFERSIZE-used;
if (l <= (unsigned )lrem)
break;
/* If we've got here then the remaining string is longer than the buffer */
/* space left, so top off the buffer before looping around again */
memcpy(ud->buf + ud->end, s, lrem);
ud->end += lrem;
l -= lrem;
s += lrem;
} while(1);
/* Copy any residual tail to the UD buffer. Note that this is l>0 and */
memcpy(ud->buf + ud->end, s, l);
ud->end += l;
return true;
}
// Lua: fread = pobj:reader(1400) -- or other number
// fread = pobj:reader('\n') -- or other delimiter (delim is stripped)
// fread = pobj:reader('\n+') -- or other delimiter (delim is retained)
#define TBL lua_upvalueindex(1)
#define N lua_upvalueindex(2)
static int pipe_read_aux(lua_State *L) {
lua_settop(L, 0); /* ignore args since we use upvals instead */
lua_pushvalue(L, TBL);
lua_pushvalue(L, N);
return pipe_read(L);
}
static int pipe_reader(lua_State *L) {
lua_settop(L,2);
checkPipeTable (L, 1,AT_HEAD);
getsize_delim(L, 2, NULL);
lua_pushcclosure(L, pipe_read_aux, 2); /* return (closure, nil, table) */
return 1;
}
// return number of records
static int pipe_nrec (lua_State *L) {
lua_pushinteger(L, lua_objlen(L, 1) - 1);
return 1;
}
LROT_BEGIN(pipe_funcs, NULL, 0)
LROT_FUNCENTRY( __len, pipe__len )
LROT_FUNCENTRY( __tostring, pipe__tostring )
LROT_FUNCENTRY( read, pipe_read )
LROT_FUNCENTRY( reader, pipe_reader )
LROT_FUNCENTRY( unread, pipe_unread )
LROT_FUNCENTRY( nrec, pipe_nrec )
LROT_END(pipe_funcs, NULL, 0)
/* Using a index func is needed because the write method is at pipe[1] */
static int pipe__index(lua_State *L) {
lua_settop(L,2);
const char *k=lua_tostring(L,2);
if(!strcmp(k,"write")){
lua_rawgeti(L, 1, 1);
} else {
lua_pushrotable(L, LROT_TABLEREF(pipe_funcs));
lua_replace(L, 1);
lua_rawget(L, 1);
}
return 1;
}
LROT_BEGIN(pipe_meta, NULL, LROT_MASK_INDEX)
LROT_FUNCENTRY( __index, pipe__index)
LROT_FUNCENTRY( __len, pipe__len )
LROT_FUNCENTRY( __tostring, pipe__tostring )
LROT_END(pipe_meta, NULL, LROT_MASK_INDEX)
LROT_BEGIN(pipe, NULL, 0)
LROT_FUNCENTRY( create, pipe_create )
LROT_END(pipe, NULL, 0)
NODEMCU_MODULE(PIPE, "pipe", pipe, NULL);
...@@ -214,7 +214,7 @@ static void pulsecnt_task(task_param_t param, task_prio_t prio) ...@@ -214,7 +214,7 @@ static void pulsecnt_task(task_param_t param, task_prio_t prio)
lua_pushboolean (L, zero); lua_pushboolean (L, zero);
// lua_pushinteger (L, moving_to); // lua_pushinteger (L, moving_to);
// lua_pushinteger (L, status); // lua_pushinteger (L, status);
lua_call (L, 6, 0); luaL_pcallx (L, 6, 0);
} else { } else {
if (pc->is_debug) ESP_LOGI("pulsecnt", "Could not find cb for unit %d with ptr %d", unit, pc->cb_ref); if (pc->is_debug) ESP_LOGI("pulsecnt", "Could not find cb for unit %d with ptr %d", unit, pc->cb_ref);
} }
...@@ -539,12 +539,12 @@ static int pulsecnt_channel_config( lua_State *L, uint8_t channel ) { ...@@ -539,12 +539,12 @@ static int pulsecnt_channel_config( lua_State *L, uint8_t channel ) {
// Lua: pc:chan0Config(pulse_gpio_num, ctrl_gpio_num, pos_mode, neg_mode, lctrl_mode, hctrl_mode, counter_l_lim, counter_h_lim) // Lua: pc:chan0Config(pulse_gpio_num, ctrl_gpio_num, pos_mode, neg_mode, lctrl_mode, hctrl_mode, counter_l_lim, counter_h_lim)
// Example: pc:chan0Config(2, 4, pulsecnt.PCNT_COUNT_INC, pulsecnt.PCNT_COUNT_DIS, pulsecnt.PCNT_MODE_REVERSE, pulsecnt.PCNT_MODE_KEEP, -100, 100) // Example: pc:chan0Config(2, 4, pulsecnt.PCNT_COUNT_INC, pulsecnt.PCNT_COUNT_DIS, pulsecnt.PCNT_MODE_REVERSE, pulsecnt.PCNT_MODE_KEEP, -100, 100)
static int pulsecnt_channel0_config( lua_State *L, uint8_t channel ) { static int pulsecnt_channel0_config( lua_State *L ) {
return pulsecnt_channel_config(L, 0); return pulsecnt_channel_config(L, 0);
} }
// Lua: pc:chan1Config(pulse_gpio_num, ctrl_gpio_num, pos_mode, neg_mode, lctrl_mode, hctrl_mode, counter_l_lim, counter_h_lim) // Lua: pc:chan1Config(pulse_gpio_num, ctrl_gpio_num, pos_mode, neg_mode, lctrl_mode, hctrl_mode, counter_l_lim, counter_h_lim)
// Example: pc:chan1Config(2, 4, pulsecnt.PCNT_COUNT_INC, pulsecnt.PCNT_COUNT_DIS, pulsecnt.PCNT_MODE_REVERSE, pulsecnt.PCNT_MODE_KEEP, -100, 100) // Example: pc:chan1Config(2, 4, pulsecnt.PCNT_COUNT_INC, pulsecnt.PCNT_COUNT_DIS, pulsecnt.PCNT_MODE_REVERSE, pulsecnt.PCNT_MODE_KEEP, -100, 100)
static int pulsecnt_channel1_config( lua_State *L, uint8_t channel ) { static int pulsecnt_channel1_config( lua_State *L ) {
return pulsecnt_channel_config(L, 1); return pulsecnt_channel_config(L, 1);
} }
...@@ -565,7 +565,7 @@ static int pulsecnt_create( lua_State *L ) { ...@@ -565,7 +565,7 @@ static int pulsecnt_create( lua_State *L ) {
// user did not provide a callback. that's ok. just don't give them one. // user did not provide a callback. that's ok. just don't give them one.
isCallback = false; isCallback = false;
} else { } else {
luaL_argcheck(L, lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION, stack, "Must be function"); luaL_checkfunction(L, stack);
} }
// ok, we have our unit number which is required. good. now create our object // ok, we have our unit number which is required. good. now create our object
...@@ -678,7 +678,10 @@ static int pulsecnt_unregister(lua_State* L){ ...@@ -678,7 +678,10 @@ static int pulsecnt_unregister(lua_State* L){
return 0; return 0;
} }
LROT_BEGIN(pulsecnt_dyn) LROT_BEGIN(pulsecnt_dyn, NULL, 0)
LROT_FUNCENTRY( __gc, pulsecnt_unregister )
LROT_TABENTRY ( __index, pulsecnt_dyn )
// LROT_FUNCENTRY( __tostring, pulsecnt_tostring )
LROT_FUNCENTRY( getCnt, pulsecnt_getCnt ) LROT_FUNCENTRY( getCnt, pulsecnt_getCnt )
LROT_FUNCENTRY( clear, pulsecnt_clear ) LROT_FUNCENTRY( clear, pulsecnt_clear )
LROT_FUNCENTRY( testCb, pulsecnt_testCb ) LROT_FUNCENTRY( testCb, pulsecnt_testCb )
...@@ -690,13 +693,9 @@ LROT_BEGIN(pulsecnt_dyn) ...@@ -690,13 +693,9 @@ LROT_BEGIN(pulsecnt_dyn)
LROT_FUNCENTRY( setFilter, pulsecnt_set_filter ) LROT_FUNCENTRY( setFilter, pulsecnt_set_filter )
LROT_FUNCENTRY( rawSetEventVal, pulsecnt_set_event_value ) LROT_FUNCENTRY( rawSetEventVal, pulsecnt_set_event_value )
LROT_FUNCENTRY( rawGetEventVal, pulsecnt_get_event_value ) LROT_FUNCENTRY( rawGetEventVal, pulsecnt_get_event_value )
// LROT_FUNCENTRY( __tostring, pulsecnt_tostring )
LROT_FUNCENTRY( __gc, pulsecnt_unregister )
LROT_TABENTRY ( __index, pulsecnt_dyn )
LROT_END(pulsecnt_dyn, NULL, 0) LROT_END(pulsecnt_dyn, NULL, 0)
LROT_BEGIN(pulsecnt) LROT_BEGIN(pulsecnt, NULL, 0)
LROT_FUNCENTRY( create, pulsecnt_create ) LROT_FUNCENTRY( create, pulsecnt_create )
LROT_NUMENTRY ( PCNT_MODE_KEEP, 0 ) /*pcnt_ctrl_mode_t.PCNT_MODE_KEEP*/ LROT_NUMENTRY ( PCNT_MODE_KEEP, 0 ) /*pcnt_ctrl_mode_t.PCNT_MODE_KEEP*/
LROT_NUMENTRY ( PCNT_MODE_REVERSE, 1 ) /*pcnt_ctrl_mode_t.PCNT_MODE_REVERSE*/ LROT_NUMENTRY ( PCNT_MODE_REVERSE, 1 ) /*pcnt_ctrl_mode_t.PCNT_MODE_REVERSE*/
...@@ -717,7 +716,7 @@ LROT_END(pulsecnt, NULL, 0) ...@@ -717,7 +716,7 @@ LROT_END(pulsecnt, NULL, 0)
int luaopen_pulsecnt(lua_State *L) { int luaopen_pulsecnt(lua_State *L) {
luaL_rometatable(L, "pulsecnt.pctr", (void *)pulsecnt_dyn_map); luaL_rometatable(L, "pulsecnt.pctr", LROT_TABLEREF(pulsecnt_dyn));
pulsecnt_task_id = task_get_id(pulsecnt_task); pulsecnt_task_id = task_get_id(pulsecnt_task);
......
...@@ -56,7 +56,7 @@ static int getPixel(lua_State *L) ...@@ -56,7 +56,7 @@ static int getPixel(lua_State *L)
return 1; return 1;
} }
LROT_BEGIN(qrcodegen) LROT_BEGIN(qrcodegen, NULL, 0)
LROT_FUNCENTRY(encodeText, encodeText) LROT_FUNCENTRY(encodeText, encodeText)
LROT_FUNCENTRY(getSize, getSize) LROT_FUNCENTRY(getSize, getSize)
LROT_FUNCENTRY(getPixel, getPixel) LROT_FUNCENTRY(getPixel, getPixel)
......
...@@ -187,7 +187,7 @@ static int lsdmmc_read( lua_State * L) ...@@ -187,7 +187,7 @@ static int lsdmmc_read( lua_State * L)
luaL_addlstring( &b, rbuf, num_sec * 512 ); luaL_addlstring( &b, rbuf, num_sec * 512 );
luaL_pushresult( &b ); luaL_pushresult( &b );
luaM_freearray( L, rbuf, rbuf_size, uint8_t ); luaN_freearray( L, rbuf, rbuf_size );
// all ok // all ok
return 1; return 1;
...@@ -195,7 +195,7 @@ static int lsdmmc_read( lua_State * L) ...@@ -195,7 +195,7 @@ static int lsdmmc_read( lua_State * L)
} else } else
err_msg = "card access failed"; err_msg = "card access failed";
luaM_freearray( L, rbuf, rbuf_size, uint8_t ); luaN_freearray( L, rbuf, rbuf_size );
return luaL_error( L, err_msg ); return luaL_error( L, err_msg );
} }
...@@ -327,16 +327,16 @@ static int lsdmmc_umount( lua_State *L ) ...@@ -327,16 +327,16 @@ static int lsdmmc_umount( lua_State *L )
return luaL_error( L, err_msg ); return luaL_error( L, err_msg );
} }
LROT_BEGIN(sdmmc_card) LROT_BEGIN(sdmmc_card, NULL, 0)
LROT_TABENTRY( __index, sdmmc_card )
LROT_FUNCENTRY( read, lsdmmc_read ) LROT_FUNCENTRY( read, lsdmmc_read )
LROT_FUNCENTRY( write, lsdmmc_write ) LROT_FUNCENTRY( write, lsdmmc_write )
LROT_FUNCENTRY( get_info, lsdmmc_get_info ) LROT_FUNCENTRY( get_info, lsdmmc_get_info )
LROT_FUNCENTRY( mount, lsdmmc_mount ) LROT_FUNCENTRY( mount, lsdmmc_mount )
LROT_FUNCENTRY( umount, lsdmmc_umount ) LROT_FUNCENTRY( umount, lsdmmc_umount )
LROT_TABENTRY( __index, sdmmc_card )
LROT_END(sdmmc_card, NULL, 0) LROT_END(sdmmc_card, NULL, 0)
LROT_BEGIN(sdmmc) LROT_BEGIN(sdmmc, NULL, 0)
LROT_FUNCENTRY( init, lsdmmc_init ) LROT_FUNCENTRY( init, lsdmmc_init )
LROT_NUMENTRY( HS1, SDMMC_HOST_SLOT_0 ) LROT_NUMENTRY( HS1, SDMMC_HOST_SLOT_0 )
LROT_NUMENTRY( HS2, SDMMC_HOST_SLOT_1 ) LROT_NUMENTRY( HS2, SDMMC_HOST_SLOT_1 )
...@@ -352,7 +352,7 @@ LROT_END(sdmmc, NULL, 0) ...@@ -352,7 +352,7 @@ LROT_END(sdmmc, NULL, 0)
static int luaopen_sdmmc( lua_State *L ) static int luaopen_sdmmc( lua_State *L )
{ {
luaL_rometatable(L, "sdmmc.card", (void *)sdmmc_card_map); luaL_rometatable(L, "sdmmc.card", LROT_TABLEREF(sdmmc_card));
// initialize cards // initialize cards
for (int i = 0; i < NUM_CARDS; i++ ) { for (int i = 0; i < NUM_CARDS; i++ ) {
......
...@@ -77,7 +77,7 @@ static int sigma_delta_setduty( lua_State *L ) ...@@ -77,7 +77,7 @@ static int sigma_delta_setduty( lua_State *L )
// Module function map // Module function map
LROT_BEGIN(sigma_delta) LROT_BEGIN(sigma_delta, NULL, 0)
LROT_FUNCENTRY( setup, sigma_delta_setup ) LROT_FUNCENTRY( setup, sigma_delta_setup )
LROT_FUNCENTRY( close, sigma_delta_close ) LROT_FUNCENTRY( close, sigma_delta_close )
//LROT_FUNCENTRY( setpwmduty, sigma_delta_setpwmduty ) //LROT_FUNCENTRY( setpwmduty, sigma_delta_setpwmduty )
......
#define LUA_LIB #define LUA_LIB
#include "lua.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "lstring.h"
#ifndef LOCAL_LUA #ifndef LOCAL_LUA
#include "module.h" #include "module.h"
...@@ -20,6 +18,8 @@ ...@@ -20,6 +18,8 @@
#define DBG_PRINTF(...) #define DBG_PRINTF(...)
#define SJSON_FLOAT_FMT ((sizeof(lua_Float) == 8) ? "%.19g" : "%.9g")
typedef struct { typedef struct {
jsonsl_t jsn; jsonsl_t jsn;
int result_ref; int result_ref;
...@@ -93,24 +93,24 @@ create_new_element(jsonsl_t jsn, ...@@ -93,24 +93,24 @@ create_new_element(jsonsl_t jsn,
case JSONSL_T_LIST: case JSONSL_T_LIST:
case JSONSL_T_OBJECT: case JSONSL_T_OBJECT:
create_table(data); create_table(data);
state->lua_object_ref = lua_ref(data->L, 1); state->lua_object_ref = luaL_ref(data->L, LUA_REGISTRYINDEX);
state->used_count = 0; state->used_count = 0;
lua_rawgeti(data->L, LUA_REGISTRYINDEX, get_parent_object_ref()); lua_rawgeti(data->L, LUA_REGISTRYINDEX, get_parent_object_ref());
if (data->hkey_ref == LUA_NOREF) { if (data->hkey_ref == LUA_NOREF) {
// list, so append // list, so append
lua_pushnumber(data->L, get_parent_object_used_count_pre_inc()); lua_pushinteger(data->L, get_parent_object_used_count_pre_inc());
DBG_PRINTF("Adding array element\n"); DBG_PRINTF("Adding array element\n");
} else { } else {
// object, so // object, so
lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->hkey_ref); lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->hkey_ref);
lua_unref(data->L, data->hkey_ref); luaL_unref(data->L, LUA_REGISTRYINDEX, data->hkey_ref);
data->hkey_ref = LUA_NOREF; data->hkey_ref = LUA_NOREF;
DBG_PRINTF("Adding hash element\n"); DBG_PRINTF("Adding hash element\n");
} }
if (data->pos_ref != LUA_NOREF && state->level > 1) { if (data->pos_ref != LUA_NOREF && state->level > 1) {
lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->pos_ref); lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->pos_ref);
lua_pushnumber(data->L, state->level - 1); lua_pushinteger(data->L, state->level - 1);
lua_pushvalue(data->L, -3); // get the key lua_pushvalue(data->L, -3); // get the key
lua_settable(data->L, -3); lua_settable(data->L, -3);
lua_pop(data->L, 1); lua_pop(data->L, 1);
...@@ -124,11 +124,11 @@ create_new_element(jsonsl_t jsn, ...@@ -124,11 +124,11 @@ create_new_element(jsonsl_t jsn,
if (data->pos_ref != LUA_NOREF) { if (data->pos_ref != LUA_NOREF) {
lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->metatable); lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->metatable);
lua_getfield(data->L, -1, "checkpath"); lua_getfield(data->L, -1, "checkpath");
if (lua_type(data->L, -1) != LUA_TNIL) { if (!lua_isnil(data->L, -1)) {
// Call with the new table and the path as arguments // Call with the new table and the path as arguments
lua_rawgeti(data->L, LUA_REGISTRYINDEX, state->lua_object_ref); lua_rawgeti(data->L, LUA_REGISTRYINDEX, state->lua_object_ref);
lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->pos_ref); lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->pos_ref);
lua_call(data->L, 2, 1); luaL_pcallx(data->L, 2, 1);
want_value = lua_toboolean(data->L, -1); want_value = lua_toboolean(data->L, -1);
} }
lua_pop(data->L, 2); // Discard the metatable and either the getfield result or retval lua_pop(data->L, 2); // Discard the metatable and either the getfield result or retval
...@@ -155,9 +155,15 @@ create_new_element(jsonsl_t jsn, ...@@ -155,9 +155,15 @@ create_new_element(jsonsl_t jsn,
static void push_number(JSN_DATA *data, struct jsonsl_state_st *state) { static void push_number(JSN_DATA *data, struct jsonsl_state_st *state) {
lua_pushlstring(data->L, get_state_buffer(data, state), state->pos_cur - state->pos_begin); lua_pushlstring(data->L, get_state_buffer(data, state), state->pos_cur - state->pos_begin);
LUA_NUMBER r = lua_tonumber(data->L, -1); #if LUA_VERSION_NUM == 501
lua_pop(data->L, 1); lua_pushnumber(data->L, lua_tonumber(data->L, -1));
lua_pushnumber(data->L, r); #else
if (!lua_stringtonumber(data->L, lua_tostring(data->L, -1))) {
// In this case stringtonumber does not push a value
luaL_error(data->L, "Invalid number");
}
#endif
lua_remove(data->L, -2);
} }
static int fromhex(char c) { static int fromhex(char c) {
...@@ -217,7 +223,7 @@ static void push_string(JSN_DATA *data, struct jsonsl_state_st *state) { ...@@ -217,7 +223,7 @@ static void push_string(JSN_DATA *data, struct jsonsl_state_st *state) {
continue; continue;
} }
} }
luaL_putchar(&b, nc); luaL_addchar(&b, nc);
} }
luaL_pushresult(&b); luaL_pushresult(&b);
} }
...@@ -236,18 +242,18 @@ cleanup_closing_element(jsonsl_t jsn, ...@@ -236,18 +242,18 @@ cleanup_closing_element(jsonsl_t jsn,
switch (state->type) { switch (state->type) {
case JSONSL_T_HKEY: case JSONSL_T_HKEY:
push_string(data, state); push_string(data, state);
data->hkey_ref = lua_ref(data->L, 1); data->hkey_ref = luaL_ref(data->L, LUA_REGISTRYINDEX);
break; break;
case JSONSL_T_STRING: case JSONSL_T_STRING:
lua_rawgeti(data->L, LUA_REGISTRYINDEX, get_parent_object_ref()); lua_rawgeti(data->L, LUA_REGISTRYINDEX, get_parent_object_ref());
if (data->hkey_ref == LUA_NOREF) { if (data->hkey_ref == LUA_NOREF) {
// list, so append // list, so append
lua_pushnumber(data->L, get_parent_object_used_count_pre_inc()); lua_pushinteger(data->L, get_parent_object_used_count_pre_inc());
} else { } else {
// object, so // object, so
lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->hkey_ref); lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->hkey_ref);
lua_unref(data->L, data->hkey_ref); luaL_unref(data->L, LUA_REGISTRYINDEX, data->hkey_ref);
data->hkey_ref = LUA_NOREF; data->hkey_ref = LUA_NOREF;
} }
push_string(data, state); push_string(data, state);
...@@ -274,11 +280,11 @@ cleanup_closing_element(jsonsl_t jsn, ...@@ -274,11 +280,11 @@ cleanup_closing_element(jsonsl_t jsn,
lua_rawgeti(data->L, LUA_REGISTRYINDEX, get_parent_object_ref()); lua_rawgeti(data->L, LUA_REGISTRYINDEX, get_parent_object_ref());
if (data->hkey_ref == LUA_NOREF) { if (data->hkey_ref == LUA_NOREF) {
// list, so append // list, so append
lua_pushnumber(data->L, get_parent_object_used_count_pre_inc()); lua_pushinteger(data->L, get_parent_object_used_count_pre_inc());
} else { } else {
// object, so // object, so
lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->hkey_ref); lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->hkey_ref);
lua_unref(data->L, data->hkey_ref); luaL_unref(data->L, LUA_REGISTRYINDEX, data->hkey_ref);
data->hkey_ref = LUA_NOREF; data->hkey_ref = LUA_NOREF;
} }
lua_pushvalue(data->L, -3); lua_pushvalue(data->L, -3);
...@@ -289,11 +295,11 @@ cleanup_closing_element(jsonsl_t jsn, ...@@ -289,11 +295,11 @@ cleanup_closing_element(jsonsl_t jsn,
break; break;
case JSONSL_T_OBJECT: case JSONSL_T_OBJECT:
case JSONSL_T_LIST: case JSONSL_T_LIST:
lua_unref(data->L, state->lua_object_ref); luaL_unref(data->L, LUA_REGISTRYINDEX, state->lua_object_ref);
state->lua_object_ref = LUA_NOREF; state->lua_object_ref = LUA_NOREF;
if (data->pos_ref != LUA_NOREF) { if (data->pos_ref != LUA_NOREF) {
lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->pos_ref); lua_rawgeti(data->L, LUA_REGISTRYINDEX, data->pos_ref);
lua_pushnumber(data->L, state->level); lua_pushinteger(data->L, state->level);
lua_pushnil(data->L); lua_pushnil(data->L);
lua_settable(data->L, -3); lua_settable(data->L, -3);
lua_pop(data->L, 1); lua_pop(data->L, 1);
...@@ -351,7 +357,7 @@ static int sjson_decoder_int(lua_State *L, int argno) { ...@@ -351,7 +357,7 @@ static int sjson_decoder_int(lua_State *L, int argno) {
data->min_needed = data->min_available = jsn->pos; data->min_needed = data->min_available = jsn->pos;
lua_pushlightuserdata(L, 0); lua_pushlightuserdata(L, 0);
data->null_ref = lua_ref(L, 1); data->null_ref = luaL_ref(L, LUA_REGISTRYINDEX);
// This may throw... // This may throw...
lua_newtable(L); lua_newtable(L);
...@@ -361,17 +367,17 @@ static int sjson_decoder_int(lua_State *L, int argno) { ...@@ -361,17 +367,17 @@ static int sjson_decoder_int(lua_State *L, int argno) {
luaL_unref(L, LUA_REGISTRYINDEX, data->null_ref); luaL_unref(L, LUA_REGISTRYINDEX, data->null_ref);
data->null_ref = LUA_NOREF; data->null_ref = LUA_NOREF;
lua_getfield(L, argno, "null"); lua_getfield(L, argno, "null");
data->null_ref = lua_ref(L, 1); data->null_ref = luaL_ref(L, LUA_REGISTRYINDEX);
lua_getfield(L, argno, "metatable"); lua_getfield(L, argno, "metatable");
lua_pushvalue(L, -1); lua_pushvalue(L, -1);
data->metatable = lua_ref(L, 1); data->metatable = luaL_ref(L, LUA_REGISTRYINDEX);
if (lua_type(L, -1) != LUA_TNIL) { if (!lua_isnil(L, -1)) {
lua_getfield(L, -1, "checkpath"); lua_getfield(L, -1, "checkpath");
if (lua_type(L, -1) != LUA_TNIL) { if (!lua_isnil(L, -1)) {
lua_newtable(L); lua_newtable(L);
data->pos_ref = lua_ref(L, 1); data->pos_ref = luaL_ref(L, LUA_REGISTRYINDEX);
} }
lua_pop(L, 1); // Throw away the checkpath value lua_pop(L, 1); // Throw away the checkpath value
} }
...@@ -477,7 +483,7 @@ static int sjson_decoder_write_int(lua_State *L, int udata_pos, int string_pos) ...@@ -477,7 +483,7 @@ static int sjson_decoder_write_int(lua_State *L, int udata_pos, int string_pos)
size_t blen; size_t blen;
data->buffer = luaL_checklstring(L, -1, &blen); data->buffer = luaL_checklstring(L, -1, &blen);
data->buffer_len = blen; data->buffer_len = blen;
data->buffer_ref = lua_ref(L, 1); data->buffer_ref = luaL_ref(L, LUA_REGISTRYINDEX);
jsonsl_feed(data->jsn, str, len); jsonsl_feed(data->jsn, str, len);
...@@ -611,9 +617,9 @@ static void enc_pop_stack(lua_State *L, ENC_DATA *data) { ...@@ -611,9 +617,9 @@ static void enc_pop_stack(lua_State *L, ENC_DATA *data) {
} }
ENC_DATA_STATE *state = &data->stack[data->level]; ENC_DATA_STATE *state = &data->stack[data->level];
lua_unref(L, state->lua_object_ref); luaL_unref(L, LUA_REGISTRYINDEX, state->lua_object_ref);
state->lua_object_ref = LUA_NOREF; state->lua_object_ref = LUA_NOREF;
lua_unref(L, state->lua_key_ref); luaL_unref(L, LUA_REGISTRYINDEX, state->lua_key_ref);
state->lua_key_ref = LUA_REFNIL; state->lua_key_ref = LUA_REFNIL;
data->level--; data->level--;
} }
...@@ -624,7 +630,7 @@ static void enc_push_stack(lua_State *L, ENC_DATA *data, int argno) { ...@@ -624,7 +630,7 @@ static void enc_push_stack(lua_State *L, ENC_DATA *data, int argno) {
} }
lua_pushvalue(L, argno); lua_pushvalue(L, argno);
ENC_DATA_STATE *state = &data->stack[data->level]; ENC_DATA_STATE *state = &data->stack[data->level];
state->lua_object_ref = lua_ref(L, 1); state->lua_object_ref = luaL_ref(L, LUA_REGISTRYINDEX);
state->size = sjson_encoder_get_table_size(L, argno); state->size = sjson_encoder_get_table_size(L, argno);
state->offset = 0; // We haven't started on this one yet state->offset = 0; // We haven't started on this one yet
} }
...@@ -674,7 +680,7 @@ static int sjson_encoder(lua_State *L) { ...@@ -674,7 +680,7 @@ static int sjson_encoder(lua_State *L) {
luaL_unref(L, LUA_REGISTRYINDEX, data->null_ref); luaL_unref(L, LUA_REGISTRYINDEX, data->null_ref);
data->null_ref = LUA_NOREF; data->null_ref = LUA_NOREF;
lua_getfield(L, argno, "null"); lua_getfield(L, argno, "null");
data->null_ref = lua_ref(L, 1); data->null_ref = luaL_ref(L, LUA_REGISTRYINDEX);
} }
return 1; return 1;
...@@ -716,12 +722,27 @@ static void encode_lua_object(lua_State *L, ENC_DATA *data, int argno, const cha ...@@ -716,12 +722,27 @@ static void encode_lua_object(lua_State *L, ENC_DATA *data, int argno, const cha
case LUA_TNUMBER: case LUA_TNUMBER:
{ {
lua_pushvalue(L, argno); lua_pushvalue(L, argno);
size_t len; char value[50];
const char *str = lua_tolstring(L, -1, &len);
char value[len + 1]; #if LUA_VERSION_NUM == 501
strcpy(value, str); #ifdef LUA_NUMBER_INTEGRAL
snprintf(value, sizeof(value), "%d", lua_tointeger(L, -1));
#else
snprintf(value, sizeof(value), SJSON_FLOAT_FMT, lua_tonumber(L, -1));
#endif
#else
if (lua_isinteger(L, -1)) {
snprintf(value, sizeof(value), LUA_INTEGER_FMT, lua_tointeger(L, -1));
} else {
snprintf(value, sizeof(value), SJSON_FLOAT_FMT, lua_tonumber(L, -1));
}
#endif
lua_pop(L, 1); lua_pop(L, 1);
if (strcmp(value, "-inf") == 0 || strcmp(value, "nan") == 0 || strcmp(value, "inf") == 0) {
luaL_addstring(&b, "null"); // According to ECMA-262 section 24.5.2 Note 4
} else {
luaL_addstring(&b, value); luaL_addstring(&b, value);
}
break; break;
} }
...@@ -785,10 +806,7 @@ static void encode_lua_object(lua_State *L, ENC_DATA *data, int argno, const cha ...@@ -785,10 +806,7 @@ static void encode_lua_object(lua_State *L, ENC_DATA *data, int argno, const cha
static int sjson_encoder_next_value_is_table(lua_State *L) { static int sjson_encoder_next_value_is_table(lua_State *L) {
int count = 10; int count = 10;
while ((lua_type(L, -1) == LUA_TFUNCTION while ((lua_isfunction(L, -1)
#ifdef LUA_TLIGHTFUNCTION
|| lua_type(L, -1) == LUA_TLIGHTFUNCTION
#endif
) && count-- > 0) { ) && count-- > 0) {
// call it and use the return value // call it and use the return value
lua_call(L, 0, 1); // Expecting replacement value lua_call(L, 0, 1); // Expecting replacement value
...@@ -806,7 +824,7 @@ static void sjson_encoder_make_next_chunk(lua_State *L, ENC_DATA *data) { ...@@ -806,7 +824,7 @@ static void sjson_encoder_make_next_chunk(lua_State *L, ENC_DATA *data) {
luaL_buffinit(L, &b); luaL_buffinit(L, &b);
// Ending condition // Ending condition
while (data->level >= 0 && !b.lvl) { while (data->level >= 0 /* && !b.lvl */) {
ENC_DATA_STATE *state = &data->stack[data->level]; ENC_DATA_STATE *state = &data->stack[data->level];
int finished = 0; int finished = 0;
...@@ -849,11 +867,11 @@ static void sjson_encoder_make_next_chunk(lua_State *L, ENC_DATA *data) { ...@@ -849,11 +867,11 @@ static void sjson_encoder_make_next_chunk(lua_State *L, ENC_DATA *data) {
if (lua_next(L, -2)) { if (lua_next(L, -2)) {
// save the key // save the key
if (state->offset & 1) { if (state->offset & 1) {
lua_unref(L, state->lua_key_ref); luaL_unref(L, LUA_REGISTRYINDEX, state->lua_key_ref);
state->lua_key_ref = LUA_NOREF; state->lua_key_ref = LUA_NOREF;
// Duplicate the key // Duplicate the key
lua_pushvalue(L, -2); lua_pushvalue(L, -2);
state->lua_key_ref = lua_ref(L, 1); state->lua_key_ref = luaL_ref(L, LUA_REGISTRYINDEX);
} }
if ((state->offset & 1) == 0) { if ((state->offset & 1) == 0) {
...@@ -895,7 +913,7 @@ static void sjson_encoder_make_next_chunk(lua_State *L, ENC_DATA *data) { ...@@ -895,7 +913,7 @@ static void sjson_encoder_make_next_chunk(lua_State *L, ENC_DATA *data) {
} }
} }
luaL_pushresult(&b); luaL_pushresult(&b);
data->current_str_ref = lua_ref(L, 1); data->current_str_ref = luaL_ref(L, LUA_REGISTRYINDEX);
data->offset = 0; data->offset = 0;
} }
...@@ -923,7 +941,7 @@ static int sjson_encoder_read_int(lua_State *L, ENC_DATA *data, int readsize) { ...@@ -923,7 +941,7 @@ static int sjson_encoder_read_int(lua_State *L, ENC_DATA *data, int readsize) {
readsize -= amnt; readsize -= amnt;
if (data->offset == len) { if (data->offset == len) {
lua_unref(L, data->current_str_ref); luaL_unref(L, LUA_REGISTRYINDEX, data->current_str_ref);
data->current_str_ref = LUA_NOREF; data->current_str_ref = LUA_NOREF;
} }
} }
...@@ -992,71 +1010,34 @@ static int sjson_encoder_destructor(lua_State *L) { ...@@ -992,71 +1010,34 @@ static int sjson_encoder_destructor(lua_State *L) {
return 0; return 0;
} }
#ifdef LOCAL_LUA
static const luaL_Reg sjson_encoder_map[] = { LROT_BEGIN(sjson_encoder_map, NULL, LROT_MASK_GC_INDEX)
{ "read", sjson_encoder_read },
{ "__gc", sjson_encoder_destructor },
{ NULL, NULL }
};
static const luaL_Reg sjson_decoder_map[] = {
{ "write", sjson_decoder_write },
{ "result", sjson_decoder_result },
{ "__gc", sjson_decoder_destructor },
{ NULL, NULL }
};
static const luaL_Reg sjsonlib[] = {
{ "decode", sjson_decode },
{ "decoder", sjson_decoder },
{ "encode", sjson_encode },
{ "encoder", sjson_encoder },
{NULL, NULL}
};
#else
LROT_BEGIN(sjson_encoder)
LROT_FUNCENTRY( read, sjson_encoder_read )
LROT_FUNCENTRY( __gc, sjson_encoder_destructor ) LROT_FUNCENTRY( __gc, sjson_encoder_destructor )
LROT_TABENTRY ( __index, sjson_encoder ) LROT_TABENTRY( __index, sjson_encoder_map )
LROT_END(sjson_encoder, NULL, 0) LROT_FUNCENTRY( read, sjson_encoder_read )
LROT_END(sjson_encoder_map, NULL, LROT_MASK_GC_INDEX)
LROT_BEGIN(sjson_decoder)
LROT_BEGIN(sjson_decoder_map, NULL, LROT_MASK_GC_INDEX)
LROT_FUNCENTRY( __gc, sjson_decoder_destructor )
LROT_TABENTRY( __index, sjson_decoder_map )
LROT_FUNCENTRY( write, sjson_decoder_write ) LROT_FUNCENTRY( write, sjson_decoder_write )
LROT_FUNCENTRY( result, sjson_decoder_result ) LROT_FUNCENTRY( result, sjson_decoder_result )
LROT_FUNCENTRY( __gc, sjson_decoder_destructor ) LROT_END(sjson_decoder_map, NULL, LROT_MASK_GC_INDEX)
LROT_TABENTRY ( __index, sjson_decoder )
LROT_END(sjson_decoder, NULL, 0)
LROT_BEGIN(sjson)
LROT_BEGIN(sjson, NULL, 0)
LROT_FUNCENTRY( encode, sjson_encode ) LROT_FUNCENTRY( encode, sjson_encode )
LROT_FUNCENTRY( decode, sjson_decode ) LROT_FUNCENTRY( decode, sjson_decode )
LROT_FUNCENTRY( encoder, sjson_encoder ) LROT_FUNCENTRY( encoder, sjson_encoder )
LROT_FUNCENTRY( decoder, sjson_decoder ) LROT_FUNCENTRY( decoder, sjson_decoder )
LROT_LUDENTRY ( NULL, 0 )
LROT_END(sjson, NULL, 0) LROT_END(sjson, NULL, 0)
#endif
LUALIB_API int luaopen_sjson (lua_State *L) { LUALIB_API int luaopen_sjson (lua_State *L) {
#ifdef LOCAL_LUA luaL_rometatable(L, "sjson.decoder", LROT_TABLEREF(sjson_decoder_map));
luaL_register(L, LUA_SJSONLIBNAME, sjsonlib); luaL_rometatable(L, "sjson.encoder", LROT_TABLEREF(sjson_encoder_map));
lua_getglobal(L, LUA_SJSONLIBNAME);
lua_pushstring(L, "NULL");
lua_pushlightuserdata(L, 0);
lua_settable(L, -3);
lua_pop(L, 1);
luaL_newmetatable(L, "sjson.encoder");
luaL_register(L, NULL, sjson_encoder_map);
lua_setfield(L, -1, "__index");
luaL_newmetatable(L, "sjson.decoder");
luaL_register(L, NULL, sjson_decoder_map);
lua_setfield(L, -1, "__index");
#else
luaL_rometatable(L, "sjson.decoder", (void *)sjson_decoder_map);
luaL_rometatable(L, "sjson.encoder", (void *)sjson_encoder_map);
#endif
return 1; return 1;
} }
#ifndef LOCAL_LUA
NODEMCU_MODULE(SJSON, "sjson", sjson, luaopen_sjson); NODEMCU_MODULE(SJSON, "sjson", sjson, luaopen_sjson);
#endif
...@@ -129,19 +129,19 @@ static int l_crypto_box_seal_open(lua_State *L) ...@@ -129,19 +129,19 @@ static int l_crypto_box_seal_open(lua_State *L)
return 1; return 1;
} }
LROT_BEGIN(random) LROT_BEGIN(random, NULL, 0)
LROT_FUNCENTRY(random, l_randombytes_random) LROT_FUNCENTRY(random, l_randombytes_random)
LROT_FUNCENTRY(uniform, l_randombytes_uniform) LROT_FUNCENTRY(uniform, l_randombytes_uniform)
LROT_FUNCENTRY(buf, l_randombytes_buf) LROT_FUNCENTRY(buf, l_randombytes_buf)
LROT_END(random, NULL, 0) LROT_END(random, NULL, 0)
LROT_BEGIN(crypto_box) LROT_BEGIN(crypto_box, NULL, 0)
LROT_FUNCENTRY(keypair, l_crypto_box_keypair) LROT_FUNCENTRY(keypair, l_crypto_box_keypair)
LROT_FUNCENTRY(seal, l_crypto_box_seal) LROT_FUNCENTRY(seal, l_crypto_box_seal)
LROT_FUNCENTRY(seal_open, l_crypto_box_seal_open) LROT_FUNCENTRY(seal_open, l_crypto_box_seal_open)
LROT_END(crypto_box, NULL, 0) LROT_END(crypto_box, NULL, 0)
LROT_BEGIN(sodium) LROT_BEGIN(sodium, NULL, 0)
LROT_TABENTRY(random, random) LROT_TABENTRY(random, random)
LROT_TABENTRY(crypto_box, crypto_box) LROT_TABENTRY(crypto_box, crypto_box)
LROT_END(sodium, NULL, 0) LROT_END(sodium, NULL, 0)
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
#include "driver/spi_common.h" #include "driver/spi_common.h"
LROT_BEGIN(lspi) LROT_BEGIN(lspi, NULL, 0)
LROT_FUNCENTRY( master, lspi_master ) LROT_FUNCENTRY( master, lspi_master )
// LROT_FUNCENTRY( slave, lspi_slave ) // LROT_FUNCENTRY( slave, lspi_slave )
#if defined(CONFIG_IDF_TARGET_ESP32) #if defined(CONFIG_IDF_TARGET_ESP32)
......
...@@ -177,11 +177,11 @@ free_mem: ...@@ -177,11 +177,11 @@ free_mem:
} }
LROT_BEGIN(lspi_device) LROT_BEGIN(lspi_device, NULL, 0)
LROT_FUNCENTRY( transfer, lspi_device_transfer )
LROT_FUNCENTRY( remove, lspi_device_free )
LROT_FUNCENTRY( __gc, lspi_device_free ) LROT_FUNCENTRY( __gc, lspi_device_free )
LROT_TABENTRY( __index, lspi_device ) LROT_TABENTRY( __index, lspi_device )
LROT_FUNCENTRY( transfer, lspi_device_transfer )
LROT_FUNCENTRY( remove, lspi_device_free )
LROT_END(lspi_device, NULL, 0) LROT_END(lspi_device, NULL, 0)
...@@ -320,11 +320,11 @@ static int lspi_host_device( lua_State *L ) ...@@ -320,11 +320,11 @@ static int lspi_host_device( lua_State *L )
} }
LROT_BEGIN(lspi_master) LROT_BEGIN(lspi_master, NULL, 0)
LROT_FUNCENTRY( device, lspi_host_device )
LROT_FUNCENTRY( close, lspi_host_free )
LROT_FUNCENTRY( __gc, lspi_host_free ) LROT_FUNCENTRY( __gc, lspi_host_free )
LROT_TABENTRY( __index, lspi_master ) LROT_TABENTRY( __index, lspi_master )
LROT_FUNCENTRY( device, lspi_host_device )
LROT_FUNCENTRY( close, lspi_host_free )
LROT_END(lspi_master, NULL, 0) LROT_END(lspi_master, NULL, 0)
...@@ -332,7 +332,7 @@ LROT_END(lspi_master, NULL, 0) ...@@ -332,7 +332,7 @@ LROT_END(lspi_master, NULL, 0)
// Generic // Generic
// //
int luaopen_spi_master( lua_State *L ) { int luaopen_spi_master( lua_State *L ) {
luaL_rometatable(L, UD_HOST_STR, (void *)lspi_master_map); luaL_rometatable(L, UD_HOST_STR, LROT_TABLEREF(lspi_master));
luaL_rometatable(L, UD_DEVICE_STR, (void *)lspi_device_map); luaL_rometatable(L, UD_DEVICE_STR, LROT_TABLEREF(lspi_device));
return 0; return 0;
} }
...@@ -392,7 +392,7 @@ static int b_size (lua_State *L) { ...@@ -392,7 +392,7 @@ static int b_size (lua_State *L) {
LROT_BEGIN(thislib) LROT_BEGIN(thislib, NULL, 0)
LROT_FUNCENTRY(pack, b_pack) LROT_FUNCENTRY(pack, b_pack)
LROT_FUNCENTRY(unpack, b_unpack) LROT_FUNCENTRY(unpack, b_unpack)
LROT_FUNCENTRY(size, b_size) LROT_FUNCENTRY(size, b_size)
......
...@@ -118,7 +118,7 @@ static int time_epoch2cal(lua_State *L) ...@@ -118,7 +118,7 @@ static int time_epoch2cal(lua_State *L)
static int time_cal2epoc(lua_State *L) static int time_cal2epoc(lua_State *L)
{ {
struct tm date; struct tm date;
luaL_checkanytable (L, 1); luaL_checktable (L, 1);
lua_getfield (L, 1, "year"); lua_getfield (L, 1, "year");
date.tm_year = luaL_optinteger(L, -1, 1900) - 1900; date.tm_year = luaL_optinteger(L, -1, 1900) - 1900;
...@@ -143,7 +143,7 @@ static int time_cal2epoc(lua_State *L) ...@@ -143,7 +143,7 @@ static int time_cal2epoc(lua_State *L)
return 1; return 1;
} }
LROT_BEGIN(time) LROT_BEGIN(time, NULL, 0)
LROT_FUNCENTRY(set, time_set) LROT_FUNCENTRY(set, time_set)
LROT_FUNCENTRY(get, time_get) LROT_FUNCENTRY(get, time_get)
LROT_FUNCENTRY(getlocal, time_getLocal) LROT_FUNCENTRY(getlocal, time_getLocal)
......
...@@ -59,7 +59,7 @@ static void alarm_timer_task(task_param_t param, task_prio_t prio) ...@@ -59,7 +59,7 @@ static void alarm_timer_task(task_param_t param, task_prio_t prio)
luaL_unref(L, LUA_REGISTRYINDEX, tmr->self_ref); luaL_unref(L, LUA_REGISTRYINDEX, tmr->self_ref);
tmr->self_ref = LUA_NOREF; tmr->self_ref = LUA_NOREF;
} }
lua_call(L, 1, 0); luaL_pcallx(L, 1, 0);
} }
static tmr_t tmr_get( lua_State *L, int stack ) static tmr_t tmr_get( lua_State *L, int stack )
...@@ -81,7 +81,7 @@ static int tmr_register(lua_State* L) ...@@ -81,7 +81,7 @@ static int tmr_register(lua_State* L)
luaL_argcheck(L, mode == TIMER_MODE_SINGLE || mode == TIMER_MODE_SEMI || mode == TIMER_MODE_AUTO, stack, "Invalid mode"); luaL_argcheck(L, mode == TIMER_MODE_SINGLE || mode == TIMER_MODE_SEMI || mode == TIMER_MODE_AUTO, stack, "Invalid mode");
++stack; ++stack;
luaL_argcheck(L, lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION, stack, "Must be function"); luaL_checkfunction(L, stack);
if (tmr->timer) { if (tmr->timer) {
// delete previous timer since mode change could be requested here // delete previous timer since mode change could be requested here
...@@ -235,7 +235,9 @@ static int tmr_create( lua_State *L ) { ...@@ -235,7 +235,9 @@ static int tmr_create( lua_State *L ) {
// Module function map // Module function map
LROT_BEGIN(tmr_dyn) LROT_BEGIN(tmr_dyn, NULL, 0)
LROT_FUNCENTRY( __gc, tmr_unregister )
LROT_TABENTRY ( __index, tmr_dyn )
LROT_FUNCENTRY( register, tmr_register ) LROT_FUNCENTRY( register, tmr_register )
LROT_FUNCENTRY( alarm, tmr_alarm ) LROT_FUNCENTRY( alarm, tmr_alarm )
LROT_FUNCENTRY( start, tmr_start ) LROT_FUNCENTRY( start, tmr_start )
...@@ -243,11 +245,9 @@ LROT_BEGIN(tmr_dyn) ...@@ -243,11 +245,9 @@ LROT_BEGIN(tmr_dyn)
LROT_FUNCENTRY( unregister, tmr_unregister ) LROT_FUNCENTRY( unregister, tmr_unregister )
LROT_FUNCENTRY( interval, tmr_interval) LROT_FUNCENTRY( interval, tmr_interval)
LROT_FUNCENTRY( state, tmr_state ) LROT_FUNCENTRY( state, tmr_state )
LROT_FUNCENTRY( __gc, tmr_unregister )
LROT_TABENTRY ( __index, tmr_dyn )
LROT_END(tmr_dyn, NULL, 0) LROT_END(tmr_dyn, NULL, 0)
LROT_BEGIN(tmr) LROT_BEGIN(tmr, NULL, 0)
LROT_FUNCENTRY( create, tmr_create ) LROT_FUNCENTRY( create, tmr_create )
LROT_NUMENTRY ( ALARM_SINGLE, TIMER_MODE_SINGLE ) LROT_NUMENTRY ( ALARM_SINGLE, TIMER_MODE_SINGLE )
LROT_NUMENTRY ( ALARM_SEMI, TIMER_MODE_SEMI ) LROT_NUMENTRY ( ALARM_SEMI, TIMER_MODE_SEMI )
...@@ -255,7 +255,7 @@ LROT_BEGIN(tmr) ...@@ -255,7 +255,7 @@ LROT_BEGIN(tmr)
LROT_END(tmr, NULL, 0) LROT_END(tmr, NULL, 0)
static int luaopen_tmr( lua_State *L ){ static int luaopen_tmr( lua_State *L ){
luaL_rometatable(L, "tmr.timer", (void *)tmr_dyn_map); luaL_rometatable(L, "tmr.timer", LROT_TABLEREF(tmr_dyn));
alarm_task_id = task_get_id(alarm_timer_task); alarm_task_id = task_get_id(alarm_timer_task);
......
...@@ -141,7 +141,7 @@ static void touch_task(task_param_t param, task_prio_t prio) ...@@ -141,7 +141,7 @@ static void touch_task(task_param_t param, task_prio_t prio)
// call the cb_ref with one argument // call the cb_ref with one argument
/* do the call (1 argument, 0 results) */ /* do the call (1 argument, 0 results) */
if (lua_pcall(L, 1, 0, 0) != 0) { if (luaL_pcallx(L, 1, 0) != 0) {
ESP_LOGI(TAG, "error running callback function `f': %s", funcName); ESP_LOGI(TAG, "error running callback function `f': %s", funcName);
} }
} }
...@@ -179,7 +179,7 @@ static int touch_create( lua_State *L ) { ...@@ -179,7 +179,7 @@ static int touch_create( lua_State *L ) {
touch_t tp = &tpObj; touch_t tp = &tpObj;
// const int top = lua_gettop(L); // const int top = lua_gettop(L);
luaL_checkanytable (L, 1); luaL_checktable (L, 1);
tp->is_debug = opt_checkbool(L, "isDebug", false); tp->is_debug = opt_checkbool(L, "isDebug", false);
tp->filterMs = opt_checkint(L, "filterMs", 0); tp->filterMs = opt_checkint(L, "filterMs", 0);
...@@ -227,7 +227,7 @@ static int touch_create( lua_State *L ) { ...@@ -227,7 +227,7 @@ static int touch_create( lua_State *L ) {
isCallback = false; isCallback = false;
if (tp->is_debug) ESP_LOGI(TAG, "No callback provided. Not turning on interrupt." ); if (tp->is_debug) ESP_LOGI(TAG, "No callback provided. Not turning on interrupt." );
} else { } else {
luaL_argcheck(L, lua_type(L, -1) == LUA_TFUNCTION || lua_type(L, -1) == LUA_TLIGHTFUNCTION, -1, "Cb must be function"); luaL_checkfunction(L, -1);
//get the lua function reference //get the lua function reference
luaL_unref(L, LUA_REGISTRYINDEX, tp->cb_ref); luaL_unref(L, LUA_REGISTRYINDEX, tp->cb_ref);
...@@ -589,19 +589,18 @@ static int touch_unregister(lua_State* L) { ...@@ -589,19 +589,18 @@ static int touch_unregister(lua_State* L) {
return 0; return 0;
} }
LROT_BEGIN(touch_dyn) LROT_BEGIN(touch_dyn, NULL, 0)
LROT_FUNCENTRY( __gc, touch_unregister )
LROT_TABENTRY ( __index, touch_dyn )
// LROT_FUNCENTRY( __tostring, touch_tostring )
LROT_FUNCENTRY( read, touch_read ) LROT_FUNCENTRY( read, touch_read )
LROT_FUNCENTRY( intrEnable, touch_intrEnable ) LROT_FUNCENTRY( intrEnable, touch_intrEnable )
LROT_FUNCENTRY( intrDisable, touch_intrDisable ) LROT_FUNCENTRY( intrDisable, touch_intrDisable )
LROT_FUNCENTRY( setThres, touch_setThres ) LROT_FUNCENTRY( setThres, touch_setThres )
LROT_FUNCENTRY( setTriggerMode, touch_setTriggerMode ) LROT_FUNCENTRY( setTriggerMode, touch_setTriggerMode )
// LROT_FUNCENTRY( __tostring, touch_tostring )
LROT_FUNCENTRY( __gc, touch_unregister )
LROT_TABENTRY ( __index, touch_dyn )
LROT_END(touch_dyn, NULL, 0) LROT_END(touch_dyn, NULL, 0)
LROT_BEGIN(touch) LROT_BEGIN(touch, NULL, 0)
LROT_FUNCENTRY( create, touch_create ) LROT_FUNCENTRY( create, touch_create )
LROT_NUMENTRY ( TOUCH_HVOLT_KEEP, TOUCH_HVOLT_KEEP ) LROT_NUMENTRY ( TOUCH_HVOLT_KEEP, TOUCH_HVOLT_KEEP )
LROT_NUMENTRY ( TOUCH_HVOLT_2V4, TOUCH_HVOLT_2V4 ) LROT_NUMENTRY ( TOUCH_HVOLT_2V4, TOUCH_HVOLT_2V4 )
...@@ -626,7 +625,7 @@ LROT_END(touch, NULL, 0) ...@@ -626,7 +625,7 @@ LROT_END(touch, NULL, 0)
int luaopen_touch(lua_State *L) { int luaopen_touch(lua_State *L) {
luaL_rometatable(L, "touch.pctr", (void *)touch_dyn_map); luaL_rometatable(L, "touch.pctr", LROT_TABLEREF(touch_dyn));
touch_task_id = task_get_id(touch_task); touch_task_id = task_get_id(touch_task);
......
...@@ -558,7 +558,9 @@ static int lu8g2_updateDisplayArea( lua_State *L ) ...@@ -558,7 +558,9 @@ static int lu8g2_updateDisplayArea( lua_State *L )
} }
LROT_BEGIN(lu8g2_display) LROT_BEGIN(lu8g2_display, NULL, 0)
//LROT_FUNCENTRY( __gc, lu8g2_display_free )
LROT_TABENTRY( __index, lu8g2_display )
LROT_FUNCENTRY( clearBuffer, lu8g2_clearBuffer ) LROT_FUNCENTRY( clearBuffer, lu8g2_clearBuffer )
LROT_FUNCENTRY( drawBox, lu8g2_drawBox ) LROT_FUNCENTRY( drawBox, lu8g2_drawBox )
LROT_FUNCENTRY( drawCircle, lu8g2_drawCircle ) LROT_FUNCENTRY( drawCircle, lu8g2_drawCircle )
...@@ -600,8 +602,6 @@ LROT_BEGIN(lu8g2_display) ...@@ -600,8 +602,6 @@ LROT_BEGIN(lu8g2_display)
LROT_FUNCENTRY( setPowerSave, lu8g2_setPowerSave ) LROT_FUNCENTRY( setPowerSave, lu8g2_setPowerSave )
LROT_FUNCENTRY( updateDispla, lu8g2_updateDisplay ) LROT_FUNCENTRY( updateDispla, lu8g2_updateDisplay )
LROT_FUNCENTRY( updateDisplayArea, lu8g2_updateDisplayArea ) LROT_FUNCENTRY( updateDisplayArea, lu8g2_updateDisplayArea )
//LROT_FUNCENTRY( __gc, lu8g2_display_free )
LROT_TABENTRY( __index, lu8g2_display )
LROT_END(lu8g2_display, NULL, 0) LROT_END(lu8g2_display, NULL, 0)
...@@ -802,7 +802,7 @@ U8G2_DISPLAY_TABLE_SPI ...@@ -802,7 +802,7 @@ U8G2_DISPLAY_TABLE_SPI
#define U8G2_DISPLAY_TABLE_ENTRY(function, binding) \ #define U8G2_DISPLAY_TABLE_ENTRY(function, binding) \
LROT_FUNCENTRY( binding, l ## binding ) LROT_FUNCENTRY( binding, l ## binding )
LROT_BEGIN(lu8g2) LROT_BEGIN(lu8g2, NULL, 0)
U8G2_DISPLAY_TABLE_I2C U8G2_DISPLAY_TABLE_I2C
U8G2_DISPLAY_TABLE_SPI U8G2_DISPLAY_TABLE_SPI
// //
...@@ -824,7 +824,7 @@ LROT_BEGIN(lu8g2) ...@@ -824,7 +824,7 @@ LROT_BEGIN(lu8g2)
LROT_END(lu8g2, NULL, 0) LROT_END(lu8g2, NULL, 0)
int luaopen_u8g2( lua_State *L ) { int luaopen_u8g2( lua_State *L ) {
luaL_rometatable(L, "u8g2.display", (void *)lu8g2_display_map); luaL_rometatable(L, "u8g2.display", LROT_TABLEREF(lu8g2_display));
return 0; return 0;
} }
......
...@@ -3,15 +3,13 @@ ...@@ -3,15 +3,13 @@
#include "module.h" #include "module.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "platform.h" #include "platform.h"
#include "driver/console.h" #include "linput.h"
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
extern uart_status_t uart_status[NUM_UART];
static lua_State *gL = NULL; static lua_State *gL = NULL;
bool run_input = true;
bool uart_on_data_cb(unsigned id, const char *buf, size_t len){ bool uart_on_data_cb(unsigned id, const char *buf, size_t len){
if(!buf || len==0) if(!buf || len==0)
return false; return false;
...@@ -23,7 +21,7 @@ bool uart_on_data_cb(unsigned id, const char *buf, size_t len){ ...@@ -23,7 +21,7 @@ bool uart_on_data_cb(unsigned id, const char *buf, size_t len){
int top = lua_gettop(gL); int top = lua_gettop(gL);
lua_rawgeti(gL, LUA_REGISTRYINDEX, uart_status[id].receive_rf); lua_rawgeti(gL, LUA_REGISTRYINDEX, uart_status[id].receive_rf);
lua_pushlstring(gL, buf, len); lua_pushlstring(gL, buf, len);
lua_pcall(gL, 1, 0, 0); luaL_pcallx(gL, 1, 0);
lua_settop(gL, top); lua_settop(gL, top);
return !run_input; return !run_input;
} }
...@@ -39,7 +37,7 @@ bool uart_on_error_cb(unsigned id, const char *buf, size_t len){ ...@@ -39,7 +37,7 @@ bool uart_on_error_cb(unsigned id, const char *buf, size_t len){
int top = lua_gettop(gL); int top = lua_gettop(gL);
lua_rawgeti(gL, LUA_REGISTRYINDEX, uart_status[id].error_rf); lua_rawgeti(gL, LUA_REGISTRYINDEX, uart_status[id].error_rf);
lua_pushlstring(gL, buf, len); lua_pushlstring(gL, buf, len);
lua_pcall(gL, 1, 0, 0); luaL_pcallx(gL, 1, 0);
lua_settop(gL, top); lua_settop(gL, top);
return true; return true;
} }
...@@ -47,7 +45,7 @@ bool uart_on_error_cb(unsigned id, const char *buf, size_t len){ ...@@ -47,7 +45,7 @@ bool uart_on_error_cb(unsigned id, const char *buf, size_t len){
// Lua: uart.on([id], "method", [number/char], function, [run_input]) // Lua: uart.on([id], "method", [number/char], function, [run_input])
static int uart_on( lua_State* L ) static int uart_on( lua_State* L )
{ {
unsigned id = CONSOLE_UART; unsigned id = CONFIG_ESP_CONSOLE_UART_NUM;
size_t sl, el; size_t sl, el;
int32_t run = 1; int32_t run = 1;
uint8_t stack = 1; uint8_t stack = 1;
...@@ -86,8 +84,7 @@ static int uart_on( lua_State* L ) ...@@ -86,8 +84,7 @@ static int uart_on( lua_State* L )
us->need_len = 0; us->need_len = 0;
} }
// luaL_checkanyfunction(L, stack); if (lua_isfunction(L, stack)) {
if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION){
if ( lua_isnumber(L, stack+1) ){ if ( lua_isnumber(L, stack+1) ){
run = lua_tointeger(L, stack+1); run = lua_tointeger(L, stack+1);
} }
...@@ -96,7 +93,7 @@ static int uart_on( lua_State* L ) ...@@ -96,7 +93,7 @@ static int uart_on( lua_State* L )
lua_pushnil(L); lua_pushnil(L);
} }
if(sl == 4 && strcmp(method, "data") == 0){ if(sl == 4 && strcmp(method, "data") == 0){
if(id == CONSOLE_UART) if(id == CONFIG_ESP_CONSOLE_UART_NUM)
run_input = true; run_input = true;
if(us->receive_rf != LUA_NOREF){ if(us->receive_rf != LUA_NOREF){
luaL_unref(L, LUA_REGISTRYINDEX, us->receive_rf); luaL_unref(L, LUA_REGISTRYINDEX, us->receive_rf);
...@@ -105,7 +102,7 @@ static int uart_on( lua_State* L ) ...@@ -105,7 +102,7 @@ static int uart_on( lua_State* L )
if(!lua_isnil(L, -1)){ if(!lua_isnil(L, -1)){
us->receive_rf = luaL_ref(L, LUA_REGISTRYINDEX); us->receive_rf = luaL_ref(L, LUA_REGISTRYINDEX);
gL = L; gL = L;
if(id == CONSOLE_UART && run==0) if(id == CONFIG_ESP_CONSOLE_UART_NUM && run==0)
run_input = false; run_input = false;
} else { } else {
lua_pop(L, 1); lua_pop(L, 1);
...@@ -128,7 +125,6 @@ static int uart_on( lua_State* L ) ...@@ -128,7 +125,6 @@ static int uart_on( lua_State* L )
return 0; return 0;
} }
bool uart0_echo = true;
// Lua: actualbaud = setup( id, baud, databits, parity, stopbits, echo ) // Lua: actualbaud = setup( id, baud, databits, parity, stopbits, echo )
static int uart_setup( lua_State* L ) static int uart_setup( lua_State* L )
{ {
...@@ -142,13 +138,13 @@ static int uart_setup( lua_State* L ) ...@@ -142,13 +138,13 @@ static int uart_setup( lua_State* L )
databits = luaL_checkinteger( L, 3 ); databits = luaL_checkinteger( L, 3 );
parity = luaL_checkinteger( L, 4 ); parity = luaL_checkinteger( L, 4 );
stopbits = luaL_checkinteger( L, 5 ); stopbits = luaL_checkinteger( L, 5 );
if(id == CONSOLE_UART && lua_isnumber(L,6)){ if(id == CONFIG_ESP_CONSOLE_UART_NUM && lua_isnumber(L,6)){
echo = lua_tointeger(L,6); echo = lua_tointeger(L,6);
if(echo!=0) if(echo!=0)
uart0_echo = true; input_echo = true;
else else
uart0_echo = false; input_echo = false;
} else if(id != CONSOLE_UART && lua_istable( L, 6 )) { } else if(id != CONFIG_ESP_CONSOLE_UART_NUM && lua_istable( L, 6 )) {
lua_getfield (L, 6, "tx"); lua_getfield (L, 6, "tx");
pins.tx_pin = luaL_checkint(L, -1); pins.tx_pin = luaL_checkint(L, -1);
lua_getfield (L, 6, "rx"); lua_getfield (L, 6, "rx");
...@@ -280,7 +276,7 @@ static int luart_tx_flush (lua_State *L) ...@@ -280,7 +276,7 @@ static int luart_tx_flush (lua_State *L)
} }
// Module function map // Module function map
LROT_BEGIN(uart) LROT_BEGIN(uart, NULL, 0)
LROT_FUNCENTRY( setup, uart_setup ) LROT_FUNCENTRY( setup, uart_setup )
LROT_FUNCENTRY( write, uart_write ) LROT_FUNCENTRY( write, uart_write )
LROT_FUNCENTRY( start, uart_start ) LROT_FUNCENTRY( start, uart_start )
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment