From 4d6be458a00a642555e95055ff640daba59513f7 Mon Sep 17 00:00:00 2001 From: metagn Date: Wed, 24 May 2023 18:55:09 +0300 Subject: [PATCH] js -r defines nodejs & program result undeclared if unavailable (#21849) * js -r defines nodejs & program result undefined if unavailable fixes #16985, fixes #16074 * fix * add changelog too * minor word change --- changelogs/changelog_2_0_0.md | 4 ++++ compiler/commands.nim | 6 ++++++ lib/pure/unittest.nim | 6 ++++++ lib/std/exitprocs.nim | 10 +++------- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/changelogs/changelog_2_0_0.md b/changelogs/changelog_2_0_0.md index b15818ae69..11fa8cba35 100644 --- a/changelogs/changelog_2_0_0.md +++ b/changelogs/changelog_2_0_0.md @@ -249,6 +249,10 @@ these deprecated aliases are likely not used anymore and it may make sense to simply remove these statements. +- `getProgramResult` and `setProgramResult` in `std/exitprocs` are no longer + declared when they are not available on the backend. Previously it would call + `doAssert false` at runtime despite the condition being compile-time. + ## Standard library additions and changes [//]: # "Changes:" diff --git a/compiler/commands.nim b/compiler/commands.nim index 93a36e714b..4980ff2685 100644 --- a/compiler/commands.nim +++ b/compiler/commands.nim @@ -654,6 +654,9 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo; if backend == TBackend.default: localError(conf, info, "invalid backend: '$1'" % arg) if backend == backendJs: # bug #21209 conf.globalOptions.excl {optThreadAnalysis, optThreads} + if optRun in conf.globalOptions: + # for now, -r uses nodejs, so define nodejs + defineSymbol(conf.symbols, "nodejs") conf.backend = backend of "doccmd": conf.docCmd = arg of "define", "d": @@ -864,6 +867,9 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo; setTarget(conf.target, conf.target.targetOS, cpu) of "run", "r": processOnOffSwitchG(conf, {optRun}, arg, pass, info) + if conf.backend == backendJs: + # for now, -r uses nodejs, so define nodejs + defineSymbol(conf.symbols, "nodejs") of "maxloopiterationsvm": expectArg(conf, switch, arg, pass, info) conf.maxLoopIterationsVM = parseInt(arg) diff --git a/lib/pure/unittest.nim b/lib/pure/unittest.nim index 9dafa8f037..964fba0e4b 100644 --- a/lib/pure/unittest.nim +++ b/lib/pure/unittest.nim @@ -519,6 +519,12 @@ proc exceptionTypeName(e: ref Exception): string {.inline.} = if e == nil: "" else: $e.name +when not declared(setProgramResult): + {.warning: "setProgramResult not available on platform, unittest will not" & + " give failing exit code on test failure".} + template setProgramResult(a: int) = + discard + template test*(name, body) {.dirty.} = ## Define a single test case identified by `name`. ## diff --git a/lib/std/exitprocs.nim b/lib/std/exitprocs.nim index 736bf06d1b..c44eb30d65 100644 --- a/lib/std/exitprocs.nim +++ b/lib/std/exitprocs.nim @@ -69,23 +69,19 @@ proc addExitProc*(cl: proc() {.noconv.}) = fun() gFuns.add Fun(kind: kNoconv, fun2: cl) -when not defined(nimscript): +when not defined(nimscript) and (not defined(js) or defined(nodejs)): proc getProgramResult*(): int = when defined(js) and defined(nodejs): asm """ `result` = process.exitCode; """ - elif not defined(js): - result = programResult else: - doAssert false + result = programResult proc setProgramResult*(a: int) = when defined(js) and defined(nodejs): asm """ process.exitCode = `a`; """ - elif not defined(js): - programResult = a else: - doAssert false + programResult = a