-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
59 lines (51 loc) · 2.17 KB
/
main.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
// Copyright 2022 Harikrishnan Balagopal
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"strings"
"syscall/js"
"go.starlark.net/starlark"
)
func getStarlarkRunner() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
if len(args) != 1 {
err := fmt.Errorf("Error: expected there to be exactly one argument with the source code. Actual len(args) %d args %+v", len(args), args)
return map[string]interface{}{"error": err.Error()}
}
starlark_code := args[0].String()
output := strings.Builder{}
thread := &starlark.Thread{Name: "js-go-starlark-thread", Print: func(_ *starlark.Thread, msg string) {
output.WriteString(msg + "\n")
}}
globals, err := starlark.ExecFile(thread, "", starlark_code, nil)
if err != nil {
err := fmt.Errorf("Error: failed to evaluate the starlark code. Error: %q", err)
return map[string]interface{}{"error": err.Error()}
}
mainFn, ok := globals["main"]
if !ok {
err := fmt.Errorf("Error: the main function is missing from the starlark code.")
return map[string]interface{}{"error": err.Error()}
}
// Call the Starlark main function from Go.
if _, err := starlark.Call(thread, mainFn, nil, nil); err != nil {
err := fmt.Errorf("Error: failed to execute the starlark code. Error: %q", err)
return map[string]interface{}{"error": err.Error()}
}
return map[string]interface{}{"message": output.String()}
})
}
func main() {
js.Global().Set("run_starlark_code", getStarlarkRunner())
fmt.Println("the run_starlark_code has been added to the javascript globals (window object)")
<-make(chan bool) // keep thread running forever so Javascript can call the function we exported.
}