Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First version of PHP 7 support #12

Merged
merged 7 commits into from
Jan 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 12 additions & 39 deletions context/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <stdbool.h>

#include <main/php.h>
#include <main/SAPI.h>
#include <main/php_main.h>

#include "value.h"
Expand All @@ -22,16 +21,11 @@ engine_context *context_new(void *ctx) {
return NULL;
}

#ifdef ZTS
TSRMLS_FETCH();
context->tsrm_ls = tsrm_ls;
#endif

context->ctx = ctx;
SG(server_context) = context;

// Initialize request lifecycle.
if (php_request_startup(TSRMLS_C) == FAILURE) {
if (php_request_startup() == FAILURE) {
SG(server_context) = NULL;
free(context);

Expand All @@ -46,10 +40,6 @@ engine_context *context_new(void *ctx) {
void context_exec(engine_context *context, char *filename) {
int ret;

#ifdef ZTS
void ***tsrm_ls = context->tsrm_ls;
#endif

// Attempt to execute script file.
zend_first_try {
zend_file_handle script;
Expand All @@ -59,7 +49,7 @@ void context_exec(engine_context *context, char *filename) {
script.opened_path = NULL;
script.free_filename = 0;

ret = php_execute_script(&script TSRMLS_CC);
ret = php_execute_script(&script);
} zend_catch {
errno = 1;
return;
Expand All @@ -75,53 +65,36 @@ void context_exec(engine_context *context, char *filename) {
}

void *context_eval(engine_context *context, char *script) {
int ret;
zval *retval;

#ifdef ZTS
void ***tsrm_ls = context->tsrm_ls;
#endif

MAKE_STD_ZVAL(retval);
int status;
zval tmp;

// Attempt to evaluate inline script.
zend_first_try {
ret = zend_eval_string(script, retval, "gophp-engine" TSRMLS_CC);
status = zend_eval_string(script, &tmp, "gophp-engine");
} zend_catch {
zval_dtor(retval);
errno = 1;
return NULL;
} zend_end_try();

if (ret == FAILURE) {
zval_dtor(retval);
if (status == FAILURE) {
errno = 1;
return NULL;
}

zval *result = malloc(sizeof(zval));
value_copy(result, &tmp);

errno = 0;
return (void *) retval;
return result;
}

void context_bind(engine_context *context, char *name, void *value) {
engine_value *v = (engine_value *) value;

#ifdef ZTS
void ***tsrm_ls = context->tsrm_ls;
#endif

ZEND_SET_SYMBOL(EG(active_symbol_table), name, v->value);

errno = 0;
return;
CONTEXT_VALUE_BIND(name, v->internal);
}

void context_destroy(engine_context *context) {
#ifdef ZTS
void ***tsrm_ls = context->tsrm_ls;
#endif

php_request_shutdown((void *) 0);
php_request_shutdown(NULL);

SG(server_context) = NULL;
free(context);
Expand Down
26 changes: 13 additions & 13 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
package context

// #cgo CFLAGS: -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM
// #cgo CFLAGS: -I/usr/include/php/Zend -I../value
// #cgo LDFLAGS: -lphp5
// #cgo CFLAGS: -I/usr/include/php/Zend -I../include
//
// #include <stdlib.h>
// #include <main/php.h>
// #include "context.h"
import "C"

Expand Down Expand Up @@ -69,11 +69,7 @@ func (c *Context) Bind(name string, val interface{}) error {
n := C.CString(name)
defer C.free(unsafe.Pointer(n))

if _, err = C.context_bind(c.context, n, v.Ptr()); err != nil {
v.Destroy()
return fmt.Errorf("Binding value '%v' to context failed", val)
}

C.context_bind(c.context, n, v.Ptr())
c.values[name] = v

return nil
Expand Down Expand Up @@ -105,12 +101,14 @@ func (c *Context) Eval(script string) (*value.Value, error) {
s := C.CString("call_user_func(function(){" + script + "});")
defer C.free(unsafe.Pointer(s))

vptr, err := C.context_eval(c.context, s)
result, err := C.context_eval(c.context, s)
if err != nil {
return nil, fmt.Errorf("Error executing script '%s' in context", script)
}

val, err := value.NewFromPtr(vptr)
defer C.free(result)

val, err := value.NewFromPtr(result)
if err != nil {
return nil, err
}
Expand All @@ -121,14 +119,16 @@ func (c *Context) Eval(script string) (*value.Value, error) {
// Destroy tears down the current execution context along with any active value
// bindings for that context.
func (c *Context) Destroy() {
if c.context == nil {
return
}

for _, v := range c.values {
v.Destroy()
}

c.values = nil

if c.context != nil {
C.context_destroy(c.context)
c.context = nil
}
C.context_destroy(c.context)
c.context = nil
}
7 changes: 7 additions & 0 deletions context/php5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build !php7

package context

// #cgo CFLAGS: -I../include/php5
// #cgo LDFLAGS: -lphp5
import "C"
7 changes: 7 additions & 0 deletions context/php7.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build php7

package context

// #cgo CFLAGS: -I../include/php7
// #cgo LDFLAGS: -lphp7
import "C"
56 changes: 18 additions & 38 deletions engine/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
#include <main/SAPI.h>
#include <main/php_main.h>
#include <main/php_variables.h>
#include <TSRM/TSRM.h>

#include "context.h"
#include "engine.h"
#include "_cgo_export.h"

// The php.ini defaults for the Go-PHP engine.
const char engine_ini_defaults[] = {
"expose_php = 0\n"
"default_mimetype =\n"
Expand All @@ -26,18 +26,22 @@ const char engine_ini_defaults[] = {
"max_input_time = -1\n\0"
};

static int engine_ub_write(const char *str, uint str_length TSRMLS_DC) {
// Unbuffered write to engine context.
//
// The function definition for this depends on the PHP version used, and is
// defined in the "_engine.h" file for the respective PHP version used.
static ENGINE_UB_WRITE(str, len) {
engine_context *context = SG(server_context);

int written = engineWriteOut(context->ctx, (void *) str, str_length);
if (written != str_length) {
int written = engineWriteOut(context->ctx, (void *) str, len);
if (written != len) {
php_handle_aborted_connection();
}

return written;
return len;
}

static int engine_header_handler(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers TSRMLS_DC) {
static int engine_header_handler(sapi_header_struct *sapi_header, sapi_header_op_enum op, sapi_headers_struct *sapi_headers) {
engine_context *context = SG(server_context);

switch (op) {
Expand All @@ -51,19 +55,19 @@ static int engine_header_handler(sapi_header_struct *sapi_header, sapi_header_op
return 0;
}

static void engine_send_header(sapi_header_struct *sapi_header, void *server_context TSRMLS_DC) {
static void engine_send_header(sapi_header_struct *sapi_header, void *server_context) {
// Do nothing.
}

static char *engine_read_cookies(TSRMLS_D) {
static char *engine_read_cookies() {
return NULL;
}

static void engine_register_variables(zval *track_vars_array TSRMLS_DC) {
php_import_environment_variables(track_vars_array TSRMLS_CC);
static void engine_register_variables(zval *track_vars_array) {
php_import_environment_variables(track_vars_array);
}

static void engine_log_message(char *str TSRMLS_DC) {
static void engine_log_message(char *str) {
engine_context *context = SG(server_context);

engineWriteLog(context->ctx, (void *) str, strlen(str));
Expand Down Expand Up @@ -96,28 +100,20 @@ static sapi_module_struct engine_module = {
engine_register_variables, // Register Server Variables
engine_log_message, // Log Message
NULL, // Get Request Time
NULL, // Child Terminate

STANDARD_SAPI_MODULE_PROPERTIES
};

php_engine *engine_init(void) {
php_engine *engine;

#ifdef ZTS
void ***tsrm_ls = NULL;
#endif

#ifdef HAVE_SIGNAL_H
#if defined(SIGPIPE) && defined(SIG_IGN)
signal(SIGPIPE, SIG_IGN);
#endif
#endif

#ifdef ZTS
tsrm_startup(1, 1, 0, NULL);
tsrm_ls = ts_resource(0);
#endif

sapi_startup(&engine_module);

engine_module.ini_entries = malloc(sizeof(engine_ini_defaults));
Expand All @@ -126,36 +122,20 @@ php_engine *engine_init(void) {
if (php_module_startup(&engine_module, NULL, 0) == FAILURE) {
sapi_shutdown();

#ifdef ZTS
tsrm_shutdown();
#endif

errno = 1;
return NULL;
}

engine = malloc((sizeof(php_engine)));

#ifdef ZTS
engine->tsrm_ls = tsrm_ls;
#endif

errno = 0;
return engine;
}

void engine_shutdown(php_engine *engine) {
#ifdef ZTS
void ***tsrm_ls = engine->tsrm_ls;
#endif

php_module_shutdown(TSRMLS_C);
php_module_shutdown();
sapi_shutdown();

#ifdef ZTS
tsrm_shutdown();
#endif

free(engine_module.ini_entries);
free(engine);
}
}
21 changes: 13 additions & 8 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
package engine

// #cgo CFLAGS: -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM
// #cgo CFLAGS: -I/usr/include/php/Zend -I../value -I../context
// #cgo LDFLAGS: -L${SRCDIR}/value -L${SRCDIR}/context -lphp5
// #cgo CFLAGS: -I/usr/include/php/Zend -I../include
//
// #include <stdlib.h>
// #include <main/php.h>
//
// #include "context.h"
// #include "engine.h"
import "C"
Expand Down Expand Up @@ -84,17 +82,24 @@ func (e *Engine) Define(name string, fn func(args []interface{}) interface{}) er

// Destroy shuts down and frees any resources related to the PHP engine bindings.
func (e *Engine) Destroy() {
if e.engine == nil {
return
}

for _, r := range e.receivers {
r.Destroy()
}

e.receivers = nil

for _, c := range e.contexts {
c.Destroy()
}

e.contexts = nil
e.receivers = nil

if e.engine != nil {
C.engine_shutdown(e.engine)
e.engine = nil
}
C.engine_shutdown(e.engine)
e.engine = nil
}

func write(w io.Writer, buffer unsafe.Pointer, length C.uint) C.int {
Expand Down
7 changes: 7 additions & 0 deletions engine/php5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build !php7

package engine

// #cgo CFLAGS: -I../include/php5
// #cgo LDFLAGS: -lphp5
import "C"
7 changes: 7 additions & 0 deletions engine/php7.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build php7

package engine

// #cgo CFLAGS: -I../include/php7
// #cgo LDFLAGS: -lphp7
import "C"
Loading