66
77package com .example ;
88
9+ import com .example .Photon .Uint8Array ;
910import io .micronaut .context .annotation .Context ;
1011import io .micronaut .core .io .ResourceResolver ;
1112import jakarta .annotation .PreDestroy ;
@@ -24,15 +25,14 @@ public class PhotonPool {
2425 private final BlockingQueue <Photon > photons ;
2526
2627 PhotonPool (ResourceResolver resourceResolve ) throws IOException {
27- URL photonModuleURL = resourceResolve .getResource ("classpath:photon/photon_rs .js" ).get ();
28+ URL photonModuleURL = resourceResolve .getResource ("classpath:photon/photon .js" ).get ();
2829 Source photonSource = Source .newBuilder ("js" , photonModuleURL ).mimeType ("application/javascript+module" ).build ();
29- byte [] wasmBytes = resourceResolve .getResourceAsStream ("classpath:photon/photon_rs_bg.wasm" ).get ().readAllBytes ();
3030 byte [] imageBytes = resourceResolve .getResourceAsStream ("classpath:daisies_fuji.jpg" ).get ().readAllBytes ();
3131
3232 int maxThreads = Runtime .getRuntime ().availableProcessors ();
3333 photons = new LinkedBlockingQueue <>(maxThreads );
3434 for (int i = 0 ; i < maxThreads ; i ++) {
35- photons .add (createPhoton (sharedEngine , photonSource , wasmBytes , imageBytes ));
35+ photons .add (createPhoton (sharedEngine , photonSource , imageBytes ));
3636 }
3737 }
3838
@@ -53,25 +53,21 @@ public void close() {
5353 sharedEngine .close ();
5454 }
5555
56- private static Photon createPhoton (Engine engine , Source photonSource , Object wasmBytes , Object imageBytes ) {
57- org . graalvm . polyglot . Context context = org .graalvm .polyglot .Context .newBuilder ("js" , "wasm" )
56+ private static Photon createPhoton (Engine engine , Source photonSource , Object imageBytes ) {
57+ var context = org .graalvm .polyglot .Context .newBuilder ("js" , "wasm" )
5858 .engine (engine )
5959 .allowAllAccess (true )
6060 .allowExperimentalOptions (true )
61- .option ("js.webassembly" , "true" )
6261 .option ("js.esm-eval-returns-exports" , "true" )
62+ .option ("js.text-encoding" , "true" )
63+ .option ("js.webassembly" , "true" )
6364 .build ();
6465
65- // Get Uint8Array class from JavaScript
66- Value uint8Array = context .eval ("js" , "Uint8Array" );
6766 // Load Photon module and initialize with wasm content
6867 Value photonModule = context .eval (photonSource );
69- // Create Uint8Array with wasm bytes
70- Value wasmContent = uint8Array .newInstance (wasmBytes );
71- // Initialize Photon module with wasm content
72- photonModule .invokeMember ("default" , wasmContent );
68+
7369 // Create Uint8Array with image bytes
74- Value imageContent = uint8Array . newInstance (imageBytes );
70+ Uint8Array imageContent = context . getBindings ( "js" ). getMember ( "Uint8Array" ). newInstance (imageBytes ). as ( Uint8Array . class );
7571
7672 return new Photon (photonModule , imageContent );
7773 }
0 commit comments