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

[WIP] AGS 4.0: rewrite ccInstance into non-forking ScriptExecutor #2621

Draft
wants to merge 10 commits into
base: ags4
Choose a base branch
from

Conversation

ivan-mogilko
Copy link
Contributor

@ivan-mogilko ivan-mogilko commented Dec 14, 2024

This is an experiment in a WIP stage currently.

Purpose:

  • reorganize ccInstance class, separate "runtime script data" and "script execution";
  • make it work without "forks", which are the copies of ccInstance, required to run "repeatedly_execute_always" while another script is suspended;
  • as a consequence, in theory support any number of nested script calls made through the nested engine API calls, i.e. interleaved engine -> script -> engine -> script -> etc, which may be wanted for delegates/function pointers to work (see discussion in Let's implement delegates, as a callback and function pointer mechanism #1409).

What is done

  1. The former organization ccScript - ccInstance is expanded into: ccScript - RuntimeScript - ScriptExecutor, where
    • ccScript is a loaded script data;
    • RuntimeScript is a script prepared for execution, with fixups and imports resolved, and linked with other scripts;
    • ScriptExecutor is a object that executes RuntimeScripts.
  2. Engine no longer keeps ccScripts, only use them for creating RuntimeScripts.
  3. RuntimeScripts don't make any "forks", there's strictly 1 per each script, and no need for more.
  4. There's a single ScriptExecutor object, which tracks a callstack of RuntimeScripts passed into it, capable of starting nested script runs while there's another/other script execution suspended.

In the situation where a script would call a blocking action, such as Wait() function, for example,
previously engine would do this:

  • run base ccInstance for some script event
  • call Wait()
  • run forked ccInstance for "repeatedly_execute_always"

after this change it will do:

  • call ScriptExecutor::Run for some script event
  • call Wait()
  • call ScriptExecutor::Run for "repeatedly_execute_always"
  • ScriptExecutor should correctly handle Run calls while there's a callstack of previously suspended scripts, and resume those after nested calls are done.

@ivan-mogilko ivan-mogilko added ags 4 related to the ags4 development context: script vm labels Dec 14, 2024
@ivan-mogilko
Copy link
Contributor Author

I think we can push these changes further, and introduce a "Script Thread" class. The "Script Thread" will contain its own data stack and call stack, leaving only registers in the ScriptExecutor. ScriptExecutor will be told to run this or that script on a specific "script thread".

Then there will be 2 default "threads": the "main" one, and a "non-blocking" one to run "rep-exec-always" on. This will nicely mimic the previous behavior.

But the "script thread" objects may become more useful somewhen in the future if we try to implement coroutines in AGS.

@ericoporto
Copy link
Member

I see there are changes to the script compiler, does this change requires rebuild of the auto test game with it or should be able to run the previously built ags4 auto test game ?

@ivan-mogilko
Copy link
Contributor Author

ivan-mogilko commented Dec 14, 2024

I see there are changes to the script compiler, does this change requires rebuild of the auto test game with it or should be able to run the previously built ags4 auto test game ?

No, there are no changes to compiler itself, there are only corrections to data types in ccScript.
I've been testing this with a game compiled by regular ags4 version.

I did not have time earlier, but I will check out what happens when this runs a test game.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ags 4 related to the ags4 development context: script vm
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants