[ English | 简体中文 ]
Feature Context API
Unified data type and context operation interfaces provided by the Feature framework. By wrapping frontend (QuickJS, WAMR, etc.) native objects through ft_value_t, developers can perform type conversion, array/object operations, and memory management without being aware of specific frontend differences.
Header: #include <feature_context.h>
openvela Implementation Notes
- Core data type
ft_value_t: Uniformly wraps frontend JSValue/Wasm objects, using 16-byte or 8-byte storage depending on whether the target platform is 64-bit ft_context_ref: A context object that accompaniesft_value_t; all APIs require it to locate the specific frontend runtime- Value types vs reference types:
ft_from_int/ft_from_booletc. return value types that do not require explicit releaseft_from_string/ft_from_buffer/ft_new_objectetc. return reference types that must be released viaft_free_value
- Release rules:
- No release needed:
ft_value_tparameters received by Feature implementation functions,ft_value_treturned to the frontend as return values - Must release: Objects created by
ft_from_xxx, objects created byft_new_object, elements retrieved byft_array_at, properties returned byft_obj_get_property, results fromft_parse_json - Strings:
const char*returned byft_to_stringmust be released withft_free_string
- No release needed:
Feature Context and Frontend Runtime
The diagram below shows how the Feature framework uses ft_value_t and ft_context_ref to uniformly wrap native objects of frontend runtimes (using QuickJS as an example):
- Feature Framework Interface: The unified C interface exposed by the Feature framework, consisting of
ft_value_t(data) andft_context_ref(context). - JS Implementation: The concrete frontend runtime implementation.
ft_value_tmaps toJSValue, andft_context_refmaps toJSContext, with an N:1 relationship between them (multiple values belong to the same context). - When switching to another frontend (for example, WAMR), the Feature implementation code does not need to change; only the underlying mapping needs to be replaced.
Type and Context Access
ft_context_get_data
void* ft_context_get_data(ft_context_ref ft_ctx);
Retrieves the user data pointer associated with the current Feature context. This user data is bound by the Feature manager during initialization.
Parameters:
ft_ctxCurrent Feature context reference.
Returns:
Returns the associated user data pointer, or NULL if not bound.
ft_get_type
ft_type ft_get_type(ft_context_ref ft_ctx, ft_value_t ft_val);
Gets the type of a given ft_value_t.
Parameters:
ft_ctxCurrent Feature context reference.ft_valTheft_value_twhose type is to be queried.
Returns:
Returns the type enum ft_type:
FT_TYPE_NULL: nullFT_TYPE_UNDEF: undefinedFT_TYPE_NONE: undefined valueFT_TYPE_NUMBER: numberFT_TYPE_BOOL: booleanFT_TYPE_STRING: stringFT_TYPE_ARRAY: arrayFT_TYPE_BUFFER: binary bufferFT_TYPE_TYPED_BUFFER: typed bufferFT_TYPE_OBJECT: object
ft_undefined
ft_value_t ft_undefined(ft_context_ref ft_ctx);
Constructs an undefined-type ft_value_t. Used to return a "no value" result to the frontend.
Parameters:
ft_ctxCurrent Feature context reference.
Returns:
Returns an ft_value_t of type FT_TYPE_UNDEF. This is a value type and does not require explicit release.
Type Conversion from Primitive Types (Native to ft_value_t)
The following interfaces convert C native types to ft_value_t for passing to the frontend.
ft_from_int
ft_value_t ft_from_int(ft_context_ref ft_ctx, int32_t val);
Converts a 32-bit signed integer to ft_value_t. The return value is a value type and does not require release.
Parameters:
ft_ctxCurrent Feature context reference.val32-bit signed integer value.
Returns:
Returns the corresponding ft_value_t (type FT_TYPE_NUMBER).
ft_from_uint
ft_value_t ft_from_uint(ft_context_ref ft_ctx, uint32_t val);
Converts a 32-bit unsigned integer to ft_value_t.
Parameters:
ft_ctxCurrent Feature context reference.val32-bit unsigned integer value.
Returns:
Returns the corresponding ft_value_t.
ft_from_int64
ft_value_t ft_from_int64(ft_context_ref ft_ctx, int64_t val);
Converts a 64-bit signed integer to ft_value_t.
Parameters:
ft_ctxCurrent Feature context reference.val64-bit signed integer value.
Returns:
Returns the corresponding ft_value_t.
ft_from_uint64
ft_value_t ft_from_uint64(ft_context_ref ft_ctx, uint64_t val);
Converts a 64-bit unsigned integer to ft_value_t.
Parameters:
ft_ctxCurrent Feature context reference.val64-bit unsigned integer value.
Returns:
Returns the corresponding ft_value_t.
ft_from_double
ft_value_t ft_from_double(ft_context_ref ft_ctx, double val);
Converts a double-precision floating-point number to ft_value_t.
Parameters:
ft_ctxCurrent Feature context reference.valDouble-precision floating-point value.
Returns:
Returns the corresponding ft_value_t.
ft_from_bool
ft_value_t ft_from_bool(ft_context_ref ft_ctx, bool val);
Converts a boolean value to ft_value_t.
Parameters:
ft_ctxCurrent Feature context reference.valBoolean value.
Returns:
Returns the corresponding ft_value_t (type FT_TYPE_BOOL).
ft_from_string
ft_value_t ft_from_string(ft_context_ref ft_ctx, const char* val);
Converts a C string to ft_value_t.
Parameters:
ft_ctxCurrent Feature context reference.valNull-terminated C string.
Returns:
Returns the corresponding ft_value_t (type FT_TYPE_STRING).
Notes:
- The returned
ft_value_tis a reference type and must be released viaft_free_value. - The implementation copies the content of
val; the original pointer can be freed immediately after the call.
Binary Buffer Conversion
ft_from_buffer
ft_value_t ft_from_buffer(ft_context_ref ft_ctx, uint8_t* buff, uint32_t size);
Wraps a native byte buffer into an ft_value_t.
Parameters:
ft_ctxCurrent Feature context reference.buffPointer to the start of the byte buffer.sizeNumber of bytes in the buffer.
Returns:
Returns the corresponding ft_value_t (type FT_TYPE_BUFFER).
Notes:
- The returned
ft_value_tis a reference type and must be released viaft_free_value.
ft_from_typed_buffer
ft_value_t ft_from_typed_buffer(ft_context_ref ft_ctx, uint8_t* buff,
uint32_t size, FtTypedArrayType type);
Wraps a native buffer into a frontend Typed Array.
Parameters:
ft_ctxCurrent Feature context reference.buffPointer to the start of the byte buffer.sizeNumber of bytes in the buffer.typeElement type of the typed array, seeFtTypedArrayType:FT_Int8Array/FT_Uint8ArrayFT_Int16Array/FT_Uint16ArrayFT_Int32Array/FT_Uint32ArrayFT_Float32Array/FT_Float64Array
Returns:
Returns the corresponding ft_value_t (type FT_TYPE_TYPED_BUFFER). Must be released via ft_free_value.
Example:
uint8_t* buff = ft_to_buffer(ft_ctx, &size, data);
// Process buff
ft_value_t ret = ft_from_typed_buffer(ft_ctx, buff, size, FT_Uint8Array);
ft_parse_json
ft_value_t ft_parse_json(ft_context_ref ft_ctx, const char* buf,
size_t buf_len, const char* filename);
Parses a JSON string into an ft_value_t object.
Parameters:
ft_ctxCurrent Feature context reference.bufPointer to the JSON string.buf_lenLength of the JSON string in bytes.filenameFilename used for error location, can beNULL.
Returns:
Returns the parsed ft_value_t on success; returns a value of type FT_TYPE_UNDEF on failure.
Notes:
- The returned
ft_value_tis a reference type and must be released viaft_free_value.
Array Type Conversion (Native Array to ft_value_t)
Converts native arrays to ft_value_t arrays. All return values are reference types and must be released via ft_free_value.
ft_from_int_array
ft_value_t ft_from_int_array(ft_context_ref ft_ctx, int32_t* val, uint32_t size);
Converts an int32 array to ft_value_t.
Parameters:
ft_ctxCurrent Feature context reference.valSource array pointer.sizeNumber of array elements.
Returns:
Returns the corresponding ft_value_t (type FT_TYPE_ARRAY).
ft_from_uint_array
ft_value_t ft_from_uint_array(ft_context_ref ft_ctx, uint32_t* val, uint32_t size);
Converts a uint32 array to ft_value_t.
Parameters:
ft_ctxCurrent Feature context reference.valSource array pointer.sizeNumber of array elements.
Returns:
Returns the corresponding ft_value_t.
ft_from_int64_array
ft_value_t ft_from_int64_array(ft_context_ref ft_ctx, int64_t* val, uint32_t size);
Converts an int64 array to ft_value_t.
Parameters:
ft_ctxCurrent Feature context reference.valSource array pointer.sizeNumber of array elements.
Returns:
Returns the corresponding ft_value_t.
ft_from_uint64_array
ft_value_t ft_from_uint64_array(ft_context_ref ft_ctx, uint64_t* val, uint32_t size);
Converts a uint64 array to ft_value_t.
Parameters:
ft_ctxCurrent Feature context reference.valSource array pointer.sizeNumber of array elements.
Returns:
Returns the corresponding ft_value_t.
ft_from_bool_array
ft_value_t ft_from_bool_array(ft_context_ref ft_ctx, bool* val, uint32_t size);
Converts a boolean array to ft_value_t.
Parameters:
ft_ctxCurrent Feature context reference.valSource array pointer.sizeNumber of array elements.
Returns:
Returns the corresponding ft_value_t.
ft_from_double_array
ft_value_t ft_from_double_array(ft_context_ref ft_ctx, double* val, uint32_t size);
Converts a double array to ft_value_t.
Parameters:
ft_ctxCurrent Feature context reference.valSource array pointer.sizeNumber of array elements.
Returns:
Returns the corresponding ft_value_t.
ft_from_string_array
ft_value_t ft_from_string_array(ft_context_ref ft_ctx, const char** val, uint32_t size);
Converts a C string array to ft_value_t.
Parameters:
ft_ctxCurrent Feature context reference.valArray of string pointers.sizeNumber of array elements.
Returns:
Returns the corresponding ft_value_t.
Type Conversion to Primitive Types (ft_value_t to Native)
The following interfaces convert ft_value_t passed from the frontend to C native types for use in Feature implementations.
ft_to_int
bool ft_to_int(ft_context_ref ft_ctx, ft_value_t f_val, int32_t* val);
Converts an ft_value_t to a 32-bit signed integer.
Parameters:
ft_ctxCurrent Feature context reference.f_valSourceft_value_t, should be a numeric type.valPointer toint32_tto receive the result.
Returns:
Returns true on successful conversion, false otherwise (typically because f_val is not a numeric type).
ft_to_uint
bool ft_to_uint(ft_context_ref ft_ctx, ft_value_t f_val, uint32_t* val);
Converts an ft_value_t to a 32-bit unsigned integer.
Parameters:
ft_ctxCurrent Feature context reference.f_valSourceft_value_t.valPointer touint32_tto receive the result.
Returns:
Returns true on success, false on failure.
ft_to_int64
bool ft_to_int64(ft_context_ref ft_ctx, ft_value_t f_val, int64_t* val);
Converts an ft_value_t to a 64-bit signed integer.
Parameters:
ft_ctxCurrent Feature context reference.f_valSourceft_value_t.valPointer toint64_tto receive the result.
Returns:
Returns true on success, false on failure.
ft_to_uint64
bool ft_to_uint64(ft_context_ref ft_ctx, ft_value_t f_val, uint64_t* val);
Converts an ft_value_t to a 64-bit unsigned integer.
Parameters:
ft_ctxCurrent Feature context reference.f_valSourceft_value_t.valPointer touint64_tto receive the result.
Returns:
Returns true on success, false on failure.
ft_to_double
bool ft_to_double(ft_context_ref ft_ctx, ft_value_t f_val, double* val);
Converts an ft_value_t to a double-precision floating-point number.
Parameters:
ft_ctxCurrent Feature context reference.f_valSourceft_value_t.valPointer todoubleto receive the result.
Returns:
Returns true on success, false on failure.
ft_to_bool
bool ft_to_bool(ft_context_ref ft_ctx, ft_value_t ft_val, bool* val);
Converts an ft_value_t to a boolean value.
Parameters:
ft_ctxCurrent Feature context reference.ft_valSourceft_value_t.valPointer toboolto receive the result.
Returns:
Returns true on success, false on failure.
ft_to_string
const char* ft_to_string(ft_context_ref ft_ctx, ft_value_t f_val);
Converts an ft_value_t to a C string.
Parameters:
ft_ctxCurrent Feature context reference.f_valSourceft_value_t, should be a string type.
Returns:
Returns the string pointer on success; returns NULL on failure.
Notes:
- The returned string is managed by the framework and must be released via
ft_free_string. - The Feature framework does not guarantee the string remains valid long-term; copy it promptly if you need to retain it.
ft_to_buffer
uint8_t* ft_to_buffer(ft_context_ref ft_ctx, size_t* p_size, ft_value_t f_val);
Converts an ft_value_t to a binary buffer.
Parameters:
ft_ctxCurrent Feature context reference.p_sizeOutput parameter that returns the number of bytes in the buffer.f_valSourceft_value_t, should beFT_TYPE_BUFFERorFT_TYPE_TYPED_BUFFER.
Returns:
Returns the buffer start pointer on success; returns NULL on failure.
Notes:
- The returned pointer is managed by the frontend; do not manually
freeit.
Array Operations
ft_array_size
uint32_t ft_array_size(ft_context_ref ft_ctx, const ft_value_t array);
Gets the number of elements in an ft_value_t array.
Parameters:
ft_ctxCurrent Feature context reference.arrayAnft_value_tof array type.
Returns:
Returns the number of array elements. Returns 0 if array is not an array type.
ft_array_at
ft_value_t ft_array_at(ft_context_ref ft_ctx, const ft_value_t array, uint32_t idx);
Accesses an element in an ft_value_t array by index.
Parameters:
ft_ctxCurrent Feature context reference.arrayAnft_value_tof array type.idxElement index, starting from 0.
Returns:
Returns the element ft_value_t at the given index on success; returns undefined if out of bounds or array is not an array.
Notes:
- The returned
ft_value_tis a reference type and must be released viaft_free_value.
Object Operations
ft_new_object
ft_value_t ft_new_object(ft_context_ref ft_ctx);
Creates an empty ft_value_t object. Features can attach custom properties to this object.
Parameters:
ft_ctxCurrent Feature context reference.
Returns:
Returns the newly created object ft_value_t (type FT_TYPE_OBJECT).
Notes:
- The return value is a reference type and must be released via
ft_free_value. - The object supports attaching child properties; child properties are automatically released when the root object is released.
ft_obj_get_property
ft_value_t ft_obj_get_property(ft_context_ref ft_ctx, ft_value_t ft_val, const char* prop);
Reads a property value from an ft_value_t object by property name.
Parameters:
ft_ctxCurrent Feature context reference.ft_valAnft_value_tof object type.propProperty name.
Returns:
Returns the property value ft_value_t on success; returns undefined if the property does not exist.
Notes:
- The returned
ft_value_tis a reference type and must be released viaft_free_value.
ft_obj_set_property
bool ft_obj_set_property(ft_context_ref ft_ctx, ft_value_t obj,
const char* prop, ft_value_t val);
Sets a property value on an ft_value_t object.
Parameters:
ft_ctxCurrent Feature context reference.objTarget objectft_value_t.propProperty name.valProperty value to set.
Returns:
Returns true on success, false on failure.
Memory Management
ft_free_value
void ft_free_value(ft_context_ref ft_ctx, ft_value_t ft_val);
Releases the reference count of an ft_value_t.
Parameters:
ft_ctxCurrent Feature context reference.ft_valTheft_value_tto release.
Notes:
ft_value_t sources that require release:
- Objects created by
ft_from_xxx - Objects created by
ft_new_object - Elements retrieved by
ft_array_at - Properties returned by
ft_obj_get_property - Objects parsed by
ft_parse_json
ft_value_t sources that do not require release:
- Parameters received by Feature implementation functions
ft_value_treturned to the frontend as return values
Failing to properly release reference-type ft_value_t will cause memory leaks.
ft_free_string
void ft_free_string(ft_context_ref ft_ctx, const char* str);
Releases a string returned by ft_to_string.
Parameters:
ft_ctxCurrent Feature context reference.strString pointer to release.
Notes:
- The Feature framework does not guarantee that strings returned by
ft_to_stringremain valid long-term; call this interface to release them promptly after use. - Do not use
free()ordeleteto release; you must use this interface.