Skip to content

Commit f5dbebd

Browse files
committed
Accept zend_string instead of zval in zend_compile_string
1 parent 85b5dc4 commit f5dbebd

File tree

9 files changed

+27
-44
lines changed

9 files changed

+27
-44
lines changed

Zend/zend_compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static inline uint32_t zend_alloc_cache_slot(void) {
7979
}
8080

8181
ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
82-
ZEND_API zend_op_array *(*zend_compile_string)(zval *source_string, const char *filename);
82+
ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename);
8383

8484
#ifndef ZTS
8585
ZEND_API zend_compiler_globals compiler_globals;

Zend/zend_compile.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ void zend_file_context_begin(zend_file_context *prev_context);
732732
void zend_file_context_end(zend_file_context *prev_context);
733733

734734
extern ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
735-
extern ZEND_API zend_op_array *(*zend_compile_string)(zval *source_string, const char *filename);
735+
extern ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename);
736736

737737
ZEND_API int ZEND_FASTCALL lex_scan(zval *zendlval, zend_parser_stack_elem *elem);
738738
void startup_scanner(void);
@@ -792,7 +792,7 @@ ZEND_API void function_add_ref(zend_function *function);
792792
struct _zend_arena;
793793

794794
ZEND_API zend_op_array *compile_file(zend_file_handle *file_handle, int type);
795-
ZEND_API zend_op_array *compile_string(zval *source_string, const char *filename);
795+
ZEND_API zend_op_array *compile_string(zend_string *source_string, const char *filename);
796796
ZEND_API zend_op_array *compile_filename(int type, zval *filename);
797797
ZEND_API zend_ast *zend_compile_string_to_ast(
798798
zend_string *code, struct _zend_arena **ast_arena, const char *filename);

Zend/zend_execute.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4217,7 +4217,9 @@ static zend_never_inline zend_op_array* ZEND_FASTCALL zend_include_or_eval(zval
42174217
break;
42184218
case ZEND_EVAL: {
42194219
char *eval_desc = zend_make_compiled_string_description("eval()'d code");
4220-
new_op_array = zend_compile_string(inc_filename, eval_desc);
4220+
zend_string *code = zval_get_string(inc_filename);
4221+
new_op_array = zend_compile_string(code, eval_desc);
4222+
zend_string_release(code);
42214223
efree(eval_desc);
42224224
}
42234225
break;

Zend/zend_execute_API.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,26 +1143,23 @@ ZEND_API zend_object *zend_get_this_object(zend_execute_data *ex) /* {{{ */
11431143

11441144
ZEND_API zend_result zend_eval_stringl(const char *str, size_t str_len, zval *retval_ptr, const char *string_name) /* {{{ */
11451145
{
1146-
zval pv;
11471146
zend_op_array *new_op_array;
11481147
uint32_t original_compiler_options;
11491148
zend_result retval;
1149+
zend_string *code_str;
11501150

11511151
if (retval_ptr) {
1152-
ZVAL_NEW_STR(&pv, zend_string_alloc(str_len + sizeof("return ;")-1, 0));
1153-
memcpy(Z_STRVAL(pv), "return ", sizeof("return ") - 1);
1154-
memcpy(Z_STRVAL(pv) + sizeof("return ") - 1, str, str_len);
1155-
Z_STRVAL(pv)[Z_STRLEN(pv) - 1] = ';';
1156-
Z_STRVAL(pv)[Z_STRLEN(pv)] = '\0';
1152+
code_str = zend_string_concat3(
1153+
"return ", sizeof("return ")-1, str, str_len, ";", sizeof(";")-1);
11571154
} else {
1158-
ZVAL_STRINGL(&pv, str, str_len);
1155+
code_str = zend_string_init(str, str_len, 0);
11591156
}
11601157

11611158
/*printf("Evaluating '%s'\n", pv.value.str.val);*/
11621159

11631160
original_compiler_options = CG(compiler_options);
11641161
CG(compiler_options) = ZEND_COMPILE_DEFAULT_FOR_EVAL;
1165-
new_op_array = zend_compile_string(&pv, string_name);
1162+
new_op_array = zend_compile_string(code_str, string_name);
11661163
CG(compiler_options) = original_compiler_options;
11671164

11681165
if (new_op_array) {
@@ -1200,7 +1197,7 @@ ZEND_API zend_result zend_eval_stringl(const char *str, size_t str_len, zval *re
12001197
} else {
12011198
retval = FAILURE;
12021199
}
1203-
zval_ptr_dtor_str(&pv);
1200+
zend_string_release(code_str);
12041201
return retval;
12051202
}
12061203
/* }}} */

Zend/zend_language_scanner.l

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -804,23 +804,18 @@ ZEND_API size_t zend_get_scanned_file_offset(void)
804804
return offset;
805805
}
806806

807-
zend_op_array *compile_string(zval *source_string, const char *filename)
807+
zend_op_array *compile_string(zend_string *source_string, const char *filename)
808808
{
809809
zend_lex_state original_lex_state;
810810
zend_op_array *op_array = NULL;
811811
zval tmp;
812812

813-
if (UNEXPECTED(Z_TYPE_P(source_string) != IS_STRING)) {
814-
ZVAL_STR(&tmp, zval_get_string_func(source_string));
815-
} else {
816-
ZVAL_COPY(&tmp, source_string);
817-
}
818-
819-
if (Z_STRLEN(tmp)==0) {
820-
zval_ptr_dtor(&tmp);
813+
if (ZSTR_LEN(source_string) == 0) {
821814
return NULL;
822815
}
823816

817+
ZVAL_STR_COPY(&tmp, source_string);
818+
824819
zend_save_lexical_state(&original_lex_state);
825820
zend_prepare_string_for_scanning(&tmp, filename);
826821
BEGIN(ST_IN_SCRIPTING);

sapi/phpdbg/phpdbg.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ ZEND_BEGIN_MODULE_GLOBALS(phpdbg)
275275

276276
zend_op_array *(*compile_file)(zend_file_handle *file_handle, int type);
277277
zend_op_array *(*init_compile_file)(zend_file_handle *file_handle, int type);
278-
zend_op_array *(*compile_string)(zval *source_string, const char *filename);
278+
zend_op_array *(*compile_string)(zend_string *source_string, const char *filename);
279279
HashTable file_sources;
280280

281281
FILE *oplog; /* opline log */

sapi/phpdbg/phpdbg_bp.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ static inline void phpdbg_create_conditional_break(phpdbg_breakcond_t *brake, co
828828
{
829829
phpdbg_breakcond_t new_break;
830830
uint32_t cops = CG(compiler_options);
831-
zval pv;
831+
zend_string *bp_code;
832832

833833
switch (param->type) {
834834
case STR_PARAM:
@@ -877,16 +877,10 @@ static inline void phpdbg_create_conditional_break(phpdbg_breakcond_t *brake, co
877877
new_break.code = estrndup(expr, expr_len);
878878
new_break.code_len = expr_len;
879879

880-
Z_STR(pv) = zend_string_alloc(expr_len + sizeof("return ;") - 1, 0);
881-
memcpy(Z_STRVAL(pv), "return ", sizeof("return ") - 1);
882-
memcpy(Z_STRVAL(pv) + sizeof("return ") - 1, expr, expr_len);
883-
Z_STRVAL(pv)[Z_STRLEN(pv) - 1] = ';';
884-
Z_STRVAL(pv)[Z_STRLEN(pv)] = '\0';
885-
Z_TYPE_INFO(pv) = IS_STRING;
886-
887-
new_break.ops = zend_compile_string(&pv, "Conditional Breakpoint Code");
888-
889-
zval_ptr_dtor_str(&pv);
880+
bp_code = zend_string_concat3(
881+
"return ", sizeof("return ")-1, expr, expr_len, ";", sizeof(";")-1);
882+
new_break.ops = zend_compile_string(bp_code, "Conditional Breakpoint Code");
883+
zend_string_release(bp_code);
890884

891885
if (new_break.ops) {
892886
brake = zend_hash_index_update_mem(&PHPDBG_G(bp)[PHPDBG_BREAK_COND], hash, &new_break, sizeof(phpdbg_breakcond_t));

sapi/phpdbg/phpdbg_list.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ zend_op_array *phpdbg_init_compile_file(zend_file_handle *file, int type) {
316316
return op_array;
317317
}
318318

319-
zend_op_array *phpdbg_compile_string(zval *source_string, const char *filename) {
319+
zend_op_array *phpdbg_compile_string(zend_string *source_string, const char *filename) {
320320
zend_string *fake_name;
321321
zend_op_array *op_array;
322322
phpdbg_file_source *dataptr;
@@ -327,9 +327,9 @@ zend_op_array *phpdbg_compile_string(zval *source_string, const char *filename)
327327
return PHPDBG_G(compile_string)(source_string, filename);
328328
}
329329

330-
dataptr = emalloc(sizeof(phpdbg_file_source) + sizeof(uint32_t) * Z_STRLEN_P(source_string));
331-
dataptr->buf = estrndup(Z_STRVAL_P(source_string), Z_STRLEN_P(source_string));
332-
dataptr->len = Z_STRLEN_P(source_string);
330+
dataptr = emalloc(sizeof(phpdbg_file_source) + sizeof(uint32_t) * ZSTR_LEN(source_string));
331+
dataptr->buf = estrndup(ZSTR_VAL(source_string), ZSTR_LEN(source_string));
332+
dataptr->len = ZSTR_LEN(source_string);
333333
dataptr->line[0] = 0;
334334
for (line = 0, bufptr = dataptr->buf - 1, endptr = dataptr->buf + dataptr->len; ++bufptr < endptr;) {
335335
if (*bufptr == '\n') {

sapi/phpdbg/phpdbg_prompt.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -519,12 +519,7 @@ PHPDBG_COMMAND(stdin)
519519
} /* }}} */
520520

521521
int phpdbg_compile_stdin(zend_string *code) {
522-
zval zv;
523-
524-
ZVAL_STR(&zv, code);
525-
526-
PHPDBG_G(ops) = zend_compile_string(&zv, "Standard input code");
527-
522+
PHPDBG_G(ops) = zend_compile_string(code, "Standard input code");
528523
zend_string_release(code);
529524

530525
if (EG(exception)) {

0 commit comments

Comments
 (0)