Incomplete Object prototype + other gotchas

Posted on
  • I wouldn't expect toLocaleString to be part of the Object.prototype since there's no local in Espruino, but I found it weird propertyIsEnumerable and isPrototypeOf are not implemented.

    A quick and dirty solution would be the following:

    Object.prototype.propertyIsEnumerable=fu­nction(p){for(var k in this)if(k==p)return this.hasOwnProperty(k);return false};
    Object.prototype.isPrototypeOf=function(­o){return o instanceof this.constructor};

    However, instanceof is also broken in Espruino. Above code uses the constructor but a proper check would be the equivalent of the following:

      function F() {}
      F.prototype = this;
      return o instanceof F;
    // this should be true

    Latter check is false but it should be true. Ideally, isPrototypeOf and propertyIsEnumerable should be implemented natively.

    Last question for now, I'v enoticed that RegExp and in general regular expression /syntax/ is not implemented. Is that too big to bring in natively or it's something planned already?


  • Ok, just added a bug for those:­ues/522 and­ues/523

    However, I've got to wonder what you're trying to do on Espruino where you need this? Unfortunately I think at the moment implementation of more JS language features has to take a back seat to fixing problems that users are encountering when they're trying to use it for controlling hardware.

    As far as RegEx, I'd like to implement it, but it's a matter of finding a compact RegEx library that implements RegExes per the JS standard and also has a suitable license. I did have a quick look but didn't find one.

    The small ones I did find managed to fit into maybe 5k - which would be ok on the Pico, but more of a struggle on the original board.

  • what you're trying to do on Espruino where you need this?

    You have, as example, an opened bug about a comment weird behavior where the user coded via ES3 regular "classes" ... that code contains syntax usable since about ever and it would work everywhere.

    I have a simple little module that brings better looking and more semantic classes in and when you deal with hardware you might want to use these patterns too (like the other user did).

    Accordingly, having instanceof not working as expected is surprising and inconsistent, while I agree propsrtyIsEnumerable is a less common method that won't bring much in here.

    The missing RegExp should at least be mentioned on top of Espruino capabilities (if not already, couldn't find it) ... I often give them for granted, so I might lazily check /something/.test(command) instead of command.indexOf(something) > 0 and for cross platform modules is very good to know limit.

    About that, I wonder what's the process to submit a module to the list of already available one in espruino, in case a working NPM solution would take long time.

    Thank you

  • P.S. you've probably checked it already, but this would be already a great thing to have.

    It's MIT Style License, it's deployed in micro controllers, and it's a subset for RegExp, a 1.5 subset.

    It's called ure:­brary/ure.html#module-ure

    Here the module used in micro python:­on/blob/a86d40ccd437fe0235c09426ef6d552b­968d86e8/extmod/modure.c

    and here the folder with the lib:­on/tree/a86d40ccd437fe0235c09426ef6d552b­968d86e8/extmod/re1.5

  • Definitely a bug in instanceof is something I want to get fixed quickly. The lack of RegEx is mentioned in, but ideally Espruino would recognise them and report that they're not supported at the very least.

    Thanks for the pointer to URE - it looks good.

    As far as submitting modules, there's a page on it here:

    Including a way of writing them that tends to minimize well and work efficiently with Espruino. You basically just issue a pull request - at the moment the volume of contributions is low enough that it actually works pretty well.

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

Incomplete Object prototype + other gotchas

Posted by Avatar for WebReflection @WebReflection