poe-code 14.0.73 → 14.0.75
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/dist/index.js +2732 -1188
- package/dist/index.js.map +4 -4
- package/dist/metafile.json +1 -1
- package/package.json +1 -1
- package/packages/safe-js/dist/chunks/{chunk-I2Z37MKZ.js → chunk-BAJI3K4B.js} +2 -2
- package/packages/safe-js/dist/chunks/{chunk-XKCTB5FR.js → chunk-EOTY4PCT.js} +2902 -1425
- package/packages/safe-js/dist/chunks/chunk-EOTY4PCT.js.map +7 -0
- package/packages/safe-js/dist/cli.js +2 -2
- package/packages/safe-js/dist/core.js +1 -1
- package/packages/safe-js/dist/graph-depth.js +3 -0
- package/packages/safe-js/dist/index.js +2 -2
- package/packages/safe-js/dist/interp/cancel.js +3 -0
- package/packages/safe-js/dist/interp/globals/misc.js +2 -1
- package/packages/safe-js/dist/interp/globals/object.js +3 -0
- package/packages/safe-js/dist/interp/globals/regex.js +196 -9
- package/packages/safe-js/dist/interp/interpreter.js +14 -7
- package/packages/safe-js/dist/interp/iteration.js +9 -1
- package/packages/safe-js/dist/interp/methods/regex-split.d.ts +3 -0
- package/packages/safe-js/dist/interp/methods/regex-split.js +102 -0
- package/packages/safe-js/dist/interp/methods/regex.d.ts +8 -4
- package/packages/safe-js/dist/interp/methods/regex.js +141 -24
- package/packages/safe-js/dist/interp/methods/regexp-iterator.d.ts +12 -0
- package/packages/safe-js/dist/interp/methods/regexp-iterator.js +101 -0
- package/packages/safe-js/dist/interp/methods/string.d.ts +2 -0
- package/packages/safe-js/dist/interp/methods/string.js +415 -121
- package/packages/safe-js/dist/interp/object-model.d.ts +3 -0
- package/packages/safe-js/dist/interp/object-model.js +43 -10
- package/packages/safe-js/dist/interp/regex/compile-guard.js +26 -4
- package/packages/safe-js/dist/interp/regex/engine.d.ts +4 -0
- package/packages/safe-js/dist/interp/regex/engine.js +230 -30
- package/packages/safe-js/dist/interp/regex/parse.d.ts +42 -4
- package/packages/safe-js/dist/interp/regex/parse.js +429 -25
- package/packages/safe-js/dist/interp/regexp-iterator.d.ts +16 -0
- package/packages/safe-js/dist/interp/regexp-iterator.js +23 -0
- package/packages/safe-js/dist/interp/string-coercion.js +11 -7
- package/packages/safe-js/dist/interp/values.d.ts +2 -1
- package/packages/safe-js/dist/interp/values.js +39 -2
- package/packages/safe-js/dist/snapshot/dump-format.js +35 -3
- package/packages/safe-js/dist/snapshot/replay-data.d.ts +12 -1
- package/packages/safe-js/dist/snapshot/replay-data.js +42 -1
- package/packages/safe-js/dist/snapshot/replay-inputs.js +17 -0
- package/packages/safe-js/dist/snapshot/restore.js +23 -1
- package/packages/safe-js/dist/snapshot/serialize.d.ts +13 -2
- package/packages/safe-js/dist/snapshot/serialize.js +30 -11
- package/packages/safe-js/dist/snapshot/validation.js +22 -2
- package/packages/safe-js/dist/chunks/chunk-XKCTB5FR.js.map +0 -7
- /package/packages/safe-js/dist/chunks/{chunk-I2Z37MKZ.js.map → chunk-BAJI3K4B.js.map} +0 -0
|
@@ -9,11 +9,14 @@ export declare function getGuestFunctionProperty(closure: SandboxClosure, key: P
|
|
|
9
9
|
export declare function installObjectPrototype(budget: Budget, prototype: SandboxObject, constructor: SandboxClosure): void;
|
|
10
10
|
export declare function installBoxedPrototype(budget: Budget, prototype: SandboxObject, constructor: SandboxClosure, kind?: BoxedKind): void;
|
|
11
11
|
export declare function getBoxedPrototype(value: BoxedPrimitive, budget: Budget): SandboxObject | undefined;
|
|
12
|
+
export declare function installRegexPrototype(budget: Budget, prototype: SandboxObject, constructor: SandboxClosure): void;
|
|
13
|
+
export declare function hasRegexPropertyOverride(value: SandboxValue, keys: readonly string[], budget: Budget): boolean;
|
|
12
14
|
export declare function isDefaultBoxedMethod(value: BoxedPrimitive, key: string, budget: Budget): boolean;
|
|
13
15
|
export declare function isIntrinsicConstructor(value: object): boolean;
|
|
14
16
|
export declare function releaseObjectPrototype(budget: Budget): void;
|
|
15
17
|
export declare function getSandboxPrototype(value: object, budget?: Budget): object | null;
|
|
16
18
|
export declare function hasExplicitSandboxPrototype(value: object): boolean;
|
|
19
|
+
export declare function hasNullObjectPrototype(value: object): boolean;
|
|
17
20
|
export declare function getSandboxPropertyDescriptor(value: SandboxValue, key: PropertyKey, budget?: Budget): PropertyDescriptor | undefined;
|
|
18
21
|
export declare function getSandboxDataProperty(value: SandboxValue, key: PropertyKey, budget?: Budget): SandboxValue;
|
|
19
22
|
export declare function setSandboxPrototype(value: object, prototype: object | null, budget?: Budget): void;
|
|
@@ -9,6 +9,8 @@ const functionProperties = new WeakMap();
|
|
|
9
9
|
const prototypes = new WeakMap();
|
|
10
10
|
const intrinsicPrototypes = new WeakMap();
|
|
11
11
|
const boxedPrototypes = new WeakMap();
|
|
12
|
+
const regexPrototypes = new WeakMap();
|
|
13
|
+
const initialRegexDescriptors = new WeakMap();
|
|
12
14
|
const intrinsicPrototypeRoots = new WeakMap();
|
|
13
15
|
const intrinsicConstructors = new WeakMap();
|
|
14
16
|
const initialBoxedMethods = new WeakMap();
|
|
@@ -80,6 +82,24 @@ export function installBoxedPrototype(budget, prototype, constructor, kind = typ
|
|
|
80
82
|
export function getBoxedPrototype(value, budget) {
|
|
81
83
|
return boxedPrototypes.get(budget)?.get(typeof value);
|
|
82
84
|
}
|
|
85
|
+
export function installRegexPrototype(budget, prototype, constructor) {
|
|
86
|
+
regexPrototypes.set(budget, prototype);
|
|
87
|
+
initialRegexDescriptors.set(budget, Object.getOwnPropertyDescriptors(prototype));
|
|
88
|
+
registerIntrinsicPrototype(budget, prototype, constructor);
|
|
89
|
+
}
|
|
90
|
+
export function hasRegexPropertyOverride(value, keys, budget) {
|
|
91
|
+
const initial = initialRegexDescriptors.get(budget);
|
|
92
|
+
const prototype = regexPrototypes.get(budget);
|
|
93
|
+
const defaultPrototype = isSandboxRegex(value) && prototype !== undefined && getSandboxPrototype(value, budget) === prototype;
|
|
94
|
+
return keys.some(key => {
|
|
95
|
+
const current = defaultPrototype
|
|
96
|
+
? Object.getOwnPropertyDescriptor(getRegexProperties(value), key) ?? Object.getOwnPropertyDescriptor(prototype, key)
|
|
97
|
+
: getSandboxPropertyDescriptor(value, key, budget);
|
|
98
|
+
const expected = initial?.[key];
|
|
99
|
+
return expected === undefined ? current !== undefined : current === undefined ||
|
|
100
|
+
current.value !== expected.value || current.get !== expected.get || current.set !== expected.set;
|
|
101
|
+
});
|
|
102
|
+
}
|
|
83
103
|
export function isDefaultBoxedMethod(value, key, budget) {
|
|
84
104
|
const prototype = getBoxedPrototype(value, budget);
|
|
85
105
|
return prototype !== undefined && initialBoxedMethods.get(prototype)?.has(key) === true &&
|
|
@@ -93,7 +113,10 @@ function registerIntrinsicPrototype(budget, prototype, constructor) {
|
|
|
93
113
|
if (roots === undefined)
|
|
94
114
|
intrinsicPrototypeRoots.set(budget, (roots = new Set()));
|
|
95
115
|
roots.add(prototype);
|
|
96
|
-
const
|
|
116
|
+
const methods = Reflect.ownKeys(prototype)
|
|
117
|
+
.map(key => Object.getOwnPropertyDescriptor(prototype, key)?.value)
|
|
118
|
+
.filter(isGuestClosure);
|
|
119
|
+
const records = [...new Set([prototype, constructor, ...methods])]
|
|
97
120
|
.map((target) => ({
|
|
98
121
|
target,
|
|
99
122
|
value: isGuestClosure(target) ? materializeFunctionProperties(target) : target,
|
|
@@ -102,7 +125,7 @@ function registerIntrinsicPrototype(budget, prototype, constructor) {
|
|
|
102
125
|
}))
|
|
103
126
|
.map((record) => ({
|
|
104
127
|
...record,
|
|
105
|
-
descriptors: new Map(
|
|
128
|
+
descriptors: new Map(Reflect.ownKeys(record.value).map(key => [key, Object.getOwnPropertyDescriptor(record.value, key)]))
|
|
106
129
|
}));
|
|
107
130
|
const unchanged = (before, after) => before !== undefined &&
|
|
108
131
|
after !== undefined &&
|
|
@@ -116,16 +139,19 @@ function registerIntrinsicPrototype(budget, prototype, constructor) {
|
|
|
116
139
|
const current = Object.getOwnPropertyDescriptors(value);
|
|
117
140
|
return (getSandboxPrototype(target) === parent &&
|
|
118
141
|
hasExplicitSandboxPrototype(target) === explicit &&
|
|
119
|
-
|
|
120
|
-
|
|
142
|
+
Reflect.ownKeys(current).length === descriptors.size &&
|
|
143
|
+
Reflect.ownKeys(value).every((key) => unchanged(descriptors.get(key), Object.getOwnPropertyDescriptor(value, key))));
|
|
121
144
|
}));
|
|
122
145
|
budget.setRetainedValues(prototype, () => records.flatMap(({ target, value, descriptors, prototype: parent }) => [
|
|
123
146
|
...(getSandboxPrototype(target) === parent
|
|
124
147
|
? []
|
|
125
148
|
: [getSandboxPrototype(target)]),
|
|
126
|
-
...
|
|
127
|
-
|
|
128
|
-
|
|
149
|
+
...Reflect.ownKeys(value).flatMap(key => {
|
|
150
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
151
|
+
return unchanged(descriptors.get(key), descriptor)
|
|
152
|
+
? []
|
|
153
|
+
: [key, descriptor.value, ...retainedAccessorClosures(descriptor)];
|
|
154
|
+
})
|
|
129
155
|
]));
|
|
130
156
|
}
|
|
131
157
|
export function releaseObjectPrototype(budget) {
|
|
@@ -133,11 +159,15 @@ export function releaseObjectPrototype(budget) {
|
|
|
133
159
|
budget.setRetainedValues(prototype, undefined);
|
|
134
160
|
intrinsicPrototypeRoots.delete(budget);
|
|
135
161
|
boxedPrototypes.delete(budget);
|
|
162
|
+
regexPrototypes.delete(budget);
|
|
163
|
+
initialRegexDescriptors.delete(budget);
|
|
136
164
|
intrinsicPrototypes.delete(budget);
|
|
137
165
|
}
|
|
138
166
|
export function getSandboxPrototype(value, budget) {
|
|
139
167
|
if (prototypes.has(value))
|
|
140
168
|
return prototypes.get(value) ?? null;
|
|
169
|
+
if (budget !== undefined && isSandboxRegex(value))
|
|
170
|
+
return regexPrototypes.get(budget) ?? null;
|
|
141
171
|
if (budget !== undefined && isSandboxBox(value)) {
|
|
142
172
|
const prototype = getBoxedPrototype(boxedValue(value), budget);
|
|
143
173
|
if (prototype !== undefined && prototype !== value)
|
|
@@ -150,6 +180,9 @@ export function getSandboxPrototype(value, budget) {
|
|
|
150
180
|
export function hasExplicitSandboxPrototype(value) {
|
|
151
181
|
return prototypes.has(value);
|
|
152
182
|
}
|
|
183
|
+
export function hasNullObjectPrototype(value) {
|
|
184
|
+
return prototypes.get(value) === null && !Array.isArray(value) && !isSandboxClosure(value) && !isSandboxRegex(value);
|
|
185
|
+
}
|
|
153
186
|
export function getSandboxPropertyDescriptor(value, key, budget) {
|
|
154
187
|
const hostProperties = isSandboxClosure(value) ? value.properties : undefined;
|
|
155
188
|
if (isSandboxClosure(value) && !isGuestClosure(value))
|
|
@@ -213,13 +246,13 @@ export function setSandboxPrototype(value, prototype, budget) {
|
|
|
213
246
|
if (budget !== undefined && intrinsicPrototypes.get(budget) === value && prototype !== null) {
|
|
214
247
|
throw new TypeError("Object.prototype has an immutable null prototype.");
|
|
215
248
|
}
|
|
216
|
-
if ((!Array.isArray(value) && !isPrototypeRecord(value)) ||
|
|
249
|
+
if ((!Array.isArray(value) && !isSandboxRegex(value) && !isPrototypeRecord(value)) ||
|
|
217
250
|
(prototype !== null && !Array.isArray(prototype) && !isPrototypeRecord(prototype))) {
|
|
218
251
|
throw new TypeError("Prototype links require ordinary sandbox objects, arrays, or guest functions.");
|
|
219
252
|
}
|
|
220
253
|
if (prototypes.has(value) && getSandboxPrototype(value, budget) === prototype)
|
|
221
254
|
return;
|
|
222
|
-
if (!Object.isExtensible(isGuestClosure(value) ? materializeFunctionProperties(value) : value)) {
|
|
255
|
+
if (!Object.isExtensible(isGuestClosure(value) ? materializeFunctionProperties(value) : isSandboxRegex(value) ? getRegexProperties(value) : value)) {
|
|
223
256
|
throw new TypeError("Cannot change the prototype of a non-extensible object.");
|
|
224
257
|
}
|
|
225
258
|
let depth = 0;
|
|
@@ -258,7 +291,7 @@ export function hasGuestObjectState(value) {
|
|
|
258
291
|
return !intrinsicUnchanged();
|
|
259
292
|
if (isLiveCapability(value))
|
|
260
293
|
return true;
|
|
261
|
-
if (functionProperties.has(value) || prototypes.has(value))
|
|
294
|
+
if (functionProperties.has(value) || (prototypes.has(value) && !hasNullObjectPrototype(value)))
|
|
262
295
|
return true;
|
|
263
296
|
if (isSandboxBox(value) || isSandboxDate(value))
|
|
264
297
|
return false;
|
|
@@ -121,7 +121,7 @@ export class RegexCompileGuard {
|
|
|
121
121
|
this.work(flags.length);
|
|
122
122
|
}
|
|
123
123
|
retain(pattern, valueUnits = 0) {
|
|
124
|
-
const units =
|
|
124
|
+
const units = measurePattern(pattern);
|
|
125
125
|
if (this.ticket !== undefined) {
|
|
126
126
|
this.ticket.owner.budget.resizeCompileTicket(this.ticket, units + valueUnits);
|
|
127
127
|
}
|
|
@@ -139,7 +139,16 @@ export class RegexCompileGuard {
|
|
|
139
139
|
}
|
|
140
140
|
}
|
|
141
141
|
export function regexCompiledData(pattern) {
|
|
142
|
-
return (compiledData.get(pattern) ?? { units:
|
|
142
|
+
return (compiledData.get(pattern) ?? { units: measurePattern(pattern) });
|
|
143
|
+
}
|
|
144
|
+
function measurePattern(pattern) {
|
|
145
|
+
let units = 10 + pattern.source.length + measureNode(pattern.body);
|
|
146
|
+
if (pattern.groups !== undefined) {
|
|
147
|
+
units += 1;
|
|
148
|
+
for (const [name, indices] of Object.entries(pattern.groups))
|
|
149
|
+
units += name.length + 2 + indices.length;
|
|
150
|
+
}
|
|
151
|
+
return units;
|
|
143
152
|
}
|
|
144
153
|
function measureNode(node) {
|
|
145
154
|
switch (node.type) {
|
|
@@ -148,18 +157,27 @@ function measureNode(node) {
|
|
|
148
157
|
return 2;
|
|
149
158
|
case "literal":
|
|
150
159
|
return 3 + node.value.length;
|
|
160
|
+
case "namedBackreference":
|
|
161
|
+
return 3 + node.name.length;
|
|
151
162
|
case "anchor":
|
|
152
163
|
case "wordBoundary":
|
|
164
|
+
case "backreference":
|
|
153
165
|
return 3;
|
|
154
166
|
case "characterClass": {
|
|
155
167
|
let usage = 5 + node.items.length;
|
|
168
|
+
if (node.operation !== undefined)
|
|
169
|
+
usage += 1 + node.operation.length;
|
|
170
|
+
if (node.strings !== undefined)
|
|
171
|
+
usage += 1;
|
|
156
172
|
for (const item of node.items) {
|
|
157
173
|
usage +=
|
|
158
174
|
item.type === "character"
|
|
159
175
|
? 3 + item.value.length
|
|
160
176
|
: item.type === "range"
|
|
161
177
|
? 4 + item.from.length + item.to.length
|
|
162
|
-
: 4
|
|
178
|
+
: item.type === "property" ? 4 + item.value.length + (item.strings === undefined ? 0 : 1)
|
|
179
|
+
: item.type === "set" ? 3 + measureNode({ type: "characterClass", ...item.value })
|
|
180
|
+
: item.type === "strings" ? 4 + item.values.reduce((total, value) => total + 1 + value.length, 0) : 4;
|
|
163
181
|
}
|
|
164
182
|
return usage;
|
|
165
183
|
}
|
|
@@ -172,7 +190,11 @@ function measureNode(node) {
|
|
|
172
190
|
return usage;
|
|
173
191
|
}
|
|
174
192
|
case "group":
|
|
175
|
-
return 5 + measureNode(node.body)
|
|
193
|
+
return 5 + measureNode(node.body) + (node.name === undefined ? 0 : 1 + node.name.length) +
|
|
194
|
+
(node.modifiers === undefined ? 0 : 2 + Object.keys(node.modifiers).length);
|
|
195
|
+
case "lookahead":
|
|
196
|
+
case "lookbehind":
|
|
197
|
+
return 4 + measureNode(node.body);
|
|
176
198
|
case "quantifier":
|
|
177
199
|
return (Object.hasOwn(node, "max") ? 6 : 5) + measureNode(node.body);
|
|
178
200
|
}
|
|
@@ -3,7 +3,11 @@ export type RegexMatch = {
|
|
|
3
3
|
index: number;
|
|
4
4
|
text: string;
|
|
5
5
|
captures: (string | undefined)[];
|
|
6
|
+
indices?: ([number, number] | undefined)[];
|
|
7
|
+
groups?: Record<string, string | undefined>;
|
|
8
|
+
indicesGroups?: Record<string, [number, number] | undefined>;
|
|
6
9
|
};
|
|
7
10
|
export declare function matchRegex(pattern: RegexPattern, input: string, lastIndex?: number): RegexMatch | null;
|
|
8
11
|
export declare function matchRegexFrom(pattern: RegexPattern, input: string, startIndex: number): RegexMatch | null;
|
|
9
12
|
export declare function normalizeLastIndex(lastIndex: number): number;
|
|
13
|
+
export declare function advanceStringIndex(input: string, index: number, unicode: boolean): number;
|
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { allocateRegexSteps } from "../budget.js";
|
|
2
2
|
export function matchRegex(pattern, input, lastIndex = 0) {
|
|
3
|
-
const startIndex = pattern.flags.global ? normalizeLastIndex(lastIndex) : 0;
|
|
3
|
+
const startIndex = pattern.flags.global || pattern.flags.sticky ? normalizeLastIndex(lastIndex) : 0;
|
|
4
4
|
return matchRegexFrom(pattern, input, startIndex);
|
|
5
5
|
}
|
|
6
6
|
export function matchRegexFrom(pattern, input, startIndex) {
|
|
7
7
|
if (startIndex > input.length) {
|
|
8
8
|
return null;
|
|
9
9
|
}
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
const unicode = pattern.flags.unicode || pattern.flags.unicodeSets;
|
|
11
|
+
if (unicode && startIndex > 0 && startIndex < input.length &&
|
|
12
|
+
input.charCodeAt(startIndex) >= 0xdc00 && input.charCodeAt(startIndex) <= 0xdfff &&
|
|
13
|
+
input.charCodeAt(startIndex - 1) >= 0xd800 && input.charCodeAt(startIndex - 1) <= 0xdbff)
|
|
14
|
+
startIndex--;
|
|
15
|
+
for (let attempt = startIndex; attempt <= input.length; attempt = advanceStringIndex(input, attempt, unicode)) {
|
|
16
|
+
const context = { input, flags: pattern.flags, groups: pattern.groups, direction: 1, work: { steps: 0 } };
|
|
12
17
|
charge(context);
|
|
13
18
|
const initialState = {
|
|
14
19
|
position: attempt,
|
|
@@ -16,26 +21,63 @@ export function matchRegexFrom(pattern, input, startIndex) {
|
|
|
16
21
|
};
|
|
17
22
|
const result = matchNode(pattern.body, initialState, context).next();
|
|
18
23
|
if (!result.done) {
|
|
19
|
-
return toRegexMatch(input, attempt, result.value);
|
|
24
|
+
return toRegexMatch(input, attempt, result.value, pattern.flags.hasIndices, pattern.groups);
|
|
20
25
|
}
|
|
26
|
+
if (pattern.flags.sticky)
|
|
27
|
+
break;
|
|
21
28
|
}
|
|
22
29
|
return null;
|
|
23
30
|
}
|
|
24
31
|
function* matchNode(node, state, context) {
|
|
25
32
|
charge(context);
|
|
33
|
+
const unicode = context.flags.unicode || context.flags.unicodeSets;
|
|
34
|
+
const character = readCharacter(context.input, state.position, context.direction, unicode);
|
|
26
35
|
switch (node.type) {
|
|
27
36
|
case "empty":
|
|
28
37
|
yield state;
|
|
29
38
|
return;
|
|
30
39
|
case "literal":
|
|
31
|
-
if (charactersEqual(
|
|
32
|
-
yield { ...state, position: state.position +
|
|
40
|
+
if (charactersEqual(character, node.value, context.flags.ignoreCase, unicode)) {
|
|
41
|
+
yield { ...state, position: state.position + context.direction * character.length };
|
|
33
42
|
}
|
|
34
43
|
return;
|
|
44
|
+
case "backreference":
|
|
45
|
+
case "namedBackreference": {
|
|
46
|
+
let capture;
|
|
47
|
+
if (node.type === "backreference")
|
|
48
|
+
capture = state.captures[node.index - 1];
|
|
49
|
+
else {
|
|
50
|
+
for (const index of context.groups?.[node.name] ?? []) {
|
|
51
|
+
charge(context);
|
|
52
|
+
if (state.captures[index - 1] !== undefined) {
|
|
53
|
+
capture = state.captures[index - 1];
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (capture === undefined) {
|
|
59
|
+
yield state;
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
let position = state.position;
|
|
63
|
+
let reference = context.direction === 1 ? capture.start : capture.end;
|
|
64
|
+
const end = context.direction === 1 ? capture.end : capture.start;
|
|
65
|
+
while (reference !== end) {
|
|
66
|
+
charge(context);
|
|
67
|
+
const expected = readCharacter(context.input, reference, context.direction, unicode);
|
|
68
|
+
const actual = readCharacter(context.input, position, context.direction, unicode);
|
|
69
|
+
if (!charactersEqual(actual, expected, context.flags.ignoreCase, unicode))
|
|
70
|
+
return;
|
|
71
|
+
reference += context.direction * expected.length;
|
|
72
|
+
position += context.direction * actual.length;
|
|
73
|
+
}
|
|
74
|
+
yield { ...state, position };
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
35
77
|
case "dot":
|
|
36
|
-
if (
|
|
37
|
-
(context.flags.dotAll || !isLineTerminator(
|
|
38
|
-
yield { ...state, position: state.position +
|
|
78
|
+
if (character !== undefined &&
|
|
79
|
+
(context.flags.dotAll || !isLineTerminator(character))) {
|
|
80
|
+
yield { ...state, position: state.position + context.direction * character.length };
|
|
39
81
|
}
|
|
40
82
|
return;
|
|
41
83
|
case "anchor":
|
|
@@ -44,18 +86,25 @@ function* matchNode(node, state, context) {
|
|
|
44
86
|
}
|
|
45
87
|
return;
|
|
46
88
|
case "wordBoundary": {
|
|
47
|
-
const previousWord = state.position > 0 &&
|
|
48
|
-
const nextWord = state.position < context.input.length &&
|
|
89
|
+
const previousWord = state.position > 0 && matchesCharacterClassItem(context.input[state.position - 1], { type: "kind", kind: "word", negated: false }, context.flags.ignoreCase, unicode);
|
|
90
|
+
const nextWord = state.position < context.input.length && matchesCharacterClassItem(context.input[state.position], { type: "kind", kind: "word", negated: false }, context.flags.ignoreCase, unicode);
|
|
49
91
|
if ((previousWord !== nextWord) !== node.negated) {
|
|
50
92
|
yield state;
|
|
51
93
|
}
|
|
52
94
|
return;
|
|
53
95
|
}
|
|
54
96
|
case "characterClass": {
|
|
55
|
-
|
|
97
|
+
if (context.flags.unicodeSets) {
|
|
98
|
+
const lengths = [...matchUnicodeSet(node, state.position, context)].sort((a, b) => b - a);
|
|
99
|
+
for (const length of lengths) {
|
|
100
|
+
charge(context);
|
|
101
|
+
yield { ...state, position: state.position + context.direction * length };
|
|
102
|
+
}
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
56
105
|
if (character !== undefined &&
|
|
57
|
-
matchesCharacterClass(character, node.items, node.negated, context
|
|
58
|
-
yield { ...state, position: state.position +
|
|
106
|
+
matchesCharacterClass(character, node.items, node.negated, context)) {
|
|
107
|
+
yield { ...state, position: state.position + context.direction * character.length };
|
|
59
108
|
}
|
|
60
109
|
return;
|
|
61
110
|
}
|
|
@@ -67,14 +116,30 @@ function* matchNode(node, state, context) {
|
|
|
67
116
|
yield* matchNode(alternative, cloneState(state), context);
|
|
68
117
|
}
|
|
69
118
|
return;
|
|
119
|
+
case "lookahead":
|
|
120
|
+
case "lookbehind": {
|
|
121
|
+
const result = matchNode(node.body, cloneState(state), {
|
|
122
|
+
...context, direction: node.type === "lookbehind" ? -1 : 1
|
|
123
|
+
}).next();
|
|
124
|
+
if (node.negated) {
|
|
125
|
+
if (result.done)
|
|
126
|
+
yield state;
|
|
127
|
+
}
|
|
128
|
+
else if (!result.done) {
|
|
129
|
+
yield { position: state.position, captures: result.value.captures };
|
|
130
|
+
}
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
70
133
|
case "group":
|
|
71
|
-
for (const result of matchNode(node.body, cloneState(state), context
|
|
134
|
+
for (const result of matchNode(node.body, cloneState(state), node.modifiers === undefined ? context : {
|
|
135
|
+
...context, flags: { ...context.flags, ...node.modifiers }
|
|
136
|
+
})) {
|
|
72
137
|
if (!node.capturing || node.index === undefined) {
|
|
73
138
|
yield result;
|
|
74
139
|
continue;
|
|
75
140
|
}
|
|
76
141
|
const captures = result.captures.slice();
|
|
77
|
-
captures[node.index - 1] = { start: state.position, end: result.position };
|
|
142
|
+
captures[node.index - 1] = { start: Math.min(state.position, result.position), end: Math.max(state.position, result.position) };
|
|
78
143
|
yield { position: result.position, captures };
|
|
79
144
|
}
|
|
80
145
|
return;
|
|
@@ -88,7 +153,8 @@ function* matchSequence(elements, index, state, context) {
|
|
|
88
153
|
yield state;
|
|
89
154
|
return;
|
|
90
155
|
}
|
|
91
|
-
|
|
156
|
+
const element = elements[context.direction === 1 ? index : elements.length - 1 - index];
|
|
157
|
+
for (const result of matchNode(element, state, context)) {
|
|
92
158
|
yield* matchSequence(elements, index + 1, result, context);
|
|
93
159
|
}
|
|
94
160
|
}
|
|
@@ -129,11 +195,35 @@ function matchesAnchor(kind, position, context) {
|
|
|
129
195
|
position < context.input.length &&
|
|
130
196
|
isLineTerminator(context.input[position])));
|
|
131
197
|
}
|
|
132
|
-
function matchesCharacterClass(character, items, negated,
|
|
133
|
-
const matched = items.some((item) =>
|
|
198
|
+
function matchesCharacterClass(character, items, negated, context) {
|
|
199
|
+
const matched = items.some((item) => {
|
|
200
|
+
if (context.flags.unicode)
|
|
201
|
+
charge(context);
|
|
202
|
+
return matchesCharacterClassItem(character, item, context.flags.ignoreCase, context.flags.unicode);
|
|
203
|
+
});
|
|
134
204
|
return negated ? !matched : matched;
|
|
135
205
|
}
|
|
136
|
-
function matchesCharacterClassItem(character, item, ignoreCase) {
|
|
206
|
+
function matchesCharacterClassItem(character, item, ignoreCase, unicode, unicodeSets = false) {
|
|
207
|
+
if (item.type === "strings" || item.type === "set")
|
|
208
|
+
return false;
|
|
209
|
+
if (unicode) {
|
|
210
|
+
// Generated atoms only: the host classifies one code point, never executes a guest pattern.
|
|
211
|
+
const hex = (value) => `\\u{${value.codePointAt(0).toString(16)}}`;
|
|
212
|
+
let atom;
|
|
213
|
+
if (item.type === "character")
|
|
214
|
+
atom = hex(item.value);
|
|
215
|
+
else if (item.type === "range")
|
|
216
|
+
atom = `[${hex(item.from)}-${hex(item.to)}]`;
|
|
217
|
+
else if (item.type === "property")
|
|
218
|
+
atom = `\\${item.negated ? "P" : "p"}{${item.value}}`;
|
|
219
|
+
else {
|
|
220
|
+
const kind = item.kind === "digit" ? "d" : item.kind === "word" ? "w" : "s";
|
|
221
|
+
atom = `\\${item.negated ? kind.toUpperCase() : kind}`;
|
|
222
|
+
}
|
|
223
|
+
return new RegExp(`^(?:${atom})$`, (ignoreCase ? "i" : "") + (unicodeSets ? "v" : "u")).test(character);
|
|
224
|
+
}
|
|
225
|
+
if (item.type === "property")
|
|
226
|
+
return false;
|
|
137
227
|
if (item.type === "character") {
|
|
138
228
|
return charactersEqual(character, item.value, ignoreCase);
|
|
139
229
|
}
|
|
@@ -147,10 +237,10 @@ function matchesCharacterClassItem(character, item, ignoreCase) {
|
|
|
147
237
|
if (!ignoreCase) {
|
|
148
238
|
return false;
|
|
149
239
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
240
|
+
// Folding endpoints does not preserve range membership. Classify one UTF-16
|
|
241
|
+
// code unit using generated hex endpoints, never a guest pattern.
|
|
242
|
+
return new RegExp(`^[\\u${from.toString(16).padStart(4, "0")}-\\u${to.toString(16).padStart(4, "0")}]$`, "i")
|
|
243
|
+
.test(character);
|
|
154
244
|
}
|
|
155
245
|
const matched = item.kind === "digit"
|
|
156
246
|
? isDigit(character)
|
|
@@ -159,12 +249,107 @@ function matchesCharacterClassItem(character, item, ignoreCase) {
|
|
|
159
249
|
: isSpaceCharacter(character);
|
|
160
250
|
return item.negated ? !matched : matched;
|
|
161
251
|
}
|
|
162
|
-
function
|
|
163
|
-
|
|
252
|
+
function matchUnicodeSet(set, position, context) {
|
|
253
|
+
charge(context);
|
|
254
|
+
let lengths = new Set();
|
|
255
|
+
for (let index = 0; index < set.items.length; index++) {
|
|
256
|
+
const next = matchUnicodeSetItem(set.items[index], position, context);
|
|
257
|
+
if (index === 0)
|
|
258
|
+
lengths = next;
|
|
259
|
+
else if (set.operation === undefined) {
|
|
260
|
+
for (const length of next) {
|
|
261
|
+
charge(context);
|
|
262
|
+
lengths.add(length);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
for (const length of lengths) {
|
|
267
|
+
charge(context);
|
|
268
|
+
if (set.operation === "intersection" ? !next.has(length) : next.has(length))
|
|
269
|
+
lengths.delete(length);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (set.negated) {
|
|
274
|
+
const character = readCharacter(context.input, position, context.direction, true);
|
|
275
|
+
return new Set(character !== undefined && !lengths.has(character.length) ? [character.length] : []);
|
|
276
|
+
}
|
|
277
|
+
return lengths;
|
|
278
|
+
}
|
|
279
|
+
function matchUnicodeSetItem(item, position, context) {
|
|
280
|
+
charge(context);
|
|
281
|
+
if (item.type === "set")
|
|
282
|
+
return matchUnicodeSet(item.value, position, context);
|
|
283
|
+
const lengths = new Set();
|
|
284
|
+
if (item.type === "strings") {
|
|
285
|
+
for (const value of item.values) {
|
|
286
|
+
charge(context);
|
|
287
|
+
let reference = context.direction === 1 ? 0 : value.length;
|
|
288
|
+
let cursor = position;
|
|
289
|
+
const end = context.direction === 1 ? value.length : 0;
|
|
290
|
+
while (reference !== end) {
|
|
291
|
+
charge(context);
|
|
292
|
+
const expected = readCharacter(value, reference, context.direction, true);
|
|
293
|
+
const actual = readCharacter(context.input, cursor, context.direction, true);
|
|
294
|
+
if (!charactersEqual(actual, expected, context.flags.ignoreCase, true))
|
|
295
|
+
break;
|
|
296
|
+
reference += context.direction * expected.length;
|
|
297
|
+
cursor += context.direction * actual.length;
|
|
298
|
+
}
|
|
299
|
+
if (reference === end)
|
|
300
|
+
lengths.add(Math.abs(cursor - position));
|
|
301
|
+
}
|
|
302
|
+
return lengths;
|
|
303
|
+
}
|
|
304
|
+
if (item.type === "property" && item.strings) {
|
|
305
|
+
// A validated property atom denotes a finite Unicode string set. The host
|
|
306
|
+
// finds its longest member only; guest alternation/backtracking stays here.
|
|
307
|
+
const atom = `\\p{${item.value}}`;
|
|
308
|
+
const flags = (context.flags.ignoreCase ? "i" : "") + "v";
|
|
309
|
+
const matcher = new RegExp(context.direction === 1 ? atom : `(?<=(${atom}))`, flags + "y");
|
|
310
|
+
matcher.lastIndex = position;
|
|
311
|
+
const match = matcher.exec(context.input);
|
|
312
|
+
if (match === null)
|
|
313
|
+
return lengths;
|
|
314
|
+
const longest = context.direction === 1 ? match[0] : match[1];
|
|
315
|
+
const member = new RegExp(`^(?:${atom})$`, flags);
|
|
316
|
+
let consumed = 0;
|
|
317
|
+
while (consumed < longest.length) {
|
|
318
|
+
charge(context);
|
|
319
|
+
const cursor = context.direction === 1 ? consumed : longest.length - consumed;
|
|
320
|
+
consumed += readCharacter(longest, cursor, context.direction, true).length;
|
|
321
|
+
const candidate = context.direction === 1 ? longest.slice(0, consumed) : longest.slice(longest.length - consumed);
|
|
322
|
+
if (member.test(candidate))
|
|
323
|
+
lengths.add(consumed);
|
|
324
|
+
}
|
|
325
|
+
return lengths;
|
|
326
|
+
}
|
|
327
|
+
const character = readCharacter(context.input, position, context.direction, true);
|
|
328
|
+
if (character !== undefined && matchesCharacterClassItem(character, item, context.flags.ignoreCase, true, true))
|
|
329
|
+
lengths.add(character.length);
|
|
330
|
+
return lengths;
|
|
331
|
+
}
|
|
332
|
+
function toRegexMatch(input, start, state, hasIndices, groups) {
|
|
333
|
+
const match = {
|
|
164
334
|
index: start,
|
|
165
335
|
text: input.slice(start, state.position),
|
|
166
336
|
captures: state.captures.map((capture) => capture === undefined ? undefined : input.slice(capture.start, capture.end))
|
|
167
337
|
};
|
|
338
|
+
if (hasIndices) {
|
|
339
|
+
match.indices = [[start, state.position], ...state.captures.map((capture) => capture === undefined ? undefined : [capture.start, capture.end])];
|
|
340
|
+
}
|
|
341
|
+
if (groups !== undefined) {
|
|
342
|
+
match.groups = Object.create(null);
|
|
343
|
+
if (hasIndices)
|
|
344
|
+
match.indicesGroups = Object.create(null);
|
|
345
|
+
for (const [name, indices] of Object.entries(groups)) {
|
|
346
|
+
const index = indices.find(index => state.captures[index - 1] !== undefined);
|
|
347
|
+
match.groups[name] = index === undefined ? undefined : match.captures[index - 1];
|
|
348
|
+
if (match.indicesGroups !== undefined)
|
|
349
|
+
match.indicesGroups[name] = index === undefined ? undefined : match.indices[index];
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return match;
|
|
168
353
|
}
|
|
169
354
|
function cloneState(state) {
|
|
170
355
|
return { position: state.position, captures: state.captures.slice() };
|
|
@@ -194,13 +379,13 @@ function clearNodeCaptures(node, captures) {
|
|
|
194
379
|
}
|
|
195
380
|
return;
|
|
196
381
|
}
|
|
197
|
-
if (node.type === "quantifier") {
|
|
382
|
+
if (node.type === "quantifier" || node.type === "lookahead" || node.type === "lookbehind") {
|
|
198
383
|
clearNodeCaptures(node.body, captures);
|
|
199
384
|
}
|
|
200
385
|
}
|
|
201
386
|
function charge(context) {
|
|
202
|
-
context.steps += 1;
|
|
203
|
-
allocateRegexSteps(context.steps);
|
|
387
|
+
context.work.steps += 1;
|
|
388
|
+
allocateRegexSteps(context.work.steps);
|
|
204
389
|
}
|
|
205
390
|
export function normalizeLastIndex(lastIndex) {
|
|
206
391
|
if (Number.isNaN(lastIndex) || lastIndex <= 0) {
|
|
@@ -208,9 +393,24 @@ export function normalizeLastIndex(lastIndex) {
|
|
|
208
393
|
}
|
|
209
394
|
return Math.min(Math.floor(lastIndex), Number.MAX_SAFE_INTEGER);
|
|
210
395
|
}
|
|
211
|
-
function charactersEqual(left, right, ignoreCase) {
|
|
396
|
+
function charactersEqual(left, right, ignoreCase, unicode = false) {
|
|
397
|
+
if (unicode && ignoreCase && left !== undefined)
|
|
398
|
+
return new RegExp(`^\\u{${right.codePointAt(0).toString(16)}}$`, "iu").test(left);
|
|
212
399
|
return left !== undefined && foldCharacter(left, ignoreCase) === foldCharacter(right, ignoreCase);
|
|
213
400
|
}
|
|
401
|
+
export function advanceStringIndex(input, index, unicode) {
|
|
402
|
+
return index + (unicode && (input.codePointAt(index) ?? 0) > 0xffff ? 2 : 1);
|
|
403
|
+
}
|
|
404
|
+
function readCharacter(input, position, direction, unicode) {
|
|
405
|
+
let index = direction === 1 ? position : position - 1;
|
|
406
|
+
if (index < 0 || index >= input.length)
|
|
407
|
+
return undefined;
|
|
408
|
+
if (unicode && direction === -1 && index > 0 &&
|
|
409
|
+
input.charCodeAt(index) >= 0xdc00 && input.charCodeAt(index) <= 0xdfff &&
|
|
410
|
+
input.charCodeAt(index - 1) >= 0xd800 && input.charCodeAt(index - 1) <= 0xdbff)
|
|
411
|
+
index--;
|
|
412
|
+
return unicode ? String.fromCodePoint(input.codePointAt(index)) : input[index];
|
|
413
|
+
}
|
|
214
414
|
function foldCharacter(character, ignoreCase) {
|
|
215
415
|
if (!ignoreCase) {
|
|
216
416
|
return character;
|