4 Replies Latest reply on Feb 14, 2017 1:27 PM by Wiedenmaier

    $.stack is not a string

    K.Daube Level 1

      ESTK and FDK documentation and even typeof claims that $.stack is a string. If it is printed with $.writeline it behaves as such and creates multiple lines.
      I want to build a function which can be called in any routine and will then indicate the calling level by indentation and the name of the function.
      For that puprose I wanted to split the 'string' into an array, the length of which indicating the nesting level of the function in question.
      But it turns out that $.stack can not be handled as a string:

       

      function ZTrace () {
      // $.stack is not a string - what is it?
      var level, aStack = [], s1Stack, s2Stack;
        s1Stack = toHex($.stack);
        $.writeln (s1Stack);                    // see what it looks like     
        s2Stack = $.stack.toString();           // sStack behaves as stack
        aStack = s2Stack.split (hex2a("829a")); // does not split
        level = aStack.length;                  // always 1
        $.writeln (aStack[0]);                  // always full stack
        $.writeln (aStack[1]);                  // undefined
      } // --- end ZTrace
      
      function toHex(str) {
          var hex = '';
          for(var i=0;i<str.length;i++) {
              hex += ''+str.charCodeAt(i).toString(16);
          }
          return hex;
      }
      function hex2a(hexx) {
      // http://stackoverflow.com/questions/3745666/how-to-convert-from-hex-to-ascii-in-javascript
      // hex2a('32343630'); // returns '2460'
          var hex = hexx.toString();//force conversion
          var str = '';
          for (var i = 0; i < hex.length; i += 2)
              str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
          return str;
      }
      

       

      For example, in a nested function calling ZTrace () will create a stack like this

       

      [ZTrace-test.jsx]
      Level_0()
      Level_1()
      Level_2()
      Level_3()
      ZTrace()
      

       

      But the line 05 creates (here both in hex and interpreted):

       

      5b5a54726163652e6a73785da4c6576656c5f302829a4c6576656c5f312829a4c6576656c5f322829a4c6576656c5f332829a5a54726163652829a
      [ Z T r a c e . j s x ] ¤ Æ W f u Å ó ??????L e v e l _ 1 ( ) ¤ Æ W f V Å ó ??????L e v e l _ 3 ( ) ¥ ¥ G ????6 R ????
      


      Now, of what type is $.stack?

        • 1. Re: $.stack is not a string
          Wiedenmaier Level 3

          Hi Klaus,

           

          $.stack is a string

           

          $.writeln(typeof($.stack)) prints "string"

           

          This

          aStack = s2Stack.split ("\n");

          does exactly, what I expected. I get an Array of lines, and the following lines of code deliver the expected results.

           

            level = aStack.length;             

            $.writeln (aStack[0]);                 

            $.writeln (aStack[1]);               

           

          Last item in Array is an empty string, so level is aStack.length - 1 and perhaps -1 for the Name of the script file.

           

          Not sure why you are doing this hex stuff. I think this is not necessary.

          In your case hex2a allways delivers an empty string, and this is why aStack.length is always 1.

           

          hope this helps

          Markus

          • 2. Re: $.stack is not a string
            Wiedenmaier Level 3

            BTW:

            str += String.fromCharCode(hex.substr(i, 2)); 

            always delivers two not visible characters for "829a" assuming these two characters are not defined as charater within Unicode (here I was wrong as I said string is empty)

             

            hex2a("647E")

            works and delivers "d~" as there are characters defined

             

            and

            hex2a("0a")

            delivers the linefeed and Splitting works

             

            var del = hex2a("0a");

            Stack = s2Stack.split (del);

             

            but this is equal to

            Stack = s2Stack.split ("\n");

            • 3. Re: $.stack is not a string
              K.Daube Level 1

              Thanks Markus,

               

              The hex business 'was necessary' because initially I did not split with "\n", but with "\\n" - which obviously is wrong... Analysing what is really in the string lead me to this erraneous deviation.

              The ZTrace function now is this:

              function ZTrace () { //==== Indicate calling sequence =============================================
              // Returns  -
              // Called by if (settings.IsDebug) {ZTrace ();}
              var indent, name, level, aStack = [], sStack,  
                  dots = ".......................................................................................";
                sStack = $.stack.toString();
                aStack = sStack.split ("\n");
                level = aStack.length - 3;
                if (level > 0) {name = aStack[level]};
                indent = dots.substring(0, level*2);
              //Console (indent + name);
                $.writeln (indent + name);
              } // --- end ZTrace
              
              • 4. Re: $.stack is not a string
                Wiedenmaier Level 3

                Hi Klaus,

                 

                glad you got this job done.

                 

                Here is a list of relevant escape sequences:

                 

                CodeOutputsHex Code
                \'single quote0x27
                \"double Quote0x22
                \\backslash0x5C
                \rCarriage Return0x0d
                \nLine feed0x0a
                \tHorizontal Tabulator0x09

                 

                For more Information visit: https://www.w3schools.com/js/js_strings.asp

                 

                double backslash means a realworld backslash, so you split statement searched for the real world string "\n" and not for the character "Linfeed (0x0a)".

                 

                Hope this helps anybody

                Markus