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