11package org .prism ;
22
3- import com .dylibso .chicory .runtime .ImportValues ;
4- import com .dylibso .chicory .runtime .Instance ;
5- import com .dylibso .chicory .wasi .WasiOptions ;
6- import com .dylibso .chicory .wasi .WasiPreview1 ;
7- import com .dylibso .chicory .wasm .Parser ;
8- import com .dylibso .chicory .wasm .types .Value ;
93import org .junit .jupiter .api .Test ;
104
11- import java .nio .charset .StandardCharsets ;
125import java .util .EnumSet ;
136
147import static org .junit .jupiter .api .Assertions .assertEquals ;
@@ -31,42 +24,13 @@ public class DummyTest {
3124
3225 @ Test
3326 public void test1 () {
34- WasiOptions wasiOpts = WasiOptions .builder ().build ();
35- WasiPreview1 wasi = WasiPreview1 .builder ().withOptions (wasiOpts ).build ();
36- var wasmPrism = Instance .builder (Parser .parse (DummyTest .class .getResourceAsStream ("/prism.wasm" )))
37- .withImportValues (ImportValues .builder ().addFunction (wasi .toHostFunctions ()).build ())
38- .build ();
39- var memory = wasmPrism .memory ();
40- var calloc = wasmPrism .export ("calloc" );
41- var pmSerializeParse = wasmPrism .export ("pm_serialize_parse" );
42- var pmBufferInit = wasmPrism .export ("pm_buffer_init" );
43- var pmBufferSizeof = wasmPrism .export ("pm_buffer_sizeof" );
44- var pmBufferValue = wasmPrism .export ("pm_buffer_value" );
45- var pmBufferLength = wasmPrism .export ("pm_buffer_length" );
46-
4727 // The Ruby source code to be processed
4828 var source = "1 + 1" ;
49- var sourceBytes = source .getBytes (StandardCharsets .US_ASCII );
50-
51- var sourcePointer = calloc .apply (1 , source .length ());
52- memory .writeString ((int ) sourcePointer [0 ], source );
53-
54- var optionsPointer = calloc .apply (1 , packedOptions .length );
55- memory .write ((int ) optionsPointer [0 ], packedOptions );
56-
57- var bufferPointer = calloc .apply (pmBufferSizeof .apply ()[0 ], 1 );
58- pmBufferInit .apply (bufferPointer );
59-
60- pmSerializeParse .apply (
61- bufferPointer [0 ], sourcePointer [0 ], source .length (), optionsPointer [0 ]);
62-
63- var result = memory .readBytes (
64- (int ) pmBufferValue .apply (bufferPointer [0 ])[0 ],
65- (int ) pmBufferLength .apply (bufferPointer [0 ])[0 ]);
6629
67- System .out .println ("RESULT: " + new String (result ));
68-
69- ParseResult pr = Loader .load (result , sourceBytes );
30+ ParseResult pr = null ;
31+ try (Prism prism = new PrismWASM ()) {
32+ pr = prism .serializeParse (packedOptions , source );
33+ }
7034
7135 assertEquals (1 , pr .value .childNodes ().length );
7236 System .out .println ("Nodes:" );
@@ -80,7 +44,7 @@ public void test1Aot() {
8044 var source = "1 + 1" ;
8145
8246 ParseResult pr = null ;
83- try (Prism prism = new Prism ()) {
47+ try (Prism prism = new PrismAOT ()) {
8448 pr = prism .serializeParse (packedOptions , source );
8549 }
8650
@@ -92,45 +56,15 @@ public void test1Aot() {
9256
9357 @ Test
9458 public void test2 () {
95- WasiOptions wasiOpts = WasiOptions .builder ().build ();
96- WasiPreview1 wasi = WasiPreview1 .builder ().withOptions (wasiOpts ).build ();
97- var wasmPrism = Instance .builder (Parser .parse (DummyTest .class .getResourceAsStream ("/prism.wasm" )))
98- .withImportValues (ImportValues .builder ().addFunction (wasi .toHostFunctions ()).build ())
99- .build ();
100- var memory = wasmPrism .memory ();
101- var calloc = wasmPrism .export ("calloc" );
102- var pmSerializeParse = wasmPrism .export ("pm_serialize_parse" );
103- var pmBufferInit = wasmPrism .export ("pm_buffer_init" );
104- var pmBufferSizeof = wasmPrism .export ("pm_buffer_sizeof" );
105- var pmBufferValue = wasmPrism .export ("pm_buffer_value" );
106- var pmBufferLength = wasmPrism .export ("pm_buffer_length" );
107-
10859 // The Ruby source code to be processed
10960 var source = "puts \" h\n e\n l\n l\n o\n \" " ;
110- var sourceBytes = source .getBytes (StandardCharsets .US_ASCII );
11161
112- var sourcePointer = calloc .apply (1 , source .length ());
113- memory .writeString ((int ) sourcePointer [0 ], source );
114-
115- var optionsPointer = calloc .apply (1 , packedOptions .length );
116- memory .write ((int ) optionsPointer [0 ], packedOptions );
117-
118- var bufferPointer = calloc .apply (pmBufferSizeof .apply ()[0 ], 1 );
119- pmBufferInit .apply (bufferPointer );
120-
121- pmSerializeParse .apply (
122- bufferPointer [0 ], sourcePointer [0 ], source .length (), optionsPointer [0 ]);
123-
124- var result = memory .readBytes (
125- (int ) pmBufferValue .apply (bufferPointer [0 ])[0 ],
126- (int ) pmBufferLength .apply (bufferPointer [0 ])[0 ]);
127-
128- System .out .println ("RESULT: " + new String (result ));
129-
130- ParseResult pr = Loader .load (result , sourceBytes );
62+ ParseResult pr = null ;
63+ try (Prism prism = new PrismWASM ()) {
64+ pr = prism .serializeParse (packedOptions , source );
65+ }
13166
13267 assertEquals (1 , pr .value .childNodes ().length );
133-
13468 System .out .println ("Nodes:" );
13569 System .out .println (pr .value .childNodes ()[0 ]);
13670 assertTrue (pr .value .childNodes ()[0 ].toString ().contains ("CallNode" ));
@@ -142,7 +76,7 @@ public void test2Aot() {
14276 var source = "puts \" h\n e\n l\n l\n o\n \" " ;
14377
14478 ParseResult pr = null ;
145- try (Prism prism = new Prism ()) {
79+ try (Prism prism = new PrismAOT ()) {
14680 pr = prism .serializeParse (packedOptions , source );
14781 }
14882
0 commit comments