Skip to content

Commit b0d95ab

Browse files
committed
Add namespace_name field to zend_op_array for visibility checks
To support namespace-scoped visibility, we need to track which namespace each function/file belongs to at runtime. This commit adds a namespace_name field to zend_op_array that is set during compilation from CG(file_context).current_namespace. This allows determining the caller's namespace context even for top-level code. * Memory cost: 8 bytes per op_array on 64-bit systems
1 parent 5ab594c commit b0d95ab

File tree

4 files changed

+13
-0
lines changed

4 files changed

+13
-0
lines changed

Zend/zend_compile.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8563,6 +8563,10 @@ static zend_op_array *zend_compile_func_decl_ex(
85638563
op_array->fn_flags |= ZEND_ACC_PRELOADED;
85648564
}
85658565

8566+
if (CG(file_context).current_namespace) {
8567+
op_array->namespace_name = zend_string_copy(CG(file_context).current_namespace);
8568+
}
8569+
85668570
op_array->fn_flags |= (orig_op_array->fn_flags & ZEND_ACC_STRICT_TYPES);
85678571
op_array->fn_flags |= decl->flags;
85688572
op_array->line_start = decl->start_lineno;

Zend/zend_compile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ struct _zend_op_array {
560560
zend_string *filename;
561561
uint32_t line_start;
562562
uint32_t line_end;
563+
zend_string *namespace_name;
563564

564565
uint32_t last_literal;
565566
uint32_t num_dynamic_func_defs;

Zend/zend_language_scanner.l

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,10 @@ static zend_op_array *zend_compile(int type)
609609
init_op_array(op_array, type, INITIAL_OP_ARRAY_SIZE);
610610
CG(active_op_array) = op_array;
611611

612+
if (CG(file_context).current_namespace) {
613+
op_array->namespace_name = zend_string_copy(CG(file_context).current_namespace);
614+
}
615+
612616
/* Use heap to not waste arena memory */
613617
op_array->fn_flags |= ZEND_ACC_HEAP_RT_CACHE;
614618

Zend/zend_opcode.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ void init_op_array(zend_op_array *op_array, uint8_t type, int initial_ops_size)
6666
op_array->filename = zend_string_copy(zend_get_compiled_filename());
6767
op_array->doc_comment = NULL;
6868
op_array->attributes = NULL;
69+
op_array->namespace_name = NULL;
6970

7071
op_array->arg_info = NULL;
7172
op_array->num_args = 0;
@@ -611,6 +612,9 @@ ZEND_API void destroy_op_array(zend_op_array *op_array)
611612
if (op_array->doc_comment) {
612613
zend_string_release_ex(op_array->doc_comment, 0);
613614
}
615+
if (op_array->namespace_name) {
616+
zend_string_release_ex(op_array->namespace_name, 0);
617+
}
614618
if (op_array->attributes) {
615619
zend_hash_release(op_array->attributes);
616620
}

0 commit comments

Comments
 (0)