Skip to content

Commit dde3216

Browse files
committed
Fixed Problems With Interpolation
Fixed a bug with String.prototype.interpolate causing stack overflows.
1 parent 4a0ceb6 commit dde3216

File tree

2 files changed

+31
-32
lines changed

2 files changed

+31
-32
lines changed

out/tools.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ defineProperties(String.prototype, {
156156
if (str)
157157
push(str);
158158
if (val) {
159-
let pos = findPairs(this, { "{": num => ++num, "}": num => ++num });
160-
let code = this.substring(val.index + 2, pos);
159+
let pos = findPairs(this, { "{": num => ++num, "}": num => --num });
160+
let code = this.substring(val.index + 2, pos - 1);
161161
if (code)
162162
push(code, true);
163163
from = pos + 1;
@@ -257,22 +257,22 @@ function evaluate(code, args = {}) {
257257
}
258258
function findPairs(stringOrOperators, operatorsOrLocation, locationOrDepth, depthArg) {
259259
const [string, operators, location, depth] = stringOrOperators instanceof Array || typeof stringOrOperators == "string" ||
260-
stringOrOperators instanceof String ?
261-
[
262-
typeof stringOrOperators == "string" ?
263-
stringOrOperators.split("") : stringOrOperators,
264-
operatorsOrLocation,
265-
locationOrDepth || 0,
266-
depthArg == null ? -1 : depthArg
267-
] :
268-
[
269-
this.split(""),
270-
stringOrOperators,
271-
operatorsOrLocation || 0,
272-
locationOrDepth == null ? -1 : locationOrDepth
273-
];
260+
stringOrOperators instanceof String ? [
261+
typeof stringOrOperators == "string" ?
262+
stringOrOperators.split("") : stringOrOperators,
263+
operatorsOrLocation,
264+
locationOrDepth || 0,
265+
depthArg == null ? -1 : depthArg
266+
] : [
267+
this.split(""),
268+
stringOrOperators,
269+
operatorsOrLocation || 0,
270+
locationOrDepth == null ? -1 : locationOrDepth
271+
];
274272
if (depth == 0)
275273
return location;
274+
if (string[location] == null)
275+
throw new Error();
276276
const operation = operators[string[location]];
277277
const newDepth = operation ? operation(depth == -1 ? 0 : depth) : depth;
278278
return findPairs(string, operators, location + 1, newDepth);

src/tools.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,8 @@ defineProperties(String.prototype, {
482482
let str = this.substring(from, val ? val.index : this.length);
483483
if (str) push(str);
484484
if (val) {
485-
let pos = findPairs(this, {"{": num => ++num, "}": num => ++num});
486-
let code = this.substring(val.index + 2, pos);
485+
let pos = findPairs(this, {"{": num => ++num, "}": num => --num});
486+
let code = this.substring(val.index + 2, pos - 1);
487487
if (code) push(code, true);
488488
from = pos + 1;
489489
}
@@ -595,21 +595,20 @@ function findPairs(this: string | void, stringOrOperators: string | string[] |
595595
depthArg?: number) {
596596
const [string, operators, location, depth] =
597597
stringOrOperators instanceof Array || typeof stringOrOperators == "string" ||
598-
stringOrOperators instanceof String ?
599-
[
600-
typeof stringOrOperators == "string" ?
601-
stringOrOperators.split("") : stringOrOperators as string[],
602-
operatorsOrLocation as Of<(depth: number) => number>,
603-
locationOrDepth as number || 0,
604-
depthArg == null ? -1 : depthArg as number
605-
] :
606-
[
607-
(this as string).split(""),
608-
stringOrOperators as Of<(depth: number) => number>,
609-
operatorsOrLocation as number || 0,
610-
locationOrDepth == null ? -1 : locationOrDepth as number
611-
];
598+
stringOrOperators instanceof String ? [
599+
typeof stringOrOperators == "string" ?
600+
stringOrOperators.split("") : stringOrOperators as string[],
601+
operatorsOrLocation as Of<(depth: number) => number>,
602+
locationOrDepth as number || 0,
603+
depthArg == null ? -1 : depthArg as number
604+
] : [
605+
(this as string).split(""),
606+
stringOrOperators as Of<(depth: number) => number>,
607+
operatorsOrLocation as number || 0,
608+
locationOrDepth == null ? -1 : locationOrDepth as number
609+
];
612610
if (depth == 0) return location;
611+
if (string[location] == null) throw new Error();
613612
const operation = operators[string[location]];
614613
const newDepth = operation ? operation(depth == -1 ? 0 : depth) : depth;
615614
return findPairs(string, operators, location + 1, newDepth);

0 commit comments

Comments
 (0)