blob: 6cf111e5f692106572ca3d5cb301c9fbb792129f [file] [log] [blame]
cparsons5d85e752018-06-26 13:47:28 -07001// Copyright 2018 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.devtools.build.skydoc.fakebuildapi;
16
cparsons858057c2019-09-17 13:16:24 -070017import com.google.common.collect.ImmutableCollection;
18import com.google.common.collect.ImmutableList;
gregce59f5cba2020-07-22 12:18:43 -070019import com.google.devtools.build.lib.starlarkbuildapi.StarlarkNativeModuleApi;
cparsons858057c2019-09-17 13:16:24 -070020import javax.annotation.Nullable;
adonovan450c7ad2020-09-14 13:00:21 -070021import net.starlark.java.eval.Dict;
22import net.starlark.java.eval.EvalException;
23import net.starlark.java.eval.NoneType;
24import net.starlark.java.eval.Printer;
25import net.starlark.java.eval.Sequence;
26import net.starlark.java.eval.Starlark;
27import net.starlark.java.eval.StarlarkCallable;
adonovane7df6822020-10-08 08:12:51 -070028import net.starlark.java.eval.StarlarkInt;
adonovan450c7ad2020-09-14 13:00:21 -070029import net.starlark.java.eval.StarlarkList;
30import net.starlark.java.eval.StarlarkThread;
adonovan267bdaa2020-11-04 11:32:24 -080031import net.starlark.java.eval.Structure;
adonovan450c7ad2020-09-14 13:00:21 -070032import net.starlark.java.syntax.Location;
cparsons5d85e752018-06-26 13:47:28 -070033
gregced281df72020-05-11 12:27:06 -070034/** Fake implementation of {@link StarlarkNativeModuleApi}. */
adonovan267bdaa2020-11-04 11:32:24 -080035public class FakeStarlarkNativeModuleApi implements StarlarkNativeModuleApi, Structure {
cparsons5d85e752018-06-26 13:47:28 -070036
37 @Override
Googler4871cb02019-11-12 17:16:42 -080038 public Sequence<?> glob(
39 Sequence<?> include,
40 Sequence<?> exclude,
adonovane7df6822020-10-08 08:12:51 -070041 StarlarkInt excludeDirectories,
laurentlb9bc841e2019-05-28 05:59:35 -070042 Object allowEmpty,
Googlera3421e22019-09-26 06:48:32 -070043 StarlarkThread thread)
cparsons5d85e752018-06-26 13:47:28 -070044 throws EvalException, InterruptedException {
Googler92578702019-11-21 12:19:31 -080045 return StarlarkList.of(thread.mutability());
cparsons5d85e752018-06-26 13:47:28 -070046 }
47
48 @Override
adonovan7891d3b2020-01-22 12:40:50 -080049 public Object existingRule(String name, StarlarkThread thread) {
cparsons5d85e752018-06-26 13:47:28 -070050 return null;
51 }
52
53 @Override
adonovan7891d3b2020-01-22 12:40:50 -080054 public Dict<String, Dict<String, Object>> existingRules(StarlarkThread thread) {
Googler6456fcb2019-11-22 13:05:42 -080055 return Dict.of(thread.mutability());
cparsons5d85e752018-06-26 13:47:28 -070056 }
57
58 @Override
Googlera3421e22019-09-26 06:48:32 -070059 public NoneType packageGroup(
adonovan7891d3b2020-01-22 12:40:50 -080060 String name, Sequence<?> packages, Sequence<?> includes, StarlarkThread thread) {
cparsons5d85e752018-06-26 13:47:28 -070061 return null;
62 }
63
64 @Override
Googlera3421e22019-09-26 06:48:32 -070065 public NoneType exportsFiles(
adonovan7891d3b2020-01-22 12:40:50 -080066 Sequence<?> srcs, Object visibility, Object licenses, StarlarkThread thread) {
cparsons5d85e752018-06-26 13:47:28 -070067 return null;
68 }
69
70 @Override
adonovan7891d3b2020-01-22 12:40:50 -080071 public String packageName(StarlarkThread thread) {
cparsons5d85e752018-06-26 13:47:28 -070072 return "";
73 }
74
75 @Override
adonovan7891d3b2020-01-22 12:40:50 -080076 public String repositoryName(StarlarkThread thread) {
cparsons5d85e752018-06-26 13:47:28 -070077 return "";
78 }
cparsons858057c2019-09-17 13:16:24 -070079
80 @Nullable
81 @Override
82 public Object getValue(String name) throws EvalException {
83 // Bazel's notion of the global "native" isn't fully exposed via public interfaces, for example,
84 // as far as native rules are concerned. Returning None on all unsupported invocations of
85 // native.[func_name]() is the safest "best effort" approach to implementing a fake for
86 // "native".
Googler9eca6d52019-11-13 06:26:47 -080087 return new StarlarkCallable() {
88 @Override
adonovan7891d3b2020-01-22 12:40:50 -080089 public Object fastcall(StarlarkThread thread, Object[] positional, Object[] named) {
Googler9eca6d52019-11-13 06:26:47 -080090 return Starlark.NONE;
91 }
92
93 @Override
94 public String getName() {
95 return name;
96 }
97
98 @Override
99 public Location getLocation() {
100 return Location.BUILTIN;
101 }
102
103 @Override
Googler34f70582019-11-25 12:27:34 -0800104 public void repr(Printer printer) {
Googler9eca6d52019-11-13 06:26:47 -0800105 printer.append("<faked no-op function " + name + ">");
106 }
107 };
cparsons858057c2019-09-17 13:16:24 -0700108 }
109
110 @Override
adonovan11e5fab2019-12-06 12:03:49 -0800111 public ImmutableCollection<String> getFieldNames() {
cparsons858057c2019-09-17 13:16:24 -0700112 return ImmutableList.of();
113 }
114
115 @Nullable
116 @Override
117 public String getErrorMessageForUnknownField(String field) {
118 return "";
119 }
cparsons5d85e752018-06-26 13:47:28 -0700120}