-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
100 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,69 @@ | ||
package lithia | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
extdocs "github.com/vknabel/lithia/external/docs" | ||
extfs "github.com/vknabel/lithia/external/fs" | ||
extos "github.com/vknabel/lithia/external/os" | ||
extrx "github.com/vknabel/lithia/external/rx" | ||
"github.com/vknabel/lithia/runtime" | ||
"github.com/vknabel/lithia/world" | ||
) | ||
|
||
func NewDefaultInterpreter(referenceFile string, importRoots ...string) *runtime.Interpreter { | ||
inter := runtime.NewIsolatedInterpreter(referenceFile, importRoots...) | ||
inter.ExternalDefinitions["os"] = extos.New(inter) | ||
inter.ExternalDefinitions["rx"] = extrx.New(inter) | ||
inter.ExternalDefinitions["docs"] = extdocs.New(inter) | ||
inter.ExternalDefinitions["fs"] = extfs.New(inter) | ||
return inter | ||
func NewDefaultInterpreter(referenceFile string, importRoots ...string) (*runtime.Interpreter, context.Context) { | ||
ctx := context.Background() | ||
if timeout, ok := world.Current.Env.LookupEnv("LITHIA_TIMEOUT"); ok { | ||
duration, err := time.ParseDuration(timeout) | ||
if err != nil { | ||
fmt.Fprint(world.Current.Stderr, err) | ||
world.Current.Env.Exit(1) | ||
} | ||
var cancel context.CancelFunc | ||
ctx, cancel = context.WithTimeout(ctx, duration) | ||
|
||
go func() { | ||
<-time.After(duration) | ||
cancel() | ||
}() | ||
} | ||
inter := runtime.NewIsolatedInterpreter(ctx, referenceFile, importRoots...) | ||
|
||
externalDefinitions := "*" | ||
if defs, ok := world.Current.Env.LookupEnv("LITHIA_EXTERNAL_DEFINITIONS"); ok { | ||
externalDefinitions = defs | ||
} | ||
var whitelistExternals []string | ||
if externalDefinitions != "*" { | ||
whitelistExternals = strings.Split(externalDefinitions, ",") | ||
} | ||
|
||
if allowsWhitelistExternal(whitelistExternals, "os") { | ||
inter.ExternalDefinitions["os"] = extos.New(inter) | ||
} | ||
if allowsWhitelistExternal(whitelistExternals, "rx") { | ||
inter.ExternalDefinitions["rx"] = extrx.New(inter) | ||
} | ||
if allowsWhitelistExternal(whitelistExternals, "docs") { | ||
inter.ExternalDefinitions["docs"] = extdocs.New(inter) | ||
} | ||
if allowsWhitelistExternal(whitelistExternals, "fs") { | ||
inter.ExternalDefinitions["fs"] = extfs.New(inter) | ||
} | ||
return inter, ctx | ||
} | ||
|
||
func allowsWhitelistExternal(whitelistExternals []string, name string) bool { | ||
if whitelistExternals == nil { | ||
return true | ||
} | ||
for _, allowed := range whitelistExternals { | ||
if allowed == name { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,33 @@ | ||
package runtime | ||
|
||
import "sync" | ||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
type LazyEvaluationCache struct { | ||
ctx context.Context | ||
once *sync.Once | ||
value RuntimeValue | ||
err *RuntimeError | ||
} | ||
|
||
func NewLazyEvaluationCache() *LazyEvaluationCache { | ||
return &LazyEvaluationCache{once: &sync.Once{}} | ||
func NewLazyEvaluationCache(ctx context.Context) *LazyEvaluationCache { | ||
return &LazyEvaluationCache{ctx: ctx, once: &sync.Once{}} | ||
} | ||
|
||
func (c *LazyEvaluationCache) Evaluate(f func() (RuntimeValue, *RuntimeError)) (RuntimeValue, *RuntimeError) { | ||
c.once.Do(func() { | ||
c.value, c.err = f() | ||
done := make(chan struct{}) | ||
go func() { | ||
c.value, c.err = f() | ||
close(done) | ||
}() | ||
select { | ||
case <-c.ctx.Done(): | ||
c.err = NewRuntimeError(c.ctx.Err()) | ||
case <-done: | ||
} | ||
}) | ||
return c.value, c.err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters