refactor: use unsafe.Pointer

This commit is contained in:
Kévin Dunglas
2025-10-08 23:31:15 +02:00
parent 5e50345c31
commit 8a0b9c1beb
4 changed files with 39 additions and 43 deletions

View File

@@ -1,12 +1,12 @@
package extgen
import (
"github.com/stretchr/testify/require"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClassParser(t *testing.T) {
@@ -138,7 +138,7 @@ func SetUserAge(u *UserStruct, age int64) {
}
//export_php:method User::getInfo(string $prefix = "User"): string
func GetUserInfo(u UserStruct, prefix *C.zend_string) unsafe.Pointer {
func GetUserInfo(u UserStruct, prefix unsafe.Pointer) unsafe.Pointer {
return nil
}`)
@@ -432,7 +432,7 @@ type TestClass struct {
}
//export_php:method TestClass::arrayReturn(string $name): array
func (tc *TestClass) arrayReturn(name *C.zend_string) any {
func (tc *TestClass) arrayReturn(name unsafe.Pointer) any {
return []string{"result"}
}`,
expectedClasses: 1,
@@ -449,7 +449,7 @@ type TestClass struct {
}
//export_php:method TestClass::objectReturn(string $name): object
func (tc *TestClass) objectReturn(name *C.zend_string) any {
func (tc *TestClass) objectReturn(name unsafe.Pointer) any {
return map[string]any{"key": "value"}
}`,
expectedClasses: 1,
@@ -466,7 +466,7 @@ type TestClass struct {
}
//export_php:method TestClass::validMethod(string $name, int $count, float $rate, bool $active): string
func validMethod(tc *TestClass, name *C.zend_string, count int64, rate float64, active bool) unsafe.Pointer {
func validMethod(tc *TestClass, name unsafe.Pointer, count int64, rate float64, active bool) unsafe.Pointer {
return nil
}`,
expectedClasses: 1,
@@ -483,7 +483,7 @@ type TestClass struct {
}
//export_php:method TestClass::voidMethod(string $message): void
func voidMethod(tc *TestClass, message *C.zend_string) {
func voidMethod(tc *TestClass, message unsafe.Pointer) {
// Do something
}`,
expectedClasses: 1,
@@ -528,7 +528,7 @@ type TestClass struct {
}
//export_php:method TestClass::countMismatch(string $name, int $count): string
func (tc *TestClass) countMismatch(name *C.zend_string) unsafe.Pointer {
func (tc *TestClass) countMismatch(name unsafe.Pointer) unsafe.Pointer {
return nil
}`,
expectedClasses: 1,
@@ -545,7 +545,7 @@ type TestClass struct {
}
//export_php:method TestClass::typeMismatch(string $name, int $count): string
func (tc *TestClass) typeMismatch(name *C.zend_string, count string) unsafe.Pointer {
func (tc *TestClass) typeMismatch(name unsafe.Pointer, count string) unsafe.Pointer {
return nil
}`,
expectedClasses: 1,
@@ -562,7 +562,7 @@ type TestClass struct {
}
//export_php:method TestClass::returnMismatch(string $name): int
func (tc *TestClass) returnMismatch(name *C.zend_string) string {
func (tc *TestClass) returnMismatch(name unsafe.Pointer) string {
return ""
}`,
expectedClasses: 1,
@@ -579,7 +579,7 @@ type TestClass struct {
}
//export_php:method TestClass::validMatch(string $name, int $count): string
func validMatch(tc *TestClass, name *C.zend_string, count int64) unsafe.Pointer {
func validMatch(tc *TestClass, name unsafe.Pointer, count int64) unsafe.Pointer {
return nil
}`,
expectedClasses: 1,

View File

@@ -1,12 +1,12 @@
package extgen
import (
"github.com/stretchr/testify/require"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFunctionParser(t *testing.T) {
@@ -20,7 +20,7 @@ func TestFunctionParser(t *testing.T) {
input: `package main
//export_php:function testFunc(string $name): string
func testFunc(name *C.zend_string) unsafe.Pointer {
func testFunc(name unsafe.Pointer) unsafe.Pointer {
return String("Hello " + CStringToGoString(name))
}`,
expected: 1,
@@ -35,7 +35,7 @@ func func1(a int64) int64 {
}
//export_php:function func2(string $b): string
func func2(b *C.zend_string) unsafe.Pointer {
func func2(b unsafe.Pointer) unsafe.Pointer {
return String("processed: " + CStringToGoString(b))
}`,
expected: 2,
@@ -54,7 +54,7 @@ func regularFunc() {
input: `package main
//export_php:function phpFunc(string $data): string
func phpFunc(data *C.zend_string) unsafe.Pointer {
func phpFunc(data unsafe.Pointer) unsafe.Pointer {
return String("PHP: " + CStringToGoString(data))
}
@@ -73,7 +73,7 @@ func anotherPhpFunc(num int64) int64 {
input: `package main
//export_php function phpFunc(data string): string
func phpFunc(data *C.zend_string) unsafe.Pointer {
func phpFunc(data unsafe.Pointer) unsafe.Pointer {
return String("PHP: " + CStringToGoString(data))
}`,
expected: 0,
@@ -83,7 +83,7 @@ func phpFunc(data *C.zend_string) unsafe.Pointer {
input: `package main
//export_php:function my_php_function(string $name): string
func myGoFunction(name *C.zend_string) unsafe.Pointer {
func myGoFunction(name unsafe.Pointer) unsafe.Pointer {
return String("Hello " + CStringToGoString(name))
}
@@ -339,7 +339,7 @@ func mixedFunc(value any) unsafe.Pointer {
input: `package main
//export_php:function arrayReturnFunc(string $name): array
func arrayReturnFunc(name *C.zend_string) any {
func arrayReturnFunc(name unsafe.Pointer) any {
return []string{"result"}
}`,
expected: 0,
@@ -350,7 +350,7 @@ func arrayReturnFunc(name *C.zend_string) any {
input: `package main
//export_php:function objectReturnFunc(string $name): object
func objectReturnFunc(name *C.zend_string) any {
func objectReturnFunc(name unsafe.Pointer) any {
return map[string]any{"key": "value"}
}`,
expected: 0,
@@ -361,7 +361,7 @@ func objectReturnFunc(name *C.zend_string) any {
input: `package main
//export_php:function validFunc(string $name, int $count, float $rate, bool $active): string
func validFunc(name *C.zend_string, count int64, rate float64, active bool) unsafe.Pointer {
func validFunc(name unsafe.Pointer, count int64, rate float64, active bool) unsafe.Pointer {
return nil
}`,
expected: 1,
@@ -372,7 +372,7 @@ func validFunc(name *C.zend_string, count int64, rate float64, active bool) unsa
input: `package main
//export_php:function voidFunc(string $message): void
func voidFunc(message *C.zend_string) {
func voidFunc(message unsafe.Pointer) {
// Do something
}`,
expected: 1,
@@ -407,7 +407,7 @@ func TestFunctionParserGoTypeMismatch(t *testing.T) {
input: `package main
//export_php:function countMismatch(string $name, int $count): string
func countMismatch(name *C.zend_string) unsafe.Pointer {
func countMismatch(name unsafe.Pointer) unsafe.Pointer {
return nil
}`,
expected: 0,
@@ -418,7 +418,7 @@ func countMismatch(name *C.zend_string) unsafe.Pointer {
input: `package main
//export_php:function typeMismatch(string $name, int $count): string
func typeMismatch(name *C.zend_string, count string) unsafe.Pointer {
func typeMismatch(name unsafe.Pointer, count string) unsafe.Pointer {
return nil
}`,
expected: 0,
@@ -429,7 +429,7 @@ func typeMismatch(name *C.zend_string, count string) unsafe.Pointer {
input: `package main
//export_php:function returnMismatch(string $name): int
func returnMismatch(name *C.zend_string) string {
func returnMismatch(name unsafe.Pointer) string {
return ""
}`,
expected: 0,
@@ -440,7 +440,7 @@ func returnMismatch(name *C.zend_string) string {
input: `package main
//export_php:function validMatch(string $name, int $count): string
func validMatch(name *C.zend_string, count int64) unsafe.Pointer {
func validMatch(name unsafe.Pointer, count int64) unsafe.Pointer {
return nil
}`,
expected: 1,

View File

@@ -195,16 +195,14 @@ func (v *Validator) validateGoFunctionSignatureWithOptions(phpFunc phpFunction,
func (v *Validator) phpTypeToGoType(t phpType, isNullable bool) string {
var baseType string
switch t {
case phpString:
baseType = "*C.zend_string"
case phpInt:
baseType = "int64"
case phpFloat:
baseType = "float64"
case phpBool:
baseType = "bool"
case phpArray, phpMixed:
baseType = "*C.zval"
case phpString, phpArray, phpMixed:
baseType = "unsafe.Pointer"
default:
baseType = "any"
}
@@ -238,15 +236,13 @@ func (v *Validator) phpReturnTypeToGoType(phpReturnType phpType) string {
switch phpReturnType {
case phpVoid:
return ""
case phpString:
return "unsafe.Pointer"
case phpInt:
return "int64"
case phpFloat:
return "float64"
case phpBool:
return "bool"
case phpArray:
case phpString, phpArray:
return "unsafe.Pointer"
default:
return "any"

View File

@@ -555,7 +555,7 @@ func TestValidateGoFunctionSignature(t *testing.T) {
{Name: "name", PhpType: phpString},
{Name: "count", PhpType: phpInt},
},
GoFunction: `func testFunc(name *C.zend_string, count int64) unsafe.Pointer {
GoFunction: `func testFunc(name unsafe.Pointer, count int64) unsafe.Pointer {
return nil
}`,
},
@@ -569,7 +569,7 @@ func TestValidateGoFunctionSignature(t *testing.T) {
Params: []phpParameter{
{Name: "message", PhpType: phpString},
},
GoFunction: `func voidFunc(message *C.zend_string) {
GoFunction: `func voidFunc(message unsafe.Pointer) {
// Do something
}`,
},
@@ -595,7 +595,7 @@ func TestValidateGoFunctionSignature(t *testing.T) {
{Name: "param1", PhpType: phpString},
{Name: "param2", PhpType: phpInt},
},
GoFunction: `func countMismatch(param1 *C.zend_string) unsafe.Pointer {
GoFunction: `func countMismatch(param1 unsafe.Pointer) unsafe.Pointer {
return nil
}`,
},
@@ -611,7 +611,7 @@ func TestValidateGoFunctionSignature(t *testing.T) {
{Name: "name", PhpType: phpString},
{Name: "count", PhpType: phpInt},
},
GoFunction: `func typeMismatch(name *C.zend_string, count string) unsafe.Pointer {
GoFunction: `func typeMismatch(name unsafe.Pointer, count string) unsafe.Pointer {
return nil
}`,
},
@@ -626,7 +626,7 @@ func TestValidateGoFunctionSignature(t *testing.T) {
Params: []phpParameter{
{Name: "value", PhpType: phpString},
},
GoFunction: `func returnMismatch(value *C.zend_string) string {
GoFunction: `func returnMismatch(value unsafe.Pointer) string {
return ""
}`,
},
@@ -669,7 +669,7 @@ func TestValidateGoFunctionSignature(t *testing.T) {
Params: []phpParameter{
{Name: "items", PhpType: phpArray},
},
GoFunction: `func arrayFunc(items *C.zval) unsafe.Pointer {
GoFunction: `func arrayFunc(items unsafe.Pointer) unsafe.Pointer {
return nil
}`,
},
@@ -684,7 +684,7 @@ func TestValidateGoFunctionSignature(t *testing.T) {
{Name: "items", PhpType: phpArray, IsNullable: true},
{Name: "name", PhpType: phpString},
},
GoFunction: `func nullableArrayFunc(items *C.zval, name *C.zend_string) unsafe.Pointer {
GoFunction: `func nullableArrayFunc(items unsafe.Pointer, name unsafe.Pointer) unsafe.Pointer {
return nil
}`,
},
@@ -700,7 +700,7 @@ func TestValidateGoFunctionSignature(t *testing.T) {
{Name: "filter", PhpType: phpString},
{Name: "limit", PhpType: phpInt},
},
GoFunction: `func mixedFunc(data *C.zval, filter *C.zend_string, limit int64) unsafe.Pointer {
GoFunction: `func mixedFunc(data unsafe.Pointer, filter unsafe.Pointer, limit int64) unsafe.Pointer {
return nil
}`,
},
@@ -729,16 +729,16 @@ func TestPhpTypeToGoType(t *testing.T) {
isNullable bool
expected string
}{
{"string", false, "*C.zend_string"},
{"string", true, "*C.zend_string"},
{"string", false, "unsafe.Pointer"},
{"string", true, "unsafe.Pointer"},
{"int", false, "int64"},
{"int", true, "*int64"},
{"float", false, "float64"},
{"float", true, "*float64"},
{"bool", false, "bool"},
{"bool", true, "*bool"},
{"array", false, "*C.zval"},
{"array", true, "*C.zval"},
{"array", false, "unsafe.Pointer"},
{"array", true, "unsafe.Pointer"},
{"unknown", false, "any"},
}