@@ -45,6 +45,28 @@ static void registerModule(VM* vm, const char* name, ObjModule* module) {
4545 pop (vm );
4646}
4747
48+ static Value native_push (int argCount , Value * args ) {
49+ if (argCount < 2 ) return NIL_VAL ;
50+ if (!IS_LIST (args [0 ])) return NIL_VAL ;
51+ ObjList * list = AS_LIST (args [0 ]);
52+ Value item = args [1 ];
53+ if (list -> capacity < list -> count + 1 ) {
54+ int oldCapacity = list -> capacity ;
55+ list -> capacity = GROW_CAPACITY (oldCapacity );
56+ list -> items = GROW_ARRAY (Value , list -> items , oldCapacity , list -> capacity );
57+ }
58+ list -> items [list -> count ++ ] = item ;
59+ return item ; // Return pushed item or nil? JS returns new length
60+ }
61+
62+ static Value native_pop (int argCount , Value * args ) {
63+ if (argCount < 1 ) return NIL_VAL ;
64+ if (!IS_LIST (args [0 ])) return NIL_VAL ;
65+ ObjList * list = AS_LIST (args [0 ]);
66+ if (list -> count == 0 ) return NIL_VAL ;
67+ return list -> items [-- list -> count ];
68+ }
69+
4870/*
4971 * Register all standard library modules
5072 * Called during VM initialization
@@ -191,4 +213,6 @@ void registerStdLib(VM* vm) {
191213 // Register simple globals for convenience
192214 defineNative (vm , "clock" , native_clock );
193215 defineNative (vm , "len" , native_len );
216+ defineNative (vm , "push" , native_push );
217+ defineNative (vm , "pop" , native_pop );
194218}
0 commit comments