Skip to content

Commit 2b69aee

Browse files
committed
fix: method calls were receiving the object reference as the last argument.
The object reference (`self` or `this` in some programming languages) must be the first argument in a method call, but it was the last one.
1 parent d79fbde commit 2b69aee

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

lib/src/compiler/emit.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -635,15 +635,16 @@ fn emit_expr(
635635
},
636636

637637
Expr::FuncCall(func_call) => {
638-
// Emit the arguments first.
639-
for expr in func_call.args.iter() {
640-
emit_expr(ctx, ir, *expr, instr);
641-
}
642-
638+
// If this is method call, the target object (self or this in some
639+
// programming languages) is the first argument.
643640
if let Some(obj) = func_call.object {
644641
emit_expr(ctx, ir, obj, instr);
645642
}
646-
643+
644+
for expr in func_call.args.iter() {
645+
emit_expr(ctx, ir, *expr, instr);
646+
}
647+
647648
if func_call.signature().result_may_be_undef {
648649
emit_call_and_handle_undef(
649650
ctx,

lib/src/modules/test_proto2/mod.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use std::rc::Rc;
2+
13
use crate::modules::prelude::*;
24
use crate::modules::protos::test_proto2::NestedProto2;
35
use crate::modules::protos::test_proto2::TestProto2;
46
use crate::types::Struct;
5-
use std::rc::Rc;
67

78
#[cfg(test)]
89
mod tests;
@@ -41,6 +42,25 @@ pub(crate) fn nested_method(
4142
structure.field_by_name("nested_bool").unwrap().type_value.as_bool()
4243
}
4344

45+
#[module_export(
46+
name = "nested_method_with_arg",
47+
method_of = "test_proto2.NestedProto2"
48+
)]
49+
pub(crate) fn nested_method_with_arg(
50+
ctx: &mut ScanContext,
51+
structure: Rc<Struct>,
52+
arg: RuntimeString,
53+
) -> bool {
54+
let arg = arg.as_bstr(ctx);
55+
let field = structure
56+
.field_by_name("nested_string")
57+
.unwrap()
58+
.type_value
59+
.as_string();
60+
61+
arg.eq(field.as_bstr())
62+
}
63+
4464
#[module_export]
4565
pub(crate) fn undef_i64(_ctx: &mut ScanContext) -> Option<i64> {
4666
None
@@ -126,6 +146,7 @@ fn main(data: &[u8], _meta: Option<&[u8]>) -> TestProto2 {
126146
nested.set_nested_int32_one(1);
127147
nested.set_nested_int64_one(1);
128148
nested.set_nested_bool(false);
149+
nested.set_nested_string("foo".to_string());
129150

130151
nested.nested_array_int64.push(1);
131152
nested.nested_array_int64.push(10);

lib/src/modules/test_proto2/tests/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@ fn test_proto2_module() {
266266
"#
267267
);
268268

269+
condition_true!(
270+
r#"
271+
test_proto2.nested.nested_method_with_arg("foo")
272+
"#
273+
);
274+
269275
condition_true!(
270276
r#"
271277
not test_proto2.array_struct[0].nested_method()

0 commit comments

Comments
 (0)