You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • I try to use Modules.addCached("moduleId","moduleSour¬≠ceCode"); with "moduleCode" as ES6 multi-line string (which is supported by Espruino) and as 'plain' concatenated string.

    The "moduleCode" can be part of the code or retrieved over communication (from Web) and enables maintaining self-updating code centrally for connected applications.

    In below code I create a Person 'class'/prototype in module with same name 'on the go', which I later in the code use with require("Person");.

    // DynamicModuleSupply.js
    
    Modules.addCached("Person",`
    // Person.js
    // "class" (Prototype definition)
    var Person = function
      ( firstName // firstName
      , lastName  // lastName
      , birthDate // birthDate as Date
      ) {
        this.firstName = firstName;
        this.lastName  = lastName;
        this.birthDate = birthDate;
      };
    Person.prototype.getFullName = function() {
      return this.firstName + " " + this.lastName; };
    Person.prototype.getAgeInYears = function() {
      return (new Date().getFullYear()) - this.birthDate.getFullYear(); };
    exports = Person;
    `);
    
    function onInit() {
    
    // usage
    
    var Person = require("Person");
    
    var date = new Date(); date.setFullYear(2001);
    var p1 = new Person("JS","Jim",date);
    
    console.log("Person " + p1.getFullName()
        + " is " + p1.getAgeInYears() + " years of age");
    
    }
    
    setTimeout(onInit, 1000);
    

    Note: The setTimeout(onInit, 1000); is there for two reasons. First, I do not need to manually start the onInit() in the console pane, and second, the setTimeout() lets the upload finish before starting the runtime part of the code.

    Despite the Acorn parse ....failed.... error message it works. (see attached screen shot).

    I also get the Module Person not found warning message, which is expected.

    Both the error and warning messages are created by the upload as expected.

    The lines 4..19 stored in Person.js file or minified in Person.min.js file and placed in .../modules folder of my sandbox (see IDE settings - Project) work perfectly.

    I tried multiple variations to get over the error message, including using a 'plain' concatenated string (as below). Using the code in concatenated string needs though one of two changes:

    Change 1: join strings with line feed (\n) delimiter. This terminates the line comments (//):

    // DynamicModuleSupply1.js
    
    Modules.addCached("Person",
    [ '// Person.js'
    , '// "class" (Prototype definition)'
    , 'var Person = function'
    , '  ( firstName // firstName'
    , '  , lastName  // lastName'
    , '  , birthDate // birthDate as Date'
    , '  ) {'
    , '    this.firstName = firstName;'
    , '    this.lastName  = lastName;'
    , '    this.birthDate = birthDate;'
    , '  };'
    , 'Person.prototype.getFullName = function() {'
    , '  return this.firstName + " " + this.lastName; };'
    , 'Person.prototype.getAgeInYears = function() {'
    , '  return (new Date().getFullYear()) - this.birthDate.getFullYear();' , '};'
    , 'exports = Person;'
    ].join('\n'));
    
    function onInit() {
    
    // usage
    
    var Person = require("Person");
    
    var date = new Date(); date.setFullYear(2001);
    var p1 = new Person("JS","Jim",date);
    
    console.log("Person " + p1.getFullName()
        + " is " + p1.getAgeInYears() + " years of age");
    
    }
    
    setTimeout(onInit, 1000);
    

    Change 2: change line comments (//) int block comments (/* ... */):

    // DynamicModuleSupply2.js
    
    Modules.addCached("Person",
    [ '/* Person.js */'
    , '/* "class" (Prototype definition) */'
    , 'var Person = function'
    , '  ( firstName /* firstName */'
    , '  , lastName  /* lastName */'
    , '  , birthDate /* birthDate as Date */'
    , '  ) {'
    , '    this.firstName = firstName;'
    , '    this.lastName  = lastName;'
    , '    this.birthDate = birthDate;'
    , '  };'
    , 'Person.prototype.getFullName = function() {'
    , '  return this.firstName + " " + this.lastName; };'
    , 'Person.prototype.getAgeInYears = function() {'
    , '  return (new Date().getFullYear()) - this.birthDate.getFullYear();' , '};'
    , 'exports = Person;'
    ].join(''));
    
    function onInit() {
    
    // usage
    
    var Person = require("Person");
    
    var date = new Date(); date.setFullYear(2001);
    var p1 = new Person("JS","Jim",date);
    
    console.log("Person " + p1.getFullName()
        + " is " + p1.getAgeInYears() + " years of age");
    
    }
    
    setTimeout(onInit, 1000);
    

    Without either one change, I get error message message in console, I get error on usage:

     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v94 Copyright 2016 G.Williams
    >
    =undefined
    Uncaught Error: Constructor should be a function, but is Object
     at line 32 col 14
    var p1 = new Person("JS","Jim",date);
                 ^
    in function called from system
    

    Multi-line string usage makes me suspicious.

    What should I change to make the multi-line string work?


    1 Attachment

    • AcornParseFailed.png
About

Avatar for allObjects @allObjects started