|
| 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 | +} |
0 commit comments