implement multinamespace translation:
add tests switch to using class descriptors in class declaration translator
This commit is contained in:
@@ -15,6 +15,8 @@ class MathClass() {
|
|||||||
fun sin(value : Double) = 0.0
|
fun sin(value : Double) = 0.0
|
||||||
fun exp(value : Double) = 0.0
|
fun exp(value : Double) = 0.0
|
||||||
fun max(vararg values : Double) = 0.0
|
fun max(vararg values : Double) = 0.0
|
||||||
|
fun max(vararg values : Int) : Int = js.noImpl
|
||||||
|
fun min(vararg values : Int) : Int = js.noImpl
|
||||||
fun min(vararg values : Double) = 0.0
|
fun min(vararg values : Double) = 0.0
|
||||||
fun sqrt(value : Double) = 0.0
|
fun sqrt(value : Double) = 0.0
|
||||||
fun tan(value : Double) = 0.0
|
fun tan(value : Double) = 0.0
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ import js.DomElement
|
|||||||
|
|
||||||
native
|
native
|
||||||
class JQuery() {
|
class JQuery() {
|
||||||
fun addClass(className : String) : JQuery = this;
|
fun addClass(className : String) : JQuery = js.noImpl;
|
||||||
fun addClass(f : DomElement.(Int, String)->String) = this;
|
fun addClass(f : DomElement.(Int, String)->String) = js.noImpl;
|
||||||
|
|
||||||
fun attr(attrName : String) = "";
|
fun attr(attrName : String) = "";
|
||||||
fun attr(attrName : String, value : String) = this;
|
fun attr(attrName : String, value : String) = this;
|
||||||
@@ -39,6 +39,8 @@ class JQuery() {
|
|||||||
fun slideUp() = this;
|
fun slideUp() = this;
|
||||||
fun hover(handlerInOut : DomElement.() -> Unit) = this;
|
fun hover(handlerInOut : DomElement.() -> Unit) = this;
|
||||||
fun hover(handlerIn : DomElement.() -> Unit, handlerOut : DomElement.() -> Unit) = this;
|
fun hover(handlerIn : DomElement.() -> Unit, handlerOut : DomElement.() -> Unit) = this;
|
||||||
|
fun next() : JQuery = js.noImpl
|
||||||
|
fun parent() : JQuery = js.noImpl
|
||||||
}
|
}
|
||||||
|
|
||||||
native
|
native
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package jquery.ui
|
||||||
|
|
||||||
|
|
||||||
|
//jquery UI
|
||||||
|
import jquery.JQuery
|
||||||
|
import js.native
|
||||||
|
|
||||||
|
native
|
||||||
|
fun JQuery.buttonset() : JQuery = js.noImpl;
|
||||||
|
native
|
||||||
|
fun JQuery.dialog() : JQuery = js.noImpl;
|
||||||
|
native
|
||||||
|
fun JQuery.button() : JQuery = js.noImpl;
|
||||||
|
native
|
||||||
|
fun JQuery.accordion() : JQuery = js.noImpl
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package jquery.pixastic
|
||||||
|
|
||||||
|
import jquery.JQuery
|
||||||
|
import js.native
|
||||||
|
|
||||||
|
native
|
||||||
|
fun JQuery.pixastic(actionName : String) = js.noImpl
|
||||||
|
|
||||||
|
|
||||||
|
native
|
||||||
|
object Pixastic {
|
||||||
|
}
|
||||||
|
|
||||||
|
native
|
||||||
|
fun addAction(actionName : String,
|
||||||
|
process : (oldData : Array<Int>, newData : Array<Int>, width : Int, height : Int)->Unit) = js.noImpl
|
||||||
@@ -28,6 +28,7 @@ public abstract class Config {
|
|||||||
private static final List<String> LIB_FILE_NAMES = Arrays.asList(
|
private static final List<String> LIB_FILE_NAMES = Arrays.asList(
|
||||||
"/core/annotations.kt",
|
"/core/annotations.kt",
|
||||||
"/jquery/common.kt",
|
"/jquery/common.kt",
|
||||||
|
"/jquery/ui.kt",
|
||||||
"/core/javautil.kt",
|
"/core/javautil.kt",
|
||||||
"/core/javalang.kt",
|
"/core/javalang.kt",
|
||||||
"/core/core.kt",
|
"/core/core.kt",
|
||||||
@@ -37,7 +38,8 @@ public abstract class Config {
|
|||||||
"/html5/canvas.kt",
|
"/html5/canvas.kt",
|
||||||
"/html5/files.kt",
|
"/html5/files.kt",
|
||||||
"/html5/image.kt",
|
"/html5/image.kt",
|
||||||
"/helper/ip.kt"
|
"/helper/ip.kt",
|
||||||
|
"/pixastic/pixastic.kt"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+7
-9
@@ -6,7 +6,6 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||||
import org.jetbrains.jet.lang.psi.JetClass;
|
import org.jetbrains.jet.lang.psi.JetClass;
|
||||||
import org.jetbrains.jet.lang.psi.JetDeclaration;
|
|
||||||
import org.jetbrains.k2js.translate.context.Namer;
|
import org.jetbrains.k2js.translate.context.Namer;
|
||||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||||
import org.jetbrains.k2js.translate.general.AbstractTranslator;
|
import org.jetbrains.k2js.translate.general.AbstractTranslator;
|
||||||
@@ -27,7 +26,7 @@ import java.util.Map;
|
|||||||
public final class ClassDeclarationTranslator extends AbstractTranslator {
|
public final class ClassDeclarationTranslator extends AbstractTranslator {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final List<JetDeclaration> namespaceDeclarations;
|
private final List<ClassDescriptor> descriptors;
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Map<JsName, JsName> localToGlobalClassName;
|
private final Map<JsName, JsName> localToGlobalClassName;
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -37,9 +36,10 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
|
|||||||
@Nullable
|
@Nullable
|
||||||
private JsStatement declarationsStatement = null;
|
private JsStatement declarationsStatement = null;
|
||||||
|
|
||||||
public ClassDeclarationTranslator(@NotNull TranslationContext context, @NotNull List<JetDeclaration> declarationList) {
|
public ClassDeclarationTranslator(@NotNull TranslationContext context,
|
||||||
|
@NotNull List<ClassDescriptor> descriptors) {
|
||||||
super(context);
|
super(context);
|
||||||
this.namespaceDeclarations = declarationList;
|
this.descriptors = descriptors;
|
||||||
this.localToGlobalClassName = new HashMap<JsName, JsName>();
|
this.localToGlobalClassName = new HashMap<JsName, JsName>();
|
||||||
this.dummyFunctionScope = new JsScope(context().jsScope(), "class declaration function");
|
this.dummyFunctionScope = new JsScope(context().jsScope(), "class declaration function");
|
||||||
}
|
}
|
||||||
@@ -106,10 +106,8 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private List<JetClass> getClassDeclarations() {
|
private List<JetClass> getClassDeclarations() {
|
||||||
List<JetClass> classes = new ArrayList<JetClass>();
|
List<JetClass> classes = new ArrayList<JetClass>();
|
||||||
for (JetDeclaration declaration : namespaceDeclarations) {
|
for (ClassDescriptor classDescriptor : descriptors) {
|
||||||
if (declaration instanceof JetClass) {
|
classes.add(BindingUtils.getClassForDescriptor(context().bindingContext(), classDescriptor));
|
||||||
classes.add((JetClass) declaration);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return ClassSorter.sortUsingInheritanceOrder(classes, context().bindingContext());
|
return ClassSorter.sortUsingInheritanceOrder(classes, context().bindingContext());
|
||||||
}
|
}
|
||||||
@@ -125,7 +123,7 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private JsName generateLocalAlias(@NotNull JetClass declaration) {
|
private JsName generateLocalAlias(@NotNull JetClass declaration) {
|
||||||
JsName globalClassName = context().getNameForElement(declaration);
|
JsName globalClassName = context().getNameForElement(declaration);
|
||||||
JsName localAlias = dummyFunctionScope.declareName(globalClassName.getIdent());
|
JsName localAlias = dummyFunctionScope.declareTemporary();
|
||||||
localToGlobalClassName.put(localAlias, globalClassName);
|
localToGlobalClassName.put(localAlias, globalClassName);
|
||||||
ClassDescriptor descriptor = BindingUtils.getClassDescriptor(context().bindingContext(), declaration);
|
ClassDescriptor descriptor = BindingUtils.getClassDescriptor(context().bindingContext(), declaration);
|
||||||
context().aliaser().setAliasForDescriptor(descriptor, localAlias);
|
context().aliaser().setAliasForDescriptor(descriptor, localAlias);
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import com.google.dart.compiler.backend.js.ast.*;
|
|||||||
import com.google.dart.compiler.util.AstUtil;
|
import com.google.dart.compiler.util.AstUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
|
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
|
||||||
import org.jetbrains.jet.lang.psi.JetDeclaration;
|
|
||||||
import org.jetbrains.k2js.translate.context.Namer;
|
import org.jetbrains.k2js.translate.context.Namer;
|
||||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||||
import org.jetbrains.k2js.translate.general.AbstractTranslator;
|
import org.jetbrains.k2js.translate.general.AbstractTranslator;
|
||||||
@@ -13,12 +12,12 @@ import org.jetbrains.k2js.translate.general.Translation;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.jetbrains.k2js.translate.utils.BindingUtils.getDeclarationsForNamespace;
|
import static org.jetbrains.k2js.translate.utils.DescriptorUtils.getAllClassesDefinedInNamespace;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Pavel.Talanov
|
* @author Pavel.Talanov
|
||||||
* <p/>
|
* <p/>
|
||||||
* Translates single namespace.
|
* Genereate code for a single namespace.
|
||||||
*/
|
*/
|
||||||
public final class NamespaceTranslator extends AbstractTranslator {
|
public final class NamespaceTranslator extends AbstractTranslator {
|
||||||
|
|
||||||
@@ -38,8 +37,8 @@ public final class NamespaceTranslator extends AbstractTranslator {
|
|||||||
super(context.newDeclaration(namespace));
|
super(context.newDeclaration(namespace));
|
||||||
this.namespace = namespace;
|
this.namespace = namespace;
|
||||||
this.namespaceName = context.getNameForDescriptor(namespace);
|
this.namespaceName = context.getNameForDescriptor(namespace);
|
||||||
List<JetDeclaration> namespaceDeclarations = getDeclarationsForNamespace(context.bindingContext(), namespace);
|
this.classDeclarationTranslator = new ClassDeclarationTranslator(context(),
|
||||||
this.classDeclarationTranslator = new ClassDeclarationTranslator(context(), namespaceDeclarations);
|
getAllClassesDefinedInNamespace(namespace));
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
|
|||||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||||
import org.jetbrains.k2js.translate.general.AbstractTranslator;
|
import org.jetbrains.k2js.translate.general.AbstractTranslator;
|
||||||
import org.jetbrains.k2js.translate.intrinsic.FunctionIntrinsic;
|
import org.jetbrains.k2js.translate.intrinsic.FunctionIntrinsic;
|
||||||
|
import org.jetbrains.k2js.translate.utils.AnnotationsUtils;
|
||||||
import org.jetbrains.k2js.translate.utils.TranslationUtils;
|
import org.jetbrains.k2js.translate.utils.TranslationUtils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -77,12 +78,16 @@ public final class CallTranslator extends AbstractTranslator {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
/*package*/ JsExpression translate() {
|
/*package*/ JsExpression translate() {
|
||||||
|
//NOTE: treat native extension function calls as usual calls
|
||||||
if (isIntrinsic()) {
|
if (isIntrinsic()) {
|
||||||
return intrinsicInvocation();
|
return intrinsicInvocation();
|
||||||
}
|
}
|
||||||
if (isConstructor()) {
|
if (isConstructor()) {
|
||||||
return constructorCall();
|
return constructorCall();
|
||||||
}
|
}
|
||||||
|
if (isNative()) {
|
||||||
|
return methodCall();
|
||||||
|
}
|
||||||
if (isExtensionFunctionLiteral()) {
|
if (isExtensionFunctionLiteral()) {
|
||||||
return extensionFunctionLiteralCall();
|
return extensionFunctionLiteralCall();
|
||||||
}
|
}
|
||||||
@@ -92,7 +97,6 @@ public final class CallTranslator extends AbstractTranslator {
|
|||||||
return methodCall();
|
return methodCall();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private boolean isIntrinsic() {
|
private boolean isIntrinsic() {
|
||||||
return context().intrinsics().isIntrinsic(descriptor);
|
return context().intrinsics().isIntrinsic(descriptor);
|
||||||
}
|
}
|
||||||
@@ -123,6 +127,10 @@ public final class CallTranslator extends AbstractTranslator {
|
|||||||
return ReferenceTranslator.translateAsFQReference(descriptor, context());
|
return ReferenceTranslator.translateAsFQReference(descriptor, context());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isNative() {
|
||||||
|
return AnnotationsUtils.isNativeObject(descriptor);
|
||||||
|
}
|
||||||
|
|
||||||
private boolean isExtensionFunctionLiteral() {
|
private boolean isExtensionFunctionLiteral() {
|
||||||
boolean isLiteral = descriptor instanceof VariableAsFunctionDescriptor
|
boolean isLiteral = descriptor instanceof VariableAsFunctionDescriptor
|
||||||
|| descriptor instanceof ExpressionAsFunctionDescriptor;
|
|| descriptor instanceof ExpressionAsFunctionDescriptor;
|
||||||
|
|||||||
@@ -59,6 +59,7 @@ public final class BindingUtils {
|
|||||||
return getDescriptorForExpression(context, declaration, FunctionDescriptor.class);
|
return getDescriptorForExpression(context, declaration, FunctionDescriptor.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO:
|
||||||
@NotNull
|
@NotNull
|
||||||
public static PropertyAccessorDescriptor getPropertyAccessorDescriptor(@NotNull BindingContext context,
|
public static PropertyAccessorDescriptor getPropertyAccessorDescriptor(@NotNull BindingContext context,
|
||||||
@NotNull JetPropertyAccessor declaration) {
|
@NotNull JetPropertyAccessor declaration) {
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import static org.jetbrains.k2js.translate.utils.DescriptorUtils.getSuperclassDe
|
|||||||
|
|
||||||
|
|
||||||
//TODO: can optimise using less dumb implementation
|
//TODO: can optimise using less dumb implementation
|
||||||
|
//TODO: pass list of descriptors here, not the list of jet classes
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.jetbrains.k2js.translate.utils;
|
package org.jetbrains.k2js.translate.utils;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.descriptors.*;
|
import org.jetbrains.jet.lang.descriptors.*;
|
||||||
@@ -187,4 +188,16 @@ public final class DescriptorUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public static List<ClassDescriptor> getAllClassesDefinedInNamespace(@NotNull NamespaceDescriptor namespaceDescriptor) {
|
||||||
|
List<ClassDescriptor> classDescriptors = Lists.newArrayList();
|
||||||
|
for (DeclarationDescriptor descriptor : namespaceDescriptor.getMemberScope().getAllDescriptors()) {
|
||||||
|
if (descriptor instanceof ClassDescriptor) {
|
||||||
|
classDescriptors.add((ClassDescriptor) descriptor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return classDescriptors;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package foo
|
||||||
|
|
||||||
|
import bar.*
|
||||||
|
|
||||||
|
open class A() {
|
||||||
|
open fun f() = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
open class C() : B() {
|
||||||
|
override fun f() = 5
|
||||||
|
}
|
||||||
|
|
||||||
|
fun box() = (A().f() == 3) && (B().f() == 4) && (C().f() == 5)
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package bar
|
||||||
|
|
||||||
|
import foo.A
|
||||||
|
|
||||||
|
open class B() : A() {
|
||||||
|
override fun f() = 4
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package bar
|
||||||
|
|
||||||
|
fun f() = 3;
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package foo
|
||||||
|
|
||||||
|
import bar.*
|
||||||
|
|
||||||
|
fun box() = (f() == 3)
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package org.jetbrains.k2js.test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Pavel Talanov
|
||||||
|
*/
|
||||||
|
public class MultiNamespaceTest extends TranslationTest {
|
||||||
|
final private static String MAIN = "multiNamespace/";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String mainDirectory() {
|
||||||
|
return MAIN;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testFunctionsVisibleFromOtherNamespace() throws Exception {
|
||||||
|
testFooBoxIsTrue("functionsVisibleFromOtherNamespace");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testClassesInheritedFromOtherNamespace() throws Exception {
|
||||||
|
testFooBoxIsTrue("classesInheritedFromOtherNamespace");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void testFooBoxIsTrue(String dirName) throws Exception {
|
||||||
|
testMultiFile(dirName, "foo", "box", true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user