diff --git a/ChangeLog b/ChangeLog index 26200a74f3..512d9bc0c1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -19,6 +19,7 @@ Fix g.wrapString lockup if wrap width is less than the character width Fix potential for crash after ReferenceError during function declaration (fix #2457) STM32: (Original/Pico/WiFi) if USB connected but PC not receiving data, throw away USB data rather than blocking (fix #2446) + Move commonly used sequences of function calls into functions (eg jsvRemoveChild,jsvUnLock -> jsvRemoveChildAndUnLock) - saves ~200b 2v20 : Ensure String.charCodeAt returns NaN for out of bounds chars Bangle.js2: When rendering overlays, *do not* use the current FG/BG color for 1 bit overlays diff --git a/libs/bluetooth/jswrap_bluetooth.c b/libs/bluetooth/jswrap_bluetooth.c index 39cd0afb92..2dc3a9cb55 100644 --- a/libs/bluetooth/jswrap_bluetooth.c +++ b/libs/bluetooth/jswrap_bluetooth.c @@ -1747,8 +1747,7 @@ void jswrap_ble_updateServices(JsVar *data) { #endif } } - jsvUnLock(charValue); - jsvUnLock(charVar); + jsvUnLock2(charValue, charVar); } else { JsVar *str = bleUUIDToStr(char_uuid); jsExceptionHere(JSET_ERROR, "Unable to find service with UUID %v", str); diff --git a/libs/filesystem/jswrap_file.c b/libs/filesystem/jswrap_file.c index 5fe297bc0e..69b4da0eea 100755 --- a/libs/filesystem/jswrap_file.c +++ b/libs/filesystem/jswrap_file.c @@ -381,10 +381,8 @@ void jswrap_file_close(JsVar* parent) { JsVar *arr = fsGetArray(false); if (arr) { JsVar *idx = jsvGetIndexOf(arr, file.fileVar, true); - if (idx) { - jsvRemoveChild(arr, idx); - jsvUnLock(idx); - } + if (idx) + jsvRemoveChildAndUnLock(arr, idx); jsvUnLock(arr); } } diff --git a/libs/graphics/jswrap_graphics.c b/libs/graphics/jswrap_graphics.c index 0c0ecd1ab4..a9b10b48b7 100644 --- a/libs/graphics/jswrap_graphics.c +++ b/libs/graphics/jswrap_graphics.c @@ -2083,9 +2083,9 @@ JsVar *jswrap_graphics_getFonts(JsVar *parent) { JsGraphics gfx; if (!graphicsGetFromVar(&gfx, parent)) return 0; JsVar *arr = jsvNewEmptyArray(); if (!arr) return 0; - jsvArrayPushAndUnLock(arr, jsvNewFromString("4x6")); + jsvArrayPushString(arr, "4x6"); #ifdef USE_FONT_6X8 - jsvArrayPushAndUnLock(arr, jsvNewFromString("6x8")); + jsvArrayPushString(arr, "6x8"); #endif // vector font is added by below.. // scan for any functions 'setFont*' and add those names @@ -2135,8 +2135,7 @@ static void _jswrap_graphics_getFontInfo(JsGraphics *gfx, JsGraphicsFontInfo *in static void _jswrap_graphics_freeFontInfo(JsGraphicsFontInfo *info) { #ifndef SAVE_ON_FLASH if (info->font & JSGRAPHICS_FONTSIZE_CUSTOM_BIT) { - jsvUnLock(info->widths); - jsvUnLock(info->bitmap); + jsvUnLock2(info->widths, info->bitmap); #ifdef ESPR_PBF_FONTS if ((info->font & JSGRAPHICS_FONTSIZE_FONT_MASK) == JSGRAPHICS_FONTSIZE_CUSTOM_PBF) jspbfFontFree(&info->pbfInfo); diff --git a/libs/network/esp8266/jswrap_esp8266_network.c b/libs/network/esp8266/jswrap_esp8266_network.c index 3e46284e34..9919355f27 100644 --- a/libs/network/esp8266/jswrap_esp8266_network.c +++ b/libs/network/esp8266/jswrap_esp8266_network.c @@ -896,7 +896,6 @@ void jswrap_wifi_save(JsVar *what) { //JsVar *arr = jsvNewArray(&o,1); jswrap_storage_erase(name); jswrap_storage_write(name,o,0,0); - //jsvUnLock3(arr,name,o); jsvUnLock2(name,o); DBGV("< Wifi.save: write completed\n"); @@ -1070,8 +1069,7 @@ static void dnsFoundCallback( params[0] = networkGetAddressAsString((uint8_t *)&ipAddr->addr, 4, 10, '.'); } jsiQueueEvents(NULL, g_jsHostByNameCallback, params, 1); - jsvUnLock(params[0]); - jsvUnLock(g_jsHostByNameCallback); + jsvUnLock2(params[0], g_jsHostByNameCallback); g_jsHostByNameCallback = NULL; } DBGV("<< Wifi.getHostByName CB\n"); @@ -1547,8 +1545,7 @@ static void scanCB(void *arg, STATUS status) { params[0] = jsAccessPointArray; jsiQueueEvents(NULL, g_jsScanCallback, params, 1); - jsvUnLock(jsAccessPointArray); - jsvUnLock(g_jsScanCallback); + jsvUnLock2(jsAccessPointArray, g_jsScanCallback); g_jsScanCallback = NULL; DBGV("<< Wifi.scanCB\n"); } diff --git a/libs/network/network.c b/libs/network/network.c index 93240a115f..590a012cef 100644 --- a/libs/network/network.c +++ b/libs/network/network.c @@ -351,8 +351,7 @@ void ssl_freeSocketData(int sckt) { JsVar *sslDataVar = jsvFindChildFromVar(ssl, scktVar, false); jsvUnLock(scktVar); JsVar *sslData = jsvSkipName(sslDataVar); - jsvRemoveChild(ssl, sslDataVar); - jsvUnLock(sslDataVar); + jsvRemoveChildAndUnLock(ssl, sslDataVar); jsvUnLock(ssl); SSLSocketData *sd = 0; if (jsvIsFlatString(sslData)) { @@ -523,8 +522,7 @@ bool ssl_newSocketData(int sckt, JsVar *options) { if (!ssl) return false; // out of memory? JsVar *scktVar = jsvNewFromInteger(sckt); JsVar *sslDataVar = jsvFindChildFromVar(ssl, scktVar, true); - jsvUnLock(scktVar); - jsvUnLock(ssl); + jsvUnLock2(scktVar, ssl); if (!sslDataVar) { return 0; // out of memory } diff --git a/libs/network/socketserver.c b/libs/network/socketserver.c index 5ae0ea6825..4286774bf0 100644 --- a/libs/network/socketserver.c +++ b/libs/network/socketserver.c @@ -589,8 +589,7 @@ bool socketServerConnectionsIdle(JsNetwork *net) { _socketConnectionKill(net, connection); JsVar *connectionName = jsvObjectIteratorGetKey(&it); jsvObjectIteratorNext(&it); - jsvRemoveChild(arr, connectionName); - jsvUnLock(connectionName); + jsvRemoveChildAndUnLock(arr, connectionName); } else jsvObjectIteratorNext(&it); jsvUnLock2(connection, socket); @@ -729,8 +728,7 @@ bool socketClientConnectionsIdle(JsNetwork *net) { _socketConnectionKill(net, connection); JsVar *connectionName = jsvObjectIteratorGetKey(&it); jsvObjectIteratorNext(&it); - jsvRemoveChild(arr, connectionName); - jsvUnLock(connectionName); + jsvRemoveChildAndUnLock(arr, connectionName); socketClosed = true; // fire error event, if there is an error @@ -897,8 +895,7 @@ void serverClose(JsNetwork *net, JsVar *server) { // remove from array JsVar *idx = jsvGetIndexOf(arr, server, true); if (idx) { - jsvRemoveChild(arr, idx); - jsvUnLock(idx); + jsvRemoveChildAndUnLock(arr, idx); } else jsWarn("Server not found!"); jsvUnLock(arr); diff --git a/libs/trigger/jswrap_trigger.c b/libs/trigger/jswrap_trigger.c index c68a89b8c1..cdc0e48c8b 100644 --- a/libs/trigger/jswrap_trigger.c +++ b/libs/trigger/jswrap_trigger.c @@ -277,7 +277,7 @@ JsVar* jswrap_trig_getErrorArray() { if (errors & i) { const char *s = trigGetErrorString(i); if (s) { - jsvArrayPushAndUnLock(arr, jsvNewFromString(s)); + jsvArrayPushString(arr); } } } diff --git a/scripts/find_common_code.js b/scripts/find_common_code.js index ae55dfe473..b4455d9c16 100755 --- a/scripts/find_common_code.js +++ b/scripts/find_common_code.js @@ -11,7 +11,7 @@ */ var IGNORE_REGISTERS = true; -var MIN_OCCURANCES = 3; +var MIN_OCCURANCES = 2; var MIN_SCORE = 20; @@ -20,17 +20,17 @@ if (process.argv.length!=3) { process.exit(1); } -var lstFile = process.argv[2]; -console.log("Loading "+lstFile); -var lst = require("fs").readFileSync(lstFile).toString().split("\n"); - - +function isFunctionHeader(line) { + return line.match(/^\s*[0-9A-Fa-f]{8}\s*<.*:/); +} function getAssembly(line) { line = line.trim(); - if (line.indexOf("\t")<0) line=""; + if (line=="") return ""; + if (isFunctionHeader(line)) return line; // function header + if (line.indexOf("\t")<0) return ""; // just code line = line.substr(line.indexOf("\t")+1); - if (line.indexOf("\t")<0) line=""; + if (line.indexOf("\t")<0) return ""; // hex dump of data line = line.substr(line.indexOf("\t")+1); @@ -40,6 +40,10 @@ function getAssembly(line) { return line; } +var lstFile = process.argv[2]; +console.log("Loading "+lstFile); +var lst = require("fs").readFileSync(lstFile).toString().split("\n").map(getAssembly).filter(l=>l!=""); + function scanLines(lst) { var occurances = {}; var mapCodeToIdx = {}; @@ -48,8 +52,6 @@ function scanLines(lst) { var history = []; console.log("Scanning for use count"); lst.forEach(function(line) { - line = getAssembly(line); - if (line!="") { var lineIdx = mapCodeToIdx[line]; if (lineIdx===undefined) { @@ -62,9 +64,7 @@ function scanLines(lst) { }); console.log("Scanning for history"); lst.forEach(function(line) { - line = getAssembly(line); - - if (line=="") history = []; + if (isFunctionHeader(line)) history = []; else { var lineIdx = mapCodeToIdx[line]; if (lineUses[lineIdx]var) { JsVarRef next = jsvGetNextSibling(it->var); - jsvRemoveChild(parent, it->var); - jsvUnLock(it->var); + jsvRemoveChildAndUnLock(parent, it->var); it->var = jsvLockSafe(next); } } diff --git a/src/jswrap_array.c b/src/jswrap_array.c index 58e09ff7b2..dd305dace9 100644 --- a/src/jswrap_array.c +++ b/src/jswrap_array.c @@ -599,8 +599,7 @@ JsVar *jswrap_array_splice(JsVar *parent, JsVarInt index, JsVar *howManyVar, JsV goToNext = false; JsVar *toRemove = jsvObjectIteratorGetKey(&it); jsvObjectIteratorNext(&it); - jsvRemoveChild(parent, toRemove); - jsvUnLock(toRemove); + jsvRemoveChildAndUnLock(parent, toRemove); } else { // we're greater than the amount we need to remove now needToAdd = true; goToNext = false; diff --git a/src/jswrap_espruino.c b/src/jswrap_espruino.c index 2cf756ca0e..7194019ea5 100644 --- a/src/jswrap_espruino.c +++ b/src/jswrap_espruino.c @@ -680,13 +680,13 @@ void jswrap_espruino_kickWatchdog() { JsVar *jswrap_espruino_getErrorFlagArray(JsErrorFlags flags) { JsVar *arr = jsvNewEmptyArray(); if (!arr) return 0; - if (flags&JSERR_RX_FIFO_FULL) jsvArrayPushAndUnLock(arr, jsvNewFromString("FIFO_FULL")); - if (flags&JSERR_BUFFER_FULL) jsvArrayPushAndUnLock(arr, jsvNewFromString("BUFFER_FULL")); - if (flags&JSERR_CALLBACK) jsvArrayPushAndUnLock(arr, jsvNewFromString("CALLBACK")); - if (flags&JSERR_LOW_MEMORY) jsvArrayPushAndUnLock(arr, jsvNewFromString("LOW_MEMORY")); - if (flags&JSERR_MEMORY) jsvArrayPushAndUnLock(arr, jsvNewFromString("MEMORY")); - if (flags&JSERR_MEMORY_BUSY) jsvArrayPushAndUnLock(arr, jsvNewFromString("MEMORY_BUSY")); - if (flags&JSERR_UART_OVERFLOW) jsvArrayPushAndUnLock(arr, jsvNewFromString("UART_OVERFLOW")); + if (flags&JSERR_RX_FIFO_FULL) jsvArrayPushString(arr, "FIFO_FULL"); + if (flags&JSERR_BUFFER_FULL) jsvArrayPushString(arr, "BUFFER_FULL"); + if (flags&JSERR_CALLBACK) jsvArrayPushString(arr, "CALLBACK"); + if (flags&JSERR_LOW_MEMORY) jsvArrayPushString(arr, "LOW_MEMORY"); + if (flags&JSERR_MEMORY) jsvArrayPushString(arr, "MEMORY"); + if (flags&JSERR_MEMORY_BUSY) jsvArrayPushString(arr, "MEMORY_BUSY"); + if (flags&JSERR_UART_OVERFLOW) jsvArrayPushString(arr, "UART_OVERFLOW"); return arr; } diff --git a/src/jswrap_interactive.c b/src/jswrap_interactive.c index 974651ff0c..b4567e583a 100644 --- a/src/jswrap_interactive.c +++ b/src/jswrap_interactive.c @@ -567,10 +567,8 @@ void _jswrap_interface_clearTimeoutOrInterval(JsVar *idVarArr, bool isTimeout) { jsExceptionHere(JSET_ERROR, "clear%s(undefined) not allowed. Use clear%s() instead", name, name); } else { JsVar *child = jsvIsBasic(idVar) ? jsvFindChildFromVar(timerArrayPtr, idVar, false) : 0; - if (child) { - jsvRemoveChild(timerArrayPtr, child); - jsvUnLock(child); - } + if (child) + jsvRemoveChildAndUnLock(timerArrayPtr, child); jsvUnLock(idVar); } } diff --git a/src/jswrap_io.c b/src/jswrap_io.c index e05fff7bdc..c51eb7a65f 100644 --- a/src/jswrap_io.c +++ b/src/jswrap_io.c @@ -339,8 +339,7 @@ void jswrap_io_digitalWrite( JsVar *w = jspGetNamedField(pinVar, "write", false); if (jsvIsFunction(w)) { JsVar *v = jsvNewFromInteger(value); - jsvUnLock(jspeFunctionCall(w,0,pinVar,false,1,&v)); - jsvUnLock(v); + jsvUnLock2(jspeFunctionCall(w,0,pinVar,false,1,&v), v); } else jsExceptionHere(JSET_ERROR, "Invalid pin"); jsvUnLock(w); } else { @@ -904,8 +903,8 @@ void jswrap_interface_clearWatch(JsVar *idVarArr) { jsvUnLock(watchPtr); JsVar *watchArrayPtr = jsvLock(watchArray); - jsvRemoveChild(watchArrayPtr, watchNamePtr); - jsvUnLock2(watchNamePtr, watchArrayPtr); + jsvRemoveChildAndUnLock(watchArrayPtr, watchNamePtr); + jsvUnLock(watchArrayPtr); // Now check if this pin is still being watched if (!jsiIsWatchingPin(pin)) diff --git a/src/jswrap_modules.c b/src/jswrap_modules.c index 157b135ddc..26b68b3b47 100644 --- a/src/jswrap_modules.c +++ b/src/jswrap_modules.c @@ -216,8 +216,7 @@ void jswrap_modules_removeCached(JsVar *id) { if (!moduleExportName) { jsExceptionHere(JSET_ERROR, "Module %q not found", id); } else { - jsvRemoveChild(moduleList, moduleExportName); - jsvUnLock(moduleExportName); + jsvRemoveChildAndUnLock(moduleList, moduleExportName); } jsvUnLock(moduleList); diff --git a/src/jswrap_object.c b/src/jswrap_object.c index b49a7234b0..582da08970 100644 --- a/src/jswrap_object.c +++ b/src/jswrap_object.c @@ -1032,8 +1032,7 @@ void jswrap_object_removeListener(JsVar *parent, JsVar *event, JsVar *callback) // it's an array, search for the index JsVar *idx = jsvGetIndexOf(eventList, callback, true); if (idx) { - jsvRemoveChild(eventList, idx); - jsvUnLock(idx); + jsvRemoveChildAndUnLock(eventList, idx); } } jsvUnLock(eventList); @@ -1078,8 +1077,7 @@ void jswrap_object_removeAllListeners(JsVar *parent, JsVar *event) { JsVar *eventList = jsvFindChildFromVar(parent, eventName, true); jsvUnLock(eventName); if (eventList) { - jsvRemoveChild(parent, eventList); - jsvUnLock(eventList); + jsvRemoveChildAndUnLock(parent, eventList); } } else if (jsvIsUndefined(event)) { // Eep. We must remove everything beginning with '#on' (JS_EVENT_PREFIX) diff --git a/src/jswrap_onewire.c b/src/jswrap_onewire.c index 84fbc538a9..ab12229498 100644 --- a/src/jswrap_onewire.c +++ b/src/jswrap_onewire.c @@ -500,7 +500,7 @@ JsVar *jswrap_onewire_search(JsVar *parent, int command) { buf[i*2+1] = itoch(ROM_NO[i] & 15); } buf[16]=0; - jsvArrayPushAndUnLock(array, jsvNewFromString(buf)); + jsvArrayPushString(array, buf); } NOT_USED(LastFamilyDiscrepancy); diff --git a/src/jswrap_pipe.c b/src/jswrap_pipe.c index 348b7389f6..9cb1582371 100644 --- a/src/jswrap_pipe.c +++ b/src/jswrap_pipe.c @@ -92,8 +92,7 @@ static void handlePipeClose(JsVar *arr, JsvObjectIterator *it, JsVar* pipe) { } jsvUnLock2(source, destination); JsVar *idx = jsvObjectIteratorGetKey(it); - jsvRemoveChild(arr,idx); - jsvUnLock(idx); + jsvRemoveChildAndUnLock(arr,idx); } static bool handlePipe(JsVar *arr, JsvObjectIterator *it, JsVar* pipe) { diff --git a/targets/esp32/BLE/esp32_bluetooth_utils.c b/targets/esp32/BLE/esp32_bluetooth_utils.c index 06a833fafc..df9bb4173c 100644 --- a/targets/esp32/BLE/esp32_bluetooth_utils.c +++ b/targets/esp32/BLE/esp32_bluetooth_utils.c @@ -12,7 +12,7 @@ * ---------------------------------------------------------------------------- */ - + #include "BLE/esp32_bluetooth_utils.h" #include "BLE/esp32_gap_func.h" #include "BLE/esp32_gatts_func.h" @@ -26,14 +26,14 @@ esp_ble_debug_t bleEventDebug = 0; esp_err_t initController(){ - esp_err_t ret; + esp_err_t ret; esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); ret = esp_bt_controller_init(&bt_cfg);if(ret) {jsWarn("init controller failed:%x\n",ret); return ret;} ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);if(ret) {jsWarn("enable controller failed:%x\n",ret); return ret;} return ret; } esp_err_t initBluedroid(){ - esp_err_t ret; + esp_err_t ret; ret = esp_bluedroid_init();if (ret) {jsWarn("init bluedroid failed:%x\n",ret);return ret;} ret = esp_bluedroid_enable();if (ret) {jsWarn("enable bluedroid failed:%x\n",ret);return ret;} return ret; @@ -48,12 +48,12 @@ esp_err_t deinitBluedroid(){ esp_err_t ret; ret = esp_bluedroid_disable();if (ret) {jsWarn("disable bluedroid failed:%x\n",ret);return ret;} ret = esp_bluedroid_deinit();if (ret) {jsWarn("deinit bluedroid failed:%x\n",ret);return ret;} - return ret; + return ret; } esp_err_t registerCallbacks(){ esp_err_t ret; ret = esp_ble_gap_register_callback(gap_event_handler);if (ret){jsWarn("gap register error:%x\n", ret);return ret;} - ret = esp_ble_gatts_register_callback(gatts_event_handler);if(ret){jsWarn("gatts register error:%x\n", ret);return ret;} + ret = esp_ble_gatts_register_callback(gatts_event_handler);if(ret){jsWarn("gatts register error:%x\n", ret);return ret;} ret = esp_ble_gattc_register_callback(gattc_event_handler);if(ret){jsWarn("gattc regigister error:%x\n",ret);return ret;} return ret; } @@ -225,8 +225,7 @@ bool bleRemoveChild(JsVar *parent, JsVar *blevar){ jsvRemoveChild(parent,child); ret = true; } - jsvUnLock(child); - jsvUnLock(name); + jsvUnLock2(child, name); jsvObjectIteratorNext(&it); } jsvObjectIteratorFree(&it); diff --git a/targets/esp32/BLE/esp32_gatts_func.c b/targets/esp32/BLE/esp32_gatts_func.c index bf4d45c62b..7fd09fdc2a 100644 --- a/targets/esp32/BLE/esp32_gatts_func.c +++ b/targets/esp32/BLE/esp32_gatts_func.c @@ -104,8 +104,7 @@ void emitNRFEvent(char *event,JsVar *args,int argCnt){ JsVar *callback = jsvSkipNameAndUnLock(jsvFindChildFromVar(nrf,eventName,0)); jsvUnLock(eventName); if(callback) jsiQueueEvents(nrf,callback,args,argCnt); - jsvUnLock(nrf); - jsvUnLock(callback); + jsvUnLock2(nrf, callback); if(args) jsvUnLockMany(argCnt,args); } @@ -576,8 +575,7 @@ void gatts_create_structs(bool enableUART){ JsVar *charVar = jsvObjectIteratorGetValue(&ble_char_it); JsVar *charDescriptionVar = jsvObjectGetChildIfExists(charVar, "description"); if (charDescriptionVar && jsvHasCharacterData(charDescriptionVar)) ble_descr_cnt++; - jsvUnLock(charDescriptionVar); - jsvUnLock(charVar); + jsvUnLock2(charDescriptionVar, charVar); jsvObjectIteratorNext(&ble_char_it); ble_char_cnt++; } diff --git a/targets/nrf5x/bluetooth_ancs.c b/targets/nrf5x/bluetooth_ancs.c index 0da1c950c6..f41e8074c7 100644 --- a/targets/nrf5x/bluetooth_ancs.c +++ b/targets/nrf5x/bluetooth_ancs.c @@ -309,13 +309,13 @@ void ble_cts_handle_time(BLEPending blep, char *buffer, size_t bufferLen) { } JsVar *arr = jsvNewEmptyArray(); if (p_time->adjust_reason.manual_time_update) - jsvArrayPushAndUnLock(arr, jsvNewFromString("manual")); + jsvArrayPushString(arr, "manual"); if (p_time->adjust_reason.external_reference_time_update) - jsvArrayPushAndUnLock(arr, jsvNewFromString("external")); + jsvArrayPushString(arr, "external"); if (p_time->adjust_reason.change_of_time_zone) - jsvArrayPushAndUnLock(arr, jsvNewFromString("timezone")); + jsvArrayPushString(arr, "timezone"); if (p_time->adjust_reason.change_of_daylight_savings_time) - jsvArrayPushAndUnLock(arr, jsvNewFromString("DST")); + jsvArrayPushString(arr, "DST"); jsvObjectSetChildAndUnLock(o, "reason", arr); jsiExecuteEventCallbackOn("E", JS_EVENT_PREFIX"CTS", 1, &o);