Skip to content

Commit 2133bf5

Browse files
committed
type system
1 parent f4da030 commit 2133bf5

5 files changed

Lines changed: 202 additions & 90 deletions

File tree

frankenphp.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import (
3939
//_ "github.com/ianlancetaylor/cgosymbolizer"
4040

4141
"github.com/dunglas/mercure"
42-
"go.uber.org/zap"
4342
)
4443

4544
type contextKeyStruct struct{}
@@ -578,7 +577,7 @@ func go_is_context_done(threadIndex C.uintptr_t) C.bool {
578577
}
579578

580579
//export go_mercure_publish
581-
func go_mercure_publish(threadIndex C.uintptr_t, topics unsafe.Pointer, data unsafe.Pointer, private bool, id, typ unsafe.Pointer, retry uint64) (generatedID *C.zend_string, error C.short) {
580+
func go_mercure_publish(threadIndex C.uintptr_t, topics *C.struct__zval_struct, data unsafe.Pointer, private bool, id, typ unsafe.Pointer, retry uint64) (generatedID *C.zend_string, error C.short) {
582581
fc := phpThreads[threadIndex].getRequestContext()
583582

584583
if fc.mercureHub == nil {
@@ -597,19 +596,26 @@ func go_mercure_publish(threadIndex C.uintptr_t, topics unsafe.Pointer, data uns
597596
Private: private,
598597
}
599598

600-
ts := GoValue[string](topics)
601-
switch t := ts.(type) {
602-
case string:
603-
u.Topics = []string{t}
604-
case []string:
605-
u.Topics = t
599+
zvalType := C.zval_get_type(topics)
600+
switch zvalType {
601+
case C.IS_STRING:
602+
u.Topics = []string{GoString(unsafe.Pointer(topics))}
603+
case C.IS_ARRAY:
604+
ts, err := GoPackedArray[string](unsafe.Pointer(topics))
605+
if err != nil {
606+
logger.Error("invalid topics type", slog.String("error", err.Error()))
607+
608+
return nil, 1
609+
}
610+
611+
u.Topics = ts
606612
default:
607613
// Never happens as the function is called from C with proper types
608-
panic(fmt.Sprintf("invalid topics type %#v", t))
614+
panic("invalid topics type")
609615
}
610616

611617
if err := fc.mercureHub.Publish(u); err != nil {
612-
logger.Error("Unable to publish Mercure update", zap.Error(err))
618+
logger.Error("Unable to publish Mercure update", slog.String("error", err.Error()))
613619

614620
return nil, 2
615621
}

frankenphp_arginfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: cd534a8394f535a600bf45a333955d23b5154756 */
2+
* Stub hash: fcc86e17663887b089b79144e10ab3ca50ce3faa */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_frankenphp_handle_request, 0, 1, _IS_BOOL, 0)
55
ZEND_ARG_TYPE_INFO(0, callback, IS_CALLABLE, 0)
@@ -27,7 +27,7 @@ ZEND_END_ARG_INFO()
2727
#define arginfo_apache_response_headers arginfo_frankenphp_response_headers
2828

2929
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_mercure_publish, 0, 1, IS_STRING, 0)
30-
ZEND_ARG_TYPE_MASK(0, topics, MAY_BE_STRING|MAY_BE_ARRAY, NULL)
30+
ZEND_ARG_INFO(0, topics)
3131
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, data, IS_STRING, 0, "\'\'")
3232
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, private, _IS_BOOL, 0, "false")
3333
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, id, IS_STRING, 1, "null")

threadworker.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package frankenphp
44
import "C"
55
import (
66
"context"
7+
"fmt"
78
"log/slog"
89
"path/filepath"
910
"time"
@@ -245,7 +246,12 @@ func go_frankenphp_finish_worker_request(threadIndex C.uintptr_t, retval *C.zval
245246
thread := phpThreads[threadIndex]
246247
fc := thread.getRequestContext()
247248
if retval != nil {
248-
fc.handlerReturn = GoValue[any](unsafe.Pointer(retval))
249+
r, err := GoValue[any](unsafe.Pointer(retval))
250+
if err != nil {
251+
logger.Error(fmt.Sprintf("cannot convert return value: %s", err))
252+
}
253+
254+
fc.handlerReturn = r
249255
}
250256

251257
fc.closeContext()

0 commit comments

Comments
 (0)