Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 84 additions & 78 deletions instrumentation-wasm/Cargo.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions instrumentation-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ name = "node_code_instrumentation"
crate-type = ["cdylib", "rlib"]

[dependencies]
oxc_allocator = "0.123.0"
oxc_ast = "0.123.0"
oxc_codegen = "0.123.0"
oxc_parser = "0.123.0"
oxc_semantic = "0.123.0"
oxc_span = "0.123.0"
oxc_traverse = "0.123.0"
oxc_allocator = "0.127.0"
oxc_ast = "0.127.0"
oxc_codegen = "0.127.0"
oxc_parser = "0.127.0"
oxc_semantic = "0.127.0"
oxc_span = "0.127.0"
oxc_traverse = "0.127.0"
serde = "1.0.228"
serde_json = "1.0.149"
wasm-bindgen = "0.2.108"
wasm-bindgen = "0.2.118"

[profile.release]
strip = true
Expand Down
21 changes: 17 additions & 4 deletions instrumentation-wasm/src/js_transformer/helpers/insert_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ use oxc_ast::{
AstBuilder, NONE,
ast::{
Argument, ArrayExpressionElement, AssignmentOperator, AssignmentTarget, Expression,
FunctionBody, Statement,
FunctionBody, Statement, UnaryOperator,
},
};
use oxc_span::SPAN;

// Add a statement to the beginning of the function: __instrumentInspectArgs('function_identifier', arguments, "{pkg_version}", this);
// In case of callback_on_block being true, we add an if statement that checks the result of the callback and returns early if the callback returns false:
// if (!__instrumentInspectArgs('function_identifier', arguments, "{pkg_version}", this)) return;
pub fn insert_inspect_args<'a>(
allocator: &'a Allocator,
builder: &'a AstBuilder,
identifier: &str,
pkg_version: &'a str,
body: &mut Box<'a, FunctionBody<'a>>,
is_constructor: bool,
callback_on_block: bool,
) {
let mut inspect_args: OxcVec<'a, Argument<'a>> = builder.vec_with_capacity(4);

Expand Down Expand Up @@ -48,10 +51,20 @@ pub fn insert_inspect_args<'a>(
false,
);

let stmt_expression = builder.statement_expression(SPAN, call_expr);

let insert_pos = get_insert_pos(body, is_constructor);
body.statements.insert(insert_pos, stmt_expression);

if !callback_on_block {
let stmt_expression = builder.statement_expression(SPAN, call_expr);

body.statements.insert(insert_pos, stmt_expression);
return;
}

let return_stmt = builder.statement_return(SPAN, None);
let test = builder.expression_unary(SPAN, UnaryOperator::LogicalNot, call_expr);
let if_stmt = builder.statement_if(SPAN, test, return_stmt, None);

body.statements.insert(insert_pos, if_stmt);
}

// Modify the arguments by adding a statement to the beginning of the function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub fn insert_instrument_method_calls<'a>(
pkg_version,
body,
is_constructor,
instruction.callback_on_block,
);
}

Expand Down
1 change: 1 addition & 0 deletions instrumentation-wasm/src/js_transformer/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ pub struct FunctionInstructions {
pub modify_return_value: bool,
pub modify_arguments_object: bool,
pub class_name: Option<String>,
pub callback_on_block: bool,
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ t.test("Benchmark: Small code transformation", async (t) => {
modifyArgs: false,
modifyReturnValue: false,
modifyArgumentsObject: false,
callbackOnBlock: false,
},
],
accessLocalVariables: [],
Expand Down Expand Up @@ -96,6 +97,7 @@ t.test("Benchmark: Large code transformation", async (t) => {
modifyArgs: false,
modifyReturnValue: false,
modifyArgumentsObject: false,
callbackOnBlock: false,
},
],
accessLocalVariables: [],
Expand Down
Loading
Loading