JS backend: use native function for parseInt.
Add radix parameter to parseInt. (cherry picked from commit b6be98d)
This commit is contained in:
@@ -18,8 +18,7 @@ public fun println(s : Any?) {}
|
||||
library("print")
|
||||
public fun print(s : Any?) {}
|
||||
//TODO: consistent parseInt
|
||||
library("parseInt")
|
||||
public fun parseInt(s : String) : Int = js.noImpl
|
||||
native public fun parseInt(s: String, radix: Int = 10): Int = js.noImpl
|
||||
library
|
||||
public fun safeParseInt(s : String) : Int? = js.noImpl
|
||||
library
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.jetbrains.k2js.translate.expression;
|
||||
import com.google.dart.compiler.backend.js.ast.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.JetNodeTypes;
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
|
||||
@@ -58,6 +59,18 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
|
||||
public JsNode visitConstantExpression(@NotNull JetConstantExpression expression,
|
||||
@NotNull TranslationContext context) {
|
||||
CompileTimeConstant<?> compileTimeValue = context.bindingContext().get(BindingContext.COMPILE_TIME_VALUE, expression);
|
||||
|
||||
// TODO: workaround for default parameters translation. Will be fixed later.
|
||||
// public fun parseInt(s: String, radix:Int = 10): Int = js.noImpl
|
||||
if (compileTimeValue == null) {
|
||||
if (expression.getNode().getElementType() == JetNodeTypes.BOOLEAN_CONSTANT) {
|
||||
return JsLiteral.getBoolean(Boolean.valueOf(expression.getText()));
|
||||
}
|
||||
else if (expression.getNode().getElementType() == JetNodeTypes.INTEGER_CONSTANT) {
|
||||
return context.program().getNumberLiteral(Integer.parseInt(expression.getText()));
|
||||
}
|
||||
}
|
||||
|
||||
assert compileTimeValue != null;
|
||||
|
||||
if (compileTimeValue instanceof NullValue) {
|
||||
|
||||
@@ -290,10 +290,6 @@ String.prototype.contains = function (s) {
|
||||
close: throwAbstractFunctionInvocationError("Closeable#close")
|
||||
});
|
||||
|
||||
Kotlin.parseInt = function (str) {
|
||||
return parseInt(str, 10);
|
||||
};
|
||||
|
||||
Kotlin.safeParseInt = function(str) {
|
||||
var r = parseInt(str, 10);
|
||||
return isNaN(r) ? null : r;
|
||||
|
||||
Reference in New Issue
Block a user