Skip to content

Commit f22c70c

Browse files
committed
Duplicate WASM artifact for full and start hooking up
1 parent c9c68c6 commit f22c70c

File tree

10 files changed

+275
-12
lines changed

10 files changed

+275
-12
lines changed

Makefile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ all: shared static
3131
shared: build/libprism.$(SOEXT)
3232
static: build/libprism.a
3333
wasm: javascript/src/prism.wasm
34-
java-wasm: java/wasm/src/main/wasm/prism.wasm
34+
java-wasm: java/wasm/src/main/wasm/prism.wasm java/wasm-full/src/main/wasm/prism-full.wasm
3535

3636
build/libprism.$(SOEXT): $(SHARED_OBJECTS)
3737
$(ECHO) "linking $@ with $(CC)"
@@ -57,6 +57,17 @@ java/wasm/src/main/wasm/prism.wasm: Makefile $(SOURCES) $(HEADERS)
5757
$(Q) $(WASI_SDK_PATH)/bin/clang \
5858
$(DEBUG_FLAGS) \
5959
-DPRISM_EXCLUDE_PRETTYPRINT -DPRISM_EXPORT_SYMBOLS -D_WASI_EMULATED_MMAN -DPRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS=1 \
60+
-DPRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS \
61+
-lwasi-emulated-mman $(CPPFLAGS) $(JAVA_WASM_CFLAGS) \
62+
-Wl,--export-all -Wl,--no-entry -mexec-model=reactor -lc++ -lc++abi \
63+
-o $@ $(SOURCES)
64+
65+
java/wasm-full/src/main/wasm/prism-full.wasm: Makefile $(SOURCES) $(HEADERS)
66+
$(ECHO) "building $@"
67+
$(Q) $(MAKEDIRS) $(@D)
68+
$(Q) $(WASI_SDK_PATH)/bin/clang \
69+
$(DEBUG_FLAGS) \
70+
-DPRISM_EXCLUDE_PRETTYPRINT -DPRISM_EXPORT_SYMBOLS -D_WASI_EMULATED_MMAN \
6071
-lwasi-emulated-mman $(CPPFLAGS) $(JAVA_WASM_CFLAGS) \
6172
-Wl,--export-all -Wl,--no-entry -mexec-model=reactor -lc++ -lc++abi \
6273
-o $@ $(SOURCES)

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ CLOBBER.concat(Prism::Template::TEMPLATES)
5555
CLOBBER.concat(["build"])
5656
CLOBBER << "lib/prism/prism.#{RbConfig::CONFIG["DLEXT"]}"
5757
CLOBBER << "java/wasm/src/main/resources/prism.wasm"
58+
CLOBBER << "java/wasm-full/src/main/resources/prism-full.wasm"
5859

5960
Prism::Template::TEMPLATES.each do |filepath|
6061
desc "Generate #{filepath}"

java/api/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</parent>
1010

1111
<artifactId>prism-parser-api</artifactId>
12-
<name>Java Prism</name>
12+
<name>Java Prism API</name>
1313
<description>Java API for the Prism Ruby language parser</description>
1414
<url>https://github.com/ruby/prism</url>
1515

java/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<artifactId>prism-parser</artifactId>
77
<version>0.0.2-SNAPSHOT</version>
88
<packaging>pom</packaging>
9-
<name>Java Prism</name>
9+
<name>Java Prism parent</name>
1010
<description>Java API for the Prism Ruby language parser</description>
1111
<url>https://github.com/ruby/prism</url>
1212

@@ -51,6 +51,7 @@
5151
<modules>
5252
<module>api</module>
5353
<module>wasm</module>
54+
<module>wasm-full</module>
5455
</modules>
5556

5657
<profiles>

java/wasm-full/pom.xml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.ruby-lang</groupId>
7+
<artifactId>prism-parser</artifactId>
8+
<version>0.0.2-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>prism-parser-wasm-full</artifactId>
12+
<name>Java Prism WASM with full parsed content</name>
13+
<description>Java WASM bindings for the Prism parser shared library</description>
14+
<url>https://github.com/ruby/prism</url>
15+
16+
<properties>
17+
<chicory.version>1.7.5</chicory.version>
18+
</properties>
19+
20+
<dependencyManagement>
21+
<dependencies>
22+
<dependency>
23+
<groupId>com.dylibso.chicory</groupId>
24+
<artifactId>bom</artifactId>
25+
<version>${chicory.version}</version>
26+
<type>pom</type>
27+
<scope>import</scope>
28+
</dependency>
29+
</dependencies>
30+
</dependencyManagement>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>com.dylibso.chicory</groupId>
35+
<artifactId>runtime</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>com.dylibso.chicory</groupId>
39+
<artifactId>log</artifactId>
40+
</dependency>
41+
<dependency>
42+
<groupId>com.dylibso.chicory</groupId>
43+
<artifactId>wasi</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>com.dylibso.chicory</groupId>
47+
<artifactId>wasm</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.junit.jupiter</groupId>
51+
<artifactId>junit-jupiter-engine</artifactId>
52+
<version>${junit.version}</version>
53+
<scope>test</scope>
54+
</dependency>
55+
</dependencies>
56+
57+
<build>
58+
<plugins>
59+
<plugin>
60+
<groupId>com.dylibso.chicory</groupId>
61+
<artifactId>chicory-compiler-maven-plugin</artifactId>
62+
<version>${chicory.version}</version>
63+
<executions>
64+
<execution>
65+
<id>prism</id>
66+
<goals>
67+
<goal>compile</goal>
68+
</goals>
69+
<configuration>
70+
<name>org.ruby_lang.prism.wasm.full.PrismParser</name>
71+
<moduleInterface>org.ruby_lang.prism.wasm.full.Prism</moduleInterface>
72+
<wasmFile>src/main/wasm/prism-full.wasm</wasmFile>
73+
</configuration>
74+
</execution>
75+
</executions>
76+
</plugin>
77+
</plugins>
78+
</build>
79+
80+
</project>
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package org.ruby_lang.prism.wasm.full;
2+
3+
import com.dylibso.chicory.runtime.ByteArrayMemory;
4+
import com.dylibso.chicory.runtime.ImportValues;
5+
import com.dylibso.chicory.runtime.Instance;
6+
import com.dylibso.chicory.wasi.WasiOptions;
7+
import com.dylibso.chicory.wasi.WasiPreview1;
8+
9+
import java.nio.charset.StandardCharsets;
10+
11+
public class Prism implements AutoCloseable {
12+
private final WasiPreview1 wasi;
13+
protected final Prism_ModuleExports exports;
14+
private final Instance instance;
15+
16+
public Prism() {
17+
this(WasiOptions.builder().build());
18+
}
19+
20+
public Prism(WasiOptions wasiOpts) {
21+
wasi = WasiPreview1.builder().withOptions(wasiOpts).build();
22+
instance = Instance.builder(PrismParser.load())
23+
.withMemoryFactory(ByteArrayMemory::new)
24+
.withMachineFactory(PrismParser::create)
25+
.withImportValues(ImportValues.builder().addFunction(wasi.toHostFunctions()).build())
26+
.build();
27+
exports = new Prism_ModuleExports(instance);
28+
}
29+
30+
public String version() {
31+
int versionPointer = exports.pmVersion();
32+
int length = exports.strchr(versionPointer, 0);
33+
34+
return new String(instance.memory().readBytes(versionPointer, length - versionPointer));
35+
}
36+
37+
public byte[] parse(byte[] sourceBytes, byte[] packedOptions) {
38+
try (
39+
Buffer buffer = new Buffer();
40+
Source source = new Source(sourceBytes, 0, sourceBytes.length);
41+
Options options = new Options(packedOptions)) {
42+
43+
return parse(buffer, source, options);
44+
}
45+
}
46+
47+
public byte[] lex(byte[] sourceBytes, byte[] packedOptions) {
48+
try (
49+
Buffer buffer = new Buffer();
50+
Source source = new Source(sourceBytes, 0, sourceBytes.length);
51+
Options options = new Options(packedOptions)) {
52+
53+
return lex(buffer, source, options);
54+
}
55+
}
56+
57+
public byte[] parse(byte[] sourceBytes, int sourceOffset, int sourceLength, byte[] packedOptions) {
58+
try (
59+
Buffer buffer = new Buffer();
60+
Source source = new Source(sourceBytes, sourceOffset, sourceLength);
61+
Options options = new Options(packedOptions)) {
62+
63+
return parse(buffer, source, options);
64+
}
65+
}
66+
67+
public byte[] parse(Buffer buffer, Source source, Options options) {
68+
exports.pmSerializeParse(
69+
buffer.pointer, source.pointer, source.length, options.pointer);
70+
71+
return buffer.read();
72+
}
73+
74+
public byte[] lex(Buffer buffer, Source source, Options options) {
75+
exports.pmSerializeLex(
76+
buffer.pointer, source.pointer, source.length, options.pointer);
77+
78+
return buffer.read();
79+
}
80+
81+
public class Buffer implements AutoCloseable {
82+
final int pointer;
83+
84+
Buffer() {
85+
pointer = exports.pmBufferNew();
86+
clear();
87+
}
88+
89+
public void clear() {
90+
exports.pmBufferClear(pointer);
91+
}
92+
93+
public void close() {
94+
exports.pmBufferFree(pointer);
95+
}
96+
97+
public byte[] read() {
98+
return instance.memory().readBytes(
99+
exports.pmBufferValue(pointer),
100+
exports.pmBufferLength(pointer));
101+
}
102+
}
103+
104+
public class Source implements AutoCloseable{
105+
final int pointer;
106+
final int length;
107+
108+
public Source(int length) {
109+
pointer = exports.calloc(1, length);
110+
this.length = length;
111+
}
112+
113+
public Source(byte[] bytes, int offset, int length) {
114+
this(length + 1);
115+
write(bytes, offset, length);
116+
}
117+
118+
public Source(byte[] bytes) {
119+
this(bytes, 0, bytes.length);
120+
}
121+
122+
public void write(byte[] bytes, int offset, int length) {
123+
assert length + 1 <= this.length;
124+
instance.memory().write(pointer, bytes, offset, length);
125+
instance.memory().writeByte(pointer + length, (byte) 0);
126+
}
127+
128+
public void close() {
129+
exports.free(pointer);
130+
}
131+
}
132+
133+
class Options implements AutoCloseable {
134+
final int pointer;
135+
136+
Options(byte[] packedOptions) {
137+
int pointer = exports.calloc(1, packedOptions.length);
138+
instance.memory().write(pointer, packedOptions);
139+
this.pointer = pointer;
140+
}
141+
142+
public void close() {
143+
exports.free(pointer);
144+
}
145+
}
146+
147+
@Override
148+
public void close() {
149+
if (wasi != null) {
150+
wasi.close();
151+
}
152+
}
153+
}
713 KB
Binary file not shown.

java/wasm/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</parent>
1010

1111
<artifactId>prism-parser-wasm</artifactId>
12-
<name>Java Prism WASM</name>
12+
<name>Java Prism WASM with semantic-only content</name>
1313
<description>Java WASM bindings for the Prism parser shared library</description>
1414
<url>https://github.com/ruby/prism</url>
1515

lib/prism/ffi/common.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ def dump_options(options)
178178

179179
# Required APIs below
180180

181+
def version # :nodoc:
182+
raise NotImplementedError
183+
end
184+
181185
def with_buffer(&b) # :nodoc:
182186
raise NotImplementedError
183187
end

lib/prism/ffi/wasm_ffi.rb

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,52 @@
1212

1313
# Load the prism-parser-wasm jar
1414
require 'jar-dependencies'
15-
require_jar('org.ruby-lang', 'prism-parser-wasm', '0.0.1-SNAPSHOT')
15+
require_jar('org.ruby-lang', 'prism-parser-wasm-full', '0.0.2-SNAPSHOT')
1616
require_jar('com.dylibso.chicory', 'runtime', '1.6.1')
1717
require_jar('com.dylibso.chicory', 'wasi', '1.6.1')
1818
require_jar('com.dylibso.chicory', 'wasm', '1.6.1')
1919
require_jar('com.dylibso.chicory', 'log', '1.6.1')
2020

2121
module Prism # :nodoc:
2222
class WASMCommon < Common # :nodoc:
23-
java_import org.ruby_lang.prism.wasm.Prism
23+
java_import org.ruby_lang.prism.wasm.full.Prism
2424

2525
# TODO: concurrency
26-
PRISM = org.ruby_lang.prism.wasm.Prism.new
26+
PRISM = org.ruby_lang.prism.wasm.full.Prism.new
2727

2828
def version
2929
# The version constant is set by reading the result of calling pm_version.
30-
WASM::PRISM.version
30+
PRISM.version
3131
end
3232

3333
def with_buffer(&b) # :nodoc:
34-
raise NotImplementedError
34+
buffer = Prism::Buffer.new
35+
begin
36+
b.call(buffer)
37+
ensure
38+
buffer.close
39+
end
3540
end
3641

3742
def with_string(string, &b) # :nodoc:
38-
raise NotImplementedError
43+
source = Prism::Source.new(string.to_java_bytes)
44+
begin
45+
b.call(source)
46+
ensure
47+
source.close
48+
end
3949
end
4050

4151
def with_file(string, &b) # :nodoc:
4252
raise NotImplementedError
4353
end
4454

4555
def lex_only(buffer, string, options) # :nodoc:
46-
raise NotImplementedError
56+
String.from_java_bytes(Prism.lex(buffer, string, dump_options(options)))
4757
end
4858

4959
def parse_only(buffer, string, options) # :nodoc:
50-
raise NotImplementedError
60+
String.from_java_bytes(Prism.lex(buffer, string, dump_options(options)))
5161
end
5262

5363
def parse_stream(buffer, callback, eof_callback, options, source) # :nodoc:
@@ -78,4 +88,7 @@ def string_query_local(string) # :nodoc:
7888
raise NotImplementedError
7989
end
8090
end
91+
92+
FFICommon = WASMCommon.new.freeze
93+
private_constant(:FFICommon)
8194
end

0 commit comments

Comments
 (0)