2727 * RETURN # returns the current value on the top of the stack and marks
2828 * # the end of the function declaration
2929 *
30- * FUNCTION bar # declaration of function 'bar'
30+ * RFUNCTION bar # declaration of read-only function 'bar'
3131 * CONSTI 432 # pushes the value 432 to the top of the stack
3232 * RETURN # returns the current value on the top of the stack and marks
3333 * # the end of the function declaration.
5555 */
5656typedef enum HelloInstKind {
5757 FUNCTION = 0 ,
58+ RFUNCTION ,
5859 CONSTI ,
5960 CONSTS ,
6061 ARGS ,
@@ -69,6 +70,7 @@ typedef enum HelloInstKind {
6970 */
7071const char * HelloInstKindStr [] = {
7172 "FUNCTION" ,
73+ "RFUNCTION" ,
7274 "CONSTI" ,
7375 "CONSTS" ,
7476 "ARGS" ,
@@ -119,6 +121,7 @@ typedef struct HelloFunc {
119121 HelloInst instructions [256 ];
120122 uint32_t num_instructions ;
121123 uint32_t index ;
124+ int read_only ;
122125} HelloFunc ;
123126
124127/*
@@ -207,11 +210,12 @@ static HelloInstKind helloLangParseInstruction(const char *token) {
207210/*
208211 * Parses the function param.
209212 */
210- static void helloLangParseFunction (HelloFunc * func ) {
213+ static void helloLangParseFunction (HelloFunc * func , int read_only ) {
211214 char * token = strtok (NULL , " \n" );
212215 ValkeyModule_Assert (token != NULL );
213216 func -> name = ValkeyModule_Alloc (sizeof (char ) * strlen (token ) + 1 );
214217 strcpy (func -> name , token );
218+ func -> read_only = read_only ;
215219}
216220
217221/*
@@ -283,12 +287,13 @@ static int helloLangParseCode(const char *code,
283287
284288 switch (kind ) {
285289 case FUNCTION :
290+ case RFUNCTION :
286291 ValkeyModule_Assert (currentFunc == NULL );
287292 currentFunc = ValkeyModule_Alloc (sizeof (HelloFunc ));
288293 memset (currentFunc , 0 , sizeof (HelloFunc ));
289294 currentFunc -> index = program -> num_functions ;
290295 program -> functions [program -> num_functions ++ ] = currentFunc ;
291- helloLangParseFunction (currentFunc );
296+ helloLangParseFunction (currentFunc , kind == RFUNCTION );
292297 break ;
293298 case CONSTI :
294299 ValkeyModule_Assert (currentFunc != NULL );
@@ -424,6 +429,7 @@ static void helloDebuggerLogCurrentInstr(uint32_t pc, HelloInst *instr) {
424429 msg = ValkeyModule_CreateStringPrintf (NULL , ">>> %3u: %s" , pc , HelloInstKindStr [instr -> kind ]);
425430 break ;
426431 case FUNCTION :
432+ case RFUNCTION :
427433 case _NUM_INSTRUCTIONS :
428434 ValkeyModule_Assert (0 );
429435 }
@@ -528,6 +534,7 @@ static HelloExecutionState executeHelloLangFunction(ValkeyModuleCtx *module_ctx,
528534 return FINISHED ;
529535 }
530536 case FUNCTION :
537+ case RFUNCTION :
531538 case _NUM_INSTRUCTIONS :
532539 ValkeyModule_Assert (0 );
533540 }
@@ -646,7 +653,7 @@ static ValkeyModuleScriptingEngineCompiledFunction **createHelloLangEngine(Valke
646653 .name = ValkeyModule_CreateString (NULL , func -> name , strlen (func -> name )),
647654 .function = func ,
648655 .desc = NULL ,
649- .f_flags = 0 ,
656+ .f_flags = func -> read_only ? VMSE_SCRIPT_FLAG_NO_WRITES : 0 ,
650657 };
651658
652659 compiled_functions [i ] = cfunc ;
0 commit comments