-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathpassStyle-helpers.js
212 lines (195 loc) · 6.38 KB
/
passStyle-helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/// <reference types="ses"/>
/** @import {Checker} from './types.js' */
/** @import {PassStyle} from './types.js' */
import { X, q } from '@endo/errors';
import { isFrozenOrIsNonTrapping } from 'ses/nonTrappingShimAdapter.js';
const { isArray } = Array;
const { prototype: functionPrototype } = Function;
const {
getOwnPropertyDescriptor,
getPrototypeOf,
hasOwnProperty: objectHasOwnProperty,
isFrozen,
prototype: objectPrototype,
} = Object;
const { apply } = Reflect;
const { toStringTag: toStringTagSymbol } = Symbol;
const typedArrayPrototype = getPrototypeOf(Uint8Array.prototype);
const typedArrayToStringTagDesc = getOwnPropertyDescriptor(
typedArrayPrototype,
toStringTagSymbol,
);
assert(typedArrayToStringTagDesc);
const getTypedArrayToStringTag = typedArrayToStringTagDesc.get;
assert(typeof getTypedArrayToStringTag === 'function');
export const hasOwnPropertyOf = (obj, prop) =>
apply(objectHasOwnProperty, obj, [prop]);
harden(hasOwnPropertyOf);
// TODO try typing this; `=> val is {} too narrow, implies no properties
export const isObject = val => Object(val) === val;
harden(isObject);
/**
* Duplicates packages/ses/src/make-hardener.js to avoid a dependency.
*
* @param {unknown} object
*/
export const isTypedArray = object => {
// The object must pass a brand check or toStringTag will return undefined.
const tag = apply(getTypedArrayToStringTag, object, []);
return tag !== undefined;
};
harden(isTypedArray);
export const PASS_STYLE = Symbol.for('passStyle');
/**
* For a function to be a valid method, it must not be passable.
* Otherwise, we risk confusing pass-by-copy data carrying
* far functions with attempts at far objects with methods.
*
* TODO HAZARD Because we check this on the way to hardening a remotable,
* we cannot yet check that `func` is hardened. However, without
* doing so, it's inheritance might change after the `PASS_STYLE`
* check below.
*
* @param {any} func
* @returns {boolean}
*/
export const canBeMethod = func =>
typeof func === 'function' && !(PASS_STYLE in func);
harden(canBeMethod);
/**
* Below we have a series of predicate functions and their (curried) assertion
* functions. The semantics of the assertion function is just to assert that
* the corresponding predicate function would have returned true. But it
* reproduces the internal tests so failures can give a better error message.
*
* @type {Checker}
*/
export const assertChecker = (cond, details) => {
assert(cond, details);
return true;
};
harden(assertChecker);
/**
* Returns a template literal tag function to fail the provided Checker with details.
* The name must be short for ergonomic inline use as in:
* ```
* return checkCondition(...) || (!!check && CX(check)`...`);
* ```
*
* @param {Checker} check
*/
export const CX = check => {
const reject = (T, ...subs) => check(false, X(T, ...subs));
return reject;
};
harden(CX);
/**
* Verifies the presence and enumerability of an own data property
* and returns its descriptor.
*
* @param {object} candidate
* @param {string|number|symbol} propName
* @param {boolean} shouldBeEnumerable
* @param {Checker} [check]
* @returns {PropertyDescriptor}
*/
export const getOwnDataDescriptor = (
candidate,
propName,
shouldBeEnumerable,
check,
) => {
const desc = /** @type {PropertyDescriptor} */ (
getOwnPropertyDescriptor(candidate, propName)
);
return (desc !== undefined ||
(!!check && CX(check)`${q(propName)} property expected: ${candidate}`)) &&
(hasOwnPropertyOf(desc, 'value') ||
(!!check &&
CX(
check,
)`${q(propName)} must not be an accessor property: ${candidate}`)) &&
(shouldBeEnumerable
? desc.enumerable ||
(!!check &&
CX(
check,
)`${q(propName)} must be an enumerable property: ${candidate}`)
: !desc.enumerable ||
(!!check &&
CX(
check,
)`${q(propName)} must not be an enumerable property: ${candidate}`))
? desc
: /** @type {PropertyDescriptor} */ (/** @type {unknown} */ (undefined));
};
harden(getOwnDataDescriptor);
/**
* @template {import('./types.js').InterfaceSpec} T
* @param {import('./types.js').PassStyled<any, T>} tagRecord
* @returns {T}
*/
export const getTag = tagRecord => tagRecord[Symbol.toStringTag];
harden(getTag);
export const checkPassStyle = (obj, passStyle, expectedPassStyle, check) => {
return (
passStyle === expectedPassStyle ||
(!!check &&
CX(check)`Expected ${q(expectedPassStyle)}, not ${q(passStyle)}: ${obj}`)
);
};
harden(checkPassStyle);
const makeCheckTagRecord = checkProto => {
/**
* @param {import('./types.js').PassStyled<any, any>} tagRecord
* @param {PassStyle} expectedPassStyle
* @param {Checker} [check]
* @returns {boolean}
*/
const checkTagRecord = (tagRecord, expectedPassStyle, check) => {
return (
(isObject(tagRecord) ||
(!!check &&
CX(check)`A non-object cannot be a tagRecord: ${tagRecord}`)) &&
(isFrozen(tagRecord) ||
(!!check && CX(check)`A tagRecord must be frozen: ${tagRecord}`)) &&
(isFrozenOrIsNonTrapping(tagRecord) ||
(!!check &&
CX(check)`A tagRecord must be non-trapping: ${tagRecord}`)) &&
(!isArray(tagRecord) ||
(!!check && CX(check)`An array cannot be a tagRecord: ${tagRecord}`)) &&
checkPassStyle(
tagRecord,
getOwnDataDescriptor(tagRecord, PASS_STYLE, false, check).value,
expectedPassStyle,
check,
) &&
(typeof getOwnDataDescriptor(tagRecord, Symbol.toStringTag, false, check)
.value === 'string' ||
(!!check &&
CX(
check,
)`A [Symbol.toStringTag]-named property must be a string: ${tagRecord}`)) &&
checkProto(tagRecord, getPrototypeOf(tagRecord), check)
);
};
return harden(checkTagRecord);
};
export const checkTagRecord = makeCheckTagRecord(
(val, proto, check) =>
proto === objectPrototype ||
(!!check &&
check(false, X`A tagRecord must inherit from Object.prototype: ${val}`)),
);
harden(checkTagRecord);
export const checkFunctionTagRecord = makeCheckTagRecord(
(val, proto, check) =>
proto === functionPrototype ||
(proto !== null && getPrototypeOf(proto) === functionPrototype) ||
(!!check &&
check(
false,
X`For functions, a tagRecord must inherit from Function.prototype: ${val}`,
)),
);
harden(checkFunctionTagRecord);