-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NumberNode abstract superclass for FloatNode and IntNode, in prep…
…aration for replacing those two with NumberNode. PiperOrigin-RevId: 726647971
- Loading branch information
1 parent
d8a2d03
commit 404f766
Showing
17 changed files
with
191 additions
and
68 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
java/src/com/google/template/soy/base/internal/NumericCoercions.java
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,51 @@ | ||
/* | ||
* Copyright 2025 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.template.soy.base.internal; | ||
|
||
/** Numeric coercions compatible with JavaScript number. */ | ||
public final class NumericCoercions { | ||
|
||
private static final long MAX_SAFE_INTEGER = (1L << 53) - 1; // 2^53 - 1 | ||
private static final long MIN_SAFE_INTEGER = -MAX_SAFE_INTEGER; | ||
|
||
private NumericCoercions() {} | ||
|
||
public static double safeDouble(long l) { | ||
if (l > MAX_SAFE_INTEGER || l < MIN_SAFE_INTEGER) { | ||
throw new IllegalArgumentException(String.valueOf(l)); | ||
} | ||
return (double) l; | ||
} | ||
|
||
public static long safeLong(double d) { | ||
if (d > MAX_SAFE_INTEGER || d < MIN_SAFE_INTEGER) { | ||
throw new IllegalArgumentException(String.valueOf(d)); | ||
} | ||
return (long) d; | ||
} | ||
|
||
public static int safeInt(long l) { | ||
if (l > Integer.MAX_VALUE || l < Integer.MIN_VALUE) { | ||
throw new IllegalArgumentException(String.valueOf(l)); | ||
} | ||
return (int) l; | ||
} | ||
|
||
public static int safeInt(double d) { | ||
return safeInt(safeLong(d)); | ||
} | ||
} |
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
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,41 @@ | ||
/* | ||
* Copyright 2025 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.template.soy.exprtree; | ||
|
||
import com.google.template.soy.base.SourceLocation; | ||
import com.google.template.soy.basetree.CopyState; | ||
|
||
/** | ||
* Node representing a Soy integer value. Note that Soy supports up to JavaScript | ||
* +-Number.MAX_SAFE_INTEGER at the least; Java and Python backends support full 64 bit longs. | ||
*/ | ||
public abstract class NumberNode extends AbstractPrimitiveNode { | ||
|
||
public NumberNode(SourceLocation sourceLocation) { | ||
super(sourceLocation); | ||
} | ||
|
||
protected NumberNode(NumberNode orig, CopyState copyState) { | ||
super(orig, copyState); | ||
} | ||
|
||
public abstract double doubleValue(); | ||
|
||
public abstract long longValue(); | ||
|
||
public abstract int intValue(); | ||
} |
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
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
Oops, something went wrong.