| 178 | * @returns {ArrayBufferTransform} transform |
| 179 | */ |
| 180 | const rewriteImportedGlobals = (state) => (bin) => { |
| 181 | const additionalInitCode = state.additionalInitCode; |
| 182 | /** @type {t.Global[]} */ |
| 183 | const newGlobals = []; |
| 184 | |
| 185 | bin = editWithAST(state.ast, bin, { |
| 186 | ModuleImport(path) { |
| 187 | if (t.isGlobalType(path.node.descr)) { |
| 188 | const globalType = |
| 189 | /** @type {GlobalType} */ |
| 190 | (path.node.descr); |
| 191 | |
| 192 | globalType.mutability = "var"; |
| 193 | |
| 194 | const init = [ |
| 195 | createDefaultInitForGlobal(globalType), |
| 196 | t.instruction("end") |
| 197 | ]; |
| 198 | |
| 199 | newGlobals.push(t.global(globalType, init)); |
| 200 | |
| 201 | path.remove(); |
| 202 | } |
| 203 | }, |
| 204 | |
| 205 | // in order to preserve non-imported global's order we need to re-inject |
| 206 | // those as well |
| 207 | /** |
| 208 | * Processes the provided path. |
| 209 | * @param {NodePath<Global>} path path |
| 210 | */ |
| 211 | Global(path) { |
| 212 | const { node } = path; |
| 213 | const [init] = node.init; |
| 214 | |
| 215 | if (init.id === "get_global") { |
| 216 | node.globalType.mutability = "var"; |
| 217 | |
| 218 | const initialGlobalIdx = init.args[0]; |
| 219 | |
| 220 | node.init = [ |
| 221 | createDefaultInitForGlobal(node.globalType), |
| 222 | t.instruction("end") |
| 223 | ]; |
| 224 | |
| 225 | additionalInitCode.push( |
| 226 | /** |
| 227 | * get_global in global initializer only works for imported globals. |
| 228 | * They have the same indices as the init params, so use the |
| 229 | * same index. |
| 230 | */ |
| 231 | t.instruction("get_local", [initialGlobalIdx]), |
| 232 | t.instruction("set_global", [t.indexLiteral(newGlobals.length)]) |
| 233 | ); |
| 234 | } |
| 235 | |
| 236 | newGlobals.push(node); |
| 237 | |