Skip to content

Commit 61bbe8b

Browse files
authored
Merge pull request #33 from lyrixx/fix
My 2 cents
2 parents 589d8ac + dfa0894 commit 61bbe8b

File tree

9 files changed

+53
-63
lines changed

9 files changed

+53
-63
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/app**
2+
/examples.so

Makefile

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
21
# Usage:
3-
# PHPCFG=/usr/bin/php-config55 make
4-
# PHPCFG=php-config55 make
5-
# PHPCFG=/usr/bin/php-config make
6-
# PHPCFG=php-config make
7-
# make
2+
# Default config:
3+
# make
4+
# Use a specific php-config:
5+
# PHPCFG=/usr/bin/php-config72 make
6+
# Disable zend log (<error):
7+
# CGO_CFLAGS="-DLOGLEVEL=error" make
88

99
ifeq ($(PHPCFG),)
1010
PHPCFG=/usr/bin/php-config
1111
endif
1212

13+
ifeq ($(APP),)
14+
APP=examples
15+
endif
16+
1317
PHPEXE := $(shell $(PHPCFG) --php-binary)
1418
PHPDIR := $(shell $(PHPCFG) --prefix)
1519

@@ -26,8 +30,8 @@ GOARCH := $(shell go env GOARCH)
2630
all:
2731
go install ./zend
2832
go install ./phpgo
29-
go build -v -buildmode=c-shared -o hello.so examples/hello.go
30-
# $(PHPEXE) -d extension=./hello.so examples/hello.php
33+
go build -buildmode=c-shared -o $(APP).so $(APP)/main.go
34+
# $(PHPEXE) -d extension=./$(APP).so $(APP)/main.php
3135

3236
zdlib:
3337
go install -v -x ./zend
@@ -43,6 +47,6 @@ quickbc:
4347
go build -v -x -buildmode=c-archive -o php-grpc.c.a examples/hello.go
4448

4549
clean:
46-
rm -f $(GOPATH)/pkg/$(GOOS)_$(GOARCH)/zend.a
47-
rm -f $(GOPATH)/pkg/$(GOOS)_$(GOARCH)/phpgo.a
50+
rm -f $(GOPATH)/pkg/$(GOOS)_$(GOARCH)/github.com/kitech/php-go/zend.a
51+
rm -f $(GOPATH)/pkg/$(GOOS)_$(GOARCH)/github.com/kitech/php-go/phpgo.a
4852
rm -f hello.so
File renamed without changes.
File renamed without changes.

phpgo/init.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ package phpgo
66
import "C"
77
import "unsafe"
88

9-
// import "runtime"
10-
11-
// import "os"
12-
import "fmt"
13-
14-
//
15-
// 给php加载扩展时调用的扩展入口函数。
169
//export get_module
1710
func get_module() unsafe.Pointer {
1811
if len(ExtName) == 0 {
@@ -22,7 +15,6 @@ func get_module() unsafe.Pointer {
2215
addBuiltins()
2316

2417
mod := C.phpgo_get_module(C.CString(ExtName), C.CString(ExtVer))
25-
fmt.Printf("mod=%p\n", mod)
2618

2719
return unsafe.Pointer(mod)
2820
}

phpgo/objectmap.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void phpgo_function_map_add(const char *class_name, const char *func_name, zend_
5252
int id = UTHASH_CNT(hh, g_funcs_map);
5353
phpgo_function_map *m = (phpgo_function_map*)malloc(sizeof(phpgo_function_map));
5454
char *key = (char*)calloc(1, class_name == NULL ? 0 : strlen(class_name) + strlen(func_name) + 2 + 1);
55-
sprintf(key, "%s::%s\0", class_name == NULL ? "" : class_name, func_name);
55+
sprintf(key, "%s::%s%c", class_name == NULL ? "" : class_name, func_name, '\0');
5656
m->name = key;
5757
m->fe = fe;
5858
m->id = id;
@@ -63,7 +63,7 @@ zend_function_entry* phpgo_function_map_get(const char *class_name, const char *
6363
{
6464
phpgo_function_map *m = NULL;
6565
char key[(class_name == NULL ? 0 : strlen(class_name)) + strlen(func_name) + 2 + 1];
66-
sprintf(key, "%s::%s\0", (class_name == NULL ? "" : class_name), func_name);
66+
sprintf(key, "%s::%s%c", (class_name == NULL ? "" : class_name), func_name, '\0');
6767
UTHASH_FIND_STR(g_funcs_map, key, m);
6868
if (m == NULL) {
6969
return NULL;
@@ -97,7 +97,7 @@ void phpgo_callback_map_add(const char *class_name, const char *func_name, int c
9797
int id = UTHASH_CNT(hh, g_callbacks_map);
9898
phpgo_callback_map *m = (phpgo_callback_map*)malloc(sizeof(phpgo_callback_map));
9999
char *key = (char*)calloc(1, class_name == NULL ? 0 : strlen(class_name) + strlen(func_name) + 2 + 1);
100-
sprintf(key, "%s::%s\0", class_name == NULL ? "" : class_name, func_name);
100+
sprintf(key, "%s::%s%c", class_name == NULL ? "" : class_name, func_name, '\0');
101101
m->name = key;
102102
m->cbid = cbid;
103103
UTHASH_ADD_KEYPTR(hh, g_callbacks_map, key, strlen(key), m);
@@ -107,7 +107,7 @@ int phpgo_callback_map_get(const char *class_name, const char *func_name)
107107
{
108108
phpgo_callback_map *m = NULL;
109109
char key[(class_name == NULL ? 0 : strlen(class_name)) + strlen(func_name) + 2 + 1];
110-
sprintf(key, "%s::%s\0", (class_name == NULL ? "" : class_name), func_name);
110+
sprintf(key, "%s::%s%c", (class_name == NULL ? "" : class_name), func_name, '\0');
111111
UTHASH_FIND_STR(g_callbacks_map, key, m);
112112
if (m == NULL) {
113113
return -1;

readme.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
[![GoDoc](https://godoc.org/github.com/kitech/php-go/phpgo?status.svg)](https://godoc.org/github.com/kitech/php-go/phpgo)
42
[![GoDoc](https://godoc.org/github.com/kitech/php-go/zend?status.svg)](https://godoc.org/github.com/kitech/php-go/zend)
53

@@ -9,7 +7,6 @@ Write PHP extension using go/golang. Zend API wrapper for go/golang.
97

108
Simple, easy, fun to write PHP extensions.
119

12-
1310
### Features
1411

1512
* function support
@@ -19,7 +16,6 @@ Simple, easy, fun to write PHP extensions.
1916
* complex data type as parameters, map/slice/array
2017
* all can be done by programmatic
2118

22-
2319
### Environment
2420

2521
* Modern Linux/Unix system
@@ -39,7 +35,6 @@ cd $GOPATH/src/github.com/kitech/php-go
3935
PHPCFG=`which php-config` make
4036
```
4137

42-
4338
manual:
4439

4540
mkdir -p $GOPATH/src/github.com/kitech
@@ -76,7 +71,7 @@ func init() {
7671
// should not run this function
7772
// required for go build though.
7873
func main() { panic("wtf") }
79-
```
74+
```
8075

8176
### TODO
8277

@@ -97,4 +92,3 @@ Contributing
9792
3. Commit your changes (``git commit -am 'Add some feature'``)
9893
4. Push to the branch (``git push origin my-new-feature``)
9994
5. Create new Pull Request
100-

zend/init.go

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,32 @@ package zend
77
import "C"
88

99
// import "unsafe"
10-
import "runtime"
11-
12-
import "time"
13-
import "math/rand"
14-
15-
// import "os"
16-
import "fmt"
17-
import "log"
10+
import (
11+
"io/ioutil"
12+
"log"
13+
"math/rand"
14+
"runtime"
15+
"time"
16+
)
1817

1918
const (
2019
SINGLE_THREAD = 1
2120
)
2221

23-
//
2422
func init() {
25-
initSingleThread()
26-
initCLog()
2723
initRawLog()
28-
}
29-
30-
func initSingleThread() {
31-
if C.gozend_iszts() == 0 {
32-
omp := runtime.GOMAXPROCS(0)
33-
if omp > SINGLE_THREAD {
34-
runtime.GOMAXPROCS(SINGLE_THREAD)
35-
fmt.Printf("Adjust GOMAXPROCS %d => %d\n", omp, SINGLE_THREAD)
36-
}
37-
}
38-
39-
rand.Seed(time.Now().UnixNano())
24+
initCLog()
25+
initSingleThread()
4026
}
4127

4228
func initRawLog() {
43-
prefix := "phpgo"
44-
// log.SetFlags(log.Llongfile | log.LstdFlags)
45-
log.SetFlags(log.Lshortfile | log.LstdFlags)
46-
oldPrefix := log.Prefix()
47-
log.SetPrefix(fmt.Sprintf("[%s ] ", prefix))
48-
if len(oldPrefix) > 0 {
49-
log.Printf("Switch log prefix: %s => [%s ]\n", oldPrefix, prefix)
29+
// To enable use true:
30+
if false {
31+
log.SetFlags(log.Lshortfile | log.LstdFlags)
32+
log.SetPrefix("[phpgo] ")
33+
} else {
34+
log.SetFlags(0)
35+
log.SetOutput(ioutil.Discard)
5036
}
5137
}
5238

@@ -57,6 +43,18 @@ func initCLog() {
5743
C.dlog_set_level(C.STDERR_FILENO, 0)
5844
}
5945

46+
func initSingleThread() {
47+
if C.gozend_iszts() == 0 {
48+
omp := runtime.GOMAXPROCS(0)
49+
if omp > SINGLE_THREAD {
50+
runtime.GOMAXPROCS(SINGLE_THREAD)
51+
log.Printf("Adjust GOMAXPROCS %d => %d\n", omp, SINGLE_THREAD)
52+
}
53+
}
54+
55+
rand.Seed(time.Now().UnixNano())
56+
}
57+
6058
const (
6159
LOG_NONE = int(-1)
6260
LOG_ERROR = int(C.CLOG_ERROR)

zend/zend_ini.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ func (this *IniEntryDef) Fill3(name string, defaultValue interface{}, modifiable
120120
}
121121

122122
if ZEND_ENGINE == ZEND_ENGINE_3 {
123-
this.zie.name_length = C.uint(len(name))
124-
this.zie.value_length = C.uint(len(value))
123+
this.zie.name_length = C.uint32_t(len(name))
124+
this.zie.value_length = C.uint32_t(len(value))
125125
} else {
126126
// why need +1 for php5?
127127
// if not, zend_alter_ini_entry_ex:280行会出现zend_hash_find无结果失败
128-
this.zie.name_length = C.uint(len(name) + 1)
129-
this.zie.value_length = C.uint(len(value) + 1)
128+
this.zie.name_length = C.uint32_t(len(name) + 1)
129+
this.zie.value_length = C.uint32_t(len(value) + 1)
130130
}
131131
log.Println(name, len(name))
132132

0 commit comments

Comments
 (0)