Avatar for WebReflection


Member since Apr 2015 • Last active May 2015
  • 4 conversations

Most recent activity

  • in JavaScript
    Avatar for WebReflection

    I've just dropped some RegExp check and polyfilled internally what was missing in Espruino, in case you'd like to have ES6 alike syntax for classes, with ES7 ability to compose through mixins/traits, I'd like to link you at this project of mine, called es-class

    The minified version is not hand crafted/compacted explicitly for Espruino, but with plain 3.5kB (1.6kB gzipped) you get granted cross platform compatibility, including the MIPS node version of Arduino Yun, and basically all other JS engines out there I could verify.

    Key features are:

    1. semantic Class({..}) declaration
    2. extends and handy this.super(...) ability
    3. static properties definitions
    4. bonus for mixins/traits via with: [ ... ] plus implements for light checks on expected interfaces

    As Gordon asked me in another thread, you ain't gonna need probably all these features, but maybe you'd like to write some code that looks like modern JS without breaking your Espruino.

    Performance are also as good as vanilla JS, only light overhead is classes definitions, luckily this happens only once at bootstrap so you won't probably notice that.

    All tests are green except those based on ES3 non standard get/set literal objects syntax which is not implemented here, and some tiny instanceof gotcha that will hopefully be solved soon since Gordon has already a bug to work on.

    I hope someone will find it handy.

    Best Regards

  • in JavaScript
    Avatar for WebReflection

    Actually I've just realized Function.bind is there indeed but Function.prototype.bind is not ... but (function(){}).bind is there too ... oh well, good to know (sort of weird)

  • in JavaScript
    Avatar for WebReflection

    As temporary work around on top of your file:

    Function.bind||(Function.prototype.bind=­function(c){var f=this,a=[].slice.call(arguments,1);retu­rn function(){a.push.apply(a,arguments);ret­urn f.apply(c,a)}});

    In case you don't care about handling extra arguments ...

    Function.bind||(Function.prototype.bind=­function(c,f){f=this;return function(){return f.apply(c,arguments)}});

    Until it's solved, of course

  • in JavaScript
    Avatar for WebReflection

    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: http://docs.micropython.org/en/latest/li­brary/ure.html#module-ure

    Here the module used in micro python:

    and here the folder with the lib:

  • in JavaScript
    Avatar for WebReflection

    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

  • in JavaScript
    Avatar for WebReflection

    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?


  • in General
    Avatar for WebReflection

    In fact I think even prefixing your module with var module=this; will work.

    watch out, the this inside a module is the exports itself, not the module ... I know it's quite confusing but better knowing this before doing any work.

    console.log(this === module.exports);

    If you require a file like that, the log is true in (node|io).js