-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathexo-makers.js
197 lines (181 loc) · 5.67 KB
/
exo-makers.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
/* global globalThis */
/// <reference types="ses"/>
import { makeEnvironmentCaptor } from '@endo/env-options';
import { objectMap } from '@endo/patterns';
import { defendPrototype, defendPrototypeKit } from './exo-tools.js';
const { create, seal, freeze, defineProperty } = Object;
const { getEnvironmentOption } = makeEnvironmentCaptor(globalThis);
const DEBUG = getEnvironmentOption('DEBUG', '');
// Turn on to give each exo instance its own toStringTag value.
const LABEL_INSTANCES = DEBUG.split(',').includes('label-instances');
const makeSelf = (proto, instanceCount) => {
const self = create(proto);
if (LABEL_INSTANCES) {
defineProperty(self, Symbol.toStringTag, {
value: `${proto[Symbol.toStringTag]}#${instanceCount}`,
writable: false,
enumerable: false,
configurable: false,
});
}
return harden(self);
};
const emptyRecord = harden({});
/**
* When calling `defineDurableKind` and
* its siblings, used as the `init` function argument to indicate that the
* state record of the (virtual/durable) instances of the kind/exoClass
* should be empty, and that the returned maker function should have zero
* parameters.
*
* @returns {{}}
*/
export const initEmpty = () => emptyRecord;
/**
* @typedef {import('./exo-tools.js').FacetName} FacetName
* @typedef {import('./exo-tools.js').Methods} Methods
*/
/**
* @template [S = any]
* @template {Methods} [M = any]
* @typedef {import('./exo-tools.js').ClassContext} ClassContext
*/
/**
* @template [S = any]
* @template {Record<FacetName, Methods>} [F = any]
* @typedef {import('./exo-tools.js').KitContext} KitContext
*/
/**
* @typedef {{[name: string]: import('@endo/patterns').Pattern}} StateShape
* It looks like a copyRecord pattern, but the interpretation is different.
* Each property is distinct, is checked and changed separately.
*/
/**
* @template C
* @typedef {object} FarClassOptions
* @property {(context: C) => void} [finish]
* @property {StateShape} [stateShape]
*/
/**
* @template {(...args: any[]) => any} I init function
* @template {Methods} M methods
* @param {string} tag
* @param {any} interfaceGuard
* @param {I} init
* @param {M & ThisType<{ self: M, state: ReturnType<I> }>} methods
* @param {FarClassOptions<ClassContext<ReturnType<I>, M>>} [options]
* @returns {(...args: Parameters<I>) => (M & import('@endo/eventual-send').RemotableBrand<{}, M>)}
*/
export const defineExoClass = (tag, interfaceGuard, init, methods, options) => {
harden(methods);
const { finish = undefined } = options || {};
/** @type {WeakMap<M,ClassContext<ReturnType<I>, M>>} */
const contextMap = new WeakMap();
const proto = defendPrototype(
tag,
self => /** @type {any} */ (contextMap.get(self)),
methods,
true,
interfaceGuard,
);
let instanceCount = 0;
/**
* @param {Parameters<I>} args
*/
const makeInstance = (...args) => {
// Be careful not to freeze the state record
const state = seal(init(...args));
instanceCount += 1;
/** @type {M} */
const self = makeSelf(proto, instanceCount);
// Be careful not to freeze the state record
/** @type {ClassContext<ReturnType<I>,M>} */
const context = freeze({ state, self });
contextMap.set(self, context);
if (finish) {
finish(context);
}
return /** @type {M & import('@endo/eventual-send').RemotableBrand<{}, M>} */ (
self
);
};
return harden(makeInstance);
};
harden(defineExoClass);
/**
* @template {(...args: any[]) => any} I init function
* @template {Record<FacetName, Methods>} F facet methods
* @param {string} tag
* @param {any} interfaceGuardKit
* @param {I} init
* @param {F & ThisType<{ facets: F, state: ReturnType<I> }> } methodsKit
* @param {FarClassOptions<KitContext<ReturnType<I>,F>>} [options]
* @returns {(...args: Parameters<I>) => F}
*/
export const defineExoClassKit = (
tag,
interfaceGuardKit,
init,
methodsKit,
options,
) => {
harden(methodsKit);
const { finish = undefined } = options || {};
const contextMapKit = objectMap(methodsKit, () => new WeakMap());
const getContextKit = objectMap(
methodsKit,
(_v, name) => facet => contextMapKit[name].get(facet),
);
const prototypeKit = defendPrototypeKit(
tag,
getContextKit,
methodsKit,
true,
interfaceGuardKit,
);
let instanceCount = 0;
/**
* @param {Parameters<I>} args
*/
const makeInstanceKit = (...args) => {
// Be careful not to freeze the state record
const state = seal(init(...args));
// Don't freeze context until we add facets
/** @type {KitContext<ReturnType<I>,F>} */
const context = { state, facets: {} };
instanceCount += 1;
const facets = objectMap(prototypeKit, (proto, facetName) => {
const self = makeSelf(proto, instanceCount);
contextMapKit[facetName].set(self, context);
return self;
});
context.facets = facets;
// Be careful not to freeze the state record
freeze(context);
if (finish) {
finish(context);
}
return context.facets;
};
return harden(makeInstanceKit);
};
harden(defineExoClassKit);
/**
* @template {Methods} T
* @param {string} tag
* @param {import('@endo/patterns').InterfaceGuard<{ [M in keyof T]: import('@endo/patterns').MethodGuard }> | undefined} interfaceGuard CAVEAT: static typing does not yet support `callWhen` transformation
* @param {T} methods
* @param {FarClassOptions<ClassContext<{},T>>} [options]
* @returns {T & import('@endo/eventual-send').RemotableBrand<{}, T>}
*/
export const makeExo = (tag, interfaceGuard, methods, options = undefined) => {
const makeInstance = defineExoClass(
tag,
interfaceGuard,
initEmpty,
methods,
options,
);
return makeInstance();
};
harden(makeExo);