Skip to content

How to create GoSlice in php code? #43

@antropov-alexey

Description

@antropov-alexey

Hi, i have .so file, generated from golang code:

package main

import "C"

import (
	"sort"
)

//export Sort
func Sort(vals []int) {
	sort.Ints(vals)
}
func main() {}

and I have .h file:

typedef long GoInt;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
extern void Sort(GoSlice vals);

i try to pass GoSlice from php to golang, but i get exception "Attempt to perform assign of owned C pointer":

<?php

$hFile  = __DIR__ . '/ffi.h';
$soFile = __DIR__ . '/ffi.so';

$ffi = \FFI::cdef(file_get_contents($hFile), $soFile);

var_dump(createGoSlice([1,2,3], $ffi);

function createGoSlice(array $data, \FFI $ffi)
{
    $slice       = $ffi->new('GoSlice');
    $slice->data = \FFI::new(\FFI::type('int[2]'));
    // $slice->data = \FFI::new(\FFI::arrayType(\FFI::type('int'), [2]));
    $slice->len  = count($data);
    $slice->cap  = count($data);

    return $slice;
}

How can i set property data of GoSlice?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions