Minified code runs into error

Posted on
  • I'm developing a module to recognize simple gestures based on hc-sr04.
    Code runs fine, but minified(simple optimization) runs into error, somewhere around calcGesture.

    function gesture(trig,echo){
      function Empty(){}
      function gestureArea(min,max){this.min = min;this.max = max;this.active = true;}
      function trigger(){ digitalPulse(trig, 1, 0.01); }
      var riseTime = 0,fallTime = 0,iv,ivDef = 50,clkDef = 0.15;
      var min = 10,max = 100,actArea = "",prevArea = "",lastEnter = 0;
      var callback = {enter:Empty,leave:Empty,click:Empty,mov­e:Empty};
      var areas = {};
      this.setRange = function(mn,mx){min = mn;max = mx;};
      this.setCallback = function(t,f){callback[t] = f;};
      this.addArea = function(name,min,max){ areas[name] = new gestureArea(min,max); };
      this.setAreaStatus = function(name,status){ areas[name].active = status; };
      this.start = function(t){
        ivDef = t || 50;
        clkDef = ivDef * 3 / 1000;
        iv = setInterval(function(){trigger();},ivDef­);};
      this.stop = function(){clearInterval(iv);};
      function calcGesture(d){
        var a;
        if(between(min,max,d)){
          a = getArea(d);
          if(a){
            if(actArea === a){
              a = areas[a];
              callback.move("move",actArea,d,(d - a.min) / (a.max - a.min) * 100);
            }
            else{
              if(actArea !== ""){ callback.leave("leave",actArea,d); }
              callback.enter("enter",a,d);
              lastEnter = fallTime; prevArea = actArea;actArea = a;
            }
          }
          else{prevArea = actArea;actArea = "";}
        }
        else{
          if(actArea){
            callback.leave("leave",actArea,d);
            if((fallTime - lastEnter) < clkDef){ callback.click("click",actArea,d); }
          }
          prevArea = actArea; actArea = "";
        }
      }
      function getArea(d){
        for(var a in areas){ 
          if(areas[a].active){
            if(between(areas[a].min,areas[a].max,d))­{ return a; }
          }
        }
        return "";
      }
      function between(min,max,value){return (value>min && value<max);}
      setWatch(function(e){riseTime = e.time;},echo,{repeat:true, edge:'rising'});
      setWatch(function(e){
        fallTime = e.time;
        calcGesture(((fallTime - riseTime) * 1000000)/ 57);
      },echo,{repeat:true, edge:'falling'});
    }
    var g = new gesture(A0,A1);
    
    g.addArea("bottom",10,20);
    g.addArea("middle",30,40);
    g.addArea("top",50,60);
    //g.setCallback("enter",function(a,t,d){­console.log(a,t,d);});
    //g.setCallback("leave",function(a,t,d){­console.log(a,t,d);});
    g.setCallback("click",function(a,t,d){co­nsole.log(a,t,d);});
    //g.setCallback("move",function(a,t,d,p)­{console.log(a,t,d,p);});
    
    
  • Does it actually output an error message, or it just doesn't work as you expect?

  • Error is:

    Uncaught SyntaxError: Got ':' expected EOF
     at line 1 col 46
    ...-n)/57;var b;if(a>r&&a<s){a:{for(b in d)if(d[b].active&&a>d[...
                                                             ^
    
  • Hmm. If it's complaining about if(a>r&&a<s){a:{... that does look a bit iffy.

    I wonder if because I switched to using ES6 minification (to allow numbers like 0b0101 to be handled nicely), it's creating some crazy minified ES6 code too :)

  • Got it running by moving code-part of functions getArea and between into calcGesture. Now it is this:

    function gesture(trig,echo){
      function Empty(){}
      function gestureArea(min,max){this.min = min;this.max = max;this.active = true;}
      function trigger(){ digitalPulse(trig, 1, 0.01); }
      var riseTime = 0,fallTime = 0,iv,ivDef = 50,clkDef = 0.15;
      var min = 10,max = 100,actArea = "",prevArea = "",lastEnter = 0;
      var callback = {enter:Empty,leave:Empty,click:Empty,mov­e:Empty};
      var areas = {};
      this.setRange = function(mn,mx){min = mn;max = mx;};
      this.setCallback = function(t,f){callback[t] = f;};
      this.addArea = function(name,min,max){ areas[name] = new gestureArea(min,max); };
      this.setAreaStatus = function(name,status){ areas[name].active = status; };
      this.start = function(t){
        ivDef = t || 50;
        clkDef = ivDef * 3 / 1000;
        iv = setInterval(function(){trigger();},ivDef­);};
      this.stop = function(){clearInterval(iv);};
      function calcGesture(d){
        var a = "",i;
        if(d > min && d < max){
          for(i in areas){
            if(areas[i].active){ if(d > areas[i].min && d < areas[i].max){ a = i; } }
          }
          if(a){
            if(actArea === a){
              a = areas[a];
              callback.move("move",actArea,d,(d - a.min) / (a.max - a.min) * 100);
            }
            else{
              if(actArea !== ""){ callback.leave("leave",actArea,d); }
              callback.enter("enter",a,d);
              lastEnter = fallTime; prevArea = actArea;actArea = a;
            }
          }
          else{prevArea = actArea;actArea = "";}
        }
        else{
          if(actArea){
            callback.leave("leave",actArea,d);
            if((fallTime - lastEnter) < clkDef){ callback.click("click",actArea,d); }
          }
          prevArea = actArea; actArea = "";
        }
      }
      setWatch(function(e){riseTime = e.time;},echo,{repeat:true, edge:'rising'});
      setWatch(function(e){
        fallTime = e.time;
        calcGesture(((fallTime - riseTime) * 1000000)/ 57);
      },echo,{repeat:true, edge:'falling'});
    }
    var g = new gesture(A0,A1);
    
    g.addArea("bottom",10,20);
    g.addArea("middle",30,40);
    g.addArea("top",50,60);
    //g.setCallback("enter",function(a,t,d){­console.log(a,t,d);});
    //g.setCallback("leave",function(a,t,d){­console.log(a,t,d);});
    g.setCallback("click",function(a,t,d){co­nsole.log(a,t,d);});
    //g.setCallback("move",function(a,t,d,p)­{console.log(a,t,d,p);});
    

    looks like Espruino does not like the construction to abort the loop.

    a:{for(b in d)if(d[b].active&&a>d[b].min&&a<d[b].max­)break a;b=""}
    
  • Ahh. Labeled break statements. Espruino doesn't support those at the moment - I'm surprised the minifier added them in though!

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

Minified code runs into error

Posted by Avatar for JumJum @JumJum

Actions