mirror of
https://github.com/php/php-src.git
synced 2025-08-15 21:48:51 +02:00
Make sure TOKEN_PARSE mode is thread safe
Introduce an on_event_context passed to the on_event hook. Use this context to pass along the token array. Previously this was stored in a non-tls global :/
This commit is contained in:
parent
40b312d4c2
commit
07af6ba898
5 changed files with 378 additions and 362 deletions
|
@ -292,7 +292,8 @@ struct _zend_php_scanner_globals {
|
|||
int scanned_string_len;
|
||||
|
||||
/* hooks */
|
||||
void (* on_event)(zend_php_scanner_event event, int token, int line);
|
||||
void (*on_event)(zend_php_scanner_event event, int token, int line, void *context);
|
||||
void *on_event_context;
|
||||
};
|
||||
|
||||
#endif /* ZEND_GLOBALS_H */
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -51,7 +51,8 @@ typedef struct _zend_lex_state {
|
|||
const zend_encoding *script_encoding;
|
||||
|
||||
/* hooks */
|
||||
void (* on_event)(zend_php_scanner_event event, int token, int line);
|
||||
void (*on_event)(zend_php_scanner_event event, int token, int line, void *context);
|
||||
void *on_event_context;
|
||||
|
||||
zend_ast *ast;
|
||||
zend_arena *ast_arena;
|
||||
|
|
|
@ -226,6 +226,7 @@ ZEND_API void zend_save_lexical_state(zend_lex_state *lex_state)
|
|||
lex_state->script_encoding = SCNG(script_encoding);
|
||||
|
||||
lex_state->on_event = SCNG(on_event);
|
||||
lex_state->on_event_context = SCNG(on_event_context);
|
||||
|
||||
lex_state->ast = CG(ast);
|
||||
lex_state->ast_arena = CG(ast_arena);
|
||||
|
@ -265,6 +266,7 @@ ZEND_API void zend_restore_lexical_state(zend_lex_state *lex_state)
|
|||
SCNG(script_encoding) = lex_state->script_encoding;
|
||||
|
||||
SCNG(on_event) = lex_state->on_event;
|
||||
SCNG(on_event_context) = lex_state->on_event_context;
|
||||
|
||||
CG(ast) = lex_state->ast;
|
||||
CG(ast_arena) = lex_state->ast_arena;
|
||||
|
@ -284,7 +286,9 @@ ZEND_API void zend_destroy_file_handle(zend_file_handle *file_handle)
|
|||
|
||||
ZEND_API void zend_lex_tstring(zval *zv)
|
||||
{
|
||||
if (SCNG(on_event)) SCNG(on_event)(ON_FEEDBACK, T_STRING, 0);
|
||||
if (SCNG(on_event)) {
|
||||
SCNG(on_event)(ON_FEEDBACK, T_STRING, 0, SCNG(on_event_context));
|
||||
}
|
||||
|
||||
ZVAL_STRINGL(zv, (char*)SCNG(yy_text), SCNG(yy_leng));
|
||||
}
|
||||
|
@ -1082,7 +1086,9 @@ static int zend_scan_escape_string(zval *zendlval, char *str, int len, char quot
|
|||
|
||||
static zend_always_inline int emit_token(int token, int token_line)
|
||||
{
|
||||
if(SCNG(on_event)) SCNG(on_event)(ON_TOKEN, token, token_line);
|
||||
if (SCNG(on_event)) {
|
||||
SCNG(on_event)(ON_TOKEN, token, token_line, SCNG(on_event_context));
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
|
|
|
@ -183,10 +183,9 @@ static zend_bool tokenize(zval *return_value, zend_string *source)
|
|||
return 1;
|
||||
}
|
||||
|
||||
zval token_stream;
|
||||
|
||||
void on_event(zend_php_scanner_event event, int token, int line)
|
||||
void on_event(zend_php_scanner_event event, int token, int line, void *context)
|
||||
{
|
||||
zval *token_stream = (zval *) context;
|
||||
zval keyword;
|
||||
HashTable *tokens_ht;
|
||||
zval *token_zv;
|
||||
|
@ -199,13 +198,13 @@ void on_event(zend_php_scanner_event event, int token, int line)
|
|||
add_next_index_long(&keyword, token);
|
||||
add_next_index_stringl(&keyword, (char *)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
|
||||
add_next_index_long(&keyword, line);
|
||||
add_next_index_zval(&token_stream, &keyword);
|
||||
add_next_index_zval(token_stream, &keyword);
|
||||
} else {
|
||||
add_next_index_stringl(&token_stream, (char *)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
|
||||
add_next_index_stringl(token_stream, (char *)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
|
||||
}
|
||||
break;
|
||||
case ON_FEEDBACK:
|
||||
tokens_ht = Z_ARRVAL(token_stream);
|
||||
tokens_ht = Z_ARRVAL_P(token_stream);
|
||||
token_zv = zend_hash_index_find(tokens_ht, zend_hash_num_elements(tokens_ht) - 1);
|
||||
if (token_zv && Z_TYPE_P(token_zv) == IS_ARRAY) {
|
||||
ZVAL_LONG(zend_hash_index_find(Z_ARRVAL_P(token_zv), 0), token);
|
||||
|
@ -218,7 +217,7 @@ void on_event(zend_php_scanner_event event, int token, int line)
|
|||
add_next_index_stringl(&keyword,
|
||||
(char *)LANG_SCNG(yy_cursor), LANG_SCNG(yy_limit) - LANG_SCNG(yy_cursor));
|
||||
add_next_index_long(&keyword, CG(zend_lineno));
|
||||
add_next_index_zval(&token_stream, &keyword);
|
||||
add_next_index_zval(token_stream, &keyword);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -238,12 +237,15 @@ static zend_bool tokenize_parse(zval *return_value, zend_string *source)
|
|||
zend_save_lexical_state(&original_lex_state);
|
||||
|
||||
if ((success = (zend_prepare_string_for_scanning(&source_zval, "") == SUCCESS))) {
|
||||
zval token_stream;
|
||||
array_init(&token_stream);
|
||||
|
||||
CG(ast) = NULL;
|
||||
CG(ast_arena) = zend_arena_create(1024 * 32);
|
||||
LANG_SCNG(yy_state) = yycINITIAL;
|
||||
LANG_SCNG(on_event) = on_event;
|
||||
LANG_SCNG(on_event_context) = &token_stream;
|
||||
|
||||
array_init(&token_stream);
|
||||
if((success = (zendparse() == SUCCESS))) {
|
||||
ZVAL_COPY_VALUE(return_value, &token_stream);
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue