• I was testing espruino on the Linux command line (while waiting for my boards to be shipped) and ran across this error. Is this a bug, or a feature?

    $ espruino
    Interactive mode.
    Size of JsVar is now 32 bytes
    Size of JsVarRef is now 4 bytes
    Added SIGINT hook
    Added SIGHUP hook
    Added SIGTERM hook
    
     ____                 _ 
    |  __|___ ___ ___ _ _|_|___ ___ 
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v04.8 (c) 2019 G.Williams
    
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    
    >function foo(x,y) {
    :for (var i = 0; i < arguments.length; i++) {
    :console.log(arguments[i]);
    :}
    :}
    =function (x,y) { ... }
    >foo(1,2)
    1
    2
    =undefined
    >foo(100)
    100
    Uncaught ReferenceError: "1" is not defined
     at line 2 col 24
    console.log(arguments[i]);
                           ^
    in function "foo" called from line 1 col 8
    foo(100)
           ^
    >quit()
    =undefined
    >
    

    The source was cloned from github and compiled on Ubuntu 16.04.6 LTS. I got the same error on an OS X build as well. It was built with the following commands:

    export BOARD=LINUX
    source ./scripts/provision.sh $BOARD
     make clean && RELEASE=1 BOARD=$BOARD make
    

    I saw this post Functions with unknown amount of args. but I didn't get the same results.

  • Yes it's a bug. arguments.length is set to the number of arguments if the number of provided arguments is higher (and/or equal) to the number specified arguments in the function definition. But it is not set to the number of arguments if the number of provided arguments is smaller.

    As workaround define the function without arguments, arguments.lengthwill be always correct.

    function foo(x,y) {
      console.log("arguments.length=" + arguments.length);
      //console.log("x=" + x);
      //console.log("y=" + y);
      //for ( var i=0; i < arguments.length; i++ ) {
      //  console.log("typeof(arguments["+ i + "])=" + typeof(arguments[i]));
      //  console.log("arguments["+ i + "]=" + arguments[i]);
      //}
    }
    function bar() {
        console.log("arguments.length=" + arguments.length);
    }
    foo();
    foo(1);
    foo(1,2);
    foo(1,2,3);
    bar();
    bar(1);
    bar(1,2);
    bar(1,2,3);
    
About

Avatar for maze1980 @maze1980 started