From 0770449c938cdd0edbadaf1e87e460b21fc1b4ef Mon Sep 17 00:00:00 2001
From: Luca Casonato <lucacasonato@yahoo.com>
Date: Fri, 12 Mar 2021 21:23:59 +0100
Subject: [PATCH] refactor: move Console to op_crates/console (#9770)

---
 Cargo.lock                                    |  8 +++++
 cli/build.rs                                  |  6 ++++
 cli/dts/lib.deno.shared_globals.d.ts          | 24 +--------------
 cli/main.rs                                   |  3 +-
 cli/tests/unit/dom_iterable_test.ts           |  5 +---
 cli/tsc.rs                                    |  1 +
 .../js => op_crates/console}/01_colors.js     |  1 +
 .../js => op_crates/console}/02_console.js    | 14 +++++----
 op_crates/console/Cargo.toml                  | 17 +++++++++++
 op_crates/console/README.md                   |  5 ++++
 op_crates/console/lib.deno_console.d.ts       | 29 +++++++++++++++++++
 op_crates/console/lib.rs                      | 25 ++++++++++++++++
 op_crates/fetch/03_dom_iterable.js            |  6 ++--
 runtime/Cargo.toml                            |  2 ++
 runtime/build.rs                              |  1 +
 runtime/js/01_internals.js                    | 24 ---------------
 runtime/js/06_util.js                         |  6 ++--
 runtime/js/40_testing.js                      | 12 ++++----
 runtime/js/99_main.js                         | 10 +++++--
 runtime/lib.rs                                |  1 +
 20 files changed, 129 insertions(+), 71 deletions(-)
 rename {runtime/js => op_crates/console}/01_colors.js (99%)
 rename {runtime/js => op_crates/console}/02_console.js (99%)
 create mode 100644 op_crates/console/Cargo.toml
 create mode 100644 op_crates/console/README.md
 create mode 100644 op_crates/console/lib.deno_console.d.ts
 create mode 100644 op_crates/console/lib.rs
 delete mode 100644 runtime/js/01_internals.js

diff --git a/Cargo.lock b/Cargo.lock
index c199cf560f..e657bd019e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -552,6 +552,13 @@ dependencies = [
  "winres",
 ]
 
+[[package]]
+name = "deno_console"
+version = "0.1.0"
+dependencies = [
+ "deno_core",
+]
+
 [[package]]
 name = "deno_core"
 version = "0.80.2"
@@ -632,6 +639,7 @@ name = "deno_runtime"
 version = "0.9.3"
 dependencies = [
  "atty",
+ "deno_console",
  "deno_core",
  "deno_crypto",
  "deno_fetch",
diff --git a/cli/build.rs b/cli/build.rs
index 3f97a71b74..6606242e2c 100644
--- a/cli/build.rs
+++ b/cli/build.rs
@@ -8,6 +8,7 @@ use deno_core::serde_json::json;
 use deno_core::serde_json::Value;
 use deno_core::JsRuntime;
 use deno_core::RuntimeOptions;
+use deno_runtime::deno_console;
 use deno_runtime::deno_crypto;
 use deno_runtime::deno_fetch;
 use deno_runtime::deno_url;
@@ -62,6 +63,7 @@ fn create_compiler_snapshot(
 ) {
   // libs that are being provided by op crates.
   let mut op_crate_libs = HashMap::new();
+  op_crate_libs.insert("deno.console", deno_console::get_declaration());
   op_crate_libs.insert("deno.url", deno_url::get_declaration());
   op_crate_libs.insert("deno.web", deno_web::get_declaration());
   op_crate_libs.insert("deno.fetch", deno_fetch::get_declaration());
@@ -256,6 +258,10 @@ fn main() {
 
   println!("cargo:rustc-env=TS_VERSION={}", ts_version());
   println!("cargo:rustc-env=GIT_COMMIT_HASH={}", git_commit_hash());
+  println!(
+    "cargo:rustc-env=DENO_CONSOLE_LIB_PATH={}",
+    deno_console::get_declaration().display()
+  );
   println!(
     "cargo:rustc-env=DENO_URL_LIB_PATH={}",
     deno_url::get_declaration().display()
diff --git a/cli/dts/lib.deno.shared_globals.d.ts b/cli/dts/lib.deno.shared_globals.d.ts
index 206cafdc30..84272caeb3 100644
--- a/cli/dts/lib.deno.shared_globals.d.ts
+++ b/cli/dts/lib.deno.shared_globals.d.ts
@@ -5,6 +5,7 @@
 
 /// <reference no-default-lib="true" />
 /// <reference lib="esnext" />
+/// <reference lib="deno.console" />
 /// <reference lib="deno.url" />
 /// <reference lib="deno.web" />
 /// <reference lib="deno.fetch" />
@@ -391,29 +392,6 @@ interface DOMStringList {
 
 type BufferSource = ArrayBufferView | ArrayBuffer;
 
-declare interface Console {
-  assert(condition?: boolean, ...data: any[]): void;
-  clear(): void;
-  count(label?: string): void;
-  countReset(label?: string): void;
-  debug(...data: any[]): void;
-  dir(item?: any, options?: any): void;
-  dirxml(...data: any[]): void;
-  error(...data: any[]): void;
-  group(...data: any[]): void;
-  groupCollapsed(...data: any[]): void;
-  groupEnd(): void;
-  info(...data: any[]): void;
-  log(...data: any[]): void;
-  table(tabularData?: any, properties?: string[]): void;
-  time(label?: string): void;
-  timeEnd(label?: string): void;
-  timeLog(label?: string, ...data: any[]): void;
-  timeStamp(label?: string): void;
-  trace(...data: any[]): void;
-  warn(...data: any[]): void;
-}
-
 declare var console: Console;
 
 interface MessageEventInit<T = any> extends EventInit {
diff --git a/cli/main.rs b/cli/main.rs
index 8d685c6dbe..1931c62dbe 100644
--- a/cli/main.rs
+++ b/cli/main.rs
@@ -278,8 +278,9 @@ fn print_cache_info(
 
 pub fn get_types(unstable: bool) -> String {
   let mut types = format!(
-    "{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}",
+    "{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}\n{}",
     crate::tsc::DENO_NS_LIB,
+    crate::tsc::DENO_CONSOLE_LIB,
     crate::tsc::DENO_URL_LIB,
     crate::tsc::DENO_WEB_LIB,
     crate::tsc::DENO_FETCH_LIB,
diff --git a/cli/tests/unit/dom_iterable_test.ts b/cli/tests/unit/dom_iterable_test.ts
index 259d2d4409..4e94cfdba2 100644
--- a/cli/tests/unit/dom_iterable_test.ts
+++ b/cli/tests/unit/dom_iterable_test.ts
@@ -1,7 +1,6 @@
 // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
 
-/* TODO https://github.com/denoland/deno/issues/7540
-import { unitTest, assert, assertEquals } from "./test_util.ts";
+import { assert, assertEquals, unitTest } from "./test_util.ts";
 
 // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
 function setup() {
@@ -27,7 +26,6 @@ function setup() {
   };
 }
 
-
 unitTest(function testDomIterable(): void {
   const { DomIterable, Base } = setup();
 
@@ -88,4 +86,3 @@ unitTest(function testDomIterableScope(): void {
   checkScope(null, window);
   checkScope(undefined, window);
 });
-*/
diff --git a/cli/tsc.rs b/cli/tsc.rs
index 6bf8546509..78a472dfa3 100644
--- a/cli/tsc.rs
+++ b/cli/tsc.rs
@@ -29,6 +29,7 @@ use std::sync::Mutex;
 // Declaration files
 
 pub static DENO_NS_LIB: &str = include_str!("dts/lib.deno.ns.d.ts");
+pub static DENO_CONSOLE_LIB: &str = include_str!(env!("DENO_CONSOLE_LIB_PATH"));
 pub static DENO_URL_LIB: &str = include_str!(env!("DENO_URL_LIB_PATH"));
 pub static DENO_WEB_LIB: &str = include_str!(env!("DENO_WEB_LIB_PATH"));
 pub static DENO_FETCH_LIB: &str = include_str!(env!("DENO_FETCH_LIB_PATH"));
diff --git a/runtime/js/01_colors.js b/op_crates/console/01_colors.js
similarity index 99%
rename from runtime/js/01_colors.js
rename to op_crates/console/01_colors.js
index 62a2b711f0..66dcb6e23d 100644
--- a/runtime/js/01_colors.js
+++ b/op_crates/console/01_colors.js
@@ -1,4 +1,5 @@
 // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
+
 "use strict";
 
 ((window) => {
diff --git a/runtime/js/02_console.js b/op_crates/console/02_console.js
similarity index 99%
rename from runtime/js/02_console.js
rename to op_crates/console/02_console.js
index 5d174ece40..0077571a6e 100644
--- a/runtime/js/02_console.js
+++ b/op_crates/console/02_console.js
@@ -3,7 +3,6 @@
 
 ((window) => {
   const core = window.Deno.core;
-  const exposeForTest = window.__bootstrap.internals.exposeForTest;
   const colors = window.__bootstrap.colors;
 
   function isInvalidDate(x) {
@@ -1762,11 +1761,14 @@
   }
 
   // Expose these fields to internalObject for tests.
-  exposeForTest("Console", Console);
-  exposeForTest("cssToAnsi", cssToAnsi);
-  exposeForTest("inspectArgs", inspectArgs);
-  exposeForTest("parseCss", parseCss);
-  exposeForTest("parseCssColor", parseCssColor);
+  window.__bootstrap.internals = {
+    ...window.__bootstrap.internals ?? {},
+    Console,
+    cssToAnsi,
+    inspectArgs,
+    parseCss,
+    parseCssColor,
+  };
 
   window.__bootstrap.console = {
     CSI,
diff --git a/op_crates/console/Cargo.toml b/op_crates/console/Cargo.toml
new file mode 100644
index 0000000000..1218f5989d
--- /dev/null
+++ b/op_crates/console/Cargo.toml
@@ -0,0 +1,17 @@
+# Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
+
+[package]
+name = "deno_console"
+version = "0.1.0"
+edition = "2018"
+description = "Implementation of Console API for Deno"
+authors = ["the Deno authors"]
+license = "MIT"
+readme = "README.md"
+repository = "https://github.com/denoland/deno"
+
+[lib]
+path = "lib.rs"
+
+[dependencies]
+deno_core = { version = "0.80.2", path = "../../core" }
diff --git a/op_crates/console/README.md b/op_crates/console/README.md
new file mode 100644
index 0000000000..2f8fb448ac
--- /dev/null
+++ b/op_crates/console/README.md
@@ -0,0 +1,5 @@
+# deno_console
+
+This crate implements the Console API.
+
+Spec: https://console.spec.whatwg.org/
diff --git a/op_crates/console/lib.deno_console.d.ts b/op_crates/console/lib.deno_console.d.ts
new file mode 100644
index 0000000000..d6cb1e99b2
--- /dev/null
+++ b/op_crates/console/lib.deno_console.d.ts
@@ -0,0 +1,29 @@
+// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
+
+// deno-lint-ignore-file no-explicit-any
+
+/// <reference no-default-lib="true" />
+/// <reference lib="esnext" />
+
+declare interface Console {
+  assert(condition?: boolean, ...data: any[]): void;
+  clear(): void;
+  count(label?: string): void;
+  countReset(label?: string): void;
+  debug(...data: any[]): void;
+  dir(item?: any, options?: any): void;
+  dirxml(...data: any[]): void;
+  error(...data: any[]): void;
+  group(...data: any[]): void;
+  groupCollapsed(...data: any[]): void;
+  groupEnd(): void;
+  info(...data: any[]): void;
+  log(...data: any[]): void;
+  table(tabularData?: any, properties?: string[]): void;
+  time(label?: string): void;
+  timeEnd(label?: string): void;
+  timeLog(label?: string, ...data: any[]): void;
+  timeStamp(label?: string): void;
+  trace(...data: any[]): void;
+  warn(...data: any[]): void;
+}
diff --git a/op_crates/console/lib.rs b/op_crates/console/lib.rs
new file mode 100644
index 0000000000..a972f62129
--- /dev/null
+++ b/op_crates/console/lib.rs
@@ -0,0 +1,25 @@
+// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
+
+use deno_core::JsRuntime;
+use std::path::PathBuf;
+
+/// Load and execute the javascript code.
+pub fn init(isolate: &mut JsRuntime) {
+  let files = vec![
+    (
+      "deno:op_crates/console/01_colors.js",
+      include_str!("01_colors.js"),
+    ),
+    (
+      "deno:op_crates/console/02_console.js",
+      include_str!("02_console.js"),
+    ),
+  ];
+  for (url, source_code) in files {
+    isolate.execute(url, source_code).unwrap();
+  }
+}
+
+pub fn get_declaration() -> PathBuf {
+  PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_console.d.ts")
+}
diff --git a/op_crates/fetch/03_dom_iterable.js b/op_crates/fetch/03_dom_iterable.js
index 2e9f6d3ea9..2a3c72fba0 100644
--- a/op_crates/fetch/03_dom_iterable.js
+++ b/op_crates/fetch/03_dom_iterable.js
@@ -3,7 +3,6 @@
 
 ((window) => {
   const { requiredArguments } = window.__bootstrap.fetchUtil;
-  // const { exposeForTest } = window.__bootstrap.internals;
 
   function DomIterableMixin(
     Base,
@@ -70,7 +69,10 @@
     return DomIterable;
   }
 
-  // exposeForTest("DomIterableMixin", DomIterableMixin);
+  window.__bootstrap.internals = {
+    ...window.__bootstrap.internals ?? {},
+    DomIterableMixin,
+  };
 
   window.__bootstrap.domIterable = {
     DomIterableMixin,
diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml
index f654e015c0..db42f93262 100644
--- a/runtime/Cargo.toml
+++ b/runtime/Cargo.toml
@@ -19,6 +19,7 @@ path = "examples/hello_runtime.rs"
 
 [build-dependencies]
 deno_core = { path = "../core", version = "0.80.2" }
+deno_console = { path = "../op_crates/console", version = "0.1.0" }
 deno_crypto = { path = "../op_crates/crypto", version = "0.14.1" }
 deno_fetch = { path = "../op_crates/fetch", version = "0.22.3" }
 deno_web = { path = "../op_crates/web", version = "0.30.3" }
@@ -33,6 +34,7 @@ winapi = "0.3.9"
 
 [dependencies]
 deno_core = { path = "../core", version = "0.80.2" }
+deno_console = { path = "../op_crates/console", version = "0.1.0" }
 deno_crypto = { path = "../op_crates/crypto", version = "0.14.1" }
 deno_fetch = { path = "../op_crates/fetch", version = "0.22.3" }
 deno_web = { path = "../op_crates/web", version = "0.30.3" }
diff --git a/runtime/build.rs b/runtime/build.rs
index 67f80c8672..9172816d82 100644
--- a/runtime/build.rs
+++ b/runtime/build.rs
@@ -14,6 +14,7 @@ fn create_snapshot(
   files: Vec<PathBuf>,
 ) {
   deno_webidl::init(&mut js_runtime);
+  deno_console::init(&mut js_runtime);
   deno_url::init(&mut js_runtime);
   deno_web::init(&mut js_runtime);
   deno_fetch::init(&mut js_runtime);
diff --git a/runtime/js/01_internals.js b/runtime/js/01_internals.js
deleted file mode 100644
index 4843bf3da2..0000000000
--- a/runtime/js/01_internals.js
+++ /dev/null
@@ -1,24 +0,0 @@
-// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
-"use strict";
-
-((window) => {
-  const internalSymbol = Symbol("Deno.internal");
-
-  // The object where all the internal fields for testing will be living.
-  const internalObject = {};
-
-  // Register a field to internalObject for test access,
-  // through Deno[Deno.internal][name].
-  function exposeForTest(name, value) {
-    Object.defineProperty(internalObject, name, {
-      value,
-      enumerable: false,
-    });
-  }
-
-  window.__bootstrap.internals = {
-    internalSymbol,
-    internalObject,
-    exposeForTest,
-  };
-})(this);
diff --git a/runtime/js/06_util.js b/runtime/js/06_util.js
index 7ceb411020..9df9ac88aa 100644
--- a/runtime/js/06_util.js
+++ b/runtime/js/06_util.js
@@ -3,7 +3,6 @@
 
 ((window) => {
   const { build } = window.__bootstrap.build;
-  const internals = window.__bootstrap.internals;
   let logDebug = false;
   let logSource = "JS";
 
@@ -100,7 +99,10 @@
     return pathOrUrl;
   }
 
-  internals.exposeForTest("pathFromURL", pathFromURL);
+  window.__bootstrap.internals = {
+    ...window.__bootstrap.internals ?? {},
+    pathFromURL,
+  };
 
   function writable(value) {
     return {
diff --git a/runtime/js/40_testing.js b/runtime/js/40_testing.js
index eec75f1338..7666fa050a 100644
--- a/runtime/js/40_testing.js
+++ b/runtime/js/40_testing.js
@@ -7,7 +7,6 @@
   const { setExitHandler, exit } = window.__bootstrap.os;
   const { Console, inspectArgs } = window.__bootstrap.console;
   const { stdout } = window.__bootstrap.files;
-  const { exposeForTest } = window.__bootstrap.internals;
   const { metrics } = window.__bootstrap.metrics;
   const { assert } = window.__bootstrap.util;
 
@@ -227,8 +226,6 @@ finishing test case.`;
     }
   }
 
-  exposeForTest("reportToConsole", reportToConsole);
-
   // TODO(bartlomieju): already implements AsyncGenerator<RunTestsMessage>, but add as "implements to class"
   // TODO(bartlomieju): implements PromiseLike<RunTestsEndResult>
   class TestRunner {
@@ -327,8 +324,6 @@ finishing test case.`;
     };
   }
 
-  exposeForTest("createFilterFn", createFilterFn);
-
   async function runTests({
     exitOnFail = true,
     failFast = false,
@@ -372,7 +367,12 @@ finishing test case.`;
     return endMsg;
   }
 
-  exposeForTest("runTests", runTests);
+  window.__bootstrap.internals = {
+    ...window.__bootstrap.internals ?? {},
+    reportToConsole,
+    createFilterFn,
+    runTests,
+  };
 
   window.__bootstrap.testing = {
     test,
diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js
index 2c4decd32b..d96aaaaae5 100644
--- a/runtime/js/99_main.js
+++ b/runtime/js/99_main.js
@@ -20,7 +20,7 @@ delete Object.prototype.__proto__;
   const Console = window.__bootstrap.console.Console;
   const worker = window.__bootstrap.worker;
   const signals = window.__bootstrap.signals;
-  const { internalSymbol, internalObject } = window.__bootstrap.internals;
+  const internals = window.__bootstrap.internals;
   const performance = window.__bootstrap.performance;
   const crypto = window.__bootstrap.crypto;
   const url = window.__bootstrap.url;
@@ -429,10 +429,12 @@ delete Object.prototype.__proto__;
 
     registerErrors();
 
+    const internalSymbol = Symbol("Deno.internal");
+
     const finalDenoNs = {
       core,
       internal: internalSymbol,
-      [internalSymbol]: internalObject,
+      [internalSymbol]: internals,
       resources: core.resources,
       close: core.close,
       ...denoNs,
@@ -491,10 +493,12 @@ delete Object.prototype.__proto__;
     fetch.setBaseUrl(locationHref);
     registerErrors();
 
+    const internalSymbol = Symbol("Deno.internal");
+
     const finalDenoNs = {
       core,
       internal: internalSymbol,
-      [internalSymbol]: internalObject,
+      [internalSymbol]: internals,
       resources: core.resources,
       close: core.close,
       ...denoNs,
diff --git a/runtime/lib.rs b/runtime/lib.rs
index 1406372616..f4cf559d25 100644
--- a/runtime/lib.rs
+++ b/runtime/lib.rs
@@ -7,6 +7,7 @@ extern crate lazy_static;
 #[macro_use]
 extern crate log;
 
+pub use deno_console;
 pub use deno_crypto;
 pub use deno_fetch;
 pub use deno_url;