@@ -61,6 +61,7 @@ export interface FunctionTable {
61
61
62
62
export class Runtime {
63
63
_interpreter : TreeInterpreter ;
64
+ _functionTable : FunctionTable ;
64
65
TYPE_NAME_TABLE : { [ InputArgument : number ] : string } = {
65
66
[ InputArgument . TYPE_NUMBER ] : 'number' ,
66
67
[ InputArgument . TYPE_ANY ] : 'any' ,
@@ -78,24 +79,25 @@ export class Runtime {
78
79
79
80
constructor ( interpreter : TreeInterpreter ) {
80
81
this . _interpreter = interpreter ;
82
+ this . _functionTable = this . functionTable
81
83
}
82
84
83
85
registerFunction (
84
86
name : string ,
85
87
customFunction : RuntimeFunction < ( JSONValue | ExpressionNode ) [ ] , JSONValue > ,
86
88
signature : InputSignature [ ] ,
87
89
) : void {
88
- if ( name in this . functionTable ) {
90
+ if ( name in this . _functionTable ) {
89
91
throw new Error ( `Function already defined: ${ name } ()` ) ;
90
92
}
91
- this . functionTable [ name ] = {
93
+ this . _functionTable [ name ] = {
92
94
_func : customFunction . bind ( this ) ,
93
95
_signature : signature ,
94
96
} ;
95
97
}
96
98
97
99
callFunction ( name : string , resolvedArgs : ( JSONValue | ExpressionNode ) [ ] ) : JSONValue {
98
- const functionEntry = this . functionTable [ name ] ;
100
+ const functionEntry = this . _functionTable [ name ] ;
99
101
if ( functionEntry === undefined ) {
100
102
throw new Error ( `Unknown function: ${ name } ()` ) ;
101
103
}
@@ -114,7 +116,7 @@ export class Runtime {
114
116
private validateArgs ( name : string , args : ( JSONValue | ExpressionNode ) [ ] , signature : InputSignature [ ] ) : void {
115
117
let pluralized : boolean ;
116
118
this . validateInputSignatures ( name , signature ) ;
117
- const numberOfRequiredArgs = signature . filter ( argSignature => ! argSignature . optional ?? false ) . length ;
119
+ const numberOfRequiredArgs = signature . filter ( argSignature => ! ( argSignature . optional ?? false ) ) . length ;
118
120
const lastArgIsVariadic = signature [ signature . length - 1 ] ?. variadic ?? false ;
119
121
const tooFewArgs = args . length < numberOfRequiredArgs ;
120
122
const tooManyArgs = args . length > signature . length ;
0 commit comments