Skip to content

Commit e00e6b3

Browse files
committed
Wire up enough wasm gem to pass dump_test
This passes dump_test 100% with the basic Chicory-wasm backend. Parsing directly from file is stubbed out to simply read the file, which is degraded functionality compared to the FFI version.
1 parent b34d81d commit e00e6b3

4 files changed

Lines changed: 55 additions & 16 deletions

File tree

java/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ api/target
33
native/target
44
wasm/src/main/wasm
55
wasm/target
6+
wasm-full/src/main/wasm
7+
wasm-full/target
68
target
79
.idea

java/wasm-full/src/main/java/org/ruby_lang/prism/wasm/full/Prism.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ public byte[] parse(Buffer buffer, Source source, Options options) {
6868
exports.pmSerializeParse(
6969
buffer.pointer, source.pointer, source.length, options.pointer);
7070

71-
return buffer.read();
71+
return buffer.readBytes();
7272
}
7373

7474
public byte[] lex(Buffer buffer, Source source, Options options) {
7575
exports.pmSerializeLex(
7676
buffer.pointer, source.pointer, source.length, options.pointer);
7777

78-
return buffer.read();
78+
return buffer.readBytes();
7979
}
8080

8181
public class Buffer implements AutoCloseable {
@@ -94,13 +94,17 @@ public void close() {
9494
exports.pmBufferFree(pointer);
9595
}
9696

97-
public byte[] read() {
97+
public byte[] readBytes() {
9898
return instance.memory().readBytes(
9999
exports.pmBufferValue(pointer),
100100
exports.pmBufferLength(pointer));
101101
}
102102
}
103103

104+
public Buffer newBuffer() {
105+
return new Buffer();
106+
}
107+
104108
public class Source implements AutoCloseable{
105109
final int pointer;
106110
final int length;
@@ -130,6 +134,10 @@ public void close() {
130134
}
131135
}
132136

137+
public Source newSource(byte[] bytes) {
138+
return new Source(bytes);
139+
}
140+
133141
class Options implements AutoCloseable {
134142
final int pointer;
135143

@@ -144,6 +152,10 @@ public void close() {
144152
}
145153
}
146154

155+
public Options newOptions(byte[] packedOptions) {
156+
return new Options(packedOptions);
157+
}
158+
147159
@Override
148160
public void close() {
149161
if (wasi != null) {

lib/prism/ffi.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ def dump(source, **options)
3535
# Mirror the Prism.dump_file API by using the serialization API.
3636
def dump_file(filepath, **options)
3737
options[:filepath] = filepath
38-
FFICommon.with_file(filepath) { |string| FFICommon.dump(string, options) }
38+
FFICommon.with_file(filepath) do |file|
39+
FFICommon.with_string(file.read) do |string|
40+
FFICommon.dump(string, options)
41+
end
42+
end
3943
end
4044

4145
# Mirror the Prism.lex API by using the serialization API.
@@ -46,7 +50,12 @@ def lex(code, **options)
4650
# Mirror the Prism.lex_file API by using the serialization API.
4751
def lex_file(filepath, **options)
4852
options[:filepath] = filepath
49-
FFICommon.with_file(filepath) { |string| FFICommon.lex(string, string.read, options) }
53+
FFICommon.with_file(filepath) do |file|
54+
code = file.read
55+
FFICommon.with_string(code) do |string|
56+
FFICommon.lex(string, code, options)
57+
end
58+
end
5059
end
5160

5261
# Mirror the Prism.parse API by using the serialization API.
@@ -59,7 +68,12 @@ def parse(code, **options)
5968
# when it is available.
6069
def parse_file(filepath, **options)
6170
options[:filepath] = filepath
62-
FFICommon.with_file(filepath) { |string| FFICommon.parse(string, string.read, options) }
71+
FFICommon.with_file(filepath) do |file|
72+
code = file.read
73+
FFICommon.with_string(code) do |string|
74+
FFICommon.parse(string, code, options)
75+
end
76+
end
6377
end
6478

6579
# Mirror the Prism.parse_stream API by using the serialization API.

lib/prism/ffi/wasm_ffi.rb

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212

1313
# Load the prism-parser-wasm jar
1414
require 'jar-dependencies'
15-
require_jar('org.ruby-lang', 'prism-parser-wasm-full', '0.0.2-SNAPSHOT')
16-
require_jar('com.dylibso.chicory', 'runtime', '1.6.1')
17-
require_jar('com.dylibso.chicory', 'wasi', '1.6.1')
18-
require_jar('com.dylibso.chicory', 'wasm', '1.6.1')
19-
require_jar('com.dylibso.chicory', 'log', '1.6.1')
15+
require_jar('org.ruby-lang', 'prism-parser-wasm-full', '0.0.4-SNAPSHOT')
16+
chicory_version = '1.7.5'
17+
redline_version = '0.0.4'
18+
require_jar('com.dylibso.chicory', 'runtime', chicory_version)
19+
require_jar('com.dylibso.chicory', 'wasi', chicory_version)
20+
require_jar('com.dylibso.chicory', 'wasm', chicory_version)
21+
require_jar('com.dylibso.chicory', 'log', chicory_version)
22+
require_jar('io.roastedroot', 'redline', redline_version)
2023

2124
module Prism # :nodoc:
2225
class WASMCommon < Common # :nodoc:
@@ -31,16 +34,22 @@ def version
3134
end
3235

3336
def with_buffer(&b) # :nodoc:
34-
buffer = Prism::Buffer.new
37+
buffer = PRISM.new_buffer
3538
begin
3639
b.call(buffer)
3740
ensure
3841
buffer.close
3942
end
4043
end
4144

45+
class Java::org.ruby_lang.prism.wasm.full.Prism::Buffer
46+
def read
47+
String.from_java_bytes(read_bytes)
48+
end
49+
end
50+
4251
def with_string(string, &b) # :nodoc:
43-
source = Prism::Source.new(string.to_java_bytes)
52+
source = PRISM.new_source(string.to_java_bytes)
4453
begin
4554
b.call(source)
4655
ensure
@@ -49,15 +58,17 @@ def with_string(string, &b) # :nodoc:
4958
end
5059

5160
def with_file(string, &b) # :nodoc:
52-
raise NotImplementedError
61+
File.open(string, "rb") do |file|
62+
b.call(file)
63+
end
5364
end
5465

5566
def lex_only(buffer, string, options) # :nodoc:
56-
String.from_java_bytes(Prism.lex(buffer, string, dump_options(options)))
67+
String.from_java_bytes(PRISM.lex(buffer, string, PRISM.new_options(dump_options(options).to_java_bytes)))
5768
end
5869

5970
def parse_only(buffer, string, options) # :nodoc:
60-
String.from_java_bytes(Prism.lex(buffer, string, dump_options(options)))
71+
String.from_java_bytes(PRISM.parse(buffer, string, PRISM.new_options(dump_options(options).to_java_bytes)))
6172
end
6273

6374
def parse_stream(buffer, callback, eof_callback, options, source) # :nodoc:

0 commit comments

Comments
 (0)