@@ -73,20 +73,54 @@ static Value native_pop(int argCount, Value* args) {
7373}
7474
7575static Value native_substr (int argCount , Value * args ) {
76+ // DEBUG: Track substr calls
77+ #ifdef DEBUG_SUBSTR
78+ printf ("[DEBUG_SUBSTR] Called with %d args\n" , argCount );
79+ if (argCount >= 1 ) {
80+ printf ("[DEBUG_SUBSTR] arg[0]: IS_OBJ=%d IS_STRING=%d\n" ,
81+ IS_OBJ (args [0 ]), IS_STRING (args [0 ]));
82+ if (IS_OBJ (args [0 ])) {
83+ Obj * obj = AS_OBJ (args [0 ]);
84+ printf ("[DEBUG_SUBSTR] arg[0] obj type=%d ptr=%p\n" , obj -> type , (void * )obj );
85+ if (IS_STRING (args [0 ])) {
86+ ObjString * str = AS_STRING (args [0 ]);
87+ printf ("[DEBUG_SUBSTR] arg[0] string: len=%d ptr=%p hash=%u\n" ,
88+ str -> length , (void * )str , str -> hash );
89+ }
90+ }
91+ }
92+ #endif
93+
7694 if (argCount < 3 ) return NIL_VAL ;
77- if (!IS_STRING (args [0 ]) || !IS_NUMBER (args [1 ]) || !IS_NUMBER (args [2 ])) return NIL_VAL ;
95+ if (!IS_STRING (args [0 ]) || !IS_NUMBER (args [1 ]) || !IS_NUMBER (args [2 ])) {
96+ #ifdef DEBUG_SUBSTR
97+ printf ("[DEBUG_SUBSTR] Type check failed: IS_STRING=%d IS_NUM[1]=%d IS_NUM[2]=%d\n" ,
98+ IS_STRING (args [0 ]), IS_NUMBER (args [1 ]), IS_NUMBER (args [2 ]));
99+ #endif
100+ return NIL_VAL ;
101+ }
78102
79103 ObjString * source = AS_STRING (args [0 ]);
80104 int start = (int )AS_NUMBER (args [1 ]);
81105 int length = (int )AS_NUMBER (args [2 ]);
82106
107+ #ifdef DEBUG_SUBSTR
108+ printf ("[DEBUG_SUBSTR] source string: len=%d start=%d length=%d\n" ,
109+ source -> length , start , length );
110+ #endif
111+
83112 if (start < 0 || start >= source -> length ) {
84113 return OBJ_VAL (copyString ("" , 0 ));
85114 }
86115 if (length < 0 ) length = 0 ;
87116 if (start + length > source -> length ) length = source -> length - start ;
88117
89- return OBJ_VAL (copyString (source -> chars + start , length ));
118+ ObjString * result = copyString (source -> chars + start , length );
119+ #ifdef DEBUG_SUBSTR
120+ printf ("[DEBUG_SUBSTR] result string: len=%d ptr=%p\n" , result -> length , (void * )result );
121+ #endif
122+
123+ return OBJ_VAL (result );
90124}
91125
92126/*
0 commit comments