question about registering functions as callbacks for custom class?
Maintainers usually reply within 5 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 25/100
- Issue type
- Bug
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- c, cpp, javascript
- Domain
- tooling
Research direction
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.
Written by the indexing model from the issue text.
Description
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;
}
- Dominant language
- C
- Stars
- 11.1k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from bellard/quickjs
-
Difficulty 1/5 Under an hour Newbie friendliness 75/100
Maintainers usually reply within 5 days
-
RegExp: patterns with more than 255 capture groups fail with "too many captures"Possibly taken @andreasrosdal claimed this 3 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 25/100
bellard/quickjs#624 · 1 comment ·
Maintainers usually reply within 5 days
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
bellard/quickjs#620 · 1 comment ·
Maintainers usually reply within 5 days
-
(comment on #611) `function g(a, d = (a = 10)) { return a; }` returns the argument instead of 10Open
Difficulty 4/5 3-5 days Newbie friendliness 30/100
Maintainers usually reply within 5 days
-
Difficulty 4/5 3-5 days Newbie friendliness 30/100
Maintainers usually reply within 5 days
Similar issues
-
documentation good first issue help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
JulianKemmerer/PipelineC#409 ·
-
Linux build fails without libspeechd-dev, which INSTALL.md doesn't list and CMake reports as foundOpen
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
-
needs-playtest slice
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Life-Framework/Life-Framework#4861 ·
Maintainers usually reply within 1 day