mirror of
https://github.com/cincheo/jsweet.git
synced 2026-02-07 14:06:35 +00:00
more collections support
This commit is contained in:
parent
b73479741b
commit
31427bf699
@ -50,6 +50,7 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.Vector;
|
||||
import java.util.function.BooleanSupplier;
|
||||
import java.util.function.DoubleBinaryOperator;
|
||||
@ -226,6 +227,9 @@ public class Java2TypeScriptAdapter extends AbstractPrinterAdapter {
|
||||
typesMapping.put(List.class.getName(), "Array");
|
||||
typesMapping.put(ArrayList.class.getName(), "Array");
|
||||
typesMapping.put(Collection.class.getName(), "Array");
|
||||
typesMapping.put(Set.class.getName(), "Array");
|
||||
typesMapping.put(HashSet.class.getName(), "Array");
|
||||
typesMapping.put(TreeSet.class.getName(), "Array");
|
||||
typesMapping.put(Vector.class.getName(), "Array");
|
||||
typesMapping.put(Enumeration.class.getName(), "any");
|
||||
typesMapping.put(Iterator.class.getName(), "any");
|
||||
@ -1080,17 +1084,28 @@ public class Java2TypeScriptAdapter extends AbstractPrinterAdapter {
|
||||
case "java.util.Vector":
|
||||
case "java.util.Set":
|
||||
case "java.util.HashSet":
|
||||
case "java.util.TreeSet":
|
||||
if (!context.options.isUseJavaApis()) {
|
||||
switch (targetMethodName) {
|
||||
case "add":
|
||||
printMacroName(targetMethodName);
|
||||
if (invocation.args.size() == 2) {
|
||||
getPrinter().print(fieldAccess.getExpression()).print(".splice(")
|
||||
.print(invocation.args.head).print(", 0, ").print(invocation.args.tail.head)
|
||||
switch (targetClassName) {
|
||||
case "java.util.Set":
|
||||
case "java.util.HashSet":
|
||||
case "java.util.TreeSet":
|
||||
getPrinter().print("((s, e) => { if(s.indexOf(e)==-1) s.push(e); })(")
|
||||
.print(fieldAccess.getExpression()).print(", ").print(invocation.args.head)
|
||||
.print(")");
|
||||
} else {
|
||||
getPrinter().print(fieldAccess.getExpression()).print(".push(")
|
||||
.printArgList(invocation.args).print(")");
|
||||
break;
|
||||
default:
|
||||
if (invocation.args.size() == 2) {
|
||||
getPrinter().print(fieldAccess.getExpression()).print(".splice(")
|
||||
.print(invocation.args.head).print(", 0, ").print(invocation.args.tail.head)
|
||||
.print(")");
|
||||
} else {
|
||||
getPrinter().print(fieldAccess.getExpression()).print(".push(")
|
||||
.printArgList(invocation.args).print(")");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
case "addAll":
|
||||
@ -1198,10 +1213,31 @@ public class Java2TypeScriptAdapter extends AbstractPrinterAdapter {
|
||||
printMacroName(targetMethodName);
|
||||
getPrinter().print("[]");
|
||||
return true;
|
||||
case "emptySet":
|
||||
printMacroName(targetMethodName);
|
||||
getPrinter().print("[]");
|
||||
return true;
|
||||
case "emptyMap":
|
||||
printMacroName(targetMethodName);
|
||||
getPrinter().print("{}");
|
||||
return true;
|
||||
case "unmodifiableList":
|
||||
printMacroName(targetMethodName);
|
||||
getPrinter().printArgList(invocation.args).print(".slice(0)");
|
||||
return true;
|
||||
case "singleton":
|
||||
printMacroName(targetMethodName);
|
||||
getPrinter().print("[").print(invocation.args.head).print("]");
|
||||
return true;
|
||||
case "singletonList":
|
||||
printMacroName(targetMethodName);
|
||||
getPrinter().print("[").print(invocation.args.head).print("]");
|
||||
return true;
|
||||
case "singletonMap":
|
||||
printMacroName(targetMethodName);
|
||||
getPrinter().print("{ ").print(invocation.args.head).print(": ")
|
||||
.print(invocation.args.tail.head).print(" }");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
@ -17,7 +17,7 @@ public class NativeStructuresTests extends AbstractTest {
|
||||
transpiler.setUseJavaApis(false);
|
||||
eval((logHandler, result) -> {
|
||||
Assert.assertEquals("There should be no errors", 0, logHandler.reportedProblems.size());
|
||||
assertEquals("1,a,1,b,3,4,d,a,d,0,0,0,a,a,2,a,true,false,3,c,c,a,b,c,a,b,c,b,1,c,b,a,b,c,a,0,true,true,it",
|
||||
assertEquals("1,a,1,b,3,4,d,a,d,0,0,0,a,a,2,a,true,false,3,c,c,a,b,c,a,b,c,b,1,c,b,a,b,c,a,0,true,true,it,true,1",
|
||||
result.<String> get("trace"));
|
||||
}, getSourceFile(Collections.class));
|
||||
transpiler.setUseJavaApis(true);
|
||||
@ -38,7 +38,7 @@ public class NativeStructuresTests extends AbstractTest {
|
||||
transpiler.setUseJavaApis(false);
|
||||
eval((logHandler, result) -> {
|
||||
Assert.assertEquals("There should be no errors", 0, logHandler.reportedProblems.size());
|
||||
assertEquals("2,a,true,[1,2],[a,b],1,true", result.<String> get("trace"));
|
||||
assertEquals("2,a,true,[1,2],[a,b],1,true,1", result.<String> get("trace"));
|
||||
}, getSourceFile(Maps.class));
|
||||
transpiler.setUseJavaApis(true);
|
||||
}
|
||||
|
||||
@ -9,6 +9,7 @@ import java.util.Comparator;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.Vector;
|
||||
|
||||
import def.js.Array;
|
||||
@ -127,7 +128,7 @@ public class Collections implements Cloneable, Serializable {
|
||||
trace.push(newArray[0]);
|
||||
trace.push(newArray[1]);
|
||||
trace.push(newArray[2]);
|
||||
|
||||
|
||||
l = new Vector<>(5);
|
||||
|
||||
trace.push("" + l.size());
|
||||
@ -144,7 +145,15 @@ public class Collections implements Cloneable, Serializable {
|
||||
for (String s : l) {
|
||||
trace.push(s);
|
||||
}
|
||||
|
||||
|
||||
Set<String> s = java.util.Collections.emptySet();
|
||||
trace.push("" + s.isEmpty());
|
||||
|
||||
s.add("test");
|
||||
s.add("test");
|
||||
|
||||
trace.push("" + s.size());
|
||||
|
||||
$export("trace", trace.join(","));
|
||||
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ package source.nativestructures;
|
||||
|
||||
import static jsweet.util.Globals.$export;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@ -34,6 +35,8 @@ public class Maps {
|
||||
trace.push("" + m.size());
|
||||
trace.push("" + (m.get("1") == null));
|
||||
|
||||
trace.push(Collections.singletonMap("a", "1").get("a"));
|
||||
|
||||
$export("trace", trace.join(","));
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user