You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The usage of `eval()` is a deliberate architectural choice to enable **Runtime Scope Bridging**.
50
+
51
+
Standard JavaScript functions cannot reflectively access the local variables of their caller. While our AST analysis allows the library to identify *identifiers* (variable names) within your closure, it cannot access their *values*.
52
+
53
+
1.`spawn()` analyzes the source and finds that you used the variable `data`.
54
+
2.`spawn()` returns a generated code string that acts as a bridge.
55
+
3.`eval()` executes this string in the **current local scope**, allowing the library to resolve the runtime value of `data` and transfer it to the thread.
56
+
57
+
This eliminates the need for manual dependency injection, and preserves the ergonomics of standard JavaScript closures.
58
+
27
59
## Architecture
28
60
29
-
### 1. Runtime Scope Analysis & AST Traversal
61
+
### Runtime Scope Analysis & AST Traversal
30
62
31
63
Standard JavaScript closures cannot be serialized to Workers because they are bound to the parent execution context. **experimental-threads** bypasses this limitation by performing **Just-In-Time (JIT) Static Analysis** on the calling code.
32
64
@@ -38,7 +70,7 @@ When `spawn(fn)` is invoked:
38
70
4.**Identifier Resolution:** It walks the scope chain of the function body, distinguishing between *bound identifiers* (parameters, local variables) and *free variables* (captured from the outer scope).
39
71
5.**Transpilation:** It generates a standalone worker entry script that imports necessary modules and injects the captured free variables as structured-cloned properties.
In a multi-process or multi-isolate architecture (like Web Workers), module singletons are not shared. They are instantiated once per thread. This breaks the identity of shared locks or buffers.
44
76
@@ -48,24 +80,7 @@ This library introduces **Location-Dependent Reference Integrity**.
48
80
***Memory Registry:** The main thread maintains a global memory registry, mapping these IDs to their underlying `SharedArrayBuffer` pointers.
49
81
***Hydration:** When a Worker boots and imports a module containing a `Global`, the constructor intercepts the instantiation. Instead of allocating new memory, it queries the registry transmitted during the handshake phase and **hydrates** the instance with the existing buffer from the main/parent thread.
50
82
51
-
This guarantees that `const lock = new Global(new Mutex())` refers to the exact same memory address (access cost) in every thread, enabling atomic synchronization.
52
-
53
-
## Usage
54
-
55
-
```typescript
56
-
import { spawn } from"experimental-threads";
57
-
58
-
const data = { hello: 'world' };
59
-
60
-
const result =awaiteval(spawn(async () => {
61
-
// Will be automatically captured
62
-
console.log(data.hello); // "world"
63
-
64
-
returnMath.random();
65
-
}));
66
-
67
-
console.log(result); // 0.6378467071314606
68
-
```
83
+
This guarantees that `const lock = new Global(new Mutex())` refers to the exact same memory address in every thread, enabling atomic synchronization.
69
84
70
85
## Synchronization Primitives
71
86
@@ -81,4 +96,4 @@ A signaling mechanism to control access to a common resource by multiple process
0 commit comments