Class: Wcl::WasmRuntime
- Inherits:
-
Object
- Object
- Wcl::WasmRuntime
- Defined in:
- lib/wcl/wasm_runtime.rb
Overview
Singleton WASM runtime that loads and manages the wcl_wasm module.
Class Method Summary collapse
Instance Method Summary collapse
- #document_blocks(handle) ⇒ Object
- #document_blocks_of_type(handle, kind) ⇒ Object
- #document_diagnostics(handle) ⇒ Object
- #document_free(handle) ⇒ Object
- #document_has_errors(handle) ⇒ Object
- #document_query(handle, query) ⇒ Object
- #document_values(handle) ⇒ Object
-
#initialize ⇒ WasmRuntime
constructor
A new instance of WasmRuntime.
-
#parse(source, options_json) ⇒ Object
– Public API (all synchronized) ————————————.
- #parse_with_functions(source, options_json, func_names_json) ⇒ Object
Constructor Details
#initialize ⇒ WasmRuntime
Returns a new instance of WasmRuntime.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/wcl/wasm_runtime.rb', line 18 def initialize @mutex = Mutex.new @engine = Wasmtime::Engine.new wasm_path = File.join(__dir__, "wcl_wasm.wasm") @module = Wasmtime::Module.from_file(@engine, wasm_path) @linker = Wasmtime::Linker.new(@engine) Wasmtime::WASI::P1.add_to_linker_sync(@linker) define_host_functions @store = Wasmtime::Store.new(@engine, wasi_p1_config: Wasmtime::WasiConfig.new) @wasm_instance = @linker.instantiate(@store, @module) # Cache exported functions @alloc = @wasm_instance.export("wcl_wasm_alloc").to_func @dealloc = @wasm_instance.export("wcl_wasm_dealloc").to_func @string_free = @wasm_instance.export("wcl_wasm_string_free").to_func @parse_fn = @wasm_instance.export("wcl_wasm_parse").to_func @parse_with_functions_fn = @wasm_instance.export("wcl_wasm_parse_with_functions").to_func @doc_free = @wasm_instance.export("wcl_wasm_document_free").to_func @doc_values = @wasm_instance.export("wcl_wasm_document_values").to_func @doc_has_errors = @wasm_instance.export("wcl_wasm_document_has_errors").to_func @doc_diagnostics = @wasm_instance.export("wcl_wasm_document_diagnostics").to_func @doc_query = @wasm_instance.export("wcl_wasm_document_query").to_func @doc_blocks = @wasm_instance.export("wcl_wasm_document_blocks").to_func @doc_blocks_of_type = @wasm_instance.export("wcl_wasm_document_blocks_of_type").to_func @memory = @wasm_instance.export("memory").to_memory end |
Class Method Details
.get ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/wcl/wasm_runtime.rb', line 9 def self.get return @instance if @instance @init_mutex.synchronize do @instance ||= new end @instance end |
Instance Method Details
#document_blocks(handle) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/wcl/wasm_runtime.rb', line 112 def document_blocks(handle) @mutex.synchronize do ptr = @doc_blocks.call(handle) consume_string(ptr) end end |
#document_blocks_of_type(handle, kind) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/wcl/wasm_runtime.rb', line 119 def document_blocks_of_type(handle, kind) @mutex.synchronize do k_ptr = write_string(kind) begin ptr = @doc_blocks_of_type.call(handle, k_ptr) consume_string(ptr) ensure dealloc_string(k_ptr, kind) if k_ptr != 0 end end end |
#document_diagnostics(handle) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/wcl/wasm_runtime.rb', line 93 def document_diagnostics(handle) @mutex.synchronize do ptr = @doc_diagnostics.call(handle) consume_string(ptr) end end |
#document_free(handle) ⇒ Object
78 79 80 |
# File 'lib/wcl/wasm_runtime.rb', line 78 def document_free(handle) @mutex.synchronize { @doc_free.call(handle) } end |
#document_has_errors(handle) ⇒ Object
89 90 91 |
# File 'lib/wcl/wasm_runtime.rb', line 89 def document_has_errors(handle) @mutex.synchronize { @doc_has_errors.call(handle) != 0 } end |
#document_query(handle, query) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/wcl/wasm_runtime.rb', line 100 def document_query(handle, query) @mutex.synchronize do q_ptr = write_string(query) begin ptr = @doc_query.call(handle, q_ptr) consume_string(ptr) ensure dealloc_string(q_ptr, query) if q_ptr != 0 end end end |
#document_values(handle) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/wcl/wasm_runtime.rb', line 82 def document_values(handle) @mutex.synchronize do ptr = @doc_values.call(handle) consume_string(ptr) end end |
#parse(source, options_json) ⇒ Object
– Public API (all synchronized) ————————————
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/wcl/wasm_runtime.rb', line 50 def parse(source, ) @mutex.synchronize do src_ptr = write_string(source) opts_ptr = write_string() begin @parse_fn.call(src_ptr, opts_ptr) ensure dealloc_string(src_ptr, source) if src_ptr != 0 dealloc_string(opts_ptr, ) if opts_ptr != 0 && end end end |
#parse_with_functions(source, options_json, func_names_json) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/wcl/wasm_runtime.rb', line 63 def parse_with_functions(source, , func_names_json) @mutex.synchronize do src_ptr = write_string(source) opts_ptr = write_string() names_ptr = write_string(func_names_json) begin @parse_with_functions_fn.call(src_ptr, opts_ptr, names_ptr) ensure dealloc_string(src_ptr, source) if src_ptr != 0 dealloc_string(opts_ptr, ) if opts_ptr != 0 && dealloc_string(names_ptr, func_names_json) if names_ptr != 0 end end end |