Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operator overload refactor via re-entrant threads, list operator overload improvement #2003

Merged

Conversation

PJB3005
Copy link
Contributor

@PJB3005 PJB3005 commented Sep 30, 2024

Operator overloads were extremely complex because they are calling user procs. This meant that operator overloads had to be "tail called" at the end of an opcode handler, so that we could return ProcStatus.Called.

This posed a huge problem, as the opcode handler loses control after the user proc is executed. This causes multiple issues:

  • Some operator overload logic had to implemented with a special AssignRefProcState, so we could properly handle the return value of the operator overload. Yuck.
  • The code surrounding this was generally spaghetti, as calling an operator overload had different stack behavior, and we have to pass ProcStatus around the place.
  • It was outright impossible to implement operator[]() properly, as it would always need to be called from the start of an opcode handler.

This however is all fixable with the following revelation: operator overloads in BYOND are implicitly waitfor = FALSE. This means that if we can simply call the proc without needing to use ProcStatus.Called, we can just immediately get the return value from it.

This commit does that: DreamThread now allows re-entrantly resuming with a new call stack proc, right from the opcode handler. This means operator overloads can just be details that immediately return a value.

With this power I was able to clean all of the above issues up. The code is cleaner and less complex. operator[]() and operator[]=() work. Adding new operator overloads in the future should be easier.

…load improvement

Operator overloads were extremely complex because they are calling user procs. This meant that operator overloads had to be "tail called" at the end of an opcode handler, so that we could return ProcStatus.Called.

This posed a huge problem, as the opcode handler loses control after the user proc is executed. This causes multiple issues:

* Some operator overload logic had to implemented with a special AssignRefProcState, so we could properly handle the return value of the operator overload. Yuck.
* The code surrounding this was generally spaghetti, as calling an operator overload had different stack behavior, and we have to pass ProcStatus around the place.
* It was outright impossible to implement operator[]() properly, as it would always need to be called from the *start* of an opcode handler.

This however is all fixable with the following revelation: operator overloads in BYOND are implicitly waitfor = FALSE. This means that if we can simply call the proc *without* needing to use ProcStatus.Called, we can just immediately get the return value from it.

This commit does that: DreamThread now allows re-entrantly resuming with a new call stack proc, right from the opcode handler. This means operator overloads can just be details that immediately return a value.

With this power I was able to clean all of the above issues up. The code is cleaner and less complex. operator[]() and operator[]=() work. Adding new operator overloads in the future should be easier.
@boring-cyborg boring-cyborg bot added Compiler Involves the OpenDream compiler Runtime Involves the OpenDream server/runtime labels Sep 30, 2024
@ike709
Copy link
Collaborator

ike709 commented Sep 30, 2024

Partially addresses #1854

@wixoaGit wixoaGit merged commit 2f83341 into OpenDreamProject:master Oct 3, 2024
7 checks passed
@PJB3005 PJB3005 deleted the 24-09-30-operator-overload-refactor branch December 18, 2024 21:44
@PJB3005 PJB3005 restored the 24-09-30-operator-overload-refactor branch December 18, 2024 21:44
@PJB3005 PJB3005 deleted the 24-09-30-operator-overload-refactor branch December 18, 2024 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compiler Involves the OpenDream compiler Runtime Involves the OpenDream server/runtime size/L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants