Skip to content

Commit 574faac

Browse files
committed
Addressed reviewer's comments and bumped scripting engine ABI version
Signed-off-by: Ricardo Dias <[email protected]>
1 parent ed081ce commit 574faac

File tree

7 files changed

+76
-7
lines changed

7 files changed

+76
-7
lines changed

src/commands.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5418,7 +5418,7 @@ struct COMMAND_ARG SCRIPT_DEBUG_mode_Subargs[] = {
54185418
/* SCRIPT DEBUG argument table */
54195419
struct COMMAND_ARG SCRIPT_DEBUG_Args[] = {
54205420
{MAKE_ARG("mode",ARG_TYPE_ONEOF,-1,NULL,NULL,NULL,CMD_ARG_NONE,3,NULL),.subargs=SCRIPT_DEBUG_mode_Subargs},
5421-
{MAKE_ARG("engine",ARG_TYPE_STRING,-1,NULL,NULL,"8.1.0",CMD_ARG_OPTIONAL,0,NULL)},
5421+
{MAKE_ARG("engine",ARG_TYPE_STRING,-1,NULL,NULL,"9.0.0",CMD_ARG_OPTIONAL,0,NULL)},
54225422
};
54235423

54245424
/********** SCRIPT EXISTS ********************/

src/commands/script-debug.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"name": "engine",
4040
"type": "string",
4141
"optional": true,
42-
"since": "8.1.0"
42+
"since": "9.0.0"
4343
}
4444
],
4545
"reply_schema": {

src/lua/engine_lua.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ int luaEngineInitEngine(void) {
426426
ldbInit();
427427

428428
engineMethods methods = {
429+
.version = VALKEYMODULE_SCRIPTING_ENGINE_ABI_VERSION,
429430
.compile_code = luaEngineCompileCode,
430431
.free_function = luaEngineFreeFunction,
431432
.call_function = luaEngineFunctionCall,

src/scripting_engine.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "dict.h"
99
#include "functions.h"
1010
#include "module.h"
11+
#include "server.h"
1112

1213
typedef struct scriptingEngineImpl {
1314
/* Engine specific context */
@@ -76,6 +77,14 @@ size_t scriptingEngineManagerGetMemoryUsage(void) {
7677
return dictMemUsage(engineMgr.engines) + sizeof(engineMgr);
7778
}
7879

80+
static inline void scriptingEngineInitializeEngineMethods(scriptingEngine *engine, engineMethods *methods) {
81+
if (methods->version < 3) {
82+
memcpy(&engine->impl.methods, methods, sizeof(engineMethodsV1));
83+
} else {
84+
engine->impl.methods = *methods;
85+
}
86+
}
87+
7988
/* Registers a new scripting engine in the engine manager.
8089
*
8190
* - `engine_name`: the name of the scripting engine. This name will match
@@ -111,11 +120,11 @@ int scriptingEngineManagerRegister(const char *engine_name,
111120
.module = engine_module,
112121
.impl = {
113122
.ctx = engine_ctx,
114-
.methods = *engine_methods,
115123
},
116124
.client = c,
117125
.module_ctx = engine_module ? moduleAllocateContext() : NULL,
118126
};
127+
scriptingEngineInitializeEngineMethods(e, engine_methods);
119128

120129
dictAdd(engineMgr.engines, engine_name_sds, e);
121130

@@ -329,6 +338,13 @@ debuggerEnableRet scriptingEngineCallDebuggerEnable(scriptingEngine *engine,
329338
subsystemType type,
330339
const debuggerCommand **commands,
331340
size_t *commands_len) {
341+
if (engine->impl.methods.version < 3) {
342+
serverLog(LL_WARNING, "Scripting engine '%s' uses ABI version '%lu', which does not support debugger API",
343+
scriptingEngineGetName(engine),
344+
(unsigned long)engine->impl.methods.version);
345+
return VMSE_DEBUG_NOT_SUPPORTED;
346+
}
347+
332348
if (engine->impl.methods.debugger_enable == NULL ||
333349
engine->impl.methods.debugger_disable == NULL ||
334350
engine->impl.methods.debugger_start == NULL ||
@@ -349,6 +365,9 @@ debuggerEnableRet scriptingEngineCallDebuggerEnable(scriptingEngine *engine,
349365

350366
void scriptingEngineCallDebuggerDisable(scriptingEngine *engine,
351367
subsystemType type) {
368+
serverAssert(engine->impl.methods.version >= 3);
369+
serverAssert(engine->impl.methods.debugger_disable != NULL);
370+
352371
engineSetupModuleCtx(engine, NULL);
353372
engine->impl.methods.debugger_disable(
354373
engine->module_ctx,
@@ -360,6 +379,9 @@ void scriptingEngineCallDebuggerDisable(scriptingEngine *engine,
360379
void scriptingEngineCallDebuggerStart(scriptingEngine *engine,
361380
subsystemType type,
362381
robj *source) {
382+
serverAssert(engine->impl.methods.version >= 3);
383+
serverAssert(engine->impl.methods.debugger_start != NULL);
384+
363385
engineSetupModuleCtx(engine, NULL);
364386
engine->impl.methods.debugger_start(
365387
engine->module_ctx,
@@ -371,6 +393,9 @@ void scriptingEngineCallDebuggerStart(scriptingEngine *engine,
371393

372394
void scriptingEngineCallDebuggerEnd(scriptingEngine *engine,
373395
subsystemType type) {
396+
serverAssert(engine->impl.methods.version >= 3);
397+
serverAssert(engine->impl.methods.debugger_end != NULL);
398+
374399
engineSetupModuleCtx(engine, NULL);
375400
engine->impl.methods.debugger_end(
376401
engine->module_ctx,
@@ -450,6 +475,11 @@ int scriptingEngineDebuggerEnable(client *c, scriptingEngine *engine, sds *err)
450475
* to properly shut down a client debugging session, see scriptingEngineDebuggerEndSession()
451476
* for more information. */
452477
void scriptingEngineDebuggerDisable(client *c) {
478+
if (ds.engine == NULL) {
479+
/* No debug session enabled. */
480+
return;
481+
}
482+
453483
ds.commands = NULL;
454484
ds.commands_len = 0;
455485
c->flag.lua_debug = 0;
@@ -569,6 +599,8 @@ int scriptingEngineDebuggerStartSession(client *c) {
569599
/* End a debugging session after the EVAL call with debugging enabled
570600
* returned. */
571601
void scriptingEngineDebuggerEndSession(client *c) {
602+
serverAssert(ds.active);
603+
572604
/* Emit the remaining logs and an <endsession> mark. */
573605
scriptingEngineDebuggerLog(sdsnew("<endsession>"));
574606
scriptingEngineDebuggerFlushLogs();

src/scripting_engine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _SCRIPTING_ENGINE_H_
33

44
#include "server.h"
5+
#include "valkeymodule.h"
56

67
// Forward declaration of the engine structure.
78
typedef struct scriptingEngine scriptingEngine;
@@ -17,6 +18,7 @@ typedef ValkeyModuleScriptingEngineCallableLazyEvalReset callableLazyEvalReset;
1718
typedef ValkeyModuleScriptingEngineDebuggerEnableRet debuggerEnableRet;
1819
typedef ValkeyModuleScriptingEngineDebuggerCommand debuggerCommand;
1920
typedef ValkeyModuleScriptingEngineDebuggerCommandParam debuggerCommandParam;
21+
typedef ValkeyModuleScriptingEngineMethodsV1 engineMethodsV1;
2022
typedef ValkeyModuleScriptingEngineMethods engineMethods;
2123

2224
/*

src/valkey-cli.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3524,7 +3524,7 @@ static int evalMode(int argc, char **argv) {
35243524

35253525
char *engine_name = NULL;
35263526
if (script[0] == '#' && script[1] == '!') {
3527-
const char *sp = strchr(script, '\n');
3527+
const char *sp = strpbrk(script, "\r\n ");
35283528
engine_name = strndup(script + 2, (sp - script) - 2);
35293529
} else {
35303530
engine_name = strdup("lua");

src/valkeymodule.h

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,12 @@ typedef void (*ValkeyModuleScriptingEngineDebuggerEndFunc)(
11191119
ValkeyModuleScriptingEngineSubsystemType type);
11201120

11211121
/* Current ABI version for scripting engine modules. */
1122-
#define VALKEYMODULE_SCRIPTING_ENGINE_ABI_VERSION 2UL
1122+
/* Version Changelog:
1123+
* - 1: Initial version.
1124+
* - 2: Changed the `compile_code` callback to support binary data in the source code.
1125+
* - 3: Added support for new debugging commands.
1126+
*/
1127+
#define VALKEYMODULE_SCRIPTING_ENGINE_ABI_VERSION 3UL
11231128

11241129
typedef struct ValkeyModuleScriptingEngineMethods {
11251130
uint64_t version; /* Version of this structure for ABI compat. */
@@ -1148,6 +1153,35 @@ typedef struct ValkeyModuleScriptingEngineMethods {
11481153
/* Function callback to get the used memory by the engine. */
11491154
ValkeyModuleScriptingEngineGetMemoryInfoFunc get_memory_info;
11501155

1156+
} ValkeyModuleScriptingEngineMethodsV1;
1157+
1158+
typedef struct ValkeyModuleScriptingEngineMethodsV2 {
1159+
uint64_t version; /* Version of this structure for ABI compat. */
1160+
1161+
/* Compile code function callback. When a new script is loaded, this
1162+
* callback will be called with the script code, compiles it, and returns a
1163+
* list of `ValkeyModuleScriptingEngineCompiledFunc` objects. */
1164+
union {
1165+
ValkeyModuleScriptingEngineCompileCodeFuncV1 compile_code_v1;
1166+
ValkeyModuleScriptingEngineCompileCodeFunc compile_code;
1167+
};
1168+
/* Function callback to free the memory of a registered engine function. */
1169+
ValkeyModuleScriptingEngineFreeFunctionFunc free_function;
1170+
1171+
/* The callback function called when `FCALL` command is called on a function
1172+
* registered in this engine. */
1173+
ValkeyModuleScriptingEngineCallFunctionFunc call_function;
1174+
1175+
/* Function callback to return memory overhead for a given function. */
1176+
ValkeyModuleScriptingEngineGetFunctionMemoryOverheadFunc get_function_memory_overhead;
1177+
1178+
/* The callback function used to reset the runtime environment used
1179+
* by the scripting engine for EVAL scripts. */
1180+
ValkeyModuleScriptingEngineResetEvalEnvFunc reset_eval_env;
1181+
1182+
/* Function callback to get the used memory by the engine. */
1183+
ValkeyModuleScriptingEngineGetMemoryInfoFunc get_memory_info;
1184+
11511185
/* Function callback to enable the debugger for the future execution of scripts. */
11521186
ValkeyModuleScriptingEngineDebuggerEnableFunc debugger_enable;
11531187

@@ -1161,9 +1195,9 @@ typedef struct ValkeyModuleScriptingEngineMethods {
11611195
ValkeyModuleScriptingEngineDebuggerEndFunc debugger_end;
11621196

11631197

1164-
} ValkeyModuleScriptingEngineMethodsV1;
1198+
} ValkeyModuleScriptingEngineMethodsV2;
11651199

1166-
#define ValkeyModuleScriptingEngineMethods ValkeyModuleScriptingEngineMethodsV1
1200+
#define ValkeyModuleScriptingEngineMethods ValkeyModuleScriptingEngineMethodsV2
11671201

11681202
/* ------------------------- End of common defines ------------------------ */
11691203

0 commit comments

Comments
 (0)