66import com .dylibso .chicory .runtime .ImportFunction ;
77import com .dylibso .chicory .runtime .ImportValues ;
88import com .dylibso .chicory .runtime .Instance ;
9+ import com .dylibso .chicory .runtime .Machine ;
910import com .dylibso .chicory .runtime .Store ;
1011import com .dylibso .chicory .runtime .WasmFunctionHandle ;
1112import com .dylibso .chicory .wasi .WasiExitException ;
2627import java .util .Objects ;
2728import java .util .Set ;
2829import java .util .Stack ;
30+ import java .util .function .Function ;
2931
3032import static java .util .stream .Collectors .groupingBy ;
3133
@@ -42,7 +44,7 @@ class DependencyGraph {
4244
4345 private final Store store = new Store ();
4446 private Manifest .Options options ;
45- private CachedAotMachineFactory aotMachineFactory ;
47+ private Function < Instance , Machine > machineFactory ;
4648
4749 public DependencyGraph (Logger logger ) {
4850 this .logger = logger ;
@@ -53,8 +55,14 @@ public DependencyGraph(Logger logger) {
5355 */
5456 public void setOptions (Manifest .Options options ) {
5557 this .options = options ;
56- if (options != null && options .aot ) {
57- this .aotMachineFactory = new CachedAotMachineFactory ();
58+ if (options != null ) {
59+ if (options .aot && options .machineFactory == null ) {
60+ this .machineFactory = new CachedAotMachineFactory ();
61+ return ;
62+ }
63+ if (options .machineFactory != null ) {
64+ this .machineFactory = options .machineFactory ;
65+ }
5866 }
5967 }
6068
@@ -98,10 +106,12 @@ private void checkCollision(String moduleName, String symbol) {
98106 */
99107 public void registerModule (String name , WasmModule m ) {
100108 checkCollision (name , null );
101- if (aotMachineFactory != null ) {
102- aotMachineFactory .compile (m );
109+ if (machineFactory != null ) {
110+ if (machineFactory instanceof CachedAotMachineFactory ) {
111+ var cachedAotMachineFactory = (CachedAotMachineFactory ) machineFactory ;
112+ cachedAotMachineFactory .compile (m );
113+ }
103114 }
104-
105115 ExportSection exportSection = m .exportSection ();
106116 for (int i = 0 ; i < exportSection .exportCount (); i ++) {
107117 Export export = exportSection .getExport (i );
@@ -238,7 +248,7 @@ private Instance instantiate(String moduleId, List<HostFunction> moreHostFunctio
238248
239249 Instance instance =
240250 ChicoryModule .instanceWithOptions (
241- Instance .builder (m ), this .options , aotMachineFactory )
251+ Instance .builder (m ), this .options , machineFactory )
242252 .withImportValues (importValues )
243253 .withStart (false )
244254 .build ();
0 commit comments