Skip to content

Commit

Permalink
make JS trunc polyfill opt-in, closes nim-lang#16144 (nim-lang#19183)
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn authored and PMunch committed Mar 28, 2022
1 parent 671ccb2 commit 5078b39
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 7 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

## Changes affecting backward compatibility


- The `Math.trunc` polyfill for targeting Internet Explorer was
previously emitted for every JavaScript output file except if
the symbol `nodejs` was defined via `-d:nodejs`. Now, it is only
emitted if the symbol `nimJsMathTruncPolyfill` is defined. If you are
targeting Internet Explorer, you may choose to enable this option
or define your own `Math.trunc` polyfill using the [`emit` pragma](https://nim-lang.org/docs/manual.html#implementation-specific-pragmas-emit-pragma). Nim uses
`Math.trunc` for the division and modulo operators for integers.

## Standard library additions and changes

Expand Down
6 changes: 3 additions & 3 deletions lib/system/jssys.nim
Original file line number Diff line number Diff line change
Expand Up @@ -763,13 +763,13 @@ proc nimParseBiggestFloat(s: string, number: var BiggestFloat, start: int): int

# Workaround for IE, IE up to version 11 lacks 'Math.trunc'. We produce
# 'Math.trunc' for Nim's ``div`` and ``mod`` operators:
const jsMathTrunc = """
when defined(nimJsMathTruncPolyfill):
{.emit: """
if (!Math.trunc) {
Math.trunc = function(v) {
v = +v;
if (!isFinite(v)) return v;
return (v - v % 1) || (v < 0 ? -0 : v === 0 ? v : 0);
};
}
"""
when not defined(nodejs): {.emit: jsMathTrunc .}
""".}

0 comments on commit 5078b39

Please sign in to comment.