-
-
Hi,
I would like to help Espruino enable to do more things, so I try to add other functions to it.
But when I coding, jsvGetArrayLength return 1 all the time.
Could anyone teach me for this, thanks.Here is the test code(added in the "jswrap_crypto.c"):
JsVar *jswrap_crypto_test(JsVar* arr){ printf("%d",jsvIsArray(arr)?1:0); //1 printf("%d",jsvGetLength(arr)); //1 printf("%d",jsvGetArrayLength(arr)); //1 printf("%d",jsvGetChildren(arr)); //1 return NULL; } /*JSON{ "type" : "staticmethod", "class" : "crypto", "name" : "test", "generate" : "jswrap_crypto_test", "params" : [ ["arr","JsVarArray","An array"] ], "return" : ["JsVar","A string"] } this is a test function */
And also copied from "jsvArrayJoin", and the while will run once.
JsVar *jswrap_crypto_test(JsVar* arr){ JsVar *str = jsvNewFromEmptyString(); if (!str) return 0; // out of memory JsvIterator it; jsvIteratorNew(&it, arr, JSIF_EVERY_ARRAY_ELEMENT); JsvStringIterator itdst; jsvStringIteratorNew(&itdst, str, 0); while (!jspIsInterrupted() && jsvIteratorHasElement(&it)) { JsVar *key = jsvIteratorGetKey(&it); if (jsvIsInt(key)) { // add the value //JsVar *value = jsvIteratorGetValue(&it); //if (value && !jsvIsNull(value)) { //JsVar *valueStr = jsvAsString(value); //if (valueStr) { // could be out of memory //jsvStringIteratorAppendString(&itdst, valueStr, 0, JSVAPPENDSTRINGVAR_MAXLENGTH); //jsvUnLock(valueStr); //} //} //jsvUnLock(value); printf("h\n"); } jsvUnLock(key); jsvIteratorNext(&it); } jsvIteratorFree(&it); jsvStringIteratorFree(&itdst); return str; } /*JSON{ "type" : "staticmethod", "class" : "crypto", "name" : "test", "generate" : "jswrap_crypto_test", "params" : [ ["arr","JsVarArray","An array"] ], "return" : ["JsVar","A string"] } this is a test function */
After make, directly run on the Ubuntu 18.04, the test code is:
require("crypto").test(new Array(1,2,3));
Or
require("crypto").test([1,2,3]);
Hello Gordon,
Thanks for teaching me. I will continue coding. :D