-
Notifications
You must be signed in to change notification settings - Fork 5
/
extensions.go
89 lines (74 loc) · 2.38 KB
/
extensions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package js
import (
"fmt"
"github.com/dop251/goja"
"github.com/tliron/kutil/ard"
"github.com/tliron/kutil/js"
"github.com/tliron/prudence/platform"
)
func newExtensions(arguments map[string]string) []js.Extension {
var extensions []js.Extension
extensions = append(extensions, js.Extension{
Name: "bind",
Create: js.CreateLateBindExtension,
})
extensions = append(extensions, js.Extension{
Name: "prudence",
Create: newPrudenceExtensionCreator(arguments),
})
extensions = append(extensions, js.Extension{
Name: "console",
Create: func(context *js.Context) goja.Value {
return context.Environment.Runtime.ToValue(ConsoleAPI{})
},
})
platform.OnAPIs(func(name string, api interface{}) bool {
extensions = append(extensions, js.Extension{
Name: name,
Create: func(context *js.Context) goja.Value {
return context.Environment.Runtime.ToValue(api)
},
})
return true
})
return extensions
}
func newPrudenceExtensionCreator(arguments map[string]string) js.CreateExtensionFunc {
return func(context *js.Context) goja.Value {
prudence := context.Environment.Runtime.NewObject()
// Copy API
prudence_ := context.Environment.Runtime.ToValue(NewPrudenceAPI(context.Environment.URLContext, context, arguments)).ToObject(context.Environment.Runtime)
for _, key := range prudence_.Keys() {
prudence.Set(key, prudence_.Get(key))
}
// Globals
prudence.Set("globals", Globals.NewDynamicObject(context.Environment.Runtime))
// Type constructors
platform.OnTypes(func(type_ string, create platform.CreateFunc) bool {
prudence.Set(type_, newTypeConstructor(create, context))
return true
})
return prudence
}
}
func newTypeConstructor(create platform.CreateFunc, context *js.Context) func(constructor goja.ConstructorCall) *goja.Object {
runtime := context.Environment.Runtime
// goja constructor signature
return func(constructor goja.ConstructorCall) *goja.Object {
var config ard.StringMap
if len(constructor.Arguments) > 0 {
config_ := constructor.Arguments[0].Export()
var ok bool
if config, ok = config_.(ard.StringMap); !ok {
panic(runtime.NewGoError(fmt.Errorf("invalid \"config\" argument: %v", config_)))
}
} else {
config = make(ard.StringMap)
}
if object, err := create(config, context); err == nil {
return runtime.ToValue(object).ToObject(runtime)
} else {
panic(runtime.NewGoError(err))
}
}
}