You are reading a single comment by @user6350 and its replies. Click here to read the full conversation.
  • To me is now becoming more clear how the Javascript interpreter works looking at how this simple example code has been parsed and put into memory structures.
    I am now asking myself which should be the best way to use Espruino.
    In example adding a simple toFixed function would require adding a prototype to Number Class, which is too complex with respect using an existing native C sprintf function, even a big piece of C code itself in the real world.
    Probably the best use of Espruino could be to act as a scripted shell coprocessor of a traditional CPU, doing more intensive computations ( i.e. another STm32Fx micro controller ).
    It seems that there are pro and cons using Espruino.
    Is there a place where to find good books about the theory of a Javascript interpreter or handy manuals about how to build one.
    Looking inside the Espruino code should give enough explanation, but to a novice this is the hardest way.

    var Circle = function(radius) {
      this.radius = radius;
    };
    
    Circle.prototype.area = function() {
      return Math.PI * this.radius* this.radius;
    };
    
    var a = new Circle(3);
    var b = new Circle(4);
    var c = new Circle(1);
    
    a.area(); // .toFixed(2);
    b.area(); // .toFixed(2);
    c.area();
    
    #1[r1,l1] Object {
      #4[r1,l0] Name: '>timers'  #5[r2,l0] Array [
      ]
      #6[r1,l0] Name: '>watches'  #7[r2,l0] Array [
      ]
      #9[r1,l0] Name: '>history'  #10[r1,l0] Array [
        #11[r1,l0] Name: int 0  #8[r1,l0] String echo(0);
        #24[r1,l0] Name: int 1  #16[r1,l0] String var Circle = function(radius) {\n  this.radius = radius;\n};
        #41[r1,l0] Name: int 2  #30[r1,l0] String Circle.prototype.area = function() {\n  return Math.PI * this.radius* this.radius;\n};
        #50[r1,l0] Name: int 3  #43[r1,l0] String var a = new Circle(3);
        #61[r1,l0] Name: int 4  #54[r1,l0] String var b = new Circle(4);
        #72[r1,l0] Name: int 5  #65[r1,l0] String var c = new Circle(1);
        #75[r1,l0] Name: int 6  #77[r1,l0] String a.area();  
       #82[r1,l0] Name: int 7  #83[r1,l0] String b.area();  
        #86[r1,l0] Name: int 8  #76[r1,l0] String c.area();
        #88[r1,l0] Name: int 9  #89[r1,l0] String echo(1);
      ]
    
      #12[r4,l0] Name: 'Circle'  #13[r1,l0] Function {
        #14[r1,l0] Param Name: 'radius'  undefined
        #23[r1,l0] Name: '>code'  
        #15[r1,l0] String {\n  this.radius = radius;\n}
        #26[r4,l0] Name: 'prototype'  #25[r1,l0] Object {
          #28[r1,l0] Name: 'area' 
          #29[r1,l0] Function {
            #40[r1,l0] Name: '>code'  
            #36[r1,l0] String {\n  return Math.PI * this.radius* this.radius;\n}
          }
        }
      }
      #42[r1,l0] Name: 'a'  #45[r1,l0] Object {
        #46[r1,l0] Name: '__proto__'  #26[r4,l0] ...
        #52[r1,l0] Name: 'radius'     #49[r1,l0] Integer 3
        #48[r1,l0] Name: 'constructor'  #12[r4,l0] ...
      }
      #53[r1,l0] Name: 'b'  #56[r1,l0] Object {
        #57[r1,l0] Name: '__proto__'  #26[r4,l0] ...
        #63[r1,l0] Name: 'radius'  #60[r1,l0] Integer 4
        #59[r1,l0] Name: 'constructor'  #12[r4,l0] ...
      }
      #64[r1,l0] Name: 'c'  #67[r1,l0] Object {
        #68[r1,l0] Name: '__proto__'  #26[r4,l0] ...
        #74[r1,l0] Name: 'radius'  #71[r1,l0] Integer 1
        #70[r1,l0] Name: 'constructor'  #12[r4,l0] ...
      }
      #81[r1,l0] Name: 'Math'  #80[r1,l0] Function {
      }
    }
    
    process.memory()
    
    =
    {"free" :5394,
     "usage":56,
     "total":5450,
     "history":40,
     "stackEndAddress" :536983744,
     "flash_start"     :134217728,
     "flash_binary_end":134404416,
     "flash_code_start":135135232,
     "flash_length"    :  1048576}
    
About

Avatar for user6350 @user6350 started