swf2js 0.0.0-stage → 0.1.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-GPL +680 -0
- package/README.md +208 -2
- package/package.json +24 -4
- package/src/cli.js +272 -0
- package/src/decoder/button-decoder.js +136 -0
- package/src/decoder/shape-decoder.js +786 -0
- package/src/decoder/symbol-table.js +36 -0
- package/src/decoder/tag-decoder.js +1190 -0
- package/src/decoder/tag-types.js +258 -0
- package/src/emitter/asset-encoder.js +708 -0
- package/src/emitter/bundle-emitter.js +596 -0
- package/src/emitter/embedded-swf.js +246 -0
- package/src/emitter/font-encoder.js +365 -0
- package/src/emitter/timeline-data-codec.js +58 -0
- package/src/model/swf-document.js +59 -0
- package/src/model/transpile-result.js +32 -0
- package/src/parser/bit-reader.js +237 -0
- package/src/parser/decompressor.js +91 -0
- package/src/parser/swf-parser.js +164 -0
- package/src/pretty-printer/pretty-printer.js +137 -0
- package/src/runtime/avm1-runtime.js +4907 -0
- package/src/runtime/avm2-runtime.js +1591 -0
- package/src/runtime/embedded-movie-runtime.js +208 -0
- package/src/runtime/index.js +17 -0
- package/src/runtime/movie-runtime.js +306 -0
- package/src/runtime/swflib.js +14050 -0
- package/src/transpiler/actionscript.js +390 -0
- package/src/transpiler/avm1/avm1-ir.js +205 -0
- package/src/transpiler/avm1/avm1-opcodes.js +149 -0
- package/src/transpiler/avm1/avm1-rewrite.js +291 -0
- package/src/transpiler/avm1/avm1-transpiler.js +2082 -0
- package/src/transpiler/avm2/abc-reader.js +416 -0
- package/src/transpiler/avm2/avm2-transpiler.js +2560 -0
- package/src/transpiler/cfg-structurer.js +876 -0
- package/src/transpiler/timeline/display-list.js +643 -0
- package/src/transpiler/timeline/timeline-codegen.js +6354 -0
- package/src/util/diagnostics.js +50 -0
- package/src/util/logger.js +40 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview AVM1 opcode constants and metadata.
|
|
3
|
+
* Opcodes < 0x80 have no payload. Opcodes >= 0x80 are followed by a UI16 length + data.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** @enum {number} */
|
|
7
|
+
export const OP = {
|
|
8
|
+
// Reserved in the SWF action table. Boxhead 2 has branches that land on this
|
|
9
|
+
// byte inside an ActionPush length field, where it is then decoded as an action.
|
|
10
|
+
Reserved03: 0x03,
|
|
11
|
+
// Stack / arithmetic
|
|
12
|
+
Add: 0x0A,
|
|
13
|
+
Add2: 0x47,
|
|
14
|
+
Subtract: 0x0B,
|
|
15
|
+
Multiply: 0x0C,
|
|
16
|
+
Divide: 0x0D,
|
|
17
|
+
Modulo: 0x3F,
|
|
18
|
+
Increment: 0x50,
|
|
19
|
+
Decrement: 0x51,
|
|
20
|
+
ToInteger: 0x18,
|
|
21
|
+
StoreRegister: 0x87,
|
|
22
|
+
Extends: 0x69,
|
|
23
|
+
CallFrame: 0x9E,
|
|
24
|
+
Equals: 0x0E,
|
|
25
|
+
Equals2: 0x49,
|
|
26
|
+
LessThan: 0x0F,
|
|
27
|
+
LessThan2: 0x48,
|
|
28
|
+
Greater: 0x67,
|
|
29
|
+
StrictEquals: 0x66,
|
|
30
|
+
StringAdd: 0x21,
|
|
31
|
+
StringEquals: 0x13,
|
|
32
|
+
StringLessThan: 0x29,
|
|
33
|
+
StringGreater: 0x68,
|
|
34
|
+
StringLength: 0x14,
|
|
35
|
+
StringExtract: 0x15,
|
|
36
|
+
StringCompare: 0x29,
|
|
37
|
+
|
|
38
|
+
// Logic
|
|
39
|
+
And: 0x10,
|
|
40
|
+
Or: 0x11,
|
|
41
|
+
Not: 0x12,
|
|
42
|
+
BitAnd: 0x60,
|
|
43
|
+
BitOr: 0x61,
|
|
44
|
+
BitXor: 0x62,
|
|
45
|
+
BitLShift: 0x63,
|
|
46
|
+
BitRShift: 0x64,
|
|
47
|
+
BitURShift: 0x65,
|
|
48
|
+
LogicalAnd: 0x10,
|
|
49
|
+
LogicalOr: 0x11,
|
|
50
|
+
|
|
51
|
+
// Stack manipulation
|
|
52
|
+
Pop: 0x17,
|
|
53
|
+
Dup: 0x4C,
|
|
54
|
+
Swap: 0x4D,
|
|
55
|
+
PushDuplicate: 0x4C,
|
|
56
|
+
|
|
57
|
+
// Variables
|
|
58
|
+
GetVariable: 0x1C,
|
|
59
|
+
SetVariable: 0x1D,
|
|
60
|
+
DefineLocal: 0x3C,
|
|
61
|
+
DefineLocal2: 0x41,
|
|
62
|
+
Delete: 0x3A,
|
|
63
|
+
Delete2: 0x3B,
|
|
64
|
+
Enumerate: 0x46,
|
|
65
|
+
Enumerate2: 0x55,
|
|
66
|
+
|
|
67
|
+
// Members / properties
|
|
68
|
+
GetMember: 0x4E,
|
|
69
|
+
SetMember: 0x4F,
|
|
70
|
+
GetProperty: 0x22,
|
|
71
|
+
SetProperty: 0x23,
|
|
72
|
+
GetNamedMember: 0x4E,
|
|
73
|
+
|
|
74
|
+
// Object
|
|
75
|
+
NewObject: 0x40,
|
|
76
|
+
NewMethod: 0x53,
|
|
77
|
+
InitArray: 0x42,
|
|
78
|
+
InitObject: 0x43,
|
|
79
|
+
TypeOf: 0x44,
|
|
80
|
+
TargetPath: 0x45,
|
|
81
|
+
InstanceOf: 0x54,
|
|
82
|
+
Cast: 0x2B,
|
|
83
|
+
Implements: 0x2C,
|
|
84
|
+
|
|
85
|
+
// Functions
|
|
86
|
+
CallFunction: 0x3D,
|
|
87
|
+
CallMethod: 0x52,
|
|
88
|
+
Return: 0x3E,
|
|
89
|
+
DefineFunction: 0x9B, // >= 0x80: has length
|
|
90
|
+
DefineFunction2:0x8E,
|
|
91
|
+
|
|
92
|
+
// Control flow
|
|
93
|
+
Jump: 0x99,
|
|
94
|
+
If: 0x9D,
|
|
95
|
+
With: 0x94,
|
|
96
|
+
Try: 0x8F,
|
|
97
|
+
Throw: 0x2A,
|
|
98
|
+
End: 0x00,
|
|
99
|
+
|
|
100
|
+
// Timeline
|
|
101
|
+
GotoFrame: 0x81,
|
|
102
|
+
GotoFrame2: 0x9F,
|
|
103
|
+
GotoLabel: 0x8C,
|
|
104
|
+
Play: 0x06,
|
|
105
|
+
Stop: 0x07,
|
|
106
|
+
StopSounds: 0x09,
|
|
107
|
+
NextFrame: 0x04,
|
|
108
|
+
PrevFrame: 0x05,
|
|
109
|
+
ToggleQuality: 0x08,
|
|
110
|
+
|
|
111
|
+
// URL / loader
|
|
112
|
+
GetURL: 0x83,
|
|
113
|
+
GetURL2: 0x9A,
|
|
114
|
+
LoadMovie: 0x9A,
|
|
115
|
+
UnloadMovie: 0x44,
|
|
116
|
+
|
|
117
|
+
// Push / constant pool
|
|
118
|
+
Push: 0x96,
|
|
119
|
+
ConstantPool: 0x88,
|
|
120
|
+
|
|
121
|
+
// Misc
|
|
122
|
+
Trace: 0x26,
|
|
123
|
+
RandomNumber: 0x30,
|
|
124
|
+
GetTime: 0x34,
|
|
125
|
+
FSCommand2: 0x2D,
|
|
126
|
+
SetTarget: 0x8B,
|
|
127
|
+
SetTarget2: 0x20,
|
|
128
|
+
ToNumber: 0x4A,
|
|
129
|
+
ToString: 0x4B,
|
|
130
|
+
TypeOf2: 0x44,
|
|
131
|
+
Chr: 0x33,
|
|
132
|
+
Ord: 0x32,
|
|
133
|
+
MBAsciiToChar: 0x37,
|
|
134
|
+
MBCharToAscii: 0x36,
|
|
135
|
+
MBStringExtract:0x35,
|
|
136
|
+
MBStringLength: 0x31,
|
|
137
|
+
CloneSprite: 0x24,
|
|
138
|
+
RemoveSprite: 0x25,
|
|
139
|
+
StartDrag: 0x27,
|
|
140
|
+
StopDrag: 0x28,
|
|
141
|
+
WaitForFrame: 0x8A,
|
|
142
|
+
WaitForFrame2: 0x8D,
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/** Opcodes that have a UI16 length field + variable data following them */
|
|
146
|
+
export const HAS_LENGTH = new Set([
|
|
147
|
+
0x81, 0x83, 0x87, 0x88, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
|
|
148
|
+
0x94, 0x96, 0x99, 0x9A, 0x9B, 0x9D, 0x9E, 0x9F,
|
|
149
|
+
]);
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Pass 2 — rewrite AVM1 JS into cleaner browser-idiomatic form + shape comments.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { shapeFromProfile, isAvm1DisplayMemberName } from './avm1-ir.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {string} js
|
|
9
|
+
* @param {object} [opts]
|
|
10
|
+
* @param {string} [opts.label]
|
|
11
|
+
* @param {{ props: Set<string>, methods: Set<string>, assigns: Set<string> }} [opts.profile]
|
|
12
|
+
* @returns {{ js: string, shape: import('./avm1-ir.js').ClipShape|null }}
|
|
13
|
+
*/
|
|
14
|
+
export function rewriteAvm1Js(js, opts = {}) {
|
|
15
|
+
let out = js || '';
|
|
16
|
+
|
|
17
|
+
// Collapse dead temps
|
|
18
|
+
out = out.replace(/var (_t\d+) = ([^;]+);\n(\s*)\1;/g, '$3$2;');
|
|
19
|
+
|
|
20
|
+
// Pass 1 already resolves scope aliases. A generated binding such as
|
|
21
|
+
// `_this` may be a local, and quoted AVM1 names need their original spelling.
|
|
22
|
+
out = out.replace(/\barguments\.([a-zA-Z_$][\w$]*)/g, 'root.$1');
|
|
23
|
+
|
|
24
|
+
// _callMethod → direct calls (balanced [args] parsing — never slice on first `]`)
|
|
25
|
+
out = rewriteCallMethods(out);
|
|
26
|
+
|
|
27
|
+
// Keep Flash display names and their script-visible aliases distinct.
|
|
28
|
+
for (let i = 0; i < 12; i++) {
|
|
29
|
+
const next = out.replace(
|
|
30
|
+
/_member\((this|root), "([a-zA-Z_$][\w$]*)"\)/g,
|
|
31
|
+
(match, base, prop) => isAvm1DisplayMemberName(prop) ? match : `${base}.${prop}`
|
|
32
|
+
);
|
|
33
|
+
if (next === out) break;
|
|
34
|
+
out = next;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// _setMember(base, "prop", val) → base.prop = val
|
|
38
|
+
out = rewriteSetMembers(out);
|
|
39
|
+
|
|
40
|
+
// Number(x) around literals
|
|
41
|
+
out = out.replace(/(?<![\w$.])Number\((-?\d+(?:\.\d+)?)\)/g, '$1');
|
|
42
|
+
|
|
43
|
+
// Empty inverted ifs from branch polarity quirks
|
|
44
|
+
out = out.replace(/if\s*\(\s*!\s*\(\s*!([^)]+)\)\s*\)\s*\{\s*\}\s*else\s*\{/g, 'if (!($1)) {');
|
|
45
|
+
out = out.replace(/if\s*\(\s*!\s*\(\s*!([^)]+)\)\s*\)\s*\{([^}]*)\}\s*else\s*\{\s*\}/g, 'if ($1) {$2}');
|
|
46
|
+
|
|
47
|
+
// Double parens
|
|
48
|
+
out = out.replace(/\(\(([^()]+)\)\)/g, '($1)');
|
|
49
|
+
|
|
50
|
+
out = out.replace(/\n{3,}/g, '\n\n');
|
|
51
|
+
|
|
52
|
+
const shape = opts.profile ? shapeFromProfile(opts.label || 'clip', opts.profile) : null;
|
|
53
|
+
|
|
54
|
+
let header = '';
|
|
55
|
+
if (shape) {
|
|
56
|
+
const propList = shape.props.filter(p => !p.startsWith('on')).slice(0, 24).join(', ');
|
|
57
|
+
const methodList = shape.methods.slice(0, 12).join(', ');
|
|
58
|
+
header += `/* ${shape.label}`;
|
|
59
|
+
if (propList) header += `: ${propList}`;
|
|
60
|
+
if (methodList) header += ` | ${methodList}`;
|
|
61
|
+
header += ' */\n';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return { js: header + out, shape };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Rewrite `_callMethod(recv, "name", [args])` with balanced bracket parsing.
|
|
69
|
+
* @param {string} src
|
|
70
|
+
* @returns {string}
|
|
71
|
+
*/
|
|
72
|
+
function rewriteCallMethods(src) {
|
|
73
|
+
const prefix = '_callMethod(';
|
|
74
|
+
let out = '';
|
|
75
|
+
let i = 0;
|
|
76
|
+
while (i < src.length) {
|
|
77
|
+
const start = src.indexOf(prefix, i);
|
|
78
|
+
if (start < 0) {
|
|
79
|
+
out += src.slice(i);
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
out += src.slice(i, start);
|
|
83
|
+
let p = start + prefix.length;
|
|
84
|
+
|
|
85
|
+
const recvStart = p;
|
|
86
|
+
let depth = 0;
|
|
87
|
+
while (p < src.length) {
|
|
88
|
+
const c = src[p];
|
|
89
|
+
if (c === '(' || c === '[') depth++;
|
|
90
|
+
else if (c === ')' || c === ']') depth--;
|
|
91
|
+
else if (c === ',' && depth === 0) break;
|
|
92
|
+
p++;
|
|
93
|
+
}
|
|
94
|
+
const recv = src.slice(recvStart, p).trim();
|
|
95
|
+
if (src[p] !== ',') {
|
|
96
|
+
out += src[start];
|
|
97
|
+
i = start + 1;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
p++;
|
|
101
|
+
while (src[p] === ' ') p++;
|
|
102
|
+
|
|
103
|
+
let method = null;
|
|
104
|
+
if (src[p] === '"') {
|
|
105
|
+
p++;
|
|
106
|
+
const mStart = p;
|
|
107
|
+
while (p < src.length && src[p] !== '"') p++;
|
|
108
|
+
method = src.slice(mStart, p);
|
|
109
|
+
p++;
|
|
110
|
+
} else if (src.slice(p, p + 4) === 'null') {
|
|
111
|
+
method = null;
|
|
112
|
+
p += 4;
|
|
113
|
+
} else {
|
|
114
|
+
out += src.slice(start, p);
|
|
115
|
+
i = p;
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
while (src[p] === ' ') p++;
|
|
119
|
+
if (src[p] !== ',') {
|
|
120
|
+
out += src.slice(start, p);
|
|
121
|
+
i = p;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
p++;
|
|
125
|
+
while (src[p] === ' ') p++;
|
|
126
|
+
if (src[p] !== '[') {
|
|
127
|
+
out += src.slice(start, p);
|
|
128
|
+
i = p;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const argsStart = p + 1;
|
|
132
|
+
depth = 1;
|
|
133
|
+
p++;
|
|
134
|
+
while (p < src.length && depth > 0) {
|
|
135
|
+
const c = src[p];
|
|
136
|
+
if (c === '[') depth++;
|
|
137
|
+
else if (c === ']') depth--;
|
|
138
|
+
p++;
|
|
139
|
+
}
|
|
140
|
+
const args = src.slice(argsStart, p - 1).trim();
|
|
141
|
+
while (src[p] === ' ') p++;
|
|
142
|
+
if (src[p] !== ')') {
|
|
143
|
+
out += src.slice(start, p);
|
|
144
|
+
i = p;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
p++; // )
|
|
148
|
+
|
|
149
|
+
// Only unfold known-safe APIs. Game methods like KillMe may be missing —
|
|
150
|
+
// keep _callMethod so they no-op instead of throwing.
|
|
151
|
+
const SAFE_CLIP_METHODS = new Set([
|
|
152
|
+
'gotoAndStop', 'gotoAndPlay', 'stop', 'play', 'removeMovieClip',
|
|
153
|
+
'nextFrame', 'prevFrame', 'hide', 'show', 'hitTest', 'swapDepths',
|
|
154
|
+
'getDepth', 'getBytesLoaded', 'getBytesTotal', 'attachMovie',
|
|
155
|
+
'createEmptyMovieClip',
|
|
156
|
+
]);
|
|
157
|
+
const knownRecv =
|
|
158
|
+
recv === 'Math' ||
|
|
159
|
+
/^(?:this|root|_r\d+|Mouse|Key|Stage|globals)(?:\.[a-zA-Z_$][\w$]*)*$/.test(recv);
|
|
160
|
+
|
|
161
|
+
if (method == null) {
|
|
162
|
+
out += `_callMethod(${recv}, null, [${args}])`;
|
|
163
|
+
} else if (recv === 'Math' && /^[a-zA-Z_$][\w$]*$/.test(method)) {
|
|
164
|
+
out += `Math.${method}(${args})`;
|
|
165
|
+
} else if (knownRecv && SAFE_CLIP_METHODS.has(method)) {
|
|
166
|
+
// Optional chain: nested receivers like root.s_se / this.p1 are often
|
|
167
|
+
// missing until their PlaceObject frame — bare `.gotoAndPlay` throws.
|
|
168
|
+
out += `${recv.replace(/\./g, '?.')}?.${method}?.(${args})`;
|
|
169
|
+
} else {
|
|
170
|
+
out += `_callMethod(${recv}, ${JSON.stringify(method)}, [${args}])`;
|
|
171
|
+
}
|
|
172
|
+
i = p;
|
|
173
|
+
}
|
|
174
|
+
return out;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Use direct assignment only when the base is the current receiver or the root.
|
|
179
|
+
* @param {string} src
|
|
180
|
+
* @returns {string}
|
|
181
|
+
*/
|
|
182
|
+
function rewriteSetMembers(src) {
|
|
183
|
+
const prefix = '_setMember(';
|
|
184
|
+
let out = '';
|
|
185
|
+
let i = 0;
|
|
186
|
+
while (i < src.length) {
|
|
187
|
+
const start = src.indexOf(prefix, i);
|
|
188
|
+
if (start < 0) {
|
|
189
|
+
out += src.slice(i);
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
out += src.slice(i, start);
|
|
193
|
+
const baseStart = start + prefix.length;
|
|
194
|
+
const baseEnd = findTopLevelDelimiter(src, baseStart, [',', ')']);
|
|
195
|
+
if (baseEnd < 0 || src[baseEnd] !== ',') {
|
|
196
|
+
out += src.slice(start, start + prefix.length);
|
|
197
|
+
i = start + prefix.length;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
const base = src.slice(baseStart, baseEnd).trim();
|
|
201
|
+
const propStart = skipWhitespace(src, baseEnd + 1);
|
|
202
|
+
const propEnd = findTopLevelDelimiter(src, propStart, [',', ')']);
|
|
203
|
+
if (propEnd < 0 || src[propEnd] !== ',') {
|
|
204
|
+
out += src.slice(start, start + prefix.length);
|
|
205
|
+
i = start + prefix.length;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
const propExpr = src.slice(propStart, propEnd).trim();
|
|
209
|
+
let prop = null;
|
|
210
|
+
if (propExpr[0] === '"') {
|
|
211
|
+
try { prop = JSON.parse(propExpr); } catch (_) { /* retain helper call */ }
|
|
212
|
+
}
|
|
213
|
+
const valStart = skipWhitespace(src, propEnd + 1);
|
|
214
|
+
const valEnd = findTopLevelDelimiter(src, valStart, [',', ')']);
|
|
215
|
+
const hasScope = src[valEnd] === ',';
|
|
216
|
+
const close = hasScope ? findTopLevelDelimiter(src, valEnd + 1, [')']) : valEnd;
|
|
217
|
+
if (close < 0) {
|
|
218
|
+
out += src.slice(start, start + prefix.length);
|
|
219
|
+
i = start + prefix.length;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
const val = src.slice(valStart, valEnd).trim();
|
|
223
|
+
if (hasScope) {
|
|
224
|
+
out += src.slice(start, close + 1);
|
|
225
|
+
i = close + 1;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (typeof prop !== 'string') {
|
|
229
|
+
// Preserve the dynamic key while passing AVM1's current clip as the
|
|
230
|
+
// fallback target for the empty path.
|
|
231
|
+
out += `_setMember.call(this, ${base}, ${propExpr}, ${val});`;
|
|
232
|
+
i = close + 1;
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
let afterCall = close + 1;
|
|
236
|
+
if (src[afterCall] === ';') afterCall++;
|
|
237
|
+
|
|
238
|
+
if ((base === 'this' || base === 'root') && !isAvm1DisplayMemberName(prop) && /^[a-zA-Z_$][\w$]*$/.test(prop)) {
|
|
239
|
+
out += `${base}.${prop} = ${val};`;
|
|
240
|
+
} else {
|
|
241
|
+
out += `_setMember(${base}, ${JSON.stringify(prop)}, ${val}, this);`;
|
|
242
|
+
}
|
|
243
|
+
i = afterCall;
|
|
244
|
+
}
|
|
245
|
+
return out;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Find a top-level delimiter while ignoring nested expressions and strings. */
|
|
249
|
+
function findTopLevelDelimiter(src, start, delimiters) {
|
|
250
|
+
const wanted = new Set(delimiters);
|
|
251
|
+
let parens = 0;
|
|
252
|
+
let brackets = 0;
|
|
253
|
+
let braces = 0;
|
|
254
|
+
for (let i = start; i < src.length; i++) {
|
|
255
|
+
const c = src[i];
|
|
256
|
+
if (c === '"' || c === "'" || c === '`') {
|
|
257
|
+
const quote = c;
|
|
258
|
+
for (i++; i < src.length; i++) {
|
|
259
|
+
if (src[i] === '\\') { i++; continue; }
|
|
260
|
+
if (src[i] === quote) break;
|
|
261
|
+
}
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
if (c === '/' && src[i + 1] === '/') {
|
|
265
|
+
i += 2;
|
|
266
|
+
while (i < src.length && src[i] !== '\n') i++;
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
if (c === '/' && src[i + 1] === '*') {
|
|
270
|
+
const end = src.indexOf('*/', i + 2);
|
|
271
|
+
if (end < 0) return -1;
|
|
272
|
+
i = end + 1;
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
if (parens === 0 && brackets === 0 && braces === 0 && wanted.has(c)) return i;
|
|
276
|
+
if (c === '(') parens++;
|
|
277
|
+
else if (c === ')') {
|
|
278
|
+
if (parens === 0) return wanted.has(c) ? i : -1;
|
|
279
|
+
parens--;
|
|
280
|
+
} else if (c === '[') brackets++;
|
|
281
|
+
else if (c === ']') brackets--;
|
|
282
|
+
else if (c === '{') braces++;
|
|
283
|
+
else if (c === '}') braces--;
|
|
284
|
+
}
|
|
285
|
+
return -1;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function skipWhitespace(src, index) {
|
|
289
|
+
while (/\s/.test(src[index] || '')) index++;
|
|
290
|
+
return index;
|
|
291
|
+
}
|