/** Push data into a stream. To be used by Espruino (not a user). * This either calls the on('data') handler if it exists, or it * puts the data in a buffer. This MAY CLAIM the string that is * passed in. * * This will return true on success, or false if the buffer is * full. Setting force=true will attempt to fill the buffer as * full as possible, and will raise an error flag if data is lost. */ bool jswrap_stream_pushData(JsVar *parent, JsVar *dataString, bool force) { assert(jsvIsObject(parent)); assert(jsvIsString(dataString)); bool ok = true; JsVar *callback = jsvFindChildFromString(parent, STREAM_CALLBACK_NAME, false); if (callback) { if (!jsiExecuteEventCallback(parent, callback, dataString, 0)) { jsError("Error processing Serial data handler - removing it."); jsErrorFlags |= JSERR_CALLBACK; jsvRemoveNamedChild(parent, STREAM_CALLBACK_NAME); } jsvUnLock(callback); } else { // No callback - try and add buffer JsVar *buf = jsvObjectGetChild(parent, STREAM_BUFFER_NAME, 0); if (!jsvIsString(buf)) { // no buffer, just set this one up jsvObjectSetChild(parent, STREAM_BUFFER_NAME, dataString); } else { // append (if there is room!) size_t bufLen = jsvGetStringLength(buf); size_t dataLen = jsvGetStringLength(dataString); if (bufLen + dataLen > STREAM_MAX_BUFFER_SIZE) { if (force) jsErrorFlags |= JSERR_BUFFER_FULL; // jsWarn("String buffer overflowed maximum size (%d)", STREAM_MAX_BUFFER_SIZE); ok = false; } if ((ok || force) && (bufLen < STREAM_MAX_BUFFER_SIZE)) jsvAppendStringVar(buf, dataString, 0, STREAM_MAX_BUFFER_SIZE-bufLen); jsvUnLock(buf); } } return ok; }
void _jswrap_promise_reject(JsVar *promise, JsVar *data) { JsVar *fn = jsvObjectGetChild(promise, JS_PROMISE_CATCH_NAME, 0); jsiExecuteEventCallback(promise, fn, 1, &data); jsvUnLock(fn); }