Generate the Factory struct, constructor, and methods.
(&self, tokens: &mut Tokens<Go>)
| 71 | |
| 72 | /// Generate the Factory struct, constructor, and methods. |
| 73 | fn generate_factory(&self, tokens: &mut Tokens<Go>) { |
| 74 | let AnalyzedImports { |
| 75 | factory_name, |
| 76 | instance_name, |
| 77 | constructor_name, |
| 78 | .. |
| 79 | } = &self.config.analyzed_imports; |
| 80 | let wasm_var_name = self.config.wasm_var_name; |
| 81 | // Build the parameter list |
| 82 | let params = self.build_parameters(); |
| 83 | quote_in! { *tokens => |
| 84 | $['\n'] |
| 85 | type $factory_name struct { |
| 86 | runtime $WAZERO_RUNTIME |
| 87 | module $WAZERO_COMPILED_MODULE |
| 88 | } |
| 89 | $['\n'] |
| 90 | func $constructor_name( |
| 91 | $['\r'] |
| 92 | $params |
| 93 | $['\r'] |
| 94 | ) (*$factory_name, error) { |
| 95 | wazeroRuntime := $WAZERO_NEW_RUNTIME(ctx) |
| 96 | |
| 97 | $(for chain in self.config.import_chains.values() => |
| 98 | $chain |
| 99 | $['\r'] |
| 100 | ) |
| 101 | |
| 102 | $(comment(&[ |
| 103 | "Compiling the module takes a LONG time, so we want to do it once and hold", |
| 104 | "onto it with the Runtime", |
| 105 | ])) |
| 106 | module, err := wazeroRuntime.CompileModule(ctx, $wasm_var_name) |
| 107 | if err != nil { |
| 108 | return nil, err |
| 109 | } |
| 110 | return &$factory_name{ |
| 111 | runtime: wazeroRuntime, |
| 112 | module: module, |
| 113 | }, nil |
| 114 | } |
| 115 | $['\n'] |
| 116 | func (f *$factory_name) Instantiate(ctx $CONTEXT_CONTEXT) (*$instance_name, error) { |
| 117 | if module, err := f.runtime.InstantiateModule(ctx, f.module, $WAZERO_NEW_MODULE_CONFIG()); err != nil { |
| 118 | return nil, err |
| 119 | } else { |
| 120 | return &$instance_name{module}, nil |
| 121 | } |
| 122 | } |
| 123 | $['\n'] |
| 124 | func (f *$factory_name) Close(ctx $CONTEXT_CONTEXT) { |
| 125 | f.runtime.Close(ctx) |
| 126 | } |
| 127 | $['\n'] |
| 128 | }; |
| 129 | } |
| 130 |
no test coverage detected