dsh-ptc-plus 0.3.1 → 0.4.1
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.en.md +66 -73
- package/README.md +66 -73
- package/assets/ptc-plus-bindings-en.png +0 -0
- package/assets/ptc-plus-bindings-zh.png +0 -0
- package/assets/ptc-plus-repair-en.png +0 -0
- package/assets/ptc-plus-repair-zh.png +0 -0
- package/assets/ptc-plus-repl-workspace-en.png +0 -0
- package/assets/ptc-plus-repl-workspace-zh.png +0 -0
- package/assets/ptc-plus-settings-en.png +0 -0
- package/assets/ptc-plus-settings-zh.png +0 -0
- package/client.js +30610 -722
- package/compiler-amaro.cjs +5 -0
- package/compiler-amaro.cjs.map +6 -0
- package/compiler-asset-124f80c1bba92ff5fe79cfec6e8ad518f476de4a42514cae3919538887f510e8.txt +1 -0
- package/compiler-assets.json +3 -0
- package/compiler-core.cjs +554 -0
- package/compiler-core.cjs.map +6 -0
- package/compiler-platform.cjs +2 -0
- package/compiler-platform.cjs.map +6 -0
- package/compiler-typescript.cjs +388 -0
- package/compiler-typescript.cjs.map +6 -0
- package/docs/README.md +6 -0
- package/docs/adr/0004-declare-program-bindings-at-the-host.md +1 -1
- package/docs/adr/0006-rewrite-module-syntax-with-an-ast.md +6 -4
- package/docs/adr/0007-separate-worker-transport-from-session-semantics.md +15 -3
- package/docs/adr/0009-bound-expensive-acceptance-default.md +2 -0
- package/docs/adr/0010-session-log-derived-recovery-tips.md +59 -5
- package/docs/adr/0011-isolate-the-local-recovery-tip-provider.md +1 -1
- package/docs/adr/0012-preserve-mixed-destructuring-initialization.md +3 -1
- package/docs/adr/0013-isolate-repl-redeclaration-convenience.md +9 -3
- package/docs/adr/0014-persist-cell-rewrite-policy.md +11 -5
- package/docs/adr/0015-preserve-source-positions-through-rewrites.md +1 -1
- package/docs/adr/0016-archive-resolved-review-findings.md +1 -1
- package/docs/adr/0017-track-the-latest-dsh-public-surface.md +49 -0
- package/docs/adr/0018-use-versioned-fixture-for-ab-benchmark.md +3 -3
- package/docs/adr/0019-plugin-settings-and-kill-switch.md +8 -6
- package/docs/adr/0020-optional-cordis-tools-in-ptc-mode.md +2 -2
- package/docs/adr/0021-separate-host-bootstrap-from-session-runtime.md +17 -3
- package/docs/adr/0023-global-user-bindings.md +104 -0
- package/docs/adr/0024-repl-console-observation.md +33 -0
- package/docs/adr/0025-use-versioned-logical-binding-identities.md +119 -0
- package/docs/adr/0026-persist-the-delivery-verdict-against-the-verified-candidate.md +21 -0
- package/docs/adr/0027-run-the-session-kernel-in-a-killable-helper-process.md +37 -0
- package/docs/adr/0028-attach-to-the-host-ptc-execution-seam.md +103 -0
- package/docs/adr/0029-keep-plugin-unavailability-out-of-the-host-agent-lifecycle.md +27 -0
- package/docs/adr/0030-serve-both-typert-codec-generations-from-one-codec.md +27 -0
- package/docs/architecture.md +82 -22
- package/docs/binding-review-placement-design.md +200 -0
- package/docs/capability-projection.md +1 -1
- package/docs/client-ui.md +96 -20
- package/docs/durability-design.md +61 -24
- package/docs/evaluation.md +78 -6
- package/docs/installation.md +41 -6
- package/docs/program-data-plane.md +6 -0
- package/docs/publishing.md +5 -3
- package/docs/runtime-reference.md +79 -25
- package/docs/semantic-validation.md +29 -0
- package/docs/stateful-binding-updates-proposal.md +755 -0
- package/docs/user-bindings.en.md +64 -0
- package/docs/user-bindings.md +64 -0
- package/docs/value-wire.md +3 -3
- package/docs/verification.md +57 -0
- package/index.js +374 -92
- package/internal/ast-traversal.js +5 -2
- package/internal/binding-descriptors.js +1 -0
- package/internal/binding-pattern-traversal.js +25 -0
- package/internal/binding-pattern.js +26 -29
- package/internal/binding-source-analysis.js +391 -0
- package/internal/binding-update-policy.js +66 -0
- package/internal/bounded-worker-round.js +56 -0
- package/internal/callable-definition-environment.js +42 -0
- package/internal/callable-source-catalog.js +129 -0
- package/internal/callable-source-encoding.js +47 -0
- package/internal/callable-source-facts.js +459 -0
- package/internal/callable-source-format.js +2 -0
- package/internal/callable-source-lowering.js +123 -0
- package/internal/cell-analysis-contract.js +25 -0
- package/internal/cell-analysis.js +252 -242
- package/internal/cell-completion.js +15 -0
- package/internal/cell-error.js +7 -0
- package/internal/cell-parser.js +66 -0
- package/internal/cell-rewriter.js +3 -553
- package/internal/commonjs-export-evidence.js +63 -0
- package/internal/commonjs-source-evidence.js +72 -0
- package/internal/compiler-amaro.cjs +2 -0
- package/internal/compiler-compression.js +12 -0
- package/internal/compiler-data.js +58 -0
- package/internal/compiler-descriptors.js +54 -0
- package/internal/compiler-dynamic-cache.js +48 -0
- package/internal/compiler-entry.js +35 -0
- package/internal/compiler-generated-names.js +17 -0
- package/internal/compiler-intrinsics.js +84 -0
- package/internal/compiler-module-links.js +34 -0
- package/internal/compiler-operations.js +61 -0
- package/internal/compiler-platform-factory.js +84 -0
- package/internal/compiler-platform.js +136 -0
- package/internal/compiler-record-roles.js +53 -0
- package/internal/compiler-region-output.js +259 -0
- package/internal/compiler-resource-lowering.js +103 -0
- package/internal/compiler-scope-facts.js +115 -0
- package/internal/compiler-service.js +109 -0
- package/internal/compiler-source-cache.js +32 -0
- package/internal/compiler-source-pieces.js +93 -0
- package/internal/compiler-source-regions.js +132 -0
- package/internal/compiler-storage-source.js +54 -0
- package/internal/compiler-text.js +15 -0
- package/internal/compiler-typescript.cjs +2 -0
- package/internal/config-spec.js +91 -25
- package/internal/cordis-tools-owner.js +130 -21
- package/internal/diagnostic.js +27 -1
- package/internal/direct-surface-owner.js +76 -23
- package/internal/dynamic-binding-evidence.js +16 -0
- package/internal/dynamic-environment-compiler.js +779 -0
- package/internal/dynamic-environment-integration.js +87 -0
- package/internal/dynamic-environment-runtime.js +362 -0
- package/internal/dynamic-native-calls.js +150 -0
- package/internal/dynamic-native-runtime.js +248 -0
- package/internal/dynamic-scope-analysis.js +90 -0
- package/internal/edit-transport-owner.js +28 -3
- package/internal/execution-seam-compat.js +193 -0
- package/internal/failure-reporting.js +118 -18
- package/internal/host-rpc.js +66 -0
- package/internal/isolated-worker.js +548 -0
- package/internal/kernel-child.js +139 -0
- package/internal/kernel-worker.js +911 -116
- package/internal/language-semantics.js +14 -0
- package/internal/legacy-binding-catalog.js +48 -0
- package/internal/legacy-cell-analysis.js +600 -0
- package/internal/legacy-cell-parser.js +50 -0
- package/internal/legacy-cell-rewriter.js +631 -0
- package/internal/legacy-repl-convenience.js +291 -0
- package/internal/managed-module-operations.js +170 -0
- package/internal/module-compilation.js +321 -0
- package/internal/module-runtime-platform.js +19 -0
- package/internal/module-transform-contract.js +12 -0
- package/internal/native-await-declarations.js +68 -0
- package/internal/native-root-compilation.js +54 -0
- package/internal/native-root-dynamic.js +67 -0
- package/internal/recovery-tips.js +27 -15
- package/internal/rejected-cell-editor.js +13 -8
- package/internal/repl-convenience.js +2 -201
- package/internal/repl-memory-projection.js +81 -12
- package/internal/repl-observation-interest.js +70 -0
- package/internal/repl-rewrite-contract.js +21 -0
- package/internal/repl-scope-normalizer.js +2091 -0
- package/internal/repl-value-observer.js +114 -0
- package/internal/rpc-contract.js +52 -0
- package/internal/runtime-bridge-owner.js +172 -28
- package/internal/runtime-config.js +48 -51
- package/internal/runtime-contexts.js +82 -10
- package/internal/runtime-messages.js +111 -0
- package/internal/session-cell-executor.js +455 -91
- package/internal/session-journal-recovery.js +684 -0
- package/internal/session-journal-schema.js +106 -0
- package/internal/session-journal.js +237 -440
- package/internal/session-log-view.js +42 -9
- package/internal/session-runtime.js +325 -80
- package/internal/session-state.js +249 -40
- package/internal/source-position-map.js +211 -71
- package/internal/stateful-module-compiler.js +154 -0
- package/internal/stateful-module-runtime.js +303 -0
- package/internal/stateful-root-compiler.js +598 -0
- package/internal/stateful-root-runtime.js +340 -0
- package/internal/typert-codec-compat.js +28 -0
- package/internal/typescript-transform.js +29 -0
- package/internal/user-binding-authoring.js +70 -0
- package/internal/user-binding-console-worker.js +184 -0
- package/internal/user-binding-console.js +148 -0
- package/internal/user-binding-draft-projection.js +335 -0
- package/internal/user-binding-model-context.js +31 -0
- package/internal/user-binding-runner.js +56 -0
- package/internal/user-bindings-owner.js +1065 -0
- package/internal/user-bindings-store.js +293 -0
- package/internal/user-bindings.js +353 -0
- package/internal/value-wire-schema.js +28 -0
- package/internal/value-wire.js +29 -20
- package/internal/worker-client.js +57 -67
- package/internal/worker-environment.js +45 -0
- package/internal/worker-output-relay.js +29 -0
- package/internal/worker-shutdown.js +14 -0
- package/package.json +81 -11
- package/src/client-activity.js +109 -45
- package/src/client-authoring-view.js +580 -0
- package/src/client-binding-review.js +230 -0
- package/src/client-bindings-data.js +29 -0
- package/src/client-catalog.js +127 -0
- package/src/client-code-editor.js +84 -0
- package/src/client-console.js +112 -0
- package/src/client-copy.js +370 -0
- package/src/client-feature-gates.js +62 -0
- package/src/client-host-compat.js +150 -0
- package/src/client-indicator-view.js +267 -0
- package/src/client-repl-view.js +292 -0
- package/src/client-rpc.js +20 -0
- package/src/client-settings-view.js +221 -0
- package/src/client-styles.js +106 -0
- package/src/client-tool-view.js +108 -0
- package/src/client-transport.js +159 -0
- package/src/client-workbench.js +521 -0
- package/src/client.js +233 -615
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { compilerDescriptors } from './compiler-descriptors.js'
|
|
2
|
+
import { fileURLToPath, registerModuleResolutionHook } from './module-runtime-platform.js'
|
|
3
|
+
import { moduleCompilerIntrinsics, moduleRuntimeIntrinsics } from './compiler-intrinsics.js'
|
|
4
|
+
export { pathToFileURL } from './module-runtime-platform.js'
|
|
5
|
+
|
|
6
|
+
// Node owns URLs, graph membership and evaluation. Only loader-proved export
|
|
7
|
+
// relations may decode the transport used by compiled source.
|
|
8
|
+
const { Object, Reflect, Map, Set, WeakMap, Proxy, mapGet, mapSet, mapForEach,
|
|
9
|
+
setHas, setAdd, weakMapGet, weakMapSet, sort, compare, startsWith, stringify, observeOwnedPromise } = moduleRuntimeIntrinsics
|
|
10
|
+
const modules = new Map()
|
|
11
|
+
const namespaces = new WeakMap()
|
|
12
|
+
const adapters = new Map()
|
|
13
|
+
const adapterRequests = new Map()
|
|
14
|
+
const adapterModules = new Map()
|
|
15
|
+
const staticLinks = new Map()
|
|
16
|
+
let nextAdapter = 0
|
|
17
|
+
|
|
18
|
+
// Native imports make these helpers available to hoisted functions before the
|
|
19
|
+
// importing module evaluates, without consulting any source-owned bindings.
|
|
20
|
+
export const moduleBindingIntrinsics = moduleCompilerIntrinsics
|
|
21
|
+
|
|
22
|
+
function moduleRecord(url) {
|
|
23
|
+
let record = mapGet(modules, url)
|
|
24
|
+
if (record === undefined) {
|
|
25
|
+
record = { resolutions: new Map(), links: new Map() }
|
|
26
|
+
mapSet(modules, url, record)
|
|
27
|
+
}
|
|
28
|
+
return record
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Reserve the compiler's closed interface before Node instantiates cycles. */
|
|
32
|
+
export function reserveStatefulModule(url, moduleInterface) {
|
|
33
|
+
const record = moduleRecord(url)
|
|
34
|
+
record.interface = moduleInterface
|
|
35
|
+
record.exports = new Map()
|
|
36
|
+
if (moduleInterface === undefined) return
|
|
37
|
+
for (let index = 0; index < moduleInterface.exports.length; index++) {
|
|
38
|
+
const descriptor = moduleInterface.exports[index]
|
|
39
|
+
mapSet(record.exports, descriptor.name, descriptor)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function resolutionKey(specifier, attributes) {
|
|
44
|
+
const entries = Object.entries(attributes ?? {})
|
|
45
|
+
sort(entries, (left, right) => compare(left[0], right[0]))
|
|
46
|
+
return stringify([specifier, entries])
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Capture the native linker's actual attribute-aware source relation. */
|
|
50
|
+
export function recordStatefulModuleResolution(parentUrl, specifier, attributes, url) {
|
|
51
|
+
mapSet(moduleRecord(parentUrl).resolutions, resolutionKey(specifier, attributes), url)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Register a compiler-owned static link reference and the source it names.
|
|
55
|
+
* The compiler already distinguished the attribute set when it built the
|
|
56
|
+
* reference, so no source-controlled string operation runs here. */
|
|
57
|
+
export function statefulModuleLink(source, reference) {
|
|
58
|
+
mapSet(staticLinks, reference, source)
|
|
59
|
+
return reference
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Resolve the original source through the complete current public hook chain. */
|
|
63
|
+
export function resolveStatefulModuleLink(reference, context) {
|
|
64
|
+
const source = mapGet(staticLinks, reference)
|
|
65
|
+
if (source === undefined) return undefined
|
|
66
|
+
const parentUrl = context.parentURL
|
|
67
|
+
const attributes = { ...context.importAttributes }
|
|
68
|
+
let resolution
|
|
69
|
+
let failure
|
|
70
|
+
const request = `ptc-module:resolve/${++nextAdapter}`
|
|
71
|
+
const dispose = registerModuleResolutionHook((specifier, details, nextResolve) => {
|
|
72
|
+
if (specifier !== request) return nextResolve(specifier, details)
|
|
73
|
+
try {
|
|
74
|
+
resolution = nextResolve(source, context)
|
|
75
|
+
return resolution
|
|
76
|
+
} catch (error) {
|
|
77
|
+
failure = { error }
|
|
78
|
+
throw error
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
try {
|
|
82
|
+
// This native operation resolves only; the original linker still owns all
|
|
83
|
+
// loading, export validation, evaluation ordering and cyclic instantiation.
|
|
84
|
+
import.meta.resolve(request)
|
|
85
|
+
} finally {
|
|
86
|
+
dispose()
|
|
87
|
+
}
|
|
88
|
+
// import.meta.resolve converts some native not-found errors into URLs. The
|
|
89
|
+
// actual static linker must receive that exact original failure instead.
|
|
90
|
+
if (failure !== undefined) throw failure.error
|
|
91
|
+
mapSet(moduleRecord(parentUrl).links, resolutionKey(source, attributes), resolution.url)
|
|
92
|
+
return { ...resolution, shortCircuit: true }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Native self namespaces settle exported membership, including require interop. */
|
|
96
|
+
export function recordStatefulModuleNamespace(url, namespace) {
|
|
97
|
+
const names = Reflect.ownKeys(namespace)
|
|
98
|
+
const keys = new Set()
|
|
99
|
+
for (let index = 0; index < names.length; index++) setAdd(keys, names[index])
|
|
100
|
+
moduleRecord(url).nativeKeys = keys
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function sourceUrl(parent, source, attributes) {
|
|
104
|
+
const record = mapGet(modules, parent)
|
|
105
|
+
const key = resolutionKey(source, attributes)
|
|
106
|
+
return record === undefined ? undefined : mapGet(record.links, key) ?? mapGet(record.resolutions, key)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function exportTransport(url, name, visited = new Set()) {
|
|
110
|
+
const record = mapGet(modules, url)
|
|
111
|
+
const plan = record?.interface
|
|
112
|
+
if (plan === undefined) return record?.nativeKeys !== undefined && setHas(record.nativeKeys, name) ? { kind: 'native' } : undefined
|
|
113
|
+
const key = stringify([url, name])
|
|
114
|
+
if (setHas(visited, key)) return undefined
|
|
115
|
+
setAdd(visited, key)
|
|
116
|
+
const descriptor = mapGet(record.exports, name)
|
|
117
|
+
if (descriptor !== undefined) {
|
|
118
|
+
if (descriptor.kind === 'accessor' || descriptor.kind === 'native') return descriptor
|
|
119
|
+
const provider = sourceUrl(url, descriptor.source, descriptor.attributes)
|
|
120
|
+
return descriptor.kind === 'namespace' ? { kind: 'namespace', url: provider }
|
|
121
|
+
: mapGet(modules, provider)?.interface === undefined ? { kind: 'native' }
|
|
122
|
+
: exportTransport(provider, descriptor.imported, visited)
|
|
123
|
+
}
|
|
124
|
+
if (name === 'default') return undefined
|
|
125
|
+
// Node's namespace keys have already excluded ambiguous star exports. An
|
|
126
|
+
// uncompiled provider cannot contribute an accessor; no value is inspected.
|
|
127
|
+
for (let index = 0; index < plan.stars.length; index++) {
|
|
128
|
+
const star = plan.stars[index]
|
|
129
|
+
const transport = exportTransport(sourceUrl(url, star.source, star.attributes), name, visited)
|
|
130
|
+
if (transport !== undefined) return transport
|
|
131
|
+
}
|
|
132
|
+
return undefined
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function decode(url, name, value) {
|
|
136
|
+
const transport = exportTransport(url, name)
|
|
137
|
+
if (transport?.kind === 'accessor') return value().v
|
|
138
|
+
if (transport?.kind === 'namespace') return managedModuleNamespace(transport.url, value)
|
|
139
|
+
return value
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Read lazily, including hoisted accesses before a provider's body executes. */
|
|
143
|
+
export function readModuleImport(parentUrl, specifier, name, getNative, attributes) {
|
|
144
|
+
const url = sourceUrl(parentUrl, specifier, attributes)
|
|
145
|
+
const native = getNative()
|
|
146
|
+
return name === null ? managedModuleNamespace(url, native) : decode(url, name, native)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Stable readonly reflection with exact live values, without a native brand. */
|
|
150
|
+
export function managedModuleNamespace(url, native) {
|
|
151
|
+
const cached = weakMapGet(namespaces, native)
|
|
152
|
+
if (cached !== undefined) return cached
|
|
153
|
+
const keys = Reflect.ownKeys(native)
|
|
154
|
+
let managed = false
|
|
155
|
+
for (let index = 0; index < keys.length; index++) {
|
|
156
|
+
if (typeof keys[index] !== 'string') continue
|
|
157
|
+
const kind = exportTransport(url, keys[index])?.kind
|
|
158
|
+
if (kind === 'accessor' || kind === 'namespace') { managed = true; break }
|
|
159
|
+
}
|
|
160
|
+
if (!managed) return native
|
|
161
|
+
const target = Object.create(null)
|
|
162
|
+
for (let index = 0; index < keys.length; index++) {
|
|
163
|
+
const key = keys[index]
|
|
164
|
+
Object.defineProperty(target, key, typeof key === 'string'
|
|
165
|
+
? { value: undefined, enumerable: true, writable: true, configurable: false }
|
|
166
|
+
: Object.getOwnPropertyDescriptor(native, key))
|
|
167
|
+
}
|
|
168
|
+
Object.preventExtensions(target)
|
|
169
|
+
const read = key => typeof key === 'string' && Object.hasOwn(target, key) ? decode(url, key, native[key]) : native[key]
|
|
170
|
+
const namespace = new Proxy(target, {
|
|
171
|
+
get: (_, key) => read(key),
|
|
172
|
+
getOwnPropertyDescriptor(_, key) {
|
|
173
|
+
const descriptor = Object.getOwnPropertyDescriptor(target, key)
|
|
174
|
+
if (descriptor !== undefined && typeof key === 'string') descriptor.value = read(key)
|
|
175
|
+
return descriptor
|
|
176
|
+
},
|
|
177
|
+
set: () => false,
|
|
178
|
+
defineProperty(_, key, descriptor) {
|
|
179
|
+
descriptor = compilerDescriptors.descriptor(descriptor)
|
|
180
|
+
const current = Object.getOwnPropertyDescriptor(target, key)
|
|
181
|
+
if (current === undefined) return false
|
|
182
|
+
if (typeof key !== 'string') return Reflect.defineProperty(target, key, descriptor)
|
|
183
|
+
return !('get' in descriptor || 'set' in descriptor) && descriptor.configurable !== true
|
|
184
|
+
&& descriptor.enumerable !== false && descriptor.writable !== false
|
|
185
|
+
&& (!('value' in descriptor) || Object.is(descriptor.value, read(key)))
|
|
186
|
+
},
|
|
187
|
+
deleteProperty: (_, key) => !Object.hasOwn(target, key),
|
|
188
|
+
})
|
|
189
|
+
weakMapSet(namespaces, native, namespace)
|
|
190
|
+
weakMapSet(namespaces, namespace, namespace)
|
|
191
|
+
return namespace
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Import a non-then wrapper, then assimilate the source's real then exactly once. */
|
|
195
|
+
const nativeModuleRealm = {
|
|
196
|
+
Promise,
|
|
197
|
+
stringify: value => `${value}`,
|
|
198
|
+
importModule: (source, options) => import(source, options),
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function managedModuleImport(parentUrl, specifier, options, realm = nativeModuleRealm) {
|
|
202
|
+
return new realm.Promise((resolve, reject) => {
|
|
203
|
+
// Template conversion follows native import's ToString (including Symbols).
|
|
204
|
+
const source = realm.stringify(specifier)
|
|
205
|
+
const key = stringify([parentUrl, source])
|
|
206
|
+
let url = mapGet(adapterRequests, key)
|
|
207
|
+
if (url === undefined) {
|
|
208
|
+
url = `ptc-module:request/${++nextAdapter}`
|
|
209
|
+
mapSet(adapterRequests, key, url)
|
|
210
|
+
mapSet(adapters, url, { kind: 'request', parentUrl, source })
|
|
211
|
+
}
|
|
212
|
+
// Public synchronous resolve hooks run within the import expression. This
|
|
213
|
+
// scope includes hooks registered by user code after PTC's loader, while
|
|
214
|
+
// the cached wrapper later consumes the resolved edge without repeating it.
|
|
215
|
+
const dispose = registerModuleResolutionHook((request, context, nextResolve) => {
|
|
216
|
+
if (request !== url) return nextResolve(request, context)
|
|
217
|
+
const resolution = nextResolve(source, { ...context, parentURL: parentUrl })
|
|
218
|
+
const attributes = resolution.importAttributes ?? context.importAttributes
|
|
219
|
+
return { url: resolveManagedModuleAdapter(resolution, attributes), format: 'module',
|
|
220
|
+
importAttributes: attributes, shortCircuit: true }
|
|
221
|
+
})
|
|
222
|
+
try {
|
|
223
|
+
const promise = realm.importModule(url, options)
|
|
224
|
+
observeOwnedPromise(promise, wrapper => {
|
|
225
|
+
resolve(managedModuleNamespace(wrapper.url, wrapper.namespace))
|
|
226
|
+
}, reject)
|
|
227
|
+
} finally {
|
|
228
|
+
dispose()
|
|
229
|
+
}
|
|
230
|
+
})
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Private virtual entries preserve native import option validation and linking. */
|
|
234
|
+
export function managedModuleAdapter(url) { return mapGet(adapters, url) }
|
|
235
|
+
|
|
236
|
+
/** Reuse evaluated wrappers only after this call's native resolution succeeds. */
|
|
237
|
+
function resolveManagedModuleAdapter(resolution, attributes) {
|
|
238
|
+
const key = stringify([resolutionKey(resolution.url, attributes), resolution.format])
|
|
239
|
+
let url = mapGet(adapterModules, key)
|
|
240
|
+
if (url === undefined) {
|
|
241
|
+
url = `ptc-module:namespace/${++nextAdapter}`
|
|
242
|
+
mapSet(adapterModules, key, url)
|
|
243
|
+
mapSet(adapters, url, { kind: 'module', source: `${url}/source`, resolution, attributes })
|
|
244
|
+
}
|
|
245
|
+
return url
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function loadManagedModuleAdapter(url) {
|
|
249
|
+
const adapter = mapGet(adapters, url)
|
|
250
|
+
if (adapter?.kind !== 'module') return undefined
|
|
251
|
+
const entries = Object.entries(adapter.attributes ?? {})
|
|
252
|
+
let suffix = ''
|
|
253
|
+
for (let index = 0; index < entries.length; index++) {
|
|
254
|
+
suffix += `${index === 0 ? ' with {' : ','}${stringify(entries[index][0])}:${stringify(entries[index][1])}`
|
|
255
|
+
}
|
|
256
|
+
if (entries.length > 0) suffix += '}'
|
|
257
|
+
return { format: 'module', shortCircuit: true,
|
|
258
|
+
source: `import * as namespace from ${stringify(adapter.source)}${suffix};export {namespace};export const url=${stringify(adapter.resolution.url)};` }
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** Only PTC-provided require paths decode formally compiled ESM returns. */
|
|
262
|
+
export function managedRequire(parentUrl, nativeRequire) {
|
|
263
|
+
const acquire = (target, receiver, args) => {
|
|
264
|
+
let url
|
|
265
|
+
let value
|
|
266
|
+
const dispose = registerModuleResolutionHook((source, context, nextResolve) => {
|
|
267
|
+
const result = nextResolve(source, context)
|
|
268
|
+
if (source === args[0] && url === undefined) url = result.url
|
|
269
|
+
return result
|
|
270
|
+
})
|
|
271
|
+
try {
|
|
272
|
+
value = Reflect.apply(target, receiver, args)
|
|
273
|
+
} finally {
|
|
274
|
+
dispose()
|
|
275
|
+
}
|
|
276
|
+
// Node may satisfy a CommonJS relative-resolution cache hit without
|
|
277
|
+
// invoking resolve hooks. Its exact cached exports identity still proves
|
|
278
|
+
// the compiled URL; do not resolve or evaluate the request a second time.
|
|
279
|
+
if (url === undefined) {
|
|
280
|
+
mapForEach(modules, (record, candidate) => {
|
|
281
|
+
if (url !== undefined) return
|
|
282
|
+
const cached = record.interface !== undefined && startsWith(candidate, 'file:')
|
|
283
|
+
? nativeRequire.cache?.[fileURLToPath(candidate)] : undefined
|
|
284
|
+
if (record.interface !== undefined && cached !== undefined && cached.exports === value) {
|
|
285
|
+
url = candidate
|
|
286
|
+
}
|
|
287
|
+
})
|
|
288
|
+
}
|
|
289
|
+
const record = mapGet(modules, url)
|
|
290
|
+
const plan = record?.interface
|
|
291
|
+
if (plan === undefined) return value
|
|
292
|
+
return setHas(record.nativeKeys, 'module.exports')
|
|
293
|
+
? decode(url, 'module.exports', value) : managedModuleNamespace(url, value)
|
|
294
|
+
}
|
|
295
|
+
return new Proxy(nativeRequire, {
|
|
296
|
+
apply: acquire,
|
|
297
|
+
construct(target, args, newTarget) {
|
|
298
|
+
// Native require is a base function. Decode inside its construction frame
|
|
299
|
+
// so a primitive interop value retains native constructor return rules.
|
|
300
|
+
return Reflect.construct(function (...values) { return acquire(target, this, values) }, args, newTarget)
|
|
301
|
+
},
|
|
302
|
+
})
|
|
303
|
+
}
|