mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 12:19:12 +00:00
refactor: add core.formatLocationFilename, remove op_format_filename (#14474)
This commit moves "op_format_location" to "core/ops_builtin.rs" and removes "Deno.core.createPrepareStackTrace" in favor of "Deno.core.prepareStackTrace". Co-authored-by: Aaron O'Mullan <aaron.omullan@gmail.com>
This commit is contained in:
parent
5ddb83a4c2
commit
3f08a40412
9 changed files with 89 additions and 104 deletions
6
.github/workflows/ci.yml
vendored
6
.github/workflows/ci.yml
vendored
|
@ -236,7 +236,7 @@ jobs:
|
||||||
~/.cargo/registry/index
|
~/.cargo/registry/index
|
||||||
~/.cargo/registry/cache
|
~/.cargo/registry/cache
|
||||||
~/.cargo/git/db
|
~/.cargo/git/db
|
||||||
key: 9-cargo-home-${{ matrix.os }}-${{ hashFiles('Cargo.lock') }}
|
key: 1-cargo-home-${{ matrix.os }}-${{ hashFiles('Cargo.lock') }}
|
||||||
|
|
||||||
# In main branch, always creates fresh cache
|
# In main branch, always creates fresh cache
|
||||||
- name: Cache build output (main)
|
- name: Cache build output (main)
|
||||||
|
@ -252,7 +252,7 @@ jobs:
|
||||||
!./target/*/*.zip
|
!./target/*/*.zip
|
||||||
!./target/*/*.tar.gz
|
!./target/*/*.tar.gz
|
||||||
key: |
|
key: |
|
||||||
9-cargo-target-${{ matrix.os }}-${{ matrix.profile }}-${{ github.sha }}
|
1-cargo-target-${{ matrix.os }}-${{ matrix.profile }}-${{ github.sha }}
|
||||||
|
|
||||||
# Restore cache from the latest 'main' branch build.
|
# Restore cache from the latest 'main' branch build.
|
||||||
- name: Cache build output (PR)
|
- name: Cache build output (PR)
|
||||||
|
@ -268,7 +268,7 @@ jobs:
|
||||||
!./target/*/*.tar.gz
|
!./target/*/*.tar.gz
|
||||||
key: never_saved
|
key: never_saved
|
||||||
restore-keys: |
|
restore-keys: |
|
||||||
9-cargo-target-${{ matrix.os }}-${{ matrix.profile }}-
|
1-cargo-target-${{ matrix.os }}-${{ matrix.profile }}-
|
||||||
|
|
||||||
# Don't save cache after building PRs or branches other than 'main'.
|
# Don't save cache after building PRs or branches other than 'main'.
|
||||||
- name: Skip save cache (PR)
|
- name: Skip save cache (PR)
|
||||||
|
|
|
@ -4,36 +4,11 @@ use crate::colors::cyan;
|
||||||
use crate::colors::italic_bold;
|
use crate::colors::italic_bold;
|
||||||
use crate::colors::red;
|
use crate::colors::red;
|
||||||
use crate::colors::yellow;
|
use crate::colors::yellow;
|
||||||
use deno_core::error::{JsError, JsStackFrame};
|
use deno_core::error::format_file_name;
|
||||||
use deno_core::url::Url;
|
use deno_core::error::JsError;
|
||||||
|
use deno_core::error::JsStackFrame;
|
||||||
|
|
||||||
const SOURCE_ABBREV_THRESHOLD: usize = 150;
|
const SOURCE_ABBREV_THRESHOLD: usize = 150;
|
||||||
const DATA_URL_ABBREV_THRESHOLD: usize = 150;
|
|
||||||
|
|
||||||
pub fn format_file_name(file_name: &str) -> String {
|
|
||||||
if file_name.len() > DATA_URL_ABBREV_THRESHOLD {
|
|
||||||
if let Ok(url) = Url::parse(file_name) {
|
|
||||||
if url.scheme() == "data" {
|
|
||||||
let data_path = url.path();
|
|
||||||
if let Some(data_pieces) = data_path.split_once(',') {
|
|
||||||
let data_length = data_pieces.1.len();
|
|
||||||
if let Some(data_start) = data_pieces.1.get(0..20) {
|
|
||||||
if let Some(data_end) = data_pieces.1.get(data_length - 20..) {
|
|
||||||
return format!(
|
|
||||||
"{}:{},{}......{}",
|
|
||||||
url.scheme(),
|
|
||||||
data_pieces.0,
|
|
||||||
data_start,
|
|
||||||
data_end
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
file_name.to_string()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keep in sync with `/core/error.js`.
|
// Keep in sync with `/core/error.js`.
|
||||||
pub fn format_location(frame: &JsStackFrame) -> String {
|
pub fn format_location(frame: &JsStackFrame) -> String {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
use crate::diagnostics::Diagnostics;
|
use crate::diagnostics::Diagnostics;
|
||||||
use crate::fmt_errors::format_file_name;
|
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use deno_core::op;
|
use deno_core::op;
|
||||||
use deno_core::serde_json;
|
use deno_core::serde_json;
|
||||||
|
@ -11,10 +10,7 @@ use deno_core::Extension;
|
||||||
|
|
||||||
pub fn init() -> Extension {
|
pub fn init() -> Extension {
|
||||||
Extension::builder()
|
Extension::builder()
|
||||||
.ops(vec![
|
.ops(vec![op_format_diagnostic::decl()])
|
||||||
op_format_diagnostic::decl(),
|
|
||||||
op_format_file_name::decl(),
|
|
||||||
])
|
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,8 +19,3 @@ fn op_format_diagnostic(args: Value) -> Result<Value, AnyError> {
|
||||||
let diagnostic: Diagnostics = serde_json::from_value(args)?;
|
let diagnostic: Diagnostics = serde_json::from_value(args)?;
|
||||||
Ok(json!(diagnostic.to_string()))
|
Ok(json!(diagnostic.to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[op]
|
|
||||||
fn op_format_file_name(file_name: String) -> Result<String, AnyError> {
|
|
||||||
Ok(format_file_name(&file_name))
|
|
||||||
}
|
|
||||||
|
|
|
@ -71,7 +71,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep in sync with `cli/fmt_errors.rs`.
|
// Keep in sync with `cli/fmt_errors.rs`.
|
||||||
function formatLocation(callSite, formatFileNameFn) {
|
function formatLocation(callSite) {
|
||||||
if (callSite.isNative()) {
|
if (callSite.isNative()) {
|
||||||
return "native";
|
return "native";
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@
|
||||||
const fileName = callSite.getFileName();
|
const fileName = callSite.getFileName();
|
||||||
|
|
||||||
if (fileName) {
|
if (fileName) {
|
||||||
result += formatFileNameFn(fileName);
|
result += core.opSync("op_format_file_name", fileName);
|
||||||
} else {
|
} else {
|
||||||
if (callSite.isEval()) {
|
if (callSite.isEval()) {
|
||||||
const evalOrigin = callSite.getEvalOrigin();
|
const evalOrigin = callSite.getEvalOrigin();
|
||||||
|
@ -107,7 +107,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep in sync with `cli/fmt_errors.rs`.
|
// Keep in sync with `cli/fmt_errors.rs`.
|
||||||
function formatCallSite(callSite, formatFileNameFn) {
|
function formatCallSite(callSite) {
|
||||||
let result = "";
|
let result = "";
|
||||||
const functionName = callSite.getFunctionName();
|
const functionName = callSite.getFunctionName();
|
||||||
|
|
||||||
|
@ -160,11 +160,11 @@
|
||||||
} else if (functionName) {
|
} else if (functionName) {
|
||||||
result += functionName;
|
result += functionName;
|
||||||
} else {
|
} else {
|
||||||
result += formatLocation(callSite, formatFileNameFn);
|
result += formatLocation(callSite);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
result += ` (${formatLocation(callSite, formatFileNameFn)})`;
|
result += ` (${formatLocation(callSite)})`;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,9 +189,8 @@
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns a function that can be used as `Error.prepareStackTrace`. */
|
/** A function that can be used as `Error.prepareStackTrace`. */
|
||||||
function createPrepareStackTrace(formatFileNameFn) {
|
function prepareStackTrace(
|
||||||
return function prepareStackTrace(
|
|
||||||
error,
|
error,
|
||||||
callSites,
|
callSites,
|
||||||
) {
|
) {
|
||||||
|
@ -219,7 +218,7 @@
|
||||||
ArrayPrototypePush(error.__callSiteEvals, evaluateCallSite(callSite));
|
ArrayPrototypePush(error.__callSiteEvals, evaluateCallSite(callSite));
|
||||||
ArrayPrototypePush(
|
ArrayPrototypePush(
|
||||||
formattedCallSites,
|
formattedCallSites,
|
||||||
formatCallSite(callSite, formatFileNameFn),
|
formatCallSite(callSite),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const message = error.message !== undefined ? error.message : "";
|
const message = error.message !== undefined ? error.message : "";
|
||||||
|
@ -237,9 +236,8 @@
|
||||||
ArrayPrototypeMap(formattedCallSites, (s) => `\n at ${s}`),
|
ArrayPrototypeMap(formattedCallSites, (s) => `\n at ${s}`),
|
||||||
"",
|
"",
|
||||||
);
|
);
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ObjectAssign(globalThis.__bootstrap.core, { createPrepareStackTrace });
|
ObjectAssign(globalThis.__bootstrap.core, { prepareStackTrace });
|
||||||
ObjectFreeze(globalThis.__bootstrap.core);
|
ObjectFreeze(globalThis.__bootstrap.core);
|
||||||
})(this);
|
})(this);
|
||||||
|
|
|
@ -243,6 +243,7 @@ pub fn initialize_context<'s>(
|
||||||
set_func(scope, core_val, "destructureError", destructure_error);
|
set_func(scope, core_val, "destructureError", destructure_error);
|
||||||
set_func(scope, core_val, "terminate", terminate);
|
set_func(scope, core_val, "terminate", terminate);
|
||||||
set_func(scope, core_val, "applySourceMap", apply_source_map);
|
set_func(scope, core_val, "applySourceMap", apply_source_map);
|
||||||
|
|
||||||
// Direct bindings on `window`.
|
// Direct bindings on `window`.
|
||||||
set_func(scope, global, "queueMicrotask", queue_microtask);
|
set_func(scope, global, "queueMicrotask", queue_microtask);
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use crate::runtime::JsRuntime;
|
use crate::runtime::JsRuntime;
|
||||||
use crate::source_map::apply_source_map;
|
use crate::source_map::apply_source_map;
|
||||||
|
use crate::url::Url;
|
||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
@ -431,6 +432,27 @@ pub(crate) fn is_instance_of_error<'s>(
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const DATA_URL_ABBREV_THRESHOLD: usize = 150;
|
||||||
|
|
||||||
|
pub fn format_file_name(file_name: &str) -> String {
|
||||||
|
abbrev_file_name(file_name).unwrap_or_else(|| file_name.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn abbrev_file_name(file_name: &str) -> Option<String> {
|
||||||
|
if file_name.len() <= DATA_URL_ABBREV_THRESHOLD {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let url = Url::parse(file_name).ok()?;
|
||||||
|
if url.scheme() != "data" {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let (head, tail) = url.path().split_once(',')?;
|
||||||
|
let len = tail.len();
|
||||||
|
let start = tail.get(0..20)?;
|
||||||
|
let end = tail.get(len - 20..)?;
|
||||||
|
Some(format!("{}:{},{}......{}", url.scheme(), head, start, end))
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::error::format_file_name;
|
||||||
use crate::error::type_error;
|
use crate::error::type_error;
|
||||||
use crate::include_js_files;
|
use crate::include_js_files;
|
||||||
use crate::ops_metrics::OpMetrics;
|
use crate::ops_metrics::OpMetrics;
|
||||||
|
@ -34,6 +35,7 @@ pub(crate) fn init_builtins() -> Extension {
|
||||||
op_write::decl(),
|
op_write::decl(),
|
||||||
op_shutdown::decl(),
|
op_shutdown::decl(),
|
||||||
op_metrics::decl(),
|
op_metrics::decl(),
|
||||||
|
op_format_file_name::decl(),
|
||||||
])
|
])
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
@ -183,3 +185,8 @@ async fn op_shutdown(
|
||||||
let resource = state.borrow().resource_table.get_any(rid)?;
|
let resource = state.borrow().resource_table.get_any(rid)?;
|
||||||
resource.shutdown().await
|
resource.shutdown().await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[op]
|
||||||
|
fn op_format_file_name(file_name: String) -> Result<String, Error> {
|
||||||
|
Ok(format_file_name(&file_name))
|
||||||
|
}
|
||||||
|
|
|
@ -8,12 +8,7 @@
|
||||||
return core.opSync("op_format_diagnostic", diagnostics);
|
return core.opSync("op_format_diagnostic", diagnostics);
|
||||||
}
|
}
|
||||||
|
|
||||||
function opFormatFileName(location) {
|
|
||||||
return core.opSync("op_format_file_name", location);
|
|
||||||
}
|
|
||||||
|
|
||||||
window.__bootstrap.errorStack = {
|
window.__bootstrap.errorStack = {
|
||||||
opFormatDiagnostics,
|
opFormatDiagnostics,
|
||||||
opFormatFileName,
|
|
||||||
};
|
};
|
||||||
})(this);
|
})(this);
|
||||||
|
|
|
@ -32,7 +32,6 @@ delete Object.prototype.__proto__;
|
||||||
const location = window.__bootstrap.location;
|
const location = window.__bootstrap.location;
|
||||||
const build = window.__bootstrap.build;
|
const build = window.__bootstrap.build;
|
||||||
const version = window.__bootstrap.version;
|
const version = window.__bootstrap.version;
|
||||||
const errorStack = window.__bootstrap.errorStack;
|
|
||||||
const os = window.__bootstrap.os;
|
const os = window.__bootstrap.os;
|
||||||
const timers = window.__bootstrap.timers;
|
const timers = window.__bootstrap.timers;
|
||||||
const base64 = window.__bootstrap.base64;
|
const base64 = window.__bootstrap.base64;
|
||||||
|
@ -221,11 +220,8 @@ delete Object.prototype.__proto__;
|
||||||
);
|
);
|
||||||
build.setBuildInfo(runtimeOptions.target);
|
build.setBuildInfo(runtimeOptions.target);
|
||||||
util.setLogDebug(runtimeOptions.debugFlag, source);
|
util.setLogDebug(runtimeOptions.debugFlag, source);
|
||||||
const prepareStackTrace = core.createPrepareStackTrace(
|
|
||||||
errorStack.opFormatFileName,
|
|
||||||
);
|
|
||||||
// deno-lint-ignore prefer-primordials
|
// deno-lint-ignore prefer-primordials
|
||||||
Error.prepareStackTrace = prepareStackTrace;
|
Error.prepareStackTrace = core.prepareStackTrace;
|
||||||
}
|
}
|
||||||
|
|
||||||
function registerErrors() {
|
function registerErrors() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue