/*JSON{ "type":"method", "class": "ArrayBufferView", "name": "interpolate2d", "description" : "Interpolate between two adjacent values in the Typed Array", "generate" : "jswrap_arraybufferview_interpolate2d", "params" : [ [ "width", "int", "Integer 'width' of 2d array" ], [ "x", "float", "Floating point X index to access" ], [ "y", "float", "Floating point Y index to access" ] ], "return" : [ "float", "The result of interpolating in 2d between the 4 surrounding cells" ] }*/ JsVarFloat jswrap_arraybufferview_interpolate2d(JsVar *parent, JsVarInt width, JsVarFloat x, JsVarFloat y) { int yidx = (int)y; JsVarFloat ay = y-yidx; JsVarFloat findex = x + (JsVarFloat)(yidx*width); size_t idx = (size_t)findex; JsVarFloat ax = findex-(int)idx; JsvArrayBufferIterator it; jsvArrayBufferIteratorNew(&it, parent, idx); JsVarFloat xa,xb; int i; xa = jsvArrayBufferIteratorGetFloatValue(&it); jsvArrayBufferIteratorNext(&it); xb = jsvArrayBufferIteratorGetFloatValue(&it); JsVarFloat ya = xa*(1-ax) + xb*ax; for (i=1;i<width;i++) jsvArrayBufferIteratorNext(&it); xa = jsvArrayBufferIteratorGetFloatValue(&it); jsvArrayBufferIteratorNext(&it); xb = jsvArrayBufferIteratorGetFloatValue(&it); jsvArrayBufferIteratorFree(&it); JsVarFloat yb = xa*(1-ax) + xb*ax; return ya*(1-ay) + yb*ay; }
JsVar *jswrap_io_peek(JsVarInt addr, JsVarInt count, int wordSize) { if (count<=1) { return jsvNewFromLongInteger((long long)_jswrap_io_peek(addr, wordSize)); } else { JsVarDataArrayBufferViewType aType; // EDIT // switch (wordSize) { case 1: aType=ARRAYBUFFERVIEW_UINT8; break; case 2: aType=ARRAYBUFFERVIEW_UINT16; break; default: aType=ARRAYBUFFERVIEW_UINT32; break; } // if (wordSize==1) aType=ARRAYBUFFERVIEW_UINT8; // if (wordSize==2) aType=ARRAYBUFFERVIEW_UINT16; // if (wordSize==4) aType=ARRAYBUFFERVIEW_UINT32; JsVar *arr = jsvNewTypedArray(aType, count); if (!arr) return 0; JsvArrayBufferIterator it; jsvArrayBufferIteratorNew(&it, arr, 0); while (jsvArrayBufferIteratorHasElement(&it)) { jsvArrayBufferIteratorSetIntegerValue(&it, (JsVarInt)_jswrap_io_peek(addr, wordSize)); addr += wordSize; jsvArrayBufferIteratorNext(&it); } jsvArrayBufferIteratorFree(&it); return arr; } }
/*JSON{ "type" : "method", "class" : "I2C", "name" : "readFrom", "generate" : "jswrap_i2c_readFrom", "params" : [ ["address","JsVar","The 7 bit address of the device to request bytes from, or an object of the form `{address:12, stop:false}` to send this data without a STOP signal."], ["quantity","int32","The number of bytes to request"] ], "return" : ["JsVar","The data that was returned - as a Uint8Array"], "return_object" : "Uint8Array" } Request bytes from the given slave device, and return them as a Uint8Array (packed array of bytes). This is like using Arduino Wire's requestFrom, available and read functions. Sends a STOP */ JsVar *jswrap_i2c_readFrom(JsVar *parent, JsVar *addressVar, int nBytes) { IOEventFlags device = jsiGetDeviceFromClass(parent); if (!DEVICE_IS_I2C(device)) return 0; bool sendStop = true; int address = i2c_get_address(addressVar, &sendStop); if (nBytes<=0) return 0; if ((unsigned int)nBytes+256 > jsuGetFreeStack()) { jsExceptionHere(JSET_ERROR, "Not enough free stack to receive this amount of data"); return 0; } unsigned char *buf = (unsigned char *)alloca((size_t)nBytes); jshI2CRead(device, (unsigned char)address, nBytes, buf, sendStop); JsVar *array = jsvNewTypedArray(ARRAYBUFFERVIEW_UINT8, nBytes); if (array) { JsvArrayBufferIterator it; jsvArrayBufferIteratorNew(&it, array, 0); unsigned int i; for (i=0;i<(unsigned)nBytes;i++) { jsvArrayBufferIteratorSetByteValue(&it, (char)buf[i]); jsvArrayBufferIteratorNext(&it); } jsvArrayBufferIteratorFree(&it); } return array; }
/*JSON{ "type" : "method", "class" : "ArrayBufferView", "name" : "set", "generate" : "jswrap_arraybufferview_set", "params" : [ ["arr","JsVar","Floating point index to access"], ["offset","int32","The offset in this array at which to write the values (optional)"] ] } Copy the contents of `array` into this one, mapping `this[x+offset]=array[x];` */ void jswrap_arraybufferview_set(JsVar *parent, JsVar *arr, int offset) { if (!(jsvIsString(arr) || jsvIsArray(arr) || jsvIsArrayBuffer(arr))) { jsExceptionHere(JSET_ERROR, "Expecting first argument to be an array, not %t", arr); return; } JsvIterator itsrc; jsvIteratorNew(&itsrc, arr); JsvArrayBufferIterator itdst; jsvArrayBufferIteratorNew(&itdst, parent, (size_t)offset); bool useInts = !JSV_ARRAYBUFFER_IS_FLOAT(itdst.type) || jsvIsString(arr); while (jsvIteratorHasElement(&itsrc) && jsvArrayBufferIteratorHasElement(&itdst)) { if (useInts) { jsvArrayBufferIteratorSetIntegerValue(&itdst, jsvIteratorGetIntegerValue(&itsrc)); } else { JsVar *value = jsvIteratorGetValue(&itsrc); jsvArrayBufferIteratorSetValue(&itdst, value); jsvUnLock(value); } jsvArrayBufferIteratorNext(&itdst); jsvIteratorNext(&itsrc); } jsvArrayBufferIteratorFree(&itdst); jsvIteratorFree(&itsrc); }
void jsvIteratorNext(JsvIterator *it) { switch (it->type) { case JSVI_OBJECT : jsvObjectIteratorNext(&it->it.obj); break; case JSVI_STRING : jsvStringIteratorNext(&it->it.str); break; case JSVI_ARRAYBUFFER : jsvArrayBufferIteratorNext(&it->it.buf); break; default: assert(0); break; } }
void jswrap_spi_send_cb(int c, jswrap_spi_send_data *data) { int result = data->spiSend(c, &data->spiSendData); if (c>=0) data->txAmt++; if (result>=0) { jsvArrayBufferIteratorSetByteValue(&data->it, (char)result); jsvArrayBufferIteratorNext(&data->it); data->rxAmt++; } }
/*JSON{ "type":"method", "class": "ArrayBufferView", "name": "interpolate", "description" : "Interpolate between two adjacent values in the Typed Array", "generate" : "jswrap_arraybufferview_interpolate", "params" : [ [ "index", "float", "Floating point index to access" ] ], "return" : [ "float", "The result of interpolating between (int)index and (int)(index+1)" ] }*/ JsVarFloat jswrap_arraybufferview_interpolate(JsVar *parent, JsVarFloat findex) { size_t idx = (size_t)findex; JsVarFloat a = findex - (int)idx; JsvArrayBufferIterator it; jsvArrayBufferIteratorNew(&it, parent, idx); JsVarFloat fa = jsvArrayBufferIteratorGetFloatValue(&it); jsvArrayBufferIteratorNext(&it); JsVarFloat fb = jsvArrayBufferIteratorGetFloatValue(&it); jsvArrayBufferIteratorFree(&it); return fa*(1-a) + fb*a; }
/*JSON{ "type" : "method", "class" : "ArrayBufferView", "name" : "map", "generate" : "jswrap_arraybufferview_map", "params" : [ ["function","JsVar","Function used to map one item to another"], ["thisArg","JsVar","if specified, the function is called with 'this' set to thisArg (optional)"] ], "return" : ["JsVar","An array containing the results"], "return_object" : "ArrayBufferView" } Return an array which is made from the following: ```A.map(function) = [function(A[0]), function(A[1]), ...]``` **Note:** This returns an ArrayBuffer of the same type it was called on. To get an Array, use `Array.prototype.map` */ JsVar *jswrap_arraybufferview_map(JsVar *parent, JsVar *funcVar, JsVar *thisVar) { if (!jsvIsArrayBuffer(parent)) { jsExceptionHere(JSET_ERROR, "ArrayBufferView.map can only be called on an ArrayBufferView"); return 0; } if (!jsvIsFunction(funcVar)) { jsExceptionHere(JSET_ERROR, "ArrayBufferView.map's first argument should be a function"); return 0; } if (!jsvIsUndefined(thisVar) && !jsvIsObject(thisVar)) { jsExceptionHere(JSET_ERROR, "ArrayBufferView.map's second argument should be undefined, or an object"); return 0; } // create ArrayBuffer result JsVarDataArrayBufferViewType arrayBufferType = parent->varData.arraybuffer.type; JsVar *arrayBufferLength = jsvNewFromInteger((JsVarInt)jsvGetArrayBufferLength(parent)); JsVar *array = jswrap_typedarray_constructor(arrayBufferType, arrayBufferLength, 0, 0); jsvUnLock(arrayBufferLength); if (!array) return 0; // now iterate JsvIterator it; // TODO: if we really are limited to ArrayBuffers, this could be an ArrayBufferIterator. jsvIteratorNew(&it, parent); JsvArrayBufferIterator itdst; jsvArrayBufferIteratorNew(&itdst, array, 0); while (jsvIteratorHasElement(&it)) { JsVar *index = jsvIteratorGetKey(&it); if (jsvIsInt(index)) { JsVarInt idxValue = jsvGetInteger(index); JsVar *args[3], *mapped; args[0] = jsvIteratorGetValue(&it); args[1] = jsvNewFromInteger(idxValue); // child is a variable name, create a new variable for the index args[2] = parent; mapped = jspeFunctionCall(funcVar, 0, thisVar, false, 3, args); jsvUnLock(args[0]); jsvUnLock(args[1]); if (mapped) { jsvArrayBufferIteratorSetValue(&itdst, mapped); jsvUnLock(mapped); } } jsvUnLock(index); jsvIteratorNext(&it); jsvArrayBufferIteratorNext(&itdst); } jsvIteratorFree(&it); jsvArrayBufferIteratorFree(&itdst); return array; }
void jsvIteratorNext(JsvIterator *it) { switch (it->type) { case JSVI_FULLARRAY: it->it.obj.index++; if (jsvIsIntegerish(it->it.obj.it.var) && jsvGetInteger(it->it.obj.it.var)<it->it.obj.index) jsvObjectIteratorNext(&it->it.obj.it); break; case JSVI_OBJECT : jsvObjectIteratorNext(&it->it.obj.it); break; case JSVI_STRING : jsvStringIteratorNext(&it->it.str); break; case JSVI_ARRAYBUFFER : jsvArrayBufferIteratorNext(&it->it.buf); break; default: assert(0); break; } }
/** * Send a single byte to the SPI device, used ad callback. */ void jswrap_spi_send_cb( int c, //!< The byte to send through SPI. jswrap_spi_send_data *data //!< Control information on how to send to SPI. ) { // Invoke the SPI send function to transmit the single byte. int result = data->spiSend(c, &data->spiSendData); if (c>=0) data->txAmt++; if (result>=0) { jsvArrayBufferIteratorSetByteValue(&data->it, (char)result); jsvArrayBufferIteratorNext(&data->it); data->rxAmt++; } }
/*JSON{ "type" : "staticmethod", "class" : "E", "name" : "interpolate2d", "ifndef" : "SAVE_ON_FLASH", "generate" : "jswrap_espruino_interpolate2d", "params" : [ ["array","JsVar","A Typed Array to interpolate between"], ["width","int32","Integer 'width' of 2d array"], ["x","float","Floating point X index to access"], ["y","float","Floating point Y index to access"] ], "return" : ["float","The result of interpolating in 2d between the 4 surrounding cells"] } Interpolate between four adjacent values in the Typed Array, in 2D. */ JsVarFloat jswrap_espruino_interpolate2d(JsVar *array, int width, JsVarFloat x, JsVarFloat y) { if (!jsvIsArrayBuffer(array)) return 0; int yidx = (int)y; JsVarFloat ay = y-yidx; if (y<0) { yidx = 0; ay = 0; } JsVarFloat findex = x + (JsVarFloat)(yidx*width); size_t idx = (size_t)findex; JsVarFloat ax = findex-(int)idx; if (x<0) { idx = (size_t)(yidx*width); ax = 0; } JsvArrayBufferIterator it; jsvArrayBufferIteratorNew(&it, array, idx); JsVarFloat xa,xb; int i; xa = jsvArrayBufferIteratorGetFloatValue(&it); jsvArrayBufferIteratorNext(&it); xb = jsvArrayBufferIteratorGetFloatValue(&it); JsVarFloat ya = xa*(1-ax) + xb*ax; for (i=1;i<width;i++) jsvArrayBufferIteratorNext(&it); xa = jsvArrayBufferIteratorGetFloatValue(&it); jsvArrayBufferIteratorNext(&it); xb = jsvArrayBufferIteratorGetFloatValue(&it); jsvArrayBufferIteratorFree(&it); JsVarFloat yb = xa*(1-ax) + xb*ax; return ya*(1-ay) + yb*ay; }
/*JSON{ "type" : "method", "class" : "OneWire", "name" : "read", "generate" : "jswrap_onewire_read", "params" : [["count","JsVar","(optional) The amount of bytes to read"]], "return" : ["JsVar","The byte that was read, or a Uint8Array if count was specified and >=0"] } Read a byte */ JsVar *jswrap_onewire_read(JsVar *parent, JsVar *count) { Pin pin = onewire_getpin(parent); if (!jshIsPinValid(pin)) return 0; if (jsvIsNumeric(count)) { JsVarInt c = jsvGetInteger(count); JsVar *arr = jsvNewTypedArray(ARRAYBUFFERVIEW_UINT8, c); if (!arr) return 0; JsvArrayBufferIterator it; jsvArrayBufferIteratorNew(&it, arr, 0); while (c--) { jsvArrayBufferIteratorSetByteValue(&it, (char)OneWireRead(pin, 8)); jsvArrayBufferIteratorNext(&it); } jsvArrayBufferIteratorFree(&it); return arr; } else { return jsvNewFromInteger(OneWireRead(pin, 8)); } }
/*JSON{ "type" : "staticmethod", "class" : "E", "name" : "interpolate", "ifndef" : "SAVE_ON_FLASH", "generate" : "jswrap_espruino_interpolate", "params" : [ ["array","JsVar","A Typed Array to interpolate between"], ["index","float","Floating point index to access"] ], "return" : ["float","The result of interpolating between (int)index and (int)(index+1)"] } Interpolate between two adjacent values in the Typed Array */ JsVarFloat jswrap_espruino_interpolate(JsVar *array, JsVarFloat findex) { if (!jsvIsArrayBuffer(array)) return 0; size_t idx = (size_t)findex; JsVarFloat a = findex - (int)idx; if (findex<0) { idx = 0; a = 0; } if (findex>=jsvGetArrayBufferLength(array)-1) { idx = jsvGetArrayBufferLength(array)-1; a = 0; } JsvArrayBufferIterator it; jsvArrayBufferIteratorNew(&it, array, idx); JsVarFloat fa = jsvArrayBufferIteratorGetFloatValue(&it); jsvArrayBufferIteratorNext(&it); JsVarFloat fb = jsvArrayBufferIteratorGetFloatValue(&it); jsvArrayBufferIteratorFree(&it); return fa*(1-a) + fb*a; }
/** * \brief Iterate over the contents of the content of a variable, calling callback for each. * Contents may be: * * numeric -> output * * a string -> output each character * * array/arraybuffer -> call itself on each element * object -> call itself object.count times, on object.data */ bool jsvIterateCallback( JsVar *data, // The data to iterate over. void (*callback)(int item, void *callbackData), // The callback function invoke. void *callbackData // Data to be passed to the callback function ) { bool ok = true; // Handle the data being a single numeric. if (jsvIsNumeric(data)) { callback((int)jsvGetInteger(data), callbackData); } // Handle the data being an object. else if (jsvIsObject(data)) { JsVar *countVar = jsvObjectGetChild(data, "count", 0); JsVar *dataVar = jsvObjectGetChild(data, "data", 0); if (countVar && dataVar && jsvIsNumeric(countVar)) { int n = (int)jsvGetInteger(countVar); while (ok && n-- > 0) { ok = jsvIterateCallback(dataVar, callback, callbackData); } } else { jsWarn("If specifying an object, it must be of the form {data : ..., count : N}"); } jsvUnLock2(countVar, dataVar); } // Handle the data being a string else if (jsvIsString(data)) { JsvStringIterator it; jsvStringIteratorNew(&it, data, 0); while (jsvStringIteratorHasChar(&it) && ok) { char ch = jsvStringIteratorGetChar(&it); callback(ch, callbackData); jsvStringIteratorNext(&it); } jsvStringIteratorFree(&it); } // Handle the data being an array buffer else if (jsvIsArrayBuffer(data)) { JsvArrayBufferIterator it; jsvArrayBufferIteratorNew(&it, data, 0); if (JSV_ARRAYBUFFER_GET_SIZE(it.type) == 1 && !JSV_ARRAYBUFFER_IS_SIGNED(it.type)) { // faster for single byte arrays. while (jsvArrayBufferIteratorHasElement(&it)) { callback((int)(unsigned char)jsvStringIteratorGetChar(&it.it), callbackData); jsvArrayBufferIteratorNext(&it); } } else { while (jsvArrayBufferIteratorHasElement(&it)) { callback((int)jsvArrayBufferIteratorGetIntegerValue(&it), callbackData); jsvArrayBufferIteratorNext(&it); } } jsvArrayBufferIteratorFree(&it); } // Handle the data being iterable else if (jsvIsIterable(data)) { JsvIterator it; jsvIteratorNew(&it, data); while (jsvIteratorHasElement(&it) && ok) { JsVar *el = jsvIteratorGetValue(&it); ok = jsvIterateCallback(el, callback, callbackData); jsvUnLock(el); jsvIteratorNext(&it); } jsvIteratorFree(&it); } else { jsWarn("Expecting a number or something iterable, got %t", data); ok = false; } return ok; }
void jsfGetJSONWithCallback(JsVar *var, JSONFlags flags, const char *whitespace, vcbprintf_callback user_callback, void *user_data) { JSONFlags nflags = flags + JSON_INDENT; // if we add a newline, make sure we indent any subsequent JSON more if (!whitespace) whitespace=" "; if (jsvIsUndefined(var)) { cbprintf(user_callback, user_data, "undefined"); } else { // Use IS_RECURSING flag to stop recursion if (var->flags & JSV_IS_RECURSING) { cbprintf(user_callback, user_data, " ... "); return; } var->flags |= JSV_IS_RECURSING; if (jsvIsArray(var)) { JsVarInt length = jsvGetArrayLength(var); bool limited = (flags&JSON_LIMIT) && (length>(JsVarInt)JSON_LIMIT_AMOUNT); bool needNewLine = false; cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?"[ ":"["); JsVarInt lastIndex = -1; bool numeric = true; bool first = true; JsvObjectIterator it; jsvObjectIteratorNew(&it, var); while (lastIndex+1<length && numeric && !jspIsInterrupted()) { JsVar *key = jsvObjectIteratorGetKey(&it); if (!jsvObjectIteratorHasValue(&it) || jsvIsNumeric(key)) { JsVarInt index = jsvObjectIteratorHasValue(&it) ? jsvGetInteger(key) : length-1; JsVar *item = jsvObjectIteratorGetValue(&it); while (lastIndex < index) { lastIndex++; if (!limited || lastIndex<(JsVarInt)JSON_LIMITED_AMOUNT || lastIndex>=length-(JsVarInt)JSON_LIMITED_AMOUNT) { if (!first) cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?", ":","); first = false; if (limited && lastIndex==length-(JsVarInt)JSON_LIMITED_AMOUNT) cbprintf(user_callback, user_data, JSON_LIMIT_TEXT); bool newNeedsNewLine = ((flags&JSON_SOME_NEWLINES) && jsonNeedsNewLine(item)); if (flags&JSON_ALL_NEWLINES) { needNewLine = true; newNeedsNewLine = true; } if (needNewLine || newNeedsNewLine) { jsonNewLine(nflags, whitespace, user_callback, user_data); needNewLine = false; } if (lastIndex == index) jsfGetJSONWithCallback(item, nflags, whitespace, user_callback, user_data); else cbprintf(user_callback, user_data, (flags&JSON_NO_UNDEFINED)?"null":"undefined"); needNewLine = newNeedsNewLine; } } jsvUnLock(item); jsvObjectIteratorNext(&it); } else { numeric = false; } jsvUnLock(key); } // non-numeric - but NOT for standard JSON if ((flags&JSON_PRETTY)) jsfGetJSONForObjectItWithCallback(&it, flags, whitespace, nflags, user_callback, user_data, first); jsvObjectIteratorFree(&it); if (needNewLine) jsonNewLine(flags, whitespace, user_callback, user_data); cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?" ]":"]"); } else if (jsvIsArrayBuffer(var)) { JsvArrayBufferIterator it; bool allZero = true; jsvArrayBufferIteratorNew(&it, var, 0); while (jsvArrayBufferIteratorHasElement(&it)) { if (jsvArrayBufferIteratorGetFloatValue(&it)!=0) allZero = false; jsvArrayBufferIteratorNext(&it); } jsvArrayBufferIteratorFree(&it); bool asArray = flags&JSON_ARRAYBUFFER_AS_ARRAY; if (allZero && !asArray) { cbprintf(user_callback, user_data, "new %s(%d)", jswGetBasicObjectName(var), jsvGetArrayBufferLength(var)); } else { const char *aname = jswGetBasicObjectName(var); /* You can't do `new ArrayBuffer([1,2,3])` so we have to output * `new Uint8Array([1,2,3]).buffer`! */ bool isBasicArrayBuffer = strcmp(aname,"ArrayBuffer")==0; if (isBasicArrayBuffer) { aname="Uint8Array"; } cbprintf(user_callback, user_data, asArray?"[":"new %s([", aname); if (flags&JSON_ALL_NEWLINES) jsonNewLine(nflags, whitespace, user_callback, user_data); size_t length = jsvGetArrayBufferLength(var); bool limited = (flags&JSON_LIMIT) && (length>JSON_LIMIT_AMOUNT); // no newlines needed for array buffers as they only contain simple stuff jsvArrayBufferIteratorNew(&it, var, 0); while (jsvArrayBufferIteratorHasElement(&it) && !jspIsInterrupted()) { if (!limited || it.index<JSON_LIMITED_AMOUNT || it.index>=length-JSON_LIMITED_AMOUNT) { if (it.index>0) cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?", ":","); if (flags&JSON_ALL_NEWLINES) jsonNewLine(nflags, whitespace, user_callback, user_data); if (limited && it.index==length-JSON_LIMITED_AMOUNT) cbprintf(user_callback, user_data, JSON_LIMIT_TEXT); JsVar *item = jsvArrayBufferIteratorGetValue(&it); jsfGetJSONWithCallback(item, nflags, whitespace, user_callback, user_data); jsvUnLock(item); } jsvArrayBufferIteratorNext(&it); } if (flags&JSON_ALL_NEWLINES) jsonNewLine(flags, whitespace, user_callback, user_data); jsvArrayBufferIteratorFree(&it); cbprintf(user_callback, user_data, asArray?"]":"])"); if (isBasicArrayBuffer && !asArray) cbprintf(user_callback, user_data, ".buffer"); } } else if (jsvIsObject(var)) { IOEventFlags device = (flags & JSON_SHOW_DEVICES) ? jsiGetDeviceFromClass(var) : EV_NONE; if (device!=EV_NONE) { cbprintf(user_callback, user_data, "%s", jshGetDeviceString(device)); } else { bool showContents = true; if (flags & JSON_SHOW_OBJECT_NAMES) { JsVar *proto = jsvObjectGetChild(var, JSPARSE_INHERITS_VAR, 0); if (jsvHasChildren(proto)) { JsVar *constr = jsvObjectGetChild(proto, JSPARSE_CONSTRUCTOR_VAR, 0); if (constr) { JsVar *p = jsvGetIndexOf(execInfo.root, constr, true); if (p) cbprintf(user_callback, user_data, "%v: ", p); jsvUnLock2(p,constr); /* We had the constructor - now if there was a non-default toString function * we'll execute it and print the result */ JsVar *toStringFn = jspGetNamedField(var, "toString", false); if (toStringFn && toStringFn->varData.native.ptr != (void (*)(void))jswrap_object_toString) { // Function found and it's not the default one - execute it JsVar *result = jspExecuteFunction(toStringFn,var,0,0); cbprintf(user_callback, user_data, "%v", result); jsvUnLock(result); showContents = false; // we already printed something } jsvUnLock(toStringFn); } } jsvUnLock(proto); } if (showContents) { JsvObjectIterator it; jsvObjectIteratorNew(&it, var); cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?"{ ":"{"); bool needNewLine = jsfGetJSONForObjectItWithCallback(&it, flags, whitespace, nflags, user_callback, user_data, true); jsvObjectIteratorFree(&it); if (needNewLine) jsonNewLine(flags, whitespace, user_callback, user_data); cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?" }":"}"); } } } else if (jsvIsFunction(var)) { if (flags & JSON_IGNORE_FUNCTIONS) { cbprintf(user_callback, user_data, "undefined"); } else { cbprintf(user_callback, user_data, "function "); jsfGetJSONForFunctionWithCallback(var, nflags, user_callback, user_data); } } else if (jsvIsString(var) && !jsvIsName(var)) { if ((flags&JSON_LIMIT) && jsvGetStringLength(var)>JSON_LIMIT_STRING_AMOUNT) { // if the string is too big, split it and put dots in the middle JsVar *var1 = jsvNewFromStringVar(var, 0, JSON_LIMITED_STRING_AMOUNT); JsVar *var2 = jsvNewFromStringVar(var, jsvGetStringLength(var)-JSON_LIMITED_STRING_AMOUNT, JSON_LIMITED_STRING_AMOUNT); cbprintf(user_callback, user_data, "%q%s%q", var1, JSON_LIMIT_TEXT, var2); jsvUnLock2(var1, var2); } else { cbprintf(user_callback, user_data, "%q", var); } } else { cbprintf(user_callback, user_data, "%v", var); } var->flags &= ~JSV_IS_RECURSING; } }
void jsfGetJSONWithCallback(JsVar *var, JSONFlags flags, vcbprintf_callback user_callback, void *user_data) { JSONFlags nflags = flags + JSON_INDENT; // if we add a newline, make sure we indent any subsequent JSON more if (jsvIsUndefined(var)) { cbprintf(user_callback, user_data, "undefined"); } else { // Use IS_RECURSING flag to stop recursion if (var->flags & JSV_IS_RECURSING) { cbprintf(user_callback, user_data, " ... "); return; } var->flags |= JSV_IS_RECURSING; if (jsvIsArray(var)) { size_t length = (size_t)jsvGetArrayLength(var); bool limited = (flags&JSON_LIMIT) && (length>JSON_LIMIT_AMOUNT); bool needNewLine = false; size_t i; cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?"[ ":"["); for (i=0;i<length && !jspIsInterrupted();i++) { if (!limited || i<JSON_LIMITED_AMOUNT || i>=length-JSON_LIMITED_AMOUNT) { if (i>0) cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?", ":","); if (limited && i==length-JSON_LIMITED_AMOUNT) { if (needNewLine) jsonNewLine(nflags, user_callback, user_data); cbprintf(user_callback, user_data, JSON_LIMIT_TEXT); } JsVar *item = jsvGetArrayItem(var, (JsVarInt)i); if (jsvIsUndefined(item) && (flags&JSON_NO_UNDEFINED)) item = jsvNewWithFlags(JSV_NULL); bool newNeedsNewLine = (flags&JSON_NEWLINES) && jsonNeedsNewLine(item); if (needNewLine || newNeedsNewLine) { jsonNewLine(nflags, user_callback, user_data); needNewLine = false; } jsfGetJSONWithCallback(item, nflags, user_callback, user_data); needNewLine = newNeedsNewLine; jsvUnLock(item); } } if (needNewLine) jsonNewLine(flags, user_callback, user_data); cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?" ]":"]"); } else if (jsvIsArrayBuffer(var)) { JsvArrayBufferIterator it; bool allZero = true; jsvArrayBufferIteratorNew(&it, var, 0); while (jsvArrayBufferIteratorHasElement(&it)) { if (jsvArrayBufferIteratorGetFloatValue(&it)!=0) allZero = false; jsvArrayBufferIteratorNext(&it); } jsvArrayBufferIteratorFree(&it); if (allZero) { cbprintf(user_callback, user_data, "new %s(%d)", jswGetBasicObjectName(var), jsvGetArrayBufferLength(var)); } else { cbprintf(user_callback, user_data, "new %s([", jswGetBasicObjectName(var)); size_t length = jsvGetArrayBufferLength(var); bool limited = (flags&JSON_LIMIT) && (length>JSON_LIMIT_AMOUNT); // no newlines needed for array buffers as they only contain simple stuff jsvArrayBufferIteratorNew(&it, var, 0); while (jsvArrayBufferIteratorHasElement(&it) && !jspIsInterrupted()) { if (!limited || it.index<JSON_LIMITED_AMOUNT || it.index>=length-JSON_LIMITED_AMOUNT) { if (it.index>0) cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?", ":","); if (limited && it.index==length-JSON_LIMITED_AMOUNT) cbprintf(user_callback, user_data, JSON_LIMIT_TEXT); JsVar *item = jsvArrayBufferIteratorGetValue(&it); jsfGetJSONWithCallback(item, nflags, user_callback, user_data); jsvUnLock(item); } jsvArrayBufferIteratorNext(&it); } jsvArrayBufferIteratorFree(&it); cbprintf(user_callback, user_data, "])"); } } else if (jsvIsObject(var)) { IOEventFlags device = (flags & JSON_SHOW_DEVICES) ? jsiGetDeviceFromClass(var) : EV_NONE; if (device!=EV_NONE) { cbprintf(user_callback, user_data, "%s", jshGetDeviceString(device)); } else { bool first = true; bool needNewLine = false; size_t sinceNewLine = 0; JsvObjectIterator it; jsvObjectIteratorNew(&it, var); cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?"{ ":"{"); while (jsvObjectIteratorHasValue(&it) && !jspIsInterrupted()) { JsVar *index = jsvObjectIteratorGetKey(&it); JsVar *item = jsvObjectIteratorGetValue(&it); bool hidden = jsvIsInternalObjectKey(index) || ((flags & JSON_IGNORE_FUNCTIONS) && jsvIsFunction(item)) || ((flags&JSON_NO_UNDEFINED) && jsvIsUndefined(item)); if (!hidden) { sinceNewLine++; if (!first) cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?", ":","); bool newNeedsNewLine = (flags&JSON_NEWLINES) && jsonNeedsNewLine(item); if ((flags&JSON_NEWLINES) && sinceNewLine>JSON_ITEMS_ON_LINE_OBJECT) needNewLine = true; if (needNewLine || newNeedsNewLine) { jsonNewLine(nflags, user_callback, user_data); needNewLine = false; sinceNewLine = 0; } cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?"%q: ":"%q:", index); if (first) first = false; jsfGetJSONWithCallback(item, nflags, user_callback, user_data); needNewLine = newNeedsNewLine; } jsvUnLock(index); jsvUnLock(item); jsvObjectIteratorNext(&it); } jsvObjectIteratorFree(&it); if (needNewLine) jsonNewLine(flags, user_callback, user_data); cbprintf(user_callback, user_data, (flags&JSON_PRETTY)?" }":"}"); } } else if (jsvIsFunction(var)) { if (flags & JSON_IGNORE_FUNCTIONS) { cbprintf(user_callback, user_data, "undefined"); } else { cbprintf(user_callback, user_data, "function "); jsfGetJSONForFunctionWithCallback(var, nflags, user_callback, user_data); } } else if (jsvIsString(var) && !jsvIsName(var)) { if ((flags&JSON_LIMIT) && jsvGetStringLength(var)>JSON_LIMIT_STRING_AMOUNT) { // if the string is too big, split it and put dots in the middle JsVar *var1 = jsvNewFromStringVar(var, 0, JSON_LIMITED_STRING_AMOUNT); JsVar *var2 = jsvNewFromStringVar(var, jsvGetStringLength(var)-JSON_LIMITED_STRING_AMOUNT, JSON_LIMITED_STRING_AMOUNT); cbprintf(user_callback, user_data, "%q%s%q", var1, JSON_LIMIT_TEXT, var2); jsvUnLock(var1); jsvUnLock(var2); } else { cbprintf(user_callback, user_data, "%q", var); } } else { cbprintf(user_callback, user_data, "%v", var); } var->flags &= ~JSV_IS_RECURSING; } }
/** Iterate over the contents of the content of a variable, calling callback for each. Contents may be: * numeric -> output * a string -> output each character * array/arraybuffer -> call itself on each element * {data:..., count:...} -> call itself object.count times, on object.data * {callback:...} -> call the given function, call itself on return value */ bool jsvIterateCallback( JsVar *data, jsvIterateCallbackFn callback, void *callbackData ) { bool ok = true; // Handle the data being a single numeric. if (jsvIsNumeric(data)) { callback((int)jsvGetInteger(data), callbackData); } // Handle the data being an object. else if (jsvIsObject(data)) { JsVar *callbackVar = jsvObjectGetChild(data, "callback", 0); if (jsvIsFunction(callbackVar)) { JsVar *result = jspExecuteFunction(callbackVar,0,0,NULL); jsvUnLock(callbackVar); if (result) { bool r = jsvIterateCallback(result, callback, callbackData); jsvUnLock(result); return r; } return true; } jsvUnLock(callbackVar); JsVar *countVar = jsvObjectGetChild(data, "count", 0); JsVar *dataVar = jsvObjectGetChild(data, "data", 0); if (countVar && dataVar && jsvIsNumeric(countVar)) { int n = (int)jsvGetInteger(countVar); while (ok && n-- > 0) { ok = jsvIterateCallback(dataVar, callback, callbackData); } } else { jsExceptionHere(JSET_TYPEERROR, "If specifying an object, it must be of the form {data : ..., count : N} or {callback : fn} - got %j", data); ok = false; } jsvUnLock2(countVar, dataVar); } // Handle the data being a string else if (jsvIsString(data)) { JsvStringIterator it; jsvStringIteratorNew(&it, data, 0); while (jsvStringIteratorHasChar(&it) && ok) { char ch = jsvStringIteratorGetChar(&it); callback(ch, callbackData); jsvStringIteratorNext(&it); } jsvStringIteratorFree(&it); } // Handle the data being an array buffer else if (jsvIsArrayBuffer(data)) { JsvArrayBufferIterator it; jsvArrayBufferIteratorNew(&it, data, 0); if (JSV_ARRAYBUFFER_GET_SIZE(it.type) == 1 && !JSV_ARRAYBUFFER_IS_SIGNED(it.type)) { JsvStringIterator *sit = &it.it; // faster for single byte arrays - read using the string iterator. while (jsvStringIteratorHasChar(sit)) { callback((int)(unsigned char)jsvStringIteratorGetChar(sit), callbackData); jsvStringIteratorNextInline(sit); } } else { while (jsvArrayBufferIteratorHasElement(&it)) { callback((int)jsvArrayBufferIteratorGetIntegerValue(&it), callbackData); jsvArrayBufferIteratorNext(&it); } } jsvArrayBufferIteratorFree(&it); } // Handle the data being iterable else if (jsvIsIterable(data)) { JsvIterator it; jsvIteratorNew(&it, data, JSIF_EVERY_ARRAY_ELEMENT); while (jsvIteratorHasElement(&it) && ok) { JsVar *el = jsvIteratorGetValue(&it); ok = jsvIterateCallback(el, callback, callbackData); jsvUnLock(el); jsvIteratorNext(&it); } jsvIteratorFree(&it); } else { jsExceptionHere(JSET_TYPEERROR, "Expecting a number or something iterable, got %t", data); ok = false; } return ok; }