Skip to content

Commit

Permalink
fix: small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohe-Am committed Jan 3, 2025
1 parent ca54e31 commit 2fc7e41
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 84 deletions.
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion ports/npmi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
77 changes: 71 additions & 6 deletions src/ghjk/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -149,13 +148,79 @@ pub fn op_dispatch_exception2(
exception: v8::Local<v8::Value>,
) -> 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<v8::Value>) -> 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
)
}
75 changes: 2 additions & 73 deletions src/ghjk/ext/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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"),
));
}
Expand Down Expand Up @@ -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"),
)),
};
Expand Down Expand Up @@ -268,75 +266,6 @@ impl Callbacks {
}
}

fn js_error_message(scope: &mut v8::HandleScope, err: v8::Local<v8::Value>) -> 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<v8::Function>,
async_work_sender: deno_core::V8CrossThreadTaskSpawner,
Expand Down
2 changes: 1 addition & 1 deletion src/ghjk/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
4 changes: 2 additions & 2 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 2fc7e41

Please sign in to comment.