summaryrefslogtreecommitdiff
path: root/Zend/zend.c
blob: 7a8947091675b393510f5be4136e0b5dc92805f0 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
   +----------------------------------------------------------------------+
   | Zend Engine                                                          |
   +----------------------------------------------------------------------+
   | Copyright (c) 1998, 1999 Andi Gutmans, Zeev Suraski                  |
   +----------------------------------------------------------------------+
   | This source file is subject to version 0.91 of the Zend license,     |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.zend.com/license/0_91.txt.                                |
   | If you did not receive a copy of the Zend license and are unable to  |
   | obtain it through the world-wide-web, please send a note to          |
   | license@zend.com so we can mail you a copy immediately.              |
   +----------------------------------------------------------------------+
   | Authors: Andi Gutmans <andi@zend.com>                                |
   |          Zeev Suraski <zeev@zend.com>                                |
   +----------------------------------------------------------------------+
*/


#include "zend.h"
#include "zend_operators.h"
#include "zend_variables.h"
#include "zend_extensions.h"
#include "modules.h"
#include "zend_constants.h"
#include "zend_list.h"
#include "zend_API.h"
#include "zend_builtin_functions.h"

#ifdef ZTS
#	define GLOBAL_FUNCTION_TABLE	global_function_table
#	define GLOBAL_CLASS_TABLE		global_class_table
#	define GLOBAL_CONSTANTS_TABLE	global_constants_table
#else
#	define GLOBAL_FUNCTION_TABLE	CG(function_table)
#	define GLOBAL_CLASS_TABLE		CG(class_table)
#	define GLOBAL_CONSTANTS_TABLE	CG(zend_constants)
#endif

/* true multithread-shared globals */
ZEND_API zend_class_entry zend_standard_class_def;
ZEND_API int (*zend_printf)(const char *format, ...);
ZEND_API int (*zend_write)(const char *str, uint str_length);
ZEND_API void (*zend_error)(int type, const char *format, ...);
ZEND_API void (*zend_message_dispatcher)(long message, void *data);
ZEND_API FILE *(*zend_fopen)(const char *filename);
ZEND_API void (*zend_block_interruptions)();
ZEND_API void (*zend_unblock_interruptions)();
ZEND_API int (*zend_get_ini_entry)(char *name, uint name_length, zval *contents);

#ifdef ZTS
ZEND_API int compiler_globals_id;
ZEND_API int executor_globals_id;
int alloc_globals_id;
HashTable *global_function_table;
HashTable *global_class_table;
HashTable *global_constants_table;
#endif

zend_utility_values zend_uv;

/* version information */
static char *zend_version_info;
static uint zend_version_info_length;
#define ZEND_CORE_VERSION_INFO	"Zend Engine v" ZEND_VERSION ", Copyright (c) 1998, 1999 Andi Gutmans, Zeev Suraski\n"


#define PRINT_PVAL_INDENT 4

static void print_hash(HashTable *ht, int indent)
{
	zval **tmp;
	char *string_key;
	unsigned long num_key;
	int i;

	for (i=0; i<indent; i++) {
		ZEND_PUTS(" ");
	}
	ZEND_PUTS("(\n");
	indent += PRINT_PVAL_INDENT;
	zend_hash_internal_pointer_reset(ht);
	while (zend_hash_get_current_data(ht, (void **) &tmp) == SUCCESS) {
		for (i=0; i<indent; i++) {
			ZEND_PUTS(" ");
		}
		ZEND_PUTS("[");
		switch (zend_hash_get_current_key(ht, &string_key, &num_key)) {
			case HASH_KEY_IS_STRING:
				ZEND_PUTS(string_key);
				efree(string_key);
				break;
			case HASH_KEY_IS_LONG:
				zend_printf("%ld",num_key);
				break;
		}
		ZEND_PUTS("] => ");
		zend_print_zval_r(*tmp, indent+PRINT_PVAL_INDENT);
		ZEND_PUTS("\n");
		zend_hash_move_forward(ht);
	}
	indent -= PRINT_PVAL_INDENT;
	for (i=0; i<indent; i++) {
		ZEND_PUTS(" ");
	}
	ZEND_PUTS(")\n");
}


ZEND_API void zend_make_printable_zval(zval *expr, zval *expr_copy, int *use_copy)
{
	if (expr->type==IS_STRING) {
		*use_copy = 0;
		return;
	}
	switch (expr->type) {
		case IS_BOOL:
#if 1
			if (expr->value.lval) {
				expr_copy->value.str.len = 1;
				expr_copy->value.str.val = estrndup("1", 1);
			} else {
				expr_copy->value.str.len = 0;
				expr_copy->value.str.val = empty_string;
			}
#else
			if (expr->value.lval) {
				expr_copy->value.str.len = sizeof("true")-1;
				expr_copy->value.str.val = estrndup("true", expr_copy->value.str.len);
			} else {
				expr_copy->value.str.len = sizeof("false")-1;
				expr_copy->value.str.val = estrndup("false", expr_copy->value.str.len);
			}
#endif
			break;
		case IS_RESOURCE:
			expr_copy->value.str.val = (char *) emalloc(sizeof("Resource id #")-1 + MAX_LENGTH_OF_LONG);
			expr_copy->value.str.len = sprintf(expr_copy->value.str.val, "Resource id #%ld", expr->value.lval);
			break;
		case IS_ARRAY:
			expr_copy->value.str.len = sizeof("Array")-1;
			expr_copy->value.str.val = estrndup("Array", expr_copy->value.str.len);
			break;
		case IS_OBJECT: {
				zval function_name;

				function_name.value.str.len = sizeof("to_string")-1;
				function_name.value.str.val = estrndup("to_string", function_name.value.str.len);
				function_name.type = IS_STRING;

				if (call_user_function(NULL, expr, &function_name, expr_copy, 0, NULL)==FAILURE) {
					expr_copy->value.str.len = sizeof("Object")-1;
					expr_copy->value.str.val = estrndup("Object", expr_copy->value.str.len);
				}
				efree(function_name.value.str.val);
			}
			break;
		default:
			*expr_copy = *expr;
			zval_copy_ctor(expr_copy);
			convert_to_string(expr_copy);
			break;
	}
	expr_copy->type = IS_STRING;
	*use_copy = 1;
}


ZEND_API int zend_print_zval(zval *expr, int indent)
{
	zval expr_copy;
	int use_copy;

	zend_make_printable_zval(expr, &expr_copy, &use_copy);
	if (use_copy) {
		expr = &expr_copy;
	}
	if (expr->value.str.len==0) { /* optimize away empty strings */
		if (use_copy) {
			zval_dtor(expr);
		}
		return 0;
	}
	ZEND_WRITE(expr->value.str.val,expr->value.str.len);
	if (use_copy) {
		zval_dtor(expr);
	}
	return expr->value.str.len;
}


ZEND_API void zend_print_zval_r(zval *expr, int indent) 
{
	switch(expr->type) {
		case IS_ARRAY:
			ZEND_PUTS("Array\n");
			print_hash(expr->value.ht,indent);
			break;
		case IS_OBJECT:
			zend_printf("%s Object\n", expr->value.obj.ce->name);
			print_hash(expr->value.obj.properties, indent);
			break;
		default:
			zend_print_variable(expr);
			break;
	}
}


static FILE *zend_fopen_wrapper(const char *filename)
{
	return fopen(filename, "r");
}


static void register_standard_class()
{
	zend_standard_class_def.type = ZEND_INTERNAL_CLASS;
	zend_standard_class_def.name_length = sizeof("stdClass") - 1;
	zend_standard_class_def.name = zend_strndup("stdClass", zend_standard_class_def.name_length);
	zend_standard_class_def.parent = NULL;
	zend_hash_init(&zend_standard_class_def.default_properties, 0, NULL, PVAL_PTR_DTOR, 1);
	zend_hash_init(&zend_standard_class_def.function_table, 0, NULL, ZEND_FUNCTION_DTOR, 1);
	zend_standard_class_def.handle_function_call = NULL;
	zend_standard_class_def.handle_property_get = NULL;
	zend_standard_class_def.handle_property_set = NULL;
	zend_standard_class_def.refcount = (int *) malloc(sizeof(int));
	*zend_standard_class_def.refcount = 1;
	zend_hash_add(GLOBAL_CLASS_TABLE, "stdclass", sizeof("stdclass"), &zend_standard_class_def, sizeof(zend_class_entry), NULL);
}


#ifdef ZTS
static void compiler_globals_ctor(zend_compiler_globals *compiler_globals)
{
	zend_function tmp_func;
	zend_class_entry tmp_class;

	compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
	zend_hash_init(compiler_globals->function_table, 100, NULL, ZEND_FUNCTION_DTOR, 1);
	zend_hash_copy(compiler_globals->function_table, global_function_table, NULL, &tmp_func, sizeof(zend_function));

	compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
	zend_hash_init(compiler_globals->class_table, 10, NULL, ZEND_CLASS_DTOR, 1);
	zend_hash_copy(compiler_globals->class_table, global_class_table, zend_class_add_ref, &tmp_class, sizeof(zend_class_entry));
}


static void compiler_globals_dtor(zend_compiler_globals *compiler_globals)
{
	if (compiler_globals->function_table != global_function_table) {
		zend_hash_destroy(compiler_globals->function_table);
		free(compiler_globals->function_table);
	}
	if (compiler_globals->class_table != global_class_table) {
		zend_hash_destroy(compiler_globals->class_table);
		free(compiler_globals->class_table);
	}
}


static void executor_globals_ctor(zend_executor_globals *executor_globals)
{
	if (global_constants_table) {
		zend_startup_constants(ELS_C);
		zend_copy_constants(executor_globals->zend_constants, global_constants_table);
	}
	init_resource_plist(ELS_C);
}


static void executor_globals_dtor(zend_executor_globals *executor_globals)
{
	zend_shutdown_constants(ELS_C);
	destroy_resource_plist();
}


static void alloc_globals_ctor(zend_alloc_globals *alloc_globals)
{
	start_memory_manager(ALS_C);
}

#endif


int zend_startup(zend_utility_functions *utility_functions, char **extensions)
{
#ifdef ZTS
	zend_compiler_globals *compiler_globals;
	zend_executor_globals *executor_globals;

	alloc_globals_id = ts_allocate_id(sizeof(zend_alloc_globals), alloc_globals_ctor, NULL);
#else
	start_memory_manager(ALS_C);
#endif

	/* Set up utility functions and values */
	zend_error = utility_functions->error_function;
	zend_printf = utility_functions->printf_function;
	zend_write = utility_functions->write_function;
	zend_fopen = utility_functions->fopen_function;
	if (!zend_fopen) {
		zend_fopen = zend_fopen_wrapper;
	}
	zend_message_dispatcher = utility_functions->message_handler;
	zend_block_interruptions = utility_functions->block_interruptions;
	zend_unblock_interruptions = utility_functions->unblock_interruptions;
	zend_get_ini_entry = utility_functions->get_ini_entry;

	zend_compile_files = compile_files;
	zend_execute = execute;

	zend_llist_init(&zend_extensions, sizeof(zend_extension), (void (*)(void *)) zend_extension_dtor, 1);

	/* set up version */
	zend_version_info = strdup(ZEND_CORE_VERSION_INFO);
	zend_version_info_length = sizeof(ZEND_CORE_VERSION_INFO)-1;

	GLOBAL_FUNCTION_TABLE = (HashTable *) malloc(sizeof(HashTable));
	GLOBAL_CLASS_TABLE = (HashTable *) malloc(sizeof(HashTable));
	zend_hash_init(GLOBAL_FUNCTION_TABLE, 100, NULL, ZEND_FUNCTION_DTOR, 1);
	zend_hash_init(GLOBAL_CLASS_TABLE, 10, NULL, ZEND_CLASS_DTOR, 1);
	register_standard_class();
	zend_hash_init(&module_registry, 50, NULL, ZEND_MODULE_DTOR, 1);
	zend_hash_init(&list_destructors, 50, NULL, NULL, 1);

#ifdef ZTS
	global_constants_table = NULL;
	compiler_globals_id = ts_allocate_id(sizeof(zend_compiler_globals), (void (*)(void *)) compiler_globals_ctor, (void (*)(void *)) compiler_globals_dtor);
	executor_globals_id = ts_allocate_id(sizeof(zend_executor_globals), (void (*)(void *)) executor_globals_ctor, (void (*)(void *)) executor_globals_dtor);
	compiler_globals = ts_resource(compiler_globals_id);
	executor_globals = ts_resource(executor_globals_id);
	compiler_globals_dtor(compiler_globals);
	compiler_globals->function_table = GLOBAL_FUNCTION_TABLE;
	compiler_globals->class_table = GLOBAL_CLASS_TABLE;
	zend_startup_constants(executor_globals);
	GLOBAL_CONSTANTS_TABLE = EG(zend_constants);
#else
	zend_startup_constants();
#endif
	zend_register_standard_constants(ELS_C);

#ifndef ZTS
	init_resource_plist(ELS_C);
#endif

	zend_startup_builtin_functions();

	return SUCCESS;
}


void zend_shutdown()
{
#ifndef ZTS
	destroy_resource_plist();
#endif
	zend_hash_destroy(&list_destructors);
	zend_hash_destroy(&module_registry);
	zend_hash_destroy(GLOBAL_FUNCTION_TABLE);
	free(GLOBAL_FUNCTION_TABLE);
	zend_hash_destroy(GLOBAL_CLASS_TABLE);
	free(GLOBAL_CLASS_TABLE);
	zend_shutdown_extensions();
	free(zend_version_info);
#ifndef ZTS
	zend_shutdown_constants(ELS_C);
#endif
}


void zend_set_utility_values(zend_utility_values *utility_values)
{
	zend_uv = *utility_values;
}


/* this should be compatible with the standard zenderror */
void zenderror(char *error)
{
	zend_error(E_PARSE, error);
}


BEGIN_EXTERN_C()
ZEND_API void zend_bailout()
{
	CLS_FETCH();
	ELS_FETCH();

	CG(unclean_shutdown) = 1;
	longjmp(EG(bailout), FAILURE);
}
END_EXTERN_C()


void zend_append_version_info(zend_extension *extension)
{
	char *new_info;
	uint new_info_length;

	new_info_length = sizeof("    with  v,  by \n")
						+ strlen(extension->name)
						+ strlen(extension->version)
						+ strlen(extension->copyright)
						+ strlen(extension->author);

	new_info = (char *) malloc(new_info_length+1);

	sprintf(new_info, "    with %s v%s, %s, by %s\n", extension->name, extension->version, extension->copyright, extension->author);

	zend_version_info = (char *) realloc(zend_version_info, zend_version_info_length+new_info_length+1);
	strcat(zend_version_info, new_info);
	zend_version_info_length += new_info_length;
	free(new_info);
}


ZEND_API char *get_zend_version()
{
	return zend_version_info;
}


void zend_activate(CLS_D ELS_DC)
{
	init_compiler(CLS_C ELS_CC);
	init_executor(CLS_C ELS_CC);
	startup_scanner(CLS_C);
}

void zend_deactivate(CLS_D ELS_DC)
{
	zend_hash_apply(&module_registry, (int (*)(void *)) module_registry_cleanup);
	shutdown_scanner(CLS_C);
	shutdown_executor(ELS_C);
	shutdown_compiler(CLS_C);
}