Top Banner
2015/11/24 PHP BLT do_aki Writing php extensions in golang
16

Writing php extensions in golang

Jan 07, 2017

Download

Technology

do_aki
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Writing php extensions in golang

2015/11/24 PHP BLTdo_aki

Writingphp

extensions in golang

Page 2: Writing php extensions in golang

@do_aki

@do_aki

http://do-aki.net/

Page 3: Writing php extensions in golang
Page 4: Writing php extensions in golang

https://speakerdeck.com/naruse/writing-extension-libraries-in-go

Page 5: Writing php extensions in golang

In case of PHP……?

Page 6: Writing php extensions in golang

golang buildmode• -buildmode=c-shared• build the main package (main function is dummy)

• export symbol by “//export”

https://golang.org/s/execmodes

Page 7: Writing php extensions in golang

cgo(call C function)package main/* #include <stdlib.h>*/import “C”

func Random() int {return int(C.random())

}

Page 8: Writing php extensions in golang

cgo(export)

package mainimport “C”

//export go_funcfunc go_func() int {

return 1}

Page 9: Writing php extensions in golang

php extension loading process

1.load shared library (.so / .dll)

2.fetch “get_module” or “_get_module” symbol

3.call fetched function and get pointer to zend_module_entry structure

4.register module using the zend_module_entry

ref:php_load_extension in ext/standard/dl.c

Page 10: Writing php extensions in golang

export get_modulepackage main/*#include “php.h”*/import “C”

var gophp_module_entry C.zend_module_entry

//export get_modulefunc get_module() *C.zend_module_entry { gophp_module_entry.name = C.Cstring(“gophp”) gophp_module_entry.functions = … // fill member in gophp_module_entry return gophp_module_entry;}

Page 11: Writing php extensions in golang

export get_modulepackage main/*#include “php.h”*/import “C”

var gophp_module_entry C.zend_module_entry

//export get_modulefunc get_module() *C.zend_module_entry { gophp_module_entry.name = C.Cstring(“gophp”) gophp_module_entry.functions = … // fill member in gophp_module_entry return gophp_module_entry;}

FAILE

D

Page 12: Writing php extensions in golang

constraints of c in go

function-like macro

Access union member

Difficul

t

Page 13: Writing php extensions in golang

separate export function and function definition

• err: multiple definition of ` xxx'

• write definition in non_export.go (not include //export)

• write declarations in export.go (include //export)

• go build non_export.go export.go

Page 14: Writing php extensions in golang

github.com/do-aki/gophp_sample

Page 15: Writing php extensions in golang

but,but,but• segmentation fault in shutdown process sometimes……

Program received signal SIGSEGV, Segmentation fault.[Switching to Thread 0x7fffee4c2700 (LWP 8943)]0x00007ffff04d6821 in ?? ()(gdb) backtrace#0 0x00007ffff04d6821 in ?? ()#1 0x00007ffff04a6213 in ?? ()#2 0x000000c82002ce90 in ?? ()#3 0x0000000000000000 in ?? ()

in go runtime? Umm…

Page 16: Writing php extensions in golang

fin.