From 7ca756e509c8a53bc93d43cc9366e19789c0d697 Mon Sep 17 00:00:00 2001 From: Gubolin Date: Mon, 3 Nov 2014 17:59:29 +0100 Subject: throw an non-js error if "list" is not List (fix #533) --- threads.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'threads.js') diff --git a/threads.js b/threads.js index 6a59f3c..e225fd9 100644 --- a/threads.js +++ b/threads.js @@ -681,6 +681,15 @@ Process.prototype.doYield = function () { // Process Exception Handling +Process.prototype.checkIfList = function (maybeList) { + if (!(maybeList instanceof List)) { + var error = {name: 'list error', message: 'expecting a list'}; + this.handleError(error); + maybeList = new List(); + } + return maybeList; +}; + Process.prototype.handleError = function (error, element) { var m = element; this.stop(); @@ -1232,15 +1241,19 @@ Process.prototype.reportCONS = function (car, cdr) { }; Process.prototype.reportCDR = function (list) { + list = this.checkIfList(list); return list.cdr(); }; Process.prototype.doAddToList = function (element, list) { + list = this.checkIfList(list); list.add(element); }; Process.prototype.doDeleteFromList = function (index, list) { var idx = index; + list = this.checkIfList(list); + if (this.inputOption(index) === 'all') { return list.clear(); } @@ -1255,6 +1268,8 @@ Process.prototype.doDeleteFromList = function (index, list) { Process.prototype.doInsertInList = function (element, index, list) { var idx = index; + list = this.checkIfList(list); + if (index === '') { return null; } @@ -1269,6 +1284,8 @@ Process.prototype.doInsertInList = function (element, index, list) { Process.prototype.doReplaceInList = function (index, list, element) { var idx = index; + list = this.checkIfList(list); + if (index === '') { return null; } @@ -1283,6 +1300,8 @@ Process.prototype.doReplaceInList = function (index, list, element) { Process.prototype.reportListItem = function (index, list) { var idx = index; + list = this.checkIfList(list); + if (index === '') { return ''; } @@ -1296,10 +1315,12 @@ Process.prototype.reportListItem = function (index, list) { }; Process.prototype.reportListLength = function (list) { + list = this.checkIfList(list); return list.length(); }; Process.prototype.reportListContainsItem = function (list, element) { + list = this.checkIfList(list); return list.contains(element); }; @@ -1582,6 +1603,8 @@ Process.prototype.reportMap = function (reporter, list) { // documented in each of the variants' code (linked or arrayed) below var next; + list = this.checkIfList(list); + if (list.isLinked) { // this.context.inputs: // [0] - reporter -- cgit v1.3.1