summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRy Dahl <ry@tinyclouds.org>2020-01-21 14:57:56 -0500
committerGitHub <noreply@github.com>2020-01-21 14:57:56 -0500
commitfa7f34eb8cec07f4c68ca4e9c46a983bc3e2308f (patch)
tree821263579dd75de3c16508fdecfdcaba52987373
parent7fd50065a7d8a4c6ed1b1090703a7baaabdbd6aa (diff)
Revert "Create an old program to be used in snapshot. (#3644)"
Ref #3712. This change allowed the deno_typescript crate to reference cli/js/lib.deno_runtime.d.ts which breaks "cargo package". We intend to reintroduce a revised version of this patch later once "cargo package" is working and tested. This reverts commit 737ab94ea1bdf65eeef323ea37e84bcf430fb92c.
-rw-r--r--cli/js.rs10
-rw-r--r--cli/js/compiler.ts17
-rw-r--r--cli/js/compiler_bootstrap.ts34
-rw-r--r--cli/js/compiler_bundler.ts14
-rw-r--r--cli/js/compiler_host.ts10
-rw-r--r--cli/js/compiler_sourcefile.ts2
-rw-r--r--cli/js/dispatch.ts5
-rw-r--r--cli/js/dispatch_json.ts2
-rw-r--r--cli/lib.rs2
-rw-r--r--cli/ops/compiler.rs22
-rw-r--r--core/isolate.rs1
-rw-r--r--core/shared_queue.js1
-rw-r--r--deno_typescript/lib.rs8
-rw-r--r--deno_typescript/ops.rs15
14 files changed, 59 insertions, 84 deletions
diff --git a/cli/js.rs b/cli/js.rs
index d6a360b52..bf13d704d 100644
--- a/cli/js.rs
+++ b/cli/js.rs
@@ -16,6 +16,16 @@ pub static COMPILER_SNAPSHOT_MAP: &[u8] =
pub static COMPILER_SNAPSHOT_DTS: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/COMPILER_SNAPSHOT.d.ts"));
+static DENO_RUNTIME: &str = include_str!("js/lib.deno_runtime.d.ts");
+
+/// Same as deno_typescript::get_asset but also has lib.deno_runtime.d.ts
+pub fn get_asset(name: &str) -> Option<&'static str> {
+ match name {
+ "lib.deno_runtime.d.ts" => Some(DENO_RUNTIME),
+ _ => deno_typescript::get_asset(name),
+ }
+}
+
#[test]
fn cli_snapshot() {
let mut isolate = deno_core::Isolate::new(
diff --git a/cli/js/compiler.ts b/cli/js/compiler.ts
index 54861f713..3cebf24b2 100644
--- a/cli/js/compiler.ts
+++ b/cli/js/compiler.ts
@@ -6,7 +6,6 @@ import "./globals.ts";
import "./ts_global.d.ts";
import { TranspileOnlyResult } from "./compiler_api.ts";
-import { oldProgram } from "./compiler_bootstrap.ts";
import { setRootExports } from "./compiler_bundler.ts";
import {
defaultBundlerOptions,
@@ -143,12 +142,7 @@ self.bootstrapTsCompiler = function tsCompilerMain(): void {
// to generate the program and possibly emit it.
if (!diagnostics || (diagnostics && diagnostics.length === 0)) {
const options = host.getCompilationSettings();
- const program = ts.createProgram({
- rootNames,
- options,
- host,
- oldProgram
- });
+ const program = ts.createProgram(rootNames, options, host);
diagnostics = ts
.getPreEmitDiagnostics(program)
@@ -226,12 +220,11 @@ self.bootstrapTsCompiler = function tsCompilerMain(): void {
}
host.mergeOptions(...compilerOptions);
- const program = ts.createProgram({
+ const program = ts.createProgram(
rootNames,
- options: host.getCompilationSettings(),
- host,
- oldProgram
- });
+ host.getCompilationSettings(),
+ host
+ );
if (bundle) {
setRootExports(program, rootNames[0]);
diff --git a/cli/js/compiler_bootstrap.ts b/cli/js/compiler_bootstrap.ts
deleted file mode 100644
index 6de978750..000000000
--- a/cli/js/compiler_bootstrap.ts
+++ /dev/null
@@ -1,34 +0,0 @@
-// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
-
-import { ASSETS, Host } from "./compiler_host.ts";
-import { core } from "./core.ts";
-import * as dispatch from "./dispatch.ts";
-import { sendSync } from "./dispatch_json.ts";
-
-// This registers ops that are available during the snapshotting process.
-const ops = core.ops();
-for (const [name, opId] of Object.entries(ops)) {
- const opName = `OP_${name.toUpperCase()}`;
- // TODO This type casting is dangerous, and should be improved when the same
- // code in `os.ts` is done.
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- (dispatch as any)[opName] = opId;
-}
-
-const host = new Host({ writeFile(): void {} });
-const options = host.getCompilationSettings();
-
-/** Used to generate the foundational AST for all other compilations, so it can
- * be cached as part of the snapshot and available to speed up startup */
-export const oldProgram = ts.createProgram({
- rootNames: [`${ASSETS}/bootstrap.ts`],
- options,
- host
-});
-
-/** A module loader which is concatenated into bundle files. We read all static
- * assets during the snapshotting process, which is why this is located in
- * compiler_bootstrap. */
-export const bundleLoader = sendSync(dispatch.OP_FETCH_ASSET, {
- name: "bundle_loader.js"
-});
diff --git a/cli/js/compiler_bundler.ts b/cli/js/compiler_bundler.ts
index 8fb68cc7a..a4e4557ca 100644
--- a/cli/js/compiler_bundler.ts
+++ b/cli/js/compiler_bundler.ts
@@ -1,6 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
-import { bundleLoader } from "./compiler_bootstrap.ts";
+import * as dispatch from "./dispatch.ts";
+import { sendSync } from "./dispatch_json.ts";
import {
assert,
commonPath,
@@ -8,6 +9,11 @@ import {
CHAR_FORWARD_SLASH
} from "./util.ts";
+const BUNDLE_LOADER = "bundle_loader.js";
+
+/** A loader of bundled modules that we will inline into any bundle outputs. */
+let bundleLoader: string;
+
/** Local state of what the root exports are of a root module. */
let rootExports: string[] | undefined;
@@ -34,6 +40,12 @@ export function buildBundle(
data: string,
sourceFiles: readonly ts.SourceFile[]
): string {
+ // we can only do this once we are bootstrapped and easiest way to do it is
+ // inline here
+ if (!bundleLoader) {
+ bundleLoader = sendSync(dispatch.OP_FETCH_ASSET, { name: BUNDLE_LOADER });
+ }
+
// when outputting to AMD and a single outfile, TypeScript makes up the module
// specifiers which are used to define the modules, and doesn't expose them
// publicly, so we have to try to replicate
diff --git a/cli/js/compiler_host.ts b/cli/js/compiler_host.ts
index 3e6df4485..f8921a352 100644
--- a/cli/js/compiler_host.ts
+++ b/cli/js/compiler_host.ts
@@ -18,7 +18,7 @@ export interface ConfigureResponse {
diagnostics?: ts.Diagnostic[];
}
-export const ASSETS = "$asset$";
+const ASSETS = "$asset$";
/** Options that need to be used when generating a bundle (either trusted or
* runtime). */
@@ -129,11 +129,11 @@ export class Host implements ts.CompilerHost {
private _writeFile: WriteFileCallback;
private _getAsset(filename: string): SourceFile {
- const url = filename.split("/").pop()!;
- const sourceFile = SourceFile.get(url);
+ const sourceFile = SourceFile.get(filename);
if (sourceFile) {
return sourceFile;
}
+ const url = filename.split("/").pop()!;
const name = url.includes(".") ? url : `${url}.d.ts`;
const sourceCode = sendSync(dispatch.OP_FETCH_ASSET, { name });
return new SourceFile({
@@ -238,15 +238,13 @@ export class Host implements ts.CompilerHost {
: SourceFile.get(fileName);
assert(sourceFile != null);
if (!sourceFile.tsSourceFile) {
- assert(sourceFile.sourceCode != null);
sourceFile.tsSourceFile = ts.createSourceFile(
fileName,
sourceFile.sourceCode,
languageVersion
);
- delete sourceFile.sourceCode;
}
- return sourceFile.tsSourceFile;
+ return sourceFile!.tsSourceFile;
} catch (e) {
if (onError) {
onError(String(e));
diff --git a/cli/js/compiler_sourcefile.ts b/cli/js/compiler_sourcefile.ts
index 8e81cdb45..087891c99 100644
--- a/cli/js/compiler_sourcefile.ts
+++ b/cli/js/compiler_sourcefile.ts
@@ -61,7 +61,7 @@ export class SourceFile {
mediaType!: MediaType;
processed = false;
- sourceCode?: string;
+ sourceCode!: string;
tsSourceFile?: ts.SourceFile;
url!: string;
diff --git a/cli/js/dispatch.ts b/cli/js/dispatch.ts
index f5049cca8..073c32f7a 100644
--- a/cli/js/dispatch.ts
+++ b/cli/js/dispatch.ts
@@ -70,15 +70,12 @@ export let OP_TRUNCATE: number;
export let OP_MAKE_TEMP_DIR: number;
export let OP_CWD: number;
export let OP_CONNECT_TLS: number;
+export let OP_FETCH_ASSET: number;
export let OP_HOSTNAME: number;
export let OP_OPEN_PLUGIN: number;
export let OP_COMPILE: number;
export let OP_TRANSPILE: number;
-/** **WARNING:** This is only available during the snapshotting process and is
- * unavailable at runtime. */
-export let OP_FETCH_ASSET: number;
-
const PLUGIN_ASYNC_HANDLER_MAP: Map<number, AsyncHandler> = new Map();
export function setPluginAsyncHandler(
diff --git a/cli/js/dispatch_json.ts b/cli/js/dispatch_json.ts
index adccb69c6..104a7e564 100644
--- a/cli/js/dispatch_json.ts
+++ b/cli/js/dispatch_json.ts
@@ -62,7 +62,7 @@ export function sendSync(
const resUi8 = core.dispatch(opId, argsUi8, zeroCopy);
util.assert(resUi8 != null);
- const res = decode(resUi8);
+ const res = decode(resUi8!);
util.assert(res.promiseId == null);
return unwrapResponse(res);
}
diff --git a/cli/lib.rs b/cli/lib.rs
index 53dac1ea9..263bd8c61 100644
--- a/cli/lib.rs
+++ b/cli/lib.rs
@@ -146,7 +146,7 @@ fn create_worker_and_state(
}
fn types_command() {
- let content = deno_typescript::get_asset("lib.deno_runtime.d.ts").unwrap();
+ let content = crate::js::get_asset("lib.deno_runtime.d.ts").unwrap();
println!("{}", content);
}
diff --git a/cli/ops/compiler.rs b/cli/ops/compiler.rs
index e515081df..cb2fb01a0 100644
--- a/cli/ops/compiler.rs
+++ b/cli/ops/compiler.rs
@@ -17,6 +17,10 @@ pub fn init(i: &mut Isolate, s: &ThreadSafeState) {
"fetch_source_files",
s.core_op(json_op(s.stateful_op(op_fetch_source_files))),
);
+ i.register_op(
+ "fetch_asset",
+ s.core_op(json_op(s.stateful_op(op_fetch_asset))),
+ );
}
#[derive(Deserialize)]
@@ -145,3 +149,21 @@ fn op_fetch_source_files(
Ok(JsonOp::Async(future))
}
+
+#[derive(Deserialize)]
+struct FetchAssetArgs {
+ name: String,
+}
+
+fn op_fetch_asset(
+ _state: &ThreadSafeState,
+ args: Value,
+ _zero_copy: Option<PinnedBuf>,
+) -> Result<JsonOp, ErrBox> {
+ let args: FetchAssetArgs = serde_json::from_value(args)?;
+ if let Some(source_code) = crate::js::get_asset(&args.name) {
+ Ok(JsonOp::Sync(json!(source_code)))
+ } else {
+ panic!("op_fetch_asset bad asset {}", args.name)
+ }
+}
diff --git a/core/isolate.rs b/core/isolate.rs
index e4405b704..0696d5ad4 100644
--- a/core/isolate.rs
+++ b/core/isolate.rs
@@ -692,7 +692,6 @@ impl Isolate {
let mut hs = v8::HandleScope::new(&mut locker);
let scope = hs.enter();
self.global_context.reset(scope);
- self.shared_response_buf.reset(scope);
let snapshot_creator = self.snapshot_creator.as_mut().unwrap();
let snapshot = snapshot_creator
diff --git a/core/shared_queue.js b/core/shared_queue.js
index 093cc223f..8ebb19e5e 100644
--- a/core/shared_queue.js
+++ b/core/shared_queue.js
@@ -184,6 +184,7 @@ SharedQueue Binary Layout
}
function dispatch(opId, control, zeroCopy = null) {
+ maybeInit();
return Deno.core.send(opId, control, zeroCopy);
}
diff --git a/deno_typescript/lib.rs b/deno_typescript/lib.rs
index c503d3508..016adcca0 100644
--- a/deno_typescript/lib.rs
+++ b/deno_typescript/lib.rs
@@ -203,10 +203,6 @@ pub fn mksnapshot_bundle_ts(
state: Arc<Mutex<TSState>>,
) -> Result<(), ErrBox> {
let runtime_isolate = &mut Isolate::new(StartupData::None, true);
- runtime_isolate.register_op(
- "fetch_asset",
- compiler_op(state.clone(), ops::json_op(ops::fetch_asset)),
- );
let source_code_vec = std::fs::read(bundle)?;
let source_code = std::str::from_utf8(&source_code_vec)?;
@@ -259,10 +255,6 @@ pub fn get_asset(name: &str) -> Option<&'static str> {
}
match name {
"bundle_loader.js" => Some(include_str!("bundle_loader.js")),
- "lib.deno_runtime.d.ts" => {
- Some(include_str!("../cli/js/lib.deno_runtime.d.ts"))
- }
- "bootstrap.ts" => Some("console.log(\"hello deno\");"),
"typescript.d.ts" => inc!("typescript.d.ts"),
"lib.esnext.d.ts" => inc!("lib.esnext.d.ts"),
"lib.es2020.d.ts" => inc!("lib.es2020.d.ts"),
diff --git a/deno_typescript/ops.rs b/deno_typescript/ops.rs
index d557b9b8c..e45349591 100644
--- a/deno_typescript/ops.rs
+++ b/deno_typescript/ops.rs
@@ -112,21 +112,6 @@ pub fn resolve_module_names(
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
-struct FetchAssetArgs {
- name: String,
-}
-
-pub fn fetch_asset(_s: &mut TSState, v: Value) -> Result<Value, ErrBox> {
- let args: FetchAssetArgs = serde_json::from_value(v)?;
- if let Some(source_code) = crate::get_asset(&args.name) {
- Ok(json!(source_code))
- } else {
- panic!("op_fetch_asset bad asset {}", args.name)
- }
-}
-
-#[derive(Debug, Deserialize)]
-#[serde(rename_all = "camelCase")]
struct Exit {
code: i32,
}