JS: fix long constants importing

This commit is contained in:
Anton Bannykh
2018-12-18 18:18:06 +03:00
parent cd0fd569b4
commit 6d5ef2d324
5 changed files with 28 additions and 30 deletions
@@ -395,12 +395,6 @@ fun extractImportTag(jsVar: JsVars.JsVar): String? {
return if (extractImportTagImpl(initExpression, sb)) sb.toString() else null
}
// Note! Doesn't handle Long's
fun extractImportTag(initExpression: JsExpression): String? {
val sb = StringBuilder()
return if (extractImportTagImpl(initExpression, sb)) sb.toString() else null
}
private fun extractImportTagImpl(expression: JsExpression, sb: StringBuilder): Boolean {
when (expression) {
is JsNameRef -> {
@@ -909,11 +909,16 @@ public class TranslationContext {
String tag = Objects.requireNonNull(staticContext.getTag(descriptor));
String suggestedName = StaticContext.getSuggestedName(descriptor);
return declareConstantValue(suggestedName, tag, value);
return declareConstantValue(suggestedName, tag, value, descriptor);
}
@NotNull
public JsExpression declareConstantValue(@NotNull String suggestedName, @NotNull String tag, @NotNull JsExpression value) {
public JsExpression declareConstantValue(
@NotNull String suggestedName,
@NotNull String tag,
@NotNull JsExpression value,
@Nullable DeclarationDescriptor descriptor
) {
if (inlineFunctionContext == null || !isPublicInlineFunction()) {
return staticContext.importDeclaration(suggestedName, tag, value).makeRef();
}
@@ -921,6 +926,9 @@ public class TranslationContext {
return inlineFunctionContext.getImports().computeIfAbsent(tag, t -> {
JsName result = JsScope.declareTemporaryName(suggestedName);
MetadataProperties.setImported(result, true);
if (descriptor != null && isFromCurrentModule(descriptor) && !AnnotationsUtils.isNativeObject(descriptor)) {
MetadataProperties.setLocalAlias(result, new LocalAlias(getInnerNameForDescriptor(descriptor), tag));
}
inlineFunctionContext.getImportBlock().getStatements().add(JsAstUtils.newVar(result, value));
return result;
}).makeRef();
@@ -116,7 +116,7 @@ public final class Translation {
}
String name = NameSuggestion.sanitizeName("L" + compileTimeValue.getValue(type).toString());
return context.declareConstantValue(name, "constant:" + name, constantResult);
return context.declareConstantValue(name, "constant:" + name, constantResult, null);
}
if (KotlinBuiltIns.isInt(type)) {
@@ -5,32 +5,28 @@
package org.jetbrains.kotlin.js.translate.general
import org.jetbrains.kotlin.js.backend.ast.JsExpression
import org.jetbrains.kotlin.js.backend.ast.JsNameBinding
import org.jetbrains.kotlin.js.backend.ast.JsProgramFragment
import org.jetbrains.kotlin.js.backend.ast.JsVars
import org.jetbrains.kotlin.js.inline.util.extractImportTag
// TODO this is a hack for `intrinsic:` tags
// TODO this is a hack for `intrinsic:` and `constant:` tags
fun JsProgramFragment.normalizeImportTags() {
val newImports = mutableMapOf<String, JsExpression>()
val replacements = mutableMapOf<String, String>()
imports.entries.retainAll { (tag, statement) ->
extractImportTag(statement)?.let { newTag ->
if (newTag != tag) {
newImports[newTag] = statement
replacements[tag] = newTag
}
newTag == tag
} ?: true
}
imports += newImports
nameBindings.replaceAll { binding ->
replacements[binding.key]?.let {
JsNameBinding(it, binding.name)
val (tag, name) = binding
imports[tag]?.let { import ->
extractImportTag(JsVars.JsVar(name, imports[tag]))?.let { newtag ->
if (tag != newtag) {
imports[newtag] = import
JsNameBinding(newtag, name)
} else null
}
} ?: binding
}
val tagSet = nameBindings.asSequence().map { it.key }.toSet()
imports.entries.retainAll { (tag, _) -> tag in tagSet }
}
@@ -145,11 +145,11 @@ inline fun testImportedLongConstInlineFunLib1() {
assertEquals(123456789012345L, bigLongConstCopy)
}
// PROPERTY_READ_COUNT: name=longConst_0 count=1 scope=testImportedLongConstInlinedLocally
// PROPERTY_READ_COUNT: name=longConst count=1 scope=testImportedLongConstInlinedLocally
// PROPERTY_READ_COUNT: name=L42 count=1 scope=testImportedLongConstInlinedLocally
// PROPERTY_READ_COUNT: name=L_42 count=4 scope=testImportedLongConstInlinedLocally
// PROPERTY_READ_COUNT: name=L84 count=2 scope=testImportedLongConstInlinedLocally
// PROPERTY_READ_COUNT: name=bigLongConst_0 count=1 scope=testImportedLongConstInlinedLocally
// PROPERTY_READ_COUNT: name=bigLongConst count=1 scope=testImportedLongConstInlinedLocally
// PROPERTY_READ_COUNT: name=L123456789012345 count=1 scope=testImportedLongConstInlinedLocally
private fun testImportedLongConstInlinedLocally() {
testImportedLongConstInlineFunLib1()