1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
- import { Module } from "./imports" ;
4
+ import { Module , runtimeHelpers } from "./imports" ;
5
5
import { mono_assert } from "./types" ;
6
6
7
7
class OperationFailedError extends Error { }
@@ -112,11 +112,13 @@ export function init_crypto(): void {
112
112
channel : chan ,
113
113
worker : worker ,
114
114
} ;
115
- worker . postMessage ( {
115
+ const messageData : InitCryptoMessageData = {
116
+ config : JSON . stringify ( runtimeHelpers . config ) ,
116
117
comm_buf : chan . get_comm_buffer ( ) ,
117
118
msg_buf : chan . get_msg_buffer ( ) ,
118
119
msg_char_len : chan . get_msg_len ( )
119
- } ) ;
120
+ } ;
121
+ worker . postMessage ( messageData ) ;
120
122
worker . onerror = event => {
121
123
console . warn ( `MONO_WASM: Error in Crypto WebWorker. Cryptography digest calls will fallback to managed implementation. Error: ${ event . message } ` ) ;
122
124
mono_wasm_crypto = null ;
@@ -202,9 +204,10 @@ class LibraryChannel {
202
204
203
205
public send_msg ( msg : string ) : string {
204
206
try {
205
- // const state = Atomics.load(this.comm, this.STATE_IDX);
206
- // if (state !== this.STATE_IDLE) console.debug(`MONO_WASM_ENCRYPT_DECRYPT: send_msg, waiting for idle now, ${state}`);
207
- this . wait_for_state ( pstate => pstate == this . STATE_IDLE , "waiting" ) ;
207
+ let state = Atomics . load ( this . comm , this . STATE_IDX ) ;
208
+ // FIXME: this console write is possibly serializing the access and prevents a deadlock
209
+ if ( state !== this . STATE_IDLE ) console . debug ( `MONO_WASM_ENCRYPT_DECRYPT: send_msg, waiting for idle now, ${ state } ` ) ;
210
+ state = this . wait_for_state ( pstate => pstate == this . STATE_IDLE , "waiting" ) ;
208
211
209
212
this . send_request ( msg ) ;
210
213
return this . read_response ( ) ;
@@ -213,13 +216,14 @@ class LibraryChannel {
213
216
throw err ;
214
217
}
215
218
finally {
216
- // const state = Atomics.load(this.comm, this.STATE_IDX);
217
- // if (state !== this.STATE_IDLE) console.debug(`MONO_WASM_ENCRYPT_DECRYPT: state at end of send_msg: ${state}`);
219
+ const state = Atomics . load ( this . comm , this . STATE_IDX ) ;
220
+ // FIXME: this console write is possibly serializing the access and prevents a deadlock
221
+ if ( state !== this . STATE_IDLE ) console . debug ( `MONO_WASM_ENCRYPT_DECRYPT: state at end of send_msg: ${ state } ` ) ;
218
222
}
219
223
}
220
224
221
225
public shutdown ( ) : void {
222
- // console.debug("MONO_WASM_ENCRYPT_DECRYPT: Shutting down crypto");
226
+ console . debug ( "MONO_WASM_ENCRYPT_DECRYPT: Shutting down crypto" ) ;
223
227
const state = Atomics . load ( this . comm , this . STATE_IDX ) ;
224
228
if ( state !== this . STATE_IDLE )
225
229
throw new Error ( `OWNER: Invalid sync communication channel state: ${ state } ` ) ;
@@ -230,15 +234,14 @@ class LibraryChannel {
230
234
Atomics . notify ( this . comm , this . STATE_IDX ) ;
231
235
}
232
236
233
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
234
237
private reset ( reason : string ) : void {
235
- // console.debug(`MONO_WASM_ENCRYPT_DECRYPT: reset: ${reason}`);
238
+ console . debug ( `MONO_WASM_ENCRYPT_DECRYPT: reset: ${ reason } ` ) ;
236
239
const state = Atomics . load ( this . comm , this . STATE_IDX ) ;
237
240
if ( state === this . STATE_SHUTDOWN )
238
241
return ;
239
242
240
243
if ( state === this . STATE_RESET || state === this . STATE_IDLE ) {
241
- // console.debug(`MONO_WASM_ENCRYPT_DECRYPT: state is already RESET or idle: ${state}`);
244
+ console . debug ( `MONO_WASM_ENCRYPT_DECRYPT: state is already RESET or idle: ${ state } ` ) ;
242
245
return ;
243
246
}
244
247
@@ -386,3 +389,10 @@ class LibraryChannel {
386
389
return new LibraryChannel ( msg_char_len ) ;
387
390
}
388
391
}
392
+
393
+ export type InitCryptoMessageData = {
394
+ config : string , // serialized to avoid passing non-clonable objects
395
+ comm_buf : SharedArrayBuffer ,
396
+ msg_buf : SharedArrayBuffer ,
397
+ msg_char_len : number
398
+ }
0 commit comments