-
Notifications
You must be signed in to change notification settings - Fork 450
/
Script.ts
484 lines (434 loc) · 19.7 KB
/
Script.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*
* Copyright (C) 2008 Apple Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import * as Protocol from '../../generated/protocol.js';
import * as TextUtils from '../../models/text_utils/text_utils.js';
import type * as Platform from '../../core/platform/platform.js';
import * as Common from '../common/common.js';
import * as i18n from '../i18n/i18n.js';
import {
Location,
type DebuggerModel,
COND_BREAKPOINT_SOURCE_URL,
LOGPOINT_SOURCE_URL,
Events,
} from './DebuggerModel.js';
import {type FrameAssociated} from './FrameAssociated.js';
import {type PageResourceLoadInitiator} from './PageResourceLoader.js';
import {ResourceTreeModel} from './ResourceTreeModel.js';
import {type ExecutionContext} from './RuntimeModel.js';
import {type Target} from './Target.js';
const UIStrings = {
/**
*@description Error message for when a script can't be loaded which had been previously
*/
scriptRemovedOrDeleted: 'Script removed or deleted.',
/**
*@description Error message when failing to load a script source text
*/
unableToFetchScriptSource: 'Unable to fetch script source.',
};
const str_ = i18n.i18n.registerUIStrings('core/sdk/Script.ts', UIStrings);
const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
let scriptCacheInstance: {
cache: Map<string, WeakRef<Promise<TextUtils.ContentProvider.DeferredContent>>>,
registry: FinalizationRegistry<string>,
}|null = null;
export class Script implements TextUtils.ContentProvider.ContentProvider, FrameAssociated {
debuggerModel: DebuggerModel;
scriptId: Protocol.Runtime.ScriptId;
sourceURL: Platform.DevToolsPath.UrlString;
lineOffset: number;
columnOffset: number;
endLine: number;
endColumn: number;
executionContextId: number;
hash: string;
readonly #isContentScriptInternal: boolean;
readonly #isLiveEditInternal: boolean;
sourceMapURL?: string;
debugSymbols: Protocol.Debugger.DebugSymbols|null;
hasSourceURL: boolean;
contentLength: number;
originStackTrace: Protocol.Runtime.StackTrace|null;
readonly #codeOffsetInternal: number|null;
readonly #language: string|null;
#contentPromise: Promise<TextUtils.ContentProvider.DeferredContent>|null;
readonly #embedderNameInternal: Platform.DevToolsPath.UrlString|null;
readonly isModule: boolean|null;
constructor(
debuggerModel: DebuggerModel, scriptId: Protocol.Runtime.ScriptId, sourceURL: Platform.DevToolsPath.UrlString,
startLine: number, startColumn: number, endLine: number, endColumn: number, executionContextId: number,
hash: string, isContentScript: boolean, isLiveEdit: boolean, sourceMapURL: string|undefined,
hasSourceURL: boolean, length: number, isModule: boolean|null, originStackTrace: Protocol.Runtime.StackTrace|null,
codeOffset: number|null, scriptLanguage: string|null, debugSymbols: Protocol.Debugger.DebugSymbols|null,
embedderName: Platform.DevToolsPath.UrlString|null) {
this.debuggerModel = debuggerModel;
this.scriptId = scriptId;
this.sourceURL = sourceURL;
this.lineOffset = startLine;
this.columnOffset = startColumn;
this.endLine = endLine;
this.endColumn = endColumn;
this.isModule = isModule;
this.executionContextId = executionContextId;
this.hash = hash;
this.#isContentScriptInternal = isContentScript;
this.#isLiveEditInternal = isLiveEdit;
this.sourceMapURL = sourceMapURL;
this.debugSymbols = debugSymbols;
this.hasSourceURL = hasSourceURL;
this.contentLength = length;
this.originStackTrace = originStackTrace;
this.#codeOffsetInternal = codeOffset;
this.#language = scriptLanguage;
this.#contentPromise = null;
this.#embedderNameInternal = embedderName;
}
embedderName(): Platform.DevToolsPath.UrlString|null {
return this.#embedderNameInternal;
}
target(): Target {
return this.debuggerModel.target();
}
private static trimSourceURLComment(source: string): string {
let sourceURLIndex = source.lastIndexOf('//# sourceURL=');
if (sourceURLIndex === -1) {
sourceURLIndex = source.lastIndexOf('//@ sourceURL=');
if (sourceURLIndex === -1) {
return source;
}
}
const sourceURLLineIndex = source.lastIndexOf('\n', sourceURLIndex);
if (sourceURLLineIndex === -1) {
return source;
}
const sourceURLLine = source.substr(sourceURLLineIndex + 1);
if (!sourceURLLine.match(sourceURLRegex)) {
return source;
}
return source.substr(0, sourceURLLineIndex);
}
isContentScript(): boolean {
return this.#isContentScriptInternal;
}
codeOffset(): number|null {
return this.#codeOffsetInternal;
}
isJavaScript(): boolean {
return this.#language === Protocol.Debugger.ScriptLanguage.JavaScript;
}
isWasm(): boolean {
return this.#language === Protocol.Debugger.ScriptLanguage.WebAssembly;
}
scriptLanguage(): string|null {
return this.#language;
}
executionContext(): ExecutionContext|null {
return this.debuggerModel.runtimeModel().executionContext(this.executionContextId);
}
isLiveEdit(): boolean {
return this.#isLiveEditInternal;
}
contentURL(): Platform.DevToolsPath.UrlString {
return this.sourceURL;
}
contentType(): Common.ResourceType.ResourceType {
return Common.ResourceType.resourceTypes.Script;
}
private async loadTextContent(): Promise<TextUtils.ContentProvider.DeferredContent> {
const result = await this.debuggerModel.target().debuggerAgent().invoke_getScriptSource({scriptId: this.scriptId});
if (result.getError()) {
throw new Error(result.getError());
}
const {scriptSource, bytecode} = result;
if (bytecode) {
return {content: bytecode, isEncoded: true};
}
let content: string = scriptSource || '';
if (this.hasSourceURL && Common.ParsedURL.schemeIs(this.sourceURL, 'snippet:')) {
// TODO(crbug.com/1330846): Find a better way to establish the snippet automapping binding then adding
// a sourceURL comment before evaluation and removing it here.
content = Script.trimSourceURLComment(content);
}
return {content, isEncoded: false};
}
private async loadWasmContent(): Promise<TextUtils.ContentProvider.DeferredContent> {
if (!this.isWasm()) {
throw new Error('Not a wasm script');
}
const result =
await this.debuggerModel.target().debuggerAgent().invoke_disassembleWasmModule({scriptId: this.scriptId});
if (result.getError()) {
// Fall through to text content loading if v8-based disassembly fails. This is to ensure backwards compatibility with
// older v8 versions;
return this.loadTextContent();
}
const {streamId, functionBodyOffsets, chunk: {lines, bytecodeOffsets}} = result;
const lineChunks = [];
const bytecodeOffsetChunks = [];
let totalLength = lines.reduce<number>((sum, line) => sum + line.length + 1, 0);
const truncationMessage = '<truncated>';
// This is a magic number used in code mirror which, when exceeded, sends it into an infinite loop.
const cmSizeLimit = 1000000000 - truncationMessage.length;
if (streamId) {
while (true) {
const result = await this.debuggerModel.target().debuggerAgent().invoke_nextWasmDisassemblyChunk({streamId});
if (result.getError()) {
throw new Error(result.getError());
}
const {chunk: {lines: linesChunk, bytecodeOffsets: bytecodeOffsetsChunk}} = result;
totalLength += linesChunk.reduce<number>((sum, line) => sum + line.length + 1, 0);
if (linesChunk.length === 0) {
break;
}
if (totalLength >= cmSizeLimit) {
lineChunks.push([truncationMessage]);
bytecodeOffsetChunks.push([0]);
break;
}
lineChunks.push(linesChunk);
bytecodeOffsetChunks.push(bytecodeOffsetsChunk);
}
}
const functionBodyRanges: Array<{start: number, end: number}> = [];
// functionBodyOffsets contains a sequence of pairs of start and end offsets
for (let i = 0; i < functionBodyOffsets.length; i += 2) {
functionBodyRanges.push({start: functionBodyOffsets[i], end: functionBodyOffsets[i + 1]});
}
const wasmDisassemblyInfo = new Common.WasmDisassembly.WasmDisassembly(
lines.concat(...lineChunks), bytecodeOffsets.concat(...bytecodeOffsetChunks), functionBodyRanges);
return {content: '', isEncoded: false, wasmDisassemblyInfo};
}
requestContent(): Promise<TextUtils.ContentProvider.DeferredContent> {
if (!this.#contentPromise) {
const fileSizeToCache = 65535; // We won't bother cacheing files under 64K
if (this.hash && !this.#isLiveEditInternal && this.contentLength > fileSizeToCache) {
// For large files that aren't live edits and have a hash, we keep a content-addressed cache
// so we don't need to load multiple copies or disassemble wasm modules multiple times.
if (!scriptCacheInstance) {
// Initialize script cache singleton. Add a finalizer for removing keys from the map.
scriptCacheInstance = {
cache: new Map(),
registry: new FinalizationRegistry(hashCode => scriptCacheInstance?.cache.delete(hashCode)),
};
}
// This key should be sufficient to identify scripts that are known to have the same content.
const fullHash = [
this.#language,
this.contentLength,
this.lineOffset,
this.columnOffset,
this.endLine,
this.endColumn,
this.#codeOffsetInternal,
this.hash,
].join(':');
const cachedContentPromise = scriptCacheInstance.cache.get(fullHash)?.deref();
if (cachedContentPromise) {
this.#contentPromise = cachedContentPromise;
} else {
this.#contentPromise = this.requestContentInternal();
scriptCacheInstance.cache.set(fullHash, new WeakRef(this.#contentPromise));
scriptCacheInstance.registry.register(this.#contentPromise, fullHash);
}
} else {
this.#contentPromise = this.requestContentInternal();
}
}
return this.#contentPromise;
}
private async requestContentInternal(): Promise<TextUtils.ContentProvider.DeferredContent> {
if (!this.scriptId) {
return {content: null, error: i18nString(UIStrings.scriptRemovedOrDeleted), isEncoded: false};
}
try {
return this.isWasm() ? await this.loadWasmContent() : await this.loadTextContent();
} catch (err) {
// TODO(bmeurer): Propagate errors as exceptions / rejections.
return {content: null, error: i18nString(UIStrings.unableToFetchScriptSource), isEncoded: false};
}
}
async getWasmBytecode(): Promise<ArrayBuffer> {
const base64 = await this.debuggerModel.target().debuggerAgent().invoke_getWasmBytecode({scriptId: this.scriptId});
const response = await fetch(`data:application/wasm;base64,${base64.bytecode}`);
return response.arrayBuffer();
}
originalContentProvider(): TextUtils.ContentProvider.ContentProvider {
return new TextUtils.StaticContentProvider.StaticContentProvider(
this.contentURL(), this.contentType(), () => this.requestContent());
}
async searchInContent(query: string, caseSensitive: boolean, isRegex: boolean):
Promise<TextUtils.ContentProvider.SearchMatch[]> {
if (!this.scriptId) {
return [];
}
const matches = await this.debuggerModel.target().debuggerAgent().invoke_searchInContent(
{scriptId: this.scriptId, query, caseSensitive, isRegex});
return TextUtils.TextUtils.performSearchInSearchMatches(matches.result || [], query, caseSensitive, isRegex);
}
private appendSourceURLCommentIfNeeded(source: string): string {
if (!this.hasSourceURL) {
return source;
}
return source + '\n //# sourceURL=' + this.sourceURL;
}
async editSource(newSource: string): Promise<{
changed: boolean,
status: Protocol.Debugger.SetScriptSourceResponseStatus,
exceptionDetails?: Protocol.Runtime.ExceptionDetails,
}> {
newSource = Script.trimSourceURLComment(newSource);
// We append correct #sourceURL to script for consistency only. It's not actually needed for things to work correctly.
newSource = this.appendSourceURLCommentIfNeeded(newSource);
const {content: oldSource} = await this.requestContent();
if (oldSource === newSource) {
return {changed: false, status: Protocol.Debugger.SetScriptSourceResponseStatus.Ok};
}
const response = await this.debuggerModel.target().debuggerAgent().invoke_setScriptSource(
{scriptId: this.scriptId, scriptSource: newSource, allowTopFrameEditing: true});
if (response.getError()) {
// Something went seriously wrong, like the V8 inspector no longer knowing about this script without
// shutting down the Debugger agent etc.
throw new Error(`Script#editSource failed for script with id ${this.scriptId}: ${response.getError()}`);
}
if (!response.getError() && response.status === Protocol.Debugger.SetScriptSourceResponseStatus.Ok) {
this.#contentPromise = Promise.resolve({content: newSource, isEncoded: false});
}
this.debuggerModel.dispatchEventToListeners(Events.ScriptSourceWasEdited, {script: this, status: response.status});
return {changed: true, status: response.status, exceptionDetails: response.exceptionDetails};
}
rawLocation(lineNumber: number, columnNumber: number): Location|null {
if (this.containsLocation(lineNumber, columnNumber)) {
return new Location(this.debuggerModel, this.scriptId, lineNumber, columnNumber);
}
return null;
}
isInlineScript(): boolean {
const startsAtZero = !this.lineOffset && !this.columnOffset;
return !this.isWasm() && Boolean(this.sourceURL) && !startsAtZero;
}
isAnonymousScript(): boolean {
return !this.sourceURL;
}
async setBlackboxedRanges(positions: Protocol.Debugger.ScriptPosition[]): Promise<boolean> {
const response = await this.debuggerModel.target().debuggerAgent().invoke_setBlackboxedRanges(
{scriptId: this.scriptId, positions});
return !response.getError();
}
containsLocation(lineNumber: number, columnNumber: number): boolean {
const afterStart =
(lineNumber === this.lineOffset && columnNumber >= this.columnOffset) || lineNumber > this.lineOffset;
const beforeEnd = lineNumber < this.endLine || (lineNumber === this.endLine && columnNumber <= this.endColumn);
return afterStart && beforeEnd;
}
get frameId(): Protocol.Page.FrameId {
// TODO(crbug.com/1172300) Ignored during the jsdoc to ts migration
// @ts-expect-error
if (typeof this[frameIdSymbol] !== 'string') {
// TODO(crbug.com/1172300) Ignored during the jsdoc to ts migration
// @ts-expect-error
this[frameIdSymbol] = frameIdForScript(this);
}
// TODO(crbug.com/1172300) Ignored during the jsdoc to ts migration
// @ts-expect-error
return this[frameIdSymbol];
}
/**
* @returns true, iff this script originates from a breakpoint/logpoint condition
*/
get isBreakpointCondition(): boolean {
return [COND_BREAKPOINT_SOURCE_URL, LOGPOINT_SOURCE_URL].includes(this.sourceURL);
}
createPageResourceLoadInitiator(): PageResourceLoadInitiator {
return {target: this.target(), frameId: this.frameId, initiatorUrl: this.embedderName()};
}
/**
* Translates the `rawLocation` from line and column number in terms of what V8 understands
* to a script relative location. Specifically this means that for inline `<script>`'s
* without a `//# sourceURL=` annotation, the line and column offset of the script
* content is subtracted to make the location within the script independent of the
* location of the `<script>` tag within the surrounding document.
*
* @param rawLocation the raw location in terms of what V8 understands.
* @returns the script relative line and column number for the {@link rawLocation}.
*/
rawLocationToRelativeLocation(rawLocation: {lineNumber: number, columnNumber: number}):
{lineNumber: number, columnNumber: number};
rawLocationToRelativeLocation(rawLocation: {lineNumber: number, columnNumber: number|undefined}):
{lineNumber: number, columnNumber: number|undefined};
rawLocationToRelativeLocation(rawLocation: {lineNumber: number, columnNumber: number|undefined}):
{lineNumber: number, columnNumber: number|undefined} {
let {lineNumber, columnNumber} = rawLocation;
if (!this.hasSourceURL && this.isInlineScript()) {
lineNumber -= this.lineOffset;
if (lineNumber === 0 && columnNumber !== undefined) {
columnNumber -= this.columnOffset;
}
}
return {lineNumber, columnNumber};
}
/**
* Translates the `relativeLocation` from script relative line and column number to
* the raw location in terms of what V8 understands. Specifically this means that for
* inline `<script>`'s without a `//# sourceURL=` annotation, the line and column offset
* of the script content is added to make the location relative to the start of the
* surrounding document.
*
* @param relativeLocation the script relative location.
* @returns the raw location in terms of what V8 understands for the {@link relativeLocation}.
*/
relativeLocationToRawLocation(relativeLocation: {lineNumber: number, columnNumber: number}):
{lineNumber: number, columnNumber: number};
relativeLocationToRawLocation(relativeLocation: {lineNumber: number, columnNumber: number|undefined}):
{lineNumber: number, columnNumber: number|undefined};
relativeLocationToRawLocation(relativeLocation: {lineNumber: number, columnNumber: number|undefined}):
{lineNumber: number, columnNumber: number|undefined} {
let {lineNumber, columnNumber} = relativeLocation;
if (!this.hasSourceURL && this.isInlineScript()) {
if (lineNumber === 0 && columnNumber !== undefined) {
columnNumber += this.columnOffset;
}
lineNumber += this.lineOffset;
}
return {lineNumber, columnNumber};
}
}
const frameIdSymbol = Symbol('frameid');
function frameIdForScript(script: Script): Protocol.Page.FrameId|null {
const executionContext = script.executionContext();
if (executionContext) {
return executionContext.frameId || null;
}
// This is to overcome compilation cache which doesn't get reset.
const resourceTreeModel = script.debuggerModel.target().model(ResourceTreeModel);
if (!resourceTreeModel || !resourceTreeModel.mainFrame) {
return null;
}
return resourceTreeModel.mainFrame.id;
}
export const sourceURLRegex = /^[\040\t]*\/\/[@#] sourceURL=\s*(\S*?)\s*$/;