From 6b33febb12fa681e4f4a89ec5b71a7828f151779 Mon Sep 17 00:00:00 2001 From: Ryan Day Date: Tue, 28 Aug 2018 09:31:23 -0700 Subject: [PATCH] fix: move vm.runInDebugContext to constructor (#509) 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 GoogleCloudPlatform/cloud-debug-nodejs/issues/503 --- package.json | 1 + src/agent/state/legacy-state.ts | 16 ++++++++-------- test/test-state.ts | 8 ++++++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 5d39ebd9..56916713 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/agent/state/legacy-state.ts b/src/agent/state/legacy-state.ts index 61aa6898..6097cda6 100644 --- a/src/agent/state/legacy-state.ts +++ b/src/agent/state/legacy-state.ts @@ -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. @@ -79,7 +77,7 @@ class StateResolver { private messageTable: stackdriver.Variable[]; private resolvedVariableTable: stackdriver.Variable[]; private rawVariableTable: Array; - + private scopeType: {Global: {}, Script: {}, Closure: {}, Local: {}}; /** * @param {!Object} execState * @param {Array} expressions @@ -127,6 +125,8 @@ class StateResolver { this.rawVariableTable = this.messageTable.map(() => { return null; }); + + this.scopeType = vm.runInDebugContext('ScopeType'); } @@ -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); } diff --git a/test/test-state.ts b/test/test-state.ts index 817d97c4..8f63cc02 100644 --- a/test/test-state.ts +++ b/test/test-state.ts @@ -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); + }); });