Skip to content

Commit 929faa1

Browse files
committed
Implemented Await and LoadFromMemory in Go port, add tests and make it work properly.
1 parent 56c3846 commit 929faa1

File tree

8 files changed

+430
-70
lines changed

8 files changed

+430
-70
lines changed

source/metacall/include/metacall/metacall.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ struct metacall_initialize_configuration_type
6161
// can use a weak API in order to implement this successfully
6262
};
6363

64+
typedef void *(*metacall_await_callback)(void *, void *);
65+
66+
typedef struct metacall_await_callbacks_type
67+
{
68+
metacall_await_callback resolve;
69+
metacall_await_callback reject;
70+
71+
} metacall_await_callbacks;
72+
6473
/* -- Global Variables -- */
6574

6675
METACALL_API extern void *metacall_null_args[1];
@@ -881,6 +890,30 @@ METACALL_API void *metacallfv_await(void *func, void *args[], void *(*resolve_ca
881890
*/
882891
METACALL_API void *metacallfv_await_s(void *func, void *args[], size_t size, void *(*resolve_callback)(void *, void *), void *(*reject_callback)(void *, void *), void *data);
883892

893+
/**
894+
* @brief
895+
* Call an asynchronous function anonymously by value array @args and function @func (offered without function pointers for languages without support to function pointers)
896+
*
897+
* @param[in] func
898+
* Reference to function to be called
899+
*
900+
* @param[in] args
901+
* Array of pointers to values
902+
*
903+
* @param[in] size
904+
* Number of elements of the array @args
905+
*
906+
* @param[in] cb
907+
* Pointer to struct containing the function pointers to reject and resolve that will be executed when task completion or error
908+
*
909+
* @param[in] data
910+
* Pointer to a context that will act as a closure for the chain
911+
*
912+
* @return
913+
* Pointer to value containing the result of the call returned by @resolve_callback or @reject_callback wrapped in a future
914+
*/
915+
METACALL_API void *metacallfv_await_struct_s(void *func, void *args[], size_t size, metacall_await_callbacks cb, void *data);
916+
884917
/**
885918
* @brief
886919
* Call an asynchronous function anonymously by value map (@keys -> @values) and function @func

source/metacall/source/metacall.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,11 @@ void *metacallfv_await_s(void *func, void *args[], size_t size, void *(*resolve_
14211421
return function_await(func, args, size, resolve_callback, reject_callback, data);
14221422
}
14231423

1424+
void *metacallfv_await_struct_s(void *func, void *args[], size_t size, metacall_await_callbacks cb, void *data)
1425+
{
1426+
return function_await(func, args, size, cb.resolve, cb.reject, data);
1427+
}
1428+
14241429
void *metacallfmv_await(void *func, void *keys[], void *values[], void *(*resolve_callback)(void *, void *), void *(*reject_callback)(void *, void *), void *data)
14251430
{
14261431
function f = (function)func;

source/ports/go_port/source/README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@ This project implements a wrapper of MetaCall API for Go.
66

77
## Examples
88

9+
`test.ts`:
10+
```ts
11+
export function concat(left: string, right: string): string {
12+
return left + right;
13+
}
14+
```
15+
16+
`main.go`:
917
```go
18+
package main
19+
1020
import (
1121
metacall "github.com/metacall/core/source/ports/go_port/source"
1222
"os"
@@ -21,14 +31,14 @@ func main() {
2131

2232
defer metacall.Destroy()
2333

24-
scripts := []string{ "test.mock" }
34+
scripts := []string{ "test.ts" }
2535

26-
if err := metacall.LoadFromFile("mock", scripts); err != nil {
36+
if err := metacall.LoadFromFile("ts", scripts); err != nil {
2737
fmt.Println(err)
2838
return
2939
}
3040

31-
ret, err := metacall.Call("three_str", "e", "f", "g")
41+
ret, err := metacall.Call("concat", "hello", "world")
3242

3343
if err != nil {
3444
fmt.Println(err)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* MetaCall Go Port by Parra Studios
3+
* A frontend for Go language bindings in MetaCall.
4+
*
5+
* Copyright (C) 2016 - 2021 Vicente Eduardo Ferrer Garcia <[email protected]>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
package metacall
22+
23+
/*
24+
extern void *goResolve(void *, void *);
25+
extern void *goReject(void *, void *);
26+
27+
void *resolveCgo(void *v, void *ctx) {
28+
return goResolve(v, ctx);
29+
}
30+
31+
void *rejectCgo(void *v, void *ctx) {
32+
return goReject(v, ctx);
33+
}
34+
*/
35+
import "C"

source/ports/go_port/source/go.sum

Whitespace-only changes.

0 commit comments

Comments
 (0)