mirror of
https://github.com/robertkrimen/otto
synced 2025-09-28 18:45:22 +08:00
Add registry package
Automatic inclusion of source on import
This commit is contained in:
parent
0b82701409
commit
5958af6914
1
Makefile
1
Makefile
|
@ -21,6 +21,7 @@ TEST := -v --run OttoError
|
|||
TEST := .
|
||||
|
||||
test:
|
||||
go test -i
|
||||
go test $(TEST)
|
||||
|
||||
assets:
|
||||
|
|
6
otto.go
6
otto.go
|
@ -74,6 +74,7 @@ package otto
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/robertkrimen/otto/registry"
|
||||
)
|
||||
|
||||
// Otto is the representation of the JavaScript runtime. Each instance of Otto has a self-contained namespace.
|
||||
|
@ -87,6 +88,11 @@ func New() *Otto {
|
|||
runtime: newContext(),
|
||||
}
|
||||
self.Set("console", self.runtime.newConsole())
|
||||
|
||||
registry.Apply(func(entry registry.Entry){
|
||||
self.Run(entry.Source())
|
||||
})
|
||||
|
||||
return self
|
||||
}
|
||||
|
||||
|
|
42
registry/registry.go
Normal file
42
registry/registry.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
package registry
|
||||
|
||||
var registry []*Entry = make([]*Entry, 0)
|
||||
|
||||
type Entry struct {
|
||||
active bool
|
||||
source string
|
||||
}
|
||||
|
||||
func newEntry(source string) *Entry {
|
||||
return &Entry{
|
||||
active: true,
|
||||
source: source,
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Entry) Enable() {
|
||||
self.active = true
|
||||
}
|
||||
|
||||
func (self *Entry) Disable() {
|
||||
self.active = false
|
||||
}
|
||||
|
||||
func (self Entry) Source() string {
|
||||
return self.source
|
||||
}
|
||||
|
||||
func Apply(callback func(Entry)) {
|
||||
for _, entry := range registry {
|
||||
if !entry.active {
|
||||
continue
|
||||
}
|
||||
callback(*entry)
|
||||
}
|
||||
}
|
||||
|
||||
func Register(source string) *Entry {
|
||||
entry := newEntry(source)
|
||||
registry = append(registry, entry)
|
||||
return entry
|
||||
}
|
|
@ -7,6 +7,20 @@ https://github.com/documentcloud/underscore
|
|||
*/
|
||||
package underscore
|
||||
|
||||
import (
|
||||
"github.com/robertkrimen/otto/registry"
|
||||
)
|
||||
|
||||
var entry *registry.Entry = registry.Register(Source())
|
||||
|
||||
func Enable() {
|
||||
entry.Enable()
|
||||
}
|
||||
|
||||
func Disable() {
|
||||
entry.Disable()
|
||||
}
|
||||
|
||||
// Source returns the underscore source.
|
||||
func Source() string {
|
||||
return string(underscore())
|
||||
|
|
|
@ -6,6 +6,10 @@ import (
|
|||
"github.com/robertkrimen/otto/underscore"
|
||||
)
|
||||
|
||||
func init() {
|
||||
underscore.Disable()
|
||||
}
|
||||
|
||||
func underscoreTest() func(string, ... interface{}) Value {
|
||||
Otto, test := runTestWithOtto()
|
||||
Otto.Run(underscore.Source())
|
||||
|
|
Loading…
Reference in New Issue
Block a user