Clean up options and fix test code

Summary:
- Clean up options and create new `prepack-options.js` file
- Add `PartialEvaluatorOptions` type which contains only `sourceMaps`
- Fix test code related to above change

https://github.com/facebook/prepack/issues/841
Closes https://github.com/facebook/prepack/pull/878

Reviewed By: NTillmann

Differential Revision: D5605347

Pulled By: cblappert

fbshipit-source-id: 024284a9904dff765417c2c849c4642e781affb3
This commit is contained in:
Sangboak Lee 2017-08-11 14:54:04 -07:00 committed by Facebook Github Bot
parent d95111b803
commit 1bbba378d2
11 changed files with 128 additions and 105 deletions

View File

@ -53,7 +53,7 @@ function generateTest(name: string, test_path: string, code: string): boolean {
try {
let s = prepackFileSync(test_path, {
internalDebug: true,
onError: errorHandler,
errorHandler: errorHandler,
sourceMaps: true,
serialize: true,
});
@ -69,7 +69,7 @@ function generateTest(name: string, test_path: string, code: string): boolean {
compatibility: "node-source-maps",
inputSourceMapFilename: name + ".new1.js.map",
internalDebug: true,
onError: errorHandler,
errorHandler: errorHandler,
sourceMaps: true,
serialize: true,
});

View File

@ -68,9 +68,9 @@ function runTest(name: string, code: string): boolean {
let options = {
internalDebug: false,
mathRandomSeed: "0",
onError: errorHandler.bind(null, recover ? "Recover" : "Fail", errors),
errorHandler: errorHandler.bind(null, recover ? "Recover" : "Fail", errors),
serialize: true,
speculate: true,
initializeMoreModules: true,
};
if (additionalFunctions) (options: any).additionalFunctions = ["global.additional1", "global['additional2']"];
prepackFileSync(name, options);

View File

@ -68,9 +68,9 @@ function runTest(name: string, code: string): boolean {
compatibility: "jsc-600-1-4-17",
delayUnsupportedRequires: true,
mathRandomSeed: "0",
onError: errorHandler,
errorHandler,
serialize: true,
speculate: !modelCode,
initializeMoreModules: !modelCode,
sourceMaps: !!sourceMap,
};
if (name.endsWith("/bundle.js~"))

View File

@ -86,14 +86,14 @@ report(inspect());`,
function runTest(name, code, options, args) {
console.log(chalk.inverse(name) + " " + util.inspect(options));
let compatibility = code.includes("// jsc") ? "jsc-600-1-4-17" : undefined;
let speculate = code.includes("// initialize more modules");
let initializeMoreModules = code.includes("// initialize more modules");
let delayUnsupportedRequires = code.includes("// delay unsupported requires");
let functionCloneCountMatch = code.match(/\/\/ serialized function clone count: (\d+)/);
options = Object.assign({}, options, {
compatibility,
speculate,
initializeMoreModules,
delayUnsupportedRequires,
onError: diag => "Fail",
errorHandler: diag => "Fail",
internalDebug: true,
serialize: true,
uniqueSuffix: "",
@ -105,7 +105,7 @@ function runTest(name, code, options, args) {
let realm = construct_realm(realmOptions);
initializeGlobals(realm);
let serializerOptions = {
initializeMoreModules: speculate,
initializeMoreModules,
delayUnsupportedRequires,
internalDebug: true,
additionalFunctions: options.additionalFunctions,

View File

@ -30,7 +30,8 @@ import {
ThrowCompletion,
} from "./completions.js";
import { FatalError } from "./errors.js";
import { defaultOptions, type Options } from "./options";
import { defaultOptions } from "./options";
import type { PartialEvaluatorOptions } from "./options";
import { ExecutionContext } from "./realm.js";
import { Value } from "./values/index.js";
import {
@ -1111,7 +1112,7 @@ export class LexicalEnvironment {
executePartialEvaluator(
sources: Array<SourceFile>,
options: Options = defaultOptions,
options: PartialEvaluatorOptions = defaultOptions,
sourceType: SourceType = "script"
): AbruptCompletion | { code: string, map?: SourceMap } {
let body: Array<BabelNodeStatement> = [];

View File

@ -39,81 +39,8 @@ export type SerializerOptions = {
trace?: boolean,
};
export type Options = {|
additionalFunctions?: Array<string>,
compatibility?: Compatibility,
debugNames?: boolean,
delayInitializations?: boolean,
delayUnsupportedRequires?: boolean,
inputSourceMapFilename?: string,
internalDebug?: boolean,
logStatistics?: boolean,
logModules?: boolean,
mathRandomSeed?: string,
onError?: ErrorHandler,
outputFilename?: string,
profile?: boolean,
residual?: boolean,
serialize?: boolean,
singlePass?: boolean,
export type PartialEvaluatorOptions = {
sourceMaps?: boolean,
speculate?: boolean,
statsFile?: string,
strictlyMonotonicDateNow?: boolean,
timeout?: number,
trace?: boolean,
uniqueSuffix?: string,
|};
};
export const defaultOptions = {};
export function getRealmOptions({
compatibility = "browser",
debugNames = false,
onError,
mathRandomSeed,
uniqueSuffix,
residual,
serialize = !residual,
strictlyMonotonicDateNow,
timeout,
}: Options): RealmOptions {
return {
compatibility,
debugNames,
errorHandler: onError,
mathRandomSeed,
uniqueSuffix,
residual,
serialize,
strictlyMonotonicDateNow,
timeout,
};
}
export function getSerializerOptions({
additionalFunctions,
delayInitializations = false,
delayUnsupportedRequires = false,
internalDebug = false,
logStatistics = false,
logModules = false,
profile = false,
singlePass = false,
speculate = false,
trace = false,
}: Options): SerializerOptions {
let result: SerializerOptions = {
delayInitializations,
delayUnsupportedRequires,
initializeMoreModules: speculate,
internalDebug,
logStatistics,
logModules,
profile,
singlePass,
trace,
};
if (additionalFunctions) result.additionalFunctions = additionalFunctions;
return result;
}

View File

@ -61,7 +61,7 @@ function run(
let outputSourceMap;
let statsFileName;
let flags = {
speculate: false,
initializeMoreModules: false,
trace: false,
debugNames: false,
singlePass: false,
@ -132,7 +132,7 @@ function run(
compatibility,
mathRandomSeed,
inputSourceMapFilename: inputSourceMap,
onError: errorHandler,
errorHandler: errorHandler,
sourceMaps: !!outputSourceMap,
},
flags

View File

@ -18,12 +18,12 @@ import { Completion } from "./completions.js";
import { Value } from "./values";
import construct_realm from "./construct_realm.js";
import initializeGlobals from "./globals.js";
import { getRealmOptions, getSerializerOptions } from "./options";
import { getRealmOptions, getSerializerOptions } from "./prepack-options";
import { FatalError } from "./errors.js";
import initializeBootstrap from "./intrinsics/node/bootstrap.js";
import initializeProcess from "./intrinsics/node/process.js";
import type { Options } from "./options";
import type { PrepackOptions } from "./prepack-options";
import { defaultOptions } from "./options";
import type { SourceMap } from "./types.js";
@ -31,7 +31,7 @@ declare var process: any;
export function prepackNodeCLI(
filename: string,
options: Options = defaultOptions,
options: PrepackOptions = defaultOptions,
callback: (any, ?{ code: string, map?: SourceMap }) => void
) {
let serialized;
@ -44,7 +44,7 @@ export function prepackNodeCLI(
callback(null, serialized);
}
export function prepackNodeCLISync(filename: string, options: Options = defaultOptions) {
export function prepackNodeCLISync(filename: string, options: PrepackOptions = defaultOptions) {
if (process.version !== "v7.9.0") {
console.warn(
`Prepack's node-cli mode currently only works on Node v7.9.0.\n` +

View File

@ -12,7 +12,8 @@
*/
/* @flow */
import { type Options, defaultOptions } from "./options";
import { defaultOptions } from "./options";
import { type PrepackOptions } from "./prepack-options";
import { prepackNodeCLI, prepackNodeCLISync } from "./prepack-node-environment.js";
import { prepackString } from "./prepack-standalone.js";
import { type SourceMap } from "./types.js";
@ -23,7 +24,7 @@ export * from "./prepack-node-environment";
export * from "./prepack-standalone";
export function prepackStdin(
options: Options = defaultOptions,
options: PrepackOptions = defaultOptions,
callback: (any, ?{ code: string, map?: SourceMap }) => void
) {
let sourceMapFilename = options.inputSourceMapFilename || "";
@ -50,7 +51,7 @@ export function prepackStdin(
export function prepackFile(
filename: string,
options: Options = defaultOptions,
options: PrepackOptions = defaultOptions,
callback: (any, ?{ code: string, map?: SourceMap }) => void,
fileErrorHandler?: (err: ?Error) => void
) {
@ -81,7 +82,7 @@ export function prepackFile(
});
}
export function prepackFileSync(filename: string, options: Options = defaultOptions) {
export function prepackFileSync(filename: string, options: PrepackOptions = defaultOptions) {
if (options.compatibility === "node-cli") {
return prepackNodeCLISync(filename, options);
}

90
src/prepack-options.js Normal file
View File

@ -0,0 +1,90 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/* @flow */
import type { ErrorHandler } from "./errors.js";
import type { SerializerOptions, RealmOptions, Compatibility } from "./options";
export type PrepackOptions = {|
additionalFunctions?: Array<string>,
compatibility?: Compatibility,
debugNames?: boolean,
delayInitializations?: boolean,
delayUnsupportedRequires?: boolean,
inputSourceMapFilename?: string,
internalDebug?: boolean,
logStatistics?: boolean,
logModules?: boolean,
mathRandomSeed?: string,
errorHandler?: ErrorHandler,
outputFilename?: string,
profile?: boolean,
residual?: boolean,
serialize?: boolean,
singlePass?: boolean,
sourceMaps?: boolean,
initializeMoreModules?: boolean,
statsFile?: string,
strictlyMonotonicDateNow?: boolean,
timeout?: number,
trace?: boolean,
uniqueSuffix?: string,
|};
export function getRealmOptions({
compatibility = "browser",
debugNames = false,
errorHandler,
mathRandomSeed,
uniqueSuffix,
residual,
serialize = !residual,
strictlyMonotonicDateNow,
timeout,
}: PrepackOptions): RealmOptions {
return {
compatibility,
debugNames,
errorHandler,
mathRandomSeed,
uniqueSuffix,
residual,
serialize,
strictlyMonotonicDateNow,
timeout,
};
}
export function getSerializerOptions({
additionalFunctions,
delayInitializations = false,
delayUnsupportedRequires = false,
internalDebug = false,
logStatistics = false,
logModules = false,
profile = false,
singlePass = false,
initializeMoreModules = false,
trace = false,
}: PrepackOptions): SerializerOptions {
let result: SerializerOptions = {
delayInitializations,
delayUnsupportedRequires,
initializeMoreModules,
internalDebug,
logStatistics,
logModules,
profile,
singlePass,
trace,
};
if (additionalFunctions) result.additionalFunctions = additionalFunctions;
return result;
}

View File

@ -15,12 +15,12 @@ import Serializer from "./serializer/index.js";
import construct_realm from "./construct_realm.js";
import initializeGlobals from "./globals.js";
import * as t from "babel-types";
import { getRealmOptions, getSerializerOptions } from "./options";
import { getRealmOptions, getSerializerOptions } from "./prepack-options";
import { FatalError } from "./errors.js";
import { SerializerStatistics, TimingStatistics } from "./serializer/types.js";
import type { SourceFile } from "./types.js";
import { AbruptCompletion } from "./completions.js";
import type { Options } from "./options";
import type { PrepackOptions } from "./prepack-options";
import { defaultOptions } from "./options";
import type { BabelNodeFile, BabelNodeProgram } from "babel-types";
import invariant from "./invariant.js";
@ -38,10 +38,10 @@ Object.setPrototypeOf(FatalError.prototype, InitializationError.prototype);
export function prepackSources(
sources: Array<SourceFile>,
options: Options = defaultOptions
options: PrepackOptions = defaultOptions
): { code: string, map?: SourceMap, statistics?: SerializerStatistics, timingStats?: TimingStatistics } {
let realmOptions = getRealmOptions(options);
realmOptions.errorHandler = options.onError;
realmOptions.errorHandler = options.errorHandler;
let realm = construct_realm(realmOptions);
initializeGlobals(realm);
@ -76,7 +76,7 @@ export function prepackString(
filename: string,
code: string,
sourceMap: string,
options: Options = defaultOptions
options: PrepackOptions = defaultOptions
): { code: string, map?: SourceMap, statistics?: SerializerStatistics, timingStats?: TimingStatistics } {
let sources = [{ filePath: filename, fileContents: code, sourceMapContents: sourceMap }];
let realmOptions = getRealmOptions(options);
@ -109,12 +109,12 @@ export function prepackString(
}
/* deprecated: please use prepackString instead. */
export function prepack(code: string, options: Options = defaultOptions) {
export function prepack(code: string, options: PrepackOptions = defaultOptions) {
let filename = options.filename || "unknown";
let sources = [{ filePath: filename, fileContents: code }];
let realmOptions = getRealmOptions(options);
realmOptions.errorHandler = options.onError;
realmOptions.errorHandler = options.errorHandler;
let realm = construct_realm(realmOptions);
initializeGlobals(realm);
@ -127,7 +127,11 @@ export function prepack(code: string, options: Options = defaultOptions) {
}
/* deprecated: please use prepackString instead. */
export function prepackFromAst(ast: BabelNodeFile | BabelNodeProgram, code: string, options: Options = defaultOptions) {
export function prepackFromAst(
ast: BabelNodeFile | BabelNodeProgram,
code: string,
options: PrepackOptions = defaultOptions
) {
if (ast && ast.type === "Program") {
ast = t.file(ast, [], []);
}