-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18730 from emberjs/integers-test
Workaround for #15635 (integer encoding issue)
- Loading branch information
Showing
5 changed files
with
238 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { assert } from '@ember/debug'; | ||
import { CapturedArguments, VM, VMArguments } from '@glimmer/interfaces'; | ||
import { HelperRootReference } from '@glimmer/reference'; | ||
|
||
function i({ positional }: CapturedArguments): number { | ||
assert('[BUG] -i takes a single string', typeof positional.at(0).value() === 'string'); | ||
return parseInt(positional.at(0).value() as string, 10); | ||
} | ||
|
||
export default function(args: VMArguments, vm: VM) { | ||
return new HelperRootReference(i, args.capture(), vm.env); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/ember-template-compiler/lib/plugins/safe-integers-bugfix.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { AST, ASTPlugin, ASTPluginEnvironment } from '@glimmer/syntax'; | ||
import { MustacheStatement, NumberLiteral } from '@glimmer/syntax/dist/types/lib/types/nodes'; | ||
|
||
/** | ||
@module ember | ||
*/ | ||
|
||
/** | ||
A Glimmer2 AST transformation that replaces all instances of | ||
```handlebars | ||
{{987654321}} | ||
``` | ||
to | ||
```handlebars | ||
{{-i "987654321"}} | ||
``` | ||
as well as other integer number literals in sexp arguments, etc. | ||
The version of Glimmer VM we are using has a bug that encodes | ||
certain integers incorrectly. This forces them into strings and | ||
use `{{-i}}` (which is a wrapper around `parseInt`) to decode | ||
them manually as a workaround. | ||
This should be removed when the Glimmer VM bug is fixed. | ||
@private | ||
@class SafeIntegersBugfix | ||
*/ | ||
|
||
export default function safeIntegersBugfix(env: ASTPluginEnvironment): ASTPlugin { | ||
let { builders: b } = env.syntax; | ||
|
||
return { | ||
name: 'safe-integers-bugfix', | ||
|
||
visitor: { | ||
MustacheStatement(node: AST.MustacheStatement): AST.MustacheStatement | undefined { | ||
if (!requiresWorkaround(node)) { | ||
return; | ||
} | ||
|
||
return b.mustache( | ||
'-i', | ||
[b.string(String(node.path.value))], | ||
undefined, | ||
!node.escaped, | ||
node.loc | ||
); | ||
}, | ||
|
||
NumberLiteral(node: AST.NumberLiteral): AST.SubExpression | undefined { | ||
if (!requiresWorkaround(node)) { | ||
return; | ||
} | ||
|
||
return b.sexpr('-i', [b.string(String(node.value))], undefined, node.loc); | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
type NumberLiteralMustacheStatement = MustacheStatement & { path: NumberLiteral }; | ||
|
||
function requiresWorkaround(node: AST.MustacheStatement): node is NumberLiteralMustacheStatement; | ||
function requiresWorkaround(node: AST.NumberLiteral): boolean; | ||
function requiresWorkaround(node: AST.MustacheStatement | AST.NumberLiteral): boolean { | ||
if (node.type === 'MustacheStatement' && node.path.type === 'NumberLiteral') { | ||
return requiresWorkaround(node.path); | ||
} else if (node.type === 'NumberLiteral') { | ||
return isInteger(node.value) && isOverflowing(node.value); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
// Number.isInteger polyfill | ||
function isInteger(value: number): boolean { | ||
return isFinite(value) && Math.floor(value) === value; | ||
} | ||
|
||
function isOverflowing(value: number): boolean { | ||
return value >= 2 ** 28; | ||
} |