Skip to content

Commit 1419e9b

Browse files
committed
Implement calls in golang port.
1 parent 58c86fd commit 1419e9b

File tree

2 files changed

+26
-53
lines changed

2 files changed

+26
-53
lines changed

source/ports/go_port/source/README.md

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ This project implements a wrapper of MetaCall API for Go.
44

55
## Examples
66

7-
Non-thread safe:
87
```go
98
import (
10-
"github.com/metacall/core/source/ports/go_port/source"
9+
metacall "github.com/metacall/core/source/ports/go_port/source"
1110
"os"
1211
)
1312

@@ -39,39 +38,3 @@ func main() {
3938
}
4039
}
4140
```
42-
43-
Thread safe:
44-
```go
45-
import (
46-
"github.com/metacall/core/source/ports/go_port/source"
47-
"os"
48-
)
49-
50-
func main() {
51-
52-
if err := metacall.InitializeSafe(); err != nil {
53-
fmt.Println(err)
54-
os.Exit(1)
55-
}
56-
57-
defer metacall.DestroySafe()
58-
59-
scripts := []string{"test.mock"}
60-
61-
if err := metacall.LoadFromFileSafe("mock", scripts); err != nil {
62-
fmt.Println(err)
63-
return
64-
}
65-
66-
ret, err := metacall.CallSafe("three_str", "e", "f", "g")
67-
68-
if err != nil {
69-
fmt.Println(err)
70-
return
71-
}
72-
73-
if str, ok := ret.(string); ok {
74-
fmt.Println(str)
75-
}
76-
}
77-
```

source/ports/go_port/source/go_port.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ type callReturnSafeWork struct {
2626

2727
type callSafeWork struct {
2828
function string
29-
size int
3029
args []interface{}
3130
ret chan callReturnSafeWork
3231
}
@@ -38,7 +37,7 @@ var toggle chan struct{}
3837
var lock sync.Mutex
3938
var wg sync.WaitGroup
4039

41-
func Initialize() error {
40+
func InitializeUnsafe() error {
4241
// TODO: Remove this once go loader is implemented
4342
if result := int(C.metacall_initialize()); result != 0 {
4443
return fmt.Errorf("initializing MetaCall (error code %d)", result)
@@ -48,7 +47,7 @@ func Initialize() error {
4847
}
4948

5049
// Start starts the metacall adapter
51-
func InitializeSafe() error {
50+
func Initialize() error {
5251
lock.Lock()
5352
defer lock.Unlock()
5453

@@ -65,7 +64,7 @@ func InitializeSafe() error {
6564
runtime.LockOSThread()
6665

6766
// Initialize MetaCall
68-
if err := Initialize(); err != nil {
67+
if err := InitializeUnsafe(); err != nil {
6968
initErr <- err
7069
return
7170
}
@@ -76,11 +75,21 @@ func InitializeSafe() error {
7675
select {
7776
case <-toggle:
7877
// Shutdown
79-
Destroy()
78+
DestroyUnsafe()
8079
return
8180
case w := <-queue:
82-
// Send work
83-
fmt.Printf("DEBUG: send work: %#v\n", w)
81+
switch v := w.(type) {
82+
case loadFromFileSafeWork:
83+
{
84+
err := LoadFromFileUnsafe(v.tag, v.scripts)
85+
v.err <- err
86+
}
87+
case callSafeWork:
88+
{
89+
value, err := CallUnsafe(v.function, v.args...)
90+
v.ret <- callReturnSafeWork{value, err}
91+
}
92+
}
8493
wg.Done()
8594
}
8695
}
@@ -89,7 +98,7 @@ func InitializeSafe() error {
8998
return <-initErr
9099
}
91100

92-
func LoadFromFile(tag string, scripts []string) error {
101+
func LoadFromFileUnsafe(tag string, scripts []string) error {
93102
size := len(scripts)
94103

95104
cTag := C.CString(tag)
@@ -112,7 +121,7 @@ func LoadFromFile(tag string, scripts []string) error {
112121
return nil
113122
}
114123

115-
func LoadFromFileSafe(tag string, scripts []string) error {
124+
func LoadFromFile(tag string, scripts []string) error {
116125
result := make(chan error, 1)
117126
w := loadFromFileSafeWork{
118127
tag,
@@ -125,7 +134,7 @@ func LoadFromFileSafe(tag string, scripts []string) error {
125134
return <-result
126135
}
127136

128-
func Call(function string, args ...interface{}) (interface{}, error) {
137+
func CallUnsafe(function string, args ...interface{}) (interface{}, error) {
129138

130139
cFunction := C.CString(function)
131140
defer C.free(unsafe.Pointer(cFunction))
@@ -210,26 +219,27 @@ func Call(function string, args ...interface{}) (interface{}, error) {
210219
}
211220

212221
// Call sends work and blocks until it's processed
213-
func CallSafe(function string, args ...interface{}) (interface{}, error) {
222+
func Call(function string, args ...interface{}) (interface{}, error) {
214223
ret := make(chan callReturnSafeWork, 1)
215224
w := callSafeWork{
216225
function: function,
217-
size: len(args),
218226
args: args,
219227
ret: ret,
220228
}
221229
wg.Add(1)
222230
queue <- w
223231

224-
return nil, nil // TODO
232+
result := <-ret
233+
234+
return result.value, result.err
225235
}
226236

227-
func Destroy() {
237+
func DestroyUnsafe() {
228238
C.metacall_destroy()
229239
}
230240

231241
// Shutdown disables the metacall adapter waiting for all calls to complete
232-
func DestroySafe() {
242+
func Destroy() {
233243
lock.Lock()
234244
close(toggle)
235245
toggle = nil

0 commit comments

Comments
 (0)