Add more exposed API functions to headers

This commit is contained in:
K. Lange 2022-07-29 09:51:00 +09:00
parent 74d3b5f479
commit 71ab25b679
4 changed files with 80 additions and 8 deletions

View File

@ -111,11 +111,6 @@ KRK_Method(object,__eq__) {
return NOTIMPL_VAL();
}
extern KrkValue krk_instanceSetAttribute_wrapper(KrkValue owner, KrkString * name, KrkValue to);
extern int krk_getAttribute(KrkString*);
extern int krk_setAttribute(KrkString*);
extern int krk_delAttribute(KrkString*);
KRK_Function(getattr) {
FUNCTION_TAKES_AT_LEAST(2);
CHECK_ARG(1,str,KrkString*,property);

View File

@ -190,3 +190,19 @@ extern int krk_hashValue(KrkValue value, uint32_t *hashOut);
* @param capacity Target capacity.
*/
extern void krk_tableAdjustCapacity(KrkTable * table, size_t capacity);
/**
* @brief Update the value of a table entry only if it is found.
* @memberof KrkTable
*
* Searches the table for @p key and updates its value to @p value if found.
* If @p key is not found, it is not added to the table.
*
* @warning Note the return value of this function is inverted from krk_tableSet
*
* @param table Table to assign to.
* @param key Key to assign.
* @param value Value to assign to the key.
* @return 0 if the key was not present, 1 if it was found and updated.
*/
extern int krk_tableSetIfExists(KrkTable * table, KrkValue key, KrkValue value);

View File

@ -892,4 +892,67 @@ extern void krk_setMaximumRecursionDepth(size_t maxDepth);
* held stack is reallocated, it will be freed when execution returns to the call
* to @c krk_callNativeOnStack that holds it.
*/
KrkValue krk_callNativeOnStack(size_t argCount, const KrkValue *stackArgs, int hasKw, NativeFn native);
extern KrkValue krk_callNativeOnStack(size_t argCount, const KrkValue *stackArgs, int hasKw, NativeFn native);
/**
* @brief Set an attribute of an instance object, bypassing \__setattr__.
*
* This can be used to emulate the behavior of super(object).__setattr__ for
* types that derive from KrkInstance and have a fields table, and is the internal
* mechanism by which object.__setattr__() performs this task.
*
* Does not bypass descriptors.
*
* @param owner Instance object to set an attribute on.
* @param name Name of the attribute
* @param to New value for the attribute
* @return The value set, which is likely @p to but may be the returned value of a descriptor \__set__ method.
*/
extern KrkValue krk_instanceSetAttribute_wrapper(KrkValue owner, KrkString * name, KrkValue to);
/**
* @brief Implementation of the GET_PROPERTY instruction.
*
* Retrieves the attribute specifed by @p name from the value at the top of the
* stack. The top of the stack will be replaced with the resulting attribute value,
* if one is found, and 1 will be returned. Otherwise, 0 is returned and the stack
* remains unchanged. No exception is raised if the property is not found, allowing
* this function to be used in context where a default value is desired, but note
* that exceptions may be raised \__getattr__ methods or by descriptor \__get__ methods.
*
* @param name Name of the attribute to look up.
* @return 1 if the attribute was found, 0 otherwise.
*/
extern int krk_getAttribute(KrkString * name);
/**
* @brief Implementation of the SET_PROPERTY instruction.
*
* Sets the attribute specifed by @p name on the value second from top of the stack
* to the value at the top of the stack. Upon successful completion, 1 is returned
* the stack is reduced by one slot, and the top of the stack is the value set, which
* may be the result of a descriptor \__set__ method. If the owner object does not
* allow for attributes to be set, and no descriptor object is present, 0 will be
* returned and the stack remains unmodified. No exception is raised in this case,
* though exceptions may still be raised by \__setattr__ methods or descriptor
* \__set__ methods.
*
* @param name Name of the attribute to set.
* @return 1 if the attribute could be set, 0 otherwise.
*/
extern int krk_setAttribute(KrkString * name);
/**
* @brief Implementation of the DEL_PROPERTY instruction.
*
* Attempts to delete the attribute specified by @p name from the value at the
* top of the stack, returning 1 and reducing the stack by one on success. If
* the attribute is not found or attribute deletion is not meaningful, 0 is
* returned and the stack remains unmodified, but no exception is raised.
*
* @warning Currently, no \__delattr__ mechanism is available.
*
* @param name Name of the attribute to delete.
* @return 1 if the attribute was found and can be deleted, 0 otherwise.
*/
extern int krk_delAttribute(KrkString * name);

View File

@ -2192,8 +2192,6 @@ static int trySetDescriptor(KrkValue owner, KrkString * name, KrkValue value) {
return 0;
}
extern int krk_tableSetIfExists(KrkTable * table, KrkValue key, KrkValue value);
_noexport
KrkValue krk_instanceSetAttribute_wrapper(KrkValue owner, KrkString * name, KrkValue to) {
if (!krk_tableSetIfExists(&AS_INSTANCE(owner)->fields, OBJECT_VAL(name), to)) {