Files
Nim/tests/js/test2.nim
ringabout 98131a9fa1 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.
2026-04-18 09:40:55 +02:00

76 lines
1.3 KiB
Nim

discard """
output: '''foo
js 3.14
7
1
-21550
-21550
none(TT)
()
destroyed
destroyed
'''
"""
# This file tests the JavaScript generator
doAssert getCurrentException() == nil
doAssert getCurrentExceptionMsg() == ""
# #335
proc foo() =
var bar = "foo"
proc baz() =
echo bar
baz()
foo()
# #376
when not defined(js):
proc foo(val: float): string = "no js " & $val
else:
proc foo(val: float): string = "js " & $val
echo foo(3.14)
# #2495
type C = concept x
proc test(x: C, T: typedesc): T =
cast[T](x)
echo 7.test(int8)
# #4222
const someConst = [ "1"]
proc procThatRefersToConst() # Forward decl
procThatRefersToConst() # Call bar before it is defined
proc procThatRefersToConst() =
var i = 0 # Use a var index, otherwise nim will constfold foo[0]
echo someConst[i] # JS exception here: foo is still not initialized (undefined)
# bug #6753
let x = -1861876800
const y = 86400
echo (x - (y - 1)) div y # Now gives `-21550`
proc foo09() =
let x = -1861876800
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