-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
load-native-functions-into-context.ts
33 lines (30 loc) 路 1.17 KB
/
load-native-functions-into-context.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
import * as _repl from 'repl';
import { ReplContext } from './repl-context';
import { ReplFunction } from './repl-function';
import { ReplFunctionClass } from './repl.interfaces';
export function loadNativeFunctionsIntoContext(
replServerContext: _repl.REPLServer['context'],
replContext: ReplContext,
) {
const registerFunctionToReplServerContext = (
nativeFunction: InstanceType<ReplFunctionClass>,
): void => {
// Bind the method to REPL's context:
replServerContext[nativeFunction.fnDefinition.name] =
nativeFunction.action.bind(nativeFunction);
// Load the help trigger as a `help` getter on each native function:
const functionBoundRef: ReplFunction['action'] =
replServerContext[nativeFunction.fnDefinition.name];
Object.defineProperty(functionBoundRef, 'help', {
enumerable: false,
configurable: false,
get: () =>
// Dynamically builds the help message as will unlikely to be called
// several times.
replContext.writeToStdout(nativeFunction.makeHelpMessage()),
});
};
for (const [, nativeFunction] of replContext.nativeFunctions) {
registerFunctionToReplServerContext(nativeFunction);
}
}