• Hi,
    I would like to share this as I had some difficulties some weeks ago and got out of trouble using this:

    Here is a recipe to extract the class definition of an object in the console pane.

    I used it on a running Espruino Pico which had an objet stopwatch.runningOnStats saved but could not get its source defintion from dump().
    It was no more available in the WebIde editor pane because I had wiped it and saved without seeing this before... So I had lost all of the source code for the class runningOnStats property of stopwatch

    In the following case, stopwatch.runningOnStats is the object which we want to get the defintion on the console.
    We will have to copy/paste/edit the result from the console to get it in a source form

    
    var rs=Object.getPrototypeOf(stopwatch.runni­ngOnStats);
    for (var p of Object.getOwnPropertyNames(rs)) {
      console.log(p);
      console.log(Object.getOwnPropertyDescrip­tor(Object.getPrototypeOf(stopwatch.runn­ingOnStats), p));
    }
    
    

    The attached file is a more explicit result and execution trace of what this gave to me.


    1 Attachment

  • That's great - a really neat. There's a lot of data hanging around inside Espruino that dump() doesn't reconstruct properly.

    So the issue is you have something like this?

    function Test() {
      console.log("Hello");
    }
    Test.prototype.foo = function() {
      console.log("Hello 2");
    };
    
    a = new Test();
    delete Test; // we've now lost the original 'Test'
    dump();
    // outputs just:
    // var a = Object.create(a.__proto__.constructor["p­rototype"]);
    

    Potentially that is something I could fix in Espruino - maybe by detecting that no top-level name has been found and by making a temporary name to use? I know dump() isn't that smart at recreating objects.

    I'll make a note about this in this long-standing bug.

  • Hi,
    Not really, but quite close to that... In fact it was a class and I did not found a way to list the source of a class. My trick gets the source of an instance of a class.
    All of this happened on an instance of the class runningStats which was itself a property or an instance of the class object stopWatch.

    Indeed I deleted the source code in the editor (right side pane) and stupidly saved that incomplete source code to my PC without noticing what had disappeared. Could we get versionning every time we save a source, maybe just renaming the previous same named file with an added time stamp automatically?

    The joined file contains that whole thing in terms of: what the little trick gave from the running source on the Espruino, what I finally got after re-editing this dump to get a new version of the source.
    Thus I was left with a running instance of my program on the Pico and no way to list it to get back the almost last version of it.
    That said, I think it would be nice to be able to reconstruct an object by just a simple dump.

    Eventually, some improvements on the editor side would be nice too: something like "Do you want to delete the lines selected" when there are many of them.

  • Ahh - sorry, I should have checked your attached file. That's much clearer.

    It's odd actually because if runningStats was a global variable it should have got dumped. It might be that just doing var runningStats = stopwatch.runningStats; dump() (if that's where it was) would have caused dump() to try and reconstruct it.

    Could we get versionning every time we save a source, maybe just renaming the previous same named file with an added time stamp automatically?

    Annoyingly I don't think the Chrome app can do that - maybe it could manage with the 'projects' feature, but it definitely wouldn't work with the online version in the web browser :(

    I think realistically the best method would some kind of Git integration as on http://forum.espruino.com/conversations/­296367/#comment14577373

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

Some tricks I had to do to recover a class object definition from a saved one on a running Espruino Pico

Posted by Avatar for asez73 @asez73

Actions