1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
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 */
15
16import { FaultID } from '../../Problems';
17
18const LIMITED_STD_SYMBOL_API = [
19  // properties
20  'asyncIterator',
21  'description',
22  'hasInstance',
23  'isConcatSpreadable',
24  'match',
25  'matchAll',
26  'replace',
27  'search',
28  'species',
29  'split',
30  'toPrimitive',
31  'toStringTag',
32  'unscopables',
33
34  // methods
35  'for',
36  'keyFor',
37  'toString',
38  'valueOf'
39];
40
41export const ALLOWED_STD_SYMBOL_API = ['iterator'];
42
43export const LIMITED_STD_OBJECT_API = [
44  // properties
45  '__proto__',
46
47  // methods
48  '__defineGetter__',
49  '__defineSetter__',
50  '__lookupGetter__',
51  '__lookupSetter__',
52  'assign',
53  'create',
54  'defineProperties',
55  'defineProperty',
56  'freeze',
57  // was relaxed but revert
58  'fromEntries',
59  'getOwnPropertyDescriptor',
60  'getOwnPropertyDescriptors',
61  'getOwnPropertySymbols',
62  'getPrototypeOf',
63  'hasOwnProperty',
64  'is',
65  'isExtensible',
66  'isFrozen',
67  'isPrototypeOf',
68  'isSealed',
69  'preventExtensions',
70  'propertyIsEnumerable',
71  'seal',
72  'setPrototypeOf'
73];
74
75export const LIMITED_STD_PROXYHANDLER_API = [
76  // properties
77
78  // methods
79  'apply',
80  'construct',
81  'defineProperty',
82  'deleteProperty',
83  'get',
84  'getOwnPropertyDescriptor',
85  'getPrototypeOf',
86  'has',
87  'isExtensible',
88  'ownKeys',
89  'preventExtensions',
90  'set',
91  'setPrototypeOf'
92];
93
94export const LIMITED_STD_REFLECT_API = [
95  // properties
96
97  // methods
98  'apply',
99  'construct',
100  'defineProperty',
101  'deleteProperty',
102  'getOwnPropertyDescriptor',
103  'getPrototypeOf',
104  'isExtensible',
105  'preventExtensions',
106  'setPrototypeOf'
107];
108
109const LIMITED_STD_FUNCTION_API_APPLY_CALL = [
110  // properties
111
112  // methods
113  'apply',
114  'call'
115];
116
117const LIMITED_STD_FUNCTION_API_BIND = [
118  // properties
119
120  // methods
121  'bind'
122];
123
124export const LIMITED_STD_GLOBAL_API = [
125  // properties
126
127  // methods
128  'eval'
129];
130
131const STD_SYMBOL_ENTRY = [{ api: LIMITED_STD_SYMBOL_API, faultId: FaultID.SymbolType }];
132const STD_OBJECT_ENTRY = [{ api: LIMITED_STD_OBJECT_API, faultId: FaultID.LimitedStdLibApi }];
133const STD_PROXYHANDLER_ENTRY = [{ api: LIMITED_STD_PROXYHANDLER_API, faultId: FaultID.LimitedStdLibApi }];
134const STD_REFLECT_ENTRY = [{ api: LIMITED_STD_REFLECT_API, faultId: FaultID.LimitedStdLibApi }];
135const STD_FUNCTION_ENTRY = [
136  { api: LIMITED_STD_FUNCTION_API_APPLY_CALL, faultId: FaultID.FunctionApplyCall },
137  { api: LIMITED_STD_FUNCTION_API_BIND, faultId: FaultID.FunctionBindError }
138];
139const STD_GLOBAL_ENTRY = [{ api: LIMITED_STD_GLOBAL_API, faultId: FaultID.LimitedStdLibApi }];
140
141export type LimitedStdLibApiEntry = { api: string[]; faultId: FaultID };
142
143export const LIMITED_STD_API = new Map<string | undefined, LimitedStdLibApiEntry[]>([
144  [undefined, STD_GLOBAL_ENTRY],
145  ['Object', STD_OBJECT_ENTRY],
146  ['ObjectConstructor', STD_OBJECT_ENTRY],
147  ['Reflect', STD_REFLECT_ENTRY],
148  ['ProxyHandler', STD_PROXYHANDLER_ENTRY],
149  ['Symbol', STD_SYMBOL_ENTRY],
150  ['SymbolConstructor', STD_SYMBOL_ENTRY],
151  ['Function', STD_FUNCTION_ENTRY],
152  ['CallableFunction', STD_FUNCTION_ENTRY]
153]);
154