Commit bbeb09b6 authored by Terry Ellison's avatar Terry Ellison Committed by Marcel Stör
Browse files

Squashed updates do get Lua51 and Lua53 working (#3075)

-  Lots of minor but nasty bugfixes to get all tests to run clean
-  core lua and test suite fixes to allow luac -F to run cleanly against test suite
-  next tranch to get LFS working
-  luac.cross -a options plus fixes from feedback
-  UART fixes and lua.c merge
-  commit of wip prior to rebaselining against current dev
-  more tweaks
parent 99aba344
......@@ -21,8 +21,20 @@
#include "user_interface.h"
#include "../esp-gdbstub/gdbstub.h"
// gdbstub.brk() just executes a break instruction. Enters gdb
static int init_done = 0;
static int lgdbstub_open(lua_State *L);
// gdbstub.brk() init gdb if nec and execute a break instructiont to entry gdb
static int lgdbstub_break(lua_State *L) {
lgdbstub_open(L);
asm("break 0,0" ::);
return 0;
}
// as for break but also redirect output to the debugger.
static int lgdbstub_pbreak(lua_State *L) {
lgdbstub_open(L);
gdbstub_redirect_output(1);
asm("break 0,0" ::);
return 0;
}
......@@ -34,16 +46,20 @@ static int lgdbstub_gdboutput(lua_State *L) {
}
static int lgdbstub_open(lua_State *L) {
if (init_done)
return 0;
gdbstub_init();
init_done = 1;
return 0;
}
// Module function map
LROT_BEGIN(gdbstub)
LROT_BEGIN(gdbstub, NULL, 0)
LROT_FUNCENTRY( brk, lgdbstub_break )
LROT_FUNCENTRY( pbrk, lgdbstub_pbreak )
LROT_FUNCENTRY( gdboutput, lgdbstub_gdboutput )
LROT_FUNCENTRY( open, lgdbstub_open )
LROT_END( gdbstub, NULL, 0 )
LROT_END(gdbstub, NULL, 0)
NODEMCU_MODULE(GDBSTUB, "gdbstub", gdbstub, NULL);
......@@ -109,7 +109,7 @@ static int lgpio_trig( lua_State* L )
// keep the old one if no callback
old_pin_ref = LUA_NOREF;
} else if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION) {
} else if (lua_isfunction(L, 3)) {
// set up the new callback if present
lua_pushvalue(L, 3);
gpio_cb_ref[pin] = luaL_ref(L, LUA_REGISTRYINDEX);
......@@ -230,7 +230,7 @@ static void seroutasync_done (task_param_t arg)
{
lua_State *L = lua_getstate();
if (serout.delay_table) {
luaM_freearray(L, serout.delay_table, serout.tablelen, uint32);
luaN_freearray(L, serout.delay_table, serout.tablelen);
serout.delay_table = NULL;
}
if (serout.lua_done_ref != LUA_NOREF) {
......@@ -278,7 +278,7 @@ static int lgpio_serout( lua_State* L )
}
if (serout.delay_table) {
luaM_freearray(L, serout.delay_table, serout.tablelen, uint32);
luaN_freearray(L, serout.delay_table, serout.tablelen);
serout.delay_table = NULL;
}
......@@ -312,7 +312,7 @@ static int lgpio_serout( lua_State* L )
delayMicroseconds(serout.delay_table[serout.index]);
}
} while (serout.repeats--);
luaM_freearray(L, serout.delay_table, serout.tablelen, uint32);
luaN_freearray(L, serout.delay_table, serout.tablelen);
serout.delay_table = NULL;
}
return 0;
......@@ -320,12 +320,12 @@ static int lgpio_serout( lua_State* L )
#undef DELAY_TABLE_MAX_LEN
#ifdef LUA_USE_MODULES_GPIO_PULSE
LROT_EXTERN(gpio_pulse);
extern LROT_TABLE(gpio_pulse);
extern int gpio_pulse_init(lua_State *);
#endif
// Module function map
LROT_BEGIN(gpio)
LROT_BEGIN(gpio, NULL, 0)
LROT_FUNCENTRY( mode, lgpio_mode )
LROT_FUNCENTRY( read, lgpio_read )
LROT_FUNCENTRY( write, lgpio_write )
......@@ -344,7 +344,7 @@ LROT_BEGIN(gpio)
LROT_NUMENTRY( LOW, LOW )
LROT_NUMENTRY( FLOAT, FLOAT )
LROT_NUMENTRY( PULLUP, PULLUP )
LROT_END( gpio, NULL, 0 )
LROT_END (gpio, NULL, 0)
int luaopen_gpio( lua_State *L ) {
......
......@@ -102,7 +102,7 @@ static int gpio_pulse_stop(lua_State *L) {
argno++;
}
if (lua_type(L, argno) == LUA_TFUNCTION || lua_type(L, argno) == LUA_TLIGHTFUNCTION) {
if (lua_isfunction(L, argno)) {
lua_pushvalue(L, argno);
} else {
return luaL_error( L, "missing callback" );
......@@ -201,7 +201,7 @@ static int gpio_pulse_build(lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
// First figure out how big we need the block to be
size_t size = luaL_getn(L, 1);
size_t size = lua_objlen(L, 1);
if (size > 100) {
return luaL_error(L, "table is too large: %d entries is more than 100", size);
......@@ -410,7 +410,7 @@ static int gpio_pulse_start(lua_State *L) {
argno++;
}
if (lua_type(L, argno) == LUA_TFUNCTION || lua_type(L, argno) == LUA_TLIGHTFUNCTION) {
if (lua_isfunction(L, argno)) {
lua_pushvalue(L, argno);
} else {
return luaL_error( L, "missing callback" );
......@@ -467,22 +467,22 @@ static void gpio_pulse_task(os_param_t param, uint8_t prio)
}
}
LROT_BEGIN(pulse)
LROT_BEGIN(pulse, NULL, LROT_MASK_GC_INDEX)
LROT_FUNCENTRY( __gc, gpio_pulse_delete )
LROT_TABENTRY( __index, pulse )
LROT_FUNCENTRY( getstate, gpio_pulse_getstate )
LROT_FUNCENTRY( stop, gpio_pulse_stop )
LROT_FUNCENTRY( cancel, gpio_pulse_cancel )
LROT_FUNCENTRY( start, gpio_pulse_start )
LROT_FUNCENTRY( adjust, gpio_pulse_adjust )
LROT_FUNCENTRY( update, gpio_pulse_update )
LROT_FUNCENTRY( __gc, gpio_pulse_delete )
LROT_TABENTRY( __index, pulse )
LROT_END( pulse, pulse, LROT_MASK_GC_INDEX )
LROT_END(pulse, NULL, LROT_MASK_GC_INDEX)
LROT_PUBLIC_BEGIN(gpio_pulse)
LROT_BEGIN(gpio_pulse, NULL, 0)
LROT_FUNCENTRY( build, gpio_pulse_build )
LROT_TABENTRY( __index, gpio_pulse )
LROT_END( gpio_pulse, gpio_pulse, LROT_MASK_INDEX )
LROT_END(gpio_pulse, NULL, 0)
int gpio_pulse_init(lua_State *L)
......
......@@ -100,10 +100,10 @@ static int hdc1080_read(lua_State* L) {
return 2;
}
LROT_BEGIN(hdc1080)
LROT_BEGIN(hdc1080, NULL, 0)
LROT_FUNCENTRY( read, hdc1080_read )
LROT_FUNCENTRY( setup, hdc1080_setup )
LROT_END( hdc1080, NULL, 0 )
LROT_END(hdc1080, NULL, 0)
NODEMCU_MODULE(HDC1080, "hdc1080", hdc1080, NULL);
......@@ -89,10 +89,10 @@ static int hmc5883_read(lua_State* L) {
return 3;
}
LROT_BEGIN(hmc5883)
LROT_BEGIN(hmc5883, NULL, 0)
LROT_FUNCENTRY( read, hmc5883_read )
LROT_FUNCENTRY( setup, hmc5883_setup )
LROT_END( hmc5883, NULL, 0 )
LROT_END(hmc5883, NULL, 0)
NODEMCU_MODULE(HMC5883L, "hmc5883l", hmc5883, NULL);
......@@ -10,7 +10,7 @@
#include "platform.h"
#include "cpu_esp8266.h"
#include "http/httpclient.h"
#include <ctype.h>
static int http_callback_registry = LUA_NOREF;
static void http_callback( char * response, int http_status, char ** full_response_p )
......@@ -128,7 +128,7 @@ static int http_lapi_request( lua_State *L )
body = luaL_checklstring(L, 4, &length);
}
if (lua_type(L, 5) == LUA_TFUNCTION || lua_type(L, 5) == LUA_TLIGHTFUNCTION) {
if (lua_isfunction(L, 5)) {
lua_pushvalue(L, 5); // copy argument (func) to the top of stack
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
http_callback_registry = luaL_ref(L, LUA_REGISTRYINDEX);
......@@ -161,7 +161,7 @@ static int http_lapi_post( lua_State *L )
body = luaL_checklstring(L, 3, &length);
}
if (lua_type(L, 4) == LUA_TFUNCTION || lua_type(L, 4) == LUA_TLIGHTFUNCTION) {
if (lua_isfunction(L, 4)) {
lua_pushvalue(L, 4); // copy argument (func) to the top of stack
if (http_callback_registry != LUA_NOREF)
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
......@@ -195,7 +195,7 @@ static int http_lapi_put( lua_State *L )
body = luaL_checklstring(L, 3, &length);
}
if (lua_type(L, 4) == LUA_TFUNCTION || lua_type(L, 4) == LUA_TLIGHTFUNCTION) {
if (lua_isfunction(L, 4)) {
lua_pushvalue(L, 4); // copy argument (func) to the top of stack
if (http_callback_registry != LUA_NOREF)
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
......@@ -229,7 +229,7 @@ static int http_lapi_delete( lua_State *L )
body = luaL_checklstring(L, 3, &length);
}
if (lua_type(L, 4) == LUA_TFUNCTION || lua_type(L, 4) == LUA_TLIGHTFUNCTION) {
if (lua_isfunction(L, 4)) {
lua_pushvalue(L, 4); // copy argument (func) to the top of stack
if (http_callback_registry != LUA_NOREF)
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
......@@ -258,7 +258,7 @@ static int http_lapi_get( lua_State *L )
headers = luaL_checklstring(L, 2, &length);
}
if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION) {
if (lua_isfunction(L, 3)) {
lua_pushvalue(L, 3); // copy argument (func) to the top of stack
if (http_callback_registry != LUA_NOREF)
luaL_unref(L, LUA_REGISTRYINDEX, http_callback_registry);
......@@ -270,7 +270,7 @@ static int http_lapi_get( lua_State *L )
}
// Module function map
LROT_BEGIN(http)
LROT_BEGIN(http, NULL, 0)
LROT_FUNCENTRY( request, http_lapi_request )
LROT_FUNCENTRY( post, http_lapi_post )
LROT_FUNCENTRY( put, http_lapi_put )
......@@ -280,7 +280,7 @@ LROT_BEGIN(http)
LROT_NUMENTRY( OK, 0 )
LROT_NUMENTRY( ERROR, HTTP_STATUS_GENERIC_ERROR )
LROT_END( http, NULL, 0 )
LROT_END(http, NULL, 0)
NODEMCU_MODULE(HTTP, "http", http, NULL);
......@@ -7,7 +7,6 @@
#include "platform.h"
#include <stdlib.h>
#include <string.h>
#include "task/task.h"
#include "user_interface.h"
static uint8_t data_pin;
static uint8_t clk_pin;
......@@ -16,7 +15,7 @@ static uint8_t pin_data_pin;
static uint8_t pin_clk_pin;
#ifdef GPIO_INTERRUPT_ENABLE
static task_handle_t tasknumber;
static platform_task_handle_t tasknumber;
// HX711_STATUS can be defined to enable the hx711.status() function to get debug info
#undef HX711_STATUS
......@@ -115,7 +114,7 @@ static void ICACHE_RAM_ATTR hx711_data_available() {
control->dropped[control->active] = control->nobuffer;
control->nobuffer = 0;
// post task
task_post_medium(tasknumber, control->active);
platform_post_medium(tasknumber, control->active);
uint8_t next_active = (1 + control->active) % BUFFERS;
......@@ -184,7 +183,7 @@ static int hx711_start( lua_State* L )
int cb_ref;
if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION) {
if (lua_type(L, 3) == LUA_TFUNCTION) {
lua_pushvalue(L, 3); // copy argument (func) to the top of stack
cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
} else {
......@@ -241,7 +240,7 @@ static int hx711_status( lua_State* L )
return 0;
}
static void hx711_task(os_param_t param, uint8_t prio)
static void hx711_task(platform_task_param_t param, uint8_t prio)
{
(void) prio;
if (!control) {
......@@ -315,7 +314,7 @@ static int hx711_read(lua_State* L) {
}
// Module function map
LROT_BEGIN(hx711)
LROT_BEGIN(hx711, NULL, 0)
LROT_FUNCENTRY( init, hx711_init )
LROT_FUNCENTRY( read, hx711_read )
#ifdef GPIO_INTERRUPT_ENABLE
......@@ -325,12 +324,12 @@ LROT_BEGIN(hx711)
#endif
LROT_FUNCENTRY( stop, hx711_stop )
#endif
LROT_END( hx711, NULL, 0 )
LROT_END(hx711, NULL, 0)
int luaopen_hx711(lua_State *L) {
#ifdef GPIO_INTERRUPT_ENABLE
tasknumber = task_get_id(hx711_task);
tasknumber = platform_task_get_id(hx711_task);
#endif
return 0;
}
......
......@@ -146,7 +146,7 @@ static int i2c_read( lua_State *L )
}
// Module function map
LROT_BEGIN(i2c)
LROT_BEGIN(i2c, NULL, 0)
LROT_FUNCENTRY( setup, i2c_setup )
LROT_FUNCENTRY( start, i2c_start )
LROT_FUNCENTRY( stop, i2c_stop )
......@@ -158,7 +158,7 @@ LROT_BEGIN(i2c)
LROT_NUMENTRY( SLOW, PLATFORM_I2C_SPEED_SLOW )
LROT_NUMENTRY( TRANSMITTER, PLATFORM_I2C_DIRECTION_TRANSMITTER )
LROT_NUMENTRY( RECEIVER, PLATFORM_I2C_DIRECTION_RECEIVER )
LROT_END( i2c, NULL, 0 )
LROT_END(i2c, NULL, 0)
NODEMCU_MODULE(I2C, "i2c", i2c, NULL);
......@@ -79,10 +79,10 @@ static int l3g4200d_read(lua_State* L) {
return 3;
}
LROT_BEGIN(l3g4200d)
LROT_BEGIN(l3g4200d, NULL, 0)
LROT_FUNCENTRY( read, l3g4200d_read )
LROT_FUNCENTRY( setup, l3g4200d_setup )
LROT_END( l3g4200d, NULL, 0 )
LROT_END(l3g4200d, NULL, 0)
NODEMCU_MODULE(L3G4200D, "l3g4200d", l3g4200d, NULL);
......@@ -204,14 +204,14 @@ static int mcp4725_read(lua_State* L){
}
LROT_BEGIN(mcp4725)
LROT_BEGIN(mcp4725, NULL, 0)
LROT_FUNCENTRY( write, mcp4725_write )
LROT_FUNCENTRY( read, mcp4725_read )
LROT_NUMENTRY( PWRDN_NONE, MCP4725_POWER_DOWN_NORMAL )
LROT_NUMENTRY( PWRDN_1K, MCP4725_POWER_DOWN_RES_1K>>1 )
LROT_NUMENTRY( PWRDN_100K, MCP4725_POWER_DOWN_RES_100K>>1 )
LROT_NUMENTRY( PWRDN_500K, MCP4725_POWER_DOWN_RES_500K>>1 )
LROT_END( mcp4725, NULL, 0 )
LROT_END(mcp4725, NULL, 0)
NODEMCU_MODULE(MCP4725, "mcp4725", mcp4725, NULL);
......@@ -88,10 +88,10 @@ static int mdns_register(lua_State *L)
}
// Module function map
LROT_BEGIN(mdns)
LROT_BEGIN(mdns, NULL, 0)
LROT_FUNCENTRY( register, mdns_register )
LROT_FUNCENTRY( close, mdns_close )
LROT_END( mdns, NULL, 0 )
LROT_END(mdns, NULL, 0)
NODEMCU_MODULE(MDNS, "mdns", mdns, NULL);
......@@ -1196,7 +1196,7 @@ static int mqtt_socket_connect( lua_State* L )
#endif
// call back function when a connection is obtained, tcp only
if ((stack<=top) && (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION)){
if ((stack<=top) && (lua_isfunction(L, stack))){
lua_pushvalue(L, stack); // copy argument (func) to the top of stack
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_connect_ref);
mud->cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
......@@ -1205,7 +1205,7 @@ static int mqtt_socket_connect( lua_State* L )
stack++;
// call back function when a connection fails
if ((stack<=top) && (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION)){
if ((stack<=top) && (lua_isfunction(L, stack))){
lua_pushvalue(L, stack); // copy argument (func) to the top of stack
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_connect_fail_ref);
mud->cb_connect_fail_ref = luaL_ref(L, LUA_REGISTRYINDEX);
......@@ -1323,7 +1323,7 @@ static int mqtt_socket_on( lua_State* L )
if (method == NULL)
return luaL_error( L, "wrong arg type" );
luaL_checkanyfunction(L, 3);
luaL_checktype(L, 3, LUA_TFUNCTION);
lua_pushvalue(L, 3); // copy argument (func) to the top of stack
if( sl == 7 && strcmp(method, "connect") == 0){
......@@ -1441,8 +1441,8 @@ static int mqtt_socket_unsubscribe( lua_State* L ) {
temp_msg = mqtt_msg_unsubscribe( &msgb, topic, msg_id );
}
if( lua_type( L, stack ) == LUA_TFUNCTION || lua_type( L, stack ) == LUA_TLIGHTFUNCTION ) { // TODO: this will overwrite the previous one.
lua_pushvalue( L, stack ); // copy argument (func) to the top of stack
if (lua_isfunction(L, stack)) { // TODO: this will overwrite the previous one.
lua_pushvalue( L, stack ); // copy argument (func) to the top of stack
luaL_unref( L, LUA_REGISTRYINDEX, mud->cb_unsuback_ref );
mud->cb_unsuback_ref = luaL_ref( L, LUA_REGISTRYINDEX );
}
......@@ -1553,7 +1553,7 @@ static int mqtt_socket_subscribe( lua_State* L ) {
stack++;
}
if( lua_type( L, stack ) == LUA_TFUNCTION || lua_type( L, stack ) == LUA_TLIGHTFUNCTION ) { // TODO: this will overwrite the previous one.
if (lua_isfunction(L, stack)) { // TODO: this will overwrite the previous one.
lua_pushvalue( L, stack ); // copy argument (func) to the top of stack
luaL_unref( L, LUA_REGISTRYINDEX, mud->cb_suback_ref );
mud->cb_suback_ref = luaL_ref( L, LUA_REGISTRYINDEX );
......@@ -1626,7 +1626,7 @@ static int mqtt_socket_publish( lua_State* L )
qos, retain,
msg_id);
if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION){
if (lua_isfunction(L, stack)){
lua_pushvalue(L, stack); // copy argument (func) to the top of stack
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_puback_ref);
mud->cb_puback_ref = luaL_ref(L, LUA_REGISTRYINDEX);
......@@ -1729,7 +1729,10 @@ static int mqtt_socket_lwt( lua_State* L )
}
// Module function map
LROT_BEGIN(mqtt_socket)
LROT_BEGIN(mqtt_socket, NULL, LROT_MASK_GC_INDEX)
LROT_FUNCENTRY( __gc, mqtt_delete )
LROT_TABENTRY( __index, mqtt_socket )
LROT_FUNCENTRY( connect, mqtt_socket_connect )
LROT_FUNCENTRY( close, mqtt_socket_close )
LROT_FUNCENTRY( publish, mqtt_socket_publish )
......@@ -1737,15 +1740,12 @@ LROT_BEGIN(mqtt_socket)
LROT_FUNCENTRY( unsubscribe, mqtt_socket_unsubscribe )
LROT_FUNCENTRY( lwt, mqtt_socket_lwt )
LROT_FUNCENTRY( on, mqtt_socket_on )
LROT_FUNCENTRY( __gc, mqtt_delete )
LROT_TABENTRY( __index, mqtt_socket )
LROT_END( mqtt_socket, mqtt_socket, 0 )
LROT_END(mqtt_socket, NULL, LROT_MASK_GC_INDEX)
LROT_BEGIN(mqtt)
LROT_BEGIN(mqtt, NULL, 0)
LROT_FUNCENTRY( Client, mqtt_socket_client )
LROT_NUMENTRY( CONN_FAIL_SERVER_NOT_FOUND, MQTT_CONN_FAIL_SERVER_NOT_FOUND )
LROT_NUMENTRY( CONN_FAIL_NOT_A_CONNACK_MSG, MQTT_CONN_FAIL_NOT_A_CONNACK_MSG )
LROT_NUMENTRY( CONN_FAIL_DNS, MQTT_CONN_FAIL_DNS )
......@@ -1757,8 +1757,7 @@ LROT_BEGIN(mqtt)
LROT_NUMENTRY( CONNACK_REFUSED_SERVER_UNAVAILABLE, MQTT_CONNACK_REFUSED_SERVER_UNAVAILABLE )
LROT_NUMENTRY( CONNACK_REFUSED_BAD_USER_OR_PASS, MQTT_CONNACK_REFUSED_BAD_USER_OR_PASS )
LROT_NUMENTRY( CONNACK_REFUSED_NOT_AUTHORIZED, MQTT_CONNACK_REFUSED_NOT_AUTHORIZED )
LROT_END( mqtt, mqtt, 0 )
LROT_END(mqtt, NULL, 0)
int luaopen_mqtt( lua_State *L )
......
......@@ -350,7 +350,7 @@ int net_listen( lua_State *L ) {
if (!ipaddr_aton(domain, &addr))
return luaL_error(L, "invalid IP address");
if (ud->type == TYPE_TCP_SERVER) {
if (lua_isfunction(L, stack) || lua_islightfunction(L, stack)) {
if (lua_isfunction(L, stack)) {
lua_pushvalue(L, stack++);
luaL_unref(L, LUA_REGISTRYINDEX, ud->server.cb_accept_ref);
ud->server.cb_accept_ref = luaL_ref(L, LUA_REGISTRYINDEX);
......@@ -421,7 +421,7 @@ int net_connect( lua_State *L ) {
domain = luaL_checklstring(L, 3, &dl);
}
if (lua_gettop(L) > 3) {
luaL_argcheck(L, lua_isfunction(L, 4) || lua_islightfunction(L, 4), 4, "not a function");
luaL_argcheck(L, lua_isfunction(L, 4), 4, "not a function");
lua_pushvalue(L, 4);
luaL_unref(L, LUA_REGISTRYINDEX, ud->client.cb_connect_ref);
ud->client.cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
......@@ -486,7 +486,7 @@ int net_on( lua_State *L ) {
}
if (refptr == NULL)
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);
luaL_unref(L, LUA_REGISTRYINDEX, *refptr);
*refptr = luaL_ref(L, LUA_REGISTRYINDEX);
......@@ -519,7 +519,7 @@ int net_send( lua_State *L ) {
}
data = luaL_checklstring(L, stack++, &datalen);
if (!data || datalen == 0) return luaL_error(L, "no data to send");
if (lua_isfunction(L, stack) || lua_islightfunction(L, stack)) {
if (lua_isfunction(L, stack)) {
lua_pushvalue(L, stack++);
luaL_unref(L, LUA_REGISTRYINDEX, ud->client.cb_sent_ref);
ud->client.cb_sent_ref = luaL_ref(L, LUA_REGISTRYINDEX);
......@@ -595,7 +595,7 @@ int net_dns( lua_State *L ) {
const char *domain = luaL_checklstring(L, 2, &dl);
if (!domain)
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);
lua_pushvalue(L, 3);
ud->client.cb_dns_ref = luaL_ref(L, LUA_REGISTRYINDEX);
......@@ -708,7 +708,7 @@ int net_getaddr( lua_State *L ) {
static void dbg_print_ud(const char *title, lnet_userdata *ud) {
int i;
dbg_printf("%s: Userdata %p:", title, ud);
for (i=0; i<(sizeof(*ud)/sizeof(uint32_t)); i++)
for (i=0; i<(sizeof(*ud)/sizeof(uint32_t)); i++)
dbg_printf( " 0x%08x", ((uint32_t *)ud)[i]);
dbg_printf("\n");
#endif
......@@ -888,9 +888,8 @@ static int net_dns_static( lua_State* L ) {
return luaL_error(L, "wrong domain");
}
/* Register callback with registry */
luaL_checkanyfunction(L, 2);
lua_pushvalue(L, 2);
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_pushvalue(L, 2); // copy argument (func) to the top of stack
int cbref = luaL_ref(L, LUA_REGISTRYINDEX);
ip_addr_t addr;
......@@ -1012,16 +1011,20 @@ static int net_ifinfo( lua_State* L ) {
#pragma mark - Tables
// Module function map
LROT_BEGIN(net_tcpserver)
LROT_BEGIN(net_tcpserver, NULL, LROT_MASK_GC_INDEX)
LROT_FUNCENTRY( __gc, net_delete )
LROT_TABENTRY( __index, net_tcpserver )
LROT_FUNCENTRY( listen, net_listen )
LROT_FUNCENTRY( getaddr, net_getaddr )
LROT_FUNCENTRY( close, net_close )
LROT_FUNCENTRY( __gc, net_delete )
LROT_TABENTRY( __index, net_tcpserver )
LROT_END( net_tcpserver, net_tcpserver, 0 )
LROT_END(net_tcpserver, NULL, LROT_MASK_GC_INDEX)
LROT_BEGIN(net_tcpsocket)
LROT_BEGIN(net_tcpsocket, NULL, LROT_MASK_GC_INDEX)
LROT_FUNCENTRY( __gc, net_delete )
LROT_TABENTRY( __index, net_tcpsocket )
LROT_FUNCENTRY( connect, net_connect )
LROT_FUNCENTRY( close, net_close )
LROT_FUNCENTRY( on, net_on )
......@@ -1032,12 +1035,13 @@ LROT_BEGIN(net_tcpsocket)
LROT_FUNCENTRY( ttl, net_ttl )
LROT_FUNCENTRY( getpeer, net_getpeer )
LROT_FUNCENTRY( getaddr, net_getaddr )
LROT_FUNCENTRY( __gc, net_delete )
LROT_TABENTRY( __index, net_tcpsocket )
LROT_END( net_tcpsocket, net_tcpsocket, 0 )
LROT_END(net_tcpsocket, NULL, LROT_MASK_GC_INDEX)
LROT_BEGIN(net_udpsocket)
LROT_BEGIN(net_udpsocket, NULL, LROT_MASK_GC_INDEX)
LROT_FUNCENTRY( __gc, net_delete )
LROT_TABENTRY( __index, net_udpsocket )
LROT_FUNCENTRY( listen, net_listen )
LROT_FUNCENTRY( close, net_close )
LROT_FUNCENTRY( on, net_on )
......@@ -1045,26 +1049,34 @@ LROT_BEGIN(net_udpsocket)
LROT_FUNCENTRY( dns, net_dns )
LROT_FUNCENTRY( ttl, net_ttl )
LROT_FUNCENTRY( getaddr, net_getaddr )
LROT_FUNCENTRY( __gc, net_delete )
LROT_TABENTRY( __index, net_udpsocket )
LROT_END( net_udpsocket, net_udpsocket, 0 )
LROT_END(net_udpsocket, NULL, LROT_MASK_GC_INDEX)
LROT_BEGIN(net_dns)
LROT_BEGIN(net_dns_map, NULL, 0)
LROT_FUNCENTRY( setdnsserver, net_setdnsserver )
LROT_FUNCENTRY( getdnsserver, net_getdnsserver )
LROT_FUNCENTRY( resolve, net_dns_static )
LROT_END( net_dns, net_dns, 0 )
LROT_END(net_dns_map, NULL, 0)
LROT_BEGIN(net)
#ifdef TLS_MODULE_PRESENT
extern LROT_TABLE(tls_cert);
#endif
LROT_BEGIN(net, NULL, 0)
LROT_FUNCENTRY( createServer, net_createServer )
LROT_FUNCENTRY( createConnection, net_createConnection )
LROT_FUNCENTRY( createUDPSocket, net_createUDPSocket )
LROT_FUNCENTRY( ifinfo, net_ifinfo )
LROT_FUNCENTRY( multicastJoin, net_multicastJoin )
LROT_FUNCENTRY( multicastLeave, net_multicastLeave )
LROT_TABENTRY( dns, net_dns )
LROT_END( net, net, 0 )
LROT_TABENTRY( dns, net_dns_map )
#ifdef TLS_MODULE_PRESENT
LROT_TABENTRY( cert, tls_cert )
#endif
LROT_NUMENTRY( TCP, TYPE_TCP )
LROT_NUMENTRY( UDP, TYPE_UDP )
LROT_END(net, NULL, 0)
int luaopen_net( lua_State *L ) {
......
// Module for interfacing with system
#include "module.h"
#include "lauxlib.h"
#include "ldebug.h"
#include "ldo.h"
#include "lfunc.h"
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
#include "legc.h"
#include "lmem.h"
#include "lopcodes.h"
#include "lstring.h"
#include "lundump.h"
#include "platform.h"
#if LUA_VERSION_NUM == 501
#include "lflash.h"
#endif
#include <stdint.h>
#include <string.h>
#include "driver/uart.h"
#include "user_interface.h"
#include "flash_api.h"
#include "vfs.h"
......@@ -29,6 +21,16 @@
#define CPU80MHZ 80
#define CPU160MHZ 160
// Lua: startupcommand(string)
static int node_startupcommand( lua_State* L ) {
size_t l, lrcr;
const char *cmd = luaL_checklstring(L, 1, &l);
lrcr = platform_rcr_write(PLATFORM_RCR_INITSTR, cmd, l+1);
lua_pushboolean(L, lrcr == ~0 ? 0 : 1);
return 1;
}
// Lua: restart()
static int node_restart( lua_State* L )
{
......@@ -118,6 +120,16 @@ static int node_sleep( lua_State* L )
return luaL_error(L, "node.sleep() is unavailable");
}
#endif //PMSLEEP_ENABLE
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 add_string_field( lua_State* L, const char *s, const char *name) {
lua_pushstring(L, s);
lua_setfield(L, -2, name);
}
static int node_info( lua_State* L )
{
const char* options[] = {"hw", "sw_version", "build_config", "legacy", NULL};
......@@ -126,49 +138,32 @@ static int node_info( lua_State* L )
switch (option) {
case 0: { // hw
lua_createtable (L, 0, 5);
int table_index = lua_gettop(L);
lua_pushinteger(L, system_get_chip_id()); // chip id
lua_setfield(L, table_index, "chip_id");
lua_pushinteger(L, spi_flash_get_id()); // flash id
lua_setfield(L, table_index, "flash_id");
lua_pushinteger(L, flash_rom_get_size_byte() / 1024); // flash size in KB
lua_setfield(L, table_index, "flash_size");
lua_pushinteger(L, flash_rom_get_mode());
lua_setfield(L, table_index, "flash_mode");
lua_pushinteger(L, flash_rom_get_speed());
lua_setfield(L, table_index, "flash_speed");
add_int_field(L, system_get_chip_id(), "chip_id");
add_int_field(L, spi_flash_get_id(), "flash_id");
add_int_field(L, flash_rom_get_size_byte() / 1024, "flash_size");
add_int_field(L, flash_rom_get_mode(), "flash_mode");
add_int_field(L, flash_rom_get_speed(), "flash_speed");
return 1;
}
case 1: { // sw_version
lua_createtable (L, 0, 7);
int table_index = lua_gettop(L);
lua_pushinteger(L, NODE_VERSION_MAJOR);
lua_setfield(L, table_index, "node_version_major");
lua_pushinteger(L, NODE_VERSION_MINOR);
lua_setfield(L, table_index, "node_version_minor");
lua_pushinteger(L, NODE_VERSION_REVISION);
lua_setfield(L, table_index, "node_version_revision");
lua_pushstring(L, BUILDINFO_BRANCH);
lua_setfield(L, table_index, "git_branch");
lua_pushstring(L, BUILDINFO_COMMIT_ID);
lua_setfield(L, table_index, "git_commit_id");
lua_pushstring(L, BUILDINFO_RELEASE);
lua_setfield(L, table_index, "git_release");
lua_pushstring(L, BUILDINFO_RELEASE_DTS);
lua_setfield(L, table_index, "git_commit_dts");
lua_createtable (L, 0, 7);
add_int_field(L, NODE_VERSION_MAJOR, "node_version_major");
add_int_field(L, NODE_VERSION_MINOR, "node_version_minor");
add_int_field(L, NODE_VERSION_REVISION, "node_version_revision");
add_string_field(L, BUILDINFO_BRANCH, "git_branch");
add_string_field(L, BUILDINFO_COMMIT_ID, "git_commit_id");
add_string_field(L, BUILDINFO_RELEASE, "git_release");
add_string_field(L, BUILDINFO_RELEASE_DTS, "git_commit_dts");
return 1;
}
case 2: { // build_config
lua_createtable (L, 0, 4);
int table_index = lua_gettop(L);
lua_pushboolean(L, BUILDINFO_SSL);
lua_setfield(L, table_index, "ssl");
lua_setfield(L, -2, "ssl");
lua_pushnumber(L, BUILDINFO_LFS_SIZE);
lua_setfield(L, table_index, "lfs_size");
lua_pushstring(L, BUILDINFO_MODULES);
lua_setfield(L, table_index, "modules");
lua_pushstring(L, BUILDINFO_BUILD_TYPE);
lua_setfield(L, table_index, "number_type");
lua_setfield(L, -2, "lfs_size");
add_string_field(L, BUILDINFO_MODULES, "modules");
add_string_field(L, BUILDINFO_BUILD_TYPE, "number_type");
return 1;
}
default:
......@@ -232,7 +227,7 @@ static int node_input( lua_State* L ) {
luaL_checkstring(L, 1);
lua_getfield(L, LUA_REGISTRYINDEX, "stdin");
lua_rawgeti(L, -1, 1); /* get the pipe_write func from stdin[1] */
lua_insert(L, -2); /* and move above the pipe ref */
lua_insert(L, -2); /* and move above the pipe ref */
lua_pushvalue(L, 1);
lua_call(L, 2, 0); /* stdin:write(line) */
return 0;
......@@ -240,22 +235,22 @@ static int node_input( lua_State* L ) {
static int serial_debug = 1;
void output_redirect(const char *str) {
void output_redirect(const char *str, size_t l) {
lua_State *L = lua_getstate();
int n = lua_gettop(L);
lua_pushliteral(L, "stdout");
lua_rawget(L, LUA_REGISTRYINDEX); /* fetch reg.stdout */
if (lua_istable(L, -1)) { /* reg.stdout is pipe */
if (serial_debug) {
uart0_sendStr(str);
uart0_sendStrn(str, l);
}
lua_rawgeti(L, -1, 1); /* get the pipe_write func from stdout[1] */
lua_insert(L, -2); /* and move above the pipe ref */
lua_pushstring(L, str);
lua_insert(L, -2); /* and move above the pipe ref */
lua_pushlstring(L, str, l);
lua_call(L, 2, 0); /* Reg.stdout:write(str) */
} else { /* reg.stdout == nil */
uart0_sendStr(str);
uart0_sendStrn(str, l);
}
lua_settop(L, n); /* Make sure all code paths leave stack unchanged */
}
......@@ -267,9 +262,9 @@ static int node_output( lua_State* L )
{
serial_debug = (lua_isnumber(L, 2) && lua_tointeger(L, 2) == 0) ? 0 : 1;
lua_settop(L, 1);
if (lua_isanyfunction(L, 1)) {
lua_pushlightfunction(L, &pipe_create);
lua_insert(L, 1);
if (lua_isfunction(L, 1)) {
lua_pushcfunction(L, pipe_create);
lua_insert(L, 1);
lua_pushinteger(L, LUA_TASK_MEDIUM);
lua_call(L, 2, 1); /* T[1] = pipe.create(CB, medium_priority) */
} else { // remove the stdout pipe
......@@ -278,7 +273,7 @@ static int node_output( lua_State* L )
serial_debug = 1;
}
lua_pushliteral(L, "stdout");
lua_insert(L, 1);
lua_insert(L, 1);
lua_rawset(L, LUA_REGISTRYINDEX); /* Reg.stdout = nil or pipe */
return 0;
}
......@@ -297,7 +292,12 @@ static int writer(lua_State* L, const void* p, size_t size, void* u)
return 0;
}
#define toproto(L,i) (clvalue(L->top+(i))->l.p)
#if LUA_VERSION_NUM == 501
#undef lua_dump
#define lua_dump lua_dumpEx
#define getproto(o) (clvalue(o)->l.p)
#endif
// Lua: compile(filename) -- compile lua file into lua bytecode, and save to .lc
static int node_compile( lua_State* L )
{
......@@ -325,7 +325,6 @@ static int node_compile( lua_State* L )
return luaL_error(L, lua_tostring(L, -1));
}
f = toproto(L, -1);
int stripping = 1; /* strip debug information? */
......@@ -336,9 +335,7 @@ static int node_compile( lua_State* L )
return luaL_error(L, "cannot open/write to file");
}
lua_lock(L);
int result = luaU_dump(L, f, writer, &file_fd, stripping);
lua_unlock(L);
int result = lua_dump(L, writer, &file_fd, stripping);
if (vfs_flush(file_fd) != VFS_RES_OK) {
// overwrite Lua error, like writer() does in case of a file io error
......@@ -371,9 +368,9 @@ static int node_task_post( lua_State* L )
luaL_argcheck(L, priority <= TASK_PRIORITY_HIGH, 1, "invalid priority");
n++;
}
luaL_checkanyfunction(L, n);
luaL_checktype(L, n, LUA_TFUNCTION);
lua_settop(L, n);
(void) luaN_posttask(L, priority);
(void) luaL_posttask(L, priority);
return 0;
}
......@@ -424,7 +421,8 @@ static int node_restore (lua_State *L)
return 0;
}
#ifdef LUA_OPTIMIZE_DEBUG
#if defined(LUA_OPTIMIZE_DEBUG) && LUA_VERSION_NUM == 501
/* node.stripdebug([level[, function]]). 
* level: 1 don't discard debug
* 2 discard Local and Upvalue debug info
......@@ -434,10 +432,11 @@ static int node_restore (lua_State *L)
* If function is omitted, this is the default setting for future compiles
* The function returns an estimated integer count of the bytes stripped.
*/
LUA_API int luaG_stripdebug (lua_State *L, Proto *f, int level, int recv);
static int node_stripdebug (lua_State *L) {
int level;
if (L->top == L->base) {
int n = lua_gettop(L);
if (n == 0) {
lua_pushlightuserdata(L, &luaG_stripdebug );
lua_gettable(L, LUA_REGISTRYINDEX);
if (lua_isnil(L, -1)) {
......@@ -447,25 +446,23 @@ static int node_stripdebug (lua_State *L) {
return 1;
}
level = luaL_checkint(L, 1);
if ((level <= 0) || (level > 3)) luaL_argerror(L, 1, "must in range 1-3");
int level = luaL_checkint(L, 1);
luaL_argcheck(L, level > 0 && level < 4, 1, "must in range 1-3");
if (L->top == L->base + 1) {
if (n == 1) {
/* Store the default level in the registry if no function parameter */
lua_pushlightuserdata(L, &luaG_stripdebug);
lua_pushinteger(L, level);
lua_settable(L, LUA_REGISTRYINDEX);
lua_settop(L,0);
return 0;
}
if (level == 1) {
lua_settop(L,0);
lua_pushinteger(L, 0);
return 1;
}
if (!lua_isfunction(L, 2)) {
if (lua_isnumber(L, 2)) {
int scope = luaL_checkint(L, 2);
if (scope > 0) {
/* if the function parameter is a +ve integer then climb to find function */
......@@ -477,16 +474,15 @@ static int node_stripdebug (lua_State *L) {
}
}
if(!lua_isfunction(L, 2) || lua_iscfunction(L, -1)) luaL_argerror(L, 2, "must be a Lua Function");
// lua_lock(L);
Proto *f = clvalue(L->base + 1)->l.p;
// lua_unlock(L);
lua_settop(L,0);
luaL_argcheck(L, lua_isfunction(L, 2), 2, "must be a Lua Function");
Proto *f = getproto(L->ci->func + 1);
lua_pushinteger(L, luaG_stripdebug(L, f, level, 1));
return 1;
}
#endif
#if LUA_VERSION_NUM == 501
// Lua: node.egc.setmode( mode, [param])
// 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
......@@ -498,10 +494,9 @@ 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_MEM_LIMIT) || limit!=0, 1, "limit must be non-zero");
legc_set_mode( L, mode, limit );
lua_setegcmode( L, mode, limit );
return 0;
}
// totalallocated, estimatedused = node.egc.meminfo()
static int node_egc_meminfo(lua_State *L) {
global_State *g = G(L);
......@@ -509,7 +504,7 @@ static int node_egc_meminfo(lua_State *L) {
lua_pushinteger(L, g->estimate);
return 2;
}
#endif
//
// Lua: osprint(true/false)
// Allows you to turn on the native Espressif SDK printing
......@@ -618,12 +613,12 @@ static int node_writercr (lua_State *L) {
typedef enum pt_t { lfs_addr=0, lfs_size, spiffs_addr, spiffs_size, max_pt} pt_t;
LROT_BEGIN(pt)
LROT_BEGIN(pt_map, NULL, 0)
LROT_NUMENTRY( lfs_addr, lfs_addr )
LROT_NUMENTRY( lfs_size, lfs_size )
LROT_NUMENTRY( spiffs_addr, spiffs_addr )
LROT_NUMENTRY( spiffs_size, spiffs_size )
LROT_END( pt, NULL, 0 )
LROT_END(pt_map, NULL, 0)
// Lua: ptinfo = node.getpartitiontable()
......@@ -634,7 +629,7 @@ static int node_getpartitiontable (lua_State *L) {
lua_settop(L, 0);
lua_createtable (L, 0, max_pt); /* at index 1 */
lua_pushrotable(L, (void*)pt_map); /* at index 2 */
lua_pushrotable(L, LROT_TABLEREF(pt_map)); /* at index 2 */
lua_pushnil(L); /* first key at index 3 */
while (lua_next(L, 2) != 0) { /* key at index 3, and v at index 4 */
lua_pushvalue(L, 3); /* dup key to index 5 */
......@@ -676,8 +671,8 @@ static int node_setpartitiontable (lua_State *L) {
luaL_argcheck(L, lua_istable(L, 1), 1, "must be table");
lua_settop(L, 1);
/* convert input table into 4 option array */
lua_pushrotable(L, (void*)pt_map); /* at index 2 */
lua_pushnil(L); /* first key at index 3 */
lua_pushrotable(L, LROT_TABLEREF(pt_map)); /* at index 2 */
lua_pushnil(L); /* first key at index 3 */
while (lua_next(L, 1) != 0) {
/* 'key' (at index 3) and 'value' (at index 4) */
luaL_argcheck(L, lua_isstring(L, 3) && lua_isnumber(L, 4), 1, "invalid partition setting");
......@@ -769,29 +764,31 @@ static int node_setpartitiontable (lua_State *L) {
// Module function map
LROT_BEGIN(node_egc)
#if LUA_VERSION_NUM == 501
LROT_BEGIN(node_egc, NULL, 0)
LROT_FUNCENTRY( meminfo, node_egc_meminfo )
LROT_FUNCENTRY( setmode, node_egc_setmode )
LROT_NUMENTRY( NOT_ACTIVE, EGC_NOT_ACTIVE )
LROT_NUMENTRY( ON_ALLOC_FAILURE, EGC_ON_ALLOC_FAILURE )
LROT_NUMENTRY( ON_MEM_LIMIT, EGC_ON_MEM_LIMIT )
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_NUMENTRY( LOW_PRIORITY, TASK_PRIORITY_LOW )
LROT_NUMENTRY( MEDIUM_PRIORITY, TASK_PRIORITY_MEDIUM )
LROT_NUMENTRY( HIGH_PRIORITY, TASK_PRIORITY_HIGH )
LROT_END( node_task, NULL, 0 )
LROT_END(node_task, NULL, 0)
LROT_BEGIN(node)
LROT_BEGIN(node, NULL, 0)
LROT_FUNCENTRY( heap, node_heap )
LROT_FUNCENTRY( info, node_info )
LROT_TABENTRY( task, node_task )
LROT_FUNCENTRY( flashreload, luaN_reload_reboot )
LROT_FUNCENTRY( flashindex, luaN_index )
LROT_FUNCENTRY( startupcommand, node_startupcommand )
LROT_FUNCENTRY( restart, node_restart )
LROT_FUNCENTRY( dsleep, node_deepsleep )
LROT_FUNCENTRY( dsleepMax, dsleepMax )
......@@ -818,10 +815,12 @@ LROT_BEGIN(node)
LROT_FUNCENTRY( bootreason, node_bootreason )
LROT_FUNCENTRY( restore, node_restore )
LROT_FUNCENTRY( random, node_random )
#ifdef LUA_OPTIMIZE_DEBUG
#if LUA_VERSION_NUM == 501 && defined(LUA_OPTIMIZE_DEBUG)
LROT_FUNCENTRY( stripdebug, node_stripdebug )
#endif
#if LUA_VERSION_NUM == 501
LROT_TABENTRY( egc, node_egc )
#endif
#ifdef DEVELOPMENT_TOOLS
LROT_FUNCENTRY( osprint, node_osprint )
#endif
......@@ -830,7 +829,7 @@ LROT_BEGIN(node)
// Combined to dsleep(us, option)
// LROT_FUNCENTRY( dsleepsetoption, node_deepsleep_setoption )
LROT_END( node, NULL, 0 )
LROT_END(node, NULL, 0)
NODEMCU_MODULE(NODE, "node", node, NULL);
......@@ -282,7 +282,7 @@ static int ow_crc16( lua_State *L )
#endif
// Module function map
LROT_BEGIN(ow)
LROT_BEGIN(ow, NULL, 0)
LROT_FUNCENTRY( setup, ow_setup )
LROT_FUNCENTRY( reset, ow_reset )
LROT_FUNCENTRY( skip, ow_skip )
......@@ -304,7 +304,7 @@ LROT_BEGIN(ow)
LROT_FUNCENTRY( crc16, ow_crc16 )
#endif
#endif
LROT_END( ow, NULL, 0 )
LROT_END(ow, NULL, 0)
NODEMCU_MODULE(OW, "ow", ow, NULL);
......@@ -146,8 +146,7 @@ static int pcm_drv_on( lua_State *L )
event = luaL_checklstring( L, 2, &len );
if ((lua_type( L, 3 ) == LUA_TFUNCTION) ||
(lua_type( L, 3 ) == LUA_TLIGHTFUNCTION)) {
if (lua_isfunction(L, 3)) {
lua_pushvalue( L, 3 ); // copy argument (func) to the top of stack
is_func = TRUE;
}
......@@ -229,19 +228,20 @@ static int pcm_new( lua_State *L )
}
LROT_BEGIN(pcm_driver)
LROT_BEGIN(pcm_driver, NULL, LROT_MASK_GC_INDEX)
LROT_FUNCENTRY( __gc, pcm_drv_free )
LROT_TABENTRY( __index, pcm_driver )
LROT_FUNCENTRY( play, pcm_drv_play )
LROT_FUNCENTRY( pause, pcm_drv_pause )
LROT_FUNCENTRY( stop, pcm_drv_stop )
LROT_FUNCENTRY( close, pcm_drv_close )
LROT_FUNCENTRY( on, pcm_drv_on )
LROT_FUNCENTRY( __gc, pcm_drv_free )
LROT_TABENTRY( __index, pcm_driver )
LROT_END( pcm_driver, pcm_driver, LROT_MASK_GC_INDEX )
LROT_END(pcm_driver, NULL, LROT_MASK_GC_INDEX)
// Module function map
LROT_BEGIN(pcm)
LROT_BEGIN(pcm, NULL, 0)
LROT_FUNCENTRY( new, pcm_new )
LROT_NUMENTRY( SD, PCM_DRIVER_SD )
LROT_NUMENTRY( RATE_1K, PCM_RATE_1K )
......@@ -252,7 +252,7 @@ LROT_BEGIN(pcm)
LROT_NUMENTRY( RATE_10K, PCM_RATE_10K )
LROT_NUMENTRY( RATE_12K, PCM_RATE_12K )
LROT_NUMENTRY( RATE_16K, PCM_RATE_16K )
LROT_END( pcm, NULL, 0 )
LROT_END(pcm, NULL, 0)
int luaopen_pcm( lua_State *L ) {
......
......@@ -84,7 +84,7 @@ static int perf_start(lua_State *L)
d->bucket_count = bins;
if (data) {
lua_unref(L, data->ref);
luaL_unref(L, LUA_REGISTRYINDEX, data->ref);
}
data = d;
......@@ -93,7 +93,7 @@ static int perf_start(lua_State *L)
if (!platform_hw_timer_init(TIMER_OWNER, FRC1_SOURCE, TRUE)) {
// Failed to init the timer
data = NULL;
lua_unref(L, d->ref);
luaL_unref(L, LUA_REGISTRYINDEX, d->ref);
luaL_error(L, "Unable to initialize timer");
}
......@@ -130,15 +130,15 @@ static int perf_stop(lua_State *L)
lua_pushnumber(L, 1 << d->bucket_shift);
lua_unref(L, d->ref);
luaL_unref(L, LUA_REGISTRYINDEX, d->ref);
return 4;
}
LROT_BEGIN(perf)
LROT_BEGIN(perf, NULL, 0)
LROT_FUNCENTRY( start, perf_start )
LROT_FUNCENTRY( stop, perf_stop )
LROT_END( perf, NULL, 0 )
LROT_END(perf, NULL, 0)
NODEMCU_MODULE(PERF, "perf", perf, NULL);
......@@ -15,16 +15,16 @@
** 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
** 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.
** 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
** 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.
** system.
**
** Implementation Notes:
**
......@@ -33,10 +33,10 @@
** 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
** - 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
** 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
......@@ -50,7 +50,6 @@
#include "lauxlib.h"
#include <string.h>
#include "platform.h"
#include "lstate.h"
#define INVALID_LEN ((unsigned)-1)
......@@ -61,7 +60,6 @@ typedef struct buffer {
char buf[LUAL_BUFFERSIZE];
} buffer_t;
LROT_TABLE(pipe_meta)
#define AT_TAIL 0x00
#define AT_HEAD 0x01
......@@ -70,6 +68,7 @@ LROT_TABLE(pipe_meta)
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]
......@@ -86,15 +85,16 @@ static buffer_t *checkPipeTable (lua_State *L, int tbl, int flags) {
lua_rawgeti(L, tbl, i); /* and fetch UD */
ud = checkPipeUD(L, -1);
}
lua_settop(L, m);
lua_settop(L, m);
return ud; /* and return ptr to buffer_t rec */
}
}
luaL_typerror(L, tbl, "pipe table");
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 */
......@@ -103,9 +103,6 @@ static buffer_t *checkPipeUD (lua_State *L, int ndx) { // [-0, +0, v]
return ud; /* and return ptr to buffer_t rec */
}
}
if (!lua_istable(L,ndx))
luaL_typerror(L, ndx, "pipeUD"); /* NORETURN error */
return NULL; /* keep compiler happy */
}
static buffer_t *newPipeUD(lua_State *L, int ndx, int n) { // [-0,+0,-]
......@@ -155,16 +152,16 @@ static char getsize_delim (lua_State *L, int ndx, int *len) { // [-0, +0, v]
#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.
** 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
** 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);
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)) {
......@@ -177,12 +174,12 @@ static int pipe_write_and_read_poster (lua_State *L) {
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] */
luaN_posttask(L, (int) lua_tointeger(L, UVprio)); /* and repost task */
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 */
/* 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);
......@@ -192,13 +189,13 @@ static int pipe_write_and_read_poster (lua_State *L) {
/*
* 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:
* occurs:
* - true = repost
* - false = don't repost
* - nil = only repost if there has been a write update.
* - nil = only repost if there has been a write update.
*/
if (lua_isboolean(L,-1)) {
repost = (lua_toboolean(L, -1) == true &&
repost = (lua_toboolean(L, -1) == true &&
lua_objlen(L, UVpipe) > 1) ? CB_WRITE_UPDATED : 0;
} else {
repost = state & CB_WRITE_UPDATED;
......@@ -209,7 +206,7 @@ static int pipe_write_and_read_poster (lua_State *L) {
if (repost) {
lua_rawgeti(L, UVpipe, 1); /* Get CB ref from pipe[1] */
luaN_posttask(L, (int) lua_tointeger(L, UVprio)); /* and repost task */
luaL_posttask(L, (int) lua_tointeger(L, UVprio)); /* and repost task */
}
}
return 0;
......@@ -224,7 +221,7 @@ int pipe_create(lua_State *L) {
lua_settop(L, 2); /* fix stack sze as 2 */
if (!lua_isnil(L, 1)) {
luaL_checkanyfunction(L, 1); /* non-nil arg1 must be a function */
luaL_checktype(L, 1, LUA_TFUNCTION); /* non-nil arg1 must be a function */
if (lua_isnil(L, 2)) {
prio = PLATFORM_TASK_PRIORITY_MEDIUM;
} else {
......@@ -237,7 +234,7 @@ int pipe_create(lua_State *L) {
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 */
......@@ -249,7 +246,7 @@ int pipe_create(lua_State *L) {
// len = #pipeobj[i]
static int pipe__len (lua_State *L) {
if (lua_type(L, 1) == LUA_TTABLE) {
if (lua_istable(L, 1)) {
lua_pushinteger(L, lua_objlen(L, 1));
} else {
buffer_t *ud = checkPipeUD(L, 1);
......@@ -304,7 +301,7 @@ int pipe_read(lua_State *L) {
/* 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_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) {
......@@ -340,14 +337,14 @@ int pipe_unread(lua_State *L) {
/* If the current UD is full insert a new UD at T[2] */
int i, nUD = lua_objlen(L, 1);
for (i = nUD; i > 0; i--) { /* for i = nUD-1,1,-1 */
lua_rawgeti(L, 1, i); lua_rawseti(L, 1, i+1); /* T[i+1] = T[i] */
lua_rawgeti(L, 1, i); lua_rawseti(L, 1, i+1); /* T[i+1] = T[i] */
}
ud = newPipeUD(L, 1, 1);
used = 0; lrem = LUAL_BUFFERSIZE;
} else if (ud->start < l) {
/* If the unread can't fit it before the start then shift content to end */
memmove(ud->buf + lrem,
memmove(ud->buf + lrem,
ud->buf + ud->start, used); /* must be memmove not cpy */
ud->start = lrem; ud->end = LUAL_BUFFERSIZE;
}
......@@ -357,14 +354,14 @@ int pipe_unread(lua_State *L) {
/* If we've got here then the remaining string is strictly longer than the */
/* remaining buffer space, so top off the buffer before looping around again */
l -= lrem;
l -= lrem;
memcpy(ud->buf, s + l, lrem);
ud->start = 0;
} while(1);
/* Copy any residual tail to the UD buffer. Note that this is l>0 and */
ud->start -= l;
ud->start -= l;
memcpy(ud->buf + ud->start, s, l);
return 0;
}
......@@ -430,13 +427,13 @@ static int pipe_reader(lua_State *L) {
return 1;
}
LROT_BEGIN(pipe_funcs)
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_END( pipe_meta, NULL, LROT_MASK_INDEX )
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) {
......@@ -452,14 +449,14 @@ static int pipe__index(lua_State *L) {
return 1;
}
LROT_BEGIN(pipe_meta)
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_END(pipe_meta, NULL, LROT_MASK_INDEX)
LROT_BEGIN(pipe)
LROT_BEGIN(pipe, NULL, 0)
LROT_FUNCENTRY( create, pipe_create )
LROT_END( lb, NULL, 0 )
LROT_END(pipe, NULL, 0)
NODEMCU_MODULE(PIPE, "pipe", pipe, NULL);
......@@ -128,7 +128,7 @@ int lpwm_open( lua_State *L ) {
}
// Module function map
LROT_BEGIN(pwm)
LROT_BEGIN(pwm, NULL, 0)
LROT_FUNCENTRY( setup, lpwm_setup )
LROT_FUNCENTRY( close, lpwm_close )
LROT_FUNCENTRY( start, lpwm_start )
......@@ -137,7 +137,7 @@ LROT_BEGIN(pwm)
LROT_FUNCENTRY( getclock, lpwm_getclock )
LROT_FUNCENTRY( setduty, lpwm_setduty )
LROT_FUNCENTRY( getduty, lpwm_getduty )
LROT_END( pwm, NULL, 0 )
LROT_END(pwm, NULL, 0)
NODEMCU_MODULE(PWM, "pwm", pwm, lpwm_open);
/*
* Software PWM using soft-interrupt timer1.
* Software PWM using soft-interrupt timer1.
* Supports higher frequencies compared to Espressif provided one.
*
* Nikolay Fiykov
......@@ -127,7 +127,7 @@ static int lpwm2_start(lua_State *L) {
}
// Module function map
LROT_BEGIN(pwm2)
LROT_BEGIN(pwm2, NULL, 0)
LROT_FUNCENTRY(setup_pin_hz, lpwm2_setup_pin_hz)
LROT_FUNCENTRY(setup_pin_sec, lpwm2_setup_pin_sec)
LROT_FUNCENTRY(release_pin, lpwm2_release_pin)
......
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