-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathLoaderRegistry.kt
69 lines (63 loc) · 2.43 KB
/
LoaderRegistry.kt
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
/*
* Copyright (c) 2024 Elide Technologies, Inc.
*
* Licensed under the MIT license (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://opensource.org/license/mit/
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under the License.
*/
package elide.runtime.gvm.loader
import com.oracle.truffle.js.runtime.JSRealm
import com.oracle.truffle.js.runtime.objects.JSModuleLoader
import java.lang.ref.WeakReference
import java.util.LinkedList
import java.util.concurrent.ConcurrentHashMap
/**
* # Loader Registry
*
* Static utility for registering [JSModuleLoader]-compliant implementations, and similar objects for other languages;
* these registered objects are consulted from delegated Elide types.
*/
public object LoaderRegistry {
// Registered JavaScript module loaders.
@JvmStatic private val rootEsLoaders = LinkedList<JSModuleLoader>()
// Registered JavaScript Realm-specific module loaders.
@JvmStatic private val realmBoundEsLoaders = ConcurrentHashMap<JSRealm, LinkedList<WeakReference<JSModuleLoader>>>()
/**
* Register a JavaScript module loader.
*
* @param loader The JavaScript module loader to register.
*/
@JvmStatic public fun register(loader: JSModuleLoader) {
rootEsLoaders.add(loader)
}
/**
* Register a JavaScript module loader.
*
* @param realm JavaScript realm to bind this loader to.
* @param loader The JavaScript module loader to register.
*/
@JvmStatic public fun register(realm: JSRealm, loader: JSModuleLoader) {
val bound = realmBoundEsLoaders.computeIfAbsent(realm) { LinkedList() }
bound.add(WeakReference(loader))
}
/**
* Mount the provided module [loader] forcibly as the main loader for a given JavaScript [realm].
*
* @param realm The JavaScript realm to mount the loader to.
* @param loader The JavaScript module loader to mount.
*/
@JvmStatic public fun mountPrimary(realm: JSRealm, loader: JSModuleLoader) {
if (realm.parent == null) {
register(loader)
}
register(realm, loader) // associate with realm, too
JSRealmPatcher.overrideLoader<JSModuleLoader, JSModuleLoader>(realm) {
loader
}
}
}