1111import org .jruby .ext .coverage .CoverageData ;
1212import org .jruby .management .ParserStats ;
1313import org .jruby .parser .Parser ;
14+ import org .jruby .parser .ParserManager ;
1415import org .jruby .parser .ParserType ;
1516import org .jruby .parser .StaticScope ;
1617import org .jruby .runtime .DynamicScope ;
2324import org .prism .Nodes ;
2425import org .prism .Nodes .*;
2526import org .prism .ParsingOptions ;
27+ import org .prism .Prism ;
2628
2729import java .io .ByteArrayInputStream ;
2830import java .io .DataInputStream ;
3234import java .util .Arrays ;
3335import java .util .List ;
3436
37+ import static org .jruby .api .Convert .asSymbol ;
3538import static org .jruby .lexer .LexingCommon .DOLLAR_UNDERSCORE ;
3639import static org .jruby .parser .ParserType .EVAL ;
3740import static org .jruby .parser .ParserType .MAIN ;
@@ -173,7 +176,7 @@ private byte[] loadFully(String fileName, InputStream in) {
173176
174177
175178 private byte [] parse (byte [] source , int sourceLength , byte [] metadata ) {
176- // if (ParserManager.PARSER_WASM) return parseChicory(source, sourceLength, metadata);
179+ if (ParserManager .PARSER_WASM ) return parseChicory (source , sourceLength , metadata );
177180
178181 long time = 0 ;
179182 if (parserTiming ) time = System .nanoTime ();
@@ -194,21 +197,12 @@ private byte[] parse(byte[] source, int sourceLength, byte[] metadata) {
194197 return src ;
195198 }
196199
197- /*
198- private byte[] parseChicory(byte[] source, int sourceLength, byte[] metadata) {
199- long time = 0;
200- if (parserTiming) time = System.nanoTime();
201-
202- byte[] serialized = prismWasmWrapper.parse(source, sourceLength, metadata);
203-
204- if (parserTiming) {
205- ParserStats stats = runtime.getParserManager().getParserStats();
206200
207- stats.addYARPTimeCParseSerialize(System.nanoTime() - time);
201+ private byte [] parseChicory (byte [] source , int sourceLength , byte [] metadata ) {
202+ try (Prism prism = new Prism ()) {
203+ return prism .serialize (metadata , source , sourceLength );
208204 }
209-
210- return serialized;
211- }*/
205+ }
212206
213207 // lineNumber (0-indexed)
214208 private byte [] generateMetadata (String fileName , int lineNumber , Encoding encoding , DynamicScope scope , ParserType type ) {
@@ -332,42 +326,43 @@ public IRubyObject getLineStub(ThreadContext context, ParseResult arg, int lineC
332326 // show it happening on line 1 (which is what it should do).
333327 @ Override
334328 public ParseResult addGetsLoop (Ruby runtime , ParseResult result , boolean printing , boolean processLineEndings , boolean split ) {
329+ var context = runtime .getCurrentContext ();
335330 List <Nodes .Node > newBody = new ArrayList <>();
336331
337332 if (processLineEndings ) {
338- newBody .add (new Nodes .GlobalVariableWriteNode (0 , 0 , runtime . newSymbol ( CommonByteLists .DOLLAR_BACKSLASH ),
339- new GlobalVariableReadNode (0 , 0 , runtime . newSymbol ( CommonByteLists .DOLLAR_SLASH ))));
333+ newBody .add (new Nodes .GlobalVariableWriteNode (- 1 , 0 , 0 , asSymbol ( context , CommonByteLists .DOLLAR_BACKSLASH ),
334+ new GlobalVariableReadNode (- 1 , 0 , 0 , asSymbol ( context , CommonByteLists .DOLLAR_SLASH ))));
340335 }
341336
342- Nodes .GlobalVariableReadNode dollarUnderscore = new GlobalVariableReadNode (0 , 0 , runtime . newSymbol ( DOLLAR_UNDERSCORE ));
337+ Nodes .GlobalVariableReadNode dollarUnderscore = new GlobalVariableReadNode (- 1 , 0 , 0 , asSymbol ( context , DOLLAR_UNDERSCORE ));
343338
344339 List <Nodes .Node > whileBody = new ArrayList <>();
345340
346341 if (processLineEndings ) {
347- whileBody .add (new CallNode (0 , 0 , (short ) 0 , dollarUnderscore , runtime . newSymbol ( "chomp!" ), null , null ));
342+ whileBody .add (new CallNode (- 1 , 0 , 0 , (short ) 0 , dollarUnderscore , asSymbol ( context , "chomp!" ), null , null ));
348343 }
349344 if (split ) {
350- whileBody .add (new GlobalVariableWriteNode (0 , 0 , runtime . newSymbol ( "$F" ),
351- new Nodes .CallNode (0 , 0 , (short ) 0 , dollarUnderscore , runtime . newSymbol ( "split" ), null , null )));
345+ whileBody .add (new GlobalVariableWriteNode (- 1 , 0 , 0 , asSymbol ( context , "$F" ),
346+ new Nodes .CallNode (- 1 , 0 , 0 , (short ) 0 , dollarUnderscore , asSymbol ( context , "split" ), null , null )));
352347 }
353348
354349 StatementsNode stmts = ((ProgramNode ) result .getAST ()).statements ;
355350 if (stmts != null && stmts .body != null ) whileBody .addAll (Arrays .asList (stmts .body ));
356351
357- ArgumentsNode args = new ArgumentsNode (0 , 0 , (short ) 0 , new Node [] { dollarUnderscore });
358- if (printing ) whileBody .add (new CallNode (0 , 0 , (short ) 0 , null , runtime . newSymbol ( "print" ), args , null ));
352+ ArgumentsNode args = new ArgumentsNode (- 1 , 0 , 0 , (short ) 0 , new Node [] { dollarUnderscore });
353+ if (printing ) whileBody .add (new CallNode (- 1 , 0 , 0 , (short ) 0 , null , asSymbol ( context , "print" ), args , null ));
359354
360355 Node [] nodes = new Node [whileBody .size ()];
361356 whileBody .toArray (nodes );
362- StatementsNode statements = new StatementsNode (0 , 0 , nodes );
357+ StatementsNode statements = new StatementsNode (- 1 , 0 , 0 , nodes );
363358
364- newBody .add (new WhileNode (0 , 0 , (short ) 0 ,
365- new CallNode (0 , 0 , CallNodeFlags .VARIABLE_CALL , null , runtime . newSymbol ( "gets" ), null , null ),
359+ newBody .add (new WhileNode (- 1 , 0 , 0 , (short ) 0 ,
360+ new CallNode (- 1 , 0 , 0 , CallNodeFlags .VARIABLE_CALL , null , asSymbol ( context , "gets" ), null , null ),
366361 statements ));
367362
368363 nodes = new Node [newBody .size ()];
369364 newBody .toArray (nodes );
370- Nodes .ProgramNode newRoot = new Nodes .ProgramNode (0 , 0 , new RubySymbol [] {}, new StatementsNode (0 , 0 , nodes ));
365+ Nodes .ProgramNode newRoot = new Nodes .ProgramNode (- 1 , 0 , 0 , new RubySymbol [] {}, new StatementsNode (- 1 , 0 , 0 , nodes ));
371366
372367 ((ParseResultPrism ) result ).setRoot (newRoot );
373368
0 commit comments