jsvGetArrayLength return 1

Posted on
  • 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]);
    
  • And, if I disabled NEOPIXEL and GRAPHICS in "board.py", how to calculate how many variables could be add to "variables" item, thanks.

  • Hi,

    Maybe just try:

    ["arr","JsVar","An array"]
    

    not:

    ["arr","JsVarArray","An array"]
    

    JsVarArray is when you want all the arguments to the function to be put in an array, and for you to receive that array.

    For example with the code you're using require("crypto").test(1,2,3); would cause jsvGetArrayLength to return 3 because there are 3 arguments

  • Hello Gordon,
    Thanks for teaching me. I will continue coding. :D

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

jsvGetArrayLength return 1

Posted by Avatar for user130285 @user130285

Actions