zend.c revision a6519d05
1573b4602SAndi Gutmans/*
2573b4602SAndi Gutmans   +----------------------------------------------------------------------+
3573b4602SAndi Gutmans   | Zend Engine                                                          |
4573b4602SAndi Gutmans   +----------------------------------------------------------------------+
5a6519d05SXinchen Hui   | Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
6573b4602SAndi Gutmans   +----------------------------------------------------------------------+
7d863d52aSSebastian Bergmann   | This source file is subject to version 2.00 of the Zend license,     |
8b5b11177SZeev Suraski   | that is bundled with this package in the file LICENSE, and is        |
9f68c7ff2SJames Cox   | available through the world-wide-web at the following url:           |
10d863d52aSSebastian Bergmann   | http://www.zend.com/license/2_00.txt.                                |
11b5b11177SZeev Suraski   | If you did not receive a copy of the Zend license and are unable to  |
12b5b11177SZeev Suraski   | obtain it through the world-wide-web, please send a note to          |
13b5b11177SZeev Suraski   | license@zend.com so we can mail you a copy immediately.              |
14573b4602SAndi Gutmans   +----------------------------------------------------------------------+
15573b4602SAndi Gutmans   | Authors: Andi Gutmans <andi@zend.com>                                |
16573b4602SAndi Gutmans   |          Zeev Suraski <zeev@zend.com>                                |
17573b4602SAndi Gutmans   +----------------------------------------------------------------------+
18573b4602SAndi Gutmans*/
19573b4602SAndi Gutmans
20333406bdSfoobar/* $Id$ */
21a6043d3eSRasmus Lerdorf
22573b4602SAndi Gutmans#include "zend.h"
23573b4602SAndi Gutmans#include "zend_extensions.h"
247080fa5bSAndi Gutmans#include "zend_modules.h"
25573b4602SAndi Gutmans#include "zend_constants.h"
26573b4602SAndi Gutmans#include "zend_list.h"
27da9faa2cSZeev Suraski#include "zend_API.h"
2800ed3badSZeev Suraski#include "zend_exceptions.h"
2913d840bcSZeev Suraski#include "zend_builtin_functions.h"
3054d53055SZeev Suraski#include "zend_ini.h"
31fcaa1c6dSDmitry Stogov#include "zend_vm.h"
3208b9fdc8SDavid Soria Parra#include "zend_dtrace.h"
33cf6ab0e9SAnatol Belski#include "zend_virtual_cwd.h"
341b29e0caSNikita Popov#include "zend_smart_str.h"
355fc9aa9aSNikita Popov#include "zend_smart_string.h"
36573b4602SAndi Gutmans
377a87fcbbSZeev Suraski#ifdef ZTS
38cd2b462aSAnatol BelskiZEND_API int compiler_globals_id;
39cd2b462aSAnatol BelskiZEND_API int executor_globals_id;
40cd2b462aSAnatol Belskistatic HashTable *global_function_table = NULL;
41cd2b462aSAnatol Belskistatic HashTable *global_class_table = NULL;
42cd2b462aSAnatol Belskistatic HashTable *global_constants_table = NULL;
43cd2b462aSAnatol Belskistatic HashTable *global_auto_globals_table = NULL;
44cd2b462aSAnatol Belskistatic HashTable *global_persistent_list = NULL;
45cd2b462aSAnatol BelskiZEND_TSRMLS_CACHE_DEFINE()
46b4892511SJani Taskinen# define GLOBAL_FUNCTION_TABLE		global_function_table
47b4892511SJani Taskinen# define GLOBAL_CLASS_TABLE			global_class_table
48b4892511SJani Taskinen# define GLOBAL_CONSTANTS_TABLE		global_constants_table
49b4892511SJani Taskinen# define GLOBAL_AUTO_GLOBALS_TABLE	global_auto_globals_table
507a87fcbbSZeev Suraski#else
51b4892511SJani Taskinen# define GLOBAL_FUNCTION_TABLE		CG(function_table)
52b4892511SJani Taskinen# define GLOBAL_CLASS_TABLE			CG(class_table)
53b4892511SJani Taskinen# define GLOBAL_AUTO_GLOBALS_TABLE	CG(auto_globals)
54b4892511SJani Taskinen# define GLOBAL_CONSTANTS_TABLE		EG(zend_constants)
557a87fcbbSZeev Suraski#endif
567a87fcbbSZeev Suraski
57cd2b462aSAnatol BelskiZEND_API zend_utility_values zend_uv;
58fa45f64bSRemi ColletZEND_API zend_bool zend_dtrace_enabled;
59cd2b462aSAnatol Belski
60cd2b462aSAnatol Belski/* version information */
61cd2b462aSAnatol Belskistatic char *zend_version_info;
62cd2b462aSAnatol Belskistatic uint32_t zend_version_info_length;
63a6519d05SXinchen Hui#define ZEND_CORE_VERSION_INFO	"Zend Engine v" ZEND_VERSION ", Copyright (c) 1998-2018 Zend Technologies\n"
64cd2b462aSAnatol Belski#define PRINT_ZVAL_INDENT 4
65cd2b462aSAnatol Belski
66573b4602SAndi Gutmans/* true multithread-shared globals */
6792dd5e61SStanislav MalyshevZEND_API zend_class_entry *zend_standard_class_def = NULL;
68c3e3c98eSAnatol BelskiZEND_API size_t (*zend_printf)(const char *format, ...);
695dba4774SZeev SuraskiZEND_API zend_write_func_t zend_write;
702fa8d67aSDmitry StogovZEND_API FILE *(*zend_fopen)(const char *filename, zend_string **opened_path);
71bdeb220fSAnatol BelskiZEND_API int (*zend_stream_open_function)(const char *filename, zend_file_handle *handle);
72bdeb220fSAnatol BelskiZEND_API void (*zend_ticks_function)(int ticks);
73d0460d8fSDmitry StogovZEND_API void (*zend_interrupt_function)(zend_execute_data *execute_data);
74b204b3abSAnatol BelskiZEND_API void (*zend_error_cb)(int type, const char *error_filename, const uint32_t error_lineno, const char *format, va_list args);
755fc9aa9aSNikita Popovvoid (*zend_printf_to_smart_string)(smart_string *buf, const char *format, va_list ap);
765fc9aa9aSNikita Popovvoid (*zend_printf_to_smart_str)(smart_str *buf, const char *format, va_list ap);
77bdeb220fSAnatol BelskiZEND_API char *(*zend_getenv)(char *name, size_t name_len);
7849d9b301SAnatol BelskiZEND_API zend_string *(*zend_resolve_path)(const char *filename, size_t filename_len);
79b4903aefSDmitry StogovZEND_API int (*zend_post_startup_cb)(void) = NULL;
80536221beSStanislav Malyshev
81bdeb220fSAnatol Belskivoid (*zend_on_timeout)(int seconds);
82bd115087SZeev Suraski
83bdeb220fSAnatol Belskistatic void (*zend_message_dispatcher_p)(zend_long message, const void *data);
8488d7ca44SDmitry Stogovstatic zval *(*zend_get_configuration_directive_p)(zend_string *name);
85d035af4aSZeev Suraski
8649ea143bSDmitry Stogov#if ZEND_RC_DEBUG
8749ea143bSDmitry StogovZEND_API zend_bool zend_rc_debug = 0;
8849ea143bSDmitry Stogov#endif
8949ea143bSDmitry Stogov
90b4892511SJani Taskinenstatic ZEND_INI_MH(OnUpdateErrorReporting) /* {{{ */
914be862b0SZeev Suraski{
924be862b0SZeev Suraski	if (!new_value) {
93d3e50265SMarcus Boerger		EG(error_reporting) = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED;
944be862b0SZeev Suraski	} else {
957aa76271SDmitry Stogov		EG(error_reporting) = atoi(ZSTR_VAL(new_value));
964be862b0SZeev Suraski	}
974be862b0SZeev Suraski	return SUCCESS;
984be862b0SZeev Suraski}
99b4892511SJani Taskinen/* }}} */
1004be862b0SZeev Suraski
10171592cecSDmitry Stogovstatic ZEND_INI_MH(OnUpdateGCEnabled) /* {{{ */
10271592cecSDmitry Stogov{
103bdeb220fSAnatol Belski	OnUpdateBool(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
10471592cecSDmitry Stogov
10571592cecSDmitry Stogov	if (GC_G(gc_enabled)) {
106bdeb220fSAnatol Belski		gc_init();
10771592cecSDmitry Stogov	}
10871592cecSDmitry Stogov
10971592cecSDmitry Stogov	return SUCCESS;
11071592cecSDmitry Stogov}
11171592cecSDmitry Stogov/* }}} */
11271592cecSDmitry Stogov
113bbf3d43cSMoriyoshi Koizumistatic ZEND_INI_MH(OnUpdateScriptEncoding) /* {{{ */
114bbf3d43cSMoriyoshi Koizumi{
115e46852d2SAnatol Belski	if (!CG(multibyte)) {
116bbf3d43cSMoriyoshi Koizumi		return FAILURE;
117bbf3d43cSMoriyoshi Koizumi	}
118bdeb220fSAnatol Belski	if (!zend_multibyte_get_functions()) {
119bbf3d43cSMoriyoshi Koizumi		return SUCCESS;
120bbf3d43cSMoriyoshi Koizumi	}
1217aa76271SDmitry Stogov	return zend_multibyte_set_script_encoding_by_string(new_value ? ZSTR_VAL(new_value) : NULL, new_value ? ZSTR_LEN(new_value) : 0);
122bbf3d43cSMoriyoshi Koizumi}
123bbf3d43cSMoriyoshi Koizumi/* }}} */
124bbf3d43cSMoriyoshi Koizumi
1259a20323eSDmitry Stogovstatic ZEND_INI_MH(OnUpdateAssertions) /* {{{ */
1269a20323eSDmitry Stogov{
1279a20323eSDmitry Stogov	zend_long *p, val;
1289a20323eSDmitry Stogov#ifndef ZTS
1299a20323eSDmitry Stogov	char *base = (char *) mh_arg2;
1309a20323eSDmitry Stogov#else
1319a20323eSDmitry Stogov	char *base;
1329a20323eSDmitry Stogov
1339a20323eSDmitry Stogov	base = (char *) ts_resource(*((int *) mh_arg2));
1349a20323eSDmitry Stogov#endif
1359a20323eSDmitry Stogov
1369a20323eSDmitry Stogov	p = (zend_long *) (base+(size_t) mh_arg1);
1379a20323eSDmitry Stogov
1380d1eeeb6SAnatol Belski	val = zend_atol(ZSTR_VAL(new_value), ZSTR_LEN(new_value));
1399a20323eSDmitry Stogov
1409a20323eSDmitry Stogov	if (stage != ZEND_INI_STAGE_STARTUP &&
1419a20323eSDmitry Stogov	    stage != ZEND_INI_STAGE_SHUTDOWN &&
1429a20323eSDmitry Stogov	    *p != val &&
1439a20323eSDmitry Stogov	    (*p < 0 || val < 0)) {
1449a20323eSDmitry Stogov		zend_error(E_WARNING, "zend.assertions may be completely enabled or disabled only in php.ini");
1459a20323eSDmitry Stogov		return FAILURE;
1469a20323eSDmitry Stogov	}
1479a20323eSDmitry Stogov
1489a20323eSDmitry Stogov	*p = val;
1499a20323eSDmitry Stogov	return SUCCESS;
1509a20323eSDmitry Stogov}
1519a20323eSDmitry Stogov/* }}} */
152bbf3d43cSMoriyoshi Koizumi
1534be862b0SZeev SuraskiZEND_INI_BEGIN()
154eb6fd52eSZeev Suraski	ZEND_INI_ENTRY("error_reporting",				NULL,		ZEND_INI_ALL,		OnUpdateErrorReporting)
155fb2c890cSRasmus Lerdorf	STD_ZEND_INI_ENTRY("zend.assertions",				"1",    ZEND_INI_ALL,       OnUpdateAssertions,           assertions,   zend_executor_globals,  executor_globals)
15671592cecSDmitry Stogov	STD_ZEND_INI_BOOLEAN("zend.enable_gc",				"1",	ZEND_INI_ALL,		OnUpdateGCEnabled,      gc_enabled,     zend_gc_globals,        gc_globals)
157ab93d8c6SDmitry Stogov 	STD_ZEND_INI_BOOLEAN("zend.multibyte", "0", ZEND_INI_PERDIR, OnUpdateBool, multibyte,      zend_compiler_globals, compiler_globals)
158bbf3d43cSMoriyoshi Koizumi 	ZEND_INI_ENTRY("zend.script_encoding",			NULL,		ZEND_INI_ALL,		OnUpdateScriptEncoding)
159bbf3d43cSMoriyoshi Koizumi 	STD_ZEND_INI_BOOLEAN("zend.detect_unicode",			"1",	ZEND_INI_ALL,		OnUpdateBool, detect_unicode, zend_compiler_globals, compiler_globals)
16034d93f0cSIlia Alshanetsky#ifdef ZEND_SIGNALS
16134d93f0cSIlia Alshanetsky	STD_ZEND_INI_BOOLEAN("zend.signal_check", "0", ZEND_INI_SYSTEM, OnUpdateBool, check, zend_signal_globals_t, zend_signal_globals)
16234d93f0cSIlia Alshanetsky#endif
1634be862b0SZeev SuraskiZEND_INI_END()
1644be862b0SZeev Suraski
1655fc9aa9aSNikita PopovZEND_API size_t zend_vspprintf(char **pbuf, size_t max_len, const char *format, va_list ap) /* {{{ */
1665fc9aa9aSNikita Popov{
1675fc9aa9aSNikita Popov	smart_string buf = {0};
1685fc9aa9aSNikita Popov
1695fc9aa9aSNikita Popov	/* since there are places where (v)spprintf called without checking for null,
1705fc9aa9aSNikita Popov	   a bit of defensive coding here */
1715fc9aa9aSNikita Popov	if (!pbuf) {
1725fc9aa9aSNikita Popov		return 0;
1735fc9aa9aSNikita Popov	}
1745fc9aa9aSNikita Popov
1755fc9aa9aSNikita Popov	zend_printf_to_smart_string(&buf, format, ap);
1765fc9aa9aSNikita Popov
1775fc9aa9aSNikita Popov	if (max_len && buf.len > max_len) {
1785fc9aa9aSNikita Popov		buf.len = max_len;
1795fc9aa9aSNikita Popov	}
1805fc9aa9aSNikita Popov
1815fc9aa9aSNikita Popov	smart_string_0(&buf);
1825fc9aa9aSNikita Popov
1835fc9aa9aSNikita Popov	if (buf.c) {
1845fc9aa9aSNikita Popov		*pbuf = buf.c;
1855fc9aa9aSNikita Popov		return buf.len;
1865fc9aa9aSNikita Popov	} else {
1875fc9aa9aSNikita Popov		*pbuf = estrndup("", 0);
1885fc9aa9aSNikita Popov		return 0;
1895fc9aa9aSNikita Popov	}
1905fc9aa9aSNikita Popov}
1915fc9aa9aSNikita Popov/* }}} */
1925fc9aa9aSNikita Popov
193c7742e28SNikita PopovZEND_API size_t zend_spprintf(char **message, size_t max_len, const char *format, ...) /* {{{ */
194c7742e28SNikita Popov{
195c7742e28SNikita Popov	va_list arg;
196c7742e28SNikita Popov	size_t len;
197c7742e28SNikita Popov
198c7742e28SNikita Popov	va_start(arg, format);
199c7742e28SNikita Popov	len = zend_vspprintf(message, max_len, format, arg);
200c7742e28SNikita Popov	va_end(arg);
201c7742e28SNikita Popov	return len;
202c7742e28SNikita Popov}
203c7742e28SNikita Popov/* }}} */
204c7742e28SNikita Popov
20526f8fc83SNikita PopovZEND_API size_t zend_spprintf_unchecked(char **message, size_t max_len, const char *format, ...) /* {{{ */
20626f8fc83SNikita Popov{
20726f8fc83SNikita Popov	va_list arg;
20826f8fc83SNikita Popov	size_t len;
20926f8fc83SNikita Popov
21026f8fc83SNikita Popov	va_start(arg, format);
21126f8fc83SNikita Popov	len = zend_vspprintf(message, max_len, format, arg);
21226f8fc83SNikita Popov	va_end(arg);
21326f8fc83SNikita Popov	return len;
21426f8fc83SNikita Popov}
21526f8fc83SNikita Popov/* }}} */
21626f8fc83SNikita Popov
2175fc9aa9aSNikita PopovZEND_API zend_string *zend_vstrpprintf(size_t max_len, const char *format, va_list ap) /* {{{ */
2185fc9aa9aSNikita Popov{
2195fc9aa9aSNikita Popov	smart_str buf = {0};
2205fc9aa9aSNikita Popov
2215fc9aa9aSNikita Popov	zend_printf_to_smart_str(&buf, format, ap);
2225fc9aa9aSNikita Popov
2235fc9aa9aSNikita Popov	if (!buf.s) {
2245fc9aa9aSNikita Popov		return ZSTR_EMPTY_ALLOC();
2255fc9aa9aSNikita Popov	}
2265fc9aa9aSNikita Popov
2275fc9aa9aSNikita Popov	if (max_len && ZSTR_LEN(buf.s) > max_len) {
2285fc9aa9aSNikita Popov		ZSTR_LEN(buf.s) = max_len;
2295fc9aa9aSNikita Popov	}
2305fc9aa9aSNikita Popov
2315fc9aa9aSNikita Popov	smart_str_0(&buf);
2325fc9aa9aSNikita Popov	return buf.s;
2335fc9aa9aSNikita Popov}
2345fc9aa9aSNikita Popov/* }}} */
2355fc9aa9aSNikita Popov
236c7742e28SNikita PopovZEND_API zend_string *zend_strpprintf(size_t max_len, const char *format, ...) /* {{{ */
237c7742e28SNikita Popov{
238c7742e28SNikita Popov	va_list arg;
239c7742e28SNikita Popov	zend_string *str;
240c7742e28SNikita Popov
241c7742e28SNikita Popov	va_start(arg, format);
242c7742e28SNikita Popov	str = zend_vstrpprintf(max_len, format, arg);
243c7742e28SNikita Popov	va_end(arg);
244c7742e28SNikita Popov	return str;
245c7742e28SNikita Popov}
246c7742e28SNikita Popov/* }}} */
247c7742e28SNikita Popov
24826f8fc83SNikita PopovZEND_API zend_string *zend_strpprintf_unchecked(size_t max_len, const char *format, ...) /* {{{ */
24926f8fc83SNikita Popov{
25026f8fc83SNikita Popov	va_list arg;
25126f8fc83SNikita Popov	zend_string *str;
25226f8fc83SNikita Popov
25326f8fc83SNikita Popov	va_start(arg, format);
25426f8fc83SNikita Popov	str = zend_vstrpprintf(max_len, format, arg);
25526f8fc83SNikita Popov	va_end(arg);
25626f8fc83SNikita Popov	return str;
25726f8fc83SNikita Popov}
25826f8fc83SNikita Popov/* }}} */
25926f8fc83SNikita Popov
2601b29e0caSNikita Popovstatic void zend_print_zval_r_to_buf(smart_str *buf, zval *expr, int indent);
2611b29e0caSNikita Popov
2621b29e0caSNikita Popovstatic void print_hash(smart_str *buf, HashTable *ht, int indent, zend_bool is_object) /* {{{ */
263573b4602SAndi Gutmans{
264f4cfaf36SDmitry Stogov	zval *tmp;
265f4cfaf36SDmitry Stogov	zend_string *string_key;
266c3e3c98eSAnatol Belski	zend_ulong num_key;
267573b4602SAndi Gutmans	int i;
268573b4602SAndi Gutmans
269b4892511SJani Taskinen	for (i = 0; i < indent; i++) {
2701b29e0caSNikita Popov		smart_str_appendc(buf, ' ');
271573b4602SAndi Gutmans	}
2721b29e0caSNikita Popov	smart_str_appends(buf, "(\n");
27380bab9d9SAndrei Zmievski	indent += PRINT_ZVAL_INDENT;
2741b29e0caSNikita Popov	ZEND_HASH_FOREACH_KEY_VAL_IND(ht, num_key, string_key, tmp) {
275b4892511SJani Taskinen		for (i = 0; i < indent; i++) {
2761b29e0caSNikita Popov			smart_str_appendc(buf, ' ');
277573b4602SAndi Gutmans		}
2781b29e0caSNikita Popov		smart_str_appendc(buf, '[');
2797652a977SDmitry Stogov		if (string_key) {
2807652a977SDmitry Stogov			if (is_object) {
2817652a977SDmitry Stogov				const char *prop_name, *class_name;
282079409bbSNikita Popov				size_t prop_len;
283079409bbSNikita Popov				int mangled = zend_unmangle_property_name_ex(string_key, &class_name, &prop_name, &prop_len);
2847652a977SDmitry Stogov
2851b29e0caSNikita Popov				smart_str_appendl(buf, prop_name, prop_len);
2867652a977SDmitry Stogov				if (class_name && mangled == SUCCESS) {
2871b29e0caSNikita Popov					if (class_name[0] == '*') {
2881b29e0caSNikita Popov						smart_str_appends(buf, ":protected");
2897652a977SDmitry Stogov					} else {
2901b29e0caSNikita Popov						smart_str_appends(buf, ":");
2911b29e0caSNikita Popov						smart_str_appends(buf, class_name);
2921b29e0caSNikita Popov						smart_str_appends(buf, ":private");
293d329ce93SZeev Suraski					}
294d3d02fbcSDmitry Stogov				}
2957652a977SDmitry Stogov			} else {
2961b29e0caSNikita Popov				smart_str_append(buf, string_key);
2977652a977SDmitry Stogov			}
2987652a977SDmitry Stogov		} else {
2991b29e0caSNikita Popov			smart_str_append_long(buf, num_key);
300573b4602SAndi Gutmans		}
3011b29e0caSNikita Popov		smart_str_appends(buf, "] => ");
3021b29e0caSNikita Popov		zend_print_zval_r_to_buf(buf, tmp, indent+PRINT_ZVAL_INDENT);
3031b29e0caSNikita Popov		smart_str_appends(buf, "\n");
3047652a977SDmitry Stogov	} ZEND_HASH_FOREACH_END();
30580bab9d9SAndrei Zmievski	indent -= PRINT_ZVAL_INDENT;
306b4892511SJani Taskinen	for (i = 0; i < indent; i++) {
3071b29e0caSNikita Popov		smart_str_appendc(buf, ' ');
308573b4602SAndi Gutmans	}
3091b29e0caSNikita Popov	smart_str_appends(buf, ")\n");
310573b4602SAndi Gutmans}
311b4892511SJani Taskinen/* }}} */
312573b4602SAndi Gutmans
313bdeb220fSAnatol Belskistatic void print_flat_hash(HashTable *ht) /* {{{ */
31427de54b0SAndi Gutmans{
315f4cfaf36SDmitry Stogov	zval *tmp;
316f4cfaf36SDmitry Stogov	zend_string *string_key;
317c3e3c98eSAnatol Belski	zend_ulong num_key;
318b4892511SJani Taskinen	int i = 0;
319b4892511SJani Taskinen
3200ade00c8SNikita Popov	ZEND_HASH_FOREACH_KEY_VAL_IND(ht, num_key, string_key, tmp) {
321b4892511SJani Taskinen		if (i++ > 0) {
322b4892511SJani Taskinen			ZEND_PUTS(",");
323b4892511SJani Taskinen		}
324b4892511SJani Taskinen		ZEND_PUTS("[");
3257652a977SDmitry Stogov		if (string_key) {
3267aa76271SDmitry Stogov			ZEND_WRITE(ZSTR_VAL(string_key), ZSTR_LEN(string_key));
3277652a977SDmitry Stogov		} else {
3284d997f63SAnatol Belski			zend_printf(ZEND_ULONG_FMT, num_key);
329b4892511SJani Taskinen		}
330b4892511SJani Taskinen		ZEND_PUTS("] => ");
331bdeb220fSAnatol Belski		zend_print_flat_zval_r(tmp);
3327652a977SDmitry Stogov	} ZEND_HASH_FOREACH_END();
33327de54b0SAndi Gutmans}
334b4892511SJani Taskinen/* }}} */
3357a87fcbbSZeev Suraski
336bdeb220fSAnatol BelskiZEND_API int zend_make_printable_zval(zval *expr, zval *expr_copy) /* {{{ */
337573b4602SAndi Gutmans{
338f3a6c30cSXinchen Hui	if (Z_TYPE_P(expr) == IS_STRING) {
3399d6e45a0SDmitry Stogov		return 0;
34022fbfdfbSXinchen Hui	} else {
341ce18738aSDmitry Stogov		ZVAL_STR(expr_copy, zval_get_string_func(expr));
34222fbfdfbSXinchen Hui		return 1;
343da9faa2cSZeev Suraski	}
344da9faa2cSZeev Suraski}
345b4892511SJani Taskinen/* }}} */
346da9faa2cSZeev Suraski
347bdeb220fSAnatol BelskiZEND_API size_t zend_print_zval(zval *expr, int indent) /* {{{ */
348da9faa2cSZeev Suraski{
349ccc12b82SDmitry Stogov	zend_string *tmp_str;
350ccc12b82SDmitry Stogov	zend_string *str = zval_get_tmp_string(expr, &tmp_str);
3517aa76271SDmitry Stogov	size_t len = ZSTR_LEN(str);
352da9faa2cSZeev Suraski
353bda96e3cSNikita Popov	if (len != 0) {
3541b29e0caSNikita Popov		zend_write(ZSTR_VAL(str), len);
355573b4602SAndi Gutmans	}
356bda96e3cSNikita Popov
357ccc12b82SDmitry Stogov	zend_tmp_string_release(tmp_str);
358bda96e3cSNikita Popov	return len;
359573b4602SAndi Gutmans}
360b4892511SJani Taskinen/* }}} */
361573b4602SAndi Gutmans
362bdeb220fSAnatol BelskiZEND_API void zend_print_flat_zval_r(zval *expr) /* {{{ */
36327de54b0SAndi Gutmans{
364b4892511SJani Taskinen	switch (Z_TYPE_P(expr)) {
365b4892511SJani Taskinen		case IS_ARRAY:
366b4892511SJani Taskinen			ZEND_PUTS("Array (");
367cb9d81efSDmitry Stogov			if (Z_REFCOUNTED_P(expr)) {
368cb9d81efSDmitry Stogov				if (Z_IS_RECURSIVE_P(expr)) {
369cb9d81efSDmitry Stogov					ZEND_PUTS(" *RECURSION*");
370cb9d81efSDmitry Stogov					return;
371cb9d81efSDmitry Stogov				}
372cb9d81efSDmitry Stogov				Z_PROTECT_RECURSION_P(expr);
373b4892511SJani Taskinen			}
374bdeb220fSAnatol Belski			print_flat_hash(Z_ARRVAL_P(expr));
375b4892511SJani Taskinen			ZEND_PUTS(")");
376cb9d81efSDmitry Stogov			if (Z_REFCOUNTED_P(expr)) {
377cb9d81efSDmitry Stogov				Z_UNPROTECT_RECURSION_P(expr);
378b3b616cfSDmitry Stogov			}
379b4892511SJani Taskinen			break;
380b4892511SJani Taskinen		case IS_OBJECT:
381b4892511SJani Taskinen		{
3829bba373aSStanislav Malyshev			HashTable *properties = NULL;
383bdeb220fSAnatol Belski			zend_string *class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr));
3847aa76271SDmitry Stogov			zend_printf("%s Object (", ZSTR_VAL(class_name));
385df79b9b2SNikita Popov			zend_string_release(class_name);
3868ce1211aSMarcus Boerger
387cb9d81efSDmitry Stogov			if (Z_IS_RECURSIVE_P(expr)) {
388dc764bf6SNikita Popov				ZEND_PUTS(" *RECURSION*");
389dc764bf6SNikita Popov				return;
390dc764bf6SNikita Popov			}
391dc764bf6SNikita Popov
392f30f3590SStanislav Malyshev			if (Z_OBJ_HANDLER_P(expr, get_properties)) {
3939bba373aSStanislav Malyshev				properties = Z_OBJPROP_P(expr);
3949bba373aSStanislav Malyshev			}
395f30f3590SStanislav Malyshev			if (properties) {
396cb9d81efSDmitry Stogov				Z_PROTECT_RECURSION_P(expr);
397bdeb220fSAnatol Belski				print_flat_hash(properties);
398cb9d81efSDmitry Stogov				Z_UNPROTECT_RECURSION_P(expr);
3999bba373aSStanislav Malyshev			}
4009bba373aSStanislav Malyshev			ZEND_PUTS(")");
4019bba373aSStanislav Malyshev			break;
402b4892511SJani Taskinen		}
403b0037688SBob Weinand		case IS_REFERENCE:
404b0037688SBob Weinand			zend_print_flat_zval_r(Z_REFVAL_P(expr));
405b0037688SBob Weinand			break;
406b4892511SJani Taskinen		default:
407bdeb220fSAnatol Belski			zend_print_variable(expr);
408b4892511SJani Taskinen			break;
409