Repository: valkey-io/valkey
File: src/scripting_engine.c (C)

Code before:
}

/*
 * Iterates the list of engines registered in the engine manager and calls the
 * callback function with each engine.
 *
 * The `context` pointer is also passed in each callback call.
 */
void scriptingEngineManagerForEachEngine(engineIterCallback callback,
                                         void *context) {
    dictIterator *iter = dictGetIterator(engineMgr.engines);
    dictEntry *entry = NULL;
    while ((entry = dictNext(iter))) {
        scriptingEngine *e = dictGetVal(entry);
        callback(e, context);
    }
    dictReleaseIterator(iter);
}

static ValkeyModuleCtx *engineSetupModuleCtx(int module_ctx_cache_index,
                                             scriptingEngine *e,
                                             client *c) {
    debugServerAssert(e != NULL);
    if (e->module == NULL) {
        return NULL;
    }

    ValkeyModuleCtx *ctx = e->module_ctx_cache[module_ctx_cache_index];
    moduleScriptingEngineInitContext(ctx, e->module, c);
    return ctx;
}

static void engineTeardownModuleCtx(int module_ctx_cache_index, scriptingEngine *e) {
    debugServerAssert(e != NULL);
    if (e->module != NULL) {
        ValkeyModuleCtx *ctx = e->module_ctx_cache[module_ctx_cache_index];
        moduleFreeContext(ctx);
    }
}

compiledFunction **scriptingEngineCallCompileCode(scriptingEngine *engine,
                                                  subsystemType type,
                                                  const char *code,
                                                  size_t code_len,
                                                  size_t timeout,
                                                  size_t *out_num_compiled_functions,
                                                  robj **err) {
    serverAssert(type == VMSE_EVAL || type == VMSE_FUNCTION);
    compiledFunction **functions = NULL;
    ValkeyModuleCtx *module_ctx = engineSetupModuleCtx(COMMON_MODULE_CTX_INDEX, engine, NULL);


Review comment (suggestion):
```suggestion
    if (e->module == NULL) return NULL;
```

Code after:
}

/*
 * Iterates the list of engines registered in the engine manager and calls the
 * callback function with each engine.
 *
 * The `context` pointer is also passed in each callback call.
 */
void scriptingEngineManagerForEachEngine(engineIterCallback callback,
                                         void *context) {
    dictIterator *iter = dictGetIterator(engineMgr.engines);
    dictEntry *entry = NULL;
    while ((entry = dictNext(iter))) {
        scriptingEngine *e = dictGetVal(entry);
        callback(e, context);
    }
    dictReleaseIterator(iter);
}

static ValkeyModuleCtx *engineSetupModuleCtx(int module_ctx_cache_index,
                                             scriptingEngine *e,
                                             client *c) {
    serverAssert(e != NULL);
    if (e->module == NULL) return NULL;

    ValkeyModuleCtx *ctx = e->module_ctx_cache[module_ctx_cache_index];
    moduleScriptingEngineInitContext(ctx, e->module, c);
    return ctx;
}

static void engineTeardownModuleCtx(int module_ctx_cache_index, scriptingEngine *e) {
    serverAssert(e != NULL);
    if (e->module != NULL) {
        ValkeyModuleCtx *ctx = e->module_ctx_cache[module_ctx_cache_index];
        moduleFreeContext(ctx);
    }
}

compiledFunction **scriptingEngineCallCompileCode(scriptingEngine *engine,
                                                  subsystemType type,
                                                  const char *code,
                                                  size_t code_len,
                                                  size_t timeout,
                                                  size_t *out_num_compiled_functions,
                                                  robj **err) {
    serverAssert(type == VMSE_EVAL || type == VMSE_FUNCTION);
    compiledFunction **functions = NULL;
    ValkeyModuleCtx *module_ctx = engineSetupModuleCtx(COMMON_MODULE_CTX_INDEX, engine, NULL);

    if (engine->impl.methods.version == 1) {
        functions = engine->impl.methods.compile_code_v1(