chakra-core / ChakraCore

ChakraCore is an open source Javascript engine with a C API.
MIT License
9.1k stars 1.19k forks source link

No way to pass user data to thread service callback #6957

Open zopsicle opened 10 months ago

zopsicle commented 10 months ago

Customarily, callbacks take an additional user data parameter so that closures can be used as callbacks. This is the case with, for example, JsMemoryAllocationCallback. However, JsThreadServiceCallback takes no such parameter (do not confuse this with its callbackState parameter, which is not user data rather engine-provided data for the work item callback).

Ideally the API would be:

typedef bool (CALLBACK *JsThreadServiceCallback)(
    _In_ JsBackgroundWorkItemCallback workItemCallback,
    _In_opt_ void *workItemCallbackState,
    _In_opt_ void *threadServiceCallbackState
);

STDAPI_(JsErrorCode) JsCreateRuntime(
    _In_ JsRuntimeAttributes attributes,
    _In_opt_ JsThreadServiceCallback threadService,
    _In_opt_ void *threadServiceCallbackState
    _Out_ JsRuntimeHandle *runtime
);

where the runtime would pass threadServiceCallbackState to threadService whenever it’s called.

rhuanjl commented 5 months ago

I wouldn't want to break the existing JsCreateRuntime, so to implement this would involve either: a) Making the existing function somehow able to accept either just a callback OR a struct that points to both the "state" and the callback - with some way of checking which it was receiving (maybe via an attribute?) OR b) Making an additional API for if people want a state