scriptable 0.1.1 → 0.2.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/LICENSE +21 -0
- package/README.md +13 -0
- package/dist/environment.d.ts +16 -5
- package/dist/environment.js +83 -35
- package/dist/evaluate.d.ts +0 -1
- package/dist/evaluate.js +194 -48
- package/dist/index.d.ts +28 -3
- package/dist/index.js +12 -7
- package/package.json +6 -2
- package/.changeset/README.md +0 -8
- package/.changeset/config.json +0 -11
- package/.github/workflows/release.yml +0 -62
- package/CHANGELOG.md +0 -14
- package/dist/logic-ops.d.ts +0 -2
- package/dist/logic-ops.js +0 -5
- package/scripts/release.js +0 -127
- package/src/__tests__/break-out.test.ts +0 -43
- package/src/__tests__/builtin-functions-date.test.ts +0 -75
- package/src/__tests__/builtin-functions.test.ts +0 -56
- package/src/__tests__/expressions.test.ts +0 -13
- package/src/__tests__/functions.test.ts +0 -81
- package/src/__tests__/logic.test.ts +0 -54
- package/src/__tests__/new-features.test.ts +0 -158
- package/src/__tests__/security.test.ts +0 -399
- package/src/__tests__/variables.test.ts +0 -200
- package/src/binary-ops.ts +0 -18
- package/src/builtin-functions.ts +0 -177
- package/src/environment.ts +0 -132
- package/src/evaluate.ts +0 -275
- package/src/index.ts +0 -42
- package/src/logic-ops.ts +0 -6
- package/tsconfig.json +0 -16
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from "vitest";
|
|
2
|
-
import runner from "../index.js";
|
|
3
|
-
|
|
4
|
-
const run = runner();
|
|
5
|
-
|
|
6
|
-
describe("define global vars", () => {
|
|
7
|
-
it(`foo`, () => {
|
|
8
|
-
const code = `foo`;
|
|
9
|
-
const vars = {};
|
|
10
|
-
expect(() => run(code, vars)).toThrow(
|
|
11
|
-
new ReferenceError(`"foo" is not defined (1:0)`)
|
|
12
|
-
);
|
|
13
|
-
});
|
|
14
|
-
it("let foo", () => {
|
|
15
|
-
const code = `let foo;`;
|
|
16
|
-
const vars = {};
|
|
17
|
-
const { result, env } = run(code, vars);
|
|
18
|
-
expect(result).toEqual(undefined);
|
|
19
|
-
expect(env).toEqual({ foo: undefined });
|
|
20
|
-
});
|
|
21
|
-
it("const foo", () => {
|
|
22
|
-
const code = `const foo;`;
|
|
23
|
-
const vars = {};
|
|
24
|
-
expect(() => run(code, vars)).toThrow(SyntaxError);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it(`foo = "bar"`, () => {
|
|
28
|
-
const code = `foo = "bar";`;
|
|
29
|
-
const vars = {};
|
|
30
|
-
const { result, env } = run(code, vars);
|
|
31
|
-
expect(result).toEqual("bar");
|
|
32
|
-
expect(env).toEqual({ foo: "bar" });
|
|
33
|
-
});
|
|
34
|
-
it(`var foo = "bar"`, () => {
|
|
35
|
-
const code = `var foo = "bar";`;
|
|
36
|
-
const vars = {};
|
|
37
|
-
const { result, env } = run(code, vars);
|
|
38
|
-
expect(result).toEqual(undefined);
|
|
39
|
-
expect(env).toEqual({ foo: "bar" });
|
|
40
|
-
});
|
|
41
|
-
it(`let foo = "bar"`, () => {
|
|
42
|
-
const code = `let foo = "bar";`;
|
|
43
|
-
const vars = {};
|
|
44
|
-
const { result, env } = run(code, vars);
|
|
45
|
-
expect(result).toEqual(undefined);
|
|
46
|
-
expect(env).toEqual({ foo: "bar" });
|
|
47
|
-
});
|
|
48
|
-
it(`const foo = "bar"`, () => {
|
|
49
|
-
const code = `const foo = "bar";`;
|
|
50
|
-
const vars = {};
|
|
51
|
-
const { result, env } = run(code, vars);
|
|
52
|
-
expect(result).toEqual(undefined);
|
|
53
|
-
expect(env).toEqual({ foo: "bar" });
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
describe("define vars in scope", () => {
|
|
58
|
-
it(`foo = "foo"`, () => {
|
|
59
|
-
const code = `
|
|
60
|
-
if (true) {
|
|
61
|
-
foo = "foo";
|
|
62
|
-
}
|
|
63
|
-
`;
|
|
64
|
-
const vars = {};
|
|
65
|
-
const { result, env } = run(code, vars);
|
|
66
|
-
expect(result).toEqual("foo");
|
|
67
|
-
expect(env).toEqual({ foo: "foo" });
|
|
68
|
-
});
|
|
69
|
-
it(`var foo = "foo"`, () => {
|
|
70
|
-
const code = `
|
|
71
|
-
if (true) {
|
|
72
|
-
var foo = "foo";
|
|
73
|
-
}
|
|
74
|
-
`;
|
|
75
|
-
const vars = {};
|
|
76
|
-
const { result, env } = run(code, vars);
|
|
77
|
-
expect(result).toEqual(undefined);
|
|
78
|
-
expect(env).toEqual({ foo: "foo" });
|
|
79
|
-
});
|
|
80
|
-
it(`let foo = "foo"`, () => {
|
|
81
|
-
const code = `
|
|
82
|
-
if (true) {
|
|
83
|
-
let foo = "foo";
|
|
84
|
-
}
|
|
85
|
-
`;
|
|
86
|
-
const vars = {};
|
|
87
|
-
const { result, env } = run(code, vars);
|
|
88
|
-
expect(result).toEqual(undefined);
|
|
89
|
-
expect(env).toEqual({ foo: undefined });
|
|
90
|
-
});
|
|
91
|
-
it(`const foo = "foo"`, () => {
|
|
92
|
-
const code = `
|
|
93
|
-
if (true) {
|
|
94
|
-
const foo = "foo";
|
|
95
|
-
}
|
|
96
|
-
`;
|
|
97
|
-
const vars = {};
|
|
98
|
-
const { result, env } = run(code, vars);
|
|
99
|
-
expect(result).toEqual(undefined);
|
|
100
|
-
expect(env).toEqual({ foo: undefined });
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
describe("bare variables eval", () => {
|
|
105
|
-
it("evals a bare var", () => {
|
|
106
|
-
const code = `foo`;
|
|
107
|
-
const env = { foo: "bar" };
|
|
108
|
-
const { result } = run(code, env);
|
|
109
|
-
expect(result).toEqual("bar");
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it("evals a deep bare var", () => {
|
|
113
|
-
const code = `foo["bar"]`;
|
|
114
|
-
const env = { foo: { bar: "baz" } };
|
|
115
|
-
const { result } = run(code, env);
|
|
116
|
-
expect(result).toEqual("baz");
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it("evals an array index", () => {
|
|
120
|
-
const code = `foo[1]`;
|
|
121
|
-
const env = { foo: ["me", "myself", "i"] };
|
|
122
|
-
const { result } = run(code, env);
|
|
123
|
-
expect(result).toEqual("myself");
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it("evals a deeper bare var", () => {
|
|
127
|
-
const code = `foo["bar"]["baz"]`;
|
|
128
|
-
const env = { foo: { bar: { baz: "fiz" } } };
|
|
129
|
-
const { result } = run(code, env);
|
|
130
|
-
expect(result).toEqual("fiz");
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
it("evals a dotted deep bare var", () => {
|
|
134
|
-
const code = `foo.bar`;
|
|
135
|
-
const env = { foo: { bar: "baz" } };
|
|
136
|
-
const { result } = run(code, env);
|
|
137
|
-
expect(result).toEqual("baz");
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it("evals a deeper bare var", () => {
|
|
141
|
-
const code = `foo.bar.baz`;
|
|
142
|
-
const env = { foo: { bar: { baz: "fiz" } } };
|
|
143
|
-
const { result } = run(code, env);
|
|
144
|
-
expect(result).toEqual("fiz");
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
describe("assignment", () => {
|
|
149
|
-
it("assigns top level var", () => {
|
|
150
|
-
const code = `foo = "bar"`;
|
|
151
|
-
const vars = {};
|
|
152
|
-
const { env } = run(code, vars);
|
|
153
|
-
expect(env).toEqual({ foo: "bar" });
|
|
154
|
-
});
|
|
155
|
-
it("re-assigns third level var", () => {
|
|
156
|
-
const code = `foo.baz.fiz = "bar"`;
|
|
157
|
-
const vars = { foo: { baz: { fiz: "b00" } } };
|
|
158
|
-
const { env } = run(code, vars);
|
|
159
|
-
expect(env).toEqual({ foo: { baz: { fiz: "bar" } } });
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
it("modifies var from inbuilt function", () => {
|
|
163
|
-
const code = `forEach(foo, bar => { bar.baz = 2 })`;
|
|
164
|
-
const env = { foo: [{ baz: 1 }, { baz: 1 }] };
|
|
165
|
-
run(code, env);
|
|
166
|
-
expect(env).toEqual({ foo: [{ baz: 2 }, { baz: 2 }] });
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
// these probably shouldn't actually work..
|
|
170
|
-
it("assigns second level var", () => {
|
|
171
|
-
const code = `foo.baz = "bar"`;
|
|
172
|
-
const vars = {};
|
|
173
|
-
const { env } = run(code, vars);
|
|
174
|
-
expect(env).toEqual({ foo: { baz: "bar" } });
|
|
175
|
-
});
|
|
176
|
-
it("assigns third level var", () => {
|
|
177
|
-
const code = `foo.baz.fiz = "bar"`;
|
|
178
|
-
const vars = {};
|
|
179
|
-
const { env } = run(code, vars);
|
|
180
|
-
expect(env).toEqual({ foo: { baz: { fiz: "bar" } } });
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
describe("objects", () => {
|
|
185
|
-
it("creates an object", () => {
|
|
186
|
-
const code = `foo = { bar: "baz" };`;
|
|
187
|
-
const vars = {};
|
|
188
|
-
const { result, env } = run(code, vars);
|
|
189
|
-
expect(result).toEqual({ bar: "baz" });
|
|
190
|
-
expect(env).toEqual({ foo: { bar: "baz" } });
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
it("creates an array", () => {
|
|
194
|
-
const code = `foo = ["bar", "baz"];`;
|
|
195
|
-
const vars = {};
|
|
196
|
-
const { result, env } = run(code, vars);
|
|
197
|
-
expect(result).toEqual(["bar", "baz"]);
|
|
198
|
-
expect(env).toEqual({ foo: ["bar", "baz"] });
|
|
199
|
-
});
|
|
200
|
-
});
|
package/src/binary-ops.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
const binaryOps: Record<string, (l: unknown, r: unknown) => unknown> = {
|
|
2
|
-
"+": (l: any, r: any) => l + r,
|
|
3
|
-
"-": (l: any, r: any) => l - r,
|
|
4
|
-
"*": (l: any, r: any) => l * r,
|
|
5
|
-
"/": (l: any, r: any) => l / r,
|
|
6
|
-
"%": (l: any, r: any) => l % r,
|
|
7
|
-
|
|
8
|
-
">": (l: any, r: any) => l > r,
|
|
9
|
-
"<": (l: any, r: any) => l < r,
|
|
10
|
-
">=": (l: any, r: any) => l >= r,
|
|
11
|
-
"<=": (l: any, r: any) => l <= r,
|
|
12
|
-
"==": (l: any, r: any) => l === r,
|
|
13
|
-
"!=": (l: any, r: any) => l !== r,
|
|
14
|
-
"===": (l: any, r: any) => l === r,
|
|
15
|
-
"!==": (l: any, r: any) => l !== r,
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export default binaryOps;
|
package/src/builtin-functions.ts
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
export type FunctionMap = Record<string, (...args: any[]) => any>;
|
|
2
|
-
|
|
3
|
-
// ---------------------------------------------------------------------------
|
|
4
|
-
// Date helpers (Intl-based, no moment dependency)
|
|
5
|
-
// ---------------------------------------------------------------------------
|
|
6
|
-
|
|
7
|
-
function getDateParts(date: Date, tz: string) {
|
|
8
|
-
const fmt = (opts: Intl.DateTimeFormatOptions) =>
|
|
9
|
-
new Intl.DateTimeFormat("en-US", { timeZone: tz, ...opts });
|
|
10
|
-
|
|
11
|
-
const year = parseInt(fmt({ year: "numeric" }).format(date));
|
|
12
|
-
const month = parseInt(fmt({ month: "numeric" }).format(date));
|
|
13
|
-
const day = parseInt(fmt({ day: "numeric" }).format(date));
|
|
14
|
-
|
|
15
|
-
const h24Parts = fmt({
|
|
16
|
-
hour: "numeric",
|
|
17
|
-
minute: "numeric",
|
|
18
|
-
second: "numeric",
|
|
19
|
-
hourCycle: "h23",
|
|
20
|
-
}).formatToParts(date);
|
|
21
|
-
|
|
22
|
-
const part = (type: string) =>
|
|
23
|
-
h24Parts.find((p) => p.type === type)?.value || "0";
|
|
24
|
-
const hour24 = parseInt(part("hour"));
|
|
25
|
-
const minute = parseInt(part("minute"));
|
|
26
|
-
const second = parseInt(part("second"));
|
|
27
|
-
|
|
28
|
-
const hour12 = hour24 === 0 ? 12 : hour24 > 12 ? hour24 - 12 : hour24;
|
|
29
|
-
const ampm = hour24 >= 12 ? "pm" : "am";
|
|
30
|
-
|
|
31
|
-
const monthShort = fmt({ month: "short" }).format(date);
|
|
32
|
-
const monthLong = fmt({ month: "long" }).format(date);
|
|
33
|
-
|
|
34
|
-
const tzAbbr =
|
|
35
|
-
fmt({ timeZoneName: "short" })
|
|
36
|
-
.formatToParts(date)
|
|
37
|
-
.find((p) => p.type === "timeZoneName")?.value || "";
|
|
38
|
-
|
|
39
|
-
return {
|
|
40
|
-
year, month, day,
|
|
41
|
-
hour12, hour24, minute, second,
|
|
42
|
-
ampm, monthShort, monthLong, tzAbbr,
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const FORMAT_TOKENS =
|
|
47
|
-
/YYYY|YY|MMMM|MMM|MM|M|DD|D|HH|H|hh|h|mm|m|ss|s|a|A|z/g;
|
|
48
|
-
|
|
49
|
-
function applyFormat(parts: ReturnType<typeof getDateParts>, format: string) {
|
|
50
|
-
return format.replace(FORMAT_TOKENS, (token) => {
|
|
51
|
-
switch (token) {
|
|
52
|
-
case "YYYY": return String(parts.year);
|
|
53
|
-
case "YY": return String(parts.year).slice(-2);
|
|
54
|
-
case "MMMM": return parts.monthLong;
|
|
55
|
-
case "MMM": return parts.monthShort;
|
|
56
|
-
case "MM": return String(parts.month).padStart(2, "0");
|
|
57
|
-
case "M": return String(parts.month);
|
|
58
|
-
case "DD": return String(parts.day).padStart(2, "0");
|
|
59
|
-
case "D": return String(parts.day);
|
|
60
|
-
case "HH": return String(parts.hour24).padStart(2, "0");
|
|
61
|
-
case "H": return String(parts.hour24);
|
|
62
|
-
case "hh": return String(parts.hour12).padStart(2, "0");
|
|
63
|
-
case "h": return String(parts.hour12);
|
|
64
|
-
case "mm": return String(parts.minute).padStart(2, "0");
|
|
65
|
-
case "m": return String(parts.minute);
|
|
66
|
-
case "ss": return String(parts.second).padStart(2, "0");
|
|
67
|
-
case "s": return String(parts.second);
|
|
68
|
-
case "a": return parts.ampm;
|
|
69
|
-
case "A": return parts.ampm.toUpperCase();
|
|
70
|
-
case "z": return parts.tzAbbr;
|
|
71
|
-
default: return token;
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// ---------------------------------------------------------------------------
|
|
77
|
-
// Number helpers
|
|
78
|
-
// ---------------------------------------------------------------------------
|
|
79
|
-
|
|
80
|
-
function numberFormatter(...args: any[]) {
|
|
81
|
-
return new Intl.NumberFormat(...args).format;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const functions: FunctionMap = {
|
|
85
|
-
push(...args: any[]) {
|
|
86
|
-
const [target, item] = args;
|
|
87
|
-
target.push(item);
|
|
88
|
-
},
|
|
89
|
-
|
|
90
|
-
shift(...args: any[]) {
|
|
91
|
-
const [target] = args;
|
|
92
|
-
return target.shift();
|
|
93
|
-
},
|
|
94
|
-
|
|
95
|
-
currentDate(tz?: string) {
|
|
96
|
-
return new Date();
|
|
97
|
-
},
|
|
98
|
-
|
|
99
|
-
formatDate(dateString: string, format: string, timezone?: string) {
|
|
100
|
-
const date = /^\d+$/.test(dateString)
|
|
101
|
-
? new Date(+dateString)
|
|
102
|
-
: new Date(dateString);
|
|
103
|
-
const tz = timezone || "UTC";
|
|
104
|
-
return applyFormat(getDateParts(date, tz), format);
|
|
105
|
-
},
|
|
106
|
-
|
|
107
|
-
numberFormatter,
|
|
108
|
-
|
|
109
|
-
formatDollars: numberFormatter("en-us", {
|
|
110
|
-
minimumFractionDigits: 2,
|
|
111
|
-
maximumFractionDigits: 2,
|
|
112
|
-
}),
|
|
113
|
-
|
|
114
|
-
// Array/collection operations (replaces lodash)
|
|
115
|
-
map(arr: any[], fn: (...args: any[]) => any) {
|
|
116
|
-
return arr.map(fn);
|
|
117
|
-
},
|
|
118
|
-
filter(arr: any[], fn: (...args: any[]) => any) {
|
|
119
|
-
return arr.filter(fn);
|
|
120
|
-
},
|
|
121
|
-
find(arr: any[], fn: (...args: any[]) => any) {
|
|
122
|
-
return arr.find(fn);
|
|
123
|
-
},
|
|
124
|
-
forEach(arr: any[], fn: (...args: any[]) => any) {
|
|
125
|
-
arr.forEach(fn);
|
|
126
|
-
},
|
|
127
|
-
reduce(arr: any[], fn: (...args: any[]) => any, initial?: any) {
|
|
128
|
-
return initial !== undefined ? arr.reduce(fn, initial) : arr.reduce(fn);
|
|
129
|
-
},
|
|
130
|
-
concat(arr: any[], ...items: any[]) {
|
|
131
|
-
return arr.concat(...items);
|
|
132
|
-
},
|
|
133
|
-
keys(obj: any) {
|
|
134
|
-
return Object.keys(obj);
|
|
135
|
-
},
|
|
136
|
-
values(obj: any) {
|
|
137
|
-
return Object.values(obj);
|
|
138
|
-
},
|
|
139
|
-
entries(obj: any) {
|
|
140
|
-
return Object.entries(obj);
|
|
141
|
-
},
|
|
142
|
-
assign(...args: any[]) {
|
|
143
|
-
return Object.assign({}, ...args);
|
|
144
|
-
},
|
|
145
|
-
join(arr: any[], sep: string) {
|
|
146
|
-
return arr.join(sep);
|
|
147
|
-
},
|
|
148
|
-
includes(arr: any[], item: any) {
|
|
149
|
-
return arr.includes(item);
|
|
150
|
-
},
|
|
151
|
-
slice(arr: any[], start?: number, end?: number) {
|
|
152
|
-
return arr.slice(start, end);
|
|
153
|
-
},
|
|
154
|
-
sort(arr: any[], fn?: (...args: any[]) => any) {
|
|
155
|
-
return [...arr].sort(fn);
|
|
156
|
-
},
|
|
157
|
-
reverse(arr: any[]) {
|
|
158
|
-
return [...arr].reverse();
|
|
159
|
-
},
|
|
160
|
-
flat(arr: any[], depth?: number) {
|
|
161
|
-
return arr.flat(depth);
|
|
162
|
-
},
|
|
163
|
-
flatMap(arr: any[], fn: (...args: any[]) => any) {
|
|
164
|
-
return arr.flatMap(fn);
|
|
165
|
-
},
|
|
166
|
-
some(arr: any[], fn: (...args: any[]) => any) {
|
|
167
|
-
return arr.some(fn);
|
|
168
|
-
},
|
|
169
|
-
every(arr: any[], fn: (...args: any[]) => any) {
|
|
170
|
-
return arr.every(fn);
|
|
171
|
-
},
|
|
172
|
-
indexOf(arr: any[], item: any) {
|
|
173
|
-
return arr.indexOf(item);
|
|
174
|
-
},
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
export default functions;
|
package/src/environment.ts
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
// Properties that could be used for prototype-chain traversal or pollution.
|
|
2
|
-
// Defense-in-depth: also checked in evaluate.ts MemberExpression.
|
|
3
|
-
const BLOCKED_PROPS = new Set([
|
|
4
|
-
"constructor",
|
|
5
|
-
"__proto__",
|
|
6
|
-
"prototype",
|
|
7
|
-
"__defineGetter__",
|
|
8
|
-
"__defineSetter__",
|
|
9
|
-
"__lookupGetter__",
|
|
10
|
-
"__lookupSetter__",
|
|
11
|
-
]);
|
|
12
|
-
|
|
13
|
-
function assertSafeParts(parts: string[]): void {
|
|
14
|
-
for (const part of parts) {
|
|
15
|
-
if (BLOCKED_PROPS.has(part)) {
|
|
16
|
-
throw new Error(`Access to property '${part}' is not allowed`);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function deepHas(obj: any, path: string): boolean {
|
|
22
|
-
const parts = path.split(".");
|
|
23
|
-
assertSafeParts(parts);
|
|
24
|
-
let current = obj;
|
|
25
|
-
for (const part of parts) {
|
|
26
|
-
if (current == null || typeof current !== "object") return false;
|
|
27
|
-
if (!Object.prototype.hasOwnProperty.call(current, part)) return false;
|
|
28
|
-
current = current[part];
|
|
29
|
-
}
|
|
30
|
-
return true;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function deepGet(obj: any, path: string): any {
|
|
34
|
-
const parts = path.split(".");
|
|
35
|
-
assertSafeParts(parts);
|
|
36
|
-
let current = obj;
|
|
37
|
-
for (const part of parts) {
|
|
38
|
-
if (current == null) return undefined;
|
|
39
|
-
current = current[part];
|
|
40
|
-
}
|
|
41
|
-
return current;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function deepSet(obj: any, path: string, value: any): void {
|
|
45
|
-
const parts = path.split(".");
|
|
46
|
-
assertSafeParts(parts);
|
|
47
|
-
let current = obj;
|
|
48
|
-
for (let i = 0; i < parts.length - 1; i++) {
|
|
49
|
-
if (current[parts[i]] == null || typeof current[parts[i]] !== "object") {
|
|
50
|
-
current[parts[i]] = {};
|
|
51
|
-
}
|
|
52
|
-
current = current[parts[i]];
|
|
53
|
-
}
|
|
54
|
-
current[parts[parts.length - 1]] = value;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export interface Loc {
|
|
58
|
-
line: number;
|
|
59
|
-
column: number;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export default class Environment {
|
|
63
|
-
vars: Record<string, any>;
|
|
64
|
-
parent: Environment | null;
|
|
65
|
-
|
|
66
|
-
constructor(parent?: Environment) {
|
|
67
|
-
this.vars = Object.create(parent ? parent.vars : null);
|
|
68
|
-
this.parent = parent || null;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
extend(): Environment {
|
|
72
|
-
return new Environment(this);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
getGlobalScope(): Environment {
|
|
76
|
-
let scope: Environment = this;
|
|
77
|
-
while (scope.parent) {
|
|
78
|
-
scope = scope.parent;
|
|
79
|
-
}
|
|
80
|
-
return scope;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
lookup(name: string): Environment {
|
|
84
|
-
let scope: Environment = this;
|
|
85
|
-
while (scope.parent) {
|
|
86
|
-
if (Object.prototype.hasOwnProperty.call(scope.vars, name)) {
|
|
87
|
-
return scope;
|
|
88
|
-
}
|
|
89
|
-
scope = scope.parent;
|
|
90
|
-
}
|
|
91
|
-
return scope;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
defined(name: string): boolean {
|
|
95
|
-
if (name in this.vars) {
|
|
96
|
-
return true;
|
|
97
|
-
}
|
|
98
|
-
if (deepHas(this.vars, name)) {
|
|
99
|
-
return true;
|
|
100
|
-
}
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
get(name: string, loc: Loc): any {
|
|
105
|
-
if (name in this.vars) {
|
|
106
|
-
return this.vars[name];
|
|
107
|
-
}
|
|
108
|
-
if (deepHas(this.vars, name)) {
|
|
109
|
-
return deepGet(this.vars, name);
|
|
110
|
-
}
|
|
111
|
-
throw new ReferenceError(
|
|
112
|
-
`"${name}" is not defined (${loc.line}:${loc.column})`
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
set(name: string, value: any, loc: Loc): any {
|
|
117
|
-
const baseName = name.replace(/\..*/, "");
|
|
118
|
-
const scope = this.lookup(baseName);
|
|
119
|
-
if (!scope && this.parent) {
|
|
120
|
-
throw new ReferenceError(
|
|
121
|
-
`"${name}" is not defined (${loc.line}:${loc.column})`
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
deepSet((scope || this).vars, name, value);
|
|
125
|
-
return value;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
def(name: string, value: any): any {
|
|
129
|
-
deepSet(this.vars, name, value);
|
|
130
|
-
return value;
|
|
131
|
-
}
|
|
132
|
-
}
|