-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
v8.ts
287 lines (243 loc) 路 13.1 KB
/
v8.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
// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This file provides a low-level interface to a few V8 runtime objects. We will use this low-level
// interface when serializing closures to walk the scope chain and find the value of free variables
// captured by closures, as well as getting source-level debug information so that we can present
// high-quality error messages.
//
// As a side-effect of importing this file, we must enable the --allow-natives-syntax V8 flag. This
// is because we are using V8 intrinsics in order to implement this module.
import * as v8 from "v8";
v8.setFlagsFromString("--allow-natives-syntax");
import * as inspector from "inspector";
import * as util from "util";
import * as vm from "vm";
import * as v8Hooks from "./v8Hooks";
/**
* Given a function, returns the file, line and column number in the file where this function was
* defined. Returns { "", 0, 0 } if the location cannot be found or if the given function has no Script.
* @internal
*/
export async function getFunctionLocationAsync(func: Function) {
// First, find the runtime's internal id for this function.
const functionId = await getRuntimeIdForFunctionAsync(func);
// Now, query for the internal properties the runtime sets up for it.
const { internalProperties } = await runtimeGetPropertiesAsync(functionId, /*ownProperties:*/ false);
// There should normally be an internal property called [[FunctionLocation]]:
// https://chromium.googlesource.com/v8/v8.git/+/3f99afc93c9ba1ba5df19f123b93cc3079893c9b/src/inspector/v8-debugger.cc#793
const functionLocation = internalProperties.find(p => p.name === "[[FunctionLocation]]");
if (!functionLocation || !functionLocation.value || !functionLocation.value.value) {
return { file: "", line: 0, column: 0 };
}
const value = functionLocation.value.value;
// Map from the scriptId the value has to a file-url.
const file = v8Hooks.getScriptUrl(value.scriptId) || "";
const line = value.lineNumber || 0;
const column = value.columnNumber || 0;
return { file, line, column };
}
/**
* Given a function and a free variable name, lookupCapturedVariableValue looks up the value of that free variable
* in the scope chain of the provided function. If the free variable is not found, `throwOnFailure` indicates
* whether or not this function should throw or return `undefined.
*
* @param func The function whose scope chain is to be analyzed
* @param freeVariable The name of the free variable to inspect
* @param throwOnFailure If true, throws if the free variable can't be found.
* @returns The value of the free variable. If `throwOnFailure` is false, returns `undefined` if not found.
* @internal
*/
export async function lookupCapturedVariableValueAsync(
func: Function, freeVariable: string, throwOnFailure: boolean): Promise<any> {
// First, find the runtime's internal id for this function.
const functionId = await getRuntimeIdForFunctionAsync(func);
// Now, query for the internal properties the runtime sets up for it.
const { internalProperties } = await runtimeGetPropertiesAsync(functionId, /*ownProperties:*/ false);
// There should normally be an internal property called [[Scopes]]:
// https://chromium.googlesource.com/v8/v8.git/+/3f99afc93c9ba1ba5df19f123b93cc3079893c9b/src/inspector/v8-debugger.cc#820
const scopes = internalProperties.find(p => p.name === "[[Scopes]]");
if (!scopes) {
throw new Error("Could not find [[Scopes]] property");
}
if (!scopes.value) {
throw new Error("[[Scopes]] property did not have [value]");
}
if (!scopes.value.objectId) {
throw new Error("[[Scopes]].value have objectId");
}
// This is sneaky, but we can actually map back from the [[Scopes]] object to a real in-memory
// v8 array-like value. Note: this isn't actually a real array. For example, it cannot be
// iterated. Nor can any actual methods be called on it. However, we can directly index into
// it, and we can. Similarly, the 'object' type it optionally points at is not a true JS
// object. So we can't call things like .hasOwnProperty on it. However, the values pointed to
// by 'object' are the real in-memory JS objects we are looking for. So we can find and return
// those successfully to our caller.
const scopesArray: { object?: Record<string, any> }[] = await getValueForObjectId(scopes.value.objectId);
// scopesArray is ordered from innermost to outermost.
for (let i = 0, n = scopesArray.length; i < n; i++) {
const scope = scopesArray[i];
if (scope.object) {
if (freeVariable in scope.object) {
const val = scope.object[freeVariable];
return val;
}
}
}
if (throwOnFailure) {
throw new Error("Unexpected missing variable in closure environment: " + freeVariable);
}
return undefined;
}
// We want to call util.promisify on inspector.Session.post. However, due to all the overloads of
// that method, promisify gets confused. To prevent this, we cast our session object down to an
// interface containing only the single overload we care about.
type PostSession<TMethod, TParams, TReturn> = {
post(method: TMethod, params?: TParams, callback?: (err: Error | null, params: TReturn) => void): void;
};
type EvaluationSession = PostSession<"Runtime.evaluate", inspector.Runtime.EvaluateParameterType, inspector.Runtime.EvaluateReturnType>;
type GetPropertiesSession = PostSession<"Runtime.getProperties", inspector.Runtime.GetPropertiesParameterType, inspector.Runtime.GetPropertiesReturnType>;
type CallFunctionSession = PostSession<"Runtime.callFunctionOn", inspector.Runtime.CallFunctionOnParameterType, inspector.Runtime.CallFunctionOnReturnType>;
type ContextSession = {
post(method: "Runtime.disable" | "Runtime.enable", callback?: (err: Error | null) => void): void;
once(event: "Runtime.executionContextCreated", listener: (message: inspector.InspectorNotification<inspector.Runtime.ExecutionContextCreatedEventDataType>) => void): void;
};
type InflightContext = {
contextId: number;
functions: Record<string, any>;
currentFunctionId: number;
calls: Record<string, any>;
currentCallId: number;
};
// Isolated singleton context accessible from the inspector.
// Used instead of `global` object to support executions with multiple V8 vm contexts as, e.g., done by Jest.
let inflightContextCache: Promise<InflightContext> | undefined;
function inflightContext() {
if (inflightContextCache) {
return inflightContextCache;
}
inflightContextCache = createContext();
return inflightContextCache;
}
async function createContext(): Promise<InflightContext> {
const context: InflightContext = {
contextId: 0,
functions: {},
currentFunctionId: 0,
calls: {},
currentCallId: 0,
};
const session = <ContextSession>await v8Hooks.getSessionAsync();
const post = util.promisify(session.post);
// Create own context with known context id and functionsContext as `global`
await post.call(session, "Runtime.enable");
const contextIdAsync = new Promise<number>(resolve => {
session.once("Runtime.executionContextCreated", event => {
resolve(event.params.context.id);
});
});
vm.createContext(context);
context.contextId = await contextIdAsync;
await post.call(session, "Runtime.disable");
return context;
}
async function getRuntimeIdForFunctionAsync(func: Function): Promise<inspector.Runtime.RemoteObjectId> {
// In order to get information about an object, we need to put it in a well known location so
// that we can call Runtime.evaluate and find it. To do this, we use a special map on the
// 'global' object of a vm context only used for this purpose, and map from a unique-id to that
// object. We then call Runtime.evaluate with an expression that then points to that unique-id
// in that global object. The runtime will then find the object and give us back an internal id
// for it. We can then query for information about the object through that internal id.
//
// Note: the reason for the mapping object and the unique-id we create is so that we don't run
// into any issues when being called asynchronously. We don't want to place the object in a
// location that might be overwritten by another call while we're asynchronously waiting for our
// original call to complete.
const session = <EvaluationSession>await v8Hooks.getSessionAsync();
const post = util.promisify(session.post);
// Place the function in a unique location
const context = await inflightContext();
const currentFunctionName = "id" + context.currentFunctionId++;
context.functions[currentFunctionName] = func;
const contextId = context.contextId;
const expression = `functions.${currentFunctionName}`;
try {
const retType = await post.call(session, "Runtime.evaluate", { contextId, expression });
if (retType.exceptionDetails) {
throw new Error(`Error calling "Runtime.evaluate(${expression})" on context ${contextId}: ` + retType.exceptionDetails.text);
}
const remoteObject = retType.result;
if (remoteObject.type !== "function") {
throw new Error("Remote object was not 'function': " + JSON.stringify(remoteObject));
}
if (!remoteObject.objectId) {
throw new Error("Remote function does not have 'objectId': " + JSON.stringify(remoteObject));
}
return remoteObject.objectId;
}
finally {
delete context.functions[currentFunctionName];
}
}
async function runtimeGetPropertiesAsync(
objectId: inspector.Runtime.RemoteObjectId,
ownProperties: boolean | undefined) {
const session = <GetPropertiesSession>await v8Hooks.getSessionAsync();
const post = util.promisify(session.post);
// This cast will become unnecessary when we move to TS 3.1.6 or above. In that version they
// support typesafe '.call' calls.
const retType = <inspector.Runtime.GetPropertiesReturnType>await post.call(
session, "Runtime.getProperties", { objectId, ownProperties });
if (retType.exceptionDetails) {
throw new Error(`Error calling "Runtime.getProperties(${objectId}, ${ownProperties})": `
+ retType.exceptionDetails.text);
}
return { internalProperties: retType.internalProperties || [], properties: retType.result };
}
async function getValueForObjectId(objectId: inspector.Runtime.RemoteObjectId): Promise<any> {
// In order to get the raw JS value for the *remote wrapper* of the [[Scopes]] array, we use
// Runtime.callFunctionOn on it passing in a fresh function-declaration. The Node runtime will
// then compile that function, invoking it with the 'real' underlying scopes-array value in
// memory as the bound 'this' value. Inside that function declaration, we can then access
// 'this' and assign it to a unique-id in a well known mapping table we have set up. As above,
// the unique-id is to prevent any issues with multiple in-flight asynchronous calls.
const session = <CallFunctionSession>await v8Hooks.getSessionAsync();
const post = util.promisify(session.post);
const context = await inflightContext();
// Get an id for an unused location in the global table.
const tableId = "id" + context.currentCallId++;
// Now, ask the runtime to call a fictitious method on the scopes-array object. When it
// does, it will get the actual underlying value for the scopes array and bind it to the
// 'this' value inside the function. Inside the function we then just grab 'this' and
// stash it in our global table. After this completes, we'll then have access to it.
// This cast will become unnecessary when we move to TS 3.1.6 or above. In that version they
// support typesafe '.call' calls.
const retType = <inspector.Runtime.CallFunctionOnReturnType>await post.call(
session, "Runtime.callFunctionOn", {
objectId,
functionDeclaration: `function () {
calls["${tableId}"] = this;
}`,
});
if (retType.exceptionDetails) {
throw new Error(`Error calling "Runtime.callFunction(${objectId})": `
+ retType.exceptionDetails.text);
}
if (!context.calls.hasOwnProperty(tableId)) {
throw new Error(`Value was not stored into table after calling "Runtime.callFunctionOn(${objectId})"`);
}
// Extract value and clear our table entry.
const val = context.calls[tableId];
delete context.calls[tableId];
return val;
}