0

I'm trying to embed V8 into an existing code that already has a mainloop.

As I understand it, script->Run() is executed before the script finishes executing its "main body" (global scope).

The main problem is that in this case my mainloop will hang until the script is executed(My application is single-threaded). Executing scripts in a separate thread also does not look like a good solution, because for each API call of my application from the script, I will have to synchronize the mainloop and the script thread, which in the end can greatly affect the performance and quality of the code.

Also, it would be nice if it was possible to forcibly suspend the execution of JS from the script itself (for example, with the yield function)

Here are the "crutch" solution ideas that are currently available, but they feel terrible:

  1. Creating Fibers for each script, and switching them with the main thread. But in this case, it seems to me that I will break a lot of mechanisms inside the engine.
  2. Using v8::TryCatch and TerminateExecution.
  3. Using the v8 debugging API to suspend the script by the "debugger"
kin4stat
  • 192
  • 1
  • 11
  • *my mainloop will hang until the script is executed* The issue is not how you call `script->Run()`, the issue is the script itself. Use asynchronous JavaScript API. – 273K Jul 07 '23 at 05:31
  • How does this solve the problem of V8 running the script synchronously in the current thread (blocking it)? – kin4stat Jul 07 '23 at 05:45
  • What are these script(s) you are running? It's unclear whether you're trying to fire off multiple of them, or one big one. Specific advice on a problem requires ultra-specific detail about exactly what you need to achieve, probably with a comprehensive example. – paddy Jul 07 '23 at 06:26
  • The main idea of this case is to provide a more convenient api for scripting than C++ (I know that Ican take lua, but I took javascript as a practice). The scripts themselves can be different in complexity. It can be a script that just executes one API call to my application. or maybe a large script implementing the logic of some large system. And yes, there will be several scripts in the end. I don't really care about their parallel execution, just sequential will do. – kin4stat Jul 07 '23 at 06:29

0 Answers0