generated from ryansonshine/typescript-npm-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
361 lines (348 loc) · 9.89 KB
/
index.ts
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* eslint-disable @typescript-eslint/ban-types */
/**
* Delayed initialized objects that support tail-call optimization.
*
* [![npm package][npm-img]][npm-url]
* [![Build Status][build-img]][build-url]
* [![Downloads][downloads-img]][downloads-url]
* [![Issues][issues-img]][issues-url]
* [![Code Coverage][codecov-img]][codecov-url]
* [![Commitizen Friendly][commitizen-img]][commitizen-url]
* [![Semantic Release][semantic-release-img]][semantic-release-url]
*
* [build-img]:https:/Atry/tail-call-proxy/actions/workflows/release.yml/badge.svg
* [build-url]:https:/Atry/tail-call-proxy/actions/workflows/release.yml
* [downloads-img]:https://img.shields.io/npm/dt/tail-call-proxy
* [downloads-url]:https://www.npmtrends.com/tail-call-proxy
* [npm-img]:https://img.shields.io/npm/v/tail-call-proxy
* [npm-url]:https://www.npmjs.com/package/tail-call-proxy
* [issues-img]:https://img.shields.io/github/issues/Atry/tail-call-proxy
* [issues-url]:https:/Atry/tail-call-proxy/issues
* [codecov-img]:https://codecov.io/gh/Atry/tail-call-proxy/branch/main/graph/badge.svg
* [codecov-url]:https://codecov.io/gh/Atry/tail-call-proxy
* [semantic-release-img]:https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
* [semantic-release-url]:https:/semantic-release/semantic-release
* [commitizen-img]:https://img.shields.io/badge/commitizen-friendly-brightgreen.svg
* [commitizen-url]:http://commitizen.github.io/cz-cli/
*
* @module
*/
import {
getTarget,
AllowGetTarget,
Mapped,
DefaultToPrimitive,
DefaultToStringTag,
TargetAsThis,
} from 'proxy-handler-decorators';
import { DefaultProxyHandler } from 'default-proxy-handler';
type ProxyTarget<T extends object> = {
tailCall?: () => T;
tail?: ProxyTarget<T>;
result?: T;
error?: any;
};
function getResult<T extends object>(target: ProxyTarget<T>): T {
if ('result' in target) {
return target.result as T;
} else if ('error' in target) {
throw target.error;
} else {
run(target);
return getResult(target);
}
}
@AllowGetTarget
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
@Mapped(getResult)
@DefaultToPrimitive
@DefaultToStringTag
@TargetAsThis
class TailCallProxyHandler<T extends object> extends DefaultProxyHandler<
T | ProxyTarget<T>
> {}
let parasiticQueue: ProxyTarget<any>[] = [];
let isRunning = false;
function step<T extends object>(target: ProxyTarget<T>): void {
const { tailCall } = target;
let proxyOrResult: T;
if (tailCall !== undefined) {
try {
try {
proxyOrResult = tailCall();
} finally {
delete target.tailCall;
}
} catch (error) {
target.error = error;
return;
}
if (proxyOrResult == null) {
target.result = proxyOrResult;
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
const tail: ProxyTarget<T> | undefined = getTarget(proxyOrResult);
if (tail === undefined) {
target.result = proxyOrResult;
} else {
target.tail = tail;
}
}
}
}
function resolveWith<T extends object>(
headTarget: ProxyTarget<T>,
lastTarget: ProxyTarget<T>
): void {
if ('error' in lastTarget) {
headTarget.error = lastTarget.error;
} else {
headTarget.result = lastTarget.result;
}
}
function resolve<T extends object>(headTarget: ProxyTarget<T>): void {
step(headTarget);
if (headTarget.tail !== undefined) {
let target = headTarget.tail;
delete headTarget.tail;
for (;;) {
step(target);
const { tail } = target;
const queue = parasiticQueue;
if (queue.length !== 0) {
parasiticQueue = [];
for (const parasiticTarget of queue) {
if (parasiticTarget !== tail) {
resolve(parasiticTarget);
}
}
}
if (tail !== undefined) {
target = tail;
} else {
resolveWith(headTarget, target);
return;
}
}
}
}
function run<T extends object>(target: ProxyTarget<T>): void {
if (isRunning) {
resolve(target);
}
isRunning = true;
try {
resolve(target);
} finally {
isRunning = false;
}
}
const LAZY_PROXY_HANDLER = new TailCallProxyHandler();
/**
* Returns an proxy object backed by `tailCall`, which will be lazily created
* at the first time its properties or methods are used.
*
* `lazy` can eliminate tail calls, preventing stack overflow errors in tail
* recursive functions or mutual recursive functions.
*
* @param tailCall the function to create the underlying object
*
* @example
*
* The initializer passed to `lazy` should not be called until the first time
* `lazyObject.hello` is accessed. When `lazyObject.hello` is accessed more than
* once, the second access would not trigger the initializer again.
*
* ```typescript doctest
* import { lazy } from 'tail-call-proxy';
*
* let counter = 0;
* const lazyObject = lazy(() => {
* counter++;
* return { hello: 'world' };
* });
* expect(counter).toBe(0);
*
* expect(lazyObject.hello).toBe('world');
* expect(counter).toBe(1);
*
* expect(lazyObject.hello).toBe('world');
* expect(counter).toBe(1);
* ```
*
* @example
*
* Note that errors thrown in the initializer will be delayed as well.
*
* ```typescript doctest
* import { lazy } from 'tail-call-proxy';
*
* let counter = 0;
* const lazyError: Record<string, unknown> = lazy(() => {
* counter++;
* throw new Error();
* });
*
* // No error is thrown, given that the underlying object have not been created
* // yet.
* expect(counter).toBe(0);
*
* expect(() => lazyError.toString()).toThrow();
* expect(counter).toBe(1);
*
* expect(() => lazyError.toLocaleString()).toThrow();
* expect(counter).toBe(1);
* ```
*
* @example
*
* The following mutual recursive functions would result in stack overflow:
*
* ```typescript doctest
* function isEven(n: number): Boolean {
* if (n === 0) {
* return new Boolean(true);
* }
* return isOdd(n - 1);
* }
*
* function isOdd(n: number): Boolean {
* if (n === 0) {
* return new Boolean(false);
* }
* return isEven(n - 1);
* }
*
* expect(() => isOdd(1000000)).toThrow();
* ```
*
* However, if you replace `return xxx` with `return lazy(() => xxx)`, it will
* use a constant size of stack memory and avoid the stack overflow.
*
* ```typescript doctest
* import { lazy } from 'tail-call-proxy';
* function isEven(n: number): Boolean {
* if (n === 0) {
* return new Boolean(true);
* }
* return lazy(() => isOdd(n - 1));
* }
*
* function isOdd(n: number): Boolean {
* if (n === 0) {
* return new Boolean(false);
* }
* return lazy(() => isEven(n - 1));
* }
*
* expect(isOdd(1000000).valueOf()).toBe(false);
* ```
*/
export function lazy<T extends object>(tailCall: () => T): T {
return new Proxy({ tailCall }, LAZY_PROXY_HANDLER) as T;
}
/**
* Performs a tail call as soon as possible.
*
* `parasitic` returns either exactly the object returned by `tailCall`, or a
* proxy object backed by the object returned by `tailCall`, if there are any
* previously started pending tail calls. In the latter case, the underlying
* object will be created after all the previous tail calls are finished.
*
* @param tailCall the function to create the underlying object
*
* @example
*
* Unlike {@link lazy}, `parasitic` performs the initialization as soon as
* possible:
*
* ```typescript doctest
* import { parasitic } from 'tail-call-proxy';
*
* let counter = 0;
* const parasiticObject = parasitic(() => {
* counter++;
* return { hello: 'world' };
* });
* expect(counter).toBe(1);
*
* expect(parasiticObject.hello).toBe('world');
* expect(counter).toBe(1);
*
* expect(parasiticObject.hello).toBe('world');
* expect(counter).toBe(1);
* ```
*
* @example
*
* `parasitic` is useful when you need tail call optimization while you don't
* need the lazy evaluation. It can be used together with {@link lazy}
* alternately.
*
* ```typescript doctest
* import { lazy, parasitic } from 'tail-call-proxy';
*
* let isEvenCounter = 0;
* const trueObject = new Boolean(true);
* function isEven(n: number): Boolean {
* isEvenCounter++;
* if (n === 0) {
* return trueObject;
* }
* return lazy(() => isOdd(n - 1));
* };
*
* let isOddCounter = 0;
* const falseObject = new Boolean(false);
* function isOdd(n: number): Boolean {
* isOddCounter++;
* if (n === 0) {
* return falseObject;
* }
* return parasitic(() => isEven(n - 1));
* };
*
* try {
* // `isEven` is called, but `lazy(() => isOdd(n - 1))` does not trigger
* // `isOdd` immediately.
* const is1000000Even = isEven(1000000);
* expect(isOddCounter).toBe(0);
* expect(isEvenCounter).toBe(1);
*
* // `valueOf` triggers the rest of the recursion.
* expect(is1000000Even.valueOf()).toBe(true);
* expect(isOddCounter).toBe(500000);
* expect(isEvenCounter).toBe(500001);
*
* // `is1000000Even` is a lazy proxy backed by `trueObject`, not the exactly
* // same object of `trueObject`.
* expect(is1000000Even).not.toStrictEqual(trueObject);
* expect(is1000000Even).toEqual(trueObject);
* } finally {
* isEvenCounter = 0;
* isOddCounter = 0;
* }
*
* // `isOdd` is called, in which `parasitic(() => isEven(n - 1))` triggers the
* // rest of the recursion immediately.
* const is1000000Odd = isOdd(1000000);
* expect(isOddCounter).toBe(500001);
* expect(isEvenCounter).toBe(500000);
* expect(is1000000Odd.valueOf()).toBe(false);
* expect(isOddCounter).toBe(500001);
* expect(isEvenCounter).toBe(500000);
*
* // `is1000000Odd` is exactly the same object of `falseObject`, not a lazy
* // proxy.
* expect(is1000000Odd).toStrictEqual(falseObject);
* ```
*/
export function parasitic<T extends object>(tailCall: () => T): T {
const target: ProxyTarget<T> = { tailCall };
if (isRunning) {
parasiticQueue.push(target);
return new Proxy(target, LAZY_PROXY_HANDLER) as T;
} else {
return getResult(target);
}
}