Skip to content

Commit 6a35ec5

Browse files
committed
Instrument xml2js, bug fixes, fix linter warnings
1 parent 3bd0277 commit 6a35ec5

File tree

12 files changed

+53
-48
lines changed

12 files changed

+53
-48
lines changed

instrumentation-wasm/src/js_transformer/helpers/get_name_str_for_member_expr.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@ pub fn get_name_str_for_member_expr<'a>(
55
allocator: &'a Allocator,
66
member_expr: &MemberExpression,
77
) -> Option<&'a str> {
8-
let obj_name_str: &str;
9-
let prop_name_str: &str;
10-
11-
match member_expr {
8+
let prop_name_str = match member_expr {
129
MemberExpression::StaticMemberExpression(static_member_expr) => {
13-
prop_name_str = static_member_expr.property.name.as_str()
10+
static_member_expr.property.name.as_str()
1411
}
1512
MemberExpression::ComputedMemberExpression(computed_member_expr) => {
1613
match &computed_member_expr.expression {
17-
Expression::Identifier(identifier_ref) => {
18-
prop_name_str = identifier_ref.name.as_str();
19-
}
14+
Expression::Identifier(identifier_ref) => identifier_ref.name.as_str(),
2015
_ => {
2116
// Unsupported AST type
2217
return None;
@@ -27,17 +22,15 @@ pub fn get_name_str_for_member_expr<'a>(
2722
// Unsupported AST type
2823
return None;
2924
}
30-
}
25+
};
3126

32-
match member_expr.object() {
33-
Expression::Identifier(identifier_ref) => {
34-
obj_name_str = identifier_ref.name.as_str();
35-
}
27+
let obj_name_str = match member_expr.object() {
28+
Expression::Identifier(identifier_ref) => identifier_ref.name.as_str(),
3629
_ => {
3730
// Unsupported AST type
3831
return None;
3932
}
40-
}
33+
};
4134

4235
if obj_name_str.is_empty() || prop_name_str.is_empty() {
4336
return None;
@@ -47,5 +40,5 @@ pub fn get_name_str_for_member_expr<'a>(
4740
return Some(allocator.alloc_str(&format!("{}[{}]", obj_name_str, prop_name_str)));
4841
}
4942

50-
Some(&allocator.alloc_str(&format!("{}.{}", obj_name_str, prop_name_str)))
43+
Some(allocator.alloc_str(&format!("{}.{}", obj_name_str, prop_name_str)))
5144
}

instrumentation-wasm/src/js_transformer/helpers/insert_code.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use oxc_allocator::{Allocator, Box, Vec as OxcVec};
22
use oxc_ast::{
3+
AstBuilder, NONE,
34
ast::{
45
Argument, ArrayExpressionElement, AssignmentOperator, AssignmentTarget, Expression,
56
FunctionBody,
67
},
7-
AstBuilder, NONE,
88
};
99
use oxc_span::SPAN;
1010

instrumentation-wasm/src/js_transformer/helpers/insert_import_statement.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use oxc_allocator::{Allocator, Vec as OxcVec};
22
use oxc_ast::{
3+
AstBuilder, NONE,
34
ast::{
45
Argument, BindingPatternKind, BindingProperty, ImportDeclarationSpecifier,
56
ImportOrExportKind, Statement, VariableDeclarationKind,
67
},
7-
AstBuilder, NONE,
88
};
9-
use oxc_span::{SourceType, SPAN};
9+
use oxc_span::{SPAN, SourceType};
1010

1111
use crate::js_transformer::instructions::{FileInstructions, FunctionInstructions};
1212

@@ -15,7 +15,9 @@ const INSTRUMENT_INSPECT_ARGS_METHOD_NAME: &str = "__instrumentInspectArgs";
1515
const INSTRUMENT_MODIFY_ARGS_METHOD_NAME: &str = "__instrumentModifyArgs";
1616
const INSTRUMENT_MODIFY_RETURN_VALUE_METHOD_NAME: &str = "__instrumentModifyReturnValue";
1717

18-
const IMPORT_METHODS: [(&str, fn(&FunctionInstructions) -> bool); 3] = [
18+
type ImportMethodPredicate = fn(&FunctionInstructions) -> bool;
19+
20+
const IMPORT_METHODS: [(&str, ImportMethodPredicate); 3] = [
1921
(
2022
INSTRUMENT_INSPECT_ARGS_METHOD_NAME,
2123
|f: &FunctionInstructions| f.inspect_args,
@@ -51,7 +53,7 @@ pub fn insert_import_statement<'a>(
5153

5254
for (method_name, predicate) in IMPORT_METHODS.iter() {
5355
// Only import the function if it is used in the file
54-
if file_instructions.functions.iter().any(|f| predicate(f)) {
56+
if file_instructions.functions.iter().any(predicate) {
5557
binding_properties.push(builder.binding_property(
5658
SPAN,
5759
builder.property_key_static_identifier(SPAN, *method_name),
@@ -109,7 +111,7 @@ pub fn insert_import_statement<'a>(
109111

110112
let mut specifiers: OxcVec<'a, ImportDeclarationSpecifier<'a>> = builder.vec_with_capacity(3);
111113
for (method_name, predicate) in IMPORT_METHODS.iter() {
112-
if file_instructions.functions.iter().any(|f| predicate(f)) {
114+
if file_instructions.functions.iter().any(predicate) {
113115
specifiers.push(builder.import_declaration_specifier_import_specifier(
114116
SPAN,
115117
builder.module_export_name_identifier_name(SPAN, *method_name),

instrumentation-wasm/src/js_transformer/helpers/insert_instrument_method_calls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn insert_instrument_method_calls<'a>(
2222
allocator,
2323
builder,
2424
&instruction.identifier,
25-
&arg_names,
25+
arg_names,
2626
body,
2727
instruction.modify_arguments_object,
2828
);

instrumentation-wasm/src/js_transformer/helpers/transform_return_statements.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use oxc_allocator::{Allocator, Box, Vec as OxcVec};
22
use oxc_ast::{
3-
ast::{Argument, FunctionBody, Statement},
43
AstBuilder, NONE,
4+
ast::{Argument, FunctionBody, Statement},
55
};
66
use oxc_span::SPAN;
77

@@ -15,14 +15,18 @@ pub fn transform_return_statements<'a>(
1515
for statement in &mut body.statements {
1616
match statement {
1717
Statement::ReturnStatement(return_stmt) => {
18-
let mut instrument_args: OxcVec<'a, Argument<'a>> = builder.vec_with_capacity(2);
18+
let mut instrument_args: OxcVec<'a, Argument<'a>> = builder.vec_with_capacity(3);
1919

2020
// Add the identifier to the arguments
2121
instrument_args.push(Argument::StringLiteral(builder.alloc_string_literal(
2222
SPAN,
2323
allocator.alloc_str(identifier),
2424
None,
2525
)));
26+
27+
// Also pass the function arguments
28+
instrument_args.push(builder.expression_identifier(SPAN, "arguments").into());
29+
2630
// Add original return value as the second argument
2731
let arg_expr = return_stmt
2832
.argument

instrumentation-wasm/src/js_transformer/transformer.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ pub fn transform_code_str(
2828

2929
let source_type = select_sourcetype_based_on_enum(src_type);
3030

31-
let parser_result = Parser::new(&allocator, &code, source_type).parse();
31+
let parser_result = Parser::new(&allocator, code, source_type).parse();
3232

33-
if parser_result.panicked || parser_result.errors.len() > 0 {
33+
if parser_result.panicked || !parser_result.errors.is_empty() {
3434
return format!("#ERR: {:?}", parser_result.errors);
3535
}
3636

3737
let program = allocator.alloc(parser_result.program);
3838

3939
// 2 Semantic Analyze
40-
let semantic = SemanticBuilder::new().build(&program);
40+
let semantic = SemanticBuilder::new().build(program);
4141

4242
if !semantic.errors.is_empty() {
4343
return format!("#ERR: {:?}", semantic.errors);
@@ -50,7 +50,7 @@ pub fn transform_code_str(
5050
let t = &mut Transformer {
5151
allocator: &allocator,
5252
file_instructions: &file_instructions,
53-
pkg_version: &pkg_version,
53+
pkg_version,
5454
ast_builder: &ast_builder,
5555
};
5656

@@ -74,7 +74,7 @@ pub fn transform_code_str(
7474
// Todo add source map using source_map_path
7575
..CodegenOptions::default()
7676
})
77-
.build(&program);
77+
.build(program);
7878

7979
js.code
8080
}

instrumentation-wasm/src/js_transformer/transformer_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl<'a> Traverse<'a> for Transformer<'a> {
2222
node: &mut MethodDefinition<'a>,
2323
_ctx: &mut TraverseCtx<'a>,
2424
) {
25-
if !node.key.is_identifier() || !node.value.body.is_some() || !node.key.name().is_some() {
25+
if !node.key.is_identifier() || node.value.body.is_none() || node.key.name().is_none() {
2626
return;
2727
}
2828

@@ -135,7 +135,7 @@ impl<'a> Traverse<'a> for Transformer<'a> {
135135
return;
136136
}
137137

138-
if !node.id.is_some() || !node.body.is_some() {
138+
if node.id.is_none() || node.body.is_none() {
139139
// No identifier or body, nothing to instrument
140140
return;
141141
}

instrumentation-wasm/src/wasm_bindings/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ pub fn wasm_transform_code_str(
1010
instructions_json: &str,
1111
source_type: i32,
1212
) -> String {
13-
return transform_code_str(pkg_name, pkg_version, code, instructions_json, source_type);
13+
transform_code_str(pkg_name, pkg_version, code, instructions_json, source_type)
1414
}

library/agent/hooks/instrumentation/codeTransformation.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ t.test("add modifyArgs to method definition (ESM)", async (t) => {
957957
testFunction(arg1) {
958958
console.log("test");
959959
960-
return __instrumentModifyReturnValue("testpkg.test.js.testFunction.MethodDefinition.v1.0.0", arg1);
960+
return __instrumentModifyReturnValue("testpkg.test.js.testFunction.MethodDefinition.v1.0.0", arguments, arg1);
961961
}
962962
}`
963963
);
@@ -975,7 +975,7 @@ t.test("add modifyArgs to method definition (ESM)", async (t) => {
975975
testFunction(arg1) {
976976
console.log("test");
977977
978-
return __instrumentModifyReturnValue("testpkg.test.js.testFunction.MethodDefinition.v1.0.0", "test");
978+
return __instrumentModifyReturnValue("testpkg.test.js.testFunction.MethodDefinition.v1.0.0", arguments,"test");
979979
}
980980
}`
981981
);
@@ -993,7 +993,7 @@ t.test("add modifyArgs to method definition (ESM)", async (t) => {
993993
testFunction(arg1) {
994994
console.log("test");
995995
996-
return __instrumentModifyReturnValue("testpkg.test.js.testFunction.MethodDefinition.v1.0.0", "test\\"");
996+
return __instrumentModifyReturnValue("testpkg.test.js.testFunction.MethodDefinition.v1.0.0", arguments, "test\\"");
997997
}
998998
}`
999999
);
@@ -1011,7 +1011,7 @@ t.test("add modifyArgs to method definition (ESM)", async (t) => {
10111011
testFunction(arg1) {
10121012
console.log("test");
10131013
1014-
return __instrumentModifyReturnValue("testpkg.test.js.testFunction.MethodDefinition.v1.0.0", [1, 2]);
1014+
return __instrumentModifyReturnValue("testpkg.test.js.testFunction.MethodDefinition.v1.0.0", arguments, [1, 2]);
10151015
}
10161016
}`
10171017
);
@@ -1029,7 +1029,7 @@ t.test("add modifyArgs to method definition (ESM)", async (t) => {
10291029
testFunction(arg1) {
10301030
console.log("test");
10311031
1032-
return __instrumentModifyReturnValue("testpkg.test.js.testFunction.MethodDefinition.v1.0.0", function() { return 42; });
1032+
return __instrumentModifyReturnValue("testpkg.test.js.testFunction.MethodDefinition.v1.0.0", arguments, function() { return 42; });
10331033
}
10341034
}`
10351035
);
@@ -1049,7 +1049,7 @@ t.test("add modifyArgs to method definition (ESM)", async (t) => {
10491049
testFunction(arg1) {
10501050
console.log("test");
10511051
1052-
return __instrumentModifyReturnValue("testpkg.test.js.testFunction.MethodDefinition.v1.0.0", funcCall({foo: [1], test: Symbol("abc")}));
1052+
return __instrumentModifyReturnValue("testpkg.test.js.testFunction.MethodDefinition.v1.0.0", arguments, funcCall({foo: [1], test: Symbol("abc")}));
10531053
}
10541054
}`
10551055
);
@@ -1194,7 +1194,7 @@ t.test("Modify function declaration (ESM)", async (t) => {
11941194
function test123(arg1, arg2 = "default") {
11951195
__instrumentInspectArgs("testpkg.application.js.test123.FunctionDeclaration.v1.0.0", arguments, "1.0.0", this);
11961196
console.log("test123");
1197-
return __instrumentModifyReturnValue("testpkg.application.js.test123.FunctionDeclaration.v1.0.0","abc");
1197+
return __instrumentModifyReturnValue("testpkg.application.js.test123.FunctionDeclaration.v1.0.0", arguments, "abc");
11981198
}
11991199
const ignore = function test123() {
12001200
console.log("ignore");

library/agent/hooks/instrumentation/loadHook.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ function patchBuiltin(
135135
builtinName: string,
136136
previousLoadResult: ReturnType<LoadFunction>
137137
) {
138-
// Todo test if used with import-in-the-middle at the same time
139-
140138
const builtinNameWithoutPrefix = removeNodePrefix(builtinName);
141139

142140
const builtin = shouldPatchBuiltin(builtinNameWithoutPrefix);

0 commit comments

Comments
 (0)