From 2fc7e4100cfa532d3692b14e557fe2de8915bbde Mon Sep 17 00:00:00 2001 From: Yohe-Am <56622350+Yohe-Am@users.noreply.github.com> Date: Fri, 3 Jan 2025 03:39:04 +0300 Subject: [PATCH] fix: small fixes --- mod.ts | 2 +- ports/npmi.ts | 2 +- src/ghjk/ext.rs | 77 ++++++++++++++++++++++++++++++++++++--- src/ghjk/ext/callbacks.rs | 75 +------------------------------------- src/ghjk/main.rs | 2 +- tests/utils.ts | 4 +- 6 files changed, 78 insertions(+), 84 deletions(-) diff --git a/mod.ts b/mod.ts index 58ee37e..681b237 100644 --- a/mod.ts +++ b/mod.ts @@ -1,4 +1,4 @@ -//! This module is intended to be re-exported by `ghjk.ts` config scripts. +//! The primary import used by `ghjk.ts` ghjkfiles. // TODO: harden most of the items in here diff --git a/ports/npmi.ts b/ports/npmi.ts index fa61b99..22b6cce 100644 --- a/ports/npmi.ts +++ b/ports/npmi.ts @@ -85,7 +85,7 @@ export class Port extends PortBase { const conf = confValidator.parse(args.config); await $.raw`${ depExecShimPath(std_ports.node_org, "npm", args.depArts) - } install --no-fund ${conf.packageName}@${args.installVersion}` + } install --prefix ${args.tmpDirPath} --no-fund ${conf.packageName}@${args.installVersion}` .cwd(args.tmpDirPath) .env(pathsWithDepArts(args.depArts, args.platform.os)); await std_fs.move(args.tmpDirPath, args.downloadPath); diff --git a/src/ghjk/ext.rs b/src/ghjk/ext.rs index 6f79729..e46ee33 100644 --- a/src/ghjk/ext.rs +++ b/src/ghjk/ext.rs @@ -2,7 +2,6 @@ use crate::interlude::*; use std::{cell::RefCell, rc::Rc}; -use deno_core::v8; use deno_core::OpState; use tokio::sync::mpsc; #[rustfmt::skip] @@ -149,13 +148,79 @@ pub fn op_dispatch_exception2( exception: v8::Local, ) -> bool { if let Some(tx) = &ctx.config.exception_tx { - tx.send(ferr!( - "unhandledrejection: {}", - exception.to_rust_string_lossy(scope) - )) - .expect_or_log("channel error"); + tx.send(ferr!(js_error_message(scope, exception)).wrap_err("unhandledrejection")) + .expect_or_log("channel error"); true } else { false } } + +fn js_error_message(scope: &mut v8::HandleScope, err: v8::Local) -> String { + let Some(obj) = err.to_object(scope) else { + return err.to_rust_string_lossy(scope); + }; + let evt_err_class = { + let name = v8::String::new(scope, "ErrorEvent") + .expect_or_log("v8 error") + .into(); + scope + .get_current_context() + // classes are stored on the global obj + .global(scope) + .get(scope, name) + .expect_or_log("v8 error") + .to_object(scope) + .expect_or_log("v8 error") + }; + if !obj + .instance_of(scope, evt_err_class) + .expect_or_log("v8 error") + { + for key in &["stack", "message"] { + let key = v8::String::new(scope, key).expect_or_log("v8 error"); + if let Some(inner) = obj.get(scope, key.into()) { + if inner.boolean_value(scope) { + return inner.to_rust_string_lossy(scope); + } + } + } + return err.to_rust_string_lossy(scope); + } + // ErrorEvents are recieved here for some reason + // https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent + { + // if it has an error value attached, prefer that + let key = v8::String::new(scope, "error") + .expect_or_log("v8 error") + .into(); + if let Some(inner) = obj.get(scope, key) { + // check if it's not null or undefined + if inner.boolean_value(scope) { + // stack messages are preferred if it has one + let Some(inner) = inner.to_object(scope) else { + return inner.to_rust_string_lossy(scope); + }; + let key = v8::String::new(scope, "stack").expect_or_log("v8 error"); + if let Some(stack) = inner.get(scope, key.into()) { + if stack.boolean_value(scope) { + return stack.to_rust_string_lossy(scope); + } + } + return inner.to_rust_string_lossy(scope); + } + } + } + #[derive(Deserialize)] + struct ErrorEvt { + lineno: i64, + colno: i64, + filename: String, + message: String, + } + let evt: ErrorEvt = serde_v8::from_v8(scope, err).unwrap(); + format!( + "{} ({}:{}:{})", + evt.message, evt.filename, evt.lineno, evt.colno + ) +} diff --git a/src/ghjk/ext/callbacks.rs b/src/ghjk/ext/callbacks.rs index 804a28e..4a319fe 100644 --- a/src/ghjk/ext/callbacks.rs +++ b/src/ghjk/ext/callbacks.rs @@ -2,8 +2,6 @@ use std::{cell::RefCell, rc::Rc}; use crate::interlude::*; -use deno_core::serde_v8; -use deno_core::v8; use deno_core::OpState; // necessary for re-exported macros to work #[rustfmt::skip] @@ -205,7 +203,7 @@ impl Callbacks { if tc_scope.has_caught() { let exception = tc_scope.exception().unwrap(); return Err(CallbackError::JsError( - ferr!(js_error_message(tc_scope, exception)) + ferr!(super::js_error_message(tc_scope, exception)) .wrap_err("callback exception"), )); } @@ -233,7 +231,7 @@ impl Callbacks { // for private deno_core functionality as discussed at // https://github.com/denoland/deno/discussions/27504 Err(err) => Err(CallbackError::JsError( - ferr!(js_error_message(scope, err)) + ferr!(super::js_error_message(scope, err)) .wrap_err("callback promise rejection"), )), }; @@ -268,75 +266,6 @@ impl Callbacks { } } -fn js_error_message(scope: &mut v8::HandleScope, err: v8::Local) -> String { - let Some(obj) = err.to_object(scope) else { - return err.to_rust_string_lossy(scope); - }; - let evt_err_class = { - let name = v8::String::new(scope, "ErrorEvent") - .expect_or_log("v8 error") - .into(); - scope - .get_current_context() - // classes are stored on the global obj - .global(scope) - .get(scope, name) - .expect_or_log("v8 error") - .to_object(scope) - .expect_or_log("v8 error") - }; - if !obj - .instance_of(scope, evt_err_class) - .expect_or_log("v8 error") - { - for key in &["stack", "message"] { - let key = v8::String::new(scope, key).expect_or_log("v8 error"); - if let Some(inner) = obj.get(scope, key.into()) { - if inner.boolean_value(scope) { - return inner.to_rust_string_lossy(scope); - } - } - } - return err.to_rust_string_lossy(scope); - } - // ErrorEvents are recieved here for some reason - // https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent - { - // if it has an error value attached, prefer that - let key = v8::String::new(scope, "error") - .expect_or_log("v8 error") - .into(); - if let Some(inner) = obj.get(scope, key) { - // check if it's not null or undefined - if inner.boolean_value(scope) { - // stack messages are preferred if it has one - let Some(inner) = inner.to_object(scope) else { - return inner.to_rust_string_lossy(scope); - }; - let key = v8::String::new(scope, "stack").expect_or_log("v8 error"); - if let Some(stack) = inner.get(scope, key.into()) { - if stack.boolean_value(scope) { - return stack.to_rust_string_lossy(scope); - } - } - return inner.to_rust_string_lossy(scope); - } - } - } - #[derive(Deserialize)] - struct ErrorEvt { - lineno: i64, - colno: i64, - filename: String, - message: String, - } - let evt: ErrorEvt = serde_v8::from_v8(scope, err).unwrap(); - format!( - "{} ({}:{}:{})", - evt.message, evt.filename, evt.lineno, evt.colno - ) -} - struct Callback { js_fn: SendPtr, async_work_sender: deno_core::V8CrossThreadTaskSpawner, diff --git a/src/ghjk/main.rs b/src/ghjk/main.rs index 95c41ad..f8324dd 100644 --- a/src/ghjk/main.rs +++ b/src/ghjk/main.rs @@ -13,7 +13,7 @@ mod interlude { self, deno_runtime::{ self, - deno_core::{self, url}, + deno_core::{self, serde_v8, url, v8}, }, }; pub use eyre::{format_err as ferr, Context, Result as Res, WrapErr}; diff --git a/tests/utils.ts b/tests/utils.ts index 91d434d..bc3e023 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -81,10 +81,10 @@ exec ${ghjkExePath.resolve().toString()} "$@"`, shellsToHook: [], }); - /* await $`ghjk print config` + await $`ghjk print config` .cwd(tmpDir.toString()) .clearEnv() - .env(env); */ + .env(env); await $`ghjk envs cook` .cwd(tmpDir.toString()) .clearEnv()