brsh 2.4.0 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [2.4.1](https://github.com/eliotstocker/Brsh-JS/compare/v2.4.0...v2.4.1) (2026-05-24)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * better sequencial execution ([6a2a688](https://github.com/eliotstocker/Brsh-JS/commit/6a2a688dc4ad05ad49caa6f3f196395d17e7a473))
7
+
1
8
  # [2.4.0](https://github.com/eliotstocker/Brsh-JS/compare/v2.3.0...v2.4.0) (2026-05-23)
2
9
 
3
10
 
@@ -22,8 +22,8 @@ Track progress toward a POSIX-compliant `sh` implementation in Brsh-JS.
22
22
  - [x] `while` / `do` / `done`
23
23
  - [x] `for var in list; do … done`
24
24
  - [x] `case … in … esac`
25
- - [ ] `break [n]` — exit loop
26
- - [ ] `continue [n]` — next iteration
25
+ - [x] `break [n]` — exit loop
26
+ - [x] `continue [n]` — next iteration
27
27
  - [ ] Subshell `( cmds )`
28
28
 
29
29
  ### Functions
@@ -56,7 +56,7 @@ Track progress toward a POSIX-compliant `sh` implementation in Brsh-JS.
56
56
 
57
57
  ### Expansion
58
58
  - [x] Command substitution `$(cmd)`
59
- - [x] Arithmetic expansion `$((expr))`
59
+ - [x] Arithmetic expansion `$((expr))` (both `$VAR` and bare `VAR` forms)
60
60
  - [ ] Tilde expansion (`~`, `~user`)
61
61
  - [ ] Filename glob expansion (`*`, `?`, `[a-z]`)
62
62
  - [ ] Brace expansion `{a,b,c}` *(non-POSIX but common)*
@@ -69,8 +69,8 @@ Track progress toward a POSIX-compliant `sh` implementation in Brsh-JS.
69
69
  - [x] `source` / `. script` *(`.` alias missing — see below)*
70
70
  - [x] `.` — dot command (alias for `source`)
71
71
  - [ ] `:` — no-op, always exits 0
72
- - [ ] `break [n]`
73
- - [ ] `continue [n]`
72
+ - [x] `break [n]`
73
+ - [x] `continue [n]`
74
74
  - [ ] `shift [n]`
75
75
  - [ ] `unset var`
76
76
  - [ ] `readonly var`
package/dist/shell.es.js CHANGED
@@ -916,7 +916,10 @@ var g, _, v, y, b, x, S, C, w, T, E = e((() => {
916
916
  return Promise.reject("Command not implemented...");
917
917
  }
918
918
  runCommand() {
919
- return this.run().then((e) => (e && e.split(/\r?\n/).forEach((e) => this.stdOut = e), this.result)).catch((e) => (e && (this.stdErr = e.message || e), this.exitCode = 127, this.result));
919
+ return this.run().then((e) => (e && e.split(/\r?\n/).forEach((e) => this.stdOut = e), this.result)).catch((e) => {
920
+ if (e && e.__type) throw e;
921
+ return e && (this.stdErr = e.message || e), this.exitCode = 127, this.result;
922
+ });
920
923
  }
921
924
  onInput(e) {}
922
925
  set exitCode(e) {
@@ -977,6 +980,7 @@ var g, _, v, y, b, x, S, C, w, T, E = e((() => {
977
980
  return this._executeNodes(n).catch((e) => {
978
981
  if (e && e.__type === "return") this.sandbox.lastCode = e.code;
979
982
  else if (e && e.__type === "errexit") this.sandbox.lastCode = e.code;
983
+ else if (e && (e.__type === "break" || e.__type === "continue")) this.stdErr = `${e.__type}: only meaningful in a for or while loop`, this.sandbox.lastCode = 1;
980
984
  else return Promise.reject(e);
981
985
  }).then(() => {
982
986
  this.exitCode = this.sandbox.lastCode, this.source && this.context.source(this.sandbox.context);
@@ -1099,8 +1103,8 @@ var g, _, v, y, b, x, S, C, w, T, E = e((() => {
1099
1103
  consumed: a - t
1100
1104
  };
1101
1105
  }
1102
- _executeNodes(e) {
1103
- return e.reduce((e, t) => e.then(() => this._executeNode(t)), Promise.resolve());
1106
+ async _executeNodes(e) {
1107
+ for (let t of e) await this._executeNode(t);
1104
1108
  }
1105
1109
  _executeNode(e) {
1106
1110
  if (e.type === "function") {
@@ -1136,6 +1140,20 @@ var g, _, v, y, b, x, S, C, w, T, E = e((() => {
1136
1140
  code: n
1137
1141
  });
1138
1142
  }
1143
+ if (/^break(\s+\d+)?\s*$/.test(e.line.trim())) {
1144
+ let t = e.line.trim().match(/^break\s+(\d+)/), n = t ? parseInt(t[1], 10) : 1;
1145
+ return Promise.reject({
1146
+ __type: "break",
1147
+ count: n
1148
+ });
1149
+ }
1150
+ if (/^continue(\s+\d+)?\s*$/.test(e.line.trim())) {
1151
+ let t = e.line.trim().match(/^continue\s+(\d+)/), n = t ? parseInt(t[1], 10) : 1;
1152
+ return Promise.reject({
1153
+ __type: "continue",
1154
+ count: n
1155
+ });
1156
+ }
1139
1157
  return (this.sandbox.context.getVar("-") || "").includes("x") && (this.stdErr = `+ ${e.line.trim()}`, this.flush()), this.sandbox.onCommand(e.line, !1).then(() => {
1140
1158
  if ((this.sandbox.context.getVar("-") || "").includes("e") && this.sandbox.lastCode !== 0) return Promise.reject({
1141
1159
  __type: "errexit",
@@ -1145,22 +1163,55 @@ var g, _, v, y, b, x, S, C, w, T, E = e((() => {
1145
1163
  }
1146
1164
  return e.type === "if" ? this._executeIf(e) : e.type === "while" ? this._executeWhile(e) : e.type === "for" ? this._executeFor(e) : e.type === "case" ? this._executeCase(e) : Promise.resolve();
1147
1165
  }
1148
- _executeIf(e) {
1149
- let t = (n) => {
1150
- if (n >= e.clauses.length) return e.elseLines.length > 0 ? this._executeNodes(this._parseLines(e.elseLines)) : Promise.resolve();
1151
- let r = e.clauses[n];
1152
- return this.sandbox.onCommand(r.condition, !1).then(() => this.sandbox.lastCode === 0 ? this._executeNodes(this._parseLines(r.bodyLines)) : t(n + 1));
1153
- };
1154
- return t(0);
1166
+ async _executeIf(e) {
1167
+ for (let t of e.clauses) if (await this.sandbox.onCommand(t.condition, !1), this.sandbox.lastCode === 0) return this._executeNodes(this._parseLines(t.bodyLines));
1168
+ if (e.elseLines.length > 0) return this._executeNodes(this._parseLines(e.elseLines));
1155
1169
  }
1156
- _executeWhile(e) {
1157
- let t = () => this.sandbox.onCommand(e.condition, !1).then(() => {
1158
- if (this.sandbox.lastCode === 0) return this._executeNodes(this._parseLines(e.bodyLines)).then(t);
1159
- });
1160
- return t();
1170
+ async _executeWhile(e) {
1171
+ for (; await this.sandbox.onCommand(e.condition, !1), this.sandbox.lastCode === 0;) try {
1172
+ await this._executeNodes(this._parseLines(e.bodyLines));
1173
+ } catch (e) {
1174
+ if (e && e.__type === "break") {
1175
+ if (e.count <= 1) break;
1176
+ throw {
1177
+ __type: "break",
1178
+ count: e.count - 1
1179
+ };
1180
+ }
1181
+ if (e && e.__type === "continue") {
1182
+ if (e.count <= 1) continue;
1183
+ throw {
1184
+ __type: "continue",
1185
+ count: e.count - 1
1186
+ };
1187
+ }
1188
+ throw e;
1189
+ }
1161
1190
  }
1162
- _executeFor(e) {
1163
- return e.list.replace(/\${?([\w\d]+)}?/g, (e, t) => this.sandbox.context.getVar(t) || "").split(/\s+/).filter(Boolean).reduce((t, n) => t.then(() => (this.sandbox.context.setVar(e.variable, n), this._executeNodes(this._parseLines(e.bodyLines)))), Promise.resolve());
1191
+ async _executeFor(e) {
1192
+ let t = e.list.replace(/\${?([\w\d]+)}?/g, (e, t) => this.sandbox.context.getVar(t) || "").split(/\s+/).filter(Boolean);
1193
+ for (let n of t) {
1194
+ this.sandbox.context.setVar(e.variable, n);
1195
+ try {
1196
+ await this._executeNodes(this._parseLines(e.bodyLines));
1197
+ } catch (e) {
1198
+ if (e && e.__type === "break") {
1199
+ if (e.count <= 1) break;
1200
+ throw {
1201
+ __type: "break",
1202
+ count: e.count - 1
1203
+ };
1204
+ }
1205
+ if (e && e.__type === "continue") {
1206
+ if (e.count <= 1) continue;
1207
+ throw {
1208
+ __type: "continue",
1209
+ count: e.count - 1
1210
+ };
1211
+ }
1212
+ throw e;
1213
+ }
1214
+ }
1164
1215
  }
1165
1216
  _parseFunction(e, t) {
1166
1217
  let n = e[t].trim(), r = n.match(/^function\s+([\w\d_]+)/) || n.match(/^([\w\d_]+)\s*\(/), i = r ? r[1] : "", a = t + 1;
@@ -1237,11 +1288,9 @@ var g, _, v, y, b, x, S, C, w, T, E = e((() => {
1237
1288
  _executeFunctionBody(e) {
1238
1289
  return this._executeNodes(this._parseLines(e));
1239
1290
  }
1240
- _executeCase(e) {
1241
- return this.sandbox._replaceVariables(e.word).then((t) => {
1242
- let n = t.startsWith("\"") && t.endsWith("\"") || t.startsWith("'") && t.endsWith("'") ? t.slice(1, -1) : t;
1243
- for (let t of e.clauses) if (this._matchPattern(t.pattern, n)) return this._executeNodes(this._parseLines(t.bodyLines));
1244
- });
1291
+ async _executeCase(e) {
1292
+ let t = await this.sandbox._replaceVariables(e.word), n = t.startsWith("\"") && t.endsWith("\"") || t.startsWith("'") && t.endsWith("'") ? t.slice(1, -1) : t;
1293
+ for (let t of e.clauses) if (this._matchPattern(t.pattern, n)) return this._executeNodes(this._parseLines(t.bodyLines));
1245
1294
  }
1246
1295
  _matchPattern(e, t) {
1247
1296
  let n = "", r = 0;
@@ -6170,7 +6219,7 @@ var V, re, H, U, W, ie, G, ae, oe, se, K = e((() => {
6170
6219
  }
6171
6220
  _evalArithmetic(e) {
6172
6221
  let t = e.replace(/\${?([\w\d]+)}?/g, (e, t) => String(parseInt(this.context.getVar(t)) || 0));
6173
- if (!/^[\d\s+\-*/%()]+$/.test(t.trim())) return 0;
6222
+ if (t = t.replace(/\b([A-Za-z_]\w*)\b/g, (e, t) => String(parseInt(this.context.getVar(t)) || 0)), !/^[\d\s+\-*/%()]+$/.test(t.trim())) return 0;
6174
6223
  try {
6175
6224
  return Function("return (" + t + ")")();
6176
6225
  } catch {