Skip to content

Commit bc85717

Browse files
committed
Wire up parse_lex and parse_lex_file for wasm
This also degrades mmap'ed file support but I will figure out a good abstraction to restore that later.
1 parent 1351b26 commit bc85717

5 files changed

Lines changed: 26 additions & 11 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ public byte[] lex(Buffer buffer, Source source, Options options) {
7878
return buffer.readBytes();
7979
}
8080

81+
public byte[] parseLex(Buffer buffer, Source source, Options options) {
82+
exports.pmSerializeParseLex(buffer.pointer, source.pointer, source.length, options.pointer);
83+
84+
return buffer.readBytes();
85+
}
86+
8187
public class Buffer implements AutoCloseable {
8288
final int pointer;
8389

lib/prism/ffi.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ def parse_lex(code, **options)
116116
# Mirror the Prism.parse_lex_file API by using the serialization API.
117117
def parse_lex_file(filepath, **options)
118118
options[:filepath] = filepath
119-
FFICommon.with_file(filepath) { |string| FFICommon.parse_lex(string, string.read, options) }
119+
FFICommon.with_file(filepath) do |file|
120+
code = file.read
121+
FFICommon.with_string(code) do |string|
122+
FFICommon.parse_lex(string, code, options)
123+
end
124+
end
120125
end
121126

122127
# Mirror the Prism.parse_success? API by using the serialization API.

lib/prism/ffi/common.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ def lex(string, code, options) # :nodoc:
2828
end
2929
end
3030

31+
def parse_lex(string, code, options) # :nodoc:
32+
with_buffer do |buffer|
33+
parse_lex_only(buffer, string, options)
34+
Serialize.load_parse_lex(code, buffer.read, options.fetch(:freeze, false))
35+
end
36+
end
37+
3138
# Return the value that should be dumped for the command_line option.
3239
def dump_options_command_line(options)
3340
command_line = options.fetch(:command_line, "")
@@ -202,15 +209,15 @@ def parse_only(buffer, string, options) # :nodoc:
202209
raise NotImplementedError
203210
end
204211

205-
def parse_stream(buffer, callback, eof_callback, options, source) # :nodoc:
212+
def parse_lex_only(buffer, string, options) # :nodoc:
206213
raise NotImplementedError
207214
end
208215

209-
def parse_comments(string, code, options) # :nodoc:
216+
def parse_stream(buffer, callback, eof_callback, options, source) # :nodoc:
210217
raise NotImplementedError
211218
end
212219

213-
def parse_lex(string, code, options) # :nodoc:
220+
def parse_comments(string, code, options) # :nodoc:
214221
raise NotImplementedError
215222
end
216223

lib/prism/ffi/native_ffi.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,8 @@ def parse_comments(string, code, options) # :nodoc:
294294
end
295295
end
296296

297-
def parse_lex(string, code, options) # :nodoc:
298-
with_buffer do |buffer|
299-
LibRubyParser.pm_serialize_parse_lex(buffer.pointer, string.pointer, string.length, dump_options(options))
300-
Serialize.load_parse_lex(code, buffer.read, options.fetch(:freeze, false))
301-
end
297+
def parse_lex_only(buffer, string, options) # :nodoc:
298+
LibRubyParser.pm_serialize_parse_lex(buffer.pointer, string.pointer, string.length, dump_options(options))
302299
end
303300

304301
def parse_file_success(string, options) # :nodoc:

lib/prism/ffi/wasm_ffi.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ def parse_comments(string, code, options) # :nodoc:
7979
raise NotImplementedError
8080
end
8181

82-
def parse_lex(string, code, options) # :nodoc:
83-
raise NotImplementedError
82+
def parse_lex_only(buffer, string, options) # :nodoc:
83+
String.from_java_bytes(PRISM.parse_lex(buffer, string, PRISM.new_options(dump_options(options).to_java_bytes)))
8484
end
8585

8686
def parse_file_success(string, options) # :nodoc:

0 commit comments

Comments
 (0)