Skip to content

Commit

Permalink
Merge branch 'main' into issue/deno/#23805
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju authored May 27, 2024
2 parents 737382c + b2e84c0 commit b2f0602
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
9 changes: 4 additions & 5 deletions core/02_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
op_format_file_name,
op_apply_source_map,
op_apply_source_map_filename,
op_set_call_site_evals,
} = core.ops;
const {
Error,
ObjectDefineProperties,
ArrayPrototypePush,
StringPrototypeStartsWith,
StringPrototypeEndsWith,
Expand Down Expand Up @@ -127,9 +127,7 @@
} else {
stack = "";
}
ObjectDefineProperties(error, {
__callSiteEvals: { __proto__: null, value: [], configurable: true },
});
const callSiteEvals = [];
for (let i = 0; i < callSites.length; ++i) {
const v8CallSite = callSites[i];
const callSite = {
Expand Down Expand Up @@ -169,9 +167,10 @@
if (res >= 2) {
callSite.fileName = op_apply_source_map_filename();
}
ArrayPrototypePush(error.__callSiteEvals, callSite);
ArrayPrototypePush(callSiteEvals, callSite);
stack += `\n at ${formatCallSiteEval(callSite)}`;
}
op_set_call_site_evals(error, callSiteEvals);
return stack;
}

Expand Down
26 changes: 21 additions & 5 deletions core/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ pub fn to_v8_error<'a>(
}
}

pub(crate) fn call_site_evals_key<'a>(
scope: &mut v8::HandleScope<'a>,
) -> v8::Local<'a, v8::Private> {
const CALL_SITE_EVALS: v8::OneByteConst =
v8::String::create_external_onebyte_const(b"#callSiteEvals");
let name =
v8::String::new_from_onebyte_const(scope, &CALL_SITE_EVALS).unwrap();
v8::Private::for_api(scope, Some(name))
}

/// A `JsError` represents an exception coming from V8, with stack frames and
/// line numbers. The deno_cli crate defines another `JsError` type, which wraps
/// the one defined here, that adds source map support and colorful formatting.
Expand Down Expand Up @@ -430,14 +440,17 @@ impl JsError {
});

// Access error.stack to ensure that prepareStackTrace() has been called.
// This should populate error.__callSiteEvals.
// This should populate error.#callSiteEvals.
let stack = get_property(scope, exception, "stack");
let stack: Option<v8::Local<v8::String>> =
stack.and_then(|s| s.try_into().ok());
let stack = stack.map(|s| s.to_rust_string_lossy(scope));

// Read an array of structured frames from error.__callSiteEvals.
let frames_v8 = get_property(scope, exception, "__callSiteEvals");
// Read an array of structured frames from error.#callSiteEvals.
let frames_v8 = {
let key = call_site_evals_key(scope);
exception.get_private(scope, key)
};
// Ignore non-array values
let frames_v8: Option<v8::Local<v8::Array>> =
frames_v8.and_then(|a| a.try_into().ok());
Expand Down Expand Up @@ -669,9 +682,12 @@ pub(crate) fn has_call_site(
}
let exception = exception.to_object(scope).unwrap();
// Access error.stack to ensure that prepareStackTrace() has been called.
// This should populate error.__callSiteEvals.
// This should populate error.#callSiteEvals.
get_property(scope, exception, "stack");
let frames_v8 = get_property(scope, exception, "__callSiteEvals");
let frames_v8 = {
let key = call_site_evals_key(scope);
exception.get_private(scope, key)
};
let frames_v8: Option<v8::Local<v8::Array>> =
frames_v8.and_then(|a| a.try_into().ok());
if let Some(frames_v8) = frames_v8 {
Expand Down
1 change: 1 addition & 0 deletions core/ops_builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ builtin_ops! {
ops_builtin_v8::op_op_names,
ops_builtin_v8::op_apply_source_map,
ops_builtin_v8::op_apply_source_map_filename,
ops_builtin_v8::op_set_call_site_evals,
ops_builtin_v8::op_current_user_call_site,
ops_builtin_v8::op_set_format_exception_callback,
ops_builtin_v8::op_event_loop_has_more_work,
Expand Down
11 changes: 11 additions & 0 deletions core/ops_builtin_v8.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use crate::error::call_site_evals_key;
use crate::error::custom_error;
use crate::error::is_instance_of_error;
use crate::error::range_error;
Expand Down Expand Up @@ -1101,6 +1102,16 @@ pub fn op_apply_source_map_filename(
.ok_or_else(|| type_error("No stashed file name"))
}

#[op2]
pub fn op_set_call_site_evals(
scope: &mut v8::HandleScope,
exception: v8::Local<v8::Object>,
value: v8::Local<v8::Value>,
) {
let key = call_site_evals_key(scope);
assert!(exception.set_private(scope, key, value).unwrap())
}

#[op2]
#[string]
pub fn op_current_user_call_site(
Expand Down

0 comments on commit b2f0602

Please sign in to comment.