Skip to content

Commit

Permalink
added builitns in context
Browse files Browse the repository at this point in the history
  • Loading branch information
refaktor committed Mar 5, 2024
1 parent 1f9eada commit 0f829ff
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -7058,7 +7058,7 @@ func RegisterBuiltins(ps *env.ProgramState) {
RegisterBuiltins2(Builtins_ps, ps, "ps")
RegisterBuiltins2(Builtins_json, ps, "json")
RegisterBuiltins2(Builtins_stackless, ps, "stackless")
RegisterBuiltins2(Builtins_eyr, ps, "eyr")
RegisterBuiltinsInContext(Builtins_eyr, ps, "eyr")
RegisterBuiltins2(Builtins_conversion, ps, "conversion")
RegisterBuiltins2(Builtins_http, ps, "http")
RegisterBuiltins2(Builtins_crypto, ps, "crypto")
Expand All @@ -7084,7 +7084,7 @@ func RegisterBuiltins(ps *env.ProgramState) {
// RegisterBuiltins2(Builtins_cayley, ps, "cayley")
}

var BuiltinNames map[string]int
var BuiltinNames map[string]int // TODO --- this looks like some hanging global ... it should move to ProgramState, it doesn't even really work with contrib and external probably

func RegisterBuiltins2(builtins map[string]*env.Builtin, ps *env.ProgramState, name string) {
BuiltinNames[name] = len(builtins)
Expand All @@ -7094,6 +7094,24 @@ func RegisterBuiltins2(builtins map[string]*env.Builtin, ps *env.ProgramState, n
}
}

func RegisterBuiltinsInContext(builtins map[string]*env.Builtin, ps *env.ProgramState, name string) {
BuiltinNames[name] = len(builtins)

ctx := ps.Ctx
ps.Ctx = env.NewEnv(ps.Ctx) // make new context with no parent

for k, v := range builtins {
bu := env.NewBuiltin(v.Fn, v.Argsn, v.AcceptFailure, v.Pure, v.Doc)
registerBuiltin(ps, k, *bu)
}
newctx := ps.Ctx
ps.Ctx = ctx

wordIdx := ps.Idx.IndexWord(name)
ps.Ctx.Set(wordIdx, *newctx)

Check failure on line 7112 in evaldo/builtins.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)
}

func registerBuiltin(ps *env.ProgramState, word string, builtin env.Builtin) {
// indexWord
// TODO -- this with string separator is a temporary way of how we define generic builtins
Expand Down

0 comments on commit 0f829ff

Please sign in to comment.