Skip to content

Commit

Permalink
fix: method calls were receiving the object reference as the last arg…
Browse files Browse the repository at this point in the history
…ument.

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.
  • Loading branch information
plusvic committed Jan 22, 2025
1 parent d79fbde commit 2b69aee
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
13 changes: 7 additions & 6 deletions lib/src/compiler/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,15 +635,16 @@ fn emit_expr(
},

Expr::FuncCall(func_call) => {
// Emit the arguments first.
for expr in func_call.args.iter() {
emit_expr(ctx, ir, *expr, instr);
}

// If this is method call, the target object (self or this in some
// programming languages) is the first argument.
if let Some(obj) = func_call.object {
emit_expr(ctx, ir, obj, instr);
}


for expr in func_call.args.iter() {
emit_expr(ctx, ir, *expr, instr);
}

if func_call.signature().result_may_be_undef {
emit_call_and_handle_undef(
ctx,
Expand Down
23 changes: 22 additions & 1 deletion lib/src/modules/test_proto2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::rc::Rc;

use crate::modules::prelude::*;
use crate::modules::protos::test_proto2::NestedProto2;
use crate::modules::protos::test_proto2::TestProto2;
use crate::types::Struct;
use std::rc::Rc;

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -41,6 +42,25 @@ pub(crate) fn nested_method(
structure.field_by_name("nested_bool").unwrap().type_value.as_bool()
}

#[module_export(
name = "nested_method_with_arg",
method_of = "test_proto2.NestedProto2"
)]
pub(crate) fn nested_method_with_arg(
ctx: &mut ScanContext,
structure: Rc<Struct>,
arg: RuntimeString,
) -> bool {
let arg = arg.as_bstr(ctx);
let field = structure
.field_by_name("nested_string")
.unwrap()
.type_value
.as_string();

arg.eq(field.as_bstr())
}

#[module_export]
pub(crate) fn undef_i64(_ctx: &mut ScanContext) -> Option<i64> {
None
Expand Down Expand Up @@ -126,6 +146,7 @@ fn main(data: &[u8], _meta: Option<&[u8]>) -> TestProto2 {
nested.set_nested_int32_one(1);
nested.set_nested_int64_one(1);
nested.set_nested_bool(false);
nested.set_nested_string("foo".to_string());

nested.nested_array_int64.push(1);
nested.nested_array_int64.push(10);
Expand Down
6 changes: 6 additions & 0 deletions lib/src/modules/test_proto2/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ fn test_proto2_module() {
"#
);

condition_true!(
r#"
test_proto2.nested.nested_method_with_arg("foo")
"#
);

condition_true!(
r#"
not test_proto2.array_struct[0].nested_method()
Expand Down

0 comments on commit 2b69aee

Please sign in to comment.