Skip to content

Commit 72f459a

Browse files
authored
Use zend_compile_string_to_ast in php 8.1 instead (nikic#196)
Fixes a compilation error in php 8.1-dev
1 parent 22be8b1 commit 72f459a

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

ast.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,18 @@ static inline void ast_update_property_long(zval *object, zend_string *name, zen
340340
ast_update_property(object, name, &value_zv, cache_slot);
341341
}
342342

343-
static zend_ast *get_ast(zend_string *code, zend_arena **ast_arena, char *filename) {
343+
static zend_ast *get_ast(zend_string *code, zend_arena **ast_arena, zend_string *filename) {
344+
#if PHP_VERSION_ID >= 80100
345+
if (filename) {
346+
return zend_compile_string_to_ast(code, ast_arena, filename);
347+
} else {
348+
zend_ast *ast;
349+
filename = zend_string_init("string code", sizeof("string code") - 1, 0);
350+
ast = zend_compile_string_to_ast(code, ast_arena, filename);
351+
zend_string_release_ex(filename, 0);
352+
return ast;
353+
}
354+
#else
344355
zval code_zv;
345356
zend_bool original_in_compilation;
346357
zend_lex_state original_lex_state;
@@ -352,7 +363,7 @@ static zend_ast *get_ast(zend_string *code, zend_arena **ast_arena, char *filena
352363
CG(in_compilation) = 1;
353364

354365
zend_save_lexical_state(&original_lex_state);
355-
zend_prepare_string_for_scanning(&code_zv, filename);
366+
zend_prepare_string_for_scanning(&code_zv, filename ? filename->val : "string code");
356367
CG(ast) = NULL;
357368
CG(ast_arena) = zend_arena_create(1024 * 32);
358369
LANG_SCNG(yy_state) = yycINITIAL;
@@ -373,6 +384,7 @@ static zend_ast *get_ast(zend_string *code, zend_arena **ast_arena, char *filena
373384
zval_dtor(&code_zv);
374385

375386
return ast;
387+
#endif
376388
}
377389

378390
/* Returns whether node->attr (i.e. flags) is used by this node kind. Not to be confused with php 8.0's attributes. */
@@ -1106,7 +1118,7 @@ PHP_FUNCTION(parse_file) {
11061118
code = ZSTR_EMPTY_ALLOC();
11071119
}
11081120

1109-
ast = get_ast(code, &arena, filename->val);
1121+
ast = get_ast(code, &arena, filename);
11101122
if (!ast) {
11111123
zend_string_free(code);
11121124
return;
@@ -1136,7 +1148,7 @@ PHP_FUNCTION(parse_code) {
11361148
return;
11371149
}
11381150

1139-
ast = get_ast(code, &arena, filename ? filename->val : "string code");
1151+
ast = get_ast(code, &arena, filename);
11401152
if (!ast) {
11411153
return;
11421154
}

0 commit comments

Comments
 (0)