Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed some reference counting errors

  • Loading branch information...
commit 61f7dd6878ba2285ebe4250287f6f969b6aca708 1 parent 5fa24be
@JosephMoniz authored
Showing with 54 additions and 36 deletions.
  1. +24 −5 src/node_events.c
  2. +29 −31 src/node_http.c
  3. +1 −0  src/node_http.h
View
29 src/node_events.c
@@ -10,6 +10,8 @@
int _node_event_emitter_on(zend_object *self, zval *event, zval *handler);
int _node_event_emitter_once(zend_object *self, zval *event, zval *handler);
int _node_event_emitter_add_to_array(zval *ht, zval *event, zval *handler);
+int _node_event_emitter_on(zend_object *self, zval *event, zval *handler);
+int _node_event_emitter_emit(zend_object *self, zval *event, zval *data);
// register methods for the event_emitter object
zend_function_entry event_emitter_methods[] = {
@@ -49,7 +51,10 @@ zend_object_value event_emitter_new(zend_class_entry *class_type TSRMLS_DC) {
}
void event_emitter_free(void *object TSRMLS_DC) {
-
+ event_emitter_t *emitter = (event_emitter_t*) object;
+ zend_objects_free_object_storage(&emitter->obj TSRMLS_CC);
+ FREE_ZVAL(emitter->listeners);
+ FREE_ZVAL(emitter->once);
}
// private methods
@@ -71,6 +76,10 @@ int _node_event_emitter_once(zend_object *self, zval *event, zval *handler) {
);
}
+int _node_event_emitter_emit(zend_object *self, zval *event, zval *data) {
+ return 0;
+}
+
int _node_event_emitter_add_to_array(zval *ht, zval *event, zval *handler) {
zval *listeners;
int index_exists;
@@ -86,12 +95,13 @@ int _node_event_emitter_add_to_array(zval *ht, zval *event, zval *handler) {
}
// lets find the index for the set of callbacks
- index_exists = zend_hash_find( ht->value.ht
+ index_exists = zend_hash_find( Z_ARRVAL_P(ht)
, Z_STRVAL_P(event)
, Z_STRLEN_P(event)
, (void**)&listeners
);
+ // if the set of callbacks was not found, lets create one
if (index_exists == FAILURE) {
ALLOC_INIT_ZVAL(listeners);
array_init(listeners);
@@ -99,6 +109,7 @@ int _node_event_emitter_add_to_array(zval *ht, zval *event, zval *handler) {
}
// array_push(&listners, handler)
+ Z_ADDREF_P(handler);
add_next_index_zval(listeners, handler);
return 1;
@@ -157,12 +168,20 @@ PHP_METHOD(node_event_emitter, listeners) {
}
PHP_METHOD(node_event_emitter, emit) {
- /*
+ /*
zend_object *self = zend_object_store_get_object(getThis() TSRMLS_CC);
int argc = ZEND_NUM_ARGS();
zval *event, *data;
int result = zend_parse_parameters(argc TSRMLS_CC, "z|z", &event, &data);
- */
- RETURN_NULL();
+
+ switch(ZEND_NUM_ARGS()) {
+ case 0: RETURN_BOOL(0);
+ case 1:
+
+ break;
+ case 2: RETURN_BOOL(_node_event_emitter_emit());
+ default: RETURN_BOOL(0);
+ }
+ */
}
View
60 src/node_http.c
@@ -77,33 +77,31 @@ zend_object_value http_new(zend_class_entry *class_type TSRMLS_DC) {
void http_wrap_free(void *object TSRMLS_DC) {
http_wrap_t *http = (http_wrap_t*) object;
- zend_object_std_dtor(&http->obj TSRMLS_CC);
- efree(http);
+ zend_objects_free_object_storage(&http->obj TSRMLS_CC);
}
zend_object_value http_response_new(zend_class_entry *class_type TSRMLS_DC) {
zend_object_value instance;
- http_response_t *wrap = emalloc(sizeof(http_response_t));
+ http_response_t *response = emalloc(sizeof(http_response_t));
- zend_object_std_init(&wrap->obj, class_type TSRMLS_CC);
- init_properties(&wrap->obj, class_type);
-
- TSRMLS_SET(wrap);
+ zend_object_std_init(&response->obj, class_type TSRMLS_CC);
+ init_properties(&response->obj, class_type);
- instance.handle = zend_objects_store_put((void*) wrap,
+ instance.handle = zend_objects_store_put((void*) response,
(zend_objects_store_dtor_t) zend_objects_destroy_object,
http_response_free,
NULL
TSRMLS_CC);
instance.handlers = zend_get_std_object_handlers();
+ response->handle = instance.handle;
+
return instance;
}
void http_response_free(void *object TSRMLS_DC) {
http_response_t *response = (http_response_t*) object;
- zend_object_std_dtor(&response->obj TSRMLS_CC);
- efree(response);
+ zend_objects_free_object_storage(&response->obj TSRMLS_CC);
}
// libuv callbacks
@@ -143,8 +141,6 @@ void _on_http_connection(uv_stream_t* server_handle, int status) {
void _http_on_close(uv_handle_t *client) {
http_request_t *request = client->data;
- FREE_ZVAL(request->request);
- FREE_ZVAL(request->headers);
efree(request);
}
@@ -164,7 +160,6 @@ void _http_on_read(uv_stream_t *client, ssize_t nread, uv_buf_t buf) {
if (s < nread) {
uv_close((uv_handle_t*) client, _http_on_close);
- // TODO: notify the user of a parse error?
}
} else {
uv_err_t error = uv_last_error(uv_default_loop());
@@ -252,14 +247,16 @@ int _http_on_message_complete(http_parser *parser) {
node_function_call_zval(cb, 2, &data, &r_zval);
zend_objects_store_del_ref_by_handle_ex(object.handle, object.handlers);
- uv_close((uv_handle_t*) &request->handle, _http_on_close);
}
return 0;
}
void _after_http_write(uv_write_t *request, int status) {
- // nothing to do here yet
+ http_response_t *self = (http_response_t*) request->data;
+ efree(self->response);
+ zend_objects_store_del_ref_by_handle(self->handle);
+ uv_close((uv_handle_t*) request->handle, _http_on_close);
}
// class methods
@@ -324,37 +321,38 @@ PHP_METHOD(node_http, listen) {
}
PHP_METHOD(node_http_response, end) {
- http_response_t *self;
zval *arg1;
+ zend_object *self;
+ http_response_t *response;
char *http_res = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: %d\r\n"
"\r\n%s";
+ int result = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg1);
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &arg1) == FAILURE) {
- return;
- }
-
- if (Z_TYPE_P(arg1) != IS_STRING) {
- return;
+ if (result == FAILURE || Z_TYPE_P(arg1) != IS_STRING) {
+ RETURN_BOOL(0);
}
- self = (http_response_t*) zend_object_store_get_object(getThis() TSRMLS_CC);
+ self = zend_object_store_get_object(getThis() TSRMLS_CC);
+ response = (http_response_t*) self;
+ zend_objects_store_add_ref_by_handle(response->handle);
- self->response = emalloc(strlen(http_res) + 20 + Z_STRLEN_P(arg1));
-
- sprintf(self->response, http_res, Z_STRLEN_P(arg1), Z_STRVAL_P(arg1));
+ response->response = emalloc(strlen(http_res) + 20 + Z_STRLEN_P(arg1));
+ sprintf(response->response, http_res, Z_STRLEN_P(arg1), Z_STRVAL_P(arg1));
uv_buf_t buf;
- buf.base = self->response;
- buf.len = strlen(self->response);
+ buf.base = response->response;
+ buf.len = strlen(response->response);
+
+ response->request.data = response;
- uv_write( &self->request
- , (uv_stream_t*)self->socket
+ uv_write( &response->request
+ , (uv_stream_t*)response->socket
, &buf
, 1
, _after_http_write
);
- RETURN_NULL();
+ RETURN_BOOL(1);
}
View
1  src/node_http.h
@@ -29,6 +29,7 @@ struct _http_request_t {
struct _http_response_t {
zend_object obj;
uv_write_t request;
+ zend_object_handle handle;
uv_tcp_t *socket;
char *response;
zval *callback;
Please sign in to comment.
Something went wrong with that request. Please try again.