JsTests speed up: Analyze standard library only once.

This commit is contained in:
Pavel V. Talanov
2012-07-02 13:28:59 +04:00
parent 2975cda4d6
commit 3504106981
24 changed files with 308 additions and 315 deletions
+4
View File
@@ -23,8 +23,12 @@
<option name="TRANSPORT" value="0" /> <option name="TRANSPORT" value="0" />
<option name="LOCAL" value="true" /> <option name="LOCAL" value="true" />
</RunnerSettings> </RunnerSettings>
<RunnerSettings RunnerId="Profile ">
<option name="myExternalizedOptions" value="&#13;&#10;use-64-bit=true&#13;&#10;additional-options2=onexit\=snapshot&#13;&#10;" />
</RunnerSettings>
<RunnerSettings RunnerId="Run" /> <RunnerSettings RunnerId="Run" />
<ConfigurationWrapper RunnerId="Debug" /> <ConfigurationWrapper RunnerId="Debug" />
<ConfigurationWrapper RunnerId="Profile " />
<ConfigurationWrapper RunnerId="Run" /> <ConfigurationWrapper RunnerId="Run" />
<method /> <method />
</configuration> </configuration>
+2 -2
View File
@@ -1,6 +1,6 @@
package js; package js;
native native
annotation class native(name : String = "") {} public annotation class native(name : String = "") {}
native native
annotation class library(name : String = "") {} public annotation class library(name : String = "") {}
+7 -7
View File
@@ -7,18 +7,18 @@ import java.lang.*;
native native
val noImpl : Nothing = throw Exception(); public val noImpl : Nothing = throw Exception();
library("println") library("println")
fun println() {} public fun println() {}
library("println") library("println")
fun println(s : Any?) {} public fun println(s : Any?) {}
library("print") library("print")
fun print(s : Any?) {} public fun print(s : Any?) {}
//TODO: consistent parseInt //TODO: consistent parseInt
library("parseInt") library("parseInt")
fun parseInt(s : String) : Int = js.noImpl public fun parseInt(s : String) : Int = js.noImpl
library library
fun safeParseInt(s : String) : Int? = js.noImpl public fun safeParseInt(s : String) : Int? = js.noImpl
library library
fun safeParseDouble(s : String) : Double? = js.noImpl public fun safeParseDouble(s : String) : Double? = js.noImpl
+2 -2
View File
@@ -1,6 +1,6 @@
package js package js
native native
class Date() { public class Date() {
fun getTime() : Int = js.noImpl public fun getTime() : Int = js.noImpl
} }
+1 -1
View File
@@ -221,7 +221,7 @@ native public trait Node {
public fun lookupNamespaceURI(arg1: String?): String = js.noImpl public fun lookupNamespaceURI(arg1: String?): String = js.noImpl
public fun isEqualNode(arg1: Node): Boolean = js.noImpl public fun isEqualNode(arg1: Node): Boolean = js.noImpl
class object { public class object {
public val ELEMENT_NODE: Short = 1 public val ELEMENT_NODE: Short = 1
public val ATTRIBUTE_NODE: Short = 2 public val ATTRIBUTE_NODE: Short = 2
public val TEXT_NODE: Short = 3 public val TEXT_NODE: Short = 3
+1 -1
View File
@@ -1,4 +1,4 @@
package java.io package java.io
library library
class IOException(message: String = "") : Exception() {} public class IOException(message: String = "") : Exception() {}
+6 -6
View File
@@ -10,25 +10,25 @@ trait Iterable<T> {
} }
library library
open class Exception() : Throwable() {} open public class Exception() : Throwable() {}
library("splitString") library("splitString")
public fun String.split(regex : String) : Array<String> = js.noImpl public fun String.split(regex : String) : Array<String> = js.noImpl
library library
class IllegalArgumentException(message: String = "") : Exception() {} public class IllegalArgumentException(message: String = "") : Exception() {}
library library
class IllegalStateException(message: String = "") : Exception() {} public class IllegalStateException(message: String = "") : Exception() {}
library library
class IndexOutOfBoundsException(message: String = "") : Exception() {} public class IndexOutOfBoundsException(message: String = "") : Exception() {}
library library
class UnsupportedOperationException(message: String = "") : Exception() {} public class UnsupportedOperationException(message: String = "") : Exception() {}
library library
class NumberFormatException(message: String = "") : Exception() {} public class NumberFormatException(message: String = "") : Exception() {}
public trait Comparable<T> { public trait Comparable<T> {
fun compareTo(that: T): Int fun compareTo(that: T): Int
+68 -69
View File
@@ -17,6 +17,7 @@ public trait Iterator<T> {
open fun remove() : Unit = js.noImpl open fun remove() : Unit = js.noImpl
} }
/*
val Collections = object { val Collections = object {
library("collectionsMax") library("collectionsMax")
public fun max<T>(col : Collection<T>, comp : Comparator<T>) : T = js.noImpl public fun max<T>(col : Collection<T>, comp : Comparator<T>) : T = js.noImpl
@@ -51,9 +52,7 @@ val Collections = object {
list[i2] = tmp list[i2] = tmp
} }
} }
}*/
}
library library
public open class ArrayList<E>() : java.util.List<E> { public open class ArrayList<E>() : java.util.List<E> {
@@ -79,19 +78,19 @@ public open class ArrayList<E>() : java.util.List<E> {
library library
public trait Collection<E> : java.lang.Iterable<E> { public trait Collection<E> : java.lang.Iterable<E> {
open fun size() : Int open public fun size() : Int
open fun isEmpty() : Boolean open public fun isEmpty() : Boolean
open fun contains(o : Any?) : Boolean open public fun contains(o : Any?) : Boolean
override fun iterator() : java.util.Iterator<E> override public fun iterator() : java.util.Iterator<E>
// open fun toArray() : Array<Any?> // open public fun toArray() : Array<Any?>
// open fun toArray<T>(a : Array<out T>) : Array<T> // open public fun toArray<T>(a : Array<out T>) : Array<T>
open fun add(e : E) : Boolean open public fun add(e : E) : Boolean
open fun remove(o : Any?) : Boolean open public fun remove(o : Any?) : Boolean
//open fun containsAll(c : java.util.Collection<*>) : Boolean //open public fun containsAll(c : java.util.Collection<*>) : Boolean
open fun addAll(c : java.util.Collection<out E>) : Boolean open public fun addAll(c : java.util.Collection<out E>) : Boolean
//open fun removeAll(c : java.util.Collection<*>) : Boolean //open public fun removeAll(c : java.util.Collection<*>) : Boolean
//open fun retainAll(c : java.util.Collection<*>) : Boolean //open public fun retainAll(c : java.util.Collection<*>) : Boolean
open fun clear() : Unit open public fun clear() : Unit
} }
library library
@@ -120,44 +119,44 @@ public abstract open class AbstractList<E>() : AbstractCollection<E>(), List<E>
library library
public trait List<E> : Collection<E> { public trait List<E> : Collection<E> {
override fun size() : Int override public fun size() : Int
override fun isEmpty() : Boolean override public fun isEmpty() : Boolean
override fun contains(o : Any?) : Boolean override public fun contains(o : Any?) : Boolean
override fun iterator() : java.util.Iterator<E> override public fun iterator() : java.util.Iterator<E>
// override fun toArray() : Array<Any?> // override public fun toArray() : Array<Any?>
// Simulate Java's array covariance // Simulate Java's array covariance
// override fun toArray<T>(a : Array<out T>) : Array<T> // override public fun toArray<T>(a : Array<out T>) : Array<T>
override fun add(e : E) : Boolean override public fun add(e : E) : Boolean
override fun remove(o : Any?) : Boolean override public fun remove(o : Any?) : Boolean
// override fun containsAll(c : java.util.Collection<*>) : Boolean // override public fun containsAll(c : java.util.Collection<*>) : Boolean
override fun addAll(c : java.util.Collection<out E>) : Boolean override public fun addAll(c : java.util.Collection<out E>) : Boolean
// open fun addAll(index : Int, c : java.util.Collection<out E>) : Boolean // open public fun addAll(index : Int, c : java.util.Collection<out E>) : Boolean
// override fun removeAll(c : java.util.Collection<*>) : Boolean // override public fun removeAll(c : java.util.Collection<*>) : Boolean
// override fun retainAll(c : java.util.Collection<*>) : Boolean // override public fun retainAll(c : java.util.Collection<*>) : Boolean
override fun clear() : Unit override public fun clear() : Unit
open fun get(index : Int) : E open public fun get(index : Int) : E
open fun set(index : Int, element : E) : E open public fun set(index : Int, element : E) : E
open fun add(index : Int, element : E) : Unit open public fun add(index : Int, element : E) : Unit
open fun remove(index : Int) : E open public fun remove(index : Int) : E
// open fun indexOf(o : Any?) : Int // open public fun indexOf(o : Any?) : Int
// open fun lastIndexOf(o : Any?) : Int // open public fun lastIndexOf(o : Any?) : Int
} }
library library
public trait Set<E> : Collection<E> { public trait Set<E> : Collection<E> {
override fun size() : Int override public fun size() : Int
override fun isEmpty() : Boolean override public fun isEmpty() : Boolean
override fun contains(o : Any?) : Boolean override public fun contains(o : Any?) : Boolean
override fun iterator() : java.util.Iterator<E> override public fun iterator() : java.util.Iterator<E>
// override fun toArray() : Array<Any?> // override public fun toArray() : Array<Any?>
// override fun toArray<T>(a : Array<out T>) : Array<T> // override public fun toArray<T>(a : Array<out T>) : Array<T>
override fun add(e : E) : Boolean override public fun add(e : E) : Boolean
override fun remove(o : Any?) : Boolean override public fun remove(o : Any?) : Boolean
//override fun containsAll(c : java.util.Collection<*>) : Boolean //override public fun containsAll(c : java.util.Collection<*>) : Boolean
override fun addAll(c : java.util.Collection<out E>) : Boolean override public fun addAll(c : java.util.Collection<out E>) : Boolean
//override fun retainAll(c : java.util.Collection<*>) : Boolean //override public fun retainAll(c : java.util.Collection<*>) : Boolean
//override fun removeAll(c : java.util.Collection<*>) : Boolean //override public fun removeAll(c : java.util.Collection<*>) : Boolean
override fun clear() : Unit override public fun clear() : Unit
} }
library library
@@ -174,28 +173,28 @@ public open class HashSet<E>() : java.util.Set<E> {
library library
public trait Map<K, V> { public trait Map<K, V> {
open fun size() : Int open public fun size() : Int
open fun isEmpty() : Boolean open public fun isEmpty() : Boolean
open fun containsKey(key : Any?) : Boolean open public fun containsKey(key : Any?) : Boolean
open fun containsValue(value : Any?) : Boolean open public fun containsValue(value : Any?) : Boolean
open fun get(key : Any?) : V? open public fun get(key : Any?) : V?
open fun put(key : K, value : V) : V? open public fun put(key : K, value : V) : V?
open fun remove(key : Any?) : V? open public fun remove(key : Any?) : V?
open fun putAll(m : java.util.Map<out K, out V>) : Unit open public fun putAll(m : java.util.Map<out K, out V>) : Unit
open fun clear() : Unit open public fun clear() : Unit
open fun keySet() : java.util.Set<K> open public fun keySet() : java.util.Set<K>
open fun values() : java.util.Collection<V> open public fun values() : java.util.Collection<V>
open fun entrySet() : java.util.Set<Entry<K, V>> open public fun entrySet() : java.util.Set<Entry<K, V>>
// open fun equals(o : Any?) : Boolean // open public fun equals(o : Any?) : Boolean
// open fun hashCode() : Int // open public fun hashCode() : Int
trait Entry<K, V> { trait Entry<K, V> {
open fun getKey() : K open public fun getKey() : K
open fun getValue() : V open public fun getValue() : V
open fun setValue(value : V) : V open public fun setValue(value : V) : V
// open fun equals(o : Any?) : Boolean // open public fun equals(o : Any?) : Boolean
// open fun hashCode() : Int // open public fun hashCode() : Int
} }
} }
@@ -244,4 +243,4 @@ public class StringBuilder() : Appendable {
} }
library library
class NoSuchElementException() : Exception() {} public class NoSuchElementException() : Exception() {}
+6 -6
View File
@@ -4,21 +4,21 @@ import java.util.*;
import js.library import js.library
native native
class Json() { public class Json() {
} }
library("jsonSet") library("jsonSet")
fun Json.set(paramName : String, value : Any?) : Unit = js.noImpl public fun Json.set(paramName : String, value : Any?) : Unit = js.noImpl
library("jsonGet") library("jsonGet")
fun Json.get(paramName : String) : Any? = js.noImpl public fun Json.get(paramName : String) : Any? = js.noImpl
library("jsonFromTuples") library("jsonFromTuples")
fun json(vararg pairs : Tuple2<String, Any?>) : Json = js.noImpl public fun json(vararg pairs : Tuple2<String, Any?>) : Json = js.noImpl
library("jsonFromTuples") library("jsonFromTuples")
fun json2(pairs : Array<Tuple2<String, Any?>>) : Json = js.noImpl public fun json2(pairs : Array<Tuple2<String, Any?>>) : Json = js.noImpl
library("jsonAddProperties") library("jsonAddProperties")
fun Json.add(other : Json) : Json = js.noImpl public fun Json.add(other : Json) : Json = js.noImpl
+23 -23
View File
@@ -4,29 +4,29 @@ import js.native
//TODO: declare using number //TODO: declare using number
native native
class MathClass() { public class MathClass() {
val PI : Double = js.noImpl; public val PI : Double = js.noImpl;
fun random() : Double = js.noImpl; public fun random() : Double = js.noImpl;
fun abs(value : Double) : Double = js.noImpl public fun abs(value : Double) : Double = js.noImpl
fun acos(value : Double) : Double = js.noImpl public fun acos(value : Double) : Double = js.noImpl
fun asin(value : Double) : Double = js.noImpl public fun asin(value : Double) : Double = js.noImpl
fun atan(value : Double) : Double = js.noImpl public fun atan(value : Double) : Double = js.noImpl
fun atan2(x : Double, y : Double) : Double = js.noImpl public fun atan2(x : Double, y : Double) : Double = js.noImpl
fun cos(value : Double) : Double = js.noImpl public fun cos(value : Double) : Double = js.noImpl
fun sin(value : Double) : Double = js.noImpl public fun sin(value : Double) : Double = js.noImpl
fun exp(value : Double) : Double = js.noImpl public fun exp(value : Double) : Double = js.noImpl
fun max(vararg values : Double) : Double = js.noImpl public fun max(vararg values : Double) : Double = js.noImpl
fun max(vararg values : Int) : Int = js.noImpl public fun max(vararg values : Int) : Int = js.noImpl
fun min(vararg values : Int) : Int = js.noImpl public fun min(vararg values : Int) : Int = js.noImpl
fun min(vararg values : Double) : Double = js.noImpl public fun min(vararg values : Double) : Double = js.noImpl
fun sqrt(value : Double) : Double = js.noImpl public fun sqrt(value : Double) : Double = js.noImpl
fun tan(value : Double) : Double = js.noImpl public fun tan(value : Double) : Double = js.noImpl
fun log(value : Double) : Double = js.noImpl public fun log(value : Double) : Double = js.noImpl
fun pow(base : Double, exp : Double) : Double = js.noImpl public fun pow(base : Double, exp : Double) : Double = js.noImpl
fun round(value : Number) : Int = js.noImpl public fun round(value : Number) : Int = js.noImpl
fun floor(value : Number) : Int = js.noImpl public fun floor(value : Number) : Int = js.noImpl
fun ceil(value : Number) : Int = js.noImpl public fun ceil(value : Number) : Int = js.noImpl
} }
native native
val Math = MathClass(); public val Math = MathClass();
+1 -1
View File
@@ -3,4 +3,4 @@ package js.dom.html
import js.noImpl import js.noImpl
native native
val window : Window = js.noImpl public val window : Window = js.noImpl
+41 -41
View File
@@ -3,70 +3,70 @@ package js.jquery;
import js.dom.core.Element import js.dom.core.Element
native native
class JQuery() { public public class JQuery() {
fun addClass(className : String) : JQuery = js.noImpl; public fun addClass(className : String) : JQuery = js.noImpl;
fun addClass(f : Element.(Int, String)->String) = js.noImpl; public fun addClass(f : Element.(Int, String)->String) = js.noImpl;
fun attr(attrName : String) = ""; public fun attr(attrName : String) = "";
fun attr(attrName : String, value : String) = this; public fun attr(attrName : String, value : String) = this;
fun html() : String = ""; public fun html() : String = "";
fun html(s : String) = this; public fun html(s : String) = this;
fun html(f : Element.(Int, String)->String) = this; public fun html(f : Element.(Int, String)->String) = this;
fun hasClass(className : String) = true public fun hasClass(className : String) = true
fun removeClass(className : String) = this public fun removeClass(className : String) = this
fun height() = 0 public fun height() = 0
fun width() = 0 public fun width() = 0
fun click() = this; public fun click() = this;
fun mousedown(handler : Element.(MouseEvent)->Unit) = this; public fun mousedown(handler : Element.(MouseEvent)->Unit) = this;
fun mouseup(handler : Element.(MouseEvent)->Unit) = this; public fun mouseup(handler : Element.(MouseEvent)->Unit) = this;
fun mousemove(handler : Element.(MouseEvent)->Unit) = this; public fun mousemove(handler : Element.(MouseEvent)->Unit) = this;
fun dblclick(handler : Element.(MouseClickEvent)->Unit) = this; public fun dblclick(handler : Element.(MouseClickEvent)->Unit) = this;
fun click(handler : Element.(MouseClickEvent)->Unit) = this; public fun click(handler : Element.(MouseClickEvent)->Unit) = this;
fun load(handler : Element.()->Unit) = this; public fun load(handler : Element.()->Unit) = this;
fun change(handler : Element.()->Unit) = this; public fun change(handler : Element.()->Unit) = this;
fun append(str : String) = this; public fun append(str : String) = this;
fun ready(handler : ()->Unit) = this; public fun ready(handler : ()->Unit) = this;
fun text(text : String) = this; public fun text(text : String) = this;
fun slideUp() = this; public fun slideUp() = this;
fun hover(handlerInOut : Element.() -> Unit) = this; public fun hover(handlerInOut : Element.() -> Unit) = this;
fun hover(handlerIn : Element.() -> Unit, handlerOut : Element.() -> Unit) = this; public fun hover(handlerIn : Element.() -> Unit, handlerOut : Element.() -> Unit) = this;
fun next() : JQuery = js.noImpl public fun next() : JQuery = js.noImpl
fun parent() : JQuery = js.noImpl public fun parent() : JQuery = js.noImpl
fun `val`() : String? = js.noImpl public fun `val`() : String? = js.noImpl
} }
native native
open class MouseEvent() { open public class MouseEvent() {
val pageX : Double = 0.0; public val pageX : Double = 0.0;
val pageY : Double = 0.0; public val pageY : Double = 0.0;
fun preventDefault() {} public fun preventDefault() {}
fun isDefaultPrevented() : Boolean = true; public fun isDefaultPrevented() : Boolean = true;
} }
native native
class MouseClickEvent() : MouseEvent() { public class MouseClickEvent() : MouseEvent() {
val which : Int = 0; val which : Int = 0;
} }
native("$") native("$")
fun jq(selector : String) = JQuery(); public fun jq(selector : String) = JQuery();
native("$") native("$")
fun jq(selector : String, context : Element) = JQuery(); public fun jq(selector : String, context : Element) = JQuery();
native("$") native("$")
fun jq(callback : () -> Unit) = JQuery(); public fun jq(callback : () -> Unit) = JQuery();
native("$") native("$")
fun jq(obj : JQuery) = JQuery(); public fun jq(obj : JQuery) = JQuery();
native("$") native("$")
fun jq(el : Element) = JQuery(); public fun jq(el : Element) = JQuery();
native("$") native("$")
fun jq() = JQuery(); public fun jq() = JQuery();
-63
View File
@@ -1,63 +0,0 @@
package raphael
import js.*;
import js.*;
native
open class Element() {
fun attr(name : String, value : Any?) : Element = this
fun attr(nameToValue : Json) : Element = this
fun click(handler : Element.()-> Unit) = this;
fun mouseover(handler : Element.() -> Unit) = this
fun mouseout(handler : Element.() -> Unit) = this
fun getTotalLength() : Double = 0.0
fun getPointAtLength(length : Double) : Point = js.noImpl
fun animate(params : Json, ms : Int, callback : ()-> Unit) : Element = js.noImpl
fun animate(params : Json, ms : Int, s : String) : Element = js.noImpl
fun animate(params : Json, ms : Int) : Element = js.noImpl
//fun mouse
}
native
class Set() : Element() {
fun push(el : Element) = js.noImpl
}
native
class Paper() {
fun path(pathString : String) : Element = Element();
fun path() : Element = Element();
fun ellipse(x : Int, y : Int, rx : Int, ry : Int) : Element = js.noImpl
fun circle(x : Int, y : Int, r : Int) : Element = js.noImpl
fun set() : Set = js.noImpl
val customAttributes : Json = Json();
}
native("Raphael")
fun Raphael(elementId : String, width : Int, height : Int) : Paper = Paper();
native("Raphael")
fun Raphael(elementId : String, width : Int, height : Int, initFun : Paper.() -> Unit) : Unit = js.noImpl
native
fun getColor() : Color = js.noImpl
native
fun resetColors() = js.noImpl
native
class Color() {
}
native
class Point() {
val x = 0
val y = 0
val alpha = 0
}
+1 -1
View File
@@ -2,4 +2,4 @@ package kotlin
import java.util.Map as JMap import java.util.Map as JMap
/** Provides [] access to maps */ /** Provides [] access to maps */
fun <K, V> JMap<K, V>.set(key : K, value : V) = this.put(key, value) public fun <K, V> JMap<K, V>.set(key : K, value : V) = this.put(key, value)
+1 -1
View File
@@ -7,5 +7,5 @@ import org.w3c.dom.Document
/** /**
* Provides access to the current active browsers DOM for the currently visible page. * Provides access to the current active browsers DOM for the currently visible page.
*/ */
native("document") var document: Document = null!! native("document") public var document: Document = null!!
+1 -1
View File
@@ -3,7 +3,7 @@ package kotlin.dom
import org.w3c.dom.Document import org.w3c.dom.Document
import org.w3c.dom.Node import org.w3c.dom.Node
fun createDocument(): Document { public fun createDocument(): Document {
return browser.document.implementation.createDocument(null, null, null) return browser.document.implementation.createDocument(null, null, null)
} }
@@ -17,64 +17,39 @@
package org.jetbrains.k2js.test.config; package org.jetbrains.k2js.test.config;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.k2js.config.Config; import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.config.EcmaVersion; import org.jetbrains.k2js.config.EcmaVersion;
import org.jetbrains.k2js.utils.JetFileUtils;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
* @author Pavel Talanov * @author Pavel Talanov
*/ */
//TODO: review/refactor public class TestConfig extends Config {
public final class TestConfig extends Config {
@Nullable
private /*var*/ List<JetFile> jsLibFiles = null;
public TestConfig(@NotNull Project project, @NotNull EcmaVersion version) {
super(project, version);
}
@NotNull @NotNull
private static List<JetFile> initLibFiles(@NotNull Project project) { private final List<JetFile> jsLibFiles;
List<JetFile> libFiles = new ArrayList<JetFile>(); @NotNull
for (String libFileName : LIB_FILE_NAMES) { private final BindingContext libraryContext;
JetFile file = null;
try { public TestConfig(@NotNull Project project, @NotNull EcmaVersion version,
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed") @NotNull List<JetFile> files, @NotNull BindingContext context) {
InputStream stream = new FileInputStream(LIBRARIES_LOCATION + libFileName); super(project, version);
try { jsLibFiles = files;
String text = FileUtil.loadTextAndClose(stream); libraryContext = context;
file = JetFileUtils.createPsiFile(libFileName, text, project); }
}
catch (IOException e) { @Override
e.printStackTrace(); public BindingContext getLibraryBindingContext() {
} return libraryContext;
libFiles.add(file);
}
catch (Exception e) {
//TODO: throw generic exception
throw new IllegalStateException(e);
}
}
return libFiles;
} }
@Override @Override
@NotNull @NotNull
public List<JetFile> generateLibFiles() { public List<JetFile> generateLibFiles() {
if (jsLibFiles == null) {
jsLibFiles = initLibFiles(getProject());
}
return jsLibFiles; return jsLibFiles;
} }
} }
@@ -17,19 +17,27 @@
package org.jetbrains.k2js.test.utils; package org.jetbrains.k2js.test.utils;
import closurecompiler.internal.com.google.common.collect.Maps; import closurecompiler.internal.com.google.common.collect.Maps;
import com.google.common.base.Predicates;
import com.google.dart.compiler.backend.js.ast.JsProgram; import com.google.dart.compiler.backend.js.ast.JsProgram;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.k2js.analyze.AnalyzerFacadeForJS;
import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.config.EcmaVersion; import org.jetbrains.k2js.config.EcmaVersion;
import org.jetbrains.k2js.facade.K2JSTranslator; import org.jetbrains.k2js.facade.K2JSTranslator;
import org.jetbrains.k2js.facade.MainCallParameters; import org.jetbrains.k2js.facade.MainCallParameters;
import org.jetbrains.k2js.generate.CodeGenerator; import org.jetbrains.k2js.generate.CodeGenerator;
import org.jetbrains.k2js.test.config.TestConfig; import org.jetbrains.k2js.test.config.TestConfig;
import org.jetbrains.k2js.utils.JetFileUtils;
import java.io.File; import java.io.*;
import java.io.FileWriter; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -44,7 +52,33 @@ public final class TranslationUtils {
} }
@NotNull @NotNull
private static final Map<EcmaVersion, K2JSTranslator> translators = Maps.newHashMap(); private static final Map<EcmaVersion, Config> testConfigs = Maps.newHashMap();
@Nullable
private static BindingContext libraryContext = null;
@NotNull
public static BindingContext getLibraryContext(@NotNull Project project, @NotNull List<JetFile> files) {
if (libraryContext == null) {
AnalyzeExhaust exhaust = AnalyzerFacadeForJS
.analyzeFiles(files, Predicates.<PsiFile>alwaysFalse(),
Config.getEmptyConfig(project));
libraryContext = exhaust.getBindingContext();
AnalyzerFacadeForJS.checkForErrors(files, libraryContext);
}
return libraryContext;
}
@NotNull
public static Config getConfig(@NotNull Project project, @NotNull EcmaVersion version) {
List<JetFile> files = initLibFiles(project);
Config config = testConfigs.get(version);
if (config == null) {
config = new TestConfig(project, version, files, getLibraryContext(project, files));
testConfigs.put(version, config);
}
return config;
}
public static void translateFiles(@NotNull Project project, @NotNull List<String> inputFiles, public static void translateFiles(@NotNull Project project, @NotNull List<String> inputFiles,
@NotNull String outputFile, @NotNull String outputFile,
@@ -64,11 +98,31 @@ public final class TranslationUtils {
@NotNull @NotNull
private static K2JSTranslator getTranslator(@NotNull Project project, @NotNull EcmaVersion version) { private static K2JSTranslator getTranslator(@NotNull Project project, @NotNull EcmaVersion version) {
K2JSTranslator translator = translators.get(version); return new K2JSTranslator(getConfig(project, version));
if (translator == null) { }
translators.put(version, translator);
translator = new K2JSTranslator(new TestConfig(project, version)); @NotNull
public static List<JetFile> initLibFiles(@NotNull Project project) {
List<JetFile> libFiles = new ArrayList<JetFile>();
for (String libFileName : Config.LIB_FILE_NAMES) {
JetFile file = null;
try {
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
InputStream stream = new FileInputStream(Config.LIBRARIES_LOCATION + libFileName);
try {
String text = FileUtil.loadTextAndClose(stream);
file = JetFileUtils.createPsiFile(libFileName, text, project);
}
catch (IOException e) {
e.printStackTrace();
}
libFiles.add(file);
}
catch (Exception e) {
//TODO: throw generic exception
throw new IllegalStateException(e);
}
} }
return translator; return libFiles;
} }
} }
@@ -34,7 +34,9 @@ import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetImportDirective; import org.jetbrains.jet.lang.psi.JetImportDirective;
import org.jetbrains.jet.lang.psi.JetPsiFactory; import org.jetbrains.jet.lang.psi.JetPsiFactory;
import org.jetbrains.jet.lang.resolve.*; import org.jetbrains.jet.lang.resolve.*;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.WritableScope; import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
import org.jetbrains.jet.lang.types.lang.JetStandardClasses; import org.jetbrains.jet.lang.types.lang.JetStandardClasses;
import org.jetbrains.k2js.config.Config; import org.jetbrains.k2js.config.Config;
@@ -44,6 +46,8 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isRootNamespace;
/** /**
* @author Pavel Talanov * @author Pavel Talanov
*/ */
@@ -75,13 +79,13 @@ public final class AnalyzerFacadeForJS {
return analyzeFiles(files, filesToAnalyzeCompletely, config, false); return analyzeFiles(files, filesToAnalyzeCompletely, config, false);
} }
//TODO: refactor
@NotNull @NotNull
public static AnalyzeExhaust analyzeFiles( public static AnalyzeExhaust analyzeFiles(
@NotNull Collection<JetFile> files, @NotNull Collection<JetFile> files,
@NotNull Predicate<PsiFile> filesToAnalyzeCompletely, @NotNull Config config, @NotNull Predicate<PsiFile> filesToAnalyzeCompletely, @NotNull Config config,
boolean storeContextForBodiesResolve) { boolean storeContextForBodiesResolve) {
Project project = config.getProject(); Project project = config.getProject();
BindingTraceContext bindingTraceContext = new BindingTraceContext();
final ModuleDescriptor owner = new ModuleDescriptor(Name.special("<module>")); final ModuleDescriptor owner = new ModuleDescriptor(Name.special("<module>"));
@@ -90,15 +94,22 @@ public final class AnalyzerFacadeForJS {
TopDownAnalysisParameters topDownAnalysisParameters = new TopDownAnalysisParameters( TopDownAnalysisParameters topDownAnalysisParameters = new TopDownAnalysisParameters(
completely, false, false, Collections.<AnalyzerScriptParameter>emptyList()); completely, false, false, Collections.<AnalyzerScriptParameter>emptyList());
BindingContext libraryBindingContext = config.getLibraryBindingContext();
BindingTrace trace = libraryBindingContext == null ?
new ObservableBindingTrace(new BindingTraceContext()) :
new DelegatingBindingTrace(libraryBindingContext);
InjectorForTopDownAnalyzerForJs injector = new InjectorForTopDownAnalyzerForJs( InjectorForTopDownAnalyzerForJs injector = new InjectorForTopDownAnalyzerForJs(
project, topDownAnalysisParameters, new ObservableBindingTrace(bindingTraceContext), owner, project, topDownAnalysisParameters, trace, owner,
JsConfiguration.jsLibConfiguration(project)); new JsConfiguration(project, libraryBindingContext));
try { try {
injector.getTopDownAnalyzer().analyzeFiles(withJsLibAdded(files, config), Collections.<AnalyzerScriptParameter>emptyList()); Collection<JetFile> allFiles = libraryBindingContext != null ?
files :
withJsLibAdded(files, config);
injector.getTopDownAnalyzer().analyzeFiles(allFiles, Collections.<AnalyzerScriptParameter>emptyList());
BodiesResolveContext bodiesResolveContext = storeContextForBodiesResolve ? BodiesResolveContext bodiesResolveContext = storeContextForBodiesResolve ?
new CachedBodiesResolveContext(injector.getTopDownAnalysisContext()) : new CachedBodiesResolveContext(injector.getTopDownAnalysisContext()) :
null; null;
return AnalyzeExhaust.success(bindingTraceContext.getBindingContext(), bodiesResolveContext); return AnalyzeExhaust.success(trace.getBindingContext(), bodiesResolveContext);
} }
finally { finally {
injector.destroy(); injector.destroy();
@@ -116,7 +127,7 @@ public final class AnalyzerFacadeForJS {
config.getProject(), Collections.<AnalyzerScriptParameter>emptyList(), completely, traceContext, bodiesResolveContext); config.getProject(), Collections.<AnalyzerScriptParameter>emptyList(), completely, traceContext, bodiesResolveContext);
} }
private static void checkForErrors(@NotNull Collection<JetFile> allFiles, @NotNull BindingContext bindingContext) { public static void checkForErrors(@NotNull Collection<JetFile> allFiles, @NotNull BindingContext bindingContext) {
AnalyzingUtils.throwExceptionOnErrors(bindingContext); AnalyzingUtils.throwExceptionOnErrors(bindingContext);
for (JetFile file : allFiles) { for (JetFile file : allFiles) {
AnalyzingUtils.checkForSyntacticErrors(file); AnalyzingUtils.checkForSyntacticErrors(file);
@@ -147,13 +158,12 @@ public final class AnalyzerFacadeForJS {
@NotNull @NotNull
private final Project project; private final Project project;
@Nullable
private final BindingContext contextToBaseOn;
public static JsConfiguration jsLibConfiguration(@NotNull Project project) { private JsConfiguration(@NotNull Project project, @Nullable BindingContext contextToBaseOn) {
return new JsConfiguration(project);
}
private JsConfiguration(@NotNull Project project) {
this.project = project; this.project = project;
this.contextToBaseOn = contextToBaseOn;
} }
@Override @Override
@@ -170,6 +180,16 @@ public final class AnalyzerFacadeForJS {
@NotNull WritableScope namespaceMemberScope) { @NotNull WritableScope namespaceMemberScope) {
DefaultModuleConfiguration.createStandardConfiguration(project, true) DefaultModuleConfiguration.createStandardConfiguration(project, true)
.extendNamespaceScope(trace, namespaceDescriptor, namespaceMemberScope); .extendNamespaceScope(trace, namespaceDescriptor, namespaceMemberScope);
if (contextToBaseOn == null) {
return;
}
if (!isRootNamespace(namespaceDescriptor)) {
return;
}
NamespaceDescriptor rootNamespaceScope = contextToBaseOn.get(BindingContext.FQNAME_TO_NAMESPACE_DESCRIPTOR, FqName.ROOT);
assert rootNamespaceScope != null;
JetScope memberScope = rootNamespaceScope.getMemberScope();
namespaceMemberScope.importScope(memberScope);
} }
} }
} }
@@ -20,6 +20,7 @@ import com.intellij.openapi.project.Project;
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.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@@ -63,7 +64,6 @@ public abstract class Config {
"/core/core.kt", "/core/core.kt",
"/core/math.kt", "/core/math.kt",
"/core/json.kt", "/core/json.kt",
"/raphael/raphael.kt",
"/stdlib/JUMaps.kt", "/stdlib/JUMaps.kt",
"/stdlib/browser.kt", "/stdlib/browser.kt",
"/core/dom.kt", "/core/dom.kt",
@@ -144,4 +144,9 @@ public abstract class Config {
} }
return libFiles; return libFiles;
} }
@Nullable
public BindingContext getLibraryBindingContext() {
return null;
}
} }
@@ -21,13 +21,17 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.JetClass;
import org.jetbrains.jet.lang.psi.JetDeclaration;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetNamedFunction;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.k2js.translate.utils.BindingUtils;
import java.util.List; import java.util.List;
import static org.jetbrains.k2js.translate.utils.BindingUtils.getNullableDescriptorForFunction;
/** /**
* Helps find functions which are annotated with a @Test annotation from junit * Helps find functions which are annotated with a @Test annotation from junit
*/ */
@@ -36,17 +40,18 @@ public class JetTestFunctionDetector {
} }
public static boolean isTest(@NotNull BindingContext bindingContext, @NotNull JetNamedFunction function) { public static boolean isTest(@NotNull BindingContext bindingContext, @NotNull JetNamedFunction function) {
FunctionDescriptor functionDescriptor = BindingUtils.getFunctionDescriptor(bindingContext, function); FunctionDescriptor functionDescriptor = getNullableDescriptorForFunction(bindingContext, function);
if (functionDescriptor != null) { if (functionDescriptor == null) {
List<AnnotationDescriptor> annotations = functionDescriptor.getAnnotations(); return false;
if (annotations != null) { }
for (AnnotationDescriptor annotation : annotations) { List<AnnotationDescriptor> annotations = functionDescriptor.getAnnotations();
// TODO ideally we should find the fully qualified name here... if (annotations != null) {
JetType type = annotation.getType(); for (AnnotationDescriptor annotation : annotations) {
String name = type.toString(); // TODO ideally we should find the fully qualified name here...
if (name.equals("Test")) { JetType type = annotation.getType();
return true; String name = type.toString();
} if (name.equals("Test")) {
return true;
} }
} }
} }
@@ -70,13 +75,15 @@ public class JetTestFunctionDetector {
} }
@Nullable @Nullable
private static List<JetNamedFunction> getTestFunctions(@NotNull BindingContext bindingContext, @NotNull List<JetDeclaration> declarations) { private static List<JetNamedFunction> getTestFunctions(@NotNull BindingContext bindingContext,
@NotNull List<JetDeclaration> declarations) {
List<JetNamedFunction> answer = Lists.newArrayList(); List<JetNamedFunction> answer = Lists.newArrayList();
for (JetDeclaration declaration : declarations) { for (JetDeclaration declaration : declarations) {
if (declaration instanceof JetClass) { if (declaration instanceof JetClass) {
JetClass klass = (JetClass) declaration; JetClass klass = (JetClass) declaration;
answer.addAll(getTestFunctions(bindingContext, klass.getDeclarations())); answer.addAll(getTestFunctions(bindingContext, klass.getDeclarations()));
} else if (declaration instanceof JetNamedFunction) { }
else if (declaration instanceof JetNamedFunction) {
JetNamedFunction candidateFunction = (JetNamedFunction) declaration; JetNamedFunction candidateFunction = (JetNamedFunction) declaration;
if (isTest(bindingContext, candidateFunction)) { if (isTest(bindingContext, candidateFunction)) {
answer.add(candidateFunction); answer.add(candidateFunction);
@@ -358,4 +358,10 @@ public final class BindingUtils {
assert primaryConstructor != null : "Traits do not have initialize methods."; assert primaryConstructor != null : "Traits do not have initialize methods.";
return primaryConstructor; return primaryConstructor;
} }
@Nullable
public static SimpleFunctionDescriptor getNullableDescriptorForFunction(@NotNull BindingContext bindingContext,
@NotNull JetNamedFunction function) {
return bindingContext.get(BindingContext.FUNCTION, function);
}
} }
@@ -22,16 +22,9 @@ fun box() : Boolean {
for (i in oneToFive) { for (i in oneToFive) {
sum += i; sum += i;
} }
for (i in oneToFive) {
print(i)
}
if (sum != 10) return false; if (sum != 10) return false;
return true; return true;
} }
fun main() {
box()
}
@@ -22,16 +22,9 @@ fun box() : Boolean {
for (i in oneToFive) { for (i in oneToFive) {
sum += i; sum += i;
} }
for (i in oneToFive) {
print(i)
}
if (sum != 15) return false; if (sum != 15) return false;
return true; return true;
} }
fun main() {
box()
}