Commit 17df207a authored by Johny Mattsson's avatar Johny Mattsson
Browse files

Port Terry's Lua 5.1 + 5.3 work from the esp8266 branch.

Changes have been kept to a minimum, but a serious chunk of work was
needed to move from 8266isms to IDFisms.

Some things got refactored into components/lua/common, in particular
the LFS location awareness.

As part of this work I also evicted our partition table manipulation
code, as with the current IDF it kept breaking checksums and rendering
things unbootable, which is the opposite of helpful (which was the
original intent behind it).

The uart module got relocated from base_nodemcu to the modules component
properly, after I worked out how to force its inclusion using Kconfig alone.
parent f123d462
......@@ -102,6 +102,12 @@ menu "NodeMCU modules"
requires a partition table with at least two OTA partitions, plus
the OTA data partition. See the IDF documentation for details.
config NODEMCU_CMODULE_PIPE
bool "Pipe module"
default "y"
help
Includes the pipe module (required by our Lua VM).
config NODEMCU_CMODULE_QRCODEGEN
bool "QR Code Generator module"
default "n"
......@@ -182,4 +188,10 @@ menu "NodeMCU modules"
help
Includes the time module.
config NODEMCU_CMODULE_UART
bool "UART module"
default y
help
Includes the UART module (required by our Lua VM).
endmenu
......@@ -60,7 +60,7 @@ static int read_hall_sensor( lua_State *L )
}
// Module function map
LROT_BEGIN(adc)
LROT_BEGIN(adc, NULL, 0)
LROT_FUNCENTRY( setwidth, adc_set_width )
LROT_FUNCENTRY( setup, adc_setup )
LROT_FUNCENTRY( read, adc_read )
......
......@@ -119,7 +119,7 @@ static int bit_clear( lua_State* L )
return 1;
}
LROT_BEGIN(bit)
LROT_BEGIN(bit, NULL, 0)
LROT_FUNCENTRY( bnot, bit_bnot )
LROT_FUNCENTRY( band, bit_band )
LROT_FUNCENTRY( bor, bit_bor )
......
......@@ -216,7 +216,7 @@ static int send_hci_command (lua_State *L, uint8_t *data, unsigned len)
if (lua_gettop (L) > 0 && !lua_isnil (L, -1))
{
cmd_q[i].cmd = cmd;
luaL_checkanyfunction (L, -1);
luaL_checkfunction (L, -1);
lua_pushvalue (L, -1);
cmd_q[i].cb_ref = luaL_ref (L, LUA_REGISTRYINDEX);
}
......@@ -424,7 +424,7 @@ static int lbthci_adv_setparams (lua_State *L)
uint8_t adv_chan_map = ADV_CHAN_ALL;
uint8_t adv_filter_pol = ADV_FILTER_NONE;
luaL_checkanytable (L, 1);
luaL_checktable (L, 1);
lua_settop (L, 2); // Pad a nil into the function slot if necessary
get_opt_field_int (1, adv_interval_min, "interval_min");
......@@ -501,7 +501,7 @@ static int lbthci_scan_setparams (lua_State *L)
uint8_t own_addr_type = 0;
uint8_t filter_policy = 0;
luaL_checkanytable (L, 1);
luaL_checktable (L, 1);
lua_settop (L, 2); // Pad a nil into the function slot if necessary
get_opt_field_int (1, scan_mode, "mode");
......@@ -557,7 +557,7 @@ static int lbthci_rawhci (lua_State *L)
}
LROT_BEGIN(bthci_adv)
LROT_BEGIN(bthci_adv, NULL, 0)
LROT_FUNCENTRY( enable, lbthci_adv_enable )
LROT_FUNCENTRY( setdata, lbthci_adv_setdata )
LROT_FUNCENTRY( setparams, lbthci_adv_setparams )
......@@ -576,14 +576,14 @@ LROT_BEGIN(bthci_adv)
LROT_END(bthci_adv, NULL, 0)
LROT_BEGIN(bthci_scan)
LROT_BEGIN(bthci_scan, NULL, 0)
LROT_FUNCENTRY( enable, lbthci_scan )
LROT_FUNCENTRY( setparams, lbthci_scan_setparams )
LROT_FUNCENTRY( on, lbthci_scan_on )
LROT_END(bthci_scan, NULL, 0)
LROT_BEGIN(bthci)
LROT_BEGIN(bthci, NULL, 0)
LROT_FUNCENTRY( rawhci, lbthci_rawhci )
LROT_FUNCENTRY( reset, lbthci_reset )
LROT_TABENTRY ( adv, bthci_adv )
......
......@@ -2,6 +2,9 @@
#define modules_common_h
#include "lua.h"
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
/* Some common code shared between modules */
......
......@@ -153,22 +153,22 @@ static int crypto_hash_gc(lua_State* L) {
}
// The following table defines methods of the hasher object
LROT_BEGIN(crypto_hasher)
LROT_FUNCENTRY(update, crypto_hash_update)
LROT_FUNCENTRY(finalize, crypto_hash_finalize)
LROT_BEGIN(crypto_hasher, NULL, 0)
LROT_FUNCENTRY(__gc, crypto_hash_gc)
LROT_TABENTRY(__index, crypto_hasher)
LROT_FUNCENTRY(update, crypto_hash_update)
LROT_FUNCENTRY(finalize, crypto_hash_finalize)
LROT_END(crypto_hasher, NULL, 0)
// This table defines the functions of the crypto module:
LROT_BEGIN(crypto)
LROT_BEGIN(crypto, NULL, 0)
LROT_FUNCENTRY(new_hash, crypto_new_hash)
LROT_FUNCENTRY(new_hmac, crypto_new_hmac)
LROT_END(crypto, NULL, 0)
// luaopen_crypto is the crypto module initialization function
int luaopen_crypto(lua_State* L) {
luaL_rometatable(L, HASH_METATABLE, (void*)crypto_hasher_map); // create metatable for crypto.hash
luaL_rometatable(L, HASH_METATABLE, LROT_TABLEREF(crypto_hasher)); // create metatable for crypto.hash
return 0;
}
......
......@@ -91,7 +91,7 @@ static int ldht_read2x( lua_State *L )
}
LROT_BEGIN(dht)
LROT_BEGIN(dht, NULL, 0)
LROT_FUNCENTRY( read11, ldht_read11 )
LROT_FUNCENTRY( read2x, ldht_read2x )
LROT_NUMENTRY ( OK, LDHT_OK )
......
......@@ -116,7 +116,7 @@ static uint8_t *fromHex ( lua_State* L, const uint8_t *msg, size_t *len){
} else if (*p >= 'A' && *p <= 'F') {
b = *p++ - ('A' - 10);
} else {
luaM_freearray(L, out, *len, uint8_t);
luaN_freearray(L, out, *len);
luaL_error (L, "Invalid hex string");
__builtin_unreachable ();
}
......@@ -140,7 +140,7 @@ static int do_func (lua_State *L, uint8_t * (*conv_func)(lua_State *, const uint
if (output) {
lua_pushlstring(L, (char *)output, len);
luaM_freearray(L, output, len, uint8_t);
luaN_freearray(L, output, len);
} else {
lua_pushstring(L, "");
}
......@@ -156,7 +156,7 @@ static int do_func (lua_State *L, uint8_t * (*conv_func)(lua_State *, const uint
DECLARE_FUNCTION(toHex);
// Module function map
LROT_BEGIN(encoder)
LROT_BEGIN(encoder, NULL, 0)
LROT_FUNCENTRY(fromBase64, encoder_fromBase64)
LROT_FUNCENTRY(toBase64, encoder_toBase64)
LROT_FUNCENTRY(fromHex, encoder_fromHex)
......
......@@ -86,8 +86,7 @@ static int file_on(lua_State *L)
case ON_RTC:
luaL_unref(L, LUA_REGISTRYINDEX, rtc_cb_ref);
if ((lua_type(L, 2) == LUA_TFUNCTION) ||
(lua_type(L, 2) == LUA_TLIGHTFUNCTION)) {
if (lua_isfunction(L, 2)) {
lua_pushvalue(L, 2); // copy argument (func) to the top of stack
rtc_cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
vfs_register_rtc_cb(file_rtc_cb);
......@@ -430,7 +429,7 @@ static int file_g_read( lua_State* L, int n, int16_t end_char, int fd )
}
if (heap_mem) {
luaM_freearray(L, heap_mem, bufsize, char);
luaN_freearray(L, heap_mem, bufsize);
}
if (err){
......@@ -550,7 +549,7 @@ static int file_chdir( lua_State *L )
}
#endif
LROT_BEGIN(file_obj)
LROT_BEGIN(file_obj, NULL, 0)
LROT_FUNCENTRY( close, file_close )
LROT_FUNCENTRY( read, file_read )
LROT_FUNCENTRY( readline, file_readline )
......@@ -563,7 +562,7 @@ LROT_BEGIN(file_obj)
LROT_END(file_obj, NULL, 0)
// Module function map
LROT_BEGIN(file)
LROT_BEGIN(file, NULL, 0)
LROT_FUNCENTRY( list, file_list )
LROT_FUNCENTRY( open, file_open )
LROT_FUNCENTRY( close, file_close )
......@@ -589,7 +588,7 @@ LROT_BEGIN(file)
LROT_END(file, NULL, 0)
int luaopen_file( lua_State *L ) {
luaL_rometatable( L, "file.obj", (void *)file_obj_map );
luaL_rometatable( L, "file.obj", LROT_TABLEREF(file_obj));
return 0;
}
......
......@@ -79,7 +79,7 @@ static int lgpio_config (lua_State *L)
luaL_checkstack (L, 5, "out of mem");
for (int i = 1; i <= n; ++i)
{
luaL_checkanytable (L, i);
luaL_checktable (L, i);
gpio_config_t cfg;
cfg.intr_type = GPIO_INTR_DISABLE;
......@@ -141,7 +141,7 @@ static int lgpio_trig (lua_State *L)
int gpio = luaL_checkint (L, 1);
int intr_type = luaL_optint (L, 2, GPIO_INTR_DISABLE);
if (!lua_isnoneornil (L, 3))
luaL_checkanyfunction (L, 3);
luaL_checkfunction (L, 3);
lua_settop (L, 3);
......@@ -250,7 +250,7 @@ static int nodemcu_gpio_init (lua_State *L)
}
LROT_BEGIN(lgpio)
LROT_BEGIN(lgpio, NULL, 0)
LROT_FUNCENTRY( config, lgpio_config )
LROT_FUNCENTRY( read, lgpio_read )
LROT_FUNCENTRY( trig, lgpio_trig )
......
......@@ -778,7 +778,7 @@ static int http_lapi_post(lua_State *L)
return make_oneshot_request(L, 4); // 4 = callback idx
}
LROT_BEGIN(http)
LROT_BEGIN(http, NULL, 0)
LROT_FUNCENTRY(createConnection, http_lapi_createConnection)
LROT_NUMENTRY (GET, HTTP_METHOD_GET)
LROT_NUMENTRY (POST, HTTP_METHOD_POST)
......@@ -790,7 +790,7 @@ LROT_BEGIN(http)
LROT_FUNCENTRY(post, http_lapi_post)
LROT_END(http, NULL, 0)
LROT_BEGIN(http_context)
LROT_BEGIN(http_context, NULL, 0)
LROT_FUNCENTRY(on, http_lapi_on)
LROT_FUNCENTRY(request, http_lapi_request)
LROT_FUNCENTRY(setmethod, http_lapi_setmethod)
......@@ -805,7 +805,7 @@ LROT_END(http_context, NULL, 0)
static int luaopen_http(lua_State *L)
{
luaL_rometatable(L, http_context_mt, (void *)http_context_map);
luaL_rometatable(L, http_context_mt, LROT_TABLEREF(http_context));
lhttp_request_task_id = task_get_id(lhttp_request_task);
lhttp_event_task_id = task_get_id(lhttp_event_task);
return 0;
......
......@@ -210,7 +210,7 @@ static int i2c_read( lua_State *L )
}
}
LROT_BEGIN(i2c)
LROT_BEGIN(i2c, NULL, 0)
LROT_FUNCENTRY( setup, i2c_setup )
LROT_FUNCENTRY( start, i2c_start )
LROT_FUNCENTRY( stop, i2c_stop )
......
......@@ -29,7 +29,7 @@ int li2c_hw_master_transfer( lua_State *L );
// ***************************************************************************
// Hardware slave prototypes
//
LROT_EXTERN(li2c_slave);
extern LROT_TABLE(li2c_slave);
void li2c_hw_slave_init( lua_State *L );
......
......@@ -319,7 +319,7 @@ int li2c_hw_master_transfer( lua_State *L )
luaL_error( L, "no commands scheduled" );
stack++;
if (lua_isfunction( L, stack ) || lua_islightfunction( L, stack )) {
if (lua_isfunction( L, stack )) {
lua_pushvalue( L, stack ); // copy argument (func) to the top of stack
luaL_unref( L, LUA_REGISTRYINDEX, job->cb_ref );
job->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
......
......@@ -173,7 +173,7 @@ static int li2c_slave_setup( lua_State *L )
ud->port = port;
int stack = 1;
luaL_checkanytable( L, ++stack );
luaL_checktable( L, ++stack );
lua_settop( L, stack );
i2c_config_t cfg;
......@@ -305,7 +305,7 @@ static int li2c_slave_on( lua_State *L )
luaL_unref( L, LUA_REGISTRYINDEX, ud->receivedcb_ref );
++stack;
if (lua_isfunction( L, stack ) || lua_islightfunction( L, stack )) {
if (lua_isfunction( L, stack )) {
lua_pushvalue( L, stack ); // copy argument (func) to the top of stack
ud->receivedcb_ref = luaL_ref( L, LUA_REGISTRYINDEX );
}
......@@ -318,7 +318,7 @@ static int li2c_slave_on( lua_State *L )
}
LROT_PUBLIC_BEGIN(li2c_slave)
LROT_BEGIN(li2c_slave, NULL, 0)
LROT_FUNCENTRY( on, li2c_slave_on )
LROT_FUNCENTRY( setup, li2c_slave_setup )
LROT_FUNCENTRY( send, li2c_slave_send )
......
......@@ -17,7 +17,7 @@ typedef struct {
static int lledc_new_channel( lua_State *L )
{
const int top = lua_gettop(L);
luaL_checkanytable (L, 1);
luaL_checktable (L, 1);
/* Setup timer */
ledc_timer_config_t ledc_timer;
......@@ -231,7 +231,8 @@ static int lledc_set_fade( lua_State *L ) {
}
// Module function map
LROT_BEGIN(ledc_channel)
LROT_BEGIN(ledc_channel, NULL, 0)
LROT_TABENTRY ( __index, ledc_channel )
LROT_FUNCENTRY( getduty, lledc_get_duty )
LROT_FUNCENTRY( setduty, lledc_set_duty )
LROT_FUNCENTRY( getfreq, lledc_get_freq )
......@@ -245,11 +246,9 @@ LROT_BEGIN(ledc_channel)
LROT_FUNCENTRY( fadewithtime, lledc_set_fade_with_time )
LROT_FUNCENTRY( fadewithstep, lledc_set_fade_with_step )
LROT_FUNCENTRY( fade, lledc_set_fade )
LROT_TABENTRY ( __index, ledc_channel )
LROT_END(ledc_channel, NULL, 0)
LROT_BEGIN(ledc)
LROT_BEGIN(ledc, NULL, 0)
LROT_FUNCENTRY( newChannel, lledc_new_channel )
#if SOC_LEDC_SUPPORT_HS_MODE
......@@ -295,7 +294,7 @@ LROT_BEGIN(ledc)
LROT_END(ledc, NULL, 0)
int luaopen_ledc(lua_State *L) {
luaL_rometatable(L, "ledc.channel", (void *)ledc_channel_map); // create metatable for ledc.channel
luaL_rometatable(L, "ledc.channel", LROT_TABLEREF(ledc_channel)); // create metatable for ledc.channel
return 0;
}
......
......@@ -635,7 +635,7 @@ static int mqtt_new(lua_State* L) {
}
// map client methods to functions:
LROT_BEGIN(mqtt_metatable)
LROT_BEGIN(mqtt_metatable, NULL, 0)
LROT_FUNCENTRY(connect, mqtt_connect)
LROT_FUNCENTRY(close, mqtt_close)
LROT_FUNCENTRY(lwt, mqtt_lwt)
......@@ -648,12 +648,12 @@ LROT_BEGIN(mqtt_metatable)
LROT_END(mqtt_metatable, NULL, 0)
// Module function map
LROT_BEGIN(mqtt)
LROT_BEGIN(mqtt, NULL, 0)
LROT_FUNCENTRY(Client, mqtt_new)
LROT_END(mqtt, NULL, 0)
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;
}
......
......@@ -479,7 +479,7 @@ static 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);
......@@ -586,7 +586,7 @@ static 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);
......@@ -620,7 +620,7 @@ static int net_send( lua_State *L ) {
data = luaL_checklstring(L, stack++, &datalen);
if (!data || datalen == 0) return luaL_error(L, "no data to send");
ud->client.num_send = datalen;
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);
......@@ -684,7 +684,7 @@ static 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);
......@@ -890,7 +890,7 @@ static int net_dns_static( lua_State* L ) {
return luaL_error(L, "domain name too long");
}
luaL_checkanyfunction(L, 2);
luaL_checkfunction(L, 2);
lua_settop(L, 2);
dns_event_t *ev = luaM_new(L, dns_event_t);
......@@ -1108,7 +1108,7 @@ static void lerr_cb (lua_State *L, lnet_userdata *ud, err_t err)
// --- Tables
// Module function map
LROT_BEGIN(net_tcpserver)
LROT_BEGIN(net_tcpserver, NULL, 0)
LROT_FUNCENTRY( listen, net_listen )
LROT_FUNCENTRY( getaddr, net_getaddr )
LROT_FUNCENTRY( close, net_close )
......@@ -1116,7 +1116,7 @@ LROT_BEGIN(net_tcpserver)
LROT_TABENTRY ( __index, net_tcpserver )
LROT_END(net_tcpserver, NULL, 0)
LROT_BEGIN(net_tcpsocket)
LROT_BEGIN(net_tcpsocket, NULL, 0)
LROT_FUNCENTRY( connect, net_connect )
LROT_FUNCENTRY( close, net_close )
LROT_FUNCENTRY( on, net_on )
......@@ -1128,7 +1128,7 @@ LROT_BEGIN(net_tcpsocket)
LROT_TABENTRY ( __index, net_tcpsocket )
LROT_END(net_tcpsocket, NULL, 0)
LROT_BEGIN(net_udpsocket)
LROT_BEGIN(net_udpsocket, NULL, 0)
LROT_FUNCENTRY( listen, net_listen )
LROT_FUNCENTRY( close, net_close )
LROT_FUNCENTRY( on, net_on )
......@@ -1139,13 +1139,13 @@ LROT_BEGIN(net_udpsocket)
LROT_TABENTRY ( __index, net_udpsocket )
LROT_END(net_udpsocket, NULL, 0)
LROT_BEGIN(net_dns)
LROT_BEGIN(net_dns, NULL, 0)
LROT_FUNCENTRY( setdnsserver, net_setdnsserver )
LROT_FUNCENTRY( getdnsserver, net_getdnsserver )
LROT_FUNCENTRY( resolve, net_dns_static )
LROT_END(net_dns, NULL, 0)
LROT_BEGIN(net)
LROT_BEGIN(net, NULL, 0)
LROT_FUNCENTRY( createServer, net_createServer )
LROT_FUNCENTRY( createConnection, net_createConnection )
LROT_FUNCENTRY( createUDPSocket, net_createUDPSocket )
......@@ -1160,9 +1160,9 @@ LROT_END(net, NULL, 0)
int luaopen_net( lua_State *L ) {
igmp_init();
luaL_rometatable(L, NET_TABLE_TCP_SERVER, (void *)net_tcpserver_map);
luaL_rometatable(L, NET_TABLE_TCP_CLIENT, (void *)net_tcpsocket_map);
luaL_rometatable(L, NET_TABLE_UDP_SOCKET, (void *)net_udpsocket_map);
luaL_rometatable(L, NET_TABLE_TCP_SERVER, LROT_TABLEREF(net_tcpserver));
luaL_rometatable(L, NET_TABLE_TCP_CLIENT, LROT_TABLEREF(net_tcpsocket));
luaL_rometatable(L, NET_TABLE_UDP_SOCKET, LROT_TABLEREF(net_udpsocket));
net_handler = task_get_id(handle_net_event);
dns_handler = task_get_id(handle_dns_event);
......
#include "module.h"
#include "lauxlib.h"
#include "common.h"
#include "legc.h"
#include "lundump.h"
#include "platform.h"
#include "task/task.h"
......@@ -14,7 +13,7 @@
#include "ldebug.h"
#include "esp_vfs.h"
#include "lnodeaux.h"
#include "lflash.h"
#include "lpanic.h"
#include "rom/rtc.h"
// Lua: node.bootreason()
......@@ -169,7 +168,7 @@ static void node_sleep_disable_wakeup_sources (lua_State *L)
static int node_sleep (lua_State *L)
{
lua_settop(L, 1);
luaL_checkanytable(L, 1);
luaL_checktable(L, 1);
node_sleep_disable_wakeup_sources(L);
// uart options: uart = num|{num, num, ...}
......@@ -342,24 +341,13 @@ static int node_dsleep (lua_State *L)
}
extern lua_Load gLoad;
extern bool user_process_input(bool force);
// Lua: input("string")
static int node_input( lua_State* L )
{
size_t l = 0;
const char *s = luaL_checklstring(L, 1, &l);
if (s != NULL && l > 0 && l < LUA_MAXINPUT - 1)
{
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);
}
}
if (l > 0 && l < LUA_MAXINPUT - 1)
lua_input_string(s, l);
return 0;
}
......@@ -395,7 +383,7 @@ int redir_open(const char *path, int flags, int mode) {
// Lua: node.output(func, serial_debug)
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) {
// create an instance of a virtual filesystem so we can use fopen
esp_vfs_t redir_fs = {
......@@ -459,7 +447,7 @@ int redir_vprintf(const char *fmt, va_list ap)
// Lua: node.output(func, serial_debug)
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) {
// register our log redirect first time this is invoked
oldvprintf = esp_log_set_vprintf(redir_vprintf);
......@@ -477,6 +465,7 @@ static int node_osoutput(lua_State *L) {
return 0;
}
/* node.stripdebug([level[, function]]). 
* level: 1 don't discard debug
* 2 discard Local and Upvalue debug info
......@@ -487,58 +476,38 @@ static int node_osoutput(lua_State *L) {
* The function returns an estimated integer count of the bytes stripped.
*/
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);
if ((level <= 0) || (level > 3)) luaL_argerror(L, 1, "must in range 1-3");
int n = lua_gettop(L);
int strip = 0;
if (L->top == L->base + 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;
lua_settop(L, 2);
if (!lua_isnil(L, 1)) {
strip = lua_tointeger(L, 1);
luaL_argcheck(L, strip > 0 && strip < 4, 1, "Invalid strip level");
}
if (!lua_isfunction(L, 2)) {
int scope = luaL_checkint(L, 2);
if (lua_isnumber(L, 2)) {
/* Use debug interface to replace stack level by corresponding function */
int scope = luaL_checkinteger(L, 2);
if (scope > 0) {
/* if the function parameter is a +ve integer then climb to find function */
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)) {
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");
// lua_lock(L);
Proto *f = clvalue(L->base + 1)->l.p;
// lua_unlock(L);
lua_settop(L,0);
lua_pushinteger(L, luaG_stripdebug(L, f, level, 1));
int isfunc = lua_isfunction(L, 2);
luaL_argcheck(L, n < 2 || isfunc, 2, "not a valid function");
/* return result of lua_stripdebug, adding 1 if this is get/set level) */
lua_pushinteger(L, lua_stripdebug(L, strip - 1) + (isfunc ? 0 : 1));
return 1;
}
#if defined(CONFIG_LUA_VERSION_51)
// 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
......@@ -550,9 +519,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_MEM_LIMIT) || limit>0, 1, "limit must be non-zero");
legc_set_mode( L, mode, limit );
lua_setegcmode( L, mode, limit );
return 0;
}
#endif
static int writer(lua_State* L, const void* p, size_t size, void* u)
......@@ -593,7 +563,7 @@ static int node_compile( lua_State* L )
output[strlen(output) - 1] = '\0';
NODE_DBG(output);
NODE_DBG("\n");
if (luaL_loadfsfile(L, fname) != 0) {
if (luaL_loadfile(L, fname) != 0) {
luaM_free( L, output );
return luaL_error(L, lua_tostring(L, -1));
}
......@@ -656,7 +626,7 @@ static int node_task_post( lua_State* L )
luaL_argcheck(L, priority <= TASK_PRIORITY_HIGH, 1, "invalid priority");
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);
int task_fn_ref = luaL_ref(L, LUA_REGISTRYINDEX);
......@@ -699,16 +669,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_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)
#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 )
......@@ -717,7 +773,7 @@ LROT_END(node_task, NULL, 0)
// Wakup reasons
LROT_BEGIN(node_wakeup)
LROT_BEGIN(node_wakeup, NULL, 0)
LROT_NUMENTRY ( GPIO, ESP_SLEEP_WAKEUP_GPIO )
LROT_NUMENTRY ( TIMER, ESP_SLEEP_WAKEUP_TIMER )
LROT_NUMENTRY ( TOUCHPAD, ESP_SLEEP_WAKEUP_TOUCHPAD )
......@@ -725,16 +781,19 @@ LROT_BEGIN(node_wakeup)
LROT_NUMENTRY ( ULP, ESP_SLEEP_WAKEUP_ULP )
LROT_END(node_wakeup, NULL, 0)
LROT_BEGIN(node)
LROT_BEGIN(node, NULL, 0)
LROT_FUNCENTRY( bootreason, node_bootreason )
#if !defined(CONFIG_IDF_TARGET_ESP32C3)
LROT_FUNCENTRY( chipid, node_chipid )
#endif
LROT_FUNCENTRY( compile, node_compile )
LROT_FUNCENTRY( dsleep, node_dsleep )
#if defined(CONFIG_LUA_VERSION_51)
LROT_TABENTRY ( egc, node_egc )
LROT_FUNCENTRY( flashreload,luaN_reload_reboot )
LROT_FUNCENTRY( flashindex, luaN_index )
#endif
LROT_FUNCENTRY( flashreload,node_lfsreload_deprecated )
LROT_FUNCENTRY( flashindex, node_lfsindex )
LROT_TABENTRY( LFS, node_lfs )
LROT_FUNCENTRY( heap, node_heap )
LROT_FUNCENTRY( input, node_input )
LROT_FUNCENTRY( output, node_output )
......
......@@ -239,13 +239,13 @@ next:
}
LROT_BEGIN(otaupgrade)
LROT_BEGIN(otaupgrade, NULL, 0)
LROT_FUNCENTRY( commence, lotaupgrade_commence )
LROT_FUNCENTRY( write, lotaupgrade_write )
LROT_FUNCENTRY( complete, lotaupgrade_complete )
LROT_FUNCENTRY( accept, lotaupgrade_accept )
LROT_FUNCENTRY( rollback, lotaupgrade_rollback )
LROT_FUNCENTRY( info, lotaupgrade_info )
LROT_END(otaupgrade, 0, NULL)
LROT_END(otaupgrade, NULL, 0)
NODEMCU_MODULE(OTAUPGRADE, "otaupgrade", otaupgrade, NULL);
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