@@ -27,7 +27,7 @@ export class ReusableVMWorkflowCreator implements WorkflowCreator {
2727 *
2828 * Use the {@link context} getter instead
2929 */
30- private _context ?: vm . Context ;
30+ private _context ?: vm . Context & typeof globalThis ;
3131 private pristineObj ?: object ;
3232
3333 constructor (
@@ -42,12 +42,12 @@ export class ReusableVMWorkflowCreator implements WorkflowCreator {
4242 ReusableVMWorkflowCreator . unhandledRejectionHandlerHasBeenSet = true ;
4343 }
4444
45- this . _context = vm . createContext ( { } , { microtaskMode : 'afterEvaluate' } ) ;
45+ this . _context = vm . createContext ( { } , { microtaskMode : 'afterEvaluate' } ) as vm . Context & typeof globalThis ;
4646 vm . runInContext (
4747 `{
4848 const __TEMPORAL_CALL_INTO_SCOPE = () => {
49- const [holder, fn, args] = globalThis.__TEMPORAL_ARGS__ ;
50- delete globalThis.__TEMPORAL_ARGS__ ;
49+ const [holder, fn, args] = globalThis.__temporal_args ;
50+ delete globalThis.__temporal_args ;
5151
5252 if (globalThis.__TEMPORAL_BAG_HOLDER__ !== holder) {
5353 if (globalThis.__TEMPORAL_BAG_HOLDER__ !== undefined) {
@@ -81,13 +81,11 @@ export class ReusableVMWorkflowCreator implements WorkflowCreator {
8181 { timeout : isolateExecutionTimeoutMs , displayErrors : true }
8282 ) ;
8383
84- this . injectGlobals ( this . _context ) ;
85-
8684 const sharedModules = new Map < string | symbol , any > ( ) ;
8785 const __webpack_module_cache__ = new Proxy (
8886 { } ,
8987 {
90- get : ( _ , p ) => {
88+ get : ( _ , p : string ) => {
9189 // Try the shared modules first
9290 const sharedModule = sharedModules . get ( p ) ;
9391 if ( sharedModule ) {
@@ -96,7 +94,7 @@ export class ReusableVMWorkflowCreator implements WorkflowCreator {
9694 const moduleCache = this . context . __TEMPORAL_ACTIVATOR__ ?. moduleCache ;
9795 return moduleCache ?. get ( p ) ;
9896 } ,
99- set : ( _ , p , val ) => {
97+ set : ( _ , p : string , val ) => {
10098 const moduleCache = this . context . __TEMPORAL_ACTIVATOR__ ?. moduleCache ;
10199 if ( moduleCache != null ) {
102100 moduleCache . set ( p , val ) ;
@@ -115,6 +113,8 @@ export class ReusableVMWorkflowCreator implements WorkflowCreator {
115113 configurable : false ,
116114 } ) ;
117115
116+ this . injectGlobals ( this . _context ) ;
117+
118118 script . runInContext ( this . context ) ;
119119
120120 // The V8 context is really composed of two distinct objects: the 'this._context' object on the outside, and another
@@ -127,7 +127,7 @@ export class ReusableVMWorkflowCreator implements WorkflowCreator {
127127 ...Object . getOwnPropertyNames ( this . pristineObj ) ,
128128 ...Object . getOwnPropertySymbols ( this . pristineObj ) ,
129129 ] ) {
130- if ( k !== 'globalThis' ) {
130+ if ( k !== 'globalThis' && k !== '__temporal_globalSandboxDestructors' ) {
131131 const v : PropertyDescriptor = ( this . pristineObj as any ) [ k ] ;
132132 v . value = deepFreeze ( v . value ) ;
133133 }
@@ -136,7 +136,7 @@ export class ReusableVMWorkflowCreator implements WorkflowCreator {
136136 for ( const v of sharedModules . values ( ) ) deepFreeze ( v ) ;
137137 }
138138
139- protected get context ( ) : vm . Context {
139+ protected get context ( ) : vm . Context & typeof globalThis {
140140 const { _context } = this ;
141141 if ( _context == null ) {
142142 throw new IllegalStateError ( 'Tried to use v8 context after Workflow creator was destroyed' ) ;
@@ -159,31 +159,31 @@ export class ReusableVMWorkflowCreator implements WorkflowCreator {
159159 async createWorkflow ( options : WorkflowCreateOptions ) : Promise < Workflow > {
160160 const context = this . context ;
161161 const holder : BagHolder = { bag : this . pristineObj ! } ;
162-
163162 const { isolateExecutionTimeoutMs } = this ;
163+
164164 const workflowModule : WorkflowModule = new Proxy (
165165 { } ,
166166 {
167167 get ( _ : any , fn : string ) {
168168 return ( ...args : any [ ] ) => {
169169 // By the time we get out of this call, all microtasks will have been executed
170- context . __TEMPORAL_ARGS__ = [ holder , fn , args ] ;
170+ context . __temporal_args = [ holder , fn , args ] ;
171171 return callIntoVmScript . runInContext ( context , {
172172 timeout : isolateExecutionTimeoutMs ,
173173 displayErrors : true ,
174174 } ) ;
175175 } ;
176176 } ,
177177 }
178- ) as any ;
178+ ) ;
179179
180180 workflowModule . initRuntime ( {
181181 ...options ,
182182 sourceMap : this . workflowBundle . sourceMap ,
183183 getTimeOfDay : native . getTimeOfDay ,
184184 registeredActivityNames : this . registeredActivityNames ,
185185 } ) ;
186- const activator = context [ ' __TEMPORAL_ACTIVATOR__' ] ;
186+ const activator = context . __TEMPORAL_ACTIVATOR__ ! ;
187187 const newVM = new ReusableVMWorkflow ( options . info . runId , context , activator , workflowModule ) ;
188188 ReusableVMWorkflowCreator . workflowByRunId . set ( options . info . runId , newVM ) ;
189189 return newVM ;
@@ -210,8 +210,12 @@ export class ReusableVMWorkflowCreator implements WorkflowCreator {
210210 * Cleanup the pre-compiled script
211211 */
212212 public async destroy ( ) : Promise < void > {
213- globalHandlers . removeWorkflowBundle ( this . workflowBundle ) ;
214- delete this . _context ;
213+ try {
214+ vm . runInContext ( `__TEMPORAL__.api.destroy()` , this . context ) ;
215+ } finally {
216+ globalHandlers . removeWorkflowBundle ( this . workflowBundle ) ;
217+ delete this . _context ;
218+ }
215219 }
216220}
217221
0 commit comments