22 Commits

Author SHA1 Message Date
wash fa9572d33a fx: iterator: return NULL value if iterator status is not SUCCESS 2026-05-30 20:19:47 +01:00
wash 8c0a31e19b meta: update rosetta support 2026-05-30 10:09:55 +01:00
wash a20306ae64 cmake: update package finder module 2026-05-29 20:45:46 +01:00
wash 99380c7810 test: fix linux compilation 2026-05-29 20:20:31 +01:00
wash 2d396ee071 fx.reflection: fix linux compilation 2026-05-29 20:20:19 +01:00
wash 34a8ccff95 fx: fix linux compilation 2026-05-29 20:20:05 +01:00
wash d9835cea0b fx.io: fix linux compilation 2026-05-29 20:19:55 +01:00
wash 7b546f6bbe meta: update clang-format config 2026-05-29 20:04:34 +01:00
wash 65f490b900 fx.serial: toml: update array and hashtable usage 2026-05-29 20:04:15 +01:00
wash 1516eee8d1 fx.collections: hashtable: update allocate_index_for_key to handle exact key matches 2026-05-29 20:02:48 +01:00
wash 53a856794f fx.collections: move fx_datetime to fx 2026-05-29 20:02:02 +01:00
wash f879d4fdfc fx: object: add nullptr check to fx_object_is_type 2026-05-29 19:59:55 +01:00
wash e53027595e fx: double add missing fx.comparable reference 2026-05-29 19:59:30 +01:00
wash bbb26c5ee5 fx: pointer: implement hash() and compare() 2026-05-29 19:59:00 +01:00
wash e78241c051 fx: printf: adjust default floating point precision 2026-05-29 19:58:40 +01:00
wash 5877abd33e fx.reflection: update iterator semantics 2026-05-28 21:00:23 +01:00
wash b31e82aaf8 fx.io: update iterator semantics 2026-05-28 20:53:53 +01:00
wash 4c06ded99f fx.cmdline: fix namespace not having the required header subdirectory 2026-05-28 20:53:16 +01:00
wash ff0d40b324 fx.collections: update iterator semantics 2026-05-28 20:52:40 +01:00
wash 883b0b24b1 fx: improve fx_value reference management 2026-05-28 20:51:55 +01:00
wash 516f5be2d6 fx: implement fx_namemap cleanup 2026-05-28 20:50:27 +01:00
wash f79650698a fx: update fx_iterator to return pointers to values 2026-05-28 20:50:00 +01:00
57 changed files with 1420 additions and 943 deletions
+1 -1
View File
@@ -45,7 +45,7 @@ SpacesBeforeTrailingComments: 3
TabWidth: 8 TabWidth: 8
UseTab: AlignWithSpaces UseTab: AlignWithSpaces
BreakAfterReturnType: Automatic BreakAfterReturnType: Automatic
PenaltyBreakAssignment: 1000000 PenaltyBreakAssignment: 0
PenaltyReturnTypeOnItsOwnLine: 100000000 PenaltyReturnTypeOnItsOwnLine: 100000000
PenaltyExcessCharacter: 10000000 PenaltyExcessCharacter: 10000000
PenaltyBreakOpenParenthesis: 0 PenaltyBreakOpenParenthesis: 0
+3 -1
View File
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.25)
project(fx C ASM) project(fx C ASM)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake) set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include (TestBigEndian) include (TestBigEndian)
include(Templates) include(Templates)
include(Platform) include(Platform)
@@ -59,4 +59,6 @@ foreach (assembly ${fx_assemblies})
add_subdirectory(assemblies/${assembly}) add_subdirectory(assemblies/${assembly})
endforeach (assembly) endforeach (assembly)
if (fx_enable_tests)
add_subdirectory(test) add_subdirectory(test)
endif ()
+43 -108
View File
@@ -49,43 +49,37 @@ if (FX_STATIC)
set(_lib_suffix "-s") set(_lib_suffix "-s")
endif () endif ()
set(supported_components Core Ds Term Cmd Io Serial Compress) set(assemblies ${FX_FIND_COMPONENTS})
set(components ${FX_FIND_COMPONENTS})
string(REPLACE ";" ", " supported_components_string_list "${supported_components}")
if (NOT components)
set(components ${supported_components})
endif ()
set(required_vars) set(required_vars)
foreach (component ${components}) if (NOT FX_INCLUDE_DIR)
if (NOT "${component}" IN_LIST supported_components) find_path(FX_INCLUDE_DIR
message(FATAL_ERROR "'${component}' is not a valid FX module.\nSupported modules: ${supported_components_string_list}") NAMES fx/misc.h ${FX_FIND_ARGS}
endif ()
string(TOLOWER ${component} header_name)
set(lib_name ${header_name}${_lib_suffix})
if (NOT FX_${component}_INCLUDE_DIR)
find_path(FX_${component}_INCLUDE_DIR
NAMES fx/${header_name}.h ${FX_FIND_ARGS}
PATH_SUFFIXES include PATH_SUFFIXES include
PATHS ${FX_SEARCH_PATHS}) PATHS ${FX_SEARCH_PATHS})
endif () endif ()
if (NOT FX_${component}_LIBRARY) set(required_vars FX_INCLUDE_DIR)
find_library(FX_${component}_LIBRARY
NAMES fx-${lib_name} ${FX_FIND_ARGS} foreach (assembly ${assemblies})
string(TOLOWER ${assembly} header_name)
string(REPLACE "." "_" macro_name ${assembly})
string(TOUPPER ${macro_name} macro_name)
set(lib_name ${assembly}${_lib_suffix})
if (NOT ${macro_name}_LIBRARY)
find_library(${macro_name}_LIBRARY
NAMES ${lib_name} ${FX_FIND_ARGS}
PATH_SUFFIXES lib PATH_SUFFIXES lib
PATHS ${FX_SEARCH_PATHS}) PATHS ${FX_SEARCH_PATHS})
else () else ()
# on Windows, ensure paths are in canonical format (forward slahes): # on Windows, ensure paths are in canonical format (forward slahes):
file(TO_CMAKE_PATH "${FX_${component}_LIBRARY}" FX_${component}_LIBRARY) file(TO_CMAKE_PATH "${${macro_name}_LIBRARY}" ${macro_name}_LIBRARY)
endif() endif()
list(APPEND required_vars FX_${component}_INCLUDE_DIR FX_${component}_LIBRARY) list(APPEND required_vars ${macro_name}_LIBRARY)
endforeach (component) endforeach (assembly)
unset(FX_FIND_ARGS) unset(FX_FIND_ARGS)
@@ -96,94 +90,35 @@ find_package_handle_standard_args(FX
if (FX_FOUND) if (FX_FOUND)
set(created_targets) set(created_targets)
foreach (component ${components}) foreach (assembly ${assemblies})
string(TOLOWER ${component} header_name) set(target_name ${assembly})
set(lib_name ${header_name}${_lib_suffix}) string(REPLACE "fx." "" target_name ${target_name})
string(SUBSTRING ${target_name} 0 1 target_name_prefix)
string(TOUPPER ${target_name_prefix} target_name_prefix)
string(SUBSTRING ${target_name} 1 -1 target_name_suffix)
set(target_name ${target_name_prefix}${target_name_suffix})
if(NOT TARGET FX::${component}) string(TOLOWER ${assembly} header_name)
add_library(FX::${component} UNKNOWN IMPORTED) string(REPLACE "." "_" macro_name ${assembly})
set_target_properties(FX::${component} PROPERTIES string(REPLACE "." "_" macro_name ${assembly})
INTERFACE_INCLUDE_DIRECTORIES "${FX_${component}_INCLUDE_DIR}") string(TOUPPER ${macro_name} macro_name)
target_compile_definitions(FX::${component} INTERFACE _CRT_SECURE_NO_WARNINGS=1)
set(lib_name ${assembly}${_lib_suffix})
if (NOT TARGET FX::${target_name})
add_library(FX::${target_name} UNKNOWN IMPORTED)
set_target_properties(FX::${target_name} PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${FX_INCLUDE_DIR}")
target_compile_definitions(FX::${target_name} INTERFACE _CRT_SECURE_NO_WARNINGS=1)
if (FX_STATIC) if (FX_STATIC)
target_compile_definitions(FX::${component} INTERFACE FX_STATIC=1) target_compile_definitions(FX::${target_name} INTERFACE FX_STATIC=1)
endif () endif ()
set_target_properties(FX::${component} PROPERTIES set_target_properties(FX::${target_name} PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES "C" IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${FX_${component}_LIBRARY}") IMPORTED_LOCATION "${${macro_name}_LIBRARY}")
set(created_targets ${created_targets} ${component}) set(created_targets ${created_targets} ${assembly})
endif () endif ()
endforeach (component) endforeach (assembly)
foreach (component ${created_targets})
if ("${component}" STREQUAL "Ds")
if (NOT TARGET FX::Core)
message(FATAL_ERROR "FX: Module 'Ds' depends on 'Core', which was not specified in find_package()")
endif ()
target_link_libraries(FX::Ds INTERFACE FX::Core)
endif ()
if ("${component}" STREQUAL "Term")
if (NOT TARGET FX::Core)
message(FATAL_ERROR "FX: Module 'Term' depends on 'Core', which was not specified in find_package()")
endif ()
if (NOT TARGET FX::Ds)
message(FATAL_ERROR "FX: Module 'Term' depends on 'Ds', which was not specified in find_package()")
endif ()
target_link_libraries(FX::Term INTERFACE FX::Core FX::Ds)
endif ()
if ("${component}" STREQUAL "Serial")
if (NOT TARGET FX::Core)
message(FATAL_ERROR "FX: Module 'Serial' depends on 'Core', which was not specified in find_package()")
endif ()
if (NOT TARGET FX::Ds)
message(FATAL_ERROR "FX: Module 'Serial' depends on 'Ds', which was not specified in find_package()")
endif ()
target_link_libraries(FX::Serial INTERFACE FX::Core FX::Ds)
endif ()
if ("${component}" STREQUAL "Cmd")
if (NOT TARGET FX::Core)
message(FATAL_ERROR "FX: Module 'Cmd' depends on 'Core', which was not specified in find_package()")
endif ()
if (NOT TARGET FX::Ds)
message(FATAL_ERROR "FX: Module 'Cmd' depends on 'Ds', which was not specified in find_package()")
endif ()
if (NOT TARGET FX::Term)
message(FATAL_ERROR "FX: Module 'Cmd' depends on 'Term', which was not specified in find_package()")
endif ()
target_link_libraries(FX::Cmd INTERFACE FX::Core FX::Ds FX::Term)
endif ()
if ("${component}" STREQUAL "Io")
if (NOT TARGET FX::Core)
message(FATAL_ERROR "FX: Module 'Io' depends on 'Core', which was not specified in find_package()")
endif ()
if (NOT TARGET FX::Ds)
message(FATAL_ERROR "FX: Module 'Io' depends on 'Ds', which was not specified in find_package()")
endif ()
target_link_libraries(FX::Io INTERFACE FX::Core FX::Ds)
endif ()
if ("${component}" STREQUAL "Compress")
if (NOT TARGET FX::Core)
message(FATAL_ERROR "FX: Module 'Compress' depends on 'Core', which was not specified in find_package()")
endif ()
target_link_libraries(FX::Compress INTERFACE FX::Core FX::Ds)
endif ()
endforeach (component)
endif() endif()
+2 -2
View File
@@ -19,7 +19,7 @@ function(get_platform_details)
elseif ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") elseif ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
set(system_name "win32") set(system_name "win32")
else () else ()
message(FATAL_ERROR "Unsupported platform: ${CMAKE_SYSTEM_NAME}") string(TOLOWER "${CMAKE_SYSTEM_NAME}" system_name)
endif () endif ()
if ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64") if ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
@@ -27,7 +27,7 @@ function(get_platform_details)
elseif ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64") elseif ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64")
set(system_arch "aarch64") set(system_arch "aarch64")
else () else ()
message(FATAL_ERROR "Unsupported architecture: ${CMAKE_SYSTEM_PROCESSOR}") string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" system_arch)
endif () endif ()
set(${arg_SYSTEM} ${system_name} PARENT_SCOPE) set(${arg_SYSTEM} ${system_name} PARENT_SCOPE)
+2
View File
@@ -47,6 +47,7 @@ function(add_fx_assembly)
target_compile_definitions(${assembly_target_name} PRIVATE ${def}) target_compile_definitions(${assembly_target_name} PRIVATE ${def})
endforeach (def) endforeach (def)
if (fx_enable_tests)
foreach (ns ${arg_NAMESPACES}) foreach (ns ${arg_NAMESPACES})
file(GLOB test_sources ${fx_source_root}/${ns}/test/*.c) file(GLOB test_sources ${fx_source_root}/${ns}/test/*.c)
foreach (test_file ${test_sources}) foreach (test_file ${test_sources})
@@ -57,6 +58,7 @@ function(add_fx_assembly)
target_link_libraries(${test_name} ${assembly_target_name}) target_link_libraries(${test_name} ${assembly_target_name})
endforeach (test_file) endforeach (test_file)
endforeach (ns) endforeach (ns)
endif ()
set_target_properties(${assembly_target_name} PROPERTIES set_target_properties(${assembly_target_name} PROPERTIES
FOLDER "${assembly_name}") FOLDER "${assembly_name}")
+1 -1
View File
@@ -1,6 +1,6 @@
#include "command.h" #include "command.h"
#include <fx/cmd.h> #include <fx/cmdline/cmd.h>
#include <fx/string.h> #include <fx/string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
+1 -1
View File
@@ -1,6 +1,6 @@
#include "command.h" #include "command.h"
#include <fx/cmd.h> #include <fx/cmdline/cmd.h>
#include <fx/misc.h> #include <fx/misc.h>
#include <fx/string.h> #include <fx/string.h>
#include <fx/term/print.h> #include <fx/term/print.h>
+1 -1
View File
@@ -1,7 +1,7 @@
#include "command.h" #include "command.h"
#include <fx/bst.h> #include <fx/bst.h>
#include <fx/cmd.h> #include <fx/cmdline/cmd.h>
#include <fx/string.h> #include <fx/string.h>
#include <fx/term/print.h> #include <fx/term/print.h>
#include <fx/term/tty.h> #include <fx/term/tty.h>
+1 -1
View File
@@ -2,7 +2,7 @@
#define _FX_COMMAND_H_ #define _FX_COMMAND_H_
#include <fx/bst.h> #include <fx/bst.h>
#include <fx/cmd.h> #include <fx/cmdline/cmd.h>
#include <fx/queue.h> #include <fx/queue.h>
#include <fx/string.h> #include <fx/string.h>
+1 -1
View File
@@ -1,6 +1,6 @@
#include "command.h" #include "command.h"
#include <fx/cmd.h> #include <fx/cmdline/cmd.h>
#include <fx/string.h> #include <fx/string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
+13 -5
View File
@@ -1,4 +1,4 @@
#include <fx/cmd.h> #include <fx/cmdline/cmd.h>
#include <stdio.h> #include <stdio.h>
enum { enum {
@@ -27,16 +27,24 @@ const char *text
"great pleasure."; "great pleasure.";
static int test_command( static int test_command(
const fx_command *self, const fx_arglist *opt, const fx_array *args) const fx_command *self,
const fx_arglist *opt,
const fx_array *args)
{ {
printf("Hello, world!\n"); printf("Hello, world!\n");
fx_arglist_iterator it; fx_arglist_iterator it;
fx_arglist_iterator_begin( fx_arglist_iterator_begin(
opt, FX_COMMAND_INVALID_ID, FX_COMMAND_INVALID_ID, &it); opt,
FX_COMMAND_INVALID_ID,
FX_COMMAND_INVALID_ID,
&it);
while (fx_arglist_iterator_is_valid(&it)) { while (fx_arglist_iterator_is_valid(&it)) {
printf("opt:%u,arg:%u,i:%zu,value: %s\n", it.opt_id, printf("opt:%u,arg:%u,i:%zu,value: %s\n",
it.value->val_id, it.i, it.value->val_str); it.opt_id,
it.value->val_id,
it.i,
it.value->val_str);
fx_arglist_iterator_next(&it); fx_arglist_iterator_next(&it);
} }
+6 -8
View File
@@ -17,7 +17,7 @@ struct fx_array_p {
}; };
struct fx_array_iterator_p { struct fx_array_iterator_p {
fx_array *_a; const fx_array *_a;
struct fx_array_p *_a_p; struct fx_array_p *_a_p;
/** The index of the current value */ /** The index of the current value */
@@ -170,9 +170,7 @@ static fx_value array_get(struct fx_array_p *array, size_t at)
return FX_VALUE_EMPTY; return FX_VALUE_EMPTY;
} }
fx_value result; return fx_value_copy_return(array->ar_data[at]);
fx_value_copy(&result, &array->ar_data[at]);
return result;
} }
static size_t array_get_size(const struct fx_array_p *array) static size_t array_get_size(const struct fx_array_p *array)
@@ -373,7 +371,7 @@ static fx_status array_to_string(
/*** ITERATOR FUNCTIONS *******************************************************/ /*** ITERATOR FUNCTIONS *******************************************************/
static fx_iterator *iterable_begin(fx_object *obj) static const fx_iterator *iterable_begin(const fx_object *obj)
{ {
fx_array_iterator *it_obj = fx_object_create(FX_TYPE_ARRAY_ITERATOR); fx_array_iterator *it_obj = fx_object_create(FX_TYPE_ARRAY_ITERATOR);
struct fx_array_iterator_p *it = fx_object_get_private( struct fx_array_iterator_p *it = fx_object_get_private(
@@ -437,7 +435,7 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_SUCCESS; return FX_SUCCESS;
} }
static fx_value iterator_get_value(const fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_array_iterator_p *it = fx_object_get_private( struct fx_array_iterator_p *it = fx_object_get_private(
obj, obj,
@@ -445,10 +443,10 @@ static fx_value iterator_get_value(const fx_iterator *obj)
struct fx_array_p *array = it->_a_p; struct fx_array_p *array = it->_a_p;
if (it->i >= array->ar_len) { if (it->i >= array->ar_len) {
return FX_VALUE_EMPTY; return NULL;
} }
return *it->value; return it->value;
} }
static enum fx_status iterator_is_valid(const fx_iterator *obj) static enum fx_status iterator_is_valid(const fx_iterator *obj)
+6 -5
View File
@@ -32,8 +32,9 @@ struct fx_hashmap_p {
struct fx_hashmap_iterator_p { struct fx_hashmap_iterator_p {
size_t i; size_t i;
fx_hashmap_item item; fx_hashmap_item item;
fx_value item_value;
fx_hashmap *_h; const fx_hashmap *_h;
struct fx_hashmap_p *_h_p; struct fx_hashmap_p *_h_p;
fx_bst_node *_cbn; fx_bst_node *_cbn;
fx_queue_entry *_cqe; fx_queue_entry *_cqe;
@@ -420,7 +421,7 @@ bool fx_hashmap_is_empty(const fx_hashmap *hashmap)
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_HASHMAP, hashmap_is_empty, hashmap); FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_HASHMAP, hashmap_is_empty, hashmap);
} }
fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap) const fx_iterator *fx_hashmap_begin(const fx_hashmap *hashmap)
{ {
fx_hashmap_iterator *it_obj = fx_object_create( fx_hashmap_iterator *it_obj = fx_object_create(
FX_TYPE_HASHMAP_ITERATOR); FX_TYPE_HASHMAP_ITERATOR);
@@ -617,12 +618,13 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_SUCCESS; return FX_SUCCESS;
} }
static fx_value iterator_get_value(const fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_hashmap_iterator_p *it = fx_object_get_private( struct fx_hashmap_iterator_p *it = fx_object_get_private(
obj, obj,
FX_TYPE_HASHMAP_ITERATOR); FX_TYPE_HASHMAP_ITERATOR);
return FX_POINTER(&it->item); it->item_value = FX_POINTER(&it->item);
return &it->item_value;
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
@@ -635,7 +637,6 @@ FX_TYPE_CLASS_BEGIN(fx_hashmap)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = fx_hashmap_begin; FX_INTERFACE_ENTRY(it_begin) = fx_hashmap_begin;
FX_INTERFACE_ENTRY(it_cbegin) = fx_hashmap_cbegin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_hashmap) FX_TYPE_CLASS_END(fx_hashmap)
+111 -56
View File
@@ -1,3 +1,4 @@
#include <assert.h>
#include <fx/collections/hashtable.h> #include <fx/collections/hashtable.h>
#include <fx/misc.h> #include <fx/misc.h>
#include <fx/reflection/property.h> #include <fx/reflection/property.h>
@@ -47,8 +48,9 @@ struct fx_hashtable_p {
struct fx_hashtable_iterator_p { struct fx_hashtable_iterator_p {
size_t i; size_t i;
fx_hashtable_item *item; fx_hashtable_item *item;
fx_value item_value;
fx_hashtable *_h; const fx_hashtable *_h;
struct fx_hashtable_p *_h_p; struct fx_hashtable_p *_h_p;
}; };
@@ -67,12 +69,14 @@ static fx_status convert_key_to_index(
} }
size_t index = hash % table_size, i = 1; size_t index = hash % table_size, i = 1;
while (table[index].i_item && i < table_size) { bool index_found = false;
while (table[index].i_item && i <= table_size) {
const struct fx_hashtable_item_p *item = fx_object_get_private( const struct fx_hashtable_item_p *item = fx_object_get_private(
table[index].i_item, table[index].i_item,
FX_TYPE_HASHTABLE_ITEM); FX_TYPE_HASHTABLE_ITEM);
int cmp = fx_value_compare(&item->i_key, v); int cmp = fx_value_compare(&item->i_key, v);
if (cmp == 0) { if (cmp == 0) {
index_found = true;
break; break;
} }
@@ -80,11 +84,16 @@ static fx_status convert_key_to_index(
i++; i++;
} }
if (!index_found) {
return FX_ERR_NO_ENTRY;
}
assert(index < table_size);
*out_index = index; *out_index = index;
return FX_SUCCESS; return FX_SUCCESS;
} }
static fx_status find_key( static fx_status allocate_index_for_key(
const fx_value *v, const fx_value *v,
struct table_item *table, struct table_item *table,
size_t table_size, size_t table_size,
@@ -96,15 +105,21 @@ static fx_status find_key(
return status; return status;
} }
bool found = false; size_t index = hash % table_size;
size_t index = hash % table_size, i = 1; bool ok = false;
while (table[index].i_item && i < table_size) {
for (size_t i = 1; i < table_size; i++) {
if (!table[index].i_item) {
ok = true;
break;
}
const struct fx_hashtable_item_p *item = fx_object_get_private( const struct fx_hashtable_item_p *item = fx_object_get_private(
table[index].i_item, table[index].i_item,
FX_TYPE_HASHTABLE_ITEM); FX_TYPE_HASHTABLE_ITEM);
int cmp = fx_value_compare(&item->i_key, v); int cmp = fx_value_compare(&item->i_key, v);
if (cmp == 0) { if (cmp == 0) {
found = true; ok = true;
break; break;
} }
@@ -112,10 +127,11 @@ static fx_status find_key(
i++; i++;
} }
if (!found) { if (!ok) {
return FX_ERR_NO_ENTRY; return FX_ERR_NO_SPACE;
} }
assert(index < table_size);
*out_index = index; *out_index = index;
return FX_SUCCESS; return FX_SUCCESS;
} }
@@ -126,10 +142,9 @@ static fx_status add_item_to_table(
size_t table_capacity) size_t table_capacity)
{ {
size_t index = 0; size_t index = 0;
struct fx_hashtable_item_p *item_p = fx_object_get_private( struct fx_hashtable_item_p *item_p
item, = fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
FX_TYPE_HASHTABLE_ITEM); fx_status status = allocate_index_for_key(
fx_status status = convert_key_to_index(
&item_p->i_key, &item_p->i_key,
table, table,
table_capacity, table_capacity,
@@ -231,26 +246,43 @@ static fx_status hashtable_put(
if (!FX_OK(status)) { if (!FX_OK(status)) {
return status; return status;
} }
capacity = primes[hashtable->t_max_index];
} }
size_t index = 0; size_t index = 0;
status = convert_key_to_index( do {
status = allocate_index_for_key(
key, key,
hashtable->t_items, hashtable->t_items,
capacity, capacity,
&index); &index);
if (status == FX_ERR_NO_SPACE) {
status = resize_table(
hashtable,
hashtable->t_max_index + 1);
if (!FX_OK(status)) { if (!FX_OK(status)) {
return status; return status;
} }
capacity = primes[hashtable->t_max_index];
continue;
}
if (!FX_OK(status)) {
return status;
}
break;
} while (1);
fx_hashtable_item *item = fx_object_create(FX_TYPE_HASHTABLE_ITEM); fx_hashtable_item *item = fx_object_create(FX_TYPE_HASHTABLE_ITEM);
if (!item) { if (!item) {
return FX_ERR_NO_MEMORY; return FX_ERR_NO_MEMORY;
} }
struct fx_hashtable_item_p *item_p = fx_object_get_private( struct fx_hashtable_item_p *item_p
item, = fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
FX_TYPE_HASHTABLE_ITEM);
fx_value_copy(&item_p->i_key, key); fx_value_copy(&item_p->i_key, key);
fx_value_copy(&item_p->i_value, value); fx_value_copy(&item_p->i_value, value);
@@ -279,9 +311,8 @@ static const fx_value *hashtable_get(
return NULL; return NULL;
} }
const struct fx_hashtable_item_p *item_p = fx_object_get_private( const struct fx_hashtable_item_p *item_p
item, = fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
FX_TYPE_HASHTABLE_ITEM);
return &item_p->i_value; return &item_p->i_value;
} }
@@ -295,14 +326,16 @@ static bool hashtable_is_empty(const struct fx_hashtable_p *hashtable)
return hashtable->t_count == 0; return hashtable->t_count == 0;
} }
static fx_value hashtable_item_get_key(const struct fx_hashtable_item_p *item) static const fx_value *hashtable_item_get_key(
const struct fx_hashtable_item_p *item)
{ {
return item->i_key; return &item->i_key;
} }
static fx_value hashtable_item_get_value(const struct fx_hashtable_item_p *item) static const fx_value *hashtable_item_get_value(
const struct fx_hashtable_item_p *item)
{ {
return item->i_value; return &item->i_value;
} }
/*** PUBLIC FUNCTIONS *********************************************************/ /*** PUBLIC FUNCTIONS *********************************************************/
@@ -347,7 +380,7 @@ bool fx_hashtable_is_empty(const fx_hashtable *hashtable)
hashtable); hashtable);
} }
fx_value fx_hashtable_item_get_key(const fx_hashtable_item *item) const fx_value *fx_hashtable_item_get_key(const fx_hashtable_item *item)
{ {
FX_CLASS_DISPATCH_STATIC_0( FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_HASHTABLE_ITEM, FX_TYPE_HASHTABLE_ITEM,
@@ -355,7 +388,7 @@ fx_value fx_hashtable_item_get_key(const fx_hashtable_item *item)
item); item);
} }
fx_value fx_hashtable_item_get_value(const fx_hashtable_item *item) const fx_value *fx_hashtable_item_get_value(const fx_hashtable_item *item)
{ {
FX_CLASS_DISPATCH_STATIC_0( FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_HASHTABLE_ITEM, FX_TYPE_HASHTABLE_ITEM,
@@ -363,13 +396,12 @@ fx_value fx_hashtable_item_get_value(const fx_hashtable_item *item)
item); item);
} }
fx_iterator *fx_hashtable_begin(fx_hashtable *hashtable) const fx_iterator *fx_hashtable_begin(const fx_hashtable *hashtable)
{ {
fx_hashtable_iterator *it_obj = fx_object_create( fx_hashtable_iterator *it_obj
FX_TYPE_HASHTABLE_ITERATOR); = fx_object_create(FX_TYPE_HASHTABLE_ITERATOR);
struct fx_hashtable_iterator_p *it = fx_object_get_private( struct fx_hashtable_iterator_p *it
it_obj, = fx_object_get_private(it_obj, FX_TYPE_HASHTABLE_ITERATOR);
FX_TYPE_HASHTABLE_ITERATOR);
it->_h = hashtable; it->_h = hashtable;
it->_h_p = fx_object_get_private(hashtable, FX_TYPE_HASHTABLE); it->_h_p = fx_object_get_private(hashtable, FX_TYPE_HASHTABLE);
@@ -387,6 +419,7 @@ fx_iterator *fx_hashtable_begin(fx_hashtable *hashtable)
it->i++; it->i++;
} }
it->item_value = FX_VALUE_OBJECT_REF(table[it->i].i_item);
return it_obj; return it_obj;
} }
@@ -405,6 +438,30 @@ static void hashtable_init(fx_object *obj, void *priv)
static void hashtable_fini(fx_object *obj, void *priv) static void hashtable_fini(fx_object *obj, void *priv)
{ {
struct fx_hashtable_p *map = priv; struct fx_hashtable_p *map = priv;
size_t capacity = primes[map->t_max_index];
size_t nr_destroyed = 0;
for (size_t i = 0; i < capacity; i++) {
if (map->t_items[i].i_item) {
nr_destroyed++;
fx_hashtable_item_unref(map->t_items[i].i_item);
}
}
free(map->t_items);
}
static void hashtable_item_fini(fx_object *obj, void *priv)
{
struct fx_hashtable_item_p *item = priv;
fx_value_unset(&item->i_key);
fx_value_unset(&item->i_value);
}
static void hashtable_iterator_fini(fx_object *obj, void *priv)
{
struct fx_hashtable_iterator_p *it = priv;
fx_value_unset(&it->item_value);
} }
static fx_status to_string( static fx_status to_string(
@@ -412,9 +469,8 @@ static fx_status to_string(
fx_stream *out, fx_stream *out,
const char *format) const char *format)
{ {
struct fx_hashtable_p *hashtable = fx_object_get_private( struct fx_hashtable_p *hashtable
obj->v_object, = fx_object_get_private(obj->v_object, FX_TYPE_HASHTABLE);
FX_TYPE_HASHTABLE);
if (!hashtable->t_count) { if (!hashtable->t_count) {
fx_stream_write_cstr(out, "{}", NULL); fx_stream_write_cstr(out, "{}", NULL);
@@ -434,9 +490,8 @@ static fx_status to_string(
} }
nr_written++; nr_written++;
struct fx_hashtable_item_p *item_p = fx_object_get_private( struct fx_hashtable_item_p *item_p
item, = fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
FX_TYPE_HASHTABLE_ITEM);
const char *cstr = NULL; const char *cstr = NULL;
fx_value_get_cstr(&item_p->i_key, &cstr); fx_value_get_cstr(&item_p->i_key, &cstr);
@@ -487,9 +542,8 @@ static fx_status get_key(
return FX_ERR_NOT_SUPPORTED; return FX_ERR_NOT_SUPPORTED;
} }
struct fx_hashtable_item_p *item_p = fx_object_get_private( struct fx_hashtable_item_p *item_p
item, = fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
FX_TYPE_HASHTABLE_ITEM);
fx_value_copy(out, &item_p->i_key); fx_value_copy(out, &item_p->i_key);
return FX_SUCCESS; return FX_SUCCESS;
@@ -506,9 +560,8 @@ static fx_status get_value(
return FX_ERR_NOT_SUPPORTED; return FX_ERR_NOT_SUPPORTED;
} }
struct fx_hashtable_item_p *item_p = fx_object_get_private( struct fx_hashtable_item_p *item_p
item, = fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
FX_TYPE_HASHTABLE_ITEM);
fx_value_copy(out, &item_p->i_value); fx_value_copy(out, &item_p->i_value);
return FX_SUCCESS; return FX_SUCCESS;
@@ -518,9 +571,10 @@ static fx_status get_value(
static enum fx_status iterator_move_next(const fx_iterator *obj) static enum fx_status iterator_move_next(const fx_iterator *obj)
{ {
struct fx_hashtable_iterator_p *it = fx_object_get_private( struct fx_hashtable_iterator_p *it
obj, = fx_object_get_private(obj, FX_TYPE_HASHTABLE_ITERATOR);
FX_TYPE_HASHTABLE_ITERATOR);
fx_value_unset(&it->item_value);
struct table_item *table = it->_h_p->t_items; struct table_item *table = it->_h_p->t_items;
size_t capacity = primes[it->_h_p->t_max_index]; size_t capacity = primes[it->_h_p->t_max_index];
@@ -533,14 +587,14 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
return FX_ERR_NO_DATA; return FX_ERR_NO_DATA;
} }
it->item_value = FX_VALUE_OBJECT_REF(table[it->i].i_item);
return FX_SUCCESS; return FX_SUCCESS;
} }
static enum fx_status iterator_erase(fx_iterator *obj) static enum fx_status iterator_erase(fx_iterator *obj)
{ {
struct fx_hashtable_iterator_p *it = fx_object_get_private( struct fx_hashtable_iterator_p *it
obj, = fx_object_get_private(obj, FX_TYPE_HASHTABLE_ITERATOR);
FX_TYPE_HASHTABLE_ITERATOR);
struct table_item *table = it->_h_p->t_items; struct table_item *table = it->_h_p->t_items;
size_t capacity = primes[it->_h_p->t_max_index]; size_t capacity = primes[it->_h_p->t_max_index];
@@ -562,19 +616,18 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_SUCCESS; return FX_SUCCESS;
} }
static fx_value iterator_get_value(const fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_hashtable_iterator_p *it = fx_object_get_private( struct fx_hashtable_iterator_p *it
obj, = fx_object_get_private(obj, FX_TYPE_HASHTABLE_ITERATOR);
FX_TYPE_HASHTABLE_ITERATOR);
struct table_item *table = it->_h_p->t_items; struct table_item *table = it->_h_p->t_items;
size_t capacity = primes[it->_h_p->t_max_index]; size_t capacity = primes[it->_h_p->t_max_index];
if (table[it->i].i_item) { if (table[it->i].i_item) {
return FX_VALUE_OBJECT(table[it->i].i_item); return &it->item_value;
} }
return FX_VALUE_EMPTY; return NULL;
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
@@ -619,6 +672,7 @@ FX_TYPE_DEFINITION_BEGIN(fx_hashtable_iterator)
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR); FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_hashtable_iterator_class); FX_TYPE_CLASS(fx_hashtable_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_iterator_p); FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_iterator_p);
FX_TYPE_INSTANCE_FINI(hashtable_iterator_fini);
FX_TYPE_DEFINITION_END(fx_hashtable_iterator) FX_TYPE_DEFINITION_END(fx_hashtable_iterator)
// ---- fx_hashtable_item DEFINITION // ---- fx_hashtable_item DEFINITION
@@ -637,4 +691,5 @@ FX_TYPE_DEFINITION_BEGIN(fx_hashtable_item)
FX_TYPE_EXTENDS(FX_TYPE_OBJECT); FX_TYPE_EXTENDS(FX_TYPE_OBJECT);
FX_TYPE_CLASS(fx_hashtable_item_class); FX_TYPE_CLASS(fx_hashtable_item_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_item_p); FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_item_p);
FX_TYPE_INSTANCE_FINI(hashtable_item_fini);
FX_TYPE_DEFINITION_END(fx_hashtable_item) FX_TYPE_DEFINITION_END(fx_hashtable_item)
@@ -83,7 +83,7 @@ FX_API bool fx_hashmap_has_key(
FX_API size_t fx_hashmap_get_size(const fx_hashmap *hashmap); FX_API size_t fx_hashmap_get_size(const fx_hashmap *hashmap);
FX_API bool fx_hashmap_is_empty(const fx_hashmap *hashmap); FX_API bool fx_hashmap_is_empty(const fx_hashmap *hashmap);
FX_API fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap); FX_API const fx_iterator *fx_hashmap_begin(const fx_hashmap *hashmap);
FX_API const fx_iterator *fx_hashmap_cbegin(const fx_hashmap *hashmap); FX_API const fx_iterator *fx_hashmap_cbegin(const fx_hashmap *hashmap);
FX_DECLS_END; FX_DECLS_END;
@@ -45,10 +45,11 @@ FX_API const fx_value *fx_hashtable_get(
FX_API size_t fx_hashtable_get_count(const fx_hashtable *hashtable); FX_API size_t fx_hashtable_get_count(const fx_hashtable *hashtable);
FX_API bool fx_hashtable_is_empty(const fx_hashtable *hashtable); FX_API bool fx_hashtable_is_empty(const fx_hashtable *hashtable);
FX_API fx_iterator *fx_hashtable_begin(fx_hashtable *hashtable); FX_API const fx_iterator *fx_hashtable_begin(const fx_hashtable *hashtable);
FX_API fx_value fx_hashtable_item_get_key(const fx_hashtable_item *item); FX_API const fx_value *fx_hashtable_item_get_key(const fx_hashtable_item *item);
FX_API fx_value fx_hashtable_item_get_value(const fx_hashtable_item *item); FX_API const fx_value *fx_hashtable_item_get_value(
const fx_hashtable_item *item);
FX_DECLS_END; FX_DECLS_END;
+1 -2
View File
@@ -59,8 +59,7 @@ FX_API void fx_list_delete_all(fx_list *q);
FX_API void *fx_list_entry_value(const fx_list_entry *entry); FX_API void *fx_list_entry_value(const fx_list_entry *entry);
FX_API fx_iterator *fx_list_begin(fx_list *q); FX_API const fx_iterator *fx_list_begin(const fx_list *q);
FX_API const fx_iterator *fx_list_cbegin(const fx_list *q);
FX_DECLS_END; FX_DECLS_END;
+5 -24
View File
@@ -23,6 +23,7 @@ struct fx_list_iterator_p {
size_t i; size_t i;
void *item; void *item;
fx_value item_value;
fx_list_entry *entry; fx_list_entry *entry;
}; };
@@ -401,27 +402,7 @@ void *fx_list_entry_value(const struct fx_list_entry *entry)
return entry ? entry->e_data : NULL; return entry ? entry->e_data : NULL;
} }
fx_iterator *fx_list_begin(fx_list *q) const fx_iterator *fx_list_begin(const fx_list *q)
{
fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it = fx_object_get_private(
it_obj,
FX_TYPE_LIST_ITERATOR);
it->_q = q;
it->_q_p = fx_object_get_private(q, FX_TYPE_LIST);
it->_q_entry = fx_queue_first(&it->_q_p->l_queue);
it->i = 0;
it->entry = fx_unbox(struct fx_list_entry, it->_q_entry, e_entry);
if (it->entry) {
it->item = it->entry->e_data;
}
return 0;
}
const fx_iterator *fx_list_cbegin(const fx_list *q)
{ {
fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR); fx_list_iterator *it_obj = fx_object_create(FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it = fx_object_get_private( struct fx_list_iterator_p *it = fx_object_get_private(
@@ -508,13 +489,14 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_SUCCESS; return FX_SUCCESS;
} }
static fx_value iterator_get_value(const fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_list_iterator_p *it = fx_object_get_private( struct fx_list_iterator_p *it = fx_object_get_private(
obj, obj,
FX_TYPE_LIST_ITERATOR); FX_TYPE_LIST_ITERATOR);
return FX_POINTER(it->item); it->item_value = FX_POINTER(it->item);
return &it->item_value;
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
@@ -527,7 +509,6 @@ FX_TYPE_CLASS_BEGIN(fx_list)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = fx_list_begin; FX_INTERFACE_ENTRY(it_begin) = fx_list_begin;
FX_INTERFACE_ENTRY(it_cbegin) = fx_list_cbegin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_list) FX_TYPE_CLASS_END(fx_list)
+5 -5
View File
@@ -19,13 +19,13 @@ int main(void)
printf("no value\n"); printf("no value\n");
} }
fx_iterator *it = fx_hashtable_begin(ht); const fx_iterator *it = fx_hashtable_begin(ht);
fx_foreach(val, it) fx_foreach(val, it)
{ {
fx_hashtable_item *item = NULL; fx_hashtable_item *item = NULL;
fx_value_get_object(&val, &item); fx_value_get_object(val, &item);
printf("item %p\n", item); printf("item %p\n", item);
fx_value key, value; const fx_value *key, *value;
key = fx_hashtable_item_get_key(item); key = fx_hashtable_item_get_key(item);
value = fx_hashtable_item_get_value(item); value = fx_hashtable_item_get_value(item);
@@ -34,10 +34,10 @@ int main(void)
fx_foreach(prop_val, prop_it) fx_foreach(prop_val, prop_it)
{ {
fx_property *prop; fx_property *prop;
fx_value_get_object(&prop_val, &prop); fx_value_get_object(prop_val, &prop);
printf("%s = ", fx_property_get_name(prop)); printf("%s = ", fx_property_get_name(prop));
fx_value value = FX_VALUE_EMPTY; fx_value value = FX_VALUE_EMPTY;
fx_property_get_value(prop, &val, &value); fx_property_get_value(prop, val, &value);
fx_value_to_string(&value, fx_stdout, NULL); fx_value_to_string(&value, fx_stdout, NULL);
printf("\n"); printf("\n");
} }
+6 -11
View File
@@ -35,6 +35,7 @@ struct fx_directory_iterator_p {
fx_directory *root; fx_directory *root;
fx_directory_entry entry; fx_directory_entry entry;
fx_value entry_value;
}; };
/*** PRIVATE FUNCTIONS ********************************************************/ /*** PRIVATE FUNCTIONS ********************************************************/
@@ -375,8 +376,6 @@ fx_result fx_directory_open_temp(fx_directory **out)
fx_path_unlink(rpath); fx_path_unlink(rpath);
fx_path_unref(rpath); fx_path_unref(rpath);
return status;
} }
} }
@@ -512,6 +511,8 @@ static void update_iterator_data(struct fx_directory_iterator_p *it)
fx_path *path = fx_path_create_from_cstr( fx_path *path = fx_path_create_from_cstr(
ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1); ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1);
fx_value_unset(&it->entry_value);
it->entry_value = FX_POINTER(&it->entry);
it->entry.filename = ent->fts_name; it->entry.filename = ent->fts_name;
it->entry.filepath = path; it->entry.filepath = path;
@@ -619,12 +620,7 @@ fx_iterator *fx_directory_begin(
return it_obj; return it_obj;
} }
static fx_iterator *iterator_begin(fx_object *obj) static const fx_iterator *iterator_begin(const fx_object *obj)
{
return fx_directory_begin(obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
}
static const fx_iterator *iterator_cbegin(const fx_object *obj)
{ {
return fx_directory_begin( return fx_directory_begin(
(fx_object *)obj, (fx_object *)obj,
@@ -698,13 +694,13 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return iterator_move_next(obj); return iterator_move_next(obj);
} }
static fx_value iterator_get_value(const fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_directory_iterator_p *it = fx_object_get_private( struct fx_directory_iterator_p *it = fx_object_get_private(
obj, obj,
FX_TYPE_DIRECTORY_ITERATOR); FX_TYPE_DIRECTORY_ITERATOR);
return FX_POINTER(&it->entry); return &it->entry_value;
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
@@ -717,7 +713,6 @@ FX_TYPE_CLASS_BEGIN(fx_directory)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = iterator_begin; FX_INTERFACE_ENTRY(it_begin) = iterator_begin;
FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_directory) FX_TYPE_CLASS_END(fx_directory)
+6 -18
View File
@@ -37,6 +37,7 @@ struct fx_directory_iterator_p {
fx_directory *root; fx_directory *root;
fx_directory_entry entry; fx_directory_entry entry;
fx_value entry_value;
}; };
/*** PRIVATE FUNCTIONS ********************************************************/ /*** PRIVATE FUNCTIONS ********************************************************/
@@ -510,6 +511,8 @@ static void update_iterator_data(struct fx_directory_iterator_p *it)
fx_path *path = fx_path_create_from_cstr( fx_path *path = fx_path_create_from_cstr(
ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1); ent->fts_path + fx_path_length(it->_p->d_path_abs) + 1);
fx_value_unset(&it->entry_value);
it->entry_value = FX_POINTER(&it->entry);
it->entry.filename = ent->fts_name; it->entry.filename = ent->fts_name;
it->entry.filepath = path; it->entry.filepath = path;
@@ -616,12 +619,7 @@ fx_iterator *fx_directory_begin(
return it_obj; return it_obj;
} }
static fx_iterator *iterator_begin(fx_object *obj) static const fx_iterator *iterator_begin(const fx_object *obj)
{
return fx_directory_begin(obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
}
static const fx_iterator *iterator_cbegin(const fx_object *obj)
{ {
return fx_directory_begin( return fx_directory_begin(
(fx_object *)obj, (fx_object *)obj,
@@ -692,20 +690,12 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return iterator_move_next(obj); return iterator_move_next(obj);
} }
static fx_iterator_value iterator_get_value(fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_directory_iterator_p *it struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR); = fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
return FX_ITERATOR_VALUE_PTR(&it->entry); return &it->entry_value;
}
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
{
struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
return FX_ITERATOR_VALUE_CPTR(&it->entry);
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
@@ -718,7 +708,6 @@ FX_TYPE_CLASS_BEGIN(fx_directory)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = iterator_begin; FX_INTERFACE_ENTRY(it_begin) = iterator_begin;
FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_directory) FX_TYPE_CLASS_END(fx_directory)
@@ -741,7 +730,6 @@ FX_TYPE_CLASS_BEGIN(fx_directory_iterator)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next; FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = iterator_erase; FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value; FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR) FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_directory_iterator) FX_TYPE_CLASS_END(fx_directory_iterator)
+10 -3
View File
@@ -392,11 +392,18 @@ void path_fini(fx_object *obj, void *priv)
fx_string_unref(path->p_pathstr); fx_string_unref(path->p_pathstr);
} }
void path_to_string(const fx_object *obj, fx_stream *out) static fx_status path_to_string(
const fx_value *obj,
fx_stream *out,
const char *format)
{ {
struct fx_path_p *path = fx_object_get_private(obj, FX_TYPE_PATH); struct fx_path_p *path
= fx_object_get_private(obj->v_object, FX_TYPE_PATH);
fx_stream_write_cstr(out, fx_string_get_cstr(path->p_pathstr), NULL); return fx_stream_write_cstr(
out,
fx_string_get_cstr(path->p_pathstr),
NULL);
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
+1 -1
View File
@@ -26,7 +26,7 @@ int main(int argc, const char **argv)
fx_foreach(val, it) fx_foreach(val, it)
{ {
fx_directory_entry *entry = NULL; fx_directory_entry *entry = NULL;
fx_value_get_pointer(&val, (void **)&entry); fx_value_get_pointer(val, (void **)&entry);
printf("%s\n", fx_path_ptr(entry->filepath)); printf("%s\n", fx_path_ptr(entry->filepath));
} }
+64 -48
View File
@@ -71,10 +71,12 @@ struct fx_assembly_p {
struct fx_assembly_iterator_p { struct fx_assembly_iterator_p {
fx_namemap_entry *it_cur; fx_namemap_entry *it_cur;
fx_value it_value;
}; };
struct fx_assembly_type_iterator_p { struct fx_assembly_type_iterator_p {
struct type *it_item; struct type *it_item;
fx_value it_value;
}; };
/*** PRIVATE FUNCTIONS ********************************************************/ /*** PRIVATE FUNCTIONS ********************************************************/
@@ -165,11 +167,11 @@ static struct map_item *map_item_next(struct map_item *item)
struct map_bucket *bucket = item->i_entry.e_bucket; struct map_bucket *bucket = item->i_entry.e_bucket;
struct map_entry *next_item = NULL; struct map_entry *next_item = NULL;
if (bucket) { if (bucket) {
struct fx_queue_entry *q_item = fx_queue_next( struct fx_queue_entry *q_item
&item->i_entry.e_entry); = fx_queue_next(&item->i_entry.e_entry);
if (!q_item) { if (!q_item) {
struct fx_bst_node *node = fx_bst_next( struct fx_bst_node *node
&bucket->b_entry.e_node); = fx_bst_next(&bucket->b_entry.e_node);
next_item = fx_unbox(struct map_entry, node, e_node); next_item = fx_unbox(struct map_entry, node, e_node);
} else { } else {
next_item = fx_unbox(struct map_entry, q_item, e_entry); next_item = fx_unbox(struct map_entry, q_item, e_entry);
@@ -185,8 +187,8 @@ static struct map_item *map_item_next(struct map_item *item)
if (next_item->e_type == MAP_ENTRY_BUCKET) { if (next_item->e_type == MAP_ENTRY_BUCKET) {
bucket = (struct map_bucket *)next_item; bucket = (struct map_bucket *)next_item;
struct fx_queue_entry *q_item = fx_queue_first( struct fx_queue_entry *q_item
&bucket->b_items); = fx_queue_first(&bucket->b_items);
next_item = fx_unbox(struct map_entry, q_item, e_entry); next_item = fx_unbox(struct map_entry, q_item, e_entry);
} }
@@ -255,10 +257,8 @@ static void assembly_dump(struct fx_assembly_p *assembly)
fx_bst_node *cur_node = fx_bst_first(&assembly->a_types.m_entries); fx_bst_node *cur_node = fx_bst_first(&assembly->a_types.m_entries);
while (cur_node) { while (cur_node) {
struct map_entry *entry = fx_unbox( struct map_entry *entry
struct map_entry, = fx_unbox(struct map_entry, cur_node, e_node);
cur_node,
e_node);
switch (entry->e_type) { switch (entry->e_type) {
case MAP_ENTRY_ITEM: { case MAP_ENTRY_ITEM: {
struct map_item *item = (struct map_item *)entry; struct map_item *item = (struct map_item *)entry;
@@ -268,8 +268,8 @@ static void assembly_dump(struct fx_assembly_p *assembly)
case MAP_ENTRY_BUCKET: { case MAP_ENTRY_BUCKET: {
struct map_bucket *bucket = (struct map_bucket *)entry; struct map_bucket *bucket = (struct map_bucket *)entry;
fx_queue_entry *cur_qentry = fx_queue_first( fx_queue_entry *cur_qentry
&bucket->b_items); = fx_queue_first(&bucket->b_items);
while (cur_qentry) { while (cur_qentry) {
struct map_item *item = fx_unbox( struct map_item *item = fx_unbox(
struct map_item, struct map_item,
@@ -289,8 +289,8 @@ static void assembly_dump(struct fx_assembly_p *assembly)
static fx_iterator *assembly_get_types(const struct fx_assembly_p *assembly) static fx_iterator *assembly_get_types(const struct fx_assembly_p *assembly)
{ {
fx_assembly_type_iterator *it = fx_object_create( fx_assembly_type_iterator *it
fx_assembly_type_iterator_get_type()); = fx_object_create(fx_assembly_type_iterator_get_type());
if (!it) { if (!it) {
return NULL; return NULL;
} }
@@ -299,12 +299,15 @@ static fx_iterator *assembly_get_types(const struct fx_assembly_p *assembly)
it, it,
fx_assembly_type_iterator_get_type()); fx_assembly_type_iterator_get_type());
struct map_item *item = map_first_item(&assembly->a_types); struct map_item *item = map_first_item(&assembly->a_types);
if (item) { if (!item) {
it_p->it_item = fx_unbox(struct type, item, e_map_item);
} else {
fx_iterator_set_status(it, FX_ERR_NO_DATA); fx_iterator_set_status(it, FX_ERR_NO_DATA);
return it;
} }
it_p->it_item = fx_unbox(struct type, item, e_map_item);
const fx_type *ty = fx_type_get_by_id(it_p->it_item->e_type);
it_p->it_value = FX_VALUE_OBJECT_REF(ty);
return it; return it;
} }
@@ -341,20 +344,25 @@ static void assembly_get_version(
fx_iterator *fx_assembly_get_all(void) fx_iterator *fx_assembly_get_all(void)
{ {
fx_assembly_iterator *it = fx_object_create( fx_assembly_iterator *it
fx_assembly_iterator_get_type()); = fx_object_create(fx_assembly_iterator_get_type());
if (!it) { if (!it) {
return NULL; return NULL;
} }
struct fx_assembly_iterator_p *it_p = fx_object_get_private( struct fx_assembly_iterator_p *it_p
it, = fx_object_get_private(it, fx_assembly_iterator_get_type());
fx_assembly_iterator_get_type());
it_p->it_cur = fx_namemap_first(&assembly_map); it_p->it_cur = fx_namemap_first(&assembly_map);
if (!it_p->it_cur) { if (!it_p->it_cur) {
fx_iterator_set_status(it, FX_ERR_NO_DATA); fx_iterator_set_status(it, FX_ERR_NO_DATA);
return it;
} }
struct fx_assembly_p *assembly
= fx_unbox(struct fx_assembly_p, it_p->it_cur, a_entry);
it_p->it_value = FX_VALUE_OBJECT_REF(assembly->a_self);
return it; return it;
} }
@@ -365,10 +373,8 @@ const fx_assembly *fx_assembly_get_by_name(const char *name)
return NULL; return NULL;
} }
struct fx_assembly_p *assembly = fx_unbox( struct fx_assembly_p *assembly
struct fx_assembly_p, = fx_unbox(struct fx_assembly_p, entry, a_entry);
entry,
a_entry);
return assembly->a_self; return assembly->a_self;
} }
@@ -483,51 +489,61 @@ static enum fx_status type_iterator_move_next(const fx_iterator *obj)
return FX_ERR_NO_DATA; return FX_ERR_NO_DATA;
} }
fx_value_unset(&it->it_value);
struct map_item *next = map_item_next(&it->it_item->e_map_item); struct map_item *next = map_item_next(&it->it_item->e_map_item);
it->it_item = fx_unbox(struct type, next, e_map_item); it->it_item = fx_unbox(struct type, next, e_map_item);
return it->it_item ? FX_SUCCESS : FX_ERR_NO_DATA; if (!it->it_item) {
return FX_ERR_NO_DATA;
} }
static fx_value type_iterator_get_value(const fx_iterator *obj) const fx_type *ty = fx_type_get_by_id(it->it_item->e_type);
it->it_value = FX_VALUE_OBJECT_REF(ty);
return FX_SUCCESS;
}
static const fx_value *type_iterator_get_value(const fx_iterator *obj)
{ {
struct fx_assembly_type_iterator_p *it = fx_object_get_private( struct fx_assembly_type_iterator_p *it = fx_object_get_private(
obj, obj,
fx_assembly_type_iterator_get_type()); fx_assembly_type_iterator_get_type());
if (!it->it_item) { if (!it->it_item) {
return FX_VALUE_EMPTY; return NULL;
} }
const fx_type *ty = fx_type_get_by_id(it->it_item->e_type); return &it->it_value;
return FX_VALUE_OBJECT(ty);
} }
static enum fx_status assembly_iterator_move_next(const fx_iterator *obj) static enum fx_status assembly_iterator_move_next(const fx_iterator *obj)
{ {
struct fx_assembly_iterator_p *it = fx_object_get_private( struct fx_assembly_iterator_p *it
obj, = fx_object_get_private(obj, fx_assembly_iterator_get_type());
fx_assembly_iterator_get_type());
if (!it->it_cur) { if (!it->it_cur) {
return FX_ERR_NO_DATA; return FX_ERR_NO_DATA;
} }
fx_value_unset(&it->it_value);
it->it_cur = fx_namemap_next(&assembly_map, it->it_cur); it->it_cur = fx_namemap_next(&assembly_map, it->it_cur);
return it->it_cur ? FX_SUCCESS : FX_ERR_NO_DATA;
}
static fx_value assembly_iterator_get_value(const fx_iterator *obj)
{
struct fx_assembly_iterator_p *it = fx_object_get_private(
obj,
fx_assembly_iterator_get_type());
if (!it->it_cur) { if (!it->it_cur) {
return FX_VALUE_EMPTY; return FX_ERR_NO_DATA;
} }
struct fx_assembly_p *assembly = fx_unbox( struct fx_assembly_p *assembly
struct fx_assembly_p, = fx_unbox(struct fx_assembly_p, it->it_cur, a_entry);
it->it_cur,
a_entry); it->it_value = FX_VALUE_OBJECT_REF(assembly->a_self);
return FX_VALUE_OBJECT(assembly->a_self); return FX_SUCCESS;
}
static const fx_value *assembly_iterator_get_value(const fx_iterator *obj)
{
struct fx_assembly_iterator_p *it
= fx_object_get_private(obj, fx_assembly_iterator_get_type());
if (!it->it_cur) {
return NULL;
}
return &it->it_value;
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
+142 -11
View File
@@ -1,3 +1,5 @@
#include <fx/type.h>
#include <fx/value-type.h>
#include <platform/callvm.h> #include <platform/callvm.h>
#include <stdlib.h> #include <stdlib.h>
@@ -77,13 +79,88 @@ static void push_double(struct callvm* vm, double value)
void callvm_push(struct callvm *vm, const fx_value *value) void callvm_push(struct callvm *vm, const fx_value *value)
{ {
switch (value->v_type.t_primitive) { if (!fx_type_is_value_type(value->v_type)) {
case FX_VALUE_TYPE_DOUBLE: push_int(vm, (uintptr_t)value->v_object);
return;
}
unsigned int value_type = __fx_type_get_value_type(value->v_type);
switch (value_type) {
case __FX_VALUE_TYPE_BOOL:
push_int(vm, value->v_bool);
break;
case __FX_VALUE_TYPE_I16:
push_int(vm, value->v_i16);
break;
case __FX_VALUE_TYPE_U16:
push_int(vm, value->v_u16);
break;
case __FX_VALUE_TYPE_I32:
push_int(vm, value->v_i32);
break;
case __FX_VALUE_TYPE_U32:
push_int(vm, value->v_u32);
break;
case __FX_VALUE_TYPE_I64:
push_int(vm, value->v_i64);
break;
case __FX_VALUE_TYPE_U64:
push_int(vm, value->v_u64);
break;
case __FX_VALUE_TYPE_IPTR:
push_int(vm, value->v_iptr);
break;
case __FX_VALUE_TYPE_UPTR:
push_int(vm, value->v_uptr);
break;
case __FX_VALUE_TYPE_SBYTE:
push_int(vm, value->v_sbyte);
break;
case __FX_VALUE_TYPE_BYTE:
push_int(vm, value->v_byte);
break;
case __FX_VALUE_TYPE_SHORT:
push_int(vm, value->v_short);
break;
case __FX_VALUE_TYPE_USHORT:
push_int(vm, value->v_ushort);
break;
case __FX_VALUE_TYPE_INT:
push_int(vm, value->v_int);
break;
case __FX_VALUE_TYPE_UINT:
push_int(vm, value->v_uint);
break;
case __FX_VALUE_TYPE_LONG:
push_int(vm, value->v_long);
break;
case __FX_VALUE_TYPE_ULONG:
push_int(vm, value->v_ulong);
break;
case __FX_VALUE_TYPE_LONGLONG:
push_int(vm, value->v_longlong);
break;
case __FX_VALUE_TYPE_ULONGLONG:
push_int(vm, value->v_ulonglong);
break;
case __FX_VALUE_TYPE_SIZE:
push_int(vm, value->v_size);
break;
case __FX_VALUE_TYPE_FLOAT:
push_double(vm, value->v_float);
break;
case __FX_VALUE_TYPE_DOUBLE:
push_double(vm, value->v_double); push_double(vm, value->v_double);
break; break;
default: case __FX_VALUE_TYPE_CSTR:
push_int(vm, (uintptr_t)value->v_cstr);
break;
case __FX_VALUE_TYPE_POINTER:
push_int(vm, (uintptr_t)value->v_pointer); push_int(vm, (uintptr_t)value->v_pointer);
break; break;
default:
break;
} }
} }
@@ -94,16 +171,70 @@ extern void callvm_invoke_v(fx_function_impl impl, struct callvm* vm);
fx_value callvm_invoke( fx_value callvm_invoke(
struct callvm *vm, struct callvm *vm,
fx_function_impl impl, fx_function_impl impl,
fx_value_type return_type) fx_type_id return_type)
{ {
switch (return_type) { if (!fx_type_is_value_type(return_type)) {
case FX_VALUE_TYPE_NONE: uintptr_t v = callvm_invoke_i(impl, vm);
callvm_invoke_v(impl, vm); fx_value result = {
break; .v_type = return_type,
case FX_VALUE_TYPE_DOUBLE: .v_object = (fx_object *)v,
return FX_VALUE_DOUBLE(callvm_invoke_d(impl, vm)); };
return result;
}
unsigned int value_type = __fx_type_get_value_type(return_type);
switch (value_type) {
case __FX_VALUE_TYPE_BOOL:
return FX_BOOL(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_I16:
return FX_I16(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_U16:
return FX_U16(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_I32:
return FX_I32(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_U32:
return FX_U32(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_I64:
return FX_I64(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_U64:
return FX_U64(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_IPTR:
return FX_IPTR(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_UPTR:
return FX_UPTR(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_SBYTE:
return FX_SBYTE(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_BYTE:
return FX_BYTE(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_SHORT:
return FX_SHORT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_USHORT:
return FX_USHORT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_INT:
return FX_INT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_UINT:
return FX_UINT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_LONG:
return FX_LONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_ULONG:
return FX_ULONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_LONGLONG:
return FX_LONGLONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_ULONGLONG:
return FX_ULONGLONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_SIZE:
return FX_SIZE(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_FLOAT:
return FX_FLOAT(callvm_invoke_d(impl, vm));
case __FX_VALUE_TYPE_DOUBLE:
return FX_DOUBLE(callvm_invoke_d(impl, vm));
case __FX_VALUE_TYPE_CSTR:
return FX_CSTR((const char *)callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_POINTER:
return FX_POINTER((void *)callvm_invoke_i(impl, vm));
default: default:
return FX_VALUE_INT(callvm_invoke_i(impl, vm)); return FX_VALUE_EMPTY;
} }
return FX_VALUE_EMPTY; return FX_VALUE_EMPTY;
@@ -32,6 +32,6 @@ extern void callvm_push(struct callvm *vm, const fx_value *value);
extern fx_value callvm_invoke( extern fx_value callvm_invoke(
struct callvm *vm, struct callvm *vm,
fx_function_impl impl, fx_function_impl impl,
fx_value_type return_type); fx_type_id return_type);
#endif #endif
+95
View File
@@ -0,0 +1,95 @@
.extern memcpy
.type memcpy, @function
.global callvm_invoke_i
.type callvm_invoke_i, @function
# %rdi = (function ptr) impl
# %rsi = (struct callvm) context
callvm_invoke_i:
# save the stack frame pointer
push %rbp
mov %rsp, %rbp
# store function pointer for later
push %r12
push %r13
push %r14
push %r15
# move our parameters out of the way
mov %rdi, %r11
mov %rsi, %r12
# calculate the amount of stack space needed for the varargs
movq 32(%r12), %r13
shl $3, %r13
# allocate the stack space
push %rsp
sub %r13, %rsp
andq $0xFFFFFFFFFFFFFFF0, %rsp # re-align the stack
# copy the excess args to the stack
mov %rsp, %rdi
mov 160(%r12), %rsi
mov %r13, %rdx
call memcpy
# Next, set up the fixed integer arguments
movq 48(%r12), %rdi # int arg 0
movq 56(%r12), %rsi # int arg 1
movq 64(%r12), %rdx # int arg 2
movq 72(%r12), %rcx # int arg 3
movq 80(%r12), %r8 # int arg 4
movq 88(%r12), %r9 # int arg 5
# Finally, set up the fixed double arguments
movq 96(%r12), %xmm0 # double arg 0
movq 104(%r12), %xmm1 # double arg 1
movq 112(%r12), %xmm2 # double arg 2
movq 120(%r12), %xmm3 # double arg 3
movq 128(%r12), %xmm4 # double arg 4
movq 136(%r12), %xmm5 # double arg 5
movq 144(%r12), %xmm6 # double arg 6
movq 152(%r12), %xmm7 # double arg 7
# set the number of vararg double parameters
# as required by the ABI
mov 168(%r12), %rax
# call the function implementation
call *%r11
# Restore the stack pointer (deallocating the varargs buffer)
mov -40(%rbp), %rsp
# Restore callee-saved registers
pop %r15
pop %r14
pop %r13
pop %r12
# restore the saved stack frame
pop %rbp
ret
.global callvm_invoke_d
.type callvm_invoke_d, @function
# %rdi = (function ptr) impl
# %rsi = (struct callvm *) context
callvm_invoke_d:
jmp callvm_invoke_i
.global callvm_invoke_v
.type callvm_invoke_v, @function
# %rdi = (function ptr) impl
# %rsi = (struct callvm *) context
callvm_invoke_v:
jmp callvm_invoke_i
+243
View File
@@ -0,0 +1,243 @@
#include <fx/type.h>
#include <fx/value-type.h>
#include <platform/callvm.h>
#include <stdlib.h>
#if 0
switch (arg->v_type.t_primitive) {
case FX_VALUE_TYPE_DOUBLE:
break;
default:
callvm_push_int(&vm, (uintptr_t)arg->v_pointer);
break;
}
#endif
void callvm_reset(struct callvm *vm, unsigned int max_fixed_args)
{
vm->vm_arg_int_count = 0;
vm->vm_arg_double_count = 0;
vm->vm_arg_fixed = max_fixed_args;
vm->vm_arg_excess_count = 0;
vm->vm_double_excess_count = 0;
}
static void expand_excess(struct callvm *vm)
{
size_t new_capacity = vm->vm_arg_excess_max * 2;
if (!new_capacity) {
new_capacity = 4;
}
void *buf = realloc(
vm->vm_arg_excess,
new_capacity * sizeof *vm->vm_arg_excess);
if (!buf) {
return;
}
vm->vm_arg_excess = buf;
vm->vm_arg_excess_max = new_capacity;
}
static void push_excess(struct callvm *vm, uintptr_t value)
{
if (vm->vm_arg_excess_count + 1 > vm->vm_arg_excess_max) {
expand_excess(vm);
}
vm->vm_arg_excess[vm->vm_arg_excess_count++] = value;
vm->vm_arg_count++;
}
static void push_int(struct callvm *vm, uintptr_t value)
{
if (vm->vm_arg_int_count >= MAX_INT_ARGS) {
push_excess(vm, value);
return;
}
vm->vm_arg_int[vm->vm_arg_int_count++] = value;
vm->vm_arg_count++;
}
static void push_double(struct callvm *vm, double value)
{
if (vm->vm_arg_double_count >= MAX_DOUBLE_ARGS) {
push_excess(vm, *(uintptr_t *)&value);
return;
}
vm->vm_arg_double[vm->vm_arg_double_count++] = value;
vm->vm_arg_count++;
if (vm->vm_arg_count > vm->vm_arg_fixed) {
vm->vm_double_excess_count++;
}
}
void callvm_push(struct callvm *vm, const fx_value *value)
{
if (!fx_type_is_value_type(value->v_type)) {
push_int(vm, (uintptr_t)value->v_object);
return;
}
unsigned int value_type = __fx_type_get_value_type(value->v_type);
switch (value_type) {
case __FX_VALUE_TYPE_BOOL:
push_int(vm, value->v_bool);
break;
case __FX_VALUE_TYPE_I16:
push_int(vm, value->v_i16);
break;
case __FX_VALUE_TYPE_U16:
push_int(vm, value->v_u16);
break;
case __FX_VALUE_TYPE_I32:
push_int(vm, value->v_i32);
break;
case __FX_VALUE_TYPE_U32:
push_int(vm, value->v_u32);
break;
case __FX_VALUE_TYPE_I64:
push_int(vm, value->v_i64);
break;
case __FX_VALUE_TYPE_U64:
push_int(vm, value->v_u64);
break;
case __FX_VALUE_TYPE_IPTR:
push_int(vm, value->v_iptr);
break;
case __FX_VALUE_TYPE_UPTR:
push_int(vm, value->v_uptr);
break;
case __FX_VALUE_TYPE_SBYTE:
push_int(vm, value->v_sbyte);
break;
case __FX_VALUE_TYPE_BYTE:
push_int(vm, value->v_byte);
break;
case __FX_VALUE_TYPE_SHORT:
push_int(vm, value->v_short);
break;
case __FX_VALUE_TYPE_USHORT:
push_int(vm, value->v_ushort);
break;
case __FX_VALUE_TYPE_INT:
push_int(vm, value->v_int);
break;
case __FX_VALUE_TYPE_UINT:
push_int(vm, value->v_uint);
break;
case __FX_VALUE_TYPE_LONG:
push_int(vm, value->v_long);
break;
case __FX_VALUE_TYPE_ULONG:
push_int(vm, value->v_ulong);
break;
case __FX_VALUE_TYPE_LONGLONG:
push_int(vm, value->v_longlong);
break;
case __FX_VALUE_TYPE_ULONGLONG:
push_int(vm, value->v_ulonglong);
break;
case __FX_VALUE_TYPE_SIZE:
push_int(vm, value->v_size);
break;
case __FX_VALUE_TYPE_FLOAT:
push_double(vm, value->v_float);
break;
case __FX_VALUE_TYPE_DOUBLE:
push_double(vm, value->v_double);
break;
case __FX_VALUE_TYPE_CSTR:
push_int(vm, (uintptr_t)value->v_cstr);
break;
case __FX_VALUE_TYPE_POINTER:
push_int(vm, (uintptr_t)value->v_pointer);
break;
default:
break;
}
}
extern uintptr_t callvm_invoke_i(fx_function_impl impl, struct callvm *vm);
extern double callvm_invoke_d(fx_function_impl impl, struct callvm *vm);
extern void callvm_invoke_v(fx_function_impl impl, struct callvm *vm);
fx_value callvm_invoke(
struct callvm *vm,
fx_function_impl impl,
fx_type_id return_type)
{
if (!fx_type_is_value_type(return_type)) {
uintptr_t v = callvm_invoke_i(impl, vm);
fx_value result = {
.v_type = return_type,
.v_object = (fx_object *)v,
};
return result;
}
unsigned int value_type = __fx_type_get_value_type(return_type);
switch (value_type) {
case __FX_VALUE_TYPE_BOOL:
return FX_BOOL(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_I16:
return FX_I16(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_U16:
return FX_U16(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_I32:
return FX_I32(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_U32:
return FX_U32(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_I64:
return FX_I64(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_U64:
return FX_U64(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_IPTR:
return FX_IPTR(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_UPTR:
return FX_UPTR(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_SBYTE:
return FX_SBYTE(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_BYTE:
return FX_BYTE(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_SHORT:
return FX_SHORT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_USHORT:
return FX_USHORT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_INT:
return FX_INT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_UINT:
return FX_UINT(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_LONG:
return FX_LONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_ULONG:
return FX_ULONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_LONGLONG:
return FX_LONGLONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_ULONGLONG:
return FX_ULONGLONG(callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_SIZE:
return FX_SIZE(callvm_invoke_i(impl, vm));
#if FX_ENABLE_FLOATING_POINT
case __FX_VALUE_TYPE_FLOAT:
return FX_FLOAT(callvm_invoke_d(impl, vm));
case __FX_VALUE_TYPE_DOUBLE:
return FX_DOUBLE(callvm_invoke_d(impl, vm));
#endif
case __FX_VALUE_TYPE_CSTR:
return FX_CSTR((const char *)callvm_invoke_i(impl, vm));
case __FX_VALUE_TYPE_POINTER:
return FX_POINTER((void *)callvm_invoke_i(impl, vm));
default:
return FX_VALUE_EMPTY;
}
return FX_VALUE_EMPTY;
}
@@ -0,0 +1,37 @@
#ifndef FX_REFLECTION_DARWIN_ARM64_CALLVM_H_
#define FX_REFLECTION_DARWIN_ARM64_CALLVM_H_
#include <fx/reflection/function.h>
#include <fx/value.h>
#include <stdint.h>
#define MAX_FIXED_ARGS ((unsigned int)-1)
#define MAX_DOUBLE_ARGS 8
#define MAX_INT_ARGS 6
/* dyn-dispatch.S depends on the layout of this struct */
struct callvm {
uint64_t vm_arg_int_count;
uint64_t vm_arg_double_count;
/* any args pushed after this limit is reached will be stored in the
* excess buffer. used for calling varargs functions */
uint64_t vm_arg_count, vm_arg_fixed;
uint64_t vm_arg_excess_count;
uint64_t vm_arg_excess_max;
uintptr_t vm_arg_int[MAX_INT_ARGS];
double vm_arg_double[MAX_DOUBLE_ARGS];
uintptr_t *vm_arg_excess;
uint64_t vm_double_excess_count;
};
extern void callvm_reset(struct callvm *vm, unsigned int max_fixed_args);
extern void callvm_push(struct callvm *vm, const fx_value *value);
extern fx_value callvm_invoke(
struct callvm *vm,
fx_function_impl impl,
fx_type_id return_type);
#endif
+47 -31
View File
@@ -31,12 +31,14 @@ struct fx_type_p {
struct fx_type_function_iterator_p { struct fx_type_function_iterator_p {
struct fx_namemap *it_src; struct fx_namemap *it_src;
struct fx_namemap_entry *it_cur; struct fx_namemap_entry *it_cur;
fx_value it_value;
}; };
struct fx_type_property_iterator_p { struct fx_type_property_iterator_p {
const fx_type_info *it_cur_type; const fx_type_info *it_cur_type;
struct fx_namemap *it_src; struct fx_namemap *it_src;
struct fx_namemap_entry *it_cur; struct fx_namemap_entry *it_cur;
fx_value it_value;
}; };
/*** PRIVATE FUNCTIONS ********************************************************/ /*** PRIVATE FUNCTIONS ********************************************************/
@@ -77,8 +79,8 @@ const fx_function *type_get_function(
fx_iterator *type_get_functions(const struct fx_type_p *ty) fx_iterator *type_get_functions(const struct fx_type_p *ty)
{ {
fx_type_function_iterator *it_obj = fx_object_create( fx_type_function_iterator *it_obj
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR); = fx_object_create(FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
struct fx_type_function_iterator_p *it = fx_object_get_private( struct fx_type_function_iterator_p *it = fx_object_get_private(
it_obj, it_obj,
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR); FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
@@ -95,9 +97,8 @@ const fx_property *type_get_property(
{ {
struct fx_type_info *cur = ty->ty_info; struct fx_type_info *cur = ty->ty_info;
while (cur) { while (cur) {
const fx_property *prop = fx_type_info_get_property_by_name( const fx_property *prop
cur, = fx_type_info_get_property_by_name(cur, name);
name);
if (prop) { if (prop) {
return prop; return prop;
} }
@@ -114,8 +115,8 @@ const fx_property *type_get_property(
static fx_iterator *type_get_properties(const struct fx_type_p *ty) static fx_iterator *type_get_properties(const struct fx_type_p *ty)
{ {
fx_type_property_iterator *it_obj = fx_object_create( fx_type_property_iterator *it_obj
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR); = fx_object_create(FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
struct fx_type_property_iterator_p *it = fx_object_get_private( struct fx_type_property_iterator_p *it = fx_object_get_private(
it_obj, it_obj,
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR); FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
@@ -129,8 +130,9 @@ static fx_iterator *type_get_properties(const struct fx_type_p *ty)
break; break;
} }
const struct fx_type_info *next_type = fx_type_info_get_by_id( const struct fx_type_info *next_type
&it->it_cur_type->ty_parent_id); = fx_type_info_get_by_id(&it->it_cur_type
->ty_parent_id);
if (!next_type) { if (!next_type) {
break; break;
} }
@@ -143,6 +145,10 @@ static fx_iterator *type_get_properties(const struct fx_type_p *ty)
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA); fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
} }
struct fx_type_property *prop
= fx_unbox(struct fx_type_property, it->it_cur, p_entry);
it->it_value = FX_VALUE_OBJECT_REF(prop->p_prop);
return it_obj; return it_obj;
} }
@@ -176,9 +182,8 @@ fx_type *__fx_type_create(struct fx_type_info *type_info)
return NULL; return NULL;
} }
struct fx_type_p *p = fx_object_get_private( struct fx_type_p *p
out, = fx_object_get_private(out, FX_REFLECTION_TYPE_TYPE);
FX_REFLECTION_TYPE_TYPE);
p->ty_info = type_info; p->ty_info = type_info;
return out; return out;
@@ -300,25 +305,29 @@ static enum fx_status function_iterator_move_next(const fx_iterator *obj)
return FX_ERR_NO_DATA; return FX_ERR_NO_DATA;
} }
fx_value_unset(&it->it_value);
it->it_cur = fx_namemap_next(it->it_src, it->it_cur); it->it_cur = fx_namemap_next(it->it_src, it->it_cur);
return it->it_cur ? FX_SUCCESS : FX_ERR_NO_DATA; if (!it->it_cur) {
return FX_ERR_NO_DATA;
} }
static fx_value function_iterator_get_value(const fx_iterator *obj) struct fx_type_function *func
= fx_unbox(struct fx_type_function, it->it_cur, f_entry);
it->it_value = FX_VALUE_OBJECT_REF(func->f_func);
return FX_SUCCESS;
}
static const fx_value *function_iterator_get_value(const fx_iterator *obj)
{ {
struct fx_type_function_iterator_p *it = fx_object_get_private( struct fx_type_function_iterator_p *it = fx_object_get_private(
obj, obj,
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR); FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
if (!it->it_cur) { if (!it->it_cur) {
return FX_VALUE_EMPTY; return NULL;
} }
struct fx_type_function *func = fx_unbox( return &it->it_value;
struct fx_type_function,
it->it_cur,
f_entry);
return FX_VALUE_OBJECT(func->f_func);
} }
static enum fx_status property_iterator_move_next(const fx_iterator *obj) static enum fx_status property_iterator_move_next(const fx_iterator *obj)
@@ -337,8 +346,9 @@ static enum fx_status property_iterator_move_next(const fx_iterator *obj)
return FX_ERR_NO_DATA; return FX_ERR_NO_DATA;
} }
const struct fx_type_info *next_type = fx_type_info_get_by_id( const struct fx_type_info *next_type
&it->it_cur_type->ty_parent_id); = fx_type_info_get_by_id(&it->it_cur_type
->ty_parent_id);
if (!next_type) { if (!next_type) {
return FX_ERR_NO_DATA; return FX_ERR_NO_DATA;
} }
@@ -347,24 +357,30 @@ static enum fx_status property_iterator_move_next(const fx_iterator *obj)
it->it_cur = fx_namemap_first(&it->it_cur_type->ty_properties); it->it_cur = fx_namemap_first(&it->it_cur_type->ty_properties);
} }
return it->it_cur ? FX_SUCCESS : FX_ERR_NO_DATA; if (!it->it_cur) {
return FX_ERR_NO_DATA;
} }
static fx_value property_iterator_get_value(const fx_iterator *obj) struct fx_type_property *prop
= fx_unbox(struct fx_type_property, it->it_cur, p_entry);
it->it_value = FX_VALUE_OBJECT_REF(prop->p_prop);
return FX_SUCCESS;
}
static const fx_value *property_iterator_get_value(const fx_iterator *obj)
{ {
struct fx_type_property_iterator_p *it = fx_object_get_private( struct fx_type_property_iterator_p *it = fx_object_get_private(
obj, obj,
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR); FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
if (!it->it_cur) { if (!it->it_cur) {
return FX_VALUE_EMPTY; return NULL;
} }
struct fx_type_property *prop = fx_unbox( struct fx_type_property *prop
struct fx_type_property, = fx_unbox(struct fx_type_property, it->it_cur, p_entry);
it->it_cur,
p_entry);
return FX_VALUE_OBJECT(prop->p_prop); return &it->it_value;
} }
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
+2 -2
View File
@@ -5,7 +5,7 @@
static struct fx_error *bitcode_serialise( static struct fx_error *bitcode_serialise(
fx_serial_ctx *serial, fx_serial_ctx *serial,
fx_object *src, const fx_value *src,
fx_stream *dest, fx_stream *dest,
enum fx_serial_flags flags) enum fx_serial_flags flags)
{ {
@@ -15,7 +15,7 @@ static struct fx_error *bitcode_serialise(
static struct fx_error *bitcode_deserialise( static struct fx_error *bitcode_deserialise(
fx_serial_ctx *serial, fx_serial_ctx *serial,
fx_stream *src, fx_stream *src,
fx_object **dest, fx_value *dest,
enum fx_serial_flags flags) enum fx_serial_flags flags)
{ {
return FX_RESULT_ERR(NOT_SUPPORTED); return FX_RESULT_ERR(NOT_SUPPORTED);
+2 -2
View File
@@ -44,7 +44,7 @@ FX_TYPE_DEFINITION_END(fx_serial_ctx)
fx_result fx_serial_ctx_serialise( fx_result fx_serial_ctx_serialise(
fx_serial_ctx *ctx, fx_serial_ctx *ctx,
fx_object *src, const fx_value *src,
fx_stream *dest, fx_stream *dest,
enum fx_serial_flags flags) enum fx_serial_flags flags)
{ {
@@ -62,7 +62,7 @@ fx_result fx_serial_ctx_serialise(
fx_result fx_serial_ctx_deserialise( fx_result fx_serial_ctx_deserialise(
fx_serial_ctx *ctx, fx_serial_ctx *ctx,
fx_stream *src, fx_stream *src,
fx_object **dest, fx_value *dest,
enum fx_serial_flags flags) enum fx_serial_flags flags)
{ {
FX_CLASS_DISPATCH_VIRTUAL( FX_CLASS_DISPATCH_VIRTUAL(
+5 -4
View File
@@ -6,6 +6,7 @@
#include <fx/object.h> #include <fx/object.h>
#include <fx/status.h> #include <fx/status.h>
#include <fx/stream.h> #include <fx/stream.h>
#include <fx/value.h>
FX_DECLS_BEGIN; FX_DECLS_BEGIN;
@@ -21,13 +22,13 @@ FX_DECLARE_TYPE(fx_serial_ctx);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_serial_ctx) FX_TYPE_CLASS_DECLARATION_BEGIN(fx_serial_ctx)
fx_result (*s_serialise)( fx_result (*s_serialise)(
fx_serial_ctx *, fx_serial_ctx *,
fx_object *, const fx_value *,
fx_stream *, fx_stream *,
fx_serial_flags); fx_serial_flags);
fx_result (*s_deserialise)( fx_result (*s_deserialise)(
fx_serial_ctx *, fx_serial_ctx *,
fx_stream *, fx_stream *,
fx_object **, fx_value *,
fx_serial_flags); fx_serial_flags);
FX_TYPE_CLASS_DECLARATION_END(fx_serial_ctx) FX_TYPE_CLASS_DECLARATION_END(fx_serial_ctx)
@@ -39,14 +40,14 @@ FX_API fx_type_id fx_serial_ctx_get_type(void);
FX_API fx_result fx_serial_ctx_serialise( FX_API fx_result fx_serial_ctx_serialise(
fx_serial_ctx *ctx, fx_serial_ctx *ctx,
fx_object *src, const fx_value *src,
fx_stream *dest, fx_stream *dest,
fx_serial_flags flags); fx_serial_flags flags);
FX_API fx_result fx_serial_ctx_deserialise( FX_API fx_result fx_serial_ctx_deserialise(
fx_serial_ctx *ctx, fx_serial_ctx *ctx,
fx_stream *src, fx_stream *src,
fx_object **dest, fx_value *dest,
fx_serial_flags flags); fx_serial_flags flags);
FX_DECLS_END; FX_DECLS_END;
+53 -75
View File
@@ -1,24 +1,26 @@
#include <fx/bool.h> #include <fx/bool.h>
#include <fx/collections/array.h> #include <fx/collections/array.h>
#include <fx/collections/datetime.h> #include <fx/collections/hashtable.h>
#include <fx/datetime.h>
#include <fx/float.h> #include <fx/float.h>
#include <fx/int.h> #include <fx/int.h>
#include <fx/serial/ctx.h> #include <fx/serial/ctx.h>
#include <fx/serial/toml.h> #include <fx/serial/toml.h>
#include <fx/string.h> #include <fx/string.h>
#include <inttypes.h> #include <inttypes.h>
#include <math.h>
void write_tagged_value(fx_object *data); void write_tagged_value(const fx_value *data);
void write_raw_string(const fx_string *data) void write_raw_string(const fx_string *data)
{ {
fx_stream_write_cstr(fx_stdout, "\"", NULL); fx_stream_write_cstr(fx_stdout, "\"", NULL);
const fx_iterator *it = fx_iterator_cbegin(data); const fx_iterator *it = fx_iterator_begin(data);
fx_foreach(val, it) fx_foreach(val, it)
{ {
fx_wchar c; fx_wchar c;
fx_value_get_wchar(&val, &c); fx_value_get_wchar(val, &c);
if (c >= 0x10000) { if (c >= 0x10000) {
c -= 0x10000; c -= 0x10000;
long hi = 0xD800 | ((c >> 10) & 0x3FF); long hi = 0xD800 | ((c >> 10) & 0x3FF);
@@ -54,75 +56,49 @@ void write_tagged_string(fx_string *data)
fx_stream_write_cstr(fx_stdout, " }", NULL); fx_stream_write_cstr(fx_stdout, " }", NULL);
} }
void write_tagged_integer(fx_int *data) void write_tagged_integer(const fx_value *data)
{ {
#if 0
fx_stream_write_cstr( fx_stream_write_cstr(
fx_stdout, fx_stdout,
"{ \"type\": \"integer\", \"value\": \"", "{ \"type\": \"integer\", \"value\": \"",
NULL); NULL);
fx_stream_write_fmt(fx_stdout, NULL, "%lld", data->v_i64, NULL);
if (fx_int_is_inf_positive(data)) {
fx_stream_write_cstr(fx_stdout, "inf", NULL);
} else if (fx_int_is_inf_negative(data)) {
fx_stream_write_cstr(fx_stdout, "-inf", NULL);
} else if (fx_int_is_nan_positive(data)) {
fx_stream_write_cstr(fx_stdout, "nan", NULL);
} else if (fx_int_is_nan_negative(data)) {
fx_stream_write_cstr(fx_stdout, "-nan", NULL);
} else {
fx_stream_write_fmt(
fx_stdout,
NULL,
"%lld",
fx_int_get_value(data),
NULL);
}
fx_stream_write_cstr(fx_stdout, "\" }", NULL); fx_stream_write_cstr(fx_stdout, "\" }", NULL);
#endif
} }
void write_tagged_float(fx_double *data) void write_tagged_float(const fx_value *data)
{ {
#if 0
fx_stream_write_cstr( fx_stream_write_cstr(
fx_stdout, fx_stdout,
"{ \"type\": \"float\", \"value\": \"", "{ \"type\": \"float\", \"value\": \"",
NULL); NULL);
if (fx_double_is_inf_positive(data)) { double d = data->v_double;
if (d == INFINITY) {
fx_stream_write_cstr(fx_stdout, "inf", NULL); fx_stream_write_cstr(fx_stdout, "inf", NULL);
} else if (fx_double_is_inf_negative(data)) { } else if (d == -INFINITY) {
fx_stream_write_cstr(fx_stdout, "-inf", NULL); fx_stream_write_cstr(fx_stdout, "-inf", NULL);
} else if (fx_double_is_nan_positive(data)) { } else if (d == NAN) {
fx_stream_write_cstr(fx_stdout, "nan", NULL); fx_stream_write_cstr(fx_stdout, "nan", NULL);
} else if (fx_double_is_nan_negative(data)) { } else if (d == -NAN) {
fx_stream_write_cstr(fx_stdout, "-nan", NULL); fx_stream_write_cstr(fx_stdout, "-nan", NULL);
} else if (d <= 1e-9 || d >= 1e9) {
fx_stream_write_fmt(fx_stdout, NULL, "%g", d, NULL);
} else { } else {
double v = fx_double_get_value(data); fx_stream_write_fmt(fx_stdout, NULL, "%.9lf", d, NULL);
if ((v <= 0.00000001 && v > 0) || (v >= -0.00000001 && v < 0)
|| (v >= 1000000000) || (v <= -1000000000)) {
fx_stream_write_fmt(fx_stdout, NULL, "%.15e", v, NULL);
} else {
fx_stream_write_fmt(fx_stdout, NULL, "%.15f", v, NULL);
}
} }
fx_stream_write_cstr(fx_stdout, "\" }", NULL); fx_stream_write_cstr(fx_stdout, "\" }", NULL);
#endif
} }
void write_tagged_bool(fx_bool *data) void write_tagged_bool(const fx_value *data)
{ {
#if 0
fx_stream_write_fmt( fx_stream_write_fmt(
fx_stdout, fx_stdout,
NULL, NULL,
"{ \"type\": \"bool\", \"value\": \"%s\" }", "{ \"type\": \"bool\", \"value\": \"%s\" }",
fx_bool_get_value(data) ? "true" : "false", data->v_bool ? "true" : "false",
NULL); NULL);
#endif
} }
void write_tagged_datetime(fx_datetime *data) void write_tagged_datetime(fx_datetime *data)
@@ -152,75 +128,78 @@ void write_tagged_datetime(fx_datetime *data)
fx_stream_write_cstr(fx_stdout, "\", \"value\": \"", NULL); fx_stream_write_cstr(fx_stdout, "\", \"value\": \"", NULL);
fx_string *new_data = fx_string_create(); fx_stringstream *new_data = fx_stringstream_create();
fx_datetime_to_string(data, FX_DATETIME_FORMAT_RFC3339, new_data); fx_datetime_to_string(data, FX_DATETIME_FORMAT_RFC3339, new_data);
fx_stream_write_cstr(fx_stdout, fx_string_get_cstr(new_data), NULL); fx_stream_write_cstr(fx_stdout, fx_stringstream_ptr(new_data), NULL);
fx_stream_write_cstr(fx_stdout, "\" }", NULL); fx_stream_write_cstr(fx_stdout, "\" }", NULL);
fx_string_unref(new_data); fx_stringstream_unref(new_data);
} }
#if 0 void write_tagged_hashtable(fx_hashtable *data)
void write_tagged_dict(fx_dict *data)
{ {
fx_stream_write_cstr(fx_stdout, "{ ", NULL); fx_stream_write_cstr(fx_stdout, "{ ", NULL);
int i = 0; int i = 0;
fx_iterator *it = fx_iterator_begin(data); const fx_iterator *it = fx_iterator_begin(data);
fx_foreach(fx_dict_item *, item, it) fx_foreach(v, it)
{ {
fx_hashtable_item *item;
fx_value_get_object(v, &item);
if (i++ > 0) { if (i++ > 0) {
fx_stream_write_cstr(fx_stdout, ", ", NULL); fx_stream_write_cstr(fx_stdout, ", ", NULL);
} }
write_raw_string(item->key); const fx_value *key = fx_hashtable_item_get_key(item);
const fx_value *value = fx_hashtable_item_get_value(item);
fx_string *key_str;
fx_value_get_object(key, &key_str);
write_raw_string(key_str);
fx_stream_write_cstr(fx_stdout, ": ", NULL); fx_stream_write_cstr(fx_stdout, ": ", NULL);
write_tagged_value(item->value); write_tagged_value(value);
} }
fx_iterator_unref(it); fx_iterator_unref(it);
fx_stream_write_cstr(fx_stdout, " }", NULL); fx_stream_write_cstr(fx_stdout, " }", NULL);
} }
#endif
void write_tagged_array(fx_array *data) void write_tagged_array(fx_array *data)
{ {
fx_stream_write_cstr(fx_stdout, "[ ", NULL); fx_stream_write_cstr(fx_stdout, "[ ", NULL);
int i = 0; int i = 0;
fx_iterator *it = fx_iterator_begin(data); const fx_iterator *it = fx_iterator_begin(data);
fx_foreach(val, it) fx_foreach(val, it)
{ {
fx_object *obj;
fx_value_get_object(&val, &obj);
if (i++ > 0) { if (i++ > 0) {
fx_stream_write_cstr(fx_stdout, ", ", NULL); fx_stream_write_cstr(fx_stdout, ", ", NULL);
} }
write_tagged_value(obj); write_tagged_value(val);
} }
fx_iterator_unref(it); fx_iterator_unref(it);
fx_stream_write_cstr(fx_stdout, " ]", NULL); fx_stream_write_cstr(fx_stdout, " ]", NULL);
} }
void write_tagged_value(fx_object *data) void write_tagged_value(const fx_value *data)
{ {
#if 0 if (fx_value_is_type(data, FX_TYPE_HASHTABLE)) {
if (fx_object_is_type(data, FX_TYPE_DICT)) { write_tagged_hashtable(data->v_object);
write_tagged_dict(data); } else if (fx_value_is_type(data, FX_TYPE_ARRAY)) {
} else write_tagged_array(data->v_object);
#endif } else if (fx_value_is_type(data, FX_TYPE_STRING)) {
if (fx_object_is_type(data, FX_TYPE_ARRAY)) { write_tagged_string(data->v_object);
write_tagged_array(data); } else if (fx_value_is_type(data, FX_TYPE_DATETIME)) {
} else if (fx_object_is_type(data, FX_TYPE_STRING)) { write_tagged_datetime(data->v_object);
write_tagged_string(data); } else if (fx_value_is_type(data, FX_TYPE_BOOL)) {
} else if (fx_object_is_type(data, FX_TYPE_DATETIME)) {
write_tagged_datetime(data);
} else if (fx_object_is_type(data, FX_TYPE_BOOL)) {
write_tagged_bool(data); write_tagged_bool(data);
} else if (fx_value_is_type(data, FX_TYPE_I64)) {
write_tagged_integer(data);
} else if (fx_value_is_type(data, FX_TYPE_DOUBLE)) {
write_tagged_float(data);
} }
} }
@@ -231,19 +210,18 @@ int main(void)
fx_serial_ctx *ctx = fx_toml_serial_ctx_create(); fx_serial_ctx *ctx = fx_toml_serial_ctx_create();
fx_object *data; fx_value data;
fx_result result = fx_serial_ctx_deserialise(ctx, src, &data, 0); fx_result result = fx_serial_ctx_deserialise(ctx, src, &data, 0);
if (fx_result_is_error(result)) { if (fx_result_is_error(result)) {
fx_throw(result); return 1;
return -1;
} }
write_tagged_value(data); write_tagged_value(&data);
fx_stream_write_char(fx_stdout, '\n'); fx_stream_write_char(fx_stdout, '\n');
fx_serial_ctx_unref(ctx); fx_serial_ctx_unref(ctx);
fx_object_unref(data); fx_value_unset(&data);
return 0; return 0;
} }
+134 -181
View File
@@ -1,8 +1,8 @@
#include <fx/bool.h> #include <fx/bool.h>
#include <fx/collections/array.h> #include <fx/collections/array.h>
#include <fx/collections/datetime.h>
#include <fx/collections/hashmap.h> #include <fx/collections/hashmap.h>
#include <fx/collections/hashtable.h> #include <fx/collections/hashtable.h>
#include <fx/datetime.h>
#include <fx/error.h> #include <fx/error.h>
#include <fx/float.h> #include <fx/float.h>
#include <fx/int.h> #include <fx/int.h>
@@ -11,6 +11,7 @@
#include <fx/status.h> #include <fx/status.h>
#include <fx/string.h> #include <fx/string.h>
#include <fx/stringstream.h> #include <fx/stringstream.h>
#include <math.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -116,9 +117,9 @@ struct ctx {
fx_stream *ctx_src; fx_stream *ctx_src;
fx_string *ctx_wordbuf; fx_string *ctx_wordbuf;
fx_string *ctx_linebuf; fx_string *ctx_linebuf;
fx_iterator *ctx_linebuf_ptr; const fx_iterator *ctx_linebuf_ptr;
fx_result ctx_result; fx_result ctx_result;
fx_hashmap *ctx_objects_flags; fx_hashtable *ctx_objects_flags;
fx_queue ctx_tokens; fx_queue ctx_tokens;
}; };
@@ -132,29 +133,18 @@ static void ctx_set_object_flags(
return; return;
} }
fx_hashmap_key key = { fx_value ptr = FX_POINTER(obj);
.key_data = obj, const fx_value *old_value
.key_size = sizeof(fx_object *), = fx_hashtable_get(ctx->ctx_objects_flags, &ptr);
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
};
const fx_hashmap_value *old_value = fx_hashmap_get(
ctx->ctx_objects_flags,
&key);
enum object_flags new_flags = 0; enum object_flags new_flags = 0;
if (old_value) { if (old_value) {
new_flags = (enum object_flags)(uintptr_t)old_value->value_data; new_flags = (enum object_flags)old_value->v_i32;
} }
new_flags |= flags; new_flags |= flags;
fx_hashmap_value value = { fx_hashtable_put(ctx->ctx_objects_flags, &ptr, &FX_I32(new_flags));
.value_data = (void *)new_flags,
.value_size = sizeof new_flags,
};
fx_hashmap_put(ctx->ctx_objects_flags, &key, &value);
} }
static void ctx_clear_object_flags( static void ctx_clear_object_flags(
@@ -166,29 +156,18 @@ static void ctx_clear_object_flags(
return; return;
} }
fx_hashmap_key key = { fx_value ptr = FX_POINTER(obj);
.key_data = obj, const fx_value *old_value
.key_size = sizeof(fx_object *), = fx_hashtable_get(ctx->ctx_objects_flags, &ptr);
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
};
const fx_hashmap_value *old_value = fx_hashmap_get(
ctx->ctx_objects_flags,
&key);
enum object_flags new_flags = 0; enum object_flags new_flags = 0;
if (old_value) { if (old_value) {
new_flags = (enum object_flags)(uintptr_t)old_value->value_data; new_flags = (enum object_flags)old_value->v_i32;
} }
new_flags &= ~mask; new_flags &= ~mask;
fx_hashmap_value value = { fx_hashtable_put(ctx->ctx_objects_flags, &ptr, &FX_I32(new_flags));
.value_data = (void *)new_flags,
.value_size = sizeof new_flags,
};
fx_hashmap_put(ctx->ctx_objects_flags, &key, &value);
} }
static enum object_flags ctx_get_object_flags(struct ctx *ctx, fx_object *obj) static enum object_flags ctx_get_object_flags(struct ctx *ctx, fx_object *obj)
@@ -197,17 +176,13 @@ static enum object_flags ctx_get_object_flags(struct ctx *ctx, fx_object *obj)
return 0; return 0;
} }
fx_hashmap_key key = { fx_value ptr = FX_POINTER(obj);
.key_data = obj, const fx_value *old_value
.key_size = sizeof(fx_object *), = fx_hashtable_get(ctx->ctx_objects_flags, &ptr);
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
};
const fx_hashmap_value *value = fx_hashmap_get( enum object_flags new_flags = 0;
ctx->ctx_objects_flags, if (old_value) {
&key); return (enum object_flags)old_value->v_i32;
if (value) {
return (enum object_flags)(uintptr_t)value->value_data;
} }
return 0; return 0;
@@ -297,7 +272,7 @@ static fx_wchar advance_char(struct ctx *ctx)
const char *s = fx_string_get_cstr(ctx->ctx_linebuf); const char *s = fx_string_get_cstr(ctx->ctx_linebuf);
fx_wchar c = fx_iterator_get_value(ctx->ctx_linebuf_ptr).v_int; fx_wchar c = fx_iterator_get_value(ctx->ctx_linebuf_ptr)->v_wchar;
if (!is_valid_char(c)) { if (!is_valid_char(c)) {
ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT); ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT);
@@ -330,7 +305,7 @@ static fx_wchar peek_char(struct ctx *ctx)
const char *s = fx_string_get_cstr(ctx->ctx_linebuf); const char *s = fx_string_get_cstr(ctx->ctx_linebuf);
fx_wchar c = fx_iterator_get_value(ctx->ctx_linebuf_ptr).v_int; fx_wchar c = fx_iterator_get_value(ctx->ctx_linebuf_ptr)->v_wchar;
if (!is_valid_char(c)) { if (!is_valid_char(c)) {
ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT); ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT);
@@ -929,7 +904,7 @@ static void split_word(struct ctx *ctx, fx_string *wordbuf)
fx_foreach(tokv, it) fx_foreach(tokv, it)
{ {
const char *tok = NULL; const char *tok = NULL;
fx_value_get_cstr(&tokv, &tok); fx_value_get_cstr(tokv, &tok);
if (i > 0) { if (i > 0) {
enqueue_token(ctx, TOK_DOT); enqueue_token(ctx, TOK_DOT);
@@ -1032,11 +1007,11 @@ static void read_word(struct ctx *ctx)
return; return;
} }
fx_iterator *it = fx_iterator_begin(wordbuf); const fx_iterator *it = fx_iterator_begin(wordbuf);
fx_foreach(cv, it) fx_foreach(cv, it)
{ {
fx_wchar c = FX_WCHAR_INVALID; fx_wchar c = FX_WCHAR_INVALID;
fx_value_get_wchar(&cv, &c); fx_value_get_wchar(cv, &c);
/* only allow ASCII numbers/letters here */ /* only allow ASCII numbers/letters here */
bool ok = isalnum(c) || c == '_' || c == '-' || c == '.'; bool ok = isalnum(c) || c == '_' || c == '-' || c == '.';
if (!ok) { if (!ok) {
@@ -1184,8 +1159,8 @@ static void read_string(struct ctx *ctx, bool squote)
} }
if (c != '\n') { if (c != '\n') {
ctx->ctx_result = FX_RESULT_ERR( ctx->ctx_result
BAD_FORMAT); = FX_RESULT_ERR(BAD_FORMAT);
fail = true; fail = true;
break; break;
} }
@@ -1230,18 +1205,16 @@ static void read_string(struct ctx *ctx, bool squote)
case 'U': case 'U':
c = read_unicode_sequence(ctx); c = read_unicode_sequence(ctx);
if (c == FX_WCHAR_INVALID) { if (c == FX_WCHAR_INVALID) {
ctx->ctx_result = FX_RESULT_ERR( ctx->ctx_result
BAD_FORMAT); = FX_RESULT_ERR(BAD_FORMAT);
fail = true; fail = true;
break; break;
} }
ctx->ctx_result = FX_OK(fx_string_append_wc( ctx->ctx_result
str, = FX_OK(fx_string_append_wc(str, c))
c))
? FX_SUCCESS ? FX_SUCCESS
: FX_RESULT_ERR( : FX_RESULT_ERR(BAD_FORMAT);
BAD_FORMAT);
fail = !FX_OK(ctx->ctx_result); fail = !FX_OK(ctx->ctx_result);
break; break;
default: default:
@@ -1482,14 +1455,24 @@ static fx_result advance_token(struct ctx *ctx)
start: start:
c = peek_char(ctx); c = peek_char(ctx);
while (isspace(c) && c != '\n' && c != '\r') {
advance_char(ctx); if (c <= 0) {
c = peek_char(ctx); if (fx_error_get_status_code(ctx->ctx_result)
== FX_ERR_NO_DATA) {
ctx->ctx_flags |= CTX_EOF;
} }
if (c == -1) { return ctx->ctx_result;
ctx->ctx_flags |= CTX_EOF; }
return FX_RESULT_ERR(NO_DATA);
if (c <= 31 && c != '\n' && c != '\r' && c != '\t') {
ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT);
return ctx->ctx_result;
}
while (fx_wchar_is_space(c) && c != '\n' && c != '\r') {
advance_char(ctx);
c = peek_char(ctx);
} }
#if 1 #if 1
@@ -1559,7 +1542,7 @@ static void ctx_cleanup(struct ctx *ctx)
} }
if (ctx->ctx_objects_flags) { if (ctx->ctx_objects_flags) {
fx_hashmap_unref(ctx->ctx_objects_flags); fx_hashtable_unref(ctx->ctx_objects_flags);
ctx->ctx_objects_flags = NULL; ctx->ctx_objects_flags = NULL;
} }
} }
@@ -1571,14 +1554,14 @@ static fx_result ctx_init(struct ctx *ctx)
ctx->ctx_linebuf = fx_string_create(); ctx->ctx_linebuf = fx_string_create();
ctx->ctx_wordbuf = fx_string_create(); ctx->ctx_wordbuf = fx_string_create();
ctx->ctx_objects_flags = fx_hashmap_create(NULL, NULL); ctx->ctx_objects_flags = fx_hashtable_create();
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result toml_serialise( static fx_result toml_serialise(
fx_serial_ctx *serial, fx_serial_ctx *serial,
fx_object *src, const fx_value *src,
fx_stream *dest, fx_stream *dest,
enum fx_serial_flags flags) enum fx_serial_flags flags)
{ {
@@ -1672,20 +1655,20 @@ static void print_token(struct token *tok)
} }
} }
static fx_result parse_value(struct ctx *ctx, fx_object **result); static fx_result parse_value(struct ctx *ctx, fx_value *result);
static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container); static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container);
static fx_result parse_timestamp(struct ctx *ctx, fx_object **result) static fx_result parse_timestamp(struct ctx *ctx, fx_value *result)
{ {
struct token *tok = peek_token(ctx); struct token *tok = peek_token(ctx);
fx_datetime *dt = tok->tok_value.time; fx_datetime *dt = tok->tok_value.time;
tok->tok_value.time = NULL; tok->tok_value.time = NULL;
*result = (dt); *result = FX_VALUE_OBJECT(dt);
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_string(struct ctx *ctx, fx_object **result) static fx_result parse_string(struct ctx *ctx, fx_value *result)
{ {
struct token *tok = peek_token(ctx); struct token *tok = peek_token(ctx);
fx_string *str = fx_string_duplicate(tok->tok_str); fx_string *str = fx_string_duplicate(tok->tok_str);
@@ -1693,81 +1676,48 @@ static fx_result parse_string(struct ctx *ctx, fx_object **result)
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
*result = (str); *result = FX_VALUE_OBJECT(str);
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_int(struct ctx *ctx, fx_object **result) static fx_result parse_int(struct ctx *ctx, fx_value *result)
{ {
#if 0
struct token *tok = peek_token(ctx); struct token *tok = peek_token(ctx);
fx_int *val = fx_int_create(tok->tok_value.i.v); *result = FX_I64(tok->tok_value.i.v);
if (!val) {
return FX_RESULT_ERR(NO_MEMORY);
}
if (tok->tok_value.i.inf) {
if (tok->tok_value.i.v >= 0) {
fx_int_set_value_inf(val);
} else {
fx_int_set_value_inf_negative(val);
}
} else if (tok->tok_value.i.nan) {
if (tok->tok_value.i.v >= 0) {
fx_int_set_value_nan(val);
} else {
fx_int_set_value_nan_negative(val);
}
}
*result = (val);
#endif
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_float(struct ctx *ctx, fx_object **result) static fx_result parse_float(struct ctx *ctx, fx_value *result)
{ {
#if 0
struct token *tok = peek_token(ctx); struct token *tok = peek_token(ctx);
fx_double *val = fx_double_create(tok->tok_value.f.v);
if (!val) {
return FX_RESULT_ERR(NO_MEMORY);
}
if (tok->tok_value.f.inf) { if (tok->tok_value.f.inf) {
if (tok->tok_value.f.v >= 0) { if (tok->tok_value.f.v >= 0) {
fx_double_set_value_inf(val); *result = FX_DOUBLE(INFINITY);
} else { } else {
fx_double_set_value_inf_negative(val); *result = FX_DOUBLE(-INFINITY);
} }
} else if (tok->tok_value.f.nan) { } else if (tok->tok_value.f.nan) {
if (tok->tok_value.f.v >= 0) { if (tok->tok_value.f.v >= 0) {
fx_double_set_value_nan(val); *result = FX_DOUBLE(NAN);
} else { } else {
fx_double_set_value_nan_negative(val); *result = FX_DOUBLE(-NAN);
} }
} else {
*result = FX_DOUBLE(tok->tok_value.f.v);
} }
*result = (val);
#endif
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_bool(struct ctx *ctx, fx_object **result) static fx_result parse_bool(struct ctx *ctx, fx_value *result)
{ {
#if 0
struct token *tok = peek_token(ctx); struct token *tok = peek_token(ctx);
fx_bool *val = fx_bool_create(tok->tok_value.b); *result = FX_BOOL(tok->tok_value.b);
if (!val) {
return FX_RESULT_ERR(NO_MEMORY);
}
*result = (val);
#endif
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_table_inline(struct ctx *ctx, fx_object **result) static fx_result parse_table_inline(struct ctx *ctx, fx_value *result)
{ {
DISABLE_EXTENDED_LEXING(ctx); DISABLE_EXTENDED_LEXING(ctx);
@@ -1780,7 +1730,7 @@ static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
struct token *tok = peek_token(ctx); struct token *tok = peek_token(ctx);
if (tok && tok->tok_type == TOK_RIGHT_BRACE) { if (tok && tok->tok_type == TOK_RIGHT_BRACE) {
*result = (table); *result = FX_VALUE_OBJECT(table);
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
@@ -1814,7 +1764,7 @@ static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
} }
} }
*result = (table); *result = FX_VALUE_OBJECT(table);
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
@@ -1828,7 +1778,7 @@ static void skip_newlines(struct ctx *ctx)
} }
} }
static fx_result parse_array_inline(struct ctx *ctx, fx_object **result) static fx_result parse_array_inline(struct ctx *ctx, fx_value *result)
{ {
bool done = false; bool done = false;
ENABLE_EXTENDED_LEXING(ctx); ENABLE_EXTENDED_LEXING(ctx);
@@ -1865,16 +1815,15 @@ static fx_result parse_array_inline(struct ctx *ctx, fx_object **result)
break; break;
} }
fx_object *value; fx_value value;
fx_result status = parse_value(ctx, &value); fx_result status = parse_value(ctx, &value);
if (!FX_OK(status)) { if (!FX_OK(status)) {
fx_array_unref(array); fx_array_unref(array);
return status; return status;
} }
#if 0 fx_array_push_back(array, value);
fx_array_append(array, FX_RV(value)); fx_value_unset(&value);
#endif
ENABLE_EXTENDED_LEXING(ctx); ENABLE_EXTENDED_LEXING(ctx);
advance_token(ctx); advance_token(ctx);
@@ -1897,11 +1846,11 @@ static fx_result parse_array_inline(struct ctx *ctx, fx_object **result)
} }
DISABLE_EXTENDED_LEXING(ctx); DISABLE_EXTENDED_LEXING(ctx);
*result = (array); *result = FX_VALUE_OBJECT(array);
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result parse_value(struct ctx *ctx, fx_object **result) static fx_result parse_value(struct ctx *ctx, fx_value *result)
{ {
struct token *tok = peek_token(ctx); struct token *tok = peek_token(ctx);
@@ -1948,9 +1897,13 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container)
} }
while (tok && tok->tok_type == TOK_DOT) { while (tok && tok->tok_type == TOK_DOT) {
const fx_value *child = fx_hashtable_get( const fx_value *child
container, = fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
&FX_VALUE_OBJECT(key)); if (child && !fx_value_is_type(child, FX_TYPE_HASHTABLE)) {
fx_string_unref(key);
return FX_RESULT_ERR(BAD_FORMAT);
}
fx_hashmap *subtable = NULL; fx_hashmap *subtable = NULL;
fx_value_get_object(child, &subtable); fx_value_get_object(child, &subtable);
if (!subtable) { if (!subtable) {
@@ -1959,18 +1912,13 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container)
container, container,
&FX_VALUE_OBJECT(key), &FX_VALUE_OBJECT(key),
&FX_VALUE_OBJECT(subtable)); &FX_VALUE_OBJECT(subtable));
} else if (
subtable
&& !fx_object_is_type(subtable, FX_TYPE_HASHTABLE)) {
free(key);
return FX_RESULT_ERR(BAD_FORMAT);
} }
#if 1 #if 1
enum object_flags flags = ctx_get_object_flags(ctx, subtable); enum object_flags flags = ctx_get_object_flags(ctx, subtable);
if (flags if (flags
& (OBJECT_KV_END_DEFINED | OBJECT_HEADER_END_DEFINED)) { & (OBJECT_KV_END_DEFINED | OBJECT_HEADER_END_DEFINED)) {
free(key); fx_string_unref(key);
return FX_RESULT_ERR(BAD_FORMAT); return FX_RESULT_ERR(BAD_FORMAT);
} }
#endif #endif
@@ -1980,7 +1928,7 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container)
advance_token(ctx); advance_token(ctx);
tok = peek_token(ctx); tok = peek_token(ctx);
if (!IS_VALID_KEY_COMPONENT(tok)) { if (!IS_VALID_KEY_COMPONENT(tok)) {
free(key); fx_string_unref(key);
return FX_RESULT_ERR(BAD_FORMAT); return FX_RESULT_ERR(BAD_FORMAT);
} }
@@ -2010,7 +1958,7 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container)
ENABLE_EXTENDED_LEXING(ctx); ENABLE_EXTENDED_LEXING(ctx);
advance_token(ctx); advance_token(ctx);
fx_object *value = NULL; fx_value value = FX_VALUE_EMPTY;
fx_result result = parse_value(ctx, &value); fx_result result = parse_value(ctx, &value);
DISABLE_EXTENDED_LEXING(ctx); DISABLE_EXTENDED_LEXING(ctx);
@@ -2025,17 +1973,17 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container)
return fx_result_propagate(result); return fx_result_propagate(result);
} }
fx_hashtable_put( fx_hashtable_put(container, &FX_VALUE_OBJECT(key), &value);
container,
&FX_VALUE_OBJECT(key),
&FX_VALUE_OBJECT(value));
fx_object_unref(value);
if (fx_object_is_type(value, FX_TYPE_HASHTABLE) if (fx_value_is_type(&value, FX_TYPE_HASHTABLE)
|| fx_object_is_type(value, FX_TYPE_ARRAY)) { || fx_value_is_type(&value, FX_TYPE_ARRAY)) {
ctx_set_object_flags(ctx, value, OBJECT_KV_END_DEFINED); ctx_set_object_flags(
ctx,
value.v_object,
OBJECT_KV_END_DEFINED);
} }
fx_value_unset(&value);
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
@@ -2062,26 +2010,24 @@ static fx_result parse_table_header(
} }
while (tok && tok->tok_type == TOK_DOT) { while (tok && tok->tok_type == TOK_DOT) {
const fx_value *value = fx_hashtable_get( const fx_value *value
container, = fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
&FX_VALUE_OBJECT(key));
fx_object *subtable = NULL; fx_object *subtable = NULL;
fx_value_get_object(value, &subtable); fx_value_get_object(value, &subtable);
enum object_flags flags = ctx_get_object_flags(ctx, subtable); enum object_flags flags = ctx_get_object_flags(ctx, subtable);
if (!subtable) { if (!value) {
subtable = (fx_hashtable_create()); subtable = (fx_hashtable_create());
fx_hashtable_put( fx_hashtable_put(
container, container,
&FX_VALUE_OBJECT(key), &FX_VALUE_OBJECT(key),
&FX_VALUE_OBJECT(subtable)); &FX_VALUE_OBJECT(subtable));
fx_object_unref(subtable); fx_object_unref(subtable);
} else if (fx_object_is_type(subtable, FX_TYPE_ARRAY)) { } else if (fx_value_is_type(value, FX_TYPE_ARRAY)) {
#if 0 value = fx_array_get_ref(
subtable = fx_array_at(
subtable, subtable,
fx_array_size(subtable) - 1); fx_array_get_size(subtable) - 1);
#endif fx_value_get_object(value, &subtable);
} else if (!fx_object_is_type(subtable, FX_TYPE_HASHTABLE)) { } else if (!fx_value_is_type(value, FX_TYPE_HASHTABLE)) {
return FX_RESULT_ERR(BAD_FORMAT); return FX_RESULT_ERR(BAD_FORMAT);
} }
@@ -2112,9 +2058,12 @@ static fx_result parse_table_header(
return FX_RESULT_ERR(BAD_FORMAT); return FX_RESULT_ERR(BAD_FORMAT);
} }
const fx_value *child = fx_hashtable_get( const fx_value *child
container, = fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
&FX_VALUE_OBJECT(key)); if (child && !fx_value_is_type(child, FX_TYPE_HASHTABLE)) {
return FX_RESULT_ERR(BAD_FORMAT);
}
fx_hashtable *new_table = NULL; fx_hashtable *new_table = NULL;
fx_value_get_object(child, &new_table); fx_value_get_object(child, &new_table);
@@ -2122,7 +2071,7 @@ static fx_result parse_table_header(
new_table = fx_hashtable_create(); new_table = fx_hashtable_create();
if (!new_table) { if (!new_table) {
free(key); fx_string_unref(key);
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
@@ -2175,26 +2124,29 @@ static fx_result parse_array_header(
} }
while (tok && tok->tok_type == TOK_DOT) { while (tok && tok->tok_type == TOK_DOT) {
const fx_value *child = fx_hashtable_get( const fx_value *child
container, = fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
&FX_VALUE_OBJECT(key));
fx_object *sub_dict = NULL; fx_object *sub_dict = NULL;
fx_value_get_object(child, &sub_dict); fx_value_get_object(child, &sub_dict);
if (!sub_dict) { enum object_flags flags = ctx_get_object_flags(ctx, (sub_dict));
if (flags & OBJECT_KV_END_DEFINED) {
return FX_RESULT_ERR(BAD_FORMAT);
}
if (!child) {
sub_dict = (fx_hashtable_create()); sub_dict = (fx_hashtable_create());
fx_hashtable_put( fx_hashtable_put(
container, container,
&FX_VALUE_OBJECT(key), &FX_VALUE_OBJECT(key),
&FX_VALUE_OBJECT(sub_dict)); &FX_VALUE_OBJECT(sub_dict));
fx_hashtable_unref(sub_dict); fx_hashtable_unref(sub_dict);
} else if (fx_object_is_type(sub_dict, FX_TYPE_ARRAY)) { } else if (fx_value_is_type(child, FX_TYPE_ARRAY)) {
#if 0 child = fx_array_get_ref(
sub_dict = fx_array_at(
sub_dict, sub_dict,
fx_array_size(sub_dict) - 1); fx_array_get_size(sub_dict) - 1);
#endif sub_dict = child->v_object;
} else if (!fx_object_is_type(sub_dict, FX_TYPE_HASHTABLE)) { } else if (!fx_value_is_type(child, FX_TYPE_HASHTABLE)) {
return FX_RESULT_ERR(BAD_FORMAT); return FX_RESULT_ERR(BAD_FORMAT);
} }
@@ -2219,9 +2171,12 @@ static fx_result parse_array_header(
return FX_RESULT_ERR(BAD_FORMAT); return FX_RESULT_ERR(BAD_FORMAT);
} }
const fx_value *child = fx_hashtable_get( const fx_value *child
container, = fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
&FX_VALUE_OBJECT(key)); if (child && !fx_value_is_type(child, FX_TYPE_ARRAY)) {
return FX_RESULT_ERR(BAD_FORMAT);
}
fx_array *array = NULL; fx_array *array = NULL;
fx_value_get_object(child, &array); fx_value_get_object(child, &array);
@@ -2234,11 +2189,11 @@ static fx_result parse_array_header(
} else if (!fx_object_is_type(array, FX_TYPE_ARRAY)) { } else if (!fx_object_is_type(array, FX_TYPE_ARRAY)) {
return FX_RESULT_ERR(BAD_FORMAT); return FX_RESULT_ERR(BAD_FORMAT);
} }
free(key); fx_string_unref(key);
enum object_flags flags = ctx_get_object_flags(ctx, (array)); enum object_flags flags = ctx_get_object_flags(ctx, (array));
if (flags & OBJECT_KV_END_DEFINED) { if (flags & OBJECT_KV_END_DEFINED) {
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(BAD_FORMAT);
} }
fx_hashtable *new_table = fx_hashtable_create(); fx_hashtable *new_table = fx_hashtable_create();
@@ -2246,9 +2201,7 @@ static fx_result parse_array_header(
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
#if 0 fx_array_push_back(array, FX_VALUE_OBJECT(new_table));
fx_array_append(array, FX_RV(new_table));
#endif
advance_token(ctx); advance_token(ctx);
*new_container = new_table; *new_container = new_table;
@@ -2330,7 +2283,7 @@ static fx_result parse_root(struct ctx *ctx, fx_hashtable **out)
static fx_result toml_deserialise( static fx_result toml_deserialise(
fx_serial_ctx *serial, fx_serial_ctx *serial,
fx_stream *src, fx_stream *src,
fx_object **dest, fx_value *dest,
enum fx_serial_flags flags) enum fx_serial_flags flags)
{ {
struct ctx ctx = {0}; struct ctx ctx = {0};
@@ -2351,7 +2304,7 @@ static fx_result toml_deserialise(
} }
if (ctx.ctx_flags & CTX_EOF) { if (ctx.ctx_flags & CTX_EOF) {
*dest = (fx_hashtable_create()); *dest = FX_VALUE_OBJECT(fx_hashtable_create());
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
@@ -2361,7 +2314,7 @@ static fx_result toml_deserialise(
return fx_result_propagate(ctx.ctx_result); return fx_result_propagate(ctx.ctx_result);
} }
*dest = (data); *dest = FX_VALUE_OBJECT(data);
#if 0 #if 0
ctx.ctx_flags ctx.ctx_flags
= CTX_ENABLE_NUMBERS | CTX_ENABLE_TIMESTAMPS | CTX_ENABLE_BOOLS; = CTX_ENABLE_NUMBERS | CTX_ENABLE_TIMESTAMPS | CTX_ENABLE_BOOLS;
+9 -1
View File
@@ -359,7 +359,7 @@ fx_status cstr_to_size(const char *in, size_t *out)
return FX_ERR_BAD_STATE; return FX_ERR_BAD_STATE;
} }
if (v > SIZE_T_MAX) { if (v > SIZE_MAX) {
return FX_ERR_BAD_STATE; return FX_ERR_BAD_STATE;
} }
@@ -369,6 +369,7 @@ fx_status cstr_to_size(const char *in, size_t *out)
fx_status cstr_to_float(const char *in, float *out) fx_status cstr_to_float(const char *in, float *out)
{ {
#if FX_ENABLE_FLOATING_POINT
char *ep; char *ep;
float v = strtof(in, &ep); float v = strtof(in, &ep);
if (*ep) { if (*ep) {
@@ -377,10 +378,14 @@ fx_status cstr_to_float(const char *in, float *out)
*out = v; *out = v;
return FX_SUCCESS; return FX_SUCCESS;
#else
return FX_ERR_NOT_SUPPORTED;
#endif
} }
fx_status cstr_to_double(const char *in, double *out) fx_status cstr_to_double(const char *in, double *out)
{ {
#if FX_ENABLE_FLOATING_POINT
char *ep; char *ep;
double v = strtod(in, &ep); double v = strtod(in, &ep);
if (*ep) { if (*ep) {
@@ -389,6 +394,9 @@ fx_status cstr_to_double(const char *in, double *out)
*out = v; *out = v;
return FX_SUCCESS; return FX_SUCCESS;
#else
return FX_ERR_NOT_SUPPORTED;
#endif
} }
fx_status cstr_to_cstr(const char *in, const char **out) fx_status cstr_to_cstr(const char *in, const char **out)
+4 -5
View File
@@ -1,4 +1,4 @@
#include <fx/collections/datetime.h> #include <fx/datetime.h>
#include <fx/stream.h> #include <fx/stream.h>
#include <fx/string.h> #include <fx/string.h>
#include <fx/value.h> #include <fx/value.h>
@@ -339,7 +339,7 @@ static enum fx_status encode_rfc3339(
dt->dt_sec); dt->dt_sec);
if (dt->dt_msec > 0) { if (dt->dt_msec > 0) {
fx_stream_write_fmt(out, NULL, ".%04ld", dt->dt_msec); fx_stream_write_fmt(out, NULL, ".%03ld", dt->dt_msec);
} }
if (!dt->dt_localtime) { if (!dt->dt_localtime) {
@@ -572,9 +572,8 @@ static fx_status _datetime_to_string(
fx_stream *out, fx_stream *out,
const char *format) const char *format)
{ {
struct fx_datetime_p *dt = fx_object_get_private( struct fx_datetime_p *dt
obj->v_object, = fx_object_get_private(obj->v_object, FX_TYPE_DATETIME);
FX_TYPE_DATETIME);
if (dt->dt_has_date) { if (dt->dt_has_date) {
fx_stream_write_fmt( fx_stream_write_fmt(
+1 -1
View File
@@ -3,7 +3,7 @@
bool fx_wchar_is_number(fx_wchar c) bool fx_wchar_is_number(fx_wchar c)
{ {
return iswdigit((wchar_t)c); return (c >= '0' && c <= '9');
} }
bool fx_wchar_is_alpha(fx_wchar c) bool fx_wchar_is_alpha(fx_wchar c)
+1
View File
@@ -94,6 +94,7 @@ FX_TYPE_DEFINITION_BEGIN(fx_double)
FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE);
FX_TYPE_IMPLEMENTS(FX_TYPE_CONVERTIBLE); FX_TYPE_IMPLEMENTS(FX_TYPE_CONVERTIBLE);
FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE); FX_TYPE_IMPLEMENTS(FX_TYPE_OPERABLE);
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
FX_TYPE_NAME("fx.double"); FX_TYPE_NAME("fx.double");
FX_TYPE_CLASS(fx_double_class); FX_TYPE_CLASS(fx_double_class);
FX_TYPE_INSTANCE_PRIVATE(double); FX_TYPE_INSTANCE_PRIVATE(double);
+5 -12
View File
@@ -10,15 +10,10 @@
FX_DECLS_BEGIN; FX_DECLS_BEGIN;
#define fx_foreach(var, iterator) \ #define fx_foreach(var, iterator) \
for (fx_value var = fx_iterator_get_value(iterator); \ for (const fx_value *var = fx_iterator_get_value(iterator); \
FX_OK(fx_iterator_get_status(iterator)); \ FX_OK(fx_iterator_get_status(iterator)); \
fx_iterator_move_next(iterator), \ fx_iterator_move_next(iterator), \
var = fx_iterator_get_value(iterator)) var = fx_iterator_get_value(iterator))
#define fx_foreach_ptr(type, var, iterator) \
for (type *var = (type *)fx_iterator_get_value(iterator).v_ptr; \
FX_OK(fx_iterator_get_status(iterator)); \
fx_iterator_move_next(iterator), \
var = (type *)fx_iterator_get_value(iterator).v_ptr)
#define FX_ITERATOR_VALUE_INT(v) ((fx_iterator_value) {.v_int = (v)}) #define FX_ITERATOR_VALUE_INT(v) ((fx_iterator_value) {.v_int = (v)})
#define FX_ITERATOR_VALUE_PTR(v) ((fx_iterator_value) {.v_ptr = (v)}) #define FX_ITERATOR_VALUE_PTR(v) ((fx_iterator_value) {.v_ptr = (v)})
@@ -36,12 +31,11 @@ FX_DECLARE_TYPE(fx_iterable);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iterator) FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iterator)
fx_status (*it_move_next)(const fx_iterator *); fx_status (*it_move_next)(const fx_iterator *);
fx_status (*it_erase)(fx_iterator *); fx_status (*it_erase)(fx_iterator *);
fx_value (*it_get_value)(const fx_iterator *); const fx_value *(*it_get_value)(const fx_iterator *);
FX_TYPE_CLASS_DECLARATION_END(fx_iterator) FX_TYPE_CLASS_DECLARATION_END(fx_iterator)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iterable) FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iterable)
fx_iterator *(*it_begin)(fx_iterable *); const fx_iterator *(*it_begin)(const fx_iterable *);
const fx_iterator *(*it_cbegin)(const fx_iterable *);
FX_TYPE_CLASS_DECLARATION_END(fx_iterable) FX_TYPE_CLASS_DECLARATION_END(fx_iterable)
FX_API fx_type_id fx_iterator_get_type(void); FX_API fx_type_id fx_iterator_get_type(void);
@@ -56,15 +50,14 @@ static inline void fx_iterator_unref(const fx_iterator *p)
fx_object_unref((fx_object *)p); fx_object_unref((fx_object *)p);
} }
FX_API fx_iterator *fx_iterator_begin(fx_iterable *it); FX_API const fx_iterator *fx_iterator_begin(const fx_iterable *it);
FX_API const fx_iterator *fx_iterator_cbegin(const fx_iterable *it);
FX_API fx_status fx_iterator_get_status(const fx_iterator *it); FX_API fx_status fx_iterator_get_status(const fx_iterator *it);
FX_API fx_status FX_API fx_status
fx_iterator_set_status(const fx_iterator *it, fx_status status); fx_iterator_set_status(const fx_iterator *it, fx_status status);
FX_API fx_status fx_iterator_move_next(const fx_iterator *it); FX_API fx_status fx_iterator_move_next(const fx_iterator *it);
FX_API fx_value fx_iterator_get_value(const fx_iterator *it); FX_API const fx_value *fx_iterator_get_value(const fx_iterator *it);
FX_API fx_status fx_iterator_erase(fx_iterator *it); FX_API fx_status fx_iterator_erase(fx_iterator *it);
static inline bool fx_iterator_is_valid(const fx_iterator *it) static inline bool fx_iterator_is_valid(const fx_iterator *it)
{ {
+4 -1
View File
@@ -26,7 +26,10 @@ typedef struct fx_namemap_entry {
const char *e_name; const char *e_name;
} fx_namemap_entry; } fx_namemap_entry;
FX_API fx_status fx_namemap_cleanup(fx_namemap *map); typedef void (*fx_namemap_cleanup_callback)(fx_namemap_entry *);
FX_API fx_status
fx_namemap_cleanup(fx_namemap *map, fx_namemap_cleanup_callback callback);
FX_API fx_status FX_API fx_status
fx_namemap_put(fx_namemap *map, const char *name, fx_namemap_entry *entry); fx_namemap_put(fx_namemap *map, const char *name, fx_namemap_entry *entry);
+5
View File
@@ -45,6 +45,11 @@
#define FX_WSTR(v) __FX_VALUE_CREATE(WSTR, v_wstr, v) #define FX_WSTR(v) __FX_VALUE_CREATE(WSTR, v_wstr, v)
#define FX_CSTR(v) __FX_VALUE_CREATE(CSTR, v_cstr, v) #define FX_CSTR(v) __FX_VALUE_CREATE(CSTR, v_cstr, v)
#define FX_POINTER(v) __FX_VALUE_CREATE(POINTER, v_pointer, v) #define FX_POINTER(v) __FX_VALUE_CREATE(POINTER, v_pointer, v)
#define FX_VALUE_OBJECT_REF(v) \
((fx_value) { \
.v_type = fx_object_query_type((fx_object *)v), \
.v_object = fx_object_ref((fx_object *)(v)), \
})
#define FX_VALUE_OBJECT(v) \ #define FX_VALUE_OBJECT(v) \
((fx_value) { \ ((fx_value) { \
.v_type = fx_object_query_type((fx_object *)v), \ .v_type = fx_object_query_type((fx_object *)v), \
+11 -19
View File
@@ -23,7 +23,7 @@ static enum fx_status iterator_set_status(
/*** PUBLIC FUNCTIONS *********************************************************/ /*** PUBLIC FUNCTIONS *********************************************************/
fx_iterator *fx_iterator_begin(fx_iterable *it) const fx_iterator *fx_iterator_begin(const fx_iterable *it)
{ {
FX_CLASS_DISPATCH_VIRTUAL_0( FX_CLASS_DISPATCH_VIRTUAL_0(
fx_iterable, fx_iterable,
@@ -33,16 +33,6 @@ fx_iterator *fx_iterator_begin(fx_iterable *it)
it); it);
} }
const fx_iterator *fx_iterator_cbegin(const fx_iterable *it)
{
FX_CLASS_DISPATCH_VIRTUAL_0(
fx_iterable,
FX_TYPE_ITERABLE,
NULL,
it_cbegin,
it);
}
enum fx_status fx_iterator_get_status(const fx_iterator *it) enum fx_status fx_iterator_get_status(const fx_iterator *it)
{ {
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ITERATOR, iterator_get_status, it); FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ITERATOR, iterator_get_status, it);
@@ -61,9 +51,8 @@ enum fx_status fx_iterator_move_next(const fx_iterator *it)
{ {
enum fx_status status = FX_ERR_NOT_SUPPORTED; enum fx_status status = FX_ERR_NOT_SUPPORTED;
fx_iterator_class *iface = fx_object_get_interface( fx_iterator_class *iface
it, = fx_object_get_interface(it, FX_TYPE_ITERATOR);
FX_TYPE_ITERATOR);
if (iface && iface->it_move_next) { if (iface && iface->it_move_next) {
status = iface->it_move_next(it); status = iface->it_move_next(it);
} }
@@ -74,12 +63,16 @@ enum fx_status fx_iterator_move_next(const fx_iterator *it)
return status; return status;
} }
fx_value fx_iterator_get_value(const fx_iterator *it) const fx_value *fx_iterator_get_value(const fx_iterator *it)
{ {
if (fx_iterator_get_status(it) != FX_SUCCESS) {
return NULL;
}
FX_CLASS_DISPATCH_VIRTUAL_0( FX_CLASS_DISPATCH_VIRTUAL_0(
fx_iterator, fx_iterator,
FX_TYPE_ITERATOR, FX_TYPE_ITERATOR,
FX_VALUE_EMPTY, NULL,
it_get_value, it_get_value,
it); it);
} }
@@ -88,9 +81,8 @@ fx_status fx_iterator_erase(fx_iterator *it)
{ {
enum fx_status status = FX_ERR_NOT_SUPPORTED; enum fx_status status = FX_ERR_NOT_SUPPORTED;
fx_iterator_class *iface = fx_object_get_interface( fx_iterator_class *iface
it, = fx_object_get_interface(it, FX_TYPE_ITERATOR);
FX_TYPE_ITERATOR);
if (iface && iface->it_erase) { if (iface && iface->it_erase) {
status = iface->it_erase(it); status = iface->it_erase(it);
} }
+63 -29
View File
@@ -45,8 +45,50 @@ static struct map_bucket *map_item_convert_to_bucket(
return bucket; return bucket;
} }
enum fx_status fx_namemap_cleanup(struct fx_namemap *map) static void cleanup_bucket(
struct map_bucket *bucket,
fx_namemap_cleanup_callback callback)
{ {
fx_queue_entry *cur = fx_queue_pop_front(&bucket->b_items);
while (cur) {
struct __fx_namemap_entry *entry
= fx_unbox(struct __fx_namemap_entry, cur, e_entry);
struct fx_namemap_entry *item
= (struct fx_namemap_entry *)entry;
if (callback) {
callback(item);
}
cur = fx_queue_pop_front(&bucket->b_items);
}
free(bucket);
}
enum fx_status fx_namemap_cleanup(
struct fx_namemap *map,
fx_namemap_cleanup_callback callback)
{
fx_bst_node *cur_node = fx_bst_first(&map->m_entries);
while (cur_node) {
struct __fx_namemap_entry *entry
= fx_unbox(struct __fx_namemap_entry, cur_node, e_node);
fx_bst_delete(&map->m_entries, cur_node);
if (entry->e_flags == MAP_ENTRY_BUCKET) {
struct map_bucket *bucket = (struct map_bucket *)entry;
cleanup_bucket(bucket, callback);
} else {
struct fx_namemap_entry *item
= (struct fx_namemap_entry *)entry;
if (callback) {
callback(item);
}
}
cur_node = fx_bst_first(&map->m_entries);
}
return FX_SUCCESS; return FX_SUCCESS;
} }
@@ -137,10 +179,8 @@ fx_namemap_entry *fx_namemap_get(const fx_namemap *map, const char *name)
struct fx_namemap_entry *fx_namemap_first(const struct fx_namemap *map) struct fx_namemap_entry *fx_namemap_first(const struct fx_namemap *map)
{ {
struct fx_bst_node *first = fx_bst_first(&map->m_entries); struct fx_bst_node *first = fx_bst_first(&map->m_entries);
struct __fx_namemap_entry *entry = fx_unbox( struct __fx_namemap_entry *entry
struct __fx_namemap_entry, = fx_unbox(struct __fx_namemap_entry, first, e_node);
first,
e_node);
if (!entry) { if (!entry) {
return NULL; return NULL;
} }
@@ -158,10 +198,8 @@ struct fx_namemap_entry *fx_namemap_first(const struct fx_namemap *map)
struct fx_namemap_entry *fx_namemap_last(const struct fx_namemap *map) struct fx_namemap_entry *fx_namemap_last(const struct fx_namemap *map)
{ {
struct fx_bst_node *last = fx_bst_last(&map->m_entries); struct fx_bst_node *last = fx_bst_last(&map->m_entries);
struct __fx_namemap_entry *entry = fx_unbox( struct __fx_namemap_entry *entry
struct __fx_namemap_entry, = fx_unbox(struct __fx_namemap_entry, last, e_node);
last,
e_node);
if (entry->e_flags == MAP_ENTRY_ITEM) { if (entry->e_flags == MAP_ENTRY_ITEM) {
return (struct fx_namemap_entry *)entry; return (struct fx_namemap_entry *)entry;
} }
@@ -179,11 +217,11 @@ struct fx_namemap_entry *fx_namemap_next(
struct map_bucket *bucket = entry->e_opaque.e_bucket; struct map_bucket *bucket = entry->e_opaque.e_bucket;
struct __fx_namemap_entry *next_entry = NULL; struct __fx_namemap_entry *next_entry = NULL;
if (bucket) { if (bucket) {
struct fx_queue_entry *q_entry = fx_queue_next( struct fx_queue_entry *q_entry
&entry->e_opaque.e_entry); = fx_queue_next(&entry->e_opaque.e_entry);
if (!q_entry) { if (!q_entry) {
struct fx_bst_node *node = fx_bst_next( struct fx_bst_node *node
&bucket->b_entry.e_node); = fx_bst_next(&bucket->b_entry.e_node);
next_entry = fx_unbox( next_entry = fx_unbox(
struct __fx_namemap_entry, struct __fx_namemap_entry,
node, node,
@@ -205,12 +243,10 @@ struct fx_namemap_entry *fx_namemap_next(
if (next_entry->e_flags == MAP_ENTRY_BUCKET) { if (next_entry->e_flags == MAP_ENTRY_BUCKET) {
bucket = (struct map_bucket *)next_entry; bucket = (struct map_bucket *)next_entry;
struct fx_queue_entry *q_entry = fx_queue_first( struct fx_queue_entry *q_entry
&bucket->b_items); = fx_queue_first(&bucket->b_items);
next_entry = fx_unbox( next_entry
struct __fx_namemap_entry, = fx_unbox(struct __fx_namemap_entry, q_entry, e_entry);
q_entry,
e_entry);
} }
return (struct fx_namemap_entry *)next_entry; return (struct fx_namemap_entry *)next_entry;
@@ -223,11 +259,11 @@ struct fx_namemap_entry *fx_namemap_prev(
struct map_bucket *bucket = entry->e_opaque.e_bucket; struct map_bucket *bucket = entry->e_opaque.e_bucket;
struct __fx_namemap_entry *prev_entry = NULL; struct __fx_namemap_entry *prev_entry = NULL;
if (bucket) { if (bucket) {
struct fx_queue_entry *q_entry = fx_queue_prev( struct fx_queue_entry *q_entry
&entry->e_opaque.e_entry); = fx_queue_prev(&entry->e_opaque.e_entry);
if (!q_entry) { if (!q_entry) {
struct fx_bst_node *node = fx_bst_prev( struct fx_bst_node *node
&bucket->b_entry.e_node); = fx_bst_prev(&bucket->b_entry.e_node);
prev_entry = fx_unbox( prev_entry = fx_unbox(
struct __fx_namemap_entry, struct __fx_namemap_entry,
node, node,
@@ -249,12 +285,10 @@ struct fx_namemap_entry *fx_namemap_prev(
if (prev_entry->e_flags == MAP_ENTRY_BUCKET) { if (prev_entry->e_flags == MAP_ENTRY_BUCKET) {
bucket = (struct map_bucket *)prev_entry; bucket = (struct map_bucket *)prev_entry;
struct fx_queue_entry *q_entry = fx_queue_last( struct fx_queue_entry *q_entry
&bucket->b_items); = fx_queue_last(&bucket->b_items);
prev_entry = fx_unbox( prev_entry
struct __fx_namemap_entry, = fx_unbox(struct __fx_namemap_entry, q_entry, e_entry);
q_entry,
e_entry);
} }
return (struct fx_namemap_entry *)prev_entry; return (struct fx_namemap_entry *)prev_entry;
+23 -27
View File
@@ -41,16 +41,14 @@ fx_result fx_object_instantiate(
out->obj_type = type; out->obj_type = type;
out->obj_ref = 1; out->obj_ref = 1;
struct fx_queue_entry *entry = fx_queue_first( struct fx_queue_entry *entry
&type->ty_class_hierarchy); = fx_queue_first(&type->ty_class_hierarchy);
while (entry) { while (entry) {
struct fx_type_component *comp = fx_unbox( struct fx_type_component *comp
struct fx_type_component, = fx_unbox(struct fx_type_component, entry, c_entry);
entry,
c_entry);
const struct fx_type_info *class_info = comp->c_type; const struct fx_type_info *class_info = comp->c_type;
void *private_data = (char *)out void *private_data
+ comp->c_instance_private_data_offset; = (char *)out + comp->c_instance_private_data_offset;
if (class_info->ty_instance_init) { if (class_info->ty_instance_init) {
class_info->ty_instance_init(out, private_data); class_info->ty_instance_init(out, private_data);
@@ -90,7 +88,7 @@ fx_status fx_object_to_string(
fx_stream *out, fx_stream *out,
const char *format) const char *format)
{ {
fx_value value = FX_VALUE_OBJECT(p); fx_value value = FX_VALUE_OBJECT_REF(p);
fx_object_class *iface = fx_object_get_interface(p, FX_TYPE_OBJECT); fx_object_class *iface = fx_object_get_interface(p, FX_TYPE_OBJECT);
if (iface && iface->to_string) { if (iface && iface->to_string) {
return iface->to_string(&value, out, format); return iface->to_string(&value, out, format);
@@ -102,13 +100,16 @@ fx_status fx_object_to_string(
bool fx_object_is_type(const struct _fx_object *p, fx_type_id type) bool fx_object_is_type(const struct _fx_object *p, fx_type_id type)
{ {
if (!p) {
return false;
}
if (fx_type_id_compare(&p->obj_type->ty_id, type) == 0) { if (fx_type_id_compare(&p->obj_type->ty_id, type) == 0) {
return true; return true;
} }
struct fx_type_component *comp = fx_type_get_component( struct fx_type_component *comp
&p->obj_type->ty_components, = fx_type_get_component(&p->obj_type->ty_components, type);
type);
return comp != NULL; return comp != NULL;
} }
@@ -125,9 +126,8 @@ void *fx_object_get_private(const struct _fx_object *object, fx_type_id type)
return (char *)object + object->obj_main_priv_offset; return (char *)object + object->obj_main_priv_offset;
} }
struct fx_type_component *comp = fx_type_get_component( struct fx_type_component *comp
&object->obj_type->ty_components, = fx_type_get_component(&object->obj_type->ty_components, type);
type);
if (!comp) { if (!comp) {
return NULL; return NULL;
} }
@@ -143,9 +143,8 @@ void *fx_object_get_protected(const struct _fx_object *object, fx_type_id type)
assert(object->obj_magic == FX_OBJECT_MAGIC); assert(object->obj_magic == FX_OBJECT_MAGIC);
struct fx_type_component *comp = fx_type_get_component( struct fx_type_component *comp
&object->obj_type->ty_components, = fx_type_get_component(&object->obj_type->ty_components, type);
type);
if (!comp) { if (!comp) {
return NULL; return NULL;
} }
@@ -177,9 +176,8 @@ enum fx_status fx_object_get_data(
assert(object->obj_magic == FX_OBJECT_MAGIC); assert(object->obj_magic == FX_OBJECT_MAGIC);
struct fx_type_component *comp = fx_type_get_component( struct fx_type_component *comp
&object->obj_type->ty_components, = fx_type_get_component(&object->obj_type->ty_components, type);
type);
if (!comp) { if (!comp) {
return FX_ERR_INVALID_ARGUMENT; return FX_ERR_INVALID_ARGUMENT;
} }
@@ -218,14 +216,12 @@ void fx_object_unref(struct _fx_object *p)
struct fx_queue_entry *cur = fx_queue_last(&type->ty_class_hierarchy); struct fx_queue_entry *cur = fx_queue_last(&type->ty_class_hierarchy);
while (cur) { while (cur) {
struct fx_type_component *comp = fx_unbox( struct fx_type_component *comp
struct fx_type_component, = fx_unbox(struct fx_type_component, cur, c_entry);
cur,
c_entry);
const struct fx_type_info *class_info = comp->c_type; const struct fx_type_info *class_info = comp->c_type;
void *private_data = (char *)p void *private_data
+ comp->c_instance_private_data_offset; = (char *)p + comp->c_instance_private_data_offset;
if (class_info->ty_instance_fini) { if (class_info->ty_instance_fini) {
class_info->ty_instance_fini(p, private_data); class_info->ty_instance_fini(p, private_data);
+34
View File
@@ -1,17 +1,51 @@
#include <fx/comparable.h>
#include <fx/pointer.h> #include <fx/pointer.h>
#include <fx/value-type.h> #include <fx/value-type.h>
#include <fx/value.h>
static fx_status hash(const fx_value *v, uint64_t *out_hash)
{
*out_hash = (uint64_t)v->v_pointer;
return FX_SUCCESS;
}
static i32 compare(const fx_value *left, const fx_value *right)
{
void *left_v = left->v_pointer;
void *right_v = 0;
if (!FX_OK(fx_value_get_pointer(right, &right_v))) {
return -1;
}
uintptr_t left_p = (uintptr_t)left_v;
uintptr_t right_p = (uintptr_t)right_v;
if (left_p < right_p) {
return -1;
} else if (left_p > right_p) {
return 1;
} else {
return 0;
}
}
/*** CLASS DEFINITION *********************************************************/ /*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_BEGIN(fx_pointer) FX_TYPE_CLASS_BEGIN(fx_pointer)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL; FX_INTERFACE_ENTRY(to_string) = NULL;
FX_INTERFACE_ENTRY(hash) = hash;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_comparable, FX_TYPE_COMPARABLE)
FX_INTERFACE_ENTRY(c_compare) = compare;
FX_TYPE_VTABLE_INTERFACE_END(fx_comparable, FX_TYPE_COMPARABLE)
FX_TYPE_CLASS_END(fx_pointer) FX_TYPE_CLASS_END(fx_pointer)
FX_TYPE_DEFINITION_BEGIN(fx_pointer) FX_TYPE_DEFINITION_BEGIN(fx_pointer)
__FX_VALUE_TYPE_ID(POINTER); __FX_VALUE_TYPE_ID(POINTER);
FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE);
FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE);
FX_TYPE_NAME("fx.pointer"); FX_TYPE_NAME("fx.pointer");
FX_TYPE_CLASS(fx_pointer_class); FX_TYPE_CLASS(fx_pointer_class);
FX_TYPE_INSTANCE_PRIVATE(void *); FX_TYPE_INSTANCE_PRIVATE(void *);
+35 -40
View File
@@ -101,7 +101,7 @@
// Default precision for the floating point conversion specifiers (the C // Default precision for the floating point conversion specifiers (the C
// standard sets this at 6) // standard sets this at 6)
#ifndef PRINTF_DEFAULT_FLOAT_PRECISION #ifndef PRINTF_DEFAULT_FLOAT_PRECISION
#define PRINTF_DEFAULT_FLOAT_PRECISION 6 #define PRINTF_DEFAULT_FLOAT_PRECISION 13
#endif #endif
// Default choice of type to use for internal floating-point computations // Default choice of type to use for internal floating-point computations
@@ -623,8 +623,8 @@ static void print_integer(
} else { } else {
do { do {
const char digit = (char)(value % base); const char digit = (char)(value % base);
buf[len++] = (char)(digit < 10 buf[len++]
? '0' + digit = (char)(digit < 10 ? '0' + digit
: (flags & FLAGS_UPPERCASE : (flags & FLAGS_UPPERCASE
? 'A' ? 'A'
: 'a') : 'a')
@@ -698,15 +698,14 @@ static struct floating_point_components get_components(
number_.is_negative = get_sign_bit(number); number_.is_negative = get_sign_bit(number);
floating_point_t abs_number = (number_.is_negative) ? -number : number; floating_point_t abs_number = (number_.is_negative) ? -number : number;
number_.integral = (int_fast64_t)abs_number; number_.integral = (int_fast64_t)abs_number;
floating_point_t scaled_remainder = (abs_number floating_point_t scaled_remainder
- (floating_point_t) = (abs_number - (floating_point_t)number_.integral)
number_.integral)
* powers_of_10[precision]; * powers_of_10[precision];
number_.fractional = (int_fast64_t) number_.fractional = (int_fast64_t)
scaled_remainder; // for precision == 0U, this will be 0 scaled_remainder; // for precision == 0U, this will be 0
floating_point_t remainder = scaled_remainder floating_point_t remainder
- (floating_point_t)number_.fractional; = scaled_remainder - (floating_point_t)number_.fractional;
const floating_point_t one_half = (floating_point_t)0.5; const floating_point_t one_half = (floating_point_t)0.5;
if (remainder > one_half) { if (remainder > one_half) {
@@ -779,18 +778,18 @@ static struct scaling_factor update_normalization(
result.raw_factor = sf.raw_factor * extra_multiplicative_factor; result.raw_factor = sf.raw_factor * extra_multiplicative_factor;
} else { } else {
int factor_exp2 = get_exp2(get_bit_access(sf.raw_factor)); int factor_exp2 = get_exp2(get_bit_access(sf.raw_factor));
int extra_factor_exp2 = get_exp2( int extra_factor_exp2
get_bit_access(extra_multiplicative_factor)); = get_exp2(get_bit_access(extra_multiplicative_factor));
// Divide the larger-exponent raw raw_factor by the smaller // Divide the larger-exponent raw raw_factor by the smaller
if (PRINTF_ABS(factor_exp2) > PRINTF_ABS(extra_factor_exp2)) { if (PRINTF_ABS(factor_exp2) > PRINTF_ABS(extra_factor_exp2)) {
result.multiply = false; result.multiply = false;
result.raw_factor = sf.raw_factor result.raw_factor
/ extra_multiplicative_factor; = sf.raw_factor / extra_multiplicative_factor;
} else { } else {
result.multiply = true; result.multiply = true;
result.raw_factor = extra_multiplicative_factor result.raw_factor
/ sf.raw_factor; = extra_multiplicative_factor / sf.raw_factor;
} }
} }
return result; return result;
@@ -818,25 +817,24 @@ static struct floating_point_components get_normalized_components(
return get_components(negative ? -scaled : scaled, precision); return get_components(negative ? -scaled : scaled, precision);
} }
components.integral = (int_fast64_t)scaled; components.integral = (int_fast64_t)scaled;
floating_point_t remainder = non_normalized floating_point_t remainder
= non_normalized
- unapply_scaling( - unapply_scaling(
(floating_point_t) (floating_point_t)components.integral,
components.integral,
normalization); normalization);
floating_point_t prec_power_of_10 = powers_of_10[precision]; floating_point_t prec_power_of_10 = powers_of_10[precision];
struct scaling_factor account_for_precision = update_normalization( struct scaling_factor account_for_precision
normalization, = update_normalization(normalization, prec_power_of_10);
prec_power_of_10); floating_point_t scaled_remainder
floating_point_t scaled_remainder = apply_scaling( = apply_scaling(remainder, account_for_precision);
remainder,
account_for_precision);
floating_point_t rounding_threshold = 0.5; floating_point_t rounding_threshold = 0.5;
components.fractional = (int_fast64_t) components.fractional = (int_fast64_t)
scaled_remainder; // when precision == 0, the assigned value scaled_remainder; // when precision == 0, the assigned value
// should be 0 // should be 0
scaled_remainder -= (floating_point_t)components scaled_remainder
.fractional; // when precision == 0, this -= (floating_point_t)
components.fractional; // when precision == 0, this
// will not change // will not change
// scaled_remainder // scaled_remainder
@@ -960,9 +958,8 @@ static void print_decimal_number(
char *buf, char *buf,
printf_size_t len) printf_size_t len)
{ {
struct floating_point_components value_ = get_components( struct floating_point_components value_
number, = get_components(number, precision);
precision);
print_broken_up_decimal( print_broken_up_decimal(
value_, value_,
output, output,
@@ -1105,9 +1102,9 @@ static void print_exponential_number(
abs_exp10_covered_by_powers_table abs_exp10_covered_by_powers_table
= PRINTF_ABS(floored_exp10) = PRINTF_ABS(floored_exp10)
< PRINTF_MAX_PRECOMPUTED_POWER_OF_10; < PRINTF_MAX_PRECOMPUTED_POWER_OF_10;
normalization.raw_factor = abs_exp10_covered_by_powers_table normalization.raw_factor
? powers_of_10[PRINTF_ABS( = abs_exp10_covered_by_powers_table
floored_exp10)] ? powers_of_10[PRINTF_ABS(floored_exp10)]
: p10; : p10;
} }
@@ -1120,9 +1117,8 @@ static void print_exponential_number(
bool fall_back_to_decimal_only_mode = false; bool fall_back_to_decimal_only_mode = false;
if (flags & FLAGS_ADAPT_EXP) { if (flags & FLAGS_ADAPT_EXP) {
int required_significant_digits = (precision == 0) int required_significant_digits
? 1 = (precision == 0) ? 1 : (int)precision;
: (int)precision;
// Should we want to fall-back to "%f" mode, and only print the // Should we want to fall-back to "%f" mode, and only print the
// decimal part? // decimal part?
fall_back_to_decimal_only_mode fall_back_to_decimal_only_mode
@@ -1133,10 +1129,10 @@ static void print_exponential_number(
// "%g" mode, "precision" is the number of _significant digits_, // "%g" mode, "precision" is the number of _significant digits_,
// and this is when we "translate" the precision value to an // and this is when we "translate" the precision value to an
// actual number of decimal digits. // actual number of decimal digits.
int precision_ = fall_back_to_decimal_only_mode int precision_
= fall_back_to_decimal_only_mode
? (int)precision - 1 - floored_exp10 ? (int)precision - 1 - floored_exp10
: (int)precision : (int)precision - 1; // the presence of the
- 1; // the presence of the
// exponent ensures only // exponent ensures only
// one significant digit // one significant digit
// comes before the // comes before the
@@ -1573,9 +1569,8 @@ static inline void format_string_loop(
if (flags & FLAGS_LONG_LONG) { if (flags & FLAGS_LONG_LONG) {
#if PRINTF_SUPPORT_LONG_LONG #if PRINTF_SUPPORT_LONG_LONG
const long long value = va_arg( const long long value
args, = va_arg(args, long long);
long long);
print_integer( print_integer(
output, output,
ABS_FOR_PRINTING(value), ABS_FOR_PRINTING(value),
+55 -75
View File
@@ -68,6 +68,7 @@ struct fx_string_iterator_p {
const char *string_value; const char *string_value;
size_t string_length; size_t string_length;
size_t string_codepoints; size_t string_codepoints;
fx_value value;
}; };
/*** PRIVATE FUNCTIONS ********************************************************/ /*** PRIVATE FUNCTIONS ********************************************************/
@@ -293,9 +294,8 @@ static fx_string *string_duplicate(const struct fx_string_p *str)
return NULL; return NULL;
} }
struct fx_string_p *new_str_p = fx_object_get_private( struct fx_string_p *new_str_p
new_str, = fx_object_get_private(new_str, FX_TYPE_STRING);
FX_TYPE_STRING);
string_change_capacity(new_str_p, str->s_len); string_change_capacity(new_str_p, str->s_len);
const char *src = string_ptr(str); const char *src = string_ptr(str);
@@ -397,9 +397,8 @@ static enum fx_status replace_utf8(
} }
size_t new_data_nr_bytes = strlen(new_data); size_t new_data_nr_bytes = strlen(new_data);
size_t new_data_nr_codepoints = fx_wchar_utf8_codepoint_count( size_t new_data_nr_codepoints
new_data, = fx_wchar_utf8_codepoint_count(new_data, new_data_nr_bytes);
new_data_nr_bytes);
if (new_data_nr_codepoints == 0) { if (new_data_nr_codepoints == 0) {
/* new_data is not a valid utf-8 string */ /* new_data is not a valid utf-8 string */
return FX_ERR_INVALID_ARGUMENT; return FX_ERR_INVALID_ARGUMENT;
@@ -417,8 +416,8 @@ static enum fx_status replace_utf8(
return status; return status;
} }
size_t new_total_bytes = str->s_len - old_data_nr_bytes size_t new_total_bytes
+ new_data_nr_bytes; = str->s_len - old_data_nr_bytes + new_data_nr_bytes;
if (new_total_bytes > str->s_max) { if (new_total_bytes > str->s_max) {
status = string_reserve(str, new_total_bytes); status = string_reserve(str, new_total_bytes);
} }
@@ -747,9 +746,8 @@ static enum fx_status string_insert_wstr_ansi(
at = dest->s_len; at = dest->s_len;
} }
size_t utf8_encoded_size = fx_wchar_utf8_string_encoded_size( size_t utf8_encoded_size
src, = fx_wchar_utf8_string_encoded_size(src, nr_codepoints);
nr_codepoints);
if (utf8_encoded_size == 0) { if (utf8_encoded_size == 0) {
return FX_ERR_INVALID_ARGUMENT; return FX_ERR_INVALID_ARGUMENT;
} }
@@ -796,9 +794,8 @@ static enum fx_status string_insert_wstr_utf8(
codepoint_offset = dest->s_codepoints; codepoint_offset = dest->s_codepoints;
} }
size_t utf8_encoded_size = fx_wchar_utf8_string_encoded_size( size_t utf8_encoded_size
src, = fx_wchar_utf8_string_encoded_size(src, nr_codepoints);
nr_codepoints);
if (utf8_encoded_size == 0) { if (utf8_encoded_size == 0) {
return FX_ERR_INVALID_ARGUMENT; return FX_ERR_INVALID_ARGUMENT;
} }
@@ -1041,9 +1038,8 @@ static fx_iterator *string_tokenise(
} }
fx_string_iterator *it_obj = fx_object_create(FX_TYPE_STRING_ITERATOR); fx_string_iterator *it_obj = fx_object_create(FX_TYPE_STRING_ITERATOR);
struct fx_string_iterator_p *it = fx_object_get_private( struct fx_string_iterator_p *it
it_obj, = fx_object_get_private(it_obj, FX_TYPE_STRING_ITERATOR);
FX_TYPE_STRING_ITERATOR);
it->_m = ITERATOR_MODE_TOKENS; it->_m = ITERATOR_MODE_TOKENS;
it->_d = delims; it->_d = delims;
@@ -1152,9 +1148,8 @@ static fx_string *string_substr(
} }
fx_string *newstr = fx_string_create(); fx_string *newstr = fx_string_create();
struct fx_string_p *newstr_p = fx_object_get_private( struct fx_string_p *newstr_p
newstr, = fx_object_get_private(newstr, FX_TYPE_STRING);
FX_TYPE_STRING);
string_reserve(newstr_p, len); string_reserve(newstr_p, len);
const char *src = string_ptr(str) + start; const char *src = string_ptr(str) + start;
@@ -1304,12 +1299,10 @@ enum fx_status fx_string_insert_s(
const fx_string *src, const fx_string *src,
size_t at) size_t at)
{ {
struct fx_string_p *dest_p = fx_object_get_private( struct fx_string_p *dest_p
dest, = fx_object_get_private(dest, FX_TYPE_STRING);
FX_TYPE_STRING); const struct fx_string_p *src_p
const struct fx_string_p *src_p = fx_object_get_private( = fx_object_get_private(src, FX_TYPE_STRING);
src,
FX_TYPE_STRING);
return string_insert_s(dest_p, src_p, at); return string_insert_s(dest_p, src_p, at);
} }
@@ -1318,9 +1311,8 @@ enum fx_status fx_string_insert_cstr(
const char *src, const char *src,
size_t at) size_t at)
{ {
struct fx_string_p *dest_p = fx_object_get_private( struct fx_string_p *dest_p
dest, = fx_object_get_private(dest, FX_TYPE_STRING);
FX_TYPE_STRING);
return string_insert_cstr(dest_p, src, strlen(src), at); return string_insert_cstr(dest_p, src, strlen(src), at);
} }
@@ -1329,9 +1321,8 @@ enum fx_status fx_string_insert_wstr(
const fx_wchar *src, const fx_wchar *src,
size_t at) size_t at)
{ {
struct fx_string_p *dest_p = fx_object_get_private( struct fx_string_p *dest_p
dest, = fx_object_get_private(dest, FX_TYPE_STRING);
FX_TYPE_STRING);
return string_insert_wstr(dest_p, src, fx_wstrlen(src), at); return string_insert_wstr(dest_p, src, fx_wstrlen(src), at);
} }
@@ -1341,9 +1332,8 @@ enum fx_status fx_string_insert_cstrf(
const char *format, const char *format,
...) ...)
{ {
struct fx_string_p *dest_p = fx_object_get_private( struct fx_string_p *dest_p
dest, = fx_object_get_private(dest, FX_TYPE_STRING);
FX_TYPE_STRING);
va_list arg; va_list arg;
va_start(arg, format); va_start(arg, format);
@@ -1370,9 +1360,8 @@ enum fx_status fx_string_insert_cstrn(
enum fx_status fx_string_append_cstrf(fx_string *dest, const char *format, ...) enum fx_status fx_string_append_cstrf(fx_string *dest, const char *format, ...)
{ {
struct fx_string_p *dest_p = fx_object_get_private( struct fx_string_p *dest_p
dest, = fx_object_get_private(dest, FX_TYPE_STRING);
FX_TYPE_STRING);
va_list arg; va_list arg;
va_start(arg, format); va_start(arg, format);
@@ -1384,9 +1373,8 @@ enum fx_status fx_string_append_cstrf(fx_string *dest, const char *format, ...)
enum fx_status fx_string_prepend_cstrf(fx_string *dest, const char *format, ...) enum fx_status fx_string_prepend_cstrf(fx_string *dest, const char *format, ...)
{ {
struct fx_string_p *dest_p = fx_object_get_private( struct fx_string_p *dest_p
dest, = fx_object_get_private(dest, FX_TYPE_STRING);
FX_TYPE_STRING);
va_list arg; va_list arg;
va_start(arg, format); va_start(arg, format);
@@ -1539,9 +1527,8 @@ static fx_status string_to_string(
fx_stream *out, fx_stream *out,
const char *format) const char *format)
{ {
struct fx_string_p *str = fx_object_get_private( struct fx_string_p *str
obj->v_object, = fx_object_get_private(obj->v_object, FX_TYPE_STRING);
FX_TYPE_STRING);
const char *s = string_ptr(str); const char *s = string_ptr(str);
for (size_t i = 0; i < str->s_len; i++) { for (size_t i = 0; i < str->s_len; i++) {
fx_stream_write_char(out, s[i]); fx_stream_write_char(out, s[i]);
@@ -1552,9 +1539,8 @@ static fx_status string_to_string(
static fx_status string_hash(const fx_value *v, uint64_t *out_hash) static fx_status string_hash(const fx_value *v, uint64_t *out_hash)
{ {
struct fx_string_p *str = fx_object_get_private( struct fx_string_p *str
v->v_object, = fx_object_get_private(v->v_object, FX_TYPE_STRING);
FX_TYPE_STRING);
const char *s = string_ptr(str); const char *s = string_ptr(str);
*out_hash = fx_hash_cstr(s); *out_hash = fx_hash_cstr(s);
return FX_SUCCESS; return FX_SUCCESS;
@@ -1570,7 +1556,9 @@ static i32 compare(const fx_value *left, const fx_value *right)
return -1; return -1;
} }
return strcmp(left_cstr, right_cstr); int result = strcmp(left_cstr, right_cstr);
return result;
} }
static fx_status get_length( static fx_status get_length(
@@ -1588,9 +1576,8 @@ static fx_status get_length(
static void iterator_fini(fx_iterator *obj) static void iterator_fini(fx_iterator *obj)
{ {
struct fx_string_iterator_p *it = fx_object_get_private( struct fx_string_iterator_p *it
obj, = fx_object_get_private(obj, FX_TYPE_STRING_ITERATOR);
FX_TYPE_STRING_ITERATOR);
if (it->_tmp) { if (it->_tmp) {
fx_string_unref(it->_tmp); fx_string_unref(it->_tmp);
} }
@@ -1598,12 +1585,11 @@ static void iterator_fini(fx_iterator *obj)
memset(it, 0x0, sizeof *it); memset(it, 0x0, sizeof *it);
} }
static fx_iterator *iterator_begin(fx_object *obj) static const fx_iterator *iterator_begin(const fx_object *obj)
{ {
fx_string_iterator *it_obj = fx_object_create(FX_TYPE_STRING_ITERATOR); fx_string_iterator *it_obj = fx_object_create(FX_TYPE_STRING_ITERATOR);
struct fx_string_iterator_p *it = fx_object_get_private( struct fx_string_iterator_p *it
it_obj, = fx_object_get_private(it_obj, FX_TYPE_STRING_ITERATOR);
FX_TYPE_STRING_ITERATOR);
struct fx_string_p *p = fx_object_get_private(obj, FX_TYPE_STRING); struct fx_string_p *p = fx_object_get_private(obj, FX_TYPE_STRING);
if (!p->s_len) { if (!p->s_len) {
@@ -1623,11 +1609,6 @@ static fx_iterator *iterator_begin(fx_object *obj)
return it_obj; return it_obj;
} }
static const fx_iterator *iterator_cbegin(const fx_object *obj)
{
return iterator_begin((fx_object *)obj);
}
static enum fx_status chars_iterator_move_next(struct fx_string_iterator_p *it) static enum fx_status chars_iterator_move_next(struct fx_string_iterator_p *it)
{ {
if (!it->_s_p) { if (!it->_s_p) {
@@ -1676,9 +1657,8 @@ static enum fx_status tokens_iterator_move_next(struct fx_string_iterator_p *it)
static enum fx_status iterator_move_next(const fx_iterator *obj) static enum fx_status iterator_move_next(const fx_iterator *obj)
{ {
struct fx_string_iterator_p *it = fx_object_get_private( struct fx_string_iterator_p *it
obj, = fx_object_get_private(obj, FX_TYPE_STRING_ITERATOR);
FX_TYPE_STRING_ITERATOR);
switch (it->_m) { switch (it->_m) {
case ITERATOR_MODE_CHARS: case ITERATOR_MODE_CHARS:
@@ -1690,21 +1670,22 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
} }
} }
static fx_value chars_iterator_get_value(struct fx_string_iterator_p *it) static fx_value *chars_iterator_get_value(struct fx_string_iterator_p *it)
{ {
return FX_U32(it->char_value); it->value = FX_WCHAR(it->char_value);
return &it->value;
} }
static fx_value tokens_iterator_get_value(struct fx_string_iterator_p *it) static fx_value *tokens_iterator_get_value(struct fx_string_iterator_p *it)
{ {
return FX_CSTR(it->string_value); it->value = FX_CSTR(it->string_value);
return &it->value;
} }
static fx_value iterator_get_value(const fx_iterator *obj) static const fx_value *iterator_get_value(const fx_iterator *obj)
{ {
struct fx_string_iterator_p *it = fx_object_get_private( struct fx_string_iterator_p *it
obj, = fx_object_get_private(obj, FX_TYPE_STRING_ITERATOR);
FX_TYPE_STRING_ITERATOR);
switch (it->_m) { switch (it->_m) {
case ITERATOR_MODE_CHARS: case ITERATOR_MODE_CHARS:
@@ -1712,7 +1693,7 @@ static fx_value iterator_get_value(const fx_iterator *obj)
case ITERATOR_MODE_TOKENS: case ITERATOR_MODE_TOKENS:
return tokens_iterator_get_value(it); return tokens_iterator_get_value(it);
default: default:
return FX_VALUE_EMPTY; return NULL;
} }
} }
@@ -1720,8 +1701,8 @@ static fx_status add(const fx_value *l, const fx_value *r, fx_value *out)
{ {
fx_string *left = l->v_object; fx_string *left = l->v_object;
fx_string *right = r->v_object; fx_string *right = r->v_object;
fx_string *result = fx_string_create_from_cstr( fx_string *result
fx_string_get_cstr(left)); = fx_string_create_from_cstr(fx_string_get_cstr(left));
if (!result) { if (!result) {
return FX_ERR_NO_MEMORY; return FX_ERR_NO_MEMORY;
} }
@@ -1775,7 +1756,6 @@ FX_TYPE_CLASS_BEGIN(fx_string)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = iterator_begin; FX_INTERFACE_ENTRY(it_begin) = iterator_begin;
FX_INTERFACE_ENTRY(it_cbegin) = iterator_cbegin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE) FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_operable, FX_TYPE_OPERABLE)
+1 -1
View File
@@ -1,4 +1,4 @@
#include <fx/core/bitop.h> #include <fx/bitop.h>
int fx_popcountl(long v) int fx_popcountl(long v)
{ {
+2
View File
@@ -29,11 +29,13 @@ int main(void)
printf("%02x", bytes[i % 16]); printf("%02x", bytes[i % 16]);
} }
#if FX_ENABLE_FLOATING_POINT
printf("\n\ngenerating %d random doubles:\n", NRAND_DOUBLES); printf("\n\ngenerating %d random doubles:\n", NRAND_DOUBLES);
for (int i = 0; i < NRAND_DOUBLES; i++) { for (int i = 0; i < NRAND_DOUBLES; i++) {
double v = fx_random_next_double(&random); double v = fx_random_next_double(&random);
printf(" %lf\n", v); printf(" %lf\n", v);
} }
#endif
return 0; return 0;
} }
+1 -1
View File
@@ -20,7 +20,7 @@ int main(void)
fx_foreach(val, it) fx_foreach(val, it)
{ {
const char *tok = NULL; const char *tok = NULL;
fx_value_get_cstr(&val, &tok); fx_value_get_cstr(val, &tok);
printf("%s\n", tok); printf("%s\n", tok);
} }
fx_iterator_unref(it); fx_iterator_unref(it);
+17 -24
View File
@@ -126,9 +126,8 @@ int fx_value_compare(const fx_value *left, const fx_value *right)
return FX_ERR_NOT_SUPPORTED; return FX_ERR_NOT_SUPPORTED;
} }
fx_comparable_class *comparable = fx_class_get_interface( fx_comparable_class *comparable
c, = fx_class_get_interface(c, FX_TYPE_COMPARABLE);
FX_TYPE_COMPARABLE);
if (!comparable || !comparable->c_compare) { if (!comparable || !comparable->c_compare) {
return FX_ERR_NOT_SUPPORTED; return FX_ERR_NOT_SUPPORTED;
} }
@@ -181,8 +180,8 @@ static fx_status value_change_string(fx_value *in, fx_value *out)
return status; return status;
} }
fx_string *result = fx_string_create_from_cstr( fx_string *result
fx_stringstream_ptr(strm)); = fx_string_create_from_cstr(fx_stringstream_ptr(strm));
fx_stringstream_unref(strm); fx_stringstream_unref(strm);
if (!result) { if (!result) {
@@ -212,9 +211,8 @@ fx_status fx_value_change_type(
return FX_ERR_NOT_SUPPORTED; return FX_ERR_NOT_SUPPORTED;
} }
fx_convertible_class *convertible = fx_class_get_interface( fx_convertible_class *convertible
c, = fx_class_get_interface(c, FX_TYPE_CONVERTIBLE);
FX_TYPE_CONVERTIBLE);
if (!convertible) { if (!convertible) {
return FX_ERR_NOT_SUPPORTED; return FX_ERR_NOT_SUPPORTED;
} }
@@ -375,17 +373,15 @@ fx_status fx_value_change_type(
return FX_ERR_NOT_SUPPORTED; \ return FX_ERR_NOT_SUPPORTED; \
} \ } \
\ \
fx_convertible_class *convertible = fx_class_get_interface( \ fx_convertible_class *convertible \
c, \ = fx_class_get_interface(c, FX_TYPE_CONVERTIBLE); \
FX_TYPE_CONVERTIBLE); \ if (!convertible || !convertible->c_to_##fx_type_name) { \
if (!convertible) { \
return FX_ERR_NOT_SUPPORTED; \ return FX_ERR_NOT_SUPPORTED; \
} \ } \
\ \
c_type_name result; \ c_type_name result; \
fx_status status = convertible->c_to_##fx_type_name( \ fx_status status \
v, \ = convertible->c_to_##fx_type_name(v, &result); \
&result); \
if (!FX_OK(status)) { \ if (!FX_OK(status)) { \
return status; \ return status; \
} \ } \
@@ -657,9 +653,8 @@ FX_API fx_type_id fx_value_get_common_type(const fx_value *a, const fx_value *b)
return FX_ERR_NOT_SUPPORTED; \ return FX_ERR_NOT_SUPPORTED; \
} \ } \
\ \
fx_operable_class *operable = fx_class_get_interface( \ fx_operable_class *operable \
c, \ = fx_class_get_interface(c, FX_TYPE_OPERABLE); \
FX_TYPE_OPERABLE); \
if (!operable || !operable->op_##name) { \ if (!operable || !operable->op_##name) { \
return FX_ERR_NOT_SUPPORTED; \ return FX_ERR_NOT_SUPPORTED; \
} \ } \
@@ -674,9 +669,8 @@ FX_API fx_type_id fx_value_get_common_type(const fx_value *a, const fx_value *b)
return FX_ERR_NOT_SUPPORTED; \ return FX_ERR_NOT_SUPPORTED; \
} \ } \
\ \
fx_operable_class *operable = fx_class_get_interface( \ fx_operable_class *operable \
c, \ = fx_class_get_interface(c, FX_TYPE_OPERABLE); \
FX_TYPE_OPERABLE); \
if (!operable || !operable->op_##name) { \ if (!operable || !operable->op_##name) { \
return FX_ERR_NOT_SUPPORTED; \ return FX_ERR_NOT_SUPPORTED; \
} \ } \
@@ -695,9 +689,8 @@ FX_API fx_type_id fx_value_get_common_type(const fx_value *a, const fx_value *b)
return FX_ERR_NOT_SUPPORTED; \ return FX_ERR_NOT_SUPPORTED; \
} \ } \
\ \
fx_operable_class *operable = fx_class_get_interface( \ fx_operable_class *operable \
c, \ = fx_class_get_interface(c, FX_TYPE_OPERABLE); \
FX_TYPE_OPERABLE); \
if (!operable || !operable->op_##name) { \ if (!operable || !operable->op_##name) { \
return FX_ERR_NOT_SUPPORTED; \ return FX_ERR_NOT_SUPPORTED; \
} \ } \
+2 -2
View File
@@ -1,10 +1,10 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
static double another_function(int a, double b, int c) static int another_function(int a, double b, int c)
{ {
printf("a=%d, b=%lf, c=%d\n", a, b, c); printf("a=%d, b=%lf, c=%d\n", a, b, c);
return 1.2; return 2;
} }
static int test_function(int a, int b, int c, ...) static int test_function(int a, int b, int c, ...)
+3 -3
View File
@@ -22,17 +22,17 @@ int main(int argc, const char **argv)
/* TODO re-implement json support */ /* TODO re-implement json support */
fx_serial_ctx *ctx = NULL; fx_serial_ctx *ctx = NULL;
fx_object *data; fx_value data = FX_VALUE_EMPTY;
result = fx_serial_ctx_deserialise(ctx, src, &data, 0); result = fx_serial_ctx_deserialise(ctx, src, &data, 0);
if (fx_result_is_error(result)) { if (fx_result_is_error(result)) {
fx_throw(result); fx_throw(result);
return -1; return -1;
} }
fx_object_to_string(data, fx_stdout, NULL); fx_value_to_string(&data, fx_stdout, NULL);
fx_stream_write_char(fx_stdout, '\n'); fx_stream_write_char(fx_stdout, '\n');
fx_object_unref(data); fx_value_unset(&data);
fx_object_unref(src); fx_object_unref(src);
fx_serial_ctx_unref(ctx); fx_serial_ctx_unref(ctx);
+4 -4
View File
@@ -22,21 +22,21 @@ int main(int argc, const char **argv)
fx_serial_ctx *ctx = fx_toml_serial_ctx_create(); fx_serial_ctx *ctx = fx_toml_serial_ctx_create();
fx_object *data = NULL; fx_value data = FX_VALUE_EMPTY;
result = fx_serial_ctx_deserialise(ctx, src, &data, 0); result = fx_serial_ctx_deserialise(ctx, src, &data, 0);
if (fx_result_is_error(result)) { if (fx_result_is_error(result)) {
fx_throw(result); fx_throw(result);
return -1; return -1;
} }
if (!data) { if (!fx_value_is_set(&data)) {
return 0; return 0;
} }
fx_object_to_string(data, fx_stdout, NULL); fx_value_to_string(&data, fx_stdout, NULL);
fx_stream_write_char(fx_stdout, '\n'); fx_stream_write_char(fx_stdout, '\n');
fx_object_unref(data); fx_value_unset(&data);
fx_object_unref(src); fx_object_unref(src);
fx_serial_ctx_unref(ctx); fx_serial_ctx_unref(ctx);