Skip to content

Commit 299ef9d

Browse files
committed
Attempting to improve EspruinoTools' loader mechanism so that at least we get line numbers on errors
1 parent 5a85b54 commit 299ef9d

File tree

9 files changed

+82
-83
lines changed

9 files changed

+82
-83
lines changed

core/serial_noble.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* sudo setcap cap_net_raw+eip $(eval readlink -f `which node`)
77
*/
88

9-
if (typeof require === 'undefined') return;
9+
if (typeof process === 'undefined') return;
1010
var noble = undefined;
1111

1212
var NORDIC_SERVICE = "6e400001b5a3f393e0a9e50e24dcca9e";

core/serial_node_ble.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
*/
2222

23-
if (typeof require === 'undefined') return;
23+
if (typeof process === 'undefined') return;
2424
if (require("os").platform() != "linux") {
2525
console.log("serial_node_ble: Not running Linux - disabling Bluetooth DBUS support");
2626
return;

core/serial_node_serial.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ Gordon Williams ([email protected])
77
console.log("We have chrome.serial - not using 'serialport' module");
88
return;
99
}
10-
if (typeof require === 'undefined') return;
10+
if (typeof process === 'undefined') return;
1111
var serialport;
1212
try {
13-
require.resolve('serialport');
13+
require.resolve('serialport');
1414
} catch (e) {
1515
console.log("No 'serialport' module found");
1616
return;

core/serial_node_socket.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Author: Alfie Kirkpatrick ([email protected])
1919
(function () {
2020
var PREFIX = "tcp://"; // prefix to use with -p option for hostname/ip address
2121

22-
if (typeof require === 'undefined') return; // definitely not node!
22+
if (typeof process === 'undefined') return; // definitely not node!
2323

2424
var net;
2525
try {

core/serial_websocket_relay.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Used for Relay service on espruino.com/ide as well as `npm espruino-web-ide`'s
77
(function() {
88
var autoconnect = false;
99
// Support for websockets on Node.js...
10-
if (typeof WebSocket == "undefined" || typeof require!=="undefined") {
10+
if (typeof WebSocket == "undefined" || typeof process!=="undefined") {
1111
try {
1212
// eslint-disable-next-line no-global-assign
1313
WebSocket = require("ws").WebSocket;
@@ -20,7 +20,7 @@ Used for Relay service on espruino.com/ide as well as `npm espruino-web-ide`'s
2020
}
2121
// ... but if we don't have them there's nothing we can do...
2222
if (typeof WebSocket == "undefined") return;
23-
23+
2424
// default host...
2525
var host = "www.espruino.com";
2626
if (typeof window != "undefined") {

espruino.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
**/
1212
"use strict";
1313

14-
var Espruino;
15-
1614
(function() {
1715

1816
/** List of processors. These are functions that are called one
@@ -123,7 +121,7 @@ var Espruino;
123121
}
124122

125123
// -----------------------------------
126-
Espruino = {
124+
globalThis.Espruino = {
127125
Core : { },
128126
Plugins : { },
129127
addProcessor : addProcessor,

index.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require('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
67
use 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
}
2424
function 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
*/
6060
function 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
*/
125126
function 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
*/
137138
function 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
*/
171172
function 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
*/
199200
function 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
*/
228229
function flash(port, filename, flashOffset, callback) {
229230
if (typeof flashOffset === 'function') {

0 commit comments

Comments
 (0)