From 93140a89a5a3ceefa6487c11a87d87de9fa49042 Mon Sep 17 00:00:00 2001 From: Johny Mattsson Date: Tue, 24 Aug 2021 00:11:31 +1000 Subject: [PATCH] Swap to luaL_pcallx for C module callbacks. Bring on that stacktracey goodness! --- components/modules/bthci.c | 4 ++-- components/modules/can.c | 2 +- components/modules/eth.c | 2 +- components/modules/file.c | 2 +- components/modules/gpio.c | 2 +- components/modules/http.c | 14 +++++++------- components/modules/i2c_hw_master.c | 2 +- components/modules/i2c_hw_slave.c | 2 +- components/modules/i2s.c | 4 ++-- components/modules/mqtt.c | 16 ++++++++-------- components/modules/net.c | 14 +++++++------- components/modules/node.c | 6 +++--- components/modules/pulsecnt.c | 2 +- components/modules/sjson.c | 2 +- components/modules/tmr.c | 2 +- components/modules/touch.c | 2 +- components/modules/uart.c | 4 ++-- components/modules/wifi_ap.c | 2 +- components/modules/wifi_sta.c | 4 ++-- 19 files changed, 44 insertions(+), 44 deletions(-) diff --git a/components/modules/bthci.c b/components/modules/bthci.c index c9d84b11..ae3aa3bd 100644 --- a/components/modules/bthci.c +++ b/components/modules/bthci.c @@ -271,7 +271,7 @@ static void invoke_cmd_q_callback ( else lua_pushnil (L); // no error lua_pushlstring (L, (const char *)data, len ); // extra bytes, if any - lua_call (L, 2, 0); + luaL_pcallx (L, 2, 0); } } @@ -316,7 +316,7 @@ static void handle_hci_event (task_param_t arg, task_prio_t prio) uint8_t *report = &hci_event[5]; lua_rawgeti (L, LUA_REGISTRYINDEX, adv_rep_cb_ref); lua_pushlstring (L, (const char *)report, len - 2); - lua_call (L, 1, 0); + luaL_pcallx (L, 1, 0); } } } diff --git a/components/modules/can.c b/components/modules/can.c index 70782e7f..6f748057 100644 --- a/components/modules/can.c +++ b/components/modules/can.c @@ -46,7 +46,7 @@ static void can_data_task( task_param_t param, task_prio_t prio ) { lua_pushinteger(L, frame->MsgID); lua_pushlstring(L, (char *)frame->data.u8, frame->DLC); free( frame ); - lua_call(L, 3, 0); + luaL_pcallx(L, 3, 0); } // RTOS diff --git a/components/modules/eth.c b/components/modules/eth.c index a8fdc254..d9f0ff49 100644 --- a/components/modules/eth.c +++ b/components/modules/eth.c @@ -119,7 +119,7 @@ static void on_event(esp_event_base_t base, int32_t id, const void *data) lua_pushstring( L, events[idx].name ); lua_createtable( L, 0, 5 ); events[idx].fill_cb_arg( L, data ); - lua_pcall( L, 2, 0, 0 ); + luaL_pcallx( L, 2, 0 ); lua_settop( L, top ); } diff --git a/components/modules/file.c b/components/modules/file.c index 3e35a40e..5e94e072 100644 --- a/components/modules/file.c +++ b/components/modules/file.c @@ -58,7 +58,7 @@ static int32_t file_rtc_cb( vfs_time *tm ) lua_State *L = lua_getstate(); lua_rawgeti( L, LUA_REGISTRYINDEX, rtc_cb_ref ); - lua_call( L, 0, 1 ); + luaL_pcallx( L, 0, 1 ); if (lua_type( L, lua_gettop( L ) ) == LUA_TTABLE) { table2tm( L, tm ); diff --git a/components/modules/gpio.c b/components/modules/gpio.c index 0c968d70..2c487a04 100644 --- a/components/modules/gpio.c +++ b/components/modules/gpio.c @@ -235,7 +235,7 @@ static void nodemcu_gpio_callback_task (task_param_t param, task_prio_t prio) lua_rawgeti (L, LUA_REGISTRYINDEX, gpio_cb_refs[gpio]); lua_pushinteger (L, gpio); lua_pushinteger (L, level); - lua_call (L, 2, 0); + luaL_pcallx (L, 2, 0); gpio_intr_enable (gpio); } } diff --git a/components/modules/http.c b/components/modules/http.c index b4b67b84..ed78455e 100644 --- a/components/modules/http.c +++ b/components/modules/http.c @@ -335,7 +335,7 @@ static int make_callback(lhttp_context_t *context, int id, void *data, size_t da // Lowercase all header names luaL_getmetafield(L, -1, "lower"); lua_insert(L, -2); - lua_call(L, 1, 1); + luaL_pcallx(L, 1, 1); char *val = item->data + item->len + 1; lua_pushstring(L, val); lua_settable(L, -3); @@ -366,7 +366,7 @@ static int make_callback(lhttp_context_t *context, int id, void *data, size_t da if (id != HTTP_REQUEST_COMPLETE) { context_setflag(context, InCallback); } - int err = lua_pcall(L, lua_gettop(L) - 1, 1, 0); + int err = luaL_pcallx(L, lua_gettop(L) - 1, 1); context_clearflag(context, InCallback); if (err) { const char *msg = lua_type(L, -1) == LUA_TSTRING ? lua_tostring(L, -1) : ""; @@ -657,7 +657,7 @@ static int http_accumulate_complete(lua_State *L) context_setref(L, context, HeadersCallback); } else { lua_rawgeti(L, cache_table, 2); // headers - lua_call(L, 3, 0); + luaL_pcallx(L, 3, 0); } // unset this since it contains a reference to the context and would prevent the context to be garbage collected context_unsetref(L, context,CompleteCallback); @@ -692,7 +692,7 @@ static int make_oneshot_request(lua_State *L, int callback_idx) // Finally, call request lua_pushcfunction(L, http_lapi_request); lua_pushvalue(L, -2); // context - lua_call(L, 1, 0); + luaL_pcallx(L, 1, 0); if (async) { return 0; @@ -736,7 +736,7 @@ static int http_lapi_get(lua_State *L) lua_pushinteger(L, HTTP_METHOD_GET); lua_pushvalue(L, 2); // options - lua_call(L, 3, 1); // returns context + luaL_pcallx(L, 3, 1); // returns context return make_oneshot_request(L, 3); } @@ -768,12 +768,12 @@ static int http_lapi_post(lua_State *L) lua_pushinteger(L, HTTP_METHOD_POST); lua_pushvalue(L, 2); // options - lua_call(L, 3, 1); // returns context + luaL_pcallx(L, 3, 1); // returns context lua_pushcfunction(L, http_lapi_setpostdata); lua_pushvalue(L, -2); // context lua_pushvalue(L, 3); // body - lua_call(L, 2, 0); + luaL_pcallx(L, 2, 0); return make_oneshot_request(L, 4); // 4 = callback idx } diff --git a/components/modules/i2c_hw_master.c b/components/modules/i2c_hw_master.c index c7d6424c..f0709292 100644 --- a/components/modules/i2c_hw_master.c +++ b/components/modules/i2c_hw_master.c @@ -111,7 +111,7 @@ static void i2c_transfer_task( task_param_t param, task_prio_t prio ) lua_pushnil( L ); } lua_pushboolean( L, job->err == ESP_OK ); - lua_call(L, 2, 0); + luaL_pcallx(L, 2, 0); } // free all memory diff --git a/components/modules/i2c_hw_slave.c b/components/modules/i2c_hw_slave.c index 964b9d55..c5790f70 100644 --- a/components/modules/i2c_hw_slave.c +++ b/components/modules/i2c_hw_slave.c @@ -109,7 +109,7 @@ static void i2c_receive_task( task_param_t param, task_prio_t prio ) } else { lua_pushnil( L ); } - lua_call(L, 2, 0); + luaL_pcallx(L, 2, 0); } // free all memory diff --git a/components/modules/i2s.c b/components/modules/i2s.c index 08f3fb37..88fab401 100644 --- a/components/modules/i2s.c +++ b/components/modules/i2s.c @@ -55,7 +55,7 @@ static void i2s_tx_task( task_param_t param, task_prio_t prio ) { lua_rawgeti(L, LUA_REGISTRYINDEX, is->cb); lua_pushinteger( L, i2s_id ); lua_pushstring( L, "tx" ); - lua_call( L, 2, 0 ); + luaL_pcallx( L, 2, 0 ); } } @@ -68,7 +68,7 @@ static void i2s_rx_task( task_param_t param, task_prio_t prio ) { lua_rawgeti(L, LUA_REGISTRYINDEX, is->cb); lua_pushinteger( L, i2s_id ); lua_pushstring( L, "rx" ); - lua_call( L, 2, 0 ); + luaL_pcallx( L, 2, 0 ); } } diff --git a/components/modules/mqtt.c b/components/modules/mqtt.c index ba4ce6e9..d3b6e21c 100644 --- a/components/modules/mqtt.c +++ b/components/modules/mqtt.c @@ -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) 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) 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 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 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) 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 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"); - 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) 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 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 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) 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 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_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) 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 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_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) 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 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_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) 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_ lua_pushlstring(L, event->data, event->data_len); 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) ESP_LOGD(TAG, "CB:data: Error when calling message callback - (%d) %s", res, luaL_checkstring(L, -1)); } diff --git a/components/modules/net.c b/components/modules/net.c index 5edac3b1..46f073c7 100644 --- a/components/modules/net.c +++ b/components/modules/net.c @@ -968,7 +968,7 @@ static void ldnsfound_cb (lua_State *L, lnet_userdata *ud, ip_addr_t *addr) { } else { lua_pushnil(L); } - lua_call(L, 2, 0); + luaL_pcallx(L, 2, 0); } ud->client.wait_dns --; 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) { } else { 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) { 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->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) { lua_pushinteger(L, port); lua_pushstring(L, iptmp); } - lua_call(L, num_args, 0); + luaL_pcallx(L, num_args, 0); } } while (netbuf_next(p) != -1); @@ -1069,7 +1069,7 @@ static void laccept_cb (lua_State *L, lnet_userdata *ud) { nud->netconn->pcb.tcp->keep_cnt = 1; } else 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) { if (ud->client.cb_sent_ref != LUA_NOREF) { lua_rawgeti(L, LUA_REGISTRYINDEX, ud->client.cb_sent_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) lua_rawgeti(L, LUA_REGISTRYINDEX, ref); lua_rawgeti(L, LUA_REGISTRYINDEX, ud->self_ref); lua_pushinteger(L, err); - lua_call(L, 2, 0); + luaL_pcallx(L, 2, 0); } if (ud->client.wait_dns == 0) { lua_gc(L, LUA_GCSTOP, 0); diff --git a/components/modules/node.c b/components/modules/node.c index 98d93b2d..a39afcab 100644 --- a/components/modules/node.c +++ b/components/modules/node.c @@ -379,7 +379,7 @@ ssize_t redir_write(int fd, const void *data, size_t size) { lua_State *L = lua_getstate(); lua_rawgeti(L, LUA_REGISTRYINDEX, output_redir); // push function reference 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; } @@ -447,7 +447,7 @@ int redir_vprintf(const char *fmt, va_list ap) lua_State *L = lua_getstate(); lua_rawgeti(L, LUA_REGISTRYINDEX, os_output_redir); // push function reference 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; } @@ -621,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); luaL_unref(L, LUA_REGISTRYINDEX, (int)task_fn_ref); 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 diff --git a/components/modules/pulsecnt.c b/components/modules/pulsecnt.c index 4e15f1fe..b994161b 100644 --- a/components/modules/pulsecnt.c +++ b/components/modules/pulsecnt.c @@ -214,7 +214,7 @@ static void pulsecnt_task(task_param_t param, task_prio_t prio) lua_pushboolean (L, zero); // lua_pushinteger (L, moving_to); // lua_pushinteger (L, status); - lua_call (L, 6, 0); + luaL_pcallx (L, 6, 0); } else { if (pc->is_debug) ESP_LOGI("pulsecnt", "Could not find cb for unit %d with ptr %d", unit, pc->cb_ref); } diff --git a/components/modules/sjson.c b/components/modules/sjson.c index 16e0e05f..8ed7ce1a 100644 --- a/components/modules/sjson.c +++ b/components/modules/sjson.c @@ -128,7 +128,7 @@ create_new_element(jsonsl_t jsn, // 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, data->pos_ref); - lua_call(data->L, 2, 1); + luaL_pcallx(data->L, 2, 1); want_value = lua_toboolean(data->L, -1); } lua_pop(data->L, 2); // Discard the metatable and either the getfield result or retval diff --git a/components/modules/tmr.c b/components/modules/tmr.c index b858dcb3..a386dc26 100755 --- a/components/modules/tmr.c +++ b/components/modules/tmr.c @@ -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); 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 ) diff --git a/components/modules/touch.c b/components/modules/touch.c index c532aa53..3f25f04a 100644 --- a/components/modules/touch.c +++ b/components/modules/touch.c @@ -141,7 +141,7 @@ static void touch_task(task_param_t param, task_prio_t prio) // call the cb_ref with one argument /* 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); } } diff --git a/components/modules/uart.c b/components/modules/uart.c index 0e97ef43..118be37d 100644 --- a/components/modules/uart.c +++ b/components/modules/uart.c @@ -21,7 +21,7 @@ bool uart_on_data_cb(unsigned id, const char *buf, size_t len){ int top = lua_gettop(gL); lua_rawgeti(gL, LUA_REGISTRYINDEX, uart_status[id].receive_rf); lua_pushlstring(gL, buf, len); - lua_pcall(gL, 1, 0, 0); + luaL_pcallx(gL, 1, 0); lua_settop(gL, top); return !run_input; } @@ -37,7 +37,7 @@ bool uart_on_error_cb(unsigned id, const char *buf, size_t len){ int top = lua_gettop(gL); lua_rawgeti(gL, LUA_REGISTRYINDEX, uart_status[id].error_rf); lua_pushlstring(gL, buf, len); - lua_pcall(gL, 1, 0, 0); + luaL_pcallx(gL, 1, 0); lua_settop(gL, top); return true; } diff --git a/components/modules/wifi_ap.c b/components/modules/wifi_ap.c index e882ffbb..e9cb4012 100644 --- a/components/modules/wifi_ap.c +++ b/components/modules/wifi_ap.c @@ -115,7 +115,7 @@ static void on_event (esp_event_base_t base, int32_t id, const void *data) lua_pushstring (L, events[idx].name); lua_createtable (L, 0, 5); events[idx].fill_cb_arg (L, data); - lua_call (L, 2, 0); + luaL_pcallx (L, 2, 0); } NODEMCU_ESP_EVENT(WIFI_EVENT, WIFI_EVENT_AP_START, on_event); diff --git a/components/modules/wifi_sta.c b/components/modules/wifi_sta.c index 8e6deea8..acf40b76 100644 --- a/components/modules/wifi_sta.c +++ b/components/modules/wifi_sta.c @@ -141,7 +141,7 @@ static void on_event (esp_event_base_t base, int32_t id, const void *data) lua_pushstring (L, events[idx].name); lua_createtable (L, 0, 5); events[idx].fill_cb_arg (L, data); - lua_call (L, 2, 0); + luaL_pcallx (L, 2, 0); } NODEMCU_ESP_EVENT(WIFI_EVENT, WIFI_EVENT_STA_START, on_event); @@ -390,7 +390,7 @@ static void on_scan_done(esp_event_base_t base, int32_t id, const void *data) else lua_pushfstring (L, "failure on scan done"); luaM_free (L, aps); - lua_call (L, nargs, 0); + luaL_pcallx (L, nargs, 0); } }