diff --git a/rewriter/Cargo.toml b/rewriter/Cargo.toml index 17170c5..d92c37d 100644 --- a/rewriter/Cargo.toml +++ b/rewriter/Cargo.toml @@ -6,6 +6,10 @@ edition = "2021" [lib] crate-type = ["cdylib"] +[features] +default = ["debug"] +debug = [] + [profile.speed] inherits = "release" opt-level = 3 diff --git a/rewriter/src/rewrite.rs b/rewriter/src/rewrite.rs index 00e4fea..8d7cd99 100644 --- a/rewriter/src/rewrite.rs +++ b/rewriter/src/rewrite.rs @@ -1,6 +1,9 @@ use oxc_allocator::Allocator; use oxc_ast::{ - ast::{AssignmentTarget, Expression, IdentifierReference, ObjectPropertyKind}, + ast::{ + AssignmentTarget, BindingPattern, BindingPatternKind, Expression, IdentifierReference, + ObjectPropertyKind, + }, visit::walk, Visit, }; @@ -144,6 +147,22 @@ impl<'a> Visit<'a> for Rewriter { // do not walk further, we don't want to rewrite the identifiers } + #[cfg(feature = "debug")] + fn visit_try_statement(&mut self, it: &oxc_ast::ast::TryStatement<'a>) { + // for debugging we need to know what the error was + + if let Some(h) = &it.handler { + if let Some(name) = &h.param { + if let Some(name) = name.pattern.get_identifier() { + self.jschanges.push(JsChange::GenericChange { + span: Span::new(h.body.span.start + 1, h.body.span.start + 1), + text: format!("$scramerr({});", name), + }); + } + } + } + } + fn visit_object_expression(&mut self, it: &oxc_ast::ast::ObjectExpression<'a>) { for prop in &it.properties { #[allow(clippy::single_match)] diff --git a/src/client/shared/function.ts b/src/client/shared/function.ts index 7b229cc..c85866f 100644 --- a/src/client/shared/function.ts +++ b/src/client/shared/function.ts @@ -2,21 +2,21 @@ import { ScramjetClient, ProxyCtx } from "../client"; import { rewriteJs } from "../shared"; function rewriteFunction(ctx: ProxyCtx) { - const stringifiedFunction = ctx.fn(...ctx.args).toString(); + const stringifiedFunction = ctx.fn(...ctx.args).toString(); - ctx.return(ctx.fn(`return ${rewriteJs(stringifiedFunction)}`)()); + ctx.return(ctx.fn(`return ${rewriteJs(stringifiedFunction)}`)()); } export default function (client: ScramjetClient, self: Self) { - client.Proxy("Function", { - apply(ctx) { - rewriteFunction(ctx); - }, + client.Proxy("Function", { + apply(ctx) { + rewriteFunction(ctx); + }, - construct(ctx) { - rewriteFunction(ctx); - }, - }); + construct(ctx) { + rewriteFunction(ctx); + }, + }); - Function.prototype.constructor = Function; -} \ No newline at end of file + Function.prototype.constructor = Function; +} diff --git a/src/client/shared/wrap.ts b/src/client/shared/wrap.ts index 763f7ac..00796f1 100644 --- a/src/client/shared/wrap.ts +++ b/src/client/shared/wrap.ts @@ -82,4 +82,8 @@ export default function (client: ScramjetClient, self: typeof globalThis) { break; } } + + window.$scramerr = function scramerr(e) { + console.error("CAUGHT ERROR", e); + }; }