Skip to content

Commit b8afc97

Browse files
authored
Merge pull request #2 from jruby/topic/chicory
Merge in WASM support
2 parents 801fe65 + 70a8b5c commit b8afc97

18 files changed

Lines changed: 308 additions & 14201 deletions

.github/workflows/ci.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Java CI with Maven
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v4
16+
- name: Set up JDK 21
17+
uses: actions/setup-java@v4
18+
with:
19+
java-version: '21'
20+
distribution: 'temurin'
21+
cache: maven
22+
- name: Build with Maven
23+
run: mvn -ntp package --file pom.xml

pom.xml

Lines changed: 89 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,20 @@
44
<groupId>org.jruby</groupId>
55
<artifactId>jruby-prism</artifactId>
66
<packaging>jar</packaging>
7-
<version>1.4.0</version>
7+
<version>2.0.0-SNAPSHOT</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+
<chicory.version>1.2.1</chicory.version>
16+
<junit.version>5.12.1</junit.version>
1517
<maven.compiler.source>21</maven.compiler.source>
16-
<maven.compiler.target>21</maven.compiler.target>
18+
<maven.compiler.target>21</maven.compiler.target>
1719
</properties>
1820

19-
<parent>
20-
<groupId>org.sonatype.oss</groupId>
21-
<artifactId>oss-parent</artifactId>
22-
<version>9</version>
23-
</parent>
24-
2521
<issueManagement>
2622
<system>GitHub</system>
2723
<url>https://github.com/jruby/jruby-prism/issues</url>
@@ -48,17 +44,41 @@
4844
</developer>
4945
</developers>
5046

47+
<repositories>
48+
<repository>
49+
<releases>
50+
<enabled>false</enabled>
51+
</releases>
52+
<snapshots>
53+
<enabled>true</enabled>
54+
</snapshots>
55+
<id>sonatype</id>
56+
<url>https://central.sonatype.com/repository/maven-snapshots/</url>
57+
</repository>
58+
</repositories>
59+
60+
<dependencyManagement>
61+
<dependencies>
62+
<dependency>
63+
<groupId>com.dylibso.chicory</groupId>
64+
<artifactId>bom</artifactId>
65+
<version>${chicory.version}</version>
66+
<type>pom</type>
67+
<scope>import</scope>
68+
</dependency>
69+
</dependencies>
70+
</dependencyManagement>
71+
5172
<dependencies>
5273
<dependency>
5374
<groupId>org.jruby</groupId>
5475
<artifactId>jruby-base</artifactId>
55-
<version>10.0.0.0-SNAPSHOT</version>
76+
<version>10.0.2.0</version>
5677
</dependency>
5778
<dependency>
58-
<groupId>junit</groupId>
59-
<artifactId>junit</artifactId>
60-
<version>4.13.1</version>
61-
<scope>test</scope>
79+
<groupId>org.jruby</groupId>
80+
<artifactId>chicory-prism</artifactId>
81+
<version>0.0.1-SNAPSHOT</version>
6282
</dependency>
6383
</dependencies>
6484

@@ -104,42 +124,12 @@
104124
</configuration>
105125
</plugin>
106126
<plugin>
107-
<groupId>org.apache.maven.plugins</groupId>
108-
<artifactId>maven-gpg-plugin</artifactId>
109-
<version>1.5</version>
110-
<executions>
111-
<execution>
112-
<id>sign-artifacts</id>
113-
<phase>verify</phase>
114-
<goals>
115-
<goal>sign</goal>
116-
</goals>
117-
</execution>
118-
</executions>
119-
</plugin>
120-
<plugin>
121-
<artifactId>maven-source-plugin</artifactId>
122-
<executions>
123-
<execution>
124-
<id>attach-sources</id>
125-
<goals>
126-
<goal>jar-no-fork</goal>
127-
</goals>
128-
</execution>
129-
</executions>
130-
</plugin>
131-
<plugin>
132-
<artifactId>maven-javadoc-plugin</artifactId>
133-
<executions>
134-
<execution>
135-
<id>attach-javadocs</id>
136-
<goals>
137-
<goal>jar</goal>
138-
</goals>
139-
</execution>
140-
</executions>
127+
<groupId>org.sonatype.central</groupId>
128+
<artifactId>central-publishing-maven-plugin</artifactId>
129+
<version>0.7.0</version>
130+
<extensions>true</extensions>
141131
<configuration>
142-
<doclint>none</doclint>
132+
<publishingServerId>central</publishingServerId>
143133
</configuration>
144134
</plugin>
145135
</plugins>
@@ -197,5 +187,56 @@
197187
</plugins>
198188
</build>
199189
</profile>
190+
<profile>
191+
<id>release</id>
192+
<build>
193+
<plugins>
194+
<plugin>
195+
<artifactId>maven-gpg-plugin</artifactId>
196+
<version>1.6</version>
197+
<executions>
198+
<execution>
199+
<id>sign-artifacts</id>
200+
<phase>verify</phase>
201+
<goals>
202+
<goal>sign</goal>
203+
</goals>
204+
</execution>
205+
</executions>
206+
<configuration>
207+
<gpgArguments>
208+
<gpgArgument>--pinentry-mode</gpgArgument>
209+
<gpgArgument>loopback</gpgArgument>
210+
</gpgArguments>
211+
</configuration>
212+
</plugin>
213+
<plugin>
214+
<artifactId>maven-source-plugin</artifactId>
215+
<executions>
216+
<execution>
217+
<id>attach-sources</id>
218+
<goals>
219+
<goal>jar-no-fork</goal>
220+
</goals>
221+
</execution>
222+
</executions>
223+
</plugin>
224+
<plugin>
225+
<artifactId>maven-javadoc-plugin</artifactId>
226+
<executions>
227+
<execution>
228+
<id>attach-javadocs</id>
229+
<goals>
230+
<goal>jar</goal>
231+
</goals>
232+
</execution>
233+
</executions>
234+
<configuration>
235+
<doclint>none</doclint>
236+
</configuration>
237+
</plugin>
238+
</plugins>
239+
</build>
240+
</profile>
200241
</profiles>
201242
</project>

src/main/java/org/jruby/prism/ParserProviderPrism.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,43 @@
11
package org.jruby.prism;
22

3+
import jnr.ffi.LibraryLoader;
34
import org.jruby.Ruby;
45
import org.jruby.ir.builder.IRBuilderFactory;
56
import org.jruby.parser.Parser;
7+
import org.jruby.parser.ParserManager;
68
import org.jruby.parser.ParserProvider;
7-
import org.jruby.prism.parser.ParserPrism;
8-
import org.jruby.prism.parser.ParserBindingPrism;
99
import org.jruby.prism.builder.IRBuilderFactoryPrism;
10+
import org.jruby.prism.parser.ParserBindingPrism;
11+
import org.jruby.prism.parser.ParserPrismNative;
12+
import org.jruby.prism.parser.ParserPrismWasm;
1013

11-
import jnr.ffi.LibraryLoader;
14+
import java.io.File;
1215

1316
public class ParserProviderPrism implements ParserProvider {
1417
private static ParserBindingPrism prismLibrary;
1518

1619
public void initialize(String path) {
17-
if (prismLibrary != null) {
18-
System.out.println("Prism already initialized");
19-
return;
20+
if (new File(path).exists()) {
21+
if (prismLibrary != null) {
22+
System.out.println("Prism already initialized");
23+
return;
24+
}
25+
prismLibrary = LibraryLoader.create(ParserBindingPrism.class).load(path);
26+
// We do something extra here as a side-effect which is how we get an UnsatisfiedLinkError
27+
// If the library didn't in fact find the .so or has other loading problems.
28+
ParserBindingPrism.Buffer buffer = new ParserBindingPrism.Buffer(jnr.ffi.Runtime.getRuntime(prismLibrary));
29+
} else {
30+
prismLibrary = null;
2031
}
21-
prismLibrary = LibraryLoader.create(ParserBindingPrism.class).load(path);
22-
// We do something extra here as a side-effect which is how we get an UnsatisfiedLinkError
23-
// If the library didn't in fact find the .so or has other loading problems.
24-
ParserBindingPrism.Buffer buffer = new ParserBindingPrism.Buffer(jnr.ffi.Runtime.getRuntime(prismLibrary));
2532
}
2633

2734
public Parser getParser(Ruby runtime) {
28-
return new ParserPrism(runtime, prismLibrary);
35+
if (ParserManager.PARSER_WASM || prismLibrary == null) {
36+
// uninitialized dynamic lib or wasm requested
37+
return new ParserPrismWasm(runtime);
38+
}
39+
40+
return new ParserPrismNative(runtime, prismLibrary);
2941
}
3042

3143
public IRBuilderFactory getBuilderFactory() {

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
import org.jruby.util.RegexpOptions;
6363
import org.jruby.util.StringSupport;
6464
import org.jruby.util.cli.Options;
65-
import org.prism.Nodes;
66-
import org.prism.Nodes.*;
65+
import org.ruby_lang.prism.Nodes;
66+
import org.ruby_lang.prism.Nodes.*;
6767
import org.jruby.prism.parser.ParseResultPrism;
6868

6969
import java.math.BigInteger;
@@ -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/builder/LazyMethodDefinitionPrism.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
import org.jruby.ir.builder.IRBuilder;
77
import org.jruby.ir.builder.LazyMethodDefinition;
88
import org.jruby.prism.builder.IRBuilderPrism;
9-
import org.prism.AbstractNodeVisitor;
10-
import org.prism.Nodes;
11-
import org.prism.Nodes.ConstantPathNode;
12-
import org.prism.Nodes.DefNode;
13-
import org.prism.Nodes.InstanceVariableReadNode;
14-
import org.prism.Nodes.InstanceVariableWriteNode;
15-
import org.prism.Nodes.Node;
16-
import org.prism.Nodes.RescueNode;
17-
import org.prism.Nodes.WhenNode;
9+
import org.ruby_lang.prism.AbstractNodeVisitor;
10+
import org.ruby_lang.prism.Nodes;
11+
import org.ruby_lang.prism.Nodes.ConstantPathNode;
12+
import org.ruby_lang.prism.Nodes.DefNode;
13+
import org.ruby_lang.prism.Nodes.InstanceVariableReadNode;
14+
import org.ruby_lang.prism.Nodes.InstanceVariableWriteNode;
15+
import org.ruby_lang.prism.Nodes.Node;
16+
import org.ruby_lang.prism.Nodes.RescueNode;
17+
import org.ruby_lang.prism.Nodes.WhenNode;
1818

1919
import java.util.ArrayList;
2020
import java.util.List;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.jruby.prism.parser;
22

3-
import org.prism.AbstractNodeVisitor;
4-
import org.prism.Nodes;
3+
import org.ruby_lang.prism.AbstractNodeVisitor;
4+
import org.ruby_lang.prism.Nodes;
55

66
public class CoverageLineVisitor extends AbstractNodeVisitor<Void> {
77

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import org.jcodings.Encoding;
44
import org.jruby.Ruby;
55
import org.jruby.util.ByteList;
6-
import org.prism.Loader;
7-
import org.prism.Nodes;
8-
import org.prism.ParseResult;
6+
import org.ruby_lang.prism.Loader;
7+
import org.ruby_lang.prism.Nodes;
8+
import org.ruby_lang.prism.ParseResult;
99

1010
/**
1111
* Extends Loader to override some things which are not generated directly

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.jruby.prism.builder.IRBuilderPrism;
66
import org.jruby.parser.StaticScope;
77
import org.jruby.runtime.DynamicScope;
8-
import org.prism.Nodes;
8+
import org.ruby_lang.prism.Nodes;
99

1010
public class ParseResultPrism implements ParseResult {
1111
final Encoding encoding;

0 commit comments

Comments
 (0)