implement multinamespace translation:
simple implementation of NamespaceDeclarationTranslator
This commit is contained in:
@@ -6,6 +6,7 @@ import java.util.*;
|
||||
|
||||
|
||||
|
||||
native
|
||||
val noImpl : Nothing = throw Exception();
|
||||
|
||||
library("println")
|
||||
|
||||
@@ -3,15 +3,16 @@ package js
|
||||
import java.util.*;
|
||||
import js.library
|
||||
|
||||
native
|
||||
class Json() {
|
||||
|
||||
}
|
||||
|
||||
library("jsonSet")
|
||||
fun Json.set(paramName : String, value : Any?) : Unit {}
|
||||
|
||||
library("jsonGet")
|
||||
fun Json.get(paramName : String) : Any? = null
|
||||
|
||||
fun <K, V> Map<K, V>.toJson() : Json = Json()
|
||||
library("jsonFromTuples")
|
||||
fun json(vararg pairs : Tuple2<String, Any?>) = Json()
|
||||
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package org.jetbrains.k2js.translate.declaration;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.dart.compiler.backend.js.ast.JsStatement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||
import org.jetbrains.k2js.translate.general.AbstractTranslator;
|
||||
import org.jetbrains.k2js.translate.general.Translation;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.k2js.translate.utils.BindingUtils.getAllNonNativeNamespaceDescriptors;
|
||||
|
||||
/**
|
||||
* @author Pavel Talanov
|
||||
*/
|
||||
public final class NamespaceDeclarationTranslator extends AbstractTranslator {
|
||||
|
||||
public static List<JsStatement> translateFiles(@NotNull List<JetFile> files, @NotNull TranslationContext context) {
|
||||
List<JsStatement> result = Lists.newArrayList();
|
||||
for (NamespaceDescriptor namespaceDescriptor : getAllNonNativeNamespaceDescriptors(context.bindingContext(), files)) {
|
||||
result.add(Translation.translateNamespace(namespaceDescriptor, context));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private NamespaceDeclarationTranslator(@NotNull TranslationContext context) {
|
||||
super(context);
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import org.jetbrains.jet.lang.types.JetStandardLibrary;
|
||||
import org.jetbrains.k2js.translate.context.StaticContext;
|
||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||
import org.jetbrains.k2js.translate.declaration.ClassTranslator;
|
||||
import org.jetbrains.k2js.translate.declaration.NamespaceDeclarationTranslator;
|
||||
import org.jetbrains.k2js.translate.declaration.NamespaceTranslator;
|
||||
import org.jetbrains.k2js.translate.expression.ExpressionVisitor;
|
||||
import org.jetbrains.k2js.translate.expression.FunctionTranslator;
|
||||
@@ -43,6 +44,11 @@ public final class Translation {
|
||||
return NamespaceTranslator.translateNamespace(namespace, context);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static List<JsStatement> translateFiles(@NotNull List<JetFile> files, @NotNull TranslationContext context) {
|
||||
return NamespaceDeclarationTranslator.translateFiles(files, context);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static JsInvocation translateClassDeclaration(@NotNull JetClass classDeclaration,
|
||||
@NotNull TranslationContext context) {
|
||||
@@ -99,8 +105,7 @@ public final class Translation {
|
||||
StaticContext staticContext = StaticContext.generateStaticContext(standardLibrary, bindingContext);
|
||||
JsBlock block = staticContext.getProgram().getFragmentBlock(0);
|
||||
TranslationContext context = TranslationContext.rootContext(staticContext);
|
||||
block.addStatement(Translation.translateNamespace(namespaceToTranslate, context));
|
||||
|
||||
block.getStatements().addAll(Translation.translateFiles(files, context));
|
||||
JsNamer namer = new JsPrettyNamer();
|
||||
namer.exec(context.program());
|
||||
return context.program();
|
||||
|
||||
@@ -70,13 +70,26 @@ public final class AnnotationsUtils {
|
||||
}
|
||||
|
||||
public static boolean isNativeObject(@NotNull DeclarationDescriptor descriptor) {
|
||||
if (getAnnotationByName(descriptor, NATIVE_ANNOTATION_FQNAME) != null) {
|
||||
return hasAnnotationOrInsideAnnotatedClass(descriptor, NATIVE_ANNOTATION_FQNAME);
|
||||
}
|
||||
|
||||
public static boolean isLibraryObject(@NotNull DeclarationDescriptor descriptor) {
|
||||
return hasAnnotationOrInsideAnnotatedClass(descriptor, LIBRARY_ANNOTATION_FQNAME);
|
||||
}
|
||||
|
||||
//TODO: the use of this method is splattered across the code which can be hard to track
|
||||
public static boolean isPredefinedObject(@NotNull DeclarationDescriptor descriptor) {
|
||||
return isLibraryObject(descriptor) || isNativeObject(descriptor);
|
||||
}
|
||||
|
||||
private static boolean hasAnnotationOrInsideAnnotatedClass(DeclarationDescriptor descriptor, String annotationFQName) {
|
||||
if (getAnnotationByName(descriptor, annotationFQName) != null) {
|
||||
return true;
|
||||
}
|
||||
ClassDescriptor containingClass = getContainingClass(descriptor);
|
||||
if (containingClass == null) {
|
||||
return false;
|
||||
}
|
||||
return (getAnnotationByName(containingClass, NATIVE_ANNOTATION_FQNAME) != null);
|
||||
return (getAnnotationByName(containingClass, annotationFQName) != null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.jetbrains.k2js.translate.utils;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -13,6 +14,7 @@ import org.jetbrains.jet.lang.types.JetType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.jetbrains.k2js.translate.utils.DescriptorUtils.*;
|
||||
|
||||
@@ -85,6 +87,9 @@ public final class BindingUtils {
|
||||
@NotNull NamespaceDescriptor namespace) {
|
||||
List<JetDeclaration> declarations = new ArrayList<JetDeclaration>();
|
||||
for (DeclarationDescriptor descriptor : namespace.getMemberScope().getAllDescriptors()) {
|
||||
if (AnnotationsUtils.isPredefinedObject(descriptor)) {
|
||||
continue;
|
||||
}
|
||||
//TODO:
|
||||
if (descriptor instanceof NamespaceDescriptor) {
|
||||
continue;
|
||||
@@ -296,4 +301,17 @@ public final class BindingUtils {
|
||||
assert propertyDescriptor != null;
|
||||
return propertyDescriptor;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Set<NamespaceDescriptor> getAllNonNativeNamespaceDescriptors(@NotNull BindingContext context,
|
||||
@NotNull List<JetFile> files) {
|
||||
Set<NamespaceDescriptor> descriptorSet = Sets.newHashSet();
|
||||
for (JetFile file : files) {
|
||||
NamespaceDescriptor namespaceDescriptor = getNamespaceDescriptor(context, file);
|
||||
if (!AnnotationsUtils.isPredefinedObject(namespaceDescriptor)) {
|
||||
descriptorSet.add(namespaceDescriptor);
|
||||
}
|
||||
}
|
||||
return descriptorSet;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,6 +192,9 @@ public final class DescriptorUtils {
|
||||
public static List<ClassDescriptor> getAllClassesDefinedInNamespace(@NotNull NamespaceDescriptor namespaceDescriptor) {
|
||||
List<ClassDescriptor> classDescriptors = Lists.newArrayList();
|
||||
for (DeclarationDescriptor descriptor : namespaceDescriptor.getMemberScope().getAllDescriptors()) {
|
||||
if (AnnotationsUtils.isPredefinedObject(descriptor)) {
|
||||
continue;
|
||||
}
|
||||
if (descriptor instanceof ClassDescriptor) {
|
||||
classDescriptors.add((ClassDescriptor) descriptor);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user