aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Yue Gan <yueg@google.com>2016-04-07 08:02:00 +0000
committerGravatar Lukacs Berki <lberki@google.com>2016-04-07 11:50:49 +0000
commit6c2276a7dd10447128d9ae92d25fdc45579e63fb (patch)
tree2d4d6773f9d3109faf4fc0e8e510e795b92c2c2c /src
parent417dad0f1e0d0ed4ccd5f8e52b49eb79937da49d (diff)
implement list.insert for skylark rule
RELNOTES: implement list.insert for skylark rule -- MOS_MIGRATED_REVID=119243427
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java34
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java12
-rw-r--r--src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java16
3 files changed, 58 insertions, 4 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java b/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java
index 8a2901bbfb..1535cd41e6 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java
@@ -1263,19 +1263,45 @@ public class MethodLibrary {
};
@SkylarkSignature(
+ name = "insert",
+ objectType = MutableList.class,
+ returnType = Runtime.NoneType.class,
+ doc = "Inserts an item at a given position.",
+ mandatoryPositionals = {
+ @Param(name = "self", type = MutableList.class, doc = "This list."),
+ @Param(name = "index", type = Integer.class, doc = "The index of the given position."),
+ @Param(name = "item", type = Object.class, doc = "The item.")
+ },
+ useLocation = true,
+ useEnvironment = true
+ )
+ private static final BuiltinFunction insert =
+ new BuiltinFunction("insert") {
+ public Runtime.NoneType invoke(
+ MutableList<Object> self, Integer index, Object item, Location loc, Environment env)
+ throws EvalException, ConversionException {
+ self.add(clampIndex(index, self.size()), item, loc, env);
+ return Runtime.NONE;
+ }
+ };
+
+ @SkylarkSignature(
name = "extend",
objectType = MutableList.class,
returnType = Runtime.NoneType.class,
doc = "Adds all items to the end of the list.",
mandatoryPositionals = {
@Param(name = "self", type = MutableList.class, doc = "This list."),
- @Param(name = "items", type = SkylarkList.class, doc = "Items to add at the end.")},
+ @Param(name = "items", type = SkylarkList.class, doc = "Items to add at the end.")
+ },
useLocation = true,
- useEnvironment = true)
+ useEnvironment = true
+ )
private static final BuiltinFunction extend =
new BuiltinFunction("extend") {
- public Runtime.NoneType invoke(MutableList<Object> self, SkylarkList<Object> items,
- Location loc, Environment env) throws EvalException, ConversionException {
+ public Runtime.NoneType invoke(
+ MutableList<Object> self, SkylarkList<Object> items, Location loc, Environment env)
+ throws EvalException, ConversionException {
self.addAll(items, loc, env);
return Runtime.NONE;
}
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java
index 102cd31dfd..60d6754766 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/SkylarkList.java
@@ -353,6 +353,18 @@ import javax.annotation.Nullable;
contents.add(element);
}
+ /**
+ * Inserts an item at a given position to the MutableList.
+ * @param index the index of the given position
+ * @param element the element to add
+ * @param loc the Location at which to report any error
+ * @param env the Environment requesting the modification
+ */
+ public void add(int index, E element, Location loc, Environment env) throws EvalException {
+ checkMutable(loc, env);
+ contents.add(index, element);
+ }
+
public void remove(int index, Location loc, Environment env) throws EvalException {
checkMutable(loc, env);
contents.remove(index);
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java b/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java
index ec2d00f8d9..9f1285cdd0 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java
@@ -1531,6 +1531,22 @@ public class MethodLibraryTest extends EvaluationTestCase {
@Test
public void testPyListAppend() throws Exception {
new BuildTest()
+ .setUp("FOO = ['a', 'b']", "FOO.insert(0, 'c')")
+ .testLookup("FOO", MutableList.of(env, "c", "a", "b"))
+ .setUp("FOO.insert(1, 'd')")
+ .testLookup("FOO", MutableList.of(env, "c", "d", "a", "b"))
+ .setUp("FOO.insert(4, 'e')")
+ .testLookup("FOO", MutableList.of(env, "c", "d", "a", "b", "e"))
+ .setUp("FOO.insert(-10, 'f')")
+ .testLookup("FOO", MutableList.of(env, "f", "c", "d", "a", "b", "e"))
+ .setUp("FOO.insert(10, 'g')")
+ .testLookup("FOO", MutableList.of(env, "f", "c", "d", "a", "b", "e", "g"))
+ .testIfErrorContains("Type tuple has no function insert(int)", "(1, 2).insert(3)");
+ }
+
+ @Test
+ public void testPyListInsert() throws Exception {
+ new BuildTest()
.setUp("FOO = ['a', 'b']", "FOO.append('c')")
.testLookup("FOO", MutableList.of(env, "a", "b", "c"))
.testIfErrorContains("Type tuple has no function append(int)", "(1, 2).append(3)");