add ref for grabbing a function

This commit is contained in:
Chris Wanstrath 2025-11-08 00:58:17 -08:00
parent 409aa9c34e
commit 88434a932a
2 changed files with 13 additions and 0 deletions

View File

@ -40,6 +40,7 @@ export const globals = {
'var?': function (this: VM, v: string) {
return typeof v !== 'string' || this.scope.has(v)
},
ref: (fn: Function) => fn,
// type predicates
'string?': (v: any) => toValue(v).type === 'string',

View File

@ -77,3 +77,15 @@ describe('introspection', () => {
await expect(`describe 'hello'`).toEvaluateTo("#<string: \u001b[32m'hello\u001b[32m'\u001b[0m>", globals)
})
})
describe('ref', () => {
expect(`rnd = do x: true end; rnd | type`).toEvaluateTo('boolean')
expect(`rnd = do x: true end; ref rnd | type`).toEvaluateTo('function')
expect(`math.random | type`).toEvaluateTo('number')
expect(`ref math.random | type`).toEvaluateTo('native')
expect(`rnd = math.random; rnd | type`).toEvaluateTo('number')
expect(`rnd = ref math.random; rnd | type`).toEvaluateTo('number')
expect(`rnd = ref math.random; ref rnd | type`).toEvaluateTo('native')
})