[ English | 简体中文 ]
Feature Export API
Core runtime interfaces provided by the Feature framework to Feature developers, covering memory and array management, reference access, callbacks, Promise, events, asynchronous Workers, JSON objects, and registration.
Header file: #include <feature_exports.h>
openvela Implementation Notes
- Memory ownership: Memory allocated by
FeatureMalloc/FeatureInstanceAlloc*is managed by the framework. UseFeatureDupValueto increase the reference andFeatureFreeValueto decrease it; memory is released when the reference count reaches 0 - Array type:
FtArrayis managed by the framework. Create viaFeatureCreateArray/FeatureArrayCopy, with support for in-place operations such asFeatureArrayAppend/FeatureArrayInsertAfter - Callbacks and Promises: Feature interfaces support both Callback and Promise asynchronous models; use
FeatureGetPromiseTypeto query the type used by the current invocation - Worker mechanism: Workers created by
FeatureCreateWorkerrun in a separate thread or uv work queue, and notify the main thread of results upon completion - Thread safety: Use
FeaturePost/FeaturePostExtto throw tasks back to the event loop bound to the Feature manager, avoiding cross-thread operations onFeatureInstanceHandle - Reference management: Use
FeatureDupInstanceHandleto extend the lifetime of aFeatureInstanceHandle, preventing access to a destroyed instance in asynchronous callbacks - Instance detach: In asynchronous callbacks, you must use
FeatureInstanceIsDetachedto check whether the instance has been detached; once detached, the handle must not be used
Memory Allocation
FeatureInstanceAlloc
void* FeatureInstanceAlloc(FeatureInstanceHandle handle, size_t size);
Allocates memory for a Feature instance. The allocated memory is managed by the framework and can be released via FeatureFreeValue.
Parameters:
handleFeature instance handle.sizeNumber of bytes to allocate.
Returns:
Returns the starting pointer on success, or NULL on failure.
FeatureInstanceAllocType
void* FeatureInstanceAllocType(FeatureInstanceHandle hInst, size_t size, FeatureType type);
Allocates memory for a Feature instance by type, with the allocated memory carrying a type tag.
Parameters:
hInstFeature instance handle.sizeNumber of bytes to allocate.typeType identifier, seeFeatureType.
Returns:
Returns the starting pointer on success, or NULL on failure.
FeatureInstanceAllocProtobuf
void* FeatureInstanceAllocProtobuf(FeatureInstanceHandle handle,
const ProtobufCMessageDescriptor* desc);
Allocates memory for a Feature instance based on a Protobuf message descriptor, and automatically initializes the fields.
Parameters:
handleFeature instance handle.descPointer to the Protobuf message descriptor.
Returns:
Returns a pointer to the initialized message object on success, or NULL on failure.
FeatureInstanceDupValue
void* FeatureInstanceDupValue(void* ptr);
Increments the reference count of a pointer allocated via FeatureInstanceAlloc*.
Parameters:
ptrPointer whose reference is to be incremented; must be a return value ofFeatureInstanceAlloc*.
Returns:
Returns the original pointer (for chained use).
FeatureInstanceFreeValue
void FeatureInstanceFreeValue(void* ptr);
Decrements the reference count of a pointer allocated via FeatureInstanceAlloc*. The memory is actually freed when the count reaches 0.
Parameters:
ptrPointer to release.
FeatureMalloc
void* FeatureMalloc(size_t size, FeatureType featureType);
Allocates a block of Feature-managed memory by type.
Parameters:
sizeNumber of bytes to allocate.featureTypeType identifier.
Returns:
Returns a pointer on success, or NULL on failure.
FeatureDupValue
void* FeatureDupValue(void* ptr);
Increments the reference count of a Feature value. Only applies to pointers allocated by FeatureMalloc.
Parameters:
ptrPointer whose reference is to be incremented.
Returns:
Returns the original pointer.
FeatureFreeValue
void FeatureFreeValue(void* ptr);
Decrements the reference count of a Feature value. The memory is actually freed when the count reaches 0. Only applies to pointers allocated by FeatureMalloc.
Parameters:
ptrPointer to release.
FeatureGetValueRefCount
int32_t FeatureGetValueRefCount(void* ptr);
Gets the current reference count of a Feature value. Only valid for pointers allocated by FeatureMalloc.
Parameters:
ptrTarget pointer to query.
Returns:
Returns the reference count.
FeatureStrCopy
char* FeatureStrCopy(FeatureInstanceHandle handle, const char* str);
Creates a Feature-framework-managed copy from a raw C string.
Parameters:
handleFeature instance handle.strSource string.
Returns:
Returns a pointer to the new string on success, or NULL on failure. The return value can be referenced via FeatureStrAddRef or released via FeatureInstanceFreeValue.
FeatureStrAddRef
#define FeatureStrAddRef(ptr) FeatureInstanceDupValue(ptr)
Increments the reference count of a Feature string. Equivalent to FeatureInstanceDupValue.
Parameters:
ptrFeature string pointer.
Returns:
Returns the original pointer (for chained use).
Array Management
FeatureCreateArray
FtArray* FeatureCreateArray(FeatureInstanceHandle handle, size_t capacity,
FeatureType element_type);
Creates an empty FtArray with the given reserved capacity.
Parameters:
handleFeature instance handle.capacityInitial capacity.element_typeElement type, seeFeatureType.
Returns:
Returns a pointer to the new array on success, or NULL on failure.
FeatureArrayCopy
FtArray* FeatureArrayCopy(FeatureInstanceHandle handle, FeatureType element_type,
const void* data, size_t count);
Creates an FtArray by copying from existing data. A type-dependent deep copy is performed for each element.
Parameters:
handleFeature instance handle.element_typeElement type.dataStarting pointer of the source data.countNumber of elements.
Returns:
Returns a pointer to the new array on success, or NULL on failure.
FeatureArrayCopyRaw
FtArray* FeatureArrayCopyRaw(FeatureInstanceHandle handle, FeatureType element_type,
const void* data, size_t count);
Creates an FtArray via raw byte copy from existing data, without deep-copying elements.
Parameters:
handleFeature instance handle.element_typeElement type.dataStarting pointer of the source data.countNumber of elements.
Returns:
Returns a pointer to the new array on success, or NULL on failure.
FeatureArrayResize
FtArray* FeatureArrayResize(FtArray* arr, size_t new_size);
Resizes an array. If new_size is greater than the current capacity, the array grows; if smaller, it is truncated.
Parameters:
arrArray to resize.new_sizeNew number of elements.
Returns:
Returns the resized array pointer on success (may point to a new address after realloc), or NULL on failure.
FeatureArrayGetLength
size_t FeatureArrayGetLength(FtArray* arr);
Gets the current number of elements in the array.
Parameters:
arrTarget array.
Returns:
Returns the current number of elements.
FeatureArrayGetData
void* FeatureArrayGetData(FtArray* arr, int start);
Gets a pointer to elements starting from the specified index.
Parameters:
arrTarget array.startStarting index.
Returns:
Returns a pointer to the starting element. Returns NULL if the index is out of bounds.
FeatureArrayAppend
FtArray* FeatureArrayAppend(FtArray* arr, const void* data);
Appends an element to the end of the array (deep copy).
Parameters:
arrTarget array.dataPointer to the element data to append.
Returns:
Returns the array pointer on success, or NULL on failure.
FeatureArrayAppendRaw
FtArray* FeatureArrayAppendRaw(FtArray* arr, const void* data);
Appends an element to the end of the array (raw byte copy, no deep copy).
Parameters:
arrTarget array.dataPointer to the element data to append.
Returns:
Returns the array pointer on success, or NULL on failure.
FeatureArrayClear
int FeatureArrayClear(FtArray* arr);
Clears all elements in the array while preserving allocated capacity.
Parameters:
arrTarget array.
Returns:
Returns 0 on success, or a negative number on failure.
FeatureArrayRemove
int FeatureArrayRemove(FtArray* arr, int start, size_t count);
Removes count elements starting at the specified position.
Parameters:
arrTarget array.startStarting index.countNumber of elements to remove.
Returns:
Returns 0 on success, or a negative number on failure.
FeatureArrayInsertAfter
int FeatureArrayInsertAfter(FtArray* arr, int start, const void* data, size_t count);
Inserts count elements after the specified position (deep copy).
Parameters:
arrTarget array.startReference position index.dataPointer to the element data.countNumber of elements to insert.
Returns:
Returns 0 on success, or a negative number on failure.
FeatureArrayInsertRawAfter
int FeatureArrayInsertRawAfter(FtArray* arr, int start, const void* data, size_t count);
Same as FeatureArrayInsertAfter, but performs raw byte copy.
Parameters:
arrTarget array.startReference position index.dataPointer to the element data.countNumber of elements to insert.
Returns:
Returns 0 on success, or a negative number on failure.
FeatureArrayInsertBefore
int FeatureArrayInsertBefore(FtArray* arr, int start, const void* data, size_t count);
Inserts count elements before the specified position (deep copy).
Parameters:
arrTarget array.startReference position index.dataPointer to the element data.countNumber of elements to insert.
Returns:
Returns 0 on success, or a negative number on failure.
FeatureArrayInsertRawBefore
int FeatureArrayInsertRawBefore(FtArray* arr, int start, const void* data, size_t count);
Same as FeatureArrayInsertBefore, but performs raw byte copy.
Parameters:
arrTarget array.startReference position index.dataPointer to the element data.countNumber of elements to insert.
Returns:
Returns 0 on success, or a negative number on failure.
Feature Handles and Context Access
FeatureGetProtoHandle
FeatureProtoHandle FeatureGetProtoHandle(FeatureInstanceHandle handle);
Gets the corresponding prototype handle from a Feature instance handle.
Parameters:
handleFeature instance handle.
Returns:
Returns the corresponding FeatureProtoHandle, or NULL on failure.
FeatureGetProtoData
void* FeatureGetProtoData(FeatureProtoHandle handle);
Gets the user data bound to the Feature prototype. This data is visible to all instances of that prototype.
Parameters:
handleFeature prototype handle.
Returns:
Returns a pointer to the user data, or NULL if not bound.
FeatureSetProtoData
void FeatureSetProtoData(FeatureProtoHandle handle, void* data);
Attaches user data to a Feature prototype.
Parameters:
handleFeature prototype handle.dataPointer to the user data.
FeatureGetObjectData
void* FeatureGetObjectData(FeatureInstanceHandle handle);
Gets the user data bound to a Feature instance.
Parameters:
handleFeature instance handle.
Returns:
Returns a pointer to the user data, or NULL if not bound.
FeatureSetObjectData
void FeatureSetObjectData(FeatureInstanceHandle handle, void* data);
Attaches user data to a Feature instance.
Parameters:
handleFeature instance handle.dataPointer to the user data.
FeatureGetContext
ft_context_ref FeatureGetContext(FeatureInstanceHandle handle);
Gets the runtime context (frontend-related) of a Feature instance.
Parameters:
handleFeature instance handle.
Returns:
Returns an ft_context_ref, or NULL on failure.
FeatureGetPackageName
const char* FeatureGetPackageName(FeatureProtoHandle handle);
Gets the QuickApp package name associated with the Feature prototype.
Parameters:
handleFeature prototype handle.
Returns:
Returns the package name string on success, or NULL on failure. The return value is managed by the framework; do not release it manually.
FeatureGetPackageVersion
const char* FeatureGetPackageVersion(FeatureProtoHandle handle);
Gets the QuickApp version string associated with the Feature prototype.
Parameters:
handleFeature prototype handle.
Returns:
Returns the version string on success, or NULL on failure.
FeatureGetEnvironmentName
const char* FeatureGetEnvironmentName(FeatureProtoHandle handle);
Gets the name of the runtime environment in which the Feature prototype resides.
Parameters:
handleFeature prototype handle.
Returns:
Returns the environment name string on success, or NULL on failure.
FeatureInstanceGetManagerUserData
void* FeatureInstanceGetManagerUserData(FeatureInstanceHandle handle, const char* name);
Gets user data from the manager that owns the Feature instance, by name.
Parameters:
handleFeature instance handle.nameUser data name.
Returns:
Returns a pointer to the user data on success, or NULL if not found.
FeatureGetManagerHandleFromInstance
FeatureManagerHandle FeatureGetManagerHandleFromInstance(FeatureInstanceHandle handle);
Gets the handle of the Feature manager that owns the given Feature instance.
Parameters:
handleFeature instance handle.
Returns:
Returns a FeatureManagerHandle on success, or NULL on failure.
FeatureGetManagerHandleFromProto
FeatureManagerHandle FeatureGetManagerHandleFromProto(FeatureProtoHandle handle);
Gets the handle of the Feature manager that owns the given Feature prototype.
Parameters:
handleFeature prototype handle.
Returns:
Returns a FeatureManagerHandle on success, or NULL on failure.
FeatureGetUVLoop
uv_loop_t* FeatureGetUVLoop(FeatureManagerHandle handle);
Gets the libuv event loop bound to the Feature manager.
Parameters:
handleFeature manager handle.
Returns:
Returns a uv_loop_t* on success, or NULL if not bound.
FeatureDupInstanceHandle
FeatureInstanceHandle FeatureDupInstanceHandle(FeatureInstanceHandle handle);
Increments the reference count of a Feature instance handle and returns the same handle (for chained use).
Parameters:
handleFeature instance handle.
Returns:
Returns the original handle.
FeatureFreeInstanceHandle
void FeatureFreeInstanceHandle(FeatureInstanceHandle handle);
Decrements the reference count of a Feature instance handle; releases it when the count reaches 0.
Parameters:
handleFeature instance handle.
FeatureInstanceIsDetached
bool FeatureInstanceIsDetached(FeatureInstanceHandle handle);
Determines whether a Feature instance has been detached from its prototype (typically during instance destruction). Asynchronous callbacks should call this before using the handle.
Parameters:
handleFeature instance handle.
Returns:
Returns true if detached; false otherwise. Once detached, no further interfaces may be called on this handle.
FeatureCreateInterface
FeatureInterfaceHandle FeatureCreateInterface(FeatureInstanceHandle handle, VTable* vtable);
Creates a Feature interface handle based on a vtable. Used to expose methods to the frontend as an object interface.
Parameters:
handleFeature instance handle.vtableVirtual function table, seeVTable.
Returns:
Returns the new FeatureInterfaceHandle on success, or NULL on failure.
Manager User Data Operations
FeatureSetManagerUserData
void FeatureSetManagerUserData(FeatureManagerHandle handle, const char* name, void* data);
Attaches user data to a Feature manager by name. A single manager may attach multiple data items under different names.
Parameters:
handleFeature manager handle.nameUser data name.dataPointer to the user data.
FeatureSetManagerUserDataWithFreeCallback
void* FeatureSetManagerUserDataWithFreeCallback(FeatureManagerHandle handle,
const char* name, void* data, ManagerUserdataFreeCallback free_cb);
Attaches user data by name and registers a free callback. The free_cb is invoked to clean up the user data when the manager is destroyed.
Parameters:
handleFeature manager handle.nameUser data name.dataPointer to the user data.free_cbFree callback with signaturevoid (*)(void* data).
Returns:
Returns the previous value if data of the same name was already bound; otherwise returns NULL.
FeatureManagerHasUserData
bool FeatureManagerHasUserData(FeatureManagerHandle handle, const char* name);
Determines whether the manager has user data bound under the specified name.
Parameters:
handleFeature manager handle.nameUser data name.
Returns:
Returns true if bound, false otherwise.
FeatureGetManagerUserData
void* FeatureGetManagerUserData(FeatureManagerHandle handle, const char* name);
Gets the user data bound to the manager by name.
Parameters:
handleFeature manager handle.nameUser data name.
Returns:
Returns a pointer to the user data on success, or NULL if not found.
Callback Management
FeatureInvokeCallback
bool FeatureInvokeCallback(FeatureInstanceHandle handle, FtCallbackId cid, ...);
Triggers a JS-layer callback by callback ID. Variadic arguments are passed in the order declared by the Feature interface.
Parameters:
handleFeature instance handle.cidCallback ID....Variadic arguments passed to the callback.
Returns:
Returns true on success, or false on failure (e.g., the callback ID is invalid or the instance has been detached).
FeatureInvokeCallbackCount
bool FeatureInvokeCallbackCount(FeatureInstanceHandle handle, FtCallbackId cid,
int count, ...);
Same as FeatureInvokeCallback, but explicitly specifies the argument count. Suitable for scenarios with a dynamic number of arguments.
Parameters:
handleFeature instance handle.cidCallback ID.countArgument count....Variadic argument list.
Returns:
Returns true on success, or false on failure.
FeatureRemoveCallback
bool FeatureRemoveCallback(FeatureInstanceHandle handle, FtCallbackId cid);
Removes the callback with the specified ID from the Feature instance. After removal, further calls to FeatureInvokeCallback will fail.
Parameters:
handleFeature instance handle.cidCallback ID.
Returns:
Returns true on success, or false if the callback ID does not exist.
FeatureCheckCallbackId
bool FeatureCheckCallbackId(FeatureInstanceHandle handle, FtCallbackId cid);
Checks whether the JS function corresponding to the callback ID is still valid.
Parameters:
handleFeature instance handle.cidCallback ID.
Returns:
Returns true if the function still exists and is valid; false otherwise.
Asynchronous Task Dispatch
FeaturePost
bool FeaturePost(FeatureInstanceHandle handle, FeatureTaskCallback task_cb, void* data);
Dispatches a task to the event loop bound to the Feature manager. Commonly used to cross back to the main thread for Feature instance operations.
Parameters:
handleFeature instance handle.task_cbTask callback with signaturevoid (*)(int status, void* data).dataUser data passed to the callback.
Returns:
Returns true if the dispatch succeeded, false otherwise.
Notes:
- Use
FeatureInstanceIsDetachedinsidetask_cbto check whether the handle has been detached. The handle must not be used once detached.
FeaturePostExt
bool FeaturePostExt(FeatureInstanceHandle handle, FeatureTaskCallbackExt task_cb_ext,
uint64_t data);
Extended version of FeaturePost. The callback signature includes the instance handle, making it easier to access the instance directly inside the callback.
Parameters:
handleFeature instance handle.task_cb_extExtended task callback with signaturevoid (*)(int status, uint64_t data, FeatureInstanceHandle feature).data64-bit user data passed to the callback.
Returns:
Returns true on success, or false on failure.
Notes:
- Also requires
FeatureInstanceIsDetachedinside the callback to check the instance state.
Promise Management
The Feature framework supports both Callback and Promise asynchronous models. Internally, both use the same FtPromiseId reference. The following interfaces are used to resolve or reject asynchronous invocations.
FeaturePromiseResolve
bool FeaturePromiseResolve(FeatureInstanceHandle handle, FtPromiseId pid, ...);
Resolves a Promise with variadic arguments. Only a single argument is supported.
Parameters:
handleFeature instance handle.pidPromise ID....The argument to pass.
Returns:
Returns true on success, or false on failure.
FeaturePromiseReject
bool FeaturePromiseReject(FeatureInstanceHandle handle, FtPromiseId pid,
int code, const char* msg);
Rejects a Promise with an error code and error message.
Parameters:
handleFeature instance handle.pidPromise ID.codeError code.msgError message.
Returns:
Returns true on success, or false on failure.
FeatureGetPromiseType
enum FeaturePromiseType FeatureGetPromiseType(FeatureInstanceHandle handle, FtPromiseId pid);
Gets the model type used by the current asynchronous invocation. Feature implementations can use this to choose between Callback or Promise interfaces.
Parameters:
handleFeature instance handle.pidPromise ID.
Returns:
Returns an FeaturePromiseType enum:
FEATURE_PROMISE_TYPE_INVALID: Invalid (typically indicates the Promise is no longer valid)FEATURE_PROMISE_TYPE_PROMISE: Promise modelFEATURE_PROMISE_TYPE_CALLBACKS: Callback model
Typed Promise Resolve
The following set of interfaces specialize Promise resolution by type, avoiding type ambiguity from variadic arguments. All return FT_TRUE on success and FT_FALSE on failure.
FeatureFtStringPromiseResolve
FtBool FeatureFtStringPromiseResolve(FeatureInstanceHandle handle, FtPromiseId pid, FtString val);
Resolves a Promise with a string.
Parameters:
handleFeature instance handle.pidPromise ID.valString result.
Returns:
Returns FT_TRUE on success, or FT_FALSE on failure.
FeatureFtIntPromiseResolve
FtBool FeatureFtIntPromiseResolve(FeatureInstanceHandle handle, FtPromiseId pid, FtInt val);
Resolves a Promise with an FtInt (int32).
Parameters:
handleFeature instance handle.pidPromise ID.valint32 result.
Returns:
Returns FT_TRUE on success, or FT_FALSE on failure.
FeatureFtUint32PromiseResolve
FtBool FeatureFtUint32PromiseResolve(FeatureInstanceHandle handle, FtPromiseId pid, FtUint32 val);
Resolves a Promise with an FtUint32.
Parameters:
handleFeature instance handle.pidPromise ID.valuint32 result.
Returns:
Returns FT_TRUE on success, or FT_FALSE on failure.
FeatureFtInt8PromiseResolve
FtBool FeatureFtInt8PromiseResolve(FeatureInstanceHandle handle, FtPromiseId pid, FtInt8 val);
Resolves a Promise with an FtInt8.
Parameters:
handleFeature instance handle.pidPromise ID.valint8 result.
Returns:
Returns FT_TRUE on success, or FT_FALSE on failure.
FeatureFtUint8PromiseResolve
FtBool FeatureFtUint8PromiseResolve(FeatureInstanceHandle handle, FtPromiseId pid, FtUint8 val);
Resolves a Promise with an FtUint8.
Parameters:
handleFeature instance handle.pidPromise ID.valuint8 result.
Returns:
Returns FT_TRUE on success, or FT_FALSE on failure.
FeatureFtInt16PromiseResolve
FtBool FeatureFtInt16PromiseResolve(FeatureInstanceHandle handle, FtPromiseId pid, FtInt16 val);
Resolves a Promise with an FtInt16.
Parameters:
handleFeature instance handle.pidPromise ID.valint16 result.
Returns:
Returns FT_TRUE on success, or FT_FALSE on failure.
FeatureFtUint16PromiseResolve
FtBool FeatureFtUint16PromiseResolve(FeatureInstanceHandle handle, FtPromiseId pid, FtUint16 val);
Resolves a Promise with an FtUint16.
Parameters:
handleFeature instance handle.pidPromise ID.valuint16 result.
Returns:
Returns FT_TRUE on success, or FT_FALSE on failure.
FeatureFtInt64PromiseResolve
FtBool FeatureFtInt64PromiseResolve(FeatureInstanceHandle handle, FtPromiseId pid, FtInt64 val);
Resolves a Promise with an FtInt64.
Parameters:
handleFeature instance handle.pidPromise ID.valint64 result.
Returns:
Returns FT_TRUE on success, or FT_FALSE on failure.
FeatureFtUint64PromiseResolve
FtBool FeatureFtUint64PromiseResolve(FeatureInstanceHandle handle, FtPromiseId pid, FtUint64 val);
Resolves a Promise with an FtUint64.
Parameters:
handleFeature instance handle.pidPromise ID.valuint64 result.
Returns:
Returns FT_TRUE on success, or FT_FALSE on failure.
FeatureFtFloatPromiseResolve
FtBool FeatureFtFloatPromiseResolve(FeatureInstanceHandle handle, FtPromiseId pid, FtFloat val);
Resolves a Promise with an FtFloat (float).
Parameters:
handleFeature instance handle.pidPromise ID.valfloat result.
Returns:
Returns FT_TRUE on success, or FT_FALSE on failure.
FeatureFtDoublePromiseResolve
FtBool FeatureFtDoublePromiseResolve(FeatureInstanceHandle handle, FtPromiseId pid, FtDouble val);
Resolves a Promise with an FtDouble (double).
Parameters:
handleFeature instance handle.pidPromise ID.valdouble result.
Returns:
Returns FT_TRUE on success, or FT_FALSE on failure.
FeatureFtBoolPromiseResolve
FtBool FeatureFtBoolPromiseResolve(FeatureInstanceHandle handle, FtPromiseId pid, FtBool val);
Resolves a Promise with an FtBool.
Parameters:
handleFeature instance handle.pidPromise ID.valbool result.
Returns:
Returns FT_TRUE on success, or FT_FALSE on failure.
FeatureFtAnyPromiseResolve
FtBool FeatureFtAnyPromiseResolve(FeatureInstanceHandle handle, FtPromiseId pid, FtAny val);
Resolves a Promise with an FtAny (ft_value_t*). Can be used to return results of any type.
Parameters:
handleFeature instance handle.pidPromise ID.valft_value_t*result.
Returns:
Returns FT_TRUE on success, or FT_FALSE on failure.
FeatureFtArrayPromiseResolve
FtBool FeatureFtArrayPromiseResolve(FeatureInstanceHandle handle, FtPromiseId pid, FtArray* val);
Resolves a Promise with an FtArray*.
Parameters:
handleFeature instance handle.pidPromise ID.valArray result.
Returns:
Returns FT_TRUE on success, or FT_FALSE on failure.
Event Management
FeatureGetEventId
FtEventId FeatureGetEventId(FeatureInstanceHandle handle, const char* name);
Looks up the event ID for the given event name.
Parameters:
handleFeature instance handle.nameEvent name.
Returns:
Returns a valid event ID (positive integer) on success, or ≤ 0 if the event does not exist.
FeatureGetEventName
const char* FeatureGetEventName(FeatureInstanceHandle handle, FtEventId eid);
Gets the event name for the given event ID.
Parameters:
handleFeature instance handle.eidEvent ID.
Returns:
Returns the event name string on success, or NULL if it does not exist.
FeatureEmitEvent
bool FeatureEmitEvent(FeatureInstanceHandle handle, FtEventId eid, ...);
Emits an event by event ID, allowing variadic arguments to be passed to subscribers.
Parameters:
handleFeature instance handle.eidEvent ID....Event payload; types must match the Feature interface declaration.
Returns:
Returns true on successful emission, or false on failure (e.g., the event does not exist, or the instance has been detached).
FeatureEmitEventByName
bool FeatureEmitEventByName(FeatureInstanceHandle handle, const char* name, ...);
Emits an event by name. Internally looks up the ID via FeatureGetEventId before emitting.
Parameters:
handleFeature instance handle.nameEvent name....Event payload.
Returns:
Returns true on success, or false on failure.
FeatureSetEventChangeListener
void FeatureSetEventChangeListener(FeatureInstanceHandle handle,
FeatureEventChangeListener listener);
Sets an event-change listener on a Feature instance. The callback is triggered when the number of subscribers changes (a subscriber is added or removed).
Parameters:
handleFeature instance handle.listenerEvent-change listener with signaturevoid (*)(FeatureInstanceHandle, FtEventId, FeatureEventStatus).- Status
FEATURE_EVENT_ADDEDindicates a subscription was added - Status
FEATURE_EVENT_REMOVEDindicates a subscription was removed
- Status
FeatureGetEventCallbackCount
int FeatureGetEventCallbackCount(FeatureInstanceHandle handle, FtEventId eid);
Queries the number of registered subscribers for the given event ID. Can be used to skip event emission when there are no subscribers to save overhead.
Parameters:
handleFeature instance handle.eidEvent ID.
Returns:
Returns the number of subscribers; 0 if the event does not exist or on failure.
FeatureGetEventCallbackCountByName
static inline int FeatureGetEventCallbackCountByName(FeatureInstanceHandle handle, const char* name);
Queries the number of registered subscribers for the given event name. Internally implemented via FeatureGetEventId + FeatureGetEventCallbackCount.
Parameters:
handleFeature instance handle.nameEvent name.
Returns:
Returns the number of subscribers; 0 if the event does not exist.
Asynchronous Worker
The Worker mechanism is used to move time-consuming tasks to background execution, then notify the main thread of the result upon completion. Suitable for file I/O, compute-intensive scenarios, etc.
FeatureCreateWorker
FeatureWorkerHandle FeatureCreateWorker(FeatureInstanceHandle handle, FtPromiseId pid,
size_t buf_size,
void (*do_work)(FeatureWorkerHandle),
void (*do_after_worker)(FeatureWorkerHandle),
void (*free)(void*));
Creates a Worker object, ready to be dispatched for background execution.
Parameters:
handleFeature instance handle.pidAssociated Promise ID used to trigger resolve/reject after the Worker completes.buf_sizeSize of the Worker's private buffer, usable for passing data between the two callbacks.do_workBackground execution function, run on a worker thread.do_after_workerCompletion callback, run on the main thread; typically callsFeatureWorkerResolveorFeatureWorkerRejecthere.freeResource release function, called when the Worker is destroyed.
Returns:
Returns a FeatureWorkerHandle on success, or NULL on failure.
FeatureWorkerCommit
bool FeatureWorkerCommit(FeatureInstanceHandle handle, FeatureWorkerHandle hworker);
Commits the Worker for background execution.
Parameters:
handleFeature instance handle.hworkerWorker handle.
Returns:
Returns true on successful commit, or false on failure.
FeatureWorkerResolve
void FeatureWorkerResolve(FeatureInstanceHandle handle, FeatureWorkerHandle hworker,
FeatureWorkerResult result);
Notifies the Worker of a successful completion, triggering resolve on the associated Promise. Typically called inside the do_after_worker callback.
Parameters:
handleFeature instance handle.hworkerWorker handle.resultExecution result, seeFeatureWorkerResult.
FeatureWorkerReject
void FeatureWorkerReject(FeatureInstanceHandle handle, FeatureWorkerHandle hworker,
int errcode, const char* err_msg);
Notifies the Worker of a failed completion, triggering reject on the associated Promise.
Parameters:
handleFeature instance handle.hworkerWorker handle.errcodeError code.err_msgError message.
FeatureWorkerIsValid
bool FeatureWorkerIsValid(FeatureInstanceHandle handle, FeatureWorkerHandle hworker);
Determines whether the Worker is still valid.
Parameters:
handleFeature instance handle.hworkerWorker handle.
Returns:
Returns true if valid, or false if invalid or already completed.
FeatureWorkerGetState
int FeatureWorkerGetState(FeatureWorkerHandle hworker);
Gets the current state of the Worker.
Parameters:
hworkerWorker handle.
Returns:
Returns a FeatureWorkerState enum value:
FEATURE_WORKER_PENDING: PendingFEATURE_WORKER_RUNNING: RunningFEATURE_WORKER_INVALID: InvalidFEATURE_WORKER_RESOLVED: ResolvedFEATURE_WORKER_REJECTED: RejectedFEATURE_WORKER_FINISHED: Finished
FeatureWorkerCancel
int FeatureWorkerCancel(FeatureInstanceHandle handle, FeatureWorkerHandle hworker);
Attempts to cancel Worker execution. Tasks in the pending state cannot be canceled immediately.
Parameters:
handleFeature instance handle.hworkerWorker handle.
Returns:
Returns a FeatureWorkerCancelResult enum value:
FeatureWorkerCancelSuccess: Successfully canceledFeatureWorkerCancelPending: Worker is pending, cancellation failedFeatureWorkerCancelInvalid: Worker is invalidFeatureWorkerCancelUnknownError: Unknown error
JSON Object
FeatureNewJsonObject
FtJsonObject FeatureNewJsonObject(const char* str);
Creates a JSON object from the given string.
Parameters:
strJSON string.
Returns:
Returns an FtJsonObject on success, or NULL on failure.
FeatureAllocJsonObject
FtJsonObject FeatureAllocJsonObject(size_t str_len);
Allocates space for a JSON object by the specified string length (content not initialized).
Parameters:
str_lenString length.
Returns:
Returns an FtJsonObject on success, or NULL on failure.
FeatureGetJsonString
const char* FeatureGetJsonString(const FtJsonObject json_obj);
Gets the string view corresponding to the JSON object.
Parameters:
json_objJSON object.
Returns:
Returns the string pointer on success, or NULL on failure. The returned pointer is managed by the framework; do not release it manually.
Feature Registration
FeatureRegisterFeatures
bool FeatureRegisterFeatures(FeatureRegistryHandle handle,
const FeatureRegistryTableHandle regTable);
Registers a group of Features to the Feature registry in batch. Typically called during module initialization.
Parameters:
handleFeature registry handle.regTableRegistry entry table, seeFeatureRegistryTable.
Returns:
Returns true if all registrations succeed, or false if any fails.