3

Is there a way to set an entry point in DWScript?

For example, if I start a script execution, I'd like it to execute a procedure Main, rather than the code in the regular entry point (begin ... end.).

I know it's possible to execute functions from Delphi, but I'm not sure this would be quite the same.

FHannes
  • 783
  • 7
  • 22

2 Answers2

2

Aside from writing your procedure Main(); and then having your regular script entry point consist of nothing but calling Main, which is probably not what you're thinking of, no, there's no way to do that in DWS.

For all its innovations in syntax, DWS is still Pascal, and it still works the way Pascal works. Asking for some sort of named Main routine would be a radical departure from Pascal style.

EDIT: To answer the clarification posted in comments:

If you want your script to spawn a new script thread, you'll have to handle it in external Delphi code. As of this writing, the DWS system doesn't have any concept of multithreading built in. If you wanted to do it, you'd do something like this:

Create an external routine called something like SpawnThread(EntryPoint: string). Its eval method (out in Native-Delphi-land) would spawn a new thread that loads the current script, then finds the routine with the specified name and executes it.

That's about the only way you could get it to work without language-level support. If you'd like a way to spawn threads from within DWS, try adding it as a feature request to the issue tracker.

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
  • My intention is not to use this procedure as a single entry point. I would like to make it possible for an execution to call a method that would start a second execution in a separate thread. The issue I'm having is that I'm not sure how to distinguish between the initial (main) execution and the threaded one, as the threaded one would have to execute different code somehow. My thought was to enter the execution in another function for the thread. – FHannes Feb 28 '13 at 00:03
  • This method is essentially what I was hoping to do. Call a function in the script which spawns a new thread in Delphi code that enters the script in a different routine. But I'm not entirely sure how to achieve that without it executing the main entry point again? – FHannes Feb 28 '13 at 01:49
  • @FHannes: assume you have an `IDwsProgram` variable called `prog`. The following syntax is what you need, or at least enough to get you started: `prog.BeginNewExecution.Info.Func['whatever'].Call(params);` – Mason Wheeler Feb 28 '13 at 02:04
  • I wasn't sure I could do that without actually starting the execution. Thanks. – FHannes Mar 01 '13 at 12:31
2

Calling functions directly is explicited in

https://code.google.com/p/dwscript/wiki/FirstSteps#Functions

If you want to execute a function in a different thread, you'll need some Delphi-side code to create a new thread, a new execution, and then call your functions. The main and threaded-execution will then be sandboxed from each other (so can't share share global vars etc.).

If you need to share data between the threads, you could do that by exposing functions or external variables, which would call into Delphi code with the proper synchronizations and locks in place (what is "proper" will depend on what your code wants to do, like always in multithreading...).

Note that it is possible to pass objects, interfaces and dynamic arrays between script executions (provided they're executions of the same program), but just like with regular code, you'll have to use locks, critical sections or mutexes explicitly.

Eric Grange
  • 5,931
  • 1
  • 39
  • 61