Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/core/dylink/src/stub.ts
1067 views
1
import debug from "debug";
2
const log = debug("stub");
3
const logUse = debug("stub:use"); // log all use of the stub
4
const logFirst = debug("stub:first"); // log first use of the stub
5
6
export default function stubProxy(
7
env,
8
functionViaPointer: (ptr) => Function | undefined | null,
9
type?: "silent" | "warn"
10
) {
11
return new Proxy(env, {
12
get(target, key) {
13
if (key in target) {
14
return Reflect.get(target, key);
15
}
16
const f = functionViaPointer(key);
17
if (f != null) {
18
log("using function via pointer for ", key);
19
return f;
20
}
21
if (type == "warn") {
22
console.warn(
23
`\n* WARNING: creating UNSAFE stub for ${String(
24
key
25
)}. Please fix ASAP!`
26
);
27
}
28
if (logUse.enabled || logFirst.enabled) {
29
return (...args) => {
30
logStubUse(key, args);
31
return 0;
32
};
33
} else {
34
// faster to not trace or even check, obviously.
35
return () => 0;
36
}
37
},
38
});
39
}
40
41
const stubUsed = new Set<string>([]);
42
function logStubUse(functionName, args) {
43
logUse("WARNING: using stub", functionName, args);
44
if (logFirst.enabled) {
45
if (stubUsed.has(functionName)) return;
46
stubUsed.add(functionName);
47
}
48
logFirst("WARNING: first use of stub", functionName, args);
49
}
50
51