Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

question about registering functions as callbacks for custom class?

Abierto
#181 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 3 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
25/100
Tipo de issue
Error
Claridad
Necesita aclaración
Estado de actividad
Estancado
Stack tecnológico
c, cpp, javascript
Área
tooling

Línea de trabajo

Start from the myClass_addEventListener example in the issue and trace the JS_Call path, including how the stored callback is retained and invoked from main.cpp. Reproduce the callback and post-call failure, then document a working event-trigger path with correct callback lifetime handling.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

i'm currently trying to embed quickjs into my own project where i have my own custom classes, i'm a little confused on how to set up a callback and cant for the life of me find any answers online. my current js script looks like this:

import { MyClass } from "MyClass"

let c = new MyClass();
c.addEventListener("callback", arbitraryFunctionName);

function otherFunction()
{
    print("update()");
}

function arbitraryFunctionName()
{
    print("function successfully called from main.cpp");
    otherFunction(); // when testing this didn't work because of scope?
}

i'm registering my callback functions into a std::map like so:

static JSValue myClass_addEventListener(
    JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
    std::cout << "CXX: myClass_addEventListener" << std::endl;

    auto* object = static_cast<MyClassData *>(JS_GetOpaque2(ctx, this_val, MyClassID));

    // Check if correct arguments are provided
    if (!object || argc != 2 || !JS_IsString(argv[0]) || !JS_IsFunction(ctx, argv[1])) 
        return JS_ThrowTypeError(ctx, "Expected a string and a function as arguments");

    // Convert the event type argument to a string
    const char* eventType = JS_ToCString(ctx, argv[0]);

    // Store the callback function in the object
    JSValue function = JS_DupValue(ctx, argv[1]);
    object->eventListeners[eventType] = function;

    // This does call the function but seg faults straight after, and not practical
    // would be good to call some sort of triggerEvent() function from main.cpp?
    // JSValue result = JS_Call(ctx, function, this_val, 0, nullptr);
    // JS_FreeValue(ctx, function);
    // JS_FreeValue(ctx, result);

    JS_FreeCString(ctx, eventType);

    return JS_UNDEFINED;
}
Lenguaje dominante
C
Estrellas
11.1k
Forks
1.2k
Merge medio
11 h 20 min
PR fusionados (30 d)
1

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de bellard/quickjs

Todos los issues de bellard/quickjs

Issues similares

Más issues de C

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.