JS backend: fix root package initialize.
This commit is contained in:
@@ -55,9 +55,8 @@ public class RegressionMergeEcmaTest extends SingleFileTranslationTest {
|
||||
checkFooBoxIsOk();
|
||||
}
|
||||
|
||||
//TODO
|
||||
public void TestRootPackageValInit() throws Exception {
|
||||
checkFooBoxIsOk();
|
||||
public void testRootPackageValInit() throws Exception {
|
||||
runFunctionOutputTest(DEFAULT_ECMA_VERSIONS, getTestName(true) + ".kt", "_", "box", "OK");
|
||||
}
|
||||
|
||||
public void testIsClassSupport() throws Exception {
|
||||
|
||||
@@ -159,6 +159,8 @@ public final class Namer {
|
||||
@NotNull
|
||||
private final JsExpression definePackage;
|
||||
@NotNull
|
||||
private final JsExpression defineRootPackage;
|
||||
@NotNull
|
||||
private final JsName objectName;
|
||||
@NotNull
|
||||
private final JsName enumEntriesName;
|
||||
@@ -172,6 +174,7 @@ public final class Namer {
|
||||
traitName = kotlinScope.declareName(TRAIT_OBJECT_NAME);
|
||||
|
||||
definePackage = kotlin("definePackage");
|
||||
defineRootPackage = kotlin("defineRootPackage");
|
||||
|
||||
className = kotlinScope.declareName(CLASS_OBJECT_NAME);
|
||||
enumEntriesName = kotlinScope.declareName(ENUM_ENTRIES_NAME);
|
||||
@@ -200,6 +203,11 @@ public final class Namer {
|
||||
return definePackage;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JsExpression rootPackageDefinitionMethodReference() {
|
||||
return defineRootPackage;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JsExpression objectCreationMethodReference() {
|
||||
return kotlin(objectName);
|
||||
|
||||
+15
-21
@@ -52,15 +52,15 @@ public final class NamespaceDeclarationTranslator extends AbstractTranslator {
|
||||
private List<JsStatement> translate() {
|
||||
// predictable order
|
||||
Map<NamespaceDescriptor, List<JsExpression>> descriptorToDefineInvocation = new THashMap<NamespaceDescriptor, List<JsExpression>>();
|
||||
JsObjectLiteral rootNamespaceDefinition = null;
|
||||
NamespaceDescriptor rootNamespaceDescriptor = null;
|
||||
|
||||
for (JetFile file : files) {
|
||||
NamespaceDescriptor descriptor = context().bindingContext().get(BindingContext.FILE_TO_NAMESPACE, file);
|
||||
assert descriptor != null;
|
||||
NamespaceTranslator translator = descriptorToTranslator.get(descriptor);
|
||||
if (translator == null) {
|
||||
if (rootNamespaceDefinition == null) {
|
||||
rootNamespaceDefinition = getRootPackage(descriptorToDefineInvocation, descriptor);
|
||||
if (rootNamespaceDescriptor == null) {
|
||||
rootNamespaceDescriptor = getRootPackageDescriptor(descriptorToDefineInvocation, descriptor);
|
||||
}
|
||||
translator = new NamespaceTranslator(descriptor, descriptorToDefineInvocation, context());
|
||||
descriptorToTranslator.put(descriptor, translator);
|
||||
@@ -69,7 +69,7 @@ public final class NamespaceDeclarationTranslator extends AbstractTranslator {
|
||||
translator.translate(file);
|
||||
}
|
||||
|
||||
if (rootNamespaceDefinition == null) {
|
||||
if (rootNamespaceDescriptor == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@@ -89,20 +89,22 @@ public final class NamespaceDeclarationTranslator extends AbstractTranslator {
|
||||
}
|
||||
|
||||
vars.addIfHasInitializer(context().classDeclarationTranslator().getDeclaration());
|
||||
vars.addIfHasInitializer(getDeclaration(rootNamespaceDefinition));
|
||||
vars.addIfHasInitializer(getRootPackageDeclaration(descriptorToDefineInvocation.get(rootNamespaceDescriptor)));
|
||||
return result;
|
||||
}
|
||||
|
||||
private JsObjectLiteral getRootPackage(Map<NamespaceDescriptor, List<JsExpression>> descriptorToDefineInvocation,
|
||||
NamespaceDescriptor descriptor) {
|
||||
@NotNull
|
||||
private NamespaceDescriptor getRootPackageDescriptor(
|
||||
@NotNull Map<NamespaceDescriptor, List<JsExpression>> descriptorToDefineInvocation,
|
||||
@NotNull NamespaceDescriptor descriptor
|
||||
) {
|
||||
NamespaceDescriptor rootNamespace = descriptor;
|
||||
while (DescriptorUtils.isTopLevelDeclaration(rootNamespace)) {
|
||||
rootNamespace = (NamespaceDescriptor) rootNamespace.getContainingDeclaration();
|
||||
}
|
||||
|
||||
JsObjectLiteral rootNamespaceDefinition = new JsObjectLiteral(true);
|
||||
descriptorToDefineInvocation.put(rootNamespace, createDefineInvocation(rootNamespace, null, rootNamespaceDefinition, context()));
|
||||
return rootNamespaceDefinition;
|
||||
descriptorToDefineInvocation.put(rootNamespace, createDefineInvocation(rootNamespace, null, new JsObjectLiteral(true), context()));
|
||||
return rootNamespace;
|
||||
}
|
||||
|
||||
static List<JsExpression> createDefineInvocation(
|
||||
@@ -121,16 +123,8 @@ public final class NamespaceDeclarationTranslator extends AbstractTranslator {
|
||||
}
|
||||
}
|
||||
|
||||
private JsVar getDeclaration(@NotNull JsObjectLiteral rootNamespaceDefinition) {
|
||||
JsExpression packageMapValue;
|
||||
if (context().isEcma5()) {
|
||||
packageMapValue = new JsInvocation(JsAstUtils.CREATE_OBJECT, JsLiteral.NULL,
|
||||
new JsDocComment(JsAstUtils.LENDS_JS_DOC_TAG, Namer.getRootNamespaceName()),
|
||||
rootNamespaceDefinition);
|
||||
}
|
||||
else {
|
||||
packageMapValue = rootNamespaceDefinition;
|
||||
}
|
||||
return new JsVar(context().scope().declareName(Namer.getRootNamespaceName()), packageMapValue);
|
||||
private JsVar getRootPackageDeclaration(@NotNull List<JsExpression> defineInvocation) {
|
||||
JsExpression rootPackageVar = new JsInvocation(context().namer().rootPackageDefinitionMethodReference(), defineInvocation);
|
||||
return new JsVar(context().scope().declareName(Namer.getRootNamespaceName()), rootPackageVar);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-8
@@ -197,14 +197,7 @@ final class NamespaceTranslator extends AbstractTranslator {
|
||||
if (initializer != null) {
|
||||
JsExpression value = Translation.translateAsExpression(initializer, initializerContext);
|
||||
PropertyDescriptor propertyDescriptor = getPropertyDescriptor(context.bindingContext(), property);
|
||||
if (value instanceof JsLiteral) {
|
||||
result.add(new JsPropertyInitializer(context.getNameForDescriptor(propertyDescriptor).makeRef(),
|
||||
context().isEcma5() ? JsAstUtils
|
||||
.createPropertyDataDescriptor(propertyDescriptor, value) : value));
|
||||
}
|
||||
else {
|
||||
initializerStatements.add(generateInitializerForProperty(context, propertyDescriptor, value));
|
||||
}
|
||||
initializerStatements.add(generateInitializerForProperty(context, propertyDescriptor, value));
|
||||
}
|
||||
|
||||
JsStatement delegate = generateInitializerForDelegate(context, property);
|
||||
|
||||
@@ -244,11 +244,22 @@ var KotlinNew = {};
|
||||
}
|
||||
};
|
||||
|
||||
KotlinNew.defineRootPackage = function (initializer, members) {
|
||||
var definition = Object.create(null, members === null ? undefined : members);
|
||||
|
||||
if (initializer === null) {
|
||||
definition.$initializer = emptyFunction;
|
||||
} else {
|
||||
definition.$initializer = initializer;
|
||||
}
|
||||
return definition;
|
||||
};
|
||||
|
||||
KotlinNew.defineModule = function (id, module) {
|
||||
if (id in Kotlin.modules) {
|
||||
throw Kotlin.$new(Kotlin.IllegalArgumentException)();
|
||||
throw new Kotlin.IllegalArgumentException();
|
||||
}
|
||||
|
||||
module.$initializer.call(module);
|
||||
Object.defineProperty(Kotlin.modules, id, {value: module});
|
||||
};
|
||||
|
||||
@@ -310,6 +321,10 @@ var Kotlin = Object.create(null);
|
||||
return KotlinNew.definePackage(initializer, members);
|
||||
};
|
||||
|
||||
Kotlin.defineRootPackage = function (initializer, members) {
|
||||
return KotlinNew.defineRootPackage(initializer, members);
|
||||
};
|
||||
|
||||
Kotlin.defineModule = function (id, module) {
|
||||
KotlinNew.defineModule(id, module);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user