@@ -28,9 +28,20 @@ var ErrorMessage = {
28
28
WorkerNotSupported : { code : 507 , text : "This browser doesn't support web workers." } ,
29
29
ThreadAllocationTimeout : { code : 508 , text : "Thread allocation failed due to timeout." } ,
30
30
MethodAssignment : { code : 509 , text : "Can't assign a method." } ,
31
- NotAccessibleExport : { code : 510 , text : "Can't access an export of type '%{0}'. Only top level functions and classes are imported." } ,
31
+ NotAccessibleExport : {
32
+ code : 510 ,
33
+ text : "Can't access an export of type '%{0}'. Only top level functions and classes are imported."
34
+ } ,
32
35
ThreadPoolTerminated : { code : 511 , text : "Thread pool has been terminated." } ,
33
- ThreadTerminated : { code : 512 , text : "Thread has been terminated." }
36
+ ThreadTerminated : { code : 512 , text : "Thread has been terminated." } ,
37
+ UnrecognizedModuleType : {
38
+ code : 513 ,
39
+ text : "A module with an unrecognized type has been passed '%{0}'."
40
+ } ,
41
+ UnexportedFunction : {
42
+ code : 514 ,
43
+ text : "No function with the name '%{0}' has been exported in module '{%1}'."
44
+ }
34
45
} ;
35
46
var ValueType = {
36
47
undefined : 1 ,
@@ -111,6 +122,12 @@ function createObject(properties) {
111
122
}
112
123
return obj ;
113
124
}
125
+ function isNativeModule ( moduleSrc ) {
126
+ if ( moduleSrc . endsWith ( ".wasm" /* WASM */ ) )
127
+ return false ;
128
+ else
129
+ return true ;
130
+ }
114
131
115
132
// libs/platform/src/core/error.ts
116
133
var ConcurrencyError = class extends Error {
@@ -173,6 +190,9 @@ __publicField(ThreadedObject, "objectRegistry", new FinalizationRegistry(({ id,
173
190
174
191
// libs/platform/src/core/worker_manager.ts
175
192
var WorkerManager = class {
193
+ constructor ( interopHandler ) {
194
+ this . interopHandler = interopHandler ;
195
+ }
176
196
objects = /* @__PURE__ */ new Map ( ) ;
177
197
lastObjectId = 0 ;
178
198
async handleMessage ( type , data ) {
@@ -240,9 +260,13 @@ var WorkerManager = class {
240
260
async invokeFunction ( moduleSrc , functionName , args = [ ] ) {
241
261
let result , error ;
242
262
try {
243
- const module = await import ( moduleSrc ) ;
244
- const method = Reflect . get ( module , functionName ) ;
245
- result = await method . apply ( module . exports , args ) ;
263
+ if ( ! isNativeModule ( moduleSrc ) ) {
264
+ result = await this . interopHandler . run ( moduleSrc , functionName , args ) ;
265
+ } else {
266
+ const module = await import ( moduleSrc ) ;
267
+ const method = Reflect . get ( module , functionName ) ;
268
+ result = await method . apply ( module . exports , args ) ;
269
+ }
246
270
} catch ( err ) {
247
271
error = err ;
248
272
}
@@ -347,10 +371,43 @@ var WorkerManager = class {
347
371
}
348
372
} ;
349
373
374
+ // libs/platform/src/core/interop/wasm.ts
375
+ var WasmInteropHandler = class {
376
+ constructor ( createInstance ) {
377
+ this . createInstance = createInstance ;
378
+ }
379
+ cache = /* @__PURE__ */ new Map ( ) ;
380
+ async run ( moduleSrc , functionName , args ) {
381
+ let instance ;
382
+ if ( this . cache . has ( moduleSrc ) )
383
+ instance = this . cache . get ( moduleSrc ) ;
384
+ else {
385
+ instance = await this . createInstance ( moduleSrc ) ;
386
+ this . cache . set ( moduleSrc , instance ) ;
387
+ }
388
+ const fn = Reflect . get ( instance . exports , functionName ) ;
389
+ if ( ! fn )
390
+ throw new ConcurrencyError ( ErrorMessage . UnexportedFunction , functionName , moduleSrc ) ;
391
+ const result = Reflect . apply ( fn , instance . exports , args ) ;
392
+ return result ;
393
+ }
394
+ } ;
395
+
350
396
// libs/platform/src/browser/worker_script.ts
351
397
if ( void 0 )
352
- throw new ConcurrencyError2 ( Constants . ErrorMessage . NotRunningOnWorker ) ;
353
- var manager = new WorkerManager ( ) ;
398
+ throw new ConcurrencyError ( Constants . ErrorMessage . NotRunningOnWorker ) ;
399
+ var wasmInteropHandler = new WasmInteropHandler ( async ( moduleSrc ) => {
400
+ const { instance } = await WebAssembly . instantiateStreaming ( fetch ( moduleSrc ) ) ;
401
+ return instance ;
402
+ } ) ;
403
+ var manager = new WorkerManager ( {
404
+ run ( moduleSrc , functionName , args ) {
405
+ if ( moduleSrc . endsWith ( ".wasm" /* WASM */ ) )
406
+ return wasmInteropHandler . run ( moduleSrc , functionName , args ) ;
407
+ else
408
+ throw new ConcurrencyError ( ErrorMessage . UnrecognizedModuleType , moduleSrc ) ;
409
+ }
410
+ } ) ;
354
411
onmessage = function ( e ) {
355
412
const [ type , data ] = e . data ;
356
413
manager . handleMessage ( type , data ) . then ( ( reply ) => {
0 commit comments