gfwilliams / EspruinoCompiler

Node.js-based JavaScript->C++ compiler
25 stars 3 forks source link

Errors generated randomly when uploading "compiled" function Espruino 1v4 #9

Closed asez73 closed 9 years ago

asez73 commented 9 years ago

So, I use some compiled javascript functions and get unpredictable results: a- the compiler is not accessible b- the complied code is rejected by the board and I get the non compiled javascript function c- the compiled code seems to be non functionnal, this creates some random behaviour later. d-the compiled code is apparently ok but the next javascript, uncompiled, function is ignored and not defined e- the IDE complains at upload time that some parenthesis are not closed... Which is impossible because those compiled functions are unchanged from one upload to the other and the IDE doesn't show any warning at the beginnig of any line.

Of course it also happens to work correctly but this is about 1/4th of the uploads. Commenting out "compiled" instructions does allow a correct execution of the code, just half the speed of compiled code.

The URL used for the compiler is "http://www.espruino.com:32766". The IDE is v0.56.1 but this happened with v0.56.0 too

The example below is the most common one.

>reset();
=undefined
 _____                 _
|   __|___ ___ ___ _ _|_|___ ___
|   __|_ -| . |  _| | | |   | . |
|_____|___|  _|_| |___|_|_|_|___|
          |_| http://espruino.com
 1v80.323 Copyright 2015 G.Williams
>echo(0);
Uncaught SyntaxError: Got ID:JsVar expected ','
 at line 1 col 192
... ddmmyy = E.nativeCall(1, "JsVar(JsVar)", atob("LenwTwVGd0iP...

Or also with throtte send, for exactly the same source code: This is just the same result actually, the functions in trouble seems to be randomly "selected".

reset();
=undefined
 _____                 _
|   __|___ ___ ___ _ _|_|___ ___
|   __|_ -| . |  _| | | |   | . |
|_____|___|  _|_| |___|_|_|_|___|
          |_| http://espruino.com
 1v80.323 Copyright 2015 G.Williams
>echo(0);
Uncaught SyntaxError: Got ID:JsVar expected ','
 at line 1 col 534
...ar secn = E.nativeCall(1, "JsVar(JsVar)", atob("LenwT4NNjbAH...

Excerpt of the code in trouble


// ---- beg (modified) GPS module code

// convert latitudes and longitudes from nmea ascii coding to float

function lln(d1,d2,negs) {
  //"compiled";
  var dfp=d1.indexOf(".")-2;
  var v=(parseInt(d1.substr(0,dfp),10)+parseFloat(d1.substr(dfp))/60);
  if (d2==negs) {
    return (0-v);
  } else { return v; }
}

function secn(d) {
  //"compiled";
  return (parseInt(d.substr(0,2),10)*3600+parseInt(d.substr(2,2),10)*60+parseFloat(d.substr(4,5)));
} // hhmmss.ss

function ddmmyy(d) {
  //"compiled";
  return Date.parse('20'+d.substr(4,2)+"-"+d.substr(2,2)+'-'+d.substr(0,2));
}

function cks7(m) { 
  //"compiled"; 
  var ck = 0;
  var l = 0|m.length; // ensure we don't have to check the length each time
  var c = m.charCodeAt.bind(m); // ensure we don't have to look up 'charCodeAt' each time
  for (var i=0;i<l;i++) ck ^= c(i);
  return ck;
}

function checksum(msg) {
  var cks = 0;
  new Uint8Array(E.toArrayBuffer(msg)).forEach(function (c) { cks^=c; }); 
  return cks;
}

// compute the checksum for NMEA sentences and splits it from ','
function nmea_checksum(s) {
  try {
    var n=s.split('$').pop(); // remove leading uncomplete sentences AND the no more usefull leading $
    n=n.split('*'); // separate checksum from line
    var cksum = parseInt(n.pop(),16); // make checksum a number and remove it from n
    n=n.pop(); // keep content alone
    if (cksum===cks7(n)) // mean leap 0.02532994300 s
      return n.split(',');  // mean leap 0.01426320198 s
    else {
      throw "different checksums "+cksum.toString(16)+" versus "+cks7(n).toString(16)+" in line "+s ;
    }
  } catch(x) {
    throw "Checksum error "+x+' in sentence '+s; 
  }
}
gfwilliams commented 9 years ago

Ok, thanks - I think I've got it.

Looks like it might get confused if you have multiple compiled functions and their results get returned out of order...

So if you've got just one function it should always be fine?

asez73 commented 9 years ago

Yes it seems to be that: I spread the compiled functions amids the other uncompiled functions and used 'throttle send' and it works like a charm...

gfwilliams commented 9 years ago

Ok fixed now - however it'll have to wait until the next time I push an IDE update before it's available (unless you install from GitHub).