add try statement catching

This commit is contained in:
velzie 2024-07-29 14:05:28 -04:00
parent ca1574fccc
commit 58f6a4330a
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F
4 changed files with 40 additions and 13 deletions

View file

@ -6,6 +6,10 @@ edition = "2021"
[lib] [lib]
crate-type = ["cdylib"] crate-type = ["cdylib"]
[features]
default = ["debug"]
debug = []
[profile.speed] [profile.speed]
inherits = "release" inherits = "release"
opt-level = 3 opt-level = 3

View file

@ -1,6 +1,9 @@
use oxc_allocator::Allocator; use oxc_allocator::Allocator;
use oxc_ast::{ use oxc_ast::{
ast::{AssignmentTarget, Expression, IdentifierReference, ObjectPropertyKind}, ast::{
AssignmentTarget, BindingPattern, BindingPatternKind, Expression, IdentifierReference,
ObjectPropertyKind,
},
visit::walk, visit::walk,
Visit, Visit,
}; };
@ -144,6 +147,22 @@ impl<'a> Visit<'a> for Rewriter {
// do not walk further, we don't want to rewrite the identifiers // 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>) { fn visit_object_expression(&mut self, it: &oxc_ast::ast::ObjectExpression<'a>) {
for prop in &it.properties { for prop in &it.properties {
#[allow(clippy::single_match)] #[allow(clippy::single_match)]

View file

@ -2,21 +2,21 @@ import { ScramjetClient, ProxyCtx } from "../client";
import { rewriteJs } from "../shared"; import { rewriteJs } from "../shared";
function rewriteFunction(ctx: ProxyCtx) { 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) { export default function (client: ScramjetClient, self: Self) {
client.Proxy("Function", { client.Proxy("Function", {
apply(ctx) { apply(ctx) {
rewriteFunction(ctx); rewriteFunction(ctx);
}, },
construct(ctx) { construct(ctx) {
rewriteFunction(ctx); rewriteFunction(ctx);
}, },
}); });
Function.prototype.constructor = Function; Function.prototype.constructor = Function;
} }

View file

@ -82,4 +82,8 @@ export default function (client: ScramjetClient, self: typeof globalThis) {
break; break;
} }
} }
window.$scramerr = function scramerr(e) {
console.error("CAUGHT ERROR", e);
};
} }