11require ( 'es6-shim' ) ;
22/* Entrypoint for node module. Not used for Web IDE */
3- var fs = require ( "fs" ) ;
3+ const fs = require ( "fs" ) ;
4+ const vm = require ( 'vm' ) ;
45
56/* load all files in EspruinoTools... we do this so we can still
67use these files normally in the Web IDE */
@@ -9,17 +10,16 @@ function loadJS(filePath) {
910 var contents = fs . readFileSync ( filePath , { encoding :"utf8" } ) ;
1011 var realExports = exports ;
1112 exports = undefined ;
12- var r ;
13+ const script = new vm . Script ( contents , {
14+ filename : filePath , // This preserves the filename in the stack trace
15+ displayErrors : true ,
16+ } ) ;
1317 try {
14- r = eval ( contents ) ;
18+ script . runInThisContext ( ) ;
1519 } catch ( e ) {
1620 console . log ( "ERROR " + e + " while loading " + filePath ) ;
1721 }
1822 exports = realExports ; // utf8 lib somehow breaks this
19- return r ;
20- /* the code below would be better, but it doesn't seem to work when running
21- CLI - works fine when running as a module. */
22- //return require("vm").runInThisContext(contents, filePath );
2323}
2424function loadDir ( dir ) {
2525 var files = fs . readdirSync ( dir ) ;
@@ -55,15 +55,15 @@ var espruinoInitialised = false;
5555
5656/**
5757 * init Espruino global vars
58- * @param {() => void } callback
58+ * @param {() => void } callback
5959 */
6060function init ( callback ) {
6161 if ( espruinoInitialised ) {
6262 console . log ( "Already initialised." ) ;
6363 return callback ( ) ;
6464 }
6565 espruinoInitialised = true ;
66-
66+
6767 if ( global . $ === undefined )
6868 global . $ = function ( ) { return jqShim ; } ;
6969 if ( global . navigator === undefined )
@@ -73,10 +73,11 @@ function init(callback) {
7373 global . document = undefined ;
7474 }
7575 global . Espruino = undefined ;
76+ global . require = require
7677
7778 try {
7879 global . acorn = require ( "acorn" ) ;
79- acorn . walk = require ( "acorn/util/walk" ) ; // FIXME - Package subpath './util/walk' is not defined by "exports" in latest
80+ acorn . walk = require ( "acorn/util/walk" ) ; // FIXME - Package subpath './util/walk' is not defined by "exports" in latest
8081 } catch ( e ) {
8182 console . log ( "Acorn library not found - you'll need it for compiled code" ) ;
8283 }
@@ -86,7 +87,7 @@ function init(callback) {
8687 loadDir ( __dirname + "/libs" ) ;
8788 loadDir ( __dirname + "/libs/esprima" ) ;
8889 // the 'main' file
89- Espruino = loadJS ( __dirname + "/espruino.js" ) ;
90+ loadJS ( __dirname + "/espruino.js" ) ;
9091 // Core features
9192 loadDir ( __dirname + "/core" ) ;
9293 // Various plugins
@@ -118,9 +119,9 @@ exports.init = init;
118119
119120/**
120121 * Send a file to an Espruino on the given port, call the callback when done
121- * @param {string } port
122- * @param {string } filename
123- * @param {() => void } callback
122+ * @param {string } port
123+ * @param {string } filename
124+ * @param {() => void } callback
124125 */
125126function sendFile ( port , filename , callback ) {
126127 var code = fs . readFileSync ( filename , { encoding :"utf8" } ) ;
@@ -130,9 +131,9 @@ exports.sendFile = sendFile;
130131
131132/**
132133 * Send code to Espruino
133- * @param {string } port
134- * @param {string } code
135- * @param {() => void } callback
134+ * @param {string } port
135+ * @param {string } code
136+ * @param {() => void } callback
136137*/
137138function sendCode ( port , code , callback ) {
138139 var response = "" ;
@@ -164,9 +165,9 @@ exports.sendCode = sendCode;
164165
165166/**
166167 * Execute an expression on Espruino, call the callback with the result
167- * @param {string } port
168- * @param {string } expr
169- * @param {(result: string) => void } callback
168+ * @param {string } port
169+ * @param {string } expr
170+ * @param {(result: string) => void } callback
170171 */
171172function expr ( port , expr , callback ) {
172173 var exprResult = undefined ;
@@ -192,9 +193,9 @@ exports.expr = expr;
192193
193194/**
194195 * Execute a statement on Espruino, call the callback with what is printed to the console
195- * @param {string } port
196- * @param {string } expr
197- * @param {(result: string) => void } callback
196+ * @param {string } port
197+ * @param {string } expr
198+ * @param {(result: string) => void } callback
198199 */
199200function statement ( port , expr , callback ) {
200201 var exprResult = undefined ;
@@ -220,10 +221,10 @@ exports.statement = statement;
220221
221222/**
222223 * Flash the given firmware file to an Espruino board.
223- * @param {string } port
224- * @param {string } filename
224+ * @param {string } port
225+ * @param {string } filename
225226 * @param {number } flashOffset
226- * @param {() => void } callback
227+ * @param {() => void } callback
227228 */
228229function flash ( port , filename , flashOffset , callback ) {
229230 if ( typeof flashOffset === 'function' ) {
0 commit comments