Skip to content

Commit 959f954

Browse files
committed
Dumping incomplete state but it has some chicory stuff in it
1 parent 801fe65 commit 959f954

10 files changed

Lines changed: 53 additions & 14040 deletions

File tree

pom.xml

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
<groupId>org.jruby</groupId>
55
<artifactId>jruby-prism</artifactId>
66
<packaging>jar</packaging>
7-
<version>1.4.0</version>
7+
<version>1.5.0</version>
88
<name>jruby-prism</name>
99
<description>
1010
Java portion of JRuby Prism parser support.
1111
</description>
1212

1313
<properties>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15-
<maven.compiler.source>21</maven.compiler.source>
16-
<maven.compiler.target>21</maven.compiler.target>
15+
<chicory.version>1.2.1</chicory.version>
16+
<junit.version>5.12.1</junit.version>
17+
<maven.compiler.source>17</maven.compiler.source>
18+
<maven.compiler.target>17</maven.compiler.target>
1719
</properties>
1820

1921
<parent>
@@ -48,17 +50,28 @@
4850
</developer>
4951
</developers>
5052

53+
<dependencyManagement>
54+
<dependencies>
55+
<dependency>
56+
<groupId>com.dylibso.chicory</groupId>
57+
<artifactId>bom</artifactId>
58+
<version>${chicory.version}</version>
59+
<type>pom</type>
60+
<scope>import</scope>
61+
</dependency>
62+
</dependencies>
63+
</dependencyManagement>
64+
5165
<dependencies>
5266
<dependency>
5367
<groupId>org.jruby</groupId>
5468
<artifactId>jruby-base</artifactId>
5569
<version>10.0.0.0-SNAPSHOT</version>
5670
</dependency>
5771
<dependency>
58-
<groupId>junit</groupId>
59-
<artifactId>junit</artifactId>
60-
<version>4.13.1</version>
61-
<scope>test</scope>
72+
<groupId>com.prism</groupId>
73+
<artifactId>java-prism</artifactId>
74+
<version>999-SNAPSHOT</version>
6275
</dependency>
6376
</dependencies>
6477

src/main/java/org/jruby/prism/builder/IRBuilderPrism.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -731,10 +731,13 @@ private Operand buildCall(Variable resultArg, CallNode node, RubySymbol name, La
731731
}
732732
}
733733

734+
boolean reusingLabels = false;
734735
if (node.isSafeNavigation()) {
735736
if (lazyLabel == null) {
736737
lazyLabel = getNewLabel();
737738
endLabel = getNewLabel();
739+
} else {
740+
reusingLabels = true;
738741
}
739742
}
740743

@@ -758,9 +761,11 @@ private Operand buildCall(Variable resultArg, CallNode node, RubySymbol name, La
758761

759762
if (node.isSafeNavigation()) {
760763
addInstr(new JumpInstr(endLabel));
761-
addInstr(new LabelInstr(lazyLabel));
762-
addInstr(new CopyInstr(result, nil()));
763-
addInstr(new LabelInstr(endLabel));
764+
if (!reusingLabels) { // This already exists.
765+
addInstr(new LabelInstr(lazyLabel));
766+
addInstr(new CopyInstr(result, nil()));
767+
addInstr(new LabelInstr(endLabel));
768+
}
764769
}
765770

766771
return result;
@@ -1366,10 +1371,10 @@ private Operand buildHash(Node[] elements, boolean hasAssignments) {
13661371
if (!keysHack.add(hack)) getManager().getRuntime().getWarnings().warn("key :" + hack + " is duplicated and overwritten on line " + (getLine(key) + 1));
13671372
} else if (keyOperand instanceof Fixnum) {
13681373
long hack = ((Fixnum) keyOperand).value;
1369-
if (!keysHack.add(new Long(hack))) getManager().getRuntime().getWarnings().warn("key " + hack + " is duplicated and overwritten on line " + (getLine(key) + 1));
1374+
if (!keysHack.add(Long.valueOf(hack))) getManager().getRuntime().getWarnings().warn("key " + hack + " is duplicated and overwritten on line " + (getLine(key) + 1));
13701375
} else if (keyOperand instanceof Float) {
13711376
double hack = ((Float) keyOperand).value;
1372-
if (!keysHack.add(new Double(hack))) getManager().getRuntime().getWarnings().warn("key " + hack + " is duplicated and overwritten on line " + (getLine(key) + 1));
1377+
if (!keysHack.add(Double.valueOf(hack))) getManager().getRuntime().getWarnings().warn("key " + hack + " is duplicated and overwritten on line " + (getLine(key) + 1));
13731378
}
13741379
}
13751380
args.add(new KeyValuePair<>(keyOperand, buildWithOrder(((AssocNode) pair).value, hasAssignments)));
@@ -1470,7 +1475,7 @@ protected Operand buildDRegex(Variable result, Node[] children, RegexpOptions op
14701475
// value of the regexp. Adding an empty string will pick up the encoding from options (this
14711476
// empty string is how legacy parsers do this but it naturally falls out of the parser.
14721477
pieces = new Node[children.length + 1];
1473-
pieces[0] = new StringNode(0, 0, (short) 0, EMPTY.bytes());
1478+
pieces[0] = new StringNode(-1, 0, 0, (short) 0, EMPTY.bytes());
14741479
pieces[1] = children[0];
14751480
} else {
14761481
pieces = children;
@@ -1603,7 +1608,7 @@ private Operand buildMatchPredicate(MatchPredicateNode node) {
16031608
}
16041609

16051610
private Operand buildMatchRequired(MatchRequiredNode node) {
1606-
return buildPatternCase(node.value, new Node[] { new InNode(0, 0, node.pattern, null) }, null);
1611+
return buildPatternCase(node.value, new Node[] { new InNode(-1, 0, 0, node.pattern, null) }, null);
16071612
}
16081613

16091614
private Operand buildMatchWrite(Variable result, MatchWriteNode node) {

src/main/java/org/jruby/prism/parser/ParserPrism.java

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.jruby.ext.coverage.CoverageData;
1212
import org.jruby.management.ParserStats;
1313
import org.jruby.parser.Parser;
14+
import org.jruby.parser.ParserManager;
1415
import org.jruby.parser.ParserType;
1516
import org.jruby.parser.StaticScope;
1617
import org.jruby.runtime.DynamicScope;
@@ -23,6 +24,7 @@
2324
import org.prism.Nodes;
2425
import org.prism.Nodes.*;
2526
import org.prism.ParsingOptions;
27+
import org.prism.Prism;
2628

2729
import java.io.ByteArrayInputStream;
2830
import java.io.DataInputStream;
@@ -32,6 +34,7 @@
3234
import java.util.Arrays;
3335
import java.util.List;
3436

37+
import static org.jruby.api.Convert.asSymbol;
3538
import static org.jruby.lexer.LexingCommon.DOLLAR_UNDERSCORE;
3639
import static org.jruby.parser.ParserType.EVAL;
3740
import 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

Comments
 (0)