work on java.util.ArrayList

This commit is contained in:
Pavel Talanov
2011-12-08 16:46:17 +04:00
parent c9e321d8e4
commit 4e80605b8b
7 changed files with 103 additions and 32 deletions
@@ -28,12 +28,23 @@ public final class StandardClasses {
bindArray(standardClasses, standardLibrary);
ClassDescriptor iteratorClass = (ClassDescriptor)
standardLibrary.getLibraryScope().getClassifier("Iterator");
assert iteratorClass != null;
bindIterator(standardClasses, iteratorClass);
declareJavaArrayList(standardClasses);
return standardClasses;
}
private static void bindIterator(StandardClasses standardClasses,
ClassDescriptor iteratorClass) {
private static void declareJavaArrayList(@NotNull StandardClasses standardClasses) {
String arrayListFQName = "java.util.ArrayList";
standardClasses.declareStandardTopLevelObject(arrayListFQName, "ArrayList");
standardClasses.declareStandardMethodOrProperty(arrayListFQName, "size", "size");
standardClasses.declareStandardMethodOrProperty(arrayListFQName, "<init>", "ArrayList");
}
//TODO: more generic ways to declare standard classes
private static void bindIterator(@NotNull StandardClasses standardClasses,
@NotNull ClassDescriptor iteratorClass) {
standardClasses.declareStandardTopLevelObject(iteratorClass, "ArrayIterator");
FunctionDescriptor nextFunction =
getFunctionByName(iteratorClass.getDefaultType().getMemberScope(), "next");
@@ -101,7 +101,10 @@ public final class TranslationContext {
if (dynamicContext.isDeclared(descriptor)) {
return dynamicContext.getLocalName(descriptor);
}
return staticContext.getGlobalName(descriptor);
if (staticContext.isDeclared(descriptor)) {
return staticContext.getGlobalName(descriptor);
}
throw new AssertionError("Undefined name in this scope: " + descriptor.getName());
}
@NotNull
@@ -50,10 +50,6 @@ public class ReferenceTranslator extends AbstractTranslator {
@NotNull
public JsExpression translate() {
if (!context().isDeclared(referencedDescriptor)) {
throw new AssertionError("Undefined name in this scope: " + referencedDescriptor.getName());
}
JsName referencedName = context().getNameForDescriptor(referencedDescriptor);
JsExpression implicitReceiver = getImplicitReceiver(context(), referencedDescriptor);
@@ -0,0 +1,21 @@
package org.jetbrains.k2js.test;
import org.junit.Test;
/**
* @author Talanov Pavel
*/
public final class ArrayListTest extends JavaClassesTest {
final private static String MAIN = "arrayList/";
@Override
protected String mainDirectory() {
return MAIN;
}
@Test
public void emptyList() throws Exception {
testFooBoxIsTrue("emptyList.kt");
}
}
@@ -0,0 +1,13 @@
package org.jetbrains.k2js.test;
/**
* @author Talanov Pavel
*/
public abstract class JavaClassesTest extends TranslationTest {
private final String SUITE = "java/";
@Override
protected String suiteDirectoryName() {
return SUITE;
}
}
@@ -0,0 +1,8 @@
namespace foo
import java.util.ArrayList;
fun box() : Boolean {
val a = ArrayList<Int>();
return a.size == 0;
}
+44 -25
View File
@@ -46,7 +46,6 @@ var Class = (function () {
parent.subclasses.push(klass);
}
klass.addMethods(
{
get_class:function () {
@@ -483,30 +482,50 @@ Kotlin.Array = Class.create({
}
});
//Kotlin.Array = Class.create({
// initialize:function (len) {
// this.array = [];
// this.size = 0
// },
// get:function (index) {
// if ((index < 0) || (index > this.size)) {
// throw Kotlin.Exceptions.IndexOutOfBounds;
// }
// return (this.array)[index];
// },
// set:function (index, value) {
// if ((index < 0) || (index > this.size)) {
// throw Kotlin.Exceptions.IndexOutOfBounds;
// }
// (this.array)[index] = value;
// },
// size:function () {
// return this.size;
// },
// iterator:function () {
// return new Kotlin.ArrayIterator(this);
// }
//});
Kotlin.ArrayList = Class.create({
initialize:function (len) {
this.array = [];
this.size = 0;
},
get:function (index) {
if ((index < 0) || (index > this.size)) {
throw Kotlin.Exceptions.IndexOutOfBounds;
}
return (this.array)[index];
},
set:function (index, value) {
if ((index < 0) || (index > this.size)) {
throw Kotlin.Exceptions.IndexOutOfBounds;
}
(this.array)[index] = value;
},
size:function () {
return this.size;
},
iterator:function () {
return new Kotlin.ArrayIterator(this);
},
isEmpty:function () {
return (this.size == 0);
},
add:function (element) {
this.array[size++] = element;
},
addAll:function (collection) {
var it = collection.iterator();
while (it.hasNext()) {
this.add(it.next());
}
},
remove:function (index) {
for (var i = index; i < this.size - 1; ++i) {
this.array[i] = this.array[i + 1];
}
this.size--;
}
});
Kotlin.ArrayIterator = Class.create({
initialize:function (array) {