Skip to content

chore(codegen-wasm): split crates/perry-codegen-wasm/src/emit.rs (10k lines, one mega WasmModuleEmitter impl) #1102

Description

@proggeramlug

Part of #1097.

Current state

crates/perry-codegen-wasm/src/emit.rs is 9,988 lines with only 12 top-level functions — because almost everything is methods on the WasmModuleEmitter struct (one giant impl block starting at line 616).

The two methods that dominate the file are:

  • emit_stmt — lines 4547 → 4956 (~410 lines — manageable)
  • emit_expr — lines 4956 → 9544 (~4.6k)
  • emit_js_stmt — lines 2413 → 2579 (handles JS-fallback emission, used when WASM can't represent something)
  • emit_js_expr — lines 2579 → 3035 (the JS-fallback expression emitter)
  • collect_strings + collect_strings_in_stmts + collect_strings_in_stmt + collect_strings_in_expr — 3035 → 3993 (~1k)
  • emit_method_call — 4203 → 4529

Proposed split

Move emit.rs to crates/perry-codegen-wasm/src/emit/:

  • emit/mod.rscompile_to_wasm, compile_to_wasm_with_async, WasmModuleEmitter::new, intern_string, get_type_idx, compile, the core type/field/function-index machinery (~533–2175), plus the small free-functions f64_const, f64_const_bits, map_ui_method
  • emit/function.rscompile_function, compile_closure, compile_class_constructor, compile_class_method, emit_frame_begin, emit_slot_addr, emit_store_arg, emit_store_const, emit_local_or_global_get (~2175–2378, ~4023–4114)
  • emit/string_collection.rscollect_strings, collect_strings_in_stmts, collect_strings_in_stmt, collect_strings_in_expr (~3035–3993)
  • emit/memcall.rsemit_memcall, emit_memcall_void, emit_memcall_i32, RuntimeImports::rt access helper (~4114–4203)
  • emit/method_call.rsemit_method_call (~4203–4529)
  • emit/binary.rsemit_bitwise_binary
  • emit/stmt.rsemit_stmt, has_return, expr_has_value (~4547–4956, ~9544+, ~9956+)
  • emit/expr.rsemit_expr — the 4.6k beast. Once isolated, the natural sub-split is by Expr::* variant the same way chore(codegen): split crates/perry-codegen/src/lower_call.rs (11.8k lines, lower_call + extract_options_fields) #1099 splits the LLVM expr.rs — but suggest landing the file move first and the within-emit_expr split as a follow-up so reviewers can stage the work.
  • emit/js_fallback.rsemit_js_async_function, emit_js_stmt, emit_js_expr (~2378–3035)
  • emit/closures.rscollect_closures_from_stmts, collect_closures_from_expr (~9699–9956)
  • emit/locals.rscollect_locals, collect_module_let_ids, resolve_source_module_idx (~9578–9699)

Hot path caveat

WASM codegen issues are tracked in #1081 right now. Hold this refactor until the active stack-balance bug is resolved or coordinate with whoever is currently in this file — don't rebase a 10k-line move on top of in-flight fixes.

Acceptance

Same conventions as #1097. Plus: smoke-test by compiling at least one TypeScript program to --backend wasm before and after.

Metadata

Metadata

Assignees

Labels

toolingDeveloper tooling, CI, tests, or release infrastructure

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions