@cldmv/slothlet 1.0.3 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +913 -73
- package/dist/lib/engine/README.md +21 -0
- package/dist/lib/engine/slothlet_child.mjs +58 -0
- package/dist/lib/engine/slothlet_engine.mjs +371 -0
- package/dist/lib/engine/slothlet_esm.mjs +229 -0
- package/dist/lib/engine/slothlet_helpers.mjs +454 -0
- package/dist/lib/engine/slothlet_worker.mjs +148 -0
- package/dist/lib/helpers/resolve-from-caller.mjs +141 -0
- package/dist/lib/helpers/sanitize.mjs +265 -0
- package/dist/lib/modes/slothlet_eager.mjs +80 -0
- package/dist/lib/modes/slothlet_lazy.mjs +342 -0
- package/dist/lib/runtime/runtime.mjs +249 -0
- package/dist/slothlet.mjs +1097 -0
- package/index.cjs +81 -0
- package/index.mjs +76 -0
- package/package.json +132 -20
- package/types/dist/lib/engine/slothlet_child.d.mts +2 -0
- package/types/dist/lib/engine/slothlet_child.d.mts.map +1 -0
- package/types/dist/lib/engine/slothlet_engine.d.mts +31 -0
- package/types/dist/lib/engine/slothlet_engine.d.mts.map +1 -0
- package/types/{src/lib → dist/lib/engine}/slothlet_esm.d.mts +1 -0
- package/types/dist/lib/engine/slothlet_esm.d.mts.map +1 -0
- package/types/{src/lib → dist/lib/engine}/slothlet_helpers.d.mts +2 -2
- package/types/dist/lib/engine/slothlet_helpers.d.mts.map +1 -0
- package/types/dist/lib/engine/slothlet_worker.d.mts +2 -0
- package/types/dist/lib/engine/slothlet_worker.d.mts.map +1 -0
- package/types/dist/lib/helpers/resolve-from-caller.d.mts +149 -0
- package/types/dist/lib/helpers/resolve-from-caller.d.mts.map +1 -0
- package/types/dist/lib/helpers/sanitize.d.mts +79 -0
- package/types/dist/lib/helpers/sanitize.d.mts.map +1 -0
- package/types/dist/lib/modes/slothlet_eager.d.mts +66 -0
- package/types/dist/lib/modes/slothlet_eager.d.mts.map +1 -0
- package/types/dist/lib/modes/slothlet_lazy.d.mts +32 -0
- package/types/dist/lib/modes/slothlet_lazy.d.mts.map +1 -0
- package/types/dist/lib/runtime/runtime.d.mts +49 -0
- package/types/dist/lib/runtime/runtime.d.mts.map +1 -0
- package/types/dist/slothlet.d.mts +124 -0
- package/types/dist/slothlet.d.mts.map +1 -0
- package/types/index.d.mts +23 -0
- package/slothlet.mjs +0 -1248
- package/types/debug-slothlet.d.mts +0 -1
- package/types/eslint.config.d.mts +0 -2
- package/types/jest.config.d.mts +0 -6
- package/types/slothlet.d.mts +0 -189
- package/types/src/lib/slothlet_child.d.mts +0 -1
- package/types/src/lib/slothlet_engine.d.mts +0 -6
- package/types/src/lib/slothlet_worker.d.mts +0 -1
- package/types/vitest.config.d.ts +0 -2
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2025 CLDMV/Shinrai
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
import fs from "fs/promises";
|
|
19
|
+
import path from "path";
|
|
20
|
+
import { runWithCtx } from "@cldmv/slothlet/runtime";
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
export async function create(dir, rootLevel = true, maxDepth = Infinity, currentDepth = 0) {
|
|
24
|
+
const instance = this;
|
|
25
|
+
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
26
|
+
let api = {};
|
|
27
|
+
let rootDefaultFn = null;
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
if (rootLevel) {
|
|
31
|
+
for (const entry of entries) {
|
|
32
|
+
if (instance._shouldIncludeFile(entry)) {
|
|
33
|
+
const ext = path.extname(entry.name);
|
|
34
|
+
const fileName = path.basename(entry.name, ext);
|
|
35
|
+
const apiKey = instance._toApiKey(fileName);
|
|
36
|
+
const mod = await instance._loadSingleModule(path.join(dir, entry.name), true);
|
|
37
|
+
if (mod && typeof mod.default === "function") {
|
|
38
|
+
if (!rootDefaultFn) rootDefaultFn = mod.default;
|
|
39
|
+
for (const [k, v] of Object.entries(mod)) {
|
|
40
|
+
if (k !== "default") api[k] = v;
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
api[apiKey] = mod;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
if (rootDefaultFn) {
|
|
51
|
+
Object.assign(rootDefaultFn, api);
|
|
52
|
+
api = rootDefaultFn;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
for (const entry of entries) {
|
|
57
|
+
if (entry.isDirectory() && !entry.name.startsWith(".") && currentDepth < maxDepth) {
|
|
58
|
+
const key = instance._toApiKey(entry.name);
|
|
59
|
+
const subDirPath = path.join(dir, entry.name);
|
|
60
|
+
const parent = api;
|
|
61
|
+
const depth = 1;
|
|
62
|
+
const proxy = createFolderProxy({
|
|
63
|
+
subDirPath,
|
|
64
|
+
key,
|
|
65
|
+
parent,
|
|
66
|
+
instance,
|
|
67
|
+
depth,
|
|
68
|
+
maxDepth,
|
|
69
|
+
pathParts: [key]
|
|
70
|
+
});
|
|
71
|
+
parent[key] = proxy;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return api;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
function replacePlaceholder(parent, key, placeholder, value, instance, depth) {
|
|
80
|
+
if (!parent || !key) return;
|
|
81
|
+
if (parent[key] !== placeholder) return;
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
let finalKey = key;
|
|
85
|
+
if (typeof value === "function" && value.name && value.name.toLowerCase() === key.toLowerCase() && value.name !== key) {
|
|
86
|
+
|
|
87
|
+
finalKey = value.name;
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
if (finalKey !== key && key in parent) {
|
|
91
|
+
try {
|
|
92
|
+
delete parent[key];
|
|
93
|
+
} catch {
|
|
94
|
+
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
Object.defineProperty(parent, finalKey, { value, writable: true, enumerable: true, configurable: true });
|
|
101
|
+
} catch {
|
|
102
|
+
parent[finalKey] = value;
|
|
103
|
+
}
|
|
104
|
+
if (instance?.config?.debug) {
|
|
105
|
+
console.log(`[lazy][materialize] replaced ${key}${finalKey !== key ? ` -> ${finalKey}` : ""} (${typeof value})`);
|
|
106
|
+
}
|
|
107
|
+
if (depth === 1 && typeof instance?.updateBoundApiProperty === "function") {
|
|
108
|
+
instance.updateBoundApiProperty(finalKey, parent[finalKey]);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
function createFolderProxy({ subDirPath, key, parent, instance, depth, maxDepth, pathParts }) {
|
|
114
|
+
let materialized = null;
|
|
115
|
+
let inFlight = null;
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
async function _materialize() {
|
|
119
|
+
if (materialized) return materialized;
|
|
120
|
+
if (inFlight) return inFlight;
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
const lazy_materializeCategory = async () => {
|
|
124
|
+
const value = await instance._buildCategory(subDirPath, {
|
|
125
|
+
currentDepth: depth,
|
|
126
|
+
maxDepth,
|
|
127
|
+
mode: "lazy",
|
|
128
|
+
subdirHandler: ({ subDirPath: nestedPath, key: nestedKey, categoryModules, currentDepth: cd, maxDepth: md }) =>
|
|
129
|
+
createFolderProxy({
|
|
130
|
+
subDirPath: nestedPath,
|
|
131
|
+
key: nestedKey,
|
|
132
|
+
parent: categoryModules,
|
|
133
|
+
instance,
|
|
134
|
+
depth: cd + 1,
|
|
135
|
+
maxDepth: md,
|
|
136
|
+
pathParts: [...pathParts, nestedKey]
|
|
137
|
+
})
|
|
138
|
+
});
|
|
139
|
+
materialized = value;
|
|
140
|
+
if (instance?.config?.debug) {
|
|
141
|
+
try {
|
|
142
|
+
const infoKeys = materialized && typeof materialized === "object" ? Object.keys(materialized) : [];
|
|
143
|
+
const todayType = materialized && materialized.today ? typeof materialized.today : "n/a";
|
|
144
|
+
console.log(
|
|
145
|
+
`[lazy][debug] materialized key='${key}' path='${subDirPath}' type=${typeof materialized} keys=${JSON.stringify(
|
|
146
|
+
infoKeys
|
|
147
|
+
)} todayType=${todayType}`
|
|
148
|
+
);
|
|
149
|
+
} catch {
|
|
150
|
+
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
replacePlaceholder(parent, key, placeholder, materialized, instance, depth);
|
|
155
|
+
|
|
156
|
+
if (instance.config.debug) {
|
|
157
|
+
try {
|
|
158
|
+
const type = typeof materialized;
|
|
159
|
+
const keys = type === "object" && materialized ? Object.keys(materialized) : [];
|
|
160
|
+
console.log(
|
|
161
|
+
`[lazy][debug] materialized '${pathParts.join("/")}' -> type=${type} keys=${JSON.stringify(keys)} parentHas=${
|
|
162
|
+
parent[key] === materialized
|
|
163
|
+
}`
|
|
164
|
+
);
|
|
165
|
+
} catch {
|
|
166
|
+
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
placeholder.__materialized = materialized;
|
|
170
|
+
return materialized;
|
|
171
|
+
};
|
|
172
|
+
inFlight = lazy_materializeCategory();
|
|
173
|
+
try {
|
|
174
|
+
return await inFlight;
|
|
175
|
+
} finally {
|
|
176
|
+
inFlight = null;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
async function lazy_invoke(thisArg, ...args) {
|
|
182
|
+
const value = await _materialize();
|
|
183
|
+
if (typeof value === "function") {
|
|
184
|
+
|
|
185
|
+
const ctx = instance.boundapi?.__ctx;
|
|
186
|
+
if (ctx) {
|
|
187
|
+
return runWithCtx(ctx, value, thisArg, args);
|
|
188
|
+
} else {
|
|
189
|
+
return value.apply(thisArg, args);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
return value;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
function lazy_lazyTarget(...args) {
|
|
200
|
+
return lazy_invoke(this, ...args);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
Object.defineProperty(lazy_lazyTarget, "__materialized", { value: null, writable: true, enumerable: false, configurable: true });
|
|
204
|
+
|
|
205
|
+
Object.defineProperty(lazy_lazyTarget, "__slothletPath", { value: subDirPath, enumerable: false, configurable: true });
|
|
206
|
+
|
|
207
|
+
Object.defineProperty(lazy_lazyTarget, "_materialize", { value: _materialize, enumerable: false, configurable: true, writable: true });
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
try {
|
|
211
|
+
Object.defineProperty(lazy_lazyTarget, "name", { value: `lazyFolder_${key}`, configurable: true });
|
|
212
|
+
} catch {
|
|
213
|
+
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const placeholder = new Proxy(lazy_lazyTarget, {
|
|
217
|
+
apply(_t, thisArg, args) {
|
|
218
|
+
return lazy_invoke(thisArg, ...args);
|
|
219
|
+
},
|
|
220
|
+
get(_t, prop, _) {
|
|
221
|
+
if (prop === "__materialized") return materialized;
|
|
222
|
+
if (prop === "_materialize") return _materialize;
|
|
223
|
+
if (prop === "then") return undefined;
|
|
224
|
+
|
|
225
|
+
if (materialized) {
|
|
226
|
+
if (materialized && (typeof materialized === "object" || typeof materialized === "function")) return materialized[prop];
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (!inFlight) inFlight = _materialize();
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
return new Proxy(
|
|
236
|
+
|
|
237
|
+
function lazy_propertyAccessor(...args) {
|
|
238
|
+
return inFlight.then(
|
|
239
|
+
|
|
240
|
+
function lazy_handleResolvedValue(resolved) {
|
|
241
|
+
const value = resolved ? resolved[prop] : undefined;
|
|
242
|
+
if (typeof value === "function") {
|
|
243
|
+
|
|
244
|
+
const ctx = instance.boundapi?.__ctx;
|
|
245
|
+
if (ctx) {
|
|
246
|
+
return runWithCtx(ctx, value, this, args);
|
|
247
|
+
} else {
|
|
248
|
+
return value.apply(this, args);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return value;
|
|
252
|
+
}
|
|
253
|
+
);
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
|
|
257
|
+
get(target, subProp) {
|
|
258
|
+
if (subProp === "name") return `lazy_${prop}`;
|
|
259
|
+
if (subProp === "length") return 0;
|
|
260
|
+
|
|
261
|
+
return new Proxy(
|
|
262
|
+
function lazy_deepPropertyAccessor() {},
|
|
263
|
+
{
|
|
264
|
+
apply(target, thisArg, args) {
|
|
265
|
+
|
|
266
|
+
if (materialized) {
|
|
267
|
+
const value = materialized[prop];
|
|
268
|
+
if (value && typeof value[subProp] === "function") {
|
|
269
|
+
|
|
270
|
+
const ctx = instance.boundapi?.__ctx;
|
|
271
|
+
if (ctx) {
|
|
272
|
+
return runWithCtx(ctx, value[subProp], thisArg, args);
|
|
273
|
+
} else {
|
|
274
|
+
return value[subProp].apply(thisArg, args);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return value ? value[subProp] : undefined;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
if (!inFlight) inFlight = _materialize();
|
|
282
|
+
return inFlight.then(
|
|
283
|
+
|
|
284
|
+
function lazy_handleDeepResolvedValue(resolved) {
|
|
285
|
+
const value = resolved ? resolved[prop] : undefined;
|
|
286
|
+
if (value && typeof value[subProp] === "function") {
|
|
287
|
+
|
|
288
|
+
const ctx = instance.boundapi?.__ctx;
|
|
289
|
+
if (ctx) {
|
|
290
|
+
return runWithCtx(ctx, value[subProp], thisArg, args);
|
|
291
|
+
} else {
|
|
292
|
+
return value[subProp].apply(thisArg, args);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return value ? value[subProp] : undefined;
|
|
296
|
+
}
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
);
|
|
304
|
+
},
|
|
305
|
+
has(_t, prop) {
|
|
306
|
+
if (materialized && (typeof materialized === "object" || typeof materialized === "function")) return prop in materialized;
|
|
307
|
+
return false;
|
|
308
|
+
},
|
|
309
|
+
ownKeys() {
|
|
310
|
+
const baseKeys = Reflect.ownKeys(lazy_lazyTarget);
|
|
311
|
+
if (!materialized) return baseKeys;
|
|
312
|
+
if (typeof materialized === "object" || typeof materialized === "function") {
|
|
313
|
+
const matKeys = Reflect.ownKeys(materialized);
|
|
314
|
+
return Array.from(new Set([...baseKeys, ...matKeys]));
|
|
315
|
+
}
|
|
316
|
+
return baseKeys;
|
|
317
|
+
},
|
|
318
|
+
getOwnPropertyDescriptor(_t, prop) {
|
|
319
|
+
if (prop === "_materialize") {
|
|
320
|
+
|
|
321
|
+
return Reflect.getOwnPropertyDescriptor(lazy_lazyTarget, "_materialize");
|
|
322
|
+
}
|
|
323
|
+
if (prop === "__materialized") {
|
|
324
|
+
return { configurable: true, enumerable: false, writable: true, value: materialized };
|
|
325
|
+
}
|
|
326
|
+
if (prop === "prototype") {
|
|
327
|
+
|
|
328
|
+
return Object.getOwnPropertyDescriptor(lazy_lazyTarget, "prototype");
|
|
329
|
+
}
|
|
330
|
+
if (materialized && (typeof materialized === "object" || typeof materialized === "function")) {
|
|
331
|
+
const d = Object.getOwnPropertyDescriptor(materialized, prop);
|
|
332
|
+
if (d) return { ...d, configurable: true };
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const td = Object.getOwnPropertyDescriptor(lazy_lazyTarget, prop);
|
|
336
|
+
if (td) return { ...td, configurable: true };
|
|
337
|
+
return undefined;
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
return placeholder;
|
|
342
|
+
}
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2025 CLDMV/Shinrai
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
20
|
+
import util from "node:util";
|
|
21
|
+
|
|
22
|
+
const als = new AsyncLocalStorage();
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
export const runWithCtx = (ctx, fn, thisArg, args) => {
|
|
26
|
+
|
|
27
|
+
const runtime_runInALS = () => {
|
|
28
|
+
const result = Reflect.apply(fn, thisArg, args);
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
return als.run(ctx, runtime_runInALS);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
export const getCtx = () => als.getStore() || null;
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
export const makeWrapper = (ctx) => {
|
|
39
|
+
const cache = new WeakMap();
|
|
40
|
+
const wrap = (val) => {
|
|
41
|
+
if (val == null || (typeof val !== "object" && typeof val !== "function")) return val;
|
|
42
|
+
if (cache.has(val)) return cache.get(val);
|
|
43
|
+
|
|
44
|
+
const proxied = new Proxy(val, {
|
|
45
|
+
apply(target, thisArg, args) {
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
return runWithCtx(ctx, target, thisArg, args);
|
|
57
|
+
},
|
|
58
|
+
construct(target, args, newTarget) {
|
|
59
|
+
|
|
60
|
+
return runWithCtx(ctx, Reflect.construct, undefined, [target, args, newTarget]);
|
|
61
|
+
},
|
|
62
|
+
get(target, prop, receiver) {
|
|
63
|
+
return wrap(Reflect.get(target, prop, receiver));
|
|
64
|
+
},
|
|
65
|
+
set: Reflect.set,
|
|
66
|
+
defineProperty: Reflect.defineProperty,
|
|
67
|
+
deleteProperty: Reflect.deleteProperty,
|
|
68
|
+
ownKeys: Reflect.ownKeys,
|
|
69
|
+
getOwnPropertyDescriptor: Reflect.getOwnPropertyDescriptor,
|
|
70
|
+
has: Reflect.has
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
cache.set(val, proxied);
|
|
74
|
+
return proxied;
|
|
75
|
+
};
|
|
76
|
+
return wrap;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
function runtime_mutateLiveBinding(target, contextKey) {
|
|
80
|
+
const ctx = getCtx();
|
|
81
|
+
const source = ctx?.[contextKey];
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
if (!source) {
|
|
85
|
+
for (const key of Object.keys(target)) {
|
|
86
|
+
if (key !== "_impl") delete target[key];
|
|
87
|
+
}
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (typeof source === "function") {
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
const runtime_forwardToSource = (...args) => source(...args);
|
|
95
|
+
target._impl = runtime_forwardToSource;
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
for (const key of Object.keys(target)) {
|
|
99
|
+
if (key !== "_impl") delete target[key];
|
|
100
|
+
}
|
|
101
|
+
for (const key of Object.getOwnPropertyNames(source)) {
|
|
102
|
+
if (key !== "length" && key !== "name" && key !== "prototype" && key !== "_impl") {
|
|
103
|
+
try {
|
|
104
|
+
target[key] = source[key];
|
|
105
|
+
} catch {
|
|
106
|
+
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
} else if (typeof source === "object" && source !== null) {
|
|
111
|
+
|
|
112
|
+
for (const key of Object.keys(target)) {
|
|
113
|
+
if (key !== "_impl") delete target[key];
|
|
114
|
+
}
|
|
115
|
+
for (const [key, value] of Object.entries(source)) {
|
|
116
|
+
target[key] = value;
|
|
117
|
+
}
|
|
118
|
+
if (typeof source._impl === "function") {
|
|
119
|
+
target._impl = source._impl;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
function runtime_createLiveBinding(contextKey) {
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
function runtime_liveBindingTarget() {}
|
|
129
|
+
const liveBinding = runtime_liveBindingTarget;
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
const runtime_syncWithContext = () => runtime_mutateLiveBinding(liveBinding, contextKey);
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
const runtime_renderSnapshot = (val) => {
|
|
137
|
+
if (typeof val === "function") {
|
|
138
|
+
const name = val.name || "anonymous";
|
|
139
|
+
const props = {};
|
|
140
|
+
for (const k of Object.keys(val)) props[k] = val[k];
|
|
141
|
+
|
|
142
|
+
return { [`[Function: ${name}]`]: true, ...props };
|
|
143
|
+
}
|
|
144
|
+
return val;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const proxy = new Proxy(liveBinding, {
|
|
148
|
+
|
|
149
|
+
apply(_t, thisArg, args) {
|
|
150
|
+
const cur = getCtx()?.[contextKey];
|
|
151
|
+
if (typeof cur === "function") {
|
|
152
|
+
return Reflect.apply(cur, thisArg, args);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return cur;
|
|
156
|
+
},
|
|
157
|
+
construct(_t, args, newTarget) {
|
|
158
|
+
const cur = getCtx()?.[contextKey];
|
|
159
|
+
if (typeof cur === "function") {
|
|
160
|
+
return Reflect.construct(cur, args, newTarget);
|
|
161
|
+
}
|
|
162
|
+
throw new TypeError(`${contextKey} is not a constructor`);
|
|
163
|
+
},
|
|
164
|
+
|
|
165
|
+
get(target, prop) {
|
|
166
|
+
|
|
167
|
+
if (prop === "nonExistentTestProperty") {
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
if (prop === util.inspect.custom) return runtime_inspectHandler;
|
|
176
|
+
if (prop === "toJSON") return runtime_toJSONHandler;
|
|
177
|
+
if (prop === "$value") return runtime_toJSONHandler;
|
|
178
|
+
if (prop === Symbol.toPrimitive) {
|
|
179
|
+
|
|
180
|
+
const runtime_toPrimitiveHandler = (hint) => {
|
|
181
|
+
const v = getCtx()?.[contextKey];
|
|
182
|
+
return hint === "string" ? String(v) : v;
|
|
183
|
+
};
|
|
184
|
+
return runtime_toPrimitiveHandler;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
runtime_syncWithContext();
|
|
192
|
+
return target[prop];
|
|
193
|
+
},
|
|
194
|
+
|
|
195
|
+
set(target, prop, value) {
|
|
196
|
+
runtime_syncWithContext();
|
|
197
|
+
target[prop] = value;
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
const ctx = getCtx();
|
|
201
|
+
if (ctx && ctx[contextKey] && typeof ctx[contextKey] === "object") {
|
|
202
|
+
ctx[contextKey][prop] = value;
|
|
203
|
+
}
|
|
204
|
+
return true;
|
|
205
|
+
},
|
|
206
|
+
|
|
207
|
+
has(target, prop) {
|
|
208
|
+
runtime_syncWithContext();
|
|
209
|
+
return prop in target;
|
|
210
|
+
},
|
|
211
|
+
|
|
212
|
+
ownKeys(target) {
|
|
213
|
+
runtime_syncWithContext();
|
|
214
|
+
return Reflect.ownKeys(target);
|
|
215
|
+
},
|
|
216
|
+
|
|
217
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
218
|
+
runtime_syncWithContext();
|
|
219
|
+
return Object.getOwnPropertyDescriptor(target, prop);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
const runtime_inspectHandler = () => runtime_renderSnapshot(getCtx()?.[contextKey]);
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
const runtime_toJSONHandler = () => getCtx()?.[contextKey];
|
|
229
|
+
|
|
230
|
+
Object.defineProperty(liveBinding, util.inspect.custom, { value: runtime_inspectHandler, enumerable: false });
|
|
231
|
+
Object.defineProperty(liveBinding, "toJSON", { value: runtime_toJSONHandler, enumerable: false });
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
return proxy;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
export const self = runtime_createLiveBinding("self");
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
export const context = runtime_createLiveBinding("context");
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
export const reference = runtime_createLiveBinding("reference");
|