summaryrefslogtreecommitdiff
path: root/ext/satellite/class.c
blob: 46419199d77fc3ba13f55cfcd045556e6a4f93fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
   +----------------------------------------------------------------------+
   | PHP version 4.0                                                      |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2001 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.txt.                                 |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Author: David Eriksson <david@2good.com>                            |
   +----------------------------------------------------------------------+
 */

/*
 * $Id$
 * vim: syntax=c tabstop=2 shiftwidth=2
 */


/*
 * Helper function for making PHP classes
 */

#include <php.h>
#include "common.h"
#include "class.h"

void orbit_class_function_call(
		zend_class_entry * pClass,
		int dataType,
		zend_property_reference *pPropertyReference, 
		Class_Constructor pConstructor, 
		Class_CallFunction pCallFunction,
		INTERNAL_FUNCTION_PARAMETERS)
{
	/* get object */
	zval * object = pPropertyReference->object;

	/* get function name */
	zend_overloaded_element * function_name =
		(zend_overloaded_element *)pPropertyReference->elements_list->tail->data;

	/* handle parameters */
	zval ** arguments = orbit_new_n(zval *, ZEND_NUM_ARGS());
		/*(zval **)emalloc(sizeof(zval *) * ZEND_NUM_ARGS());*/
	if (getParametersArray(ht, ZEND_NUM_ARGS(), arguments) == FAILURE)
	{
		/* TODO: handle error */
	}

	/* constructor or normal function? */
	if (zend_llist_count(pPropertyReference->elements_list) == 1
			&& !strcasecmp(function_name->element.value.str.val, pClass->name))
	{
		/* constructor */
		if (pConstructor)
		{
			void * p_data = NULL;
			zend_bool success = (*pConstructor)(&p_data, ZEND_NUM_ARGS(), arguments);

			if (success)
				orbit_save_data(object, dataType, p_data);
		}
		else
		{
			zend_error(E_WARNING, "(Satellite) This class has no constructor");\
		}
	}
	else
	{
		/* normal function */
		if (pCallFunction)
		{
			void * p_data = orbit_retrieve_data(object, dataType);

			if (p_data == NULL)
			{
				/* 
				 * this means that the constructor has failed earlier! 
				 * -- or should NULL be allowed here? 
				 */
				php_error(E_WARNING, "(Satellite) Class has no data!");
				RETVAL_NULL();
				goto orbit_class_function_call_exit;
			}
			
			/* pval * return_value is a part of INTERNAL_FUNCTION_PARAMETERS */
			(*pCallFunction)(p_data, function_name->element.value.str.val,
											 ZEND_NUM_ARGS(), arguments, return_value);
		}
		else
		{
			zend_error(E_WARNING, "(Satellite) Can't call functions in this class");\
		}
	}

orbit_class_function_call_exit:
	satellite_delete(arguments);
	
	/* seems to be required! */
	zval_dtor(&function_name->element);
}

/*
 * save a corba object to a php object
 */
void orbit_save_data(zval * php_object, int type, void * data)
{
	pval * orbit_data_handle = NULL;
	long id = zend_list_insert(
			data, 										/* data */
			type								/* type */
			);
	

	/* 
	 * do it like they do in php_COM_call_function_handler 
	 * (insert into some magic hash index)
	 */
	ALLOC_ZVAL(orbit_data_handle);	/* allocate memory for value */
	
	orbit_data_handle->type = IS_LONG;
	orbit_data_handle->value.lval = id;
	
	pval_copy_constructor(orbit_data_handle);	/* why? */
	
	INIT_PZVAL(orbit_data_handle);	/* set reference count */

	zend_hash_index_update(
			php_object->value.obj.properties, /* hashtable */
			0, 																/* hash??? */
			&orbit_data_handle,								/* data */
			sizeof(pval *),										/* data size */
			NULL															/* destination */
			);
}

/*
 * retrieve a corba object from a php object
 */
void * orbit_retrieve_data(const zval * php_object, int wanted_type)
{
	void * data = NULL;
	pval ** orbit_data_handle = NULL;
	int type = 0;

	/* get handle to corba data */
	zend_hash_index_find(										
			php_object->value.obj.properties,		/* hash table */	
			0, 																	/* hash??? */
			(void **)&orbit_data_handle					/* data */
			);

	if (orbit_data_handle == NULL || *orbit_data_handle == NULL)
	{
		return NULL;
	}

	/* get corba data */
	data = zend_list_find(
			(*orbit_data_handle)->value.lval, 	/* id */
			&type																/* type */
			);

	/* verify corba object */
	if (!data || (type != wanted_type))
	{
		/* TODO: handle error */
		return NULL;
	}
	
	return data;
}