pulumi/sdk/nodejs/runtime/closure/v8.ts
CyrusNajmabadi 901a238fd5
Get closure serialiation working in Node11 (#2101)
* Make v8 primitives async as there is no way to avoid async in node11.

* Simplify API.

* Move processing of well-known globals into the v8 layer.
We'll need this so that we can map from RemoteObjectIds back to these well known values.

* Remove unnecesssary check.

* Cleanup comments and extract helper.

* Introduce helper bridge method for the simple case of making an entry for a string.

* Make functions async.  They'll need to be async once we move to the Inspector api.

* Make functions async.  They'll need to be async once we move to the Inspector api.

* Make functions async.  They'll need to be async once we move to the Inspector api.

* Move property access behind helpers so they can move to the Inspector API in the future.

* Only call function when we know we have a Function.  Remove redundant null check.

* Properly serialize certain special JavaScript number values that JSON serialization cannot handle.

* Only marshall across the 'source' and 'flags' for a RegExp when serializing.

* Add a simple test to validate a regex without flags.

* Extract functionality into helper method.

* Add test with complex output scenarios.

* Output serialization needs to avoid recursively trying to serialize a serialized value.

* Introduce indirection for introspecting properties of an object.

* Use our own introspection API for examining an Array.

* Hide direct property access through API indirection.

* Produce values like the v8 Inspector does.

* Compute the module map asynchronously.  Will need that when mapping mirrors instead.

* Cleanup a little code in closure creation.

* Get serialization working on Node11 (except function locations).

* Run tests in the same order on <v11 and >=v11

* Make tests run on multiple versions of node.

* Rename file to make PR simpler to review.

* Cleanup.

* Be more careful with global state.

* Remove commented line.

* Only allow getting a session when on Node11 or above.

* Promisify methods.
2018-11-01 15:46:21 -07:00

52 lines
2.6 KiB
TypeScript

// 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 semver from "semver";
import * as v8 from "v8";
v8.setFlagsFromString("--allow-natives-syntax");
import * as v8Hooks from "./v8Hooks";
import * as v8_v10andLower from "./v8_v10andLower";
import * as v8_v11andHigher from "./v8_v11andHigher";
// Node majorly changed their introspection apis between 10.0 and 11.0 (removing outright some
// of the APIs we use). Detect if we're before or after this change and delegate to the
const versionSpecificV8Module = v8Hooks.isNodeAtLeastV11 ? v8_v11andHigher : v8_v10andLower;
/**
* 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.
*/
export const lookupCapturedVariableValueAsync = versionSpecificV8Module.lookupCapturedVariableValueAsync;
/**
* 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.
*/
export const getFunctionLocationAsync = versionSpecificV8Module.getFunctionLocationAsync;