| // Copyright 2026 The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| import {assert} from 'chai'; | |
| import {describe, it} from '../../../test/api/mocha-interface.js'; | |
| import * as Platform from '../../core/platform/platform.js'; | |
| import * as SDK from '../../core/sdk/sdk.js'; | |
| import * as Formatter from '../../models/formatter/formatter.js'; | |
| import * as SourceMapScopes from './source_map_scopes.js'; | |
| const SOURCE_MAP = { | |
| version: 3, | |
| sources: ['resolve-variable-names-origin.js'], | |
| names: [ | |
| 'foo', | |
| 'parameter1', | |
| 'parameter2', | |
| 'error', | |
| 'longObject', | |
| 'longMap', | |
| 'Map', | |
| 'set', | |
| 'get', | |
| 'testFunction', | |
| ], | |
| mappings: [ | |
| 'AAAA,QAASA,KAAIC,EAAYC,GAErB,IACI,KAAM,QACR,MAAOC,GACL,GAAIC,KACJ,', | |
| 'IAAIC,GAAU,GAAIC,IAClBD,GAAQE,IAAIN,EAAYC,EACxB,SACA,OAAOG,GAAQG,', | |
| 'IAAIJ,IAI3B,QAASK,gBAELT,IAAI,IAAK', | |
| ].join(''), | |
| }; | |
| const COMPRESSED_SCRIPT_SOURCE = [ | |
| 'function foo(o,t){try{throw"boom!"}catch(e){var n={};var r=new Map;', | |
| 'r.set(o,t);debugger;return r.get(n)}}function testFunction(){foo(100,"hello")}', | |
| ].join(''); | |
| async function stringifyScopeChain(scopeChain: SDK.DebuggerModel.ScopeChainEntry[]): Promise<string> { | |
| const lines: string[] = []; | |
| for (const scope of scopeChain) { | |
| if (scope.type() === 'global') { | |
| continue; | |
| } | |
| lines.push(scope.typeName()); | |
| const {properties} = await scope.object().getAllProperties(false, false); | |
| for (const prop of properties ?? []) { | |
| if (prop.value?.type === 'number') { | |
| lines.push(` ${prop.name}: ${prop.value.value}`); | |
| } | |
| } | |
| } | |
| return lines.join('\n'); | |
| } | |
| describe('ScopeChainResolver API Test', () => { | |
| // Regression test for crbug.com/583942448. | |
| it('provides up-to-date variable values after a console evaluation modified them while paused', | |
| async ({inspectedPage, universe}) => { | |
| const primaryTarget = universe.targetManager.primaryPageTarget(); | |
| assert.isNotNull(primaryTarget); | |
| const debuggerModel = primaryTarget.model(SDK.DebuggerModel.DebuggerModel); | |
| assert.isNotNull(debuggerModel); | |
| const consoleModel = primaryTarget.model(SDK.ConsoleModel.ConsoleModel); | |
| assert.isNotNull(consoleModel); | |
| // Load an empty page first so navigation does not block on the debugger statement. | |
| await inspectedPage.goToHtml('<!DOCTYPE html><html><body></body></html>'); | |
| const paused = debuggerModel.once(SDK.DebuggerModel.Events.DebuggerPaused); | |
| void inspectedPage.evaluate(() => { | |
| const script = document.createElement('script'); | |
| script.textContent = ` | |
| let scriptVar = 1; | |
| function outer() { | |
| let closureVar = 2; | |
| function inner() { | |
| let localVar = 3; | |
| let untouchedVar = 4; | |
| debugger; | |
| return closureVar + localVar + untouchedVar + scriptVar; | |
| } | |
| return inner(); | |
| } | |
| setTimeout(outer, 0);`; | |
| document.body.appendChild(script); | |
| }); | |
| await paused; | |
| try { | |
| const callFrame = debuggerModel.debuggerPausedDetails()?.callFrames[0]; | |
| assert.exists(callFrame); | |
| const resolver = universe.scopeChainResolver; | |
| assert.strictEqual(await stringifyScopeChain(await resolver.resolveScopeChain(callFrame)), [ | |
| 'Local', | |
| ' localVar: 3', | |
| ' untouchedVar: 4', | |
| 'Closure', | |
| ' closureVar: 2', | |
| 'Script', | |
| ' scriptVar: 1', | |
| ].join('\n')); | |
| const executionContext = debuggerModel.runtimeModel().executionContext(callFrame.script.executionContextId); | |
| assert.exists(executionContext); | |
| const invalidated: SDK.Script.Script[] = []; | |
| resolver.addEventListener(SourceMapScopes.ScopeChainResolver.Events.SCOPE_CHAIN_INVALIDATED, | |
| event => invalidated.push(event.data)); | |
| const expression = 'localVar = 30; closureVar = 20; scriptVar = 10;'; | |
| await consoleModel.evaluateCommandInConsole( | |
| executionContext, consoleModel.addCommandMessage(executionContext, expression), expression, | |
| /* useCommandLineAPI */ false); | |
| assert.deepEqual(invalidated, [callFrame.script]); | |
| assert.strictEqual(await stringifyScopeChain(await resolver.resolveScopeChain(callFrame)), [ | |
| 'Local', | |
| ' localVar: 30', | |
| ' untouchedVar: 4', | |
| 'Closure', | |
| ' closureVar: 20', | |
| 'Script', | |
| ' scriptVar: 10', | |
| ].join('\n')); | |
| } finally { | |
| if (debuggerModel.isPaused()) { | |
| await debuggerModel.resume(); | |
| } | |
| } | |
| }); | |
| it('resolves minified variable names in catch and local scopes from a source map', | |
| async ({inspectedPage, universe}) => { | |
| const primaryTarget = universe.targetManager.primaryPageTarget(); | |
| assert.isNotNull(primaryTarget); | |
| const debuggerModel = primaryTarget.model(SDK.DebuggerModel.DebuggerModel); | |
| assert.isNotNull(debuggerModel); | |
| await inspectedPage.goToHtml('<!DOCTYPE html><html><body></body></html>'); | |
| await debuggerModel.syncDebuggerId(); | |
| const sourceMapAttached = debuggerModel.sourceMapManager().once(SDK.SourceMapManager.Events.SourceMapAttached); | |
| const sourceMapDataUrl = `data:application/json;charset=utf-8;base64,${ | |
| Platform.StringUtilities.toBase64(JSON.stringify(SOURCE_MAP))}`; | |
| const scriptContent = [ | |
| COMPRESSED_SCRIPT_SOURCE, | |
| '//# sourceURL=resolve-variable-names-compressed.js', | |
| `//# sourceMappingURL=${sourceMapDataUrl}`, | |
| ].join('\n'); | |
| await inspectedPage.evaluate(code => { | |
| const script = document.createElement('script'); | |
| script.textContent = code; | |
| document.body.appendChild(script); | |
| }, scriptContent); | |
| const {sourceMap} = await sourceMapAttached; | |
| assert.isTrue(sourceMap.sourceURLs().some(url => url.endsWith('resolve-variable-names-origin.js'))); | |
| const paused = debuggerModel.once(SDK.DebuggerModel.Events.DebuggerPaused); | |
| void inspectedPage.evaluate(() => { | |
| setTimeout(() => { | |
| (window as unknown as {testFunction: () => void}).testFunction(); | |
| }, 0); | |
| }); | |
| await paused; | |
| try { | |
| const callFrame = debuggerModel.debuggerPausedDetails()?.callFrames[0]; | |
| assert.exists(callFrame); | |
| const scopeChain = await universe.scopeChainResolver.resolveScopeChain(callFrame); | |
| assert.deepEqual(scopeChain.map(scope => `${scope.typeName()}${scope.description()}`), [ | |
| 'Catch block', | |
| 'Local', | |
| 'GlobalWindow', | |
| ]); | |
| const [catchScope, localScope, globalScope] = scopeChain; | |
| assert.strictEqual(catchScope.type(), 'catch'); | |
| assert.isEmpty(catchScope.extraProperties()); | |
| const {properties: catchProperties} = await catchScope.object().getAllProperties(false, true); | |
| assert.deepEqual( | |
| catchProperties?.map(prop => ({name: prop.name, type: prop.value?.type, value: prop.value?.value})), | |
| [{name: 'error', type: 'string', value: 'boom!'}]); | |
| assert.strictEqual(localScope.type(), 'local'); | |
| assert.deepEqual(localScope.extraProperties().map(prop => ({ | |
| name: prop.name, | |
| synthetic: prop.synthetic, | |
| type: prop.value?.type, | |
| description: prop.value?.description, | |
| })), | |
| [{name: 'this', synthetic: true, type: 'object', description: 'Window'}]); | |
| const {properties: localProperties} = await localScope.object().getAllProperties(false, true); | |
| const byName = new Map((localProperties ?? []).map(prop => [prop.name, prop])); | |
| assert.deepEqual([...byName.keys()].sort(), ['longMap', 'longObject', 'parameter1', 'parameter2']); | |
| assert.strictEqual(byName.get('parameter1')?.value?.type, 'number'); | |
| assert.strictEqual(byName.get('parameter1')?.value?.value, 100); | |
| assert.strictEqual(byName.get('parameter2')?.value?.type, 'string'); | |
| assert.strictEqual(byName.get('parameter2')?.value?.value, 'hello'); | |
| const longObject = byName.get('longObject')?.value; | |
| assert.exists(longObject); | |
| assert.strictEqual(longObject.type, 'object'); | |
| assert.strictEqual(longObject.className, 'Object'); | |
| assert.strictEqual(longObject.description, 'Object'); | |
| const {properties: longObjectProps} = await longObject.getOwnProperties(false); | |
| assert.deepEqual(longObjectProps, []); | |
| const longMap = byName.get('longMap')?.value; | |
| assert.exists(longMap); | |
| assert.strictEqual(longMap.type, 'object'); | |
| assert.strictEqual(longMap.subtype, 'map'); | |
| assert.strictEqual(longMap.className, 'Map'); | |
| assert.strictEqual(longMap.description, 'Map(1)'); | |
| assert.deepEqual(longMap.preview?.entries?.map(entry => ({ | |
| key: { | |
| type: String(entry.key?.type), | |
| description: entry.key?.description, | |
| }, | |
| value: { | |
| type: String(entry.value?.type), | |
| description: entry.value?.description, | |
| }, | |
| })), | |
| [{key: {type: 'number', description: '100'}, value: {type: 'string', description: 'hello'}}]); | |
| assert.strictEqual(globalScope.type(), 'global'); | |
| assert.strictEqual(globalScope.typeName(), 'Global'); | |
| assert.strictEqual(globalScope.description(), 'Window'); | |
| assert.strictEqual(globalScope.object().type, 'object'); | |
| assert.strictEqual(globalScope.object().description, 'Window'); | |
| } finally { | |
| if (debuggerModel.isPaused()) { | |
| await debuggerModel.resume(); | |
| } | |
| Formatter.FormatterWorkerPool.FormatterWorkerPool.removeInstance(); | |
| } | |
| }); | |
| }); |