fixes #25751; JS backend crashes when returning Option[T] with custom =destroy (#25752)

fixes #25751

This pull request improves the JavaScript backend code generation and
expands test coverage, particularly around temporary and loop variables,
as well as object destruction behavior. The main changes include
updating the code generator to handle more symbol kinds and adding tests
to ensure proper destruction and option handling.

**JavaScript code generation improvements:**

* Updated `genSymAddr` in `compiler/jsgen.nim` to support additional
symbol kinds, specifically `skTemp` and `skForVar`, ensuring correct
address generation for temporaries and loop variables.

**Test suite enhancements:**

* Added tests in `tests/js/test2.nim` to verify correct behavior of
option types, object destruction (`=destroy`), and to check for
backend-specific crashes. This includes printing results of
option-returning functions and confirming destruction messages.
* Updated expected output in `tests/js/test2.nim` to include results
from new tests and destruction messages, ensuring the test suite
reflects the latest code behavior.

(cherry picked from commit 98131a9fa1)
This commit is contained in:
ringabout
2026-04-18 15:40:55 +08:00
committed by narimiran
parent 2599fdc0ac
commit eb6b923135
2 changed files with 19 additions and 2 deletions

View File

@@ -1532,7 +1532,7 @@ proc genSymAddr(p: PProc, n: PNode, typ: PType, r: var TCompRes) =
r.res = s.loc.snippet
r.address = ""
r.typ = etyNone
of skVar, skLet, skResult:
of skVar, skLet, skResult, skTemp, skForVar:
r.kind = resExpr
let jsType = mapType(p):
if typ.isNil:

View File

@@ -4,7 +4,12 @@ js 3.14
7
1
-21550
-21550'''
-21550
none(TT)
()
destroyed
destroyed
'''
"""
# This file tests the JavaScript generator
@@ -56,3 +61,15 @@ proc foo09() =
const y = 86400
echo (x - (y - 1)) div y # Still gives `-21551`
foo09()
import std/options
type TT = object
proc `=destroy`(x: TT) = echo "destroyed"
func test1: Option[TT] = discard
func test2: TT = discard
echo test1() # Crash in JS backend, not crash in C backend
echo test2() # Not crash