Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
fix: move vm.runInDebugContext to constructor (#509)
Browse files Browse the repository at this point in the history
to support bundlers, and more closely match the pattern of other code in
the project we create the debug context in the constructor rather than
in the file.

fixes /issues/503
  • Loading branch information
soldair committed Aug 28, 2018
1 parent 9dec439 commit 6b33feb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -57,6 +57,7 @@
"once": "^1.4.0",
"post-install-check": "0.0.1",
"proxyquire": "^2.0.0",
"require-inject": "^1.4.3",
"rimraf": "^2.6.2",
"source-map-support": "^0.5.6",
"tmp": "0.0.33",
Expand Down
16 changes: 8 additions & 8 deletions src/agent/state/legacy-state.ts
Expand Up @@ -29,9 +29,7 @@ import * as v8 from '../../types/v8';
import * as stackdriver from '../../types/stackdriver';
import {ResolvedDebugAgentConfig} from '../config';

// TODO: Determine if `ScopeType` should be named `scopeType`.
// tslint:disable-next-line:variable-name
const ScopeType = vm.runInDebugContext('ScopeType');

const assert = debugAssert(!!process.env.CLOUD_DEBUG_ASSERTIONS);

// Error message indices into the resolved variable table.
Expand Down Expand Up @@ -79,7 +77,7 @@ class StateResolver {
private messageTable: stackdriver.Variable[];
private resolvedVariableTable: stackdriver.Variable[];
private rawVariableTable: Array<v8.ValueMirror|null>;

private scopeType: {Global: {}, Script: {}, Closure: {}, Local: {}};
/**
* @param {!Object} execState
* @param {Array<string>} expressions
Expand Down Expand Up @@ -127,6 +125,8 @@ class StateResolver {
this.rawVariableTable = this.messageTable.map(() => {
return null;
});

this.scopeType = vm.runInDebugContext('ScopeType');
}


Expand Down Expand Up @@ -382,17 +382,17 @@ class StateResolver {
// For top-level breakpoints: [local, script, global]
// Other: [..., closure (module IIFE), script, global]
assert.ok(count >= 3);
assert.strictEqual(allScopes[count - 1].scopeType(), ScopeType.Global);
assert.strictEqual(allScopes[count - 2].scopeType(), ScopeType.Script);
assert.strictEqual(allScopes[count - 1].scopeType(), self.scopeType.Global);
assert.strictEqual(allScopes[count - 2].scopeType(), self.scopeType.Script);

// We find the top-level (module global) variable pollute the local
// variables we omit them by default, unless the breakpoint itself is
// top-level. The last two scopes are always omitted.
let scopes: v8.ScopeMirror[];
if (allScopes[count - 3].scopeType() === ScopeType.Closure) {
if (allScopes[count - 3].scopeType() === self.scopeType.Closure) {
scopes = allScopes.slice(0, -3);
} else {
assert.ok(allScopes[count - 3].scopeType() === ScopeType.Local);
assert.ok(allScopes[count - 3].scopeType() === self.scopeType.Local);
scopes = allScopes.slice(0, -2);
}

Expand Down
8 changes: 8 additions & 0 deletions test/test-state.ts
Expand Up @@ -39,4 +39,12 @@ describeFn('state', () => {
state.testAssert();
});
});

it('should not throw if vm is not an object', () => {
// test for
// https://github.com/GoogleCloudPlatform/cloud-debug-nodejs/issues/503
const inject = require('require-inject');
const state = inject('../src/agent/state/legacy-state', {vm: false});
assert.ok(state);
});
});

0 comments on commit 6b33feb

Please sign in to comment.