Started using jsvObjectRemoveChild to remove some internal object properties that are undefined (frees some variables)

This commit is contained in:
Gordon Williams 2016-11-07 17:07:29 +00:00
parent a1c0d1c452
commit aeebfb1409
15 changed files with 34 additions and 32 deletions

View File

@ -34,6 +34,7 @@
nRF5x: Add Puck.js self-test code, fix issue where analogRead reset pin state
nRF5x: Change central mode API to mirror Web Bluetooth
Fix switch fall-through to default (fix #964)
Started using jsvObjectRemoveChild to remove some internal object properties that are undefined (frees some variables)
1v87 : Add support for compiling with float-abi=hard (even if it doesn't give us real-world benefits)
Add shortcut for quick execution of common call types

View File

@ -558,10 +558,10 @@ void jswrap_graphics_setFontSizeX(JsVar *parent, int size, bool checkValid) {
if (size>1023) size=1023;
}
if (gfx.data.fontSize == JSGRAPHICS_FONTSIZE_CUSTOM) {
jsvObjectSetChild(parent, JSGRAPHICS_CUSTOMFONT_BMP, 0);
jsvObjectSetChild(parent, JSGRAPHICS_CUSTOMFONT_WIDTH, 0);
jsvObjectSetChild(parent, JSGRAPHICS_CUSTOMFONT_HEIGHT, 0);
jsvObjectSetChild(parent, JSGRAPHICS_CUSTOMFONT_FIRSTCHAR, 0);
jsvObjectRemoveChild(parent, JSGRAPHICS_CUSTOMFONT_BMP);
jsvObjectRemoveChild(parent, JSGRAPHICS_CUSTOMFONT_WIDTH);
jsvObjectRemoveChild(parent, JSGRAPHICS_CUSTOMFONT_HEIGHT);
jsvObjectRemoveChild(parent, JSGRAPHICS_CUSTOMFONT_FIRSTCHAR);
}
gfx.data.fontSize = (short)size;
graphicsSetVar(&gfx);

View File

@ -194,7 +194,7 @@ void _socketConnectionKill(JsNetwork *net, JsVar *connection) {
int sckt = (int)jsvGetIntegerAndUnLock(jsvObjectGetChild(connection,HTTP_NAME_SOCKET,0))-1; // so -1 if undefined
if (sckt>=0) {
netCloseSocket(net, sckt);
jsvObjectSetChild(connection,HTTP_NAME_SOCKET,0);
jsvObjectRemoveChild(connection,HTTP_NAME_SOCKET);
}
}
@ -441,7 +441,7 @@ void socketClientPushReceiveData(JsVar *connection, JsVar *socket, JsVar **recei
if (jsvIsEmptyString(*receiveData) ||
jswrap_stream_pushData(socket, *receiveData, false)) {
// clear - because we have issued a callback
jsvObjectSetChild(connection,HTTP_NAME_RECEIVE_DATA,0);
jsvObjectRemoveChild(connection,HTTP_NAME_RECEIVE_DATA);
jsvUnLock(*receiveData);
*receiveData = 0;
}

View File

@ -477,7 +477,7 @@ void jsiSoftInit(bool hasBeenReset) {
JsVar *initCode = jsvObjectGetChild(execInfo.hiddenRoot, JSI_INIT_CODE_NAME, 0);
if (initCode) {
jsvUnLock2(jspEvaluateVar(initCode, 0, 0), initCode);
jsvRemoveNamedChild(execInfo.hiddenRoot, JSI_INIT_CODE_NAME);
jsvObjectRemoveChild(execInfo.hiddenRoot, JSI_INIT_CODE_NAME);
}
// Check any existing watches and set up interrupts for them
@ -1950,7 +1950,7 @@ void jsiIdle() {
}
jsvUnLock(data);
if (watchPtr) { // if we had a watch pointer, be sure to remove us from it
jsvObjectSetChild(watchPtr, "timeout", 0);
jsvObjectRemoveChild(watchPtr, "timeout");
// Deal with non-recurring watches
if (exec) {
bool watchRecurring = jsvGetBoolAndUnLock(jsvObjectGetChild(watchPtr, "recur", 0));

View File

@ -2357,7 +2357,7 @@ NO_INLINE JsVar *jspeStatementTry() {
jsvRemoveChild(execInfo.hiddenRoot, actualExceptionName);
jsvUnLock(actualExceptionName);
// remove any stack trace
jsvRemoveNamedChild(execInfo.hiddenRoot, JSPARSE_STACKTRACE_VAR);
jsvObjectRemoveChild(execInfo.hiddenRoot, JSPARSE_STACKTRACE_VAR);
}
// Now clear the exception flag (it's handled - we hope!)
execInfo.execute = execInfo.execute & (JsExecFlags)~(EXEC_EXCEPTION|EXEC_ERROR_LINE_REPORTED);

View File

@ -2356,14 +2356,6 @@ void jsvRemoveAllChildren(JsVar *parent) {
}
}
void jsvRemoveNamedChild(JsVar *parent, const char *name) {
JsVar *child = jsvFindChildFromString(parent, name, false);
if (child) {
jsvRemoveChild(parent, child);
jsvUnLock(child);
}
}
/// Check if the given name is a child of the parent
bool jsvIsChild(JsVar *parent, JsVar *child) {
assert(jsvIsArray(parent) || jsvIsObject(parent));
@ -2412,6 +2404,14 @@ void jsvObjectSetChildAndUnLock(JsVar *obj, const char *name, JsVar *child) {
jsvUnLock(jsvObjectSetChild(obj, name, child));
}
void jsvObjectRemoveChild(JsVar *obj, const char *name) {
JsVar *child = jsvFindChildFromString(obj, name, false);
if (child) {
jsvRemoveChild(obj, child);
jsvUnLock(child);
}
}
int jsvGetChildren(JsVar *v) {
//OPT: could length be stored as the value of the array?
int children = 0;

View File

@ -653,7 +653,6 @@ JsVar *jsvFindChildFromVar(JsVar *parent, JsVar *childName, bool addIfNotFound);
/// Remove a child - note that the child MUST ACTUALLY BE A CHILD! and should be a name, not a value.
void jsvRemoveChild(JsVar *parent, JsVar *child);
void jsvRemoveAllChildren(JsVar *parent);
void jsvRemoveNamedChild(JsVar *parent, const char *name);
/// Get the named child of an object. If createChild!=0 then create the child
JsVar *jsvObjectGetChild(JsVar *obj, const char *name, JsVarFlags createChild);
@ -661,6 +660,8 @@ JsVar *jsvObjectGetChild(JsVar *obj, const char *name, JsVarFlags createChild);
JsVar *jsvObjectSetChild(JsVar *obj, const char *name, JsVar *child);
/// Set the named child of an object, and unlock the child
void jsvObjectSetChildAndUnLock(JsVar *obj, const char *name, JsVar *child);
/// Remove the named child of an Object
void jsvObjectRemoveChild(JsVar *parent, const char *name);
int jsvGetChildren(JsVar *v); ///< number of children of a variable. also see jsvGetArrayLength and jsvGetLength
JsVar *jsvGetFirstName(JsVar *v); ///< Get the first child's name from an object,array or function

View File

@ -1184,7 +1184,7 @@ this function.
void jswrap_espruino_setUSBHID(JsVar *arr) {
if (jsvIsUndefined(arr)) {
// Disable HID
jsvObjectSetChild(execInfo.hiddenRoot, JS_USB_HID_VAR_NAME, 0);
jsvObjectRemoveChild(execInfo.hiddenRoot, JS_USB_HID_VAR_NAME);
return;
}
if (!jsvIsObject(arr)) {

View File

@ -610,7 +610,7 @@ void jswrap_object_on(JsVar *parent, JsVar *event, JsVar *listener) {
JsVar *buf = jsvObjectGetChild(parent, STREAM_BUFFER_NAME, 0);
if (jsvIsString(buf)) {
jsiQueueObjectCallbacks(parent, STREAM_CALLBACK_NAME, &buf, 1);
jsvRemoveNamedChild(parent, STREAM_BUFFER_NAME);
jsvObjectRemoveChild(parent, STREAM_BUFFER_NAME);
}
jsvUnLock(buf);
}

View File

@ -44,7 +44,7 @@ static void handlePipeClose(JsVar *arr, JsvObjectIterator *it, JsVar* pipe) {
if (source && destination) {
JsVar *buffer = jsvObjectGetChild(source, STREAM_BUFFER_NAME, 0);
if (buffer && jsvGetStringLength(buffer)) {
jsvObjectSetChild(source, STREAM_BUFFER_NAME, 0); // remove outstanding data
jsvObjectRemoveChild(source, STREAM_BUFFER_NAME); // remove outstanding data
/* call write fn - we ignore drain/etc here because the source has
just closed and we want to get this sorted quickly */
JsVar *writeFunc = jspGetNamedField(destination, "write", false);

View File

@ -54,10 +54,10 @@ void _jswrap_promise_resolve_or_reject(JsVar *promise, JsVar *data, JsVar *fn) {
} else if (fn) {
result = jspExecuteFunction(fn, promise, 1, &data);
}
jsvObjectSetChild(promise, JS_PROMISE_THEN_NAME, 0); // remove 'resolve' and 'reject' handlers
jsvObjectSetChild(promise, JS_PROMISE_CATCH_NAME, 0); // remove 'resolve' and 'reject' handlers
jsvObjectRemoveChild(promise, JS_PROMISE_THEN_NAME); // remove 'resolve' and 'reject' handlers
jsvObjectRemoveChild(promise, JS_PROMISE_CATCH_NAME); // remove 'resolve' and 'reject' handlers
JsVar *chainedPromise = jsvObjectGetChild(promise, "chain", 0);
jsvObjectSetChild(promise, "chain", 0); // unlink chain
jsvObjectRemoveChild(promise, "chain"); // unlink chain
if (chainedPromise) {
JsVar *constr = jspGetConstructor(result);
if (constr && (void*)constr->varData.native.ptr==(void*)jswrap_promise_constructor) {
@ -134,7 +134,7 @@ void jswrap_promise_all_reject(JsVar *promise, JsVar *data) {
if (arr) {
// if not rejected before
jsvUnLock(arr);
jsvObjectSetChildAndUnLock(promise, JS_PROMISE_RESULT_NAME, 0);
jsvObjectRemoveChild(promise, JS_PROMISE_RESULT_NAME);
_jswrap_promise_queuereject(promise, data);
}
}

View File

@ -273,7 +273,7 @@ void jswrap_serial_setup(JsVar *parent, JsVar *baud, JsVar *options) {
if (options)
jsvObjectSetChildAndUnLock(parent, DEVICE_OPTIONS_NAME, options);
else
jsvRemoveNamedChild(parent, DEVICE_OPTIONS_NAME);
jsvObjectRemoveChild(parent, DEVICE_OPTIONS_NAME);
}

View File

@ -132,7 +132,7 @@ void jswrap_spi_setup(
if (options)
jsvUnLock(jsvSetNamedChild(parent, options, DEVICE_OPTIONS_NAME));
else
jsvRemoveNamedChild(parent, DEVICE_OPTIONS_NAME);
jsvObjectRemoveChild(parent, DEVICE_OPTIONS_NAME);
}
@ -554,7 +554,7 @@ void jswrap_i2c_setup(JsVar *parent, JsVar *options) {
if (options)
jsvUnLock(jsvSetNamedChild(parent, options, DEVICE_OPTIONS_NAME));
else
jsvRemoveNamedChild(parent, DEVICE_OPTIONS_NAME);
jsvObjectRemoveChild(parent, DEVICE_OPTIONS_NAME);
}
}

View File

@ -48,7 +48,7 @@ JsVar *jswrap_stream_read(JsVar *parent, JsVarInt chars) {
// return the whole buffer and remove it
data = buf;
buf = 0;
jsvRemoveNamedChild(parent, STREAM_BUFFER_NAME);
jsvObjectRemoveChild(parent, STREAM_BUFFER_NAME);
} else {
// return just part of the buffer, and shorten it accordingly
data = jsvNewFromStringVar(buf, 0, (size_t)chars);
@ -80,7 +80,7 @@ bool jswrap_stream_pushData(JsVar *parent, JsVar *dataString, bool force) {
if (!jsiExecuteEventCallback(parent, callback, 1, &dataString)) {
jsError("Error processing Serial data handler - removing it.");
jsErrorFlags |= JSERR_CALLBACK;
jsvRemoveNamedChild(parent, STREAM_CALLBACK_NAME);
jsvObjectRemoveChild(parent, STREAM_CALLBACK_NAME);
}
jsvUnLock(callback);
} else {

View File

@ -463,7 +463,7 @@ static void on_ble_evt(ble_evt_t * p_ble_evt)
srvcs = t;
}
bleCompleteTaskSuccess(BLETASK_PRIMARYSERVICE, srvcs);
jsvObjectSetChild(execInfo.hiddenRoot, "bleSvcs", 0);
jsvObjectRemoveChild(execInfo.hiddenRoot, "bleSvcs");
} // else error
jsvUnLock(srvcs);
break;
@ -515,7 +515,7 @@ static void on_ble_evt(ble_evt_t * p_ble_evt)
}
if (chars) bleCompleteTaskSuccess(BLETASK_CHARACTERISTIC, chars);
else bleCompleteTaskFail(BLETASK_CHARACTERISTIC, 0);
jsvObjectSetChild(execInfo.hiddenRoot, "bleChrs", 0);
jsvObjectRemoveChild(execInfo.hiddenRoot, "bleChrs");
}
jsvUnLock(chars);
break;