P icon indicating copy to clipboard operation
P copied to clipboard

Support `this` in global/static functions ?

Open ghost opened this issue 3 years ago • 3 comments

I noticed one can use send/receive in a global function but I don't know if it's intended because THIS is not allowed in global functions, and I would expect it to be, since it's the maching sending/receiving.

The below machine has one such global function getValue that works. You have to specify the source machine for the message getValue sends, because it can't use THIS. But it can use receive and that seems to work in the context of the calling machine.

// Watch 'pc D.p && coyote test POutput/netcoreapp3.1/D.dll'

type tRequest = (source: machine);
event eRequest: tRequest;

type tResponse = (value: int);
event eResponse: tResponse;

machine Server {
	start state _ {
		on eRequest do (r: tRequest) {
			send r.source, eResponse, (value = 42, );
		}
	}
}

machine Main {
	var server: Server;
	var answer: int;

	start state _ {
		entry {
			server = new Server();
			answer = getValue(this, server);
			// NOTE: This prints:
			// <PrintLog> getValue returned 42
			print format("getValue returned {0}", answer);
		}
	}
}

fun getValue(source: machine, server: Server): int {
	var ret: int;
	send server, eRequest, (source = source, );
	receive {
		case eResponse: (r: tResponse) {
			ret = r.value;
		}
	}
	return ret;
}

ghost avatar Apr 07 '22 22:04 ghost

I didnt completely understand your question. The message is received by the machine that invokes the function that has a receive in it.

ankushdesai avatar May 23 '22 01:05 ankushdesai

The message is received by the machine that invokes the function that has a receive in it.

Right. IMO it would make sense to bind this to that machine, instead of disallowing this and forcing the caller to pass this as first argument.

ghost avatar May 23 '22 11:05 ghost

Yeah, I agree. I think this should be doable and easy to fix. Thanks for pointing this out 🥇 .

ankushdesai avatar May 23 '22 17:05 ankushdesai