feat: improve initialization with RAII patterns and runtime IDs#315
Conversation
|
Noticed the test failure now. On it. |
…nd runtime IDs - Add Context::Scope RAII pattern to RunMainScript for proper context handling - Introduce atomic runtime ID generation for unique runtime identification - Ensures proper context scope management during script execution - Runtime IDs use thread-safe atomic operations for main and worker threads
c501ec0 to
f36ecd7
Compare
|
The runtime ID change introduces a new bug where the main thread runtime is now considered a Worker. Also, you'll get duplicate runtime ids because the worker ids and runtime ids aren't generated from the same atomic int. ios/NativeScript/runtime/Worker.mm Line 127 in fd2703d @NathanWalker please hold off merging until at least a proper review comes in. I'm reverting this for now |
Will look into this and come back to you. |
@edusperoni, I apologize! You are right that the change is wrong in the current implementation due to |
This PR improves runtime initialization correctness and modernizes V8 API usage:
Context Management (RAII):
Adds
Context::ScopeRAII pattern toRunMainScriptfor proper context handling. TheContext::Scopeensures the context is correctly exited when the scope ends, even if exceptions occur. This is the recommended V8 C++ API pattern perv8-locker.hdocumentation.Note:
Runtime::Initcontinues to use manualcontext->Enter()because the context must remain entered for the runtime's entire lifetime, not just during initialization.Runtime ID Generation:
Introduces a static atomic counter (
nextRuntimeId_) that generates unique IDs for eachRuntimeinstance during initialization. This provides reliable runtime identification for both main and worker threads using thread-safe atomic operations withmemory_order_relaxed(appropriate for ID generation).The patterns align with modern C++ best practices and V8 API recommendations.