68 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
wash 01cbcf712a test: update tests 2026-05-25 17:35:09 +01:00
wash 5e3439bc4a cmake: update handling of public namespace headers 2026-05-25 17:34:59 +01:00
wash 56e1fae273 cmake: update endianness test 2026-05-25 17:34:41 +01:00
wash c5eac75a0b fx.term: remove redundant printf implementation 2026-05-25 17:31:40 +01:00
wash aea3f99859 fx.collections: update exported classes 2026-05-25 17:31:17 +01:00
wash cca52c788a fx.serial: remove references to obsolete classes 2026-05-25 17:30:59 +01:00
wash 74fbd94f95 fx.io: update interface implementations 2026-05-25 17:30:33 +01:00
wash 97fe00d4a1 fx.collections: update interface implementations 2026-05-25 17:30:08 +01:00
wash 0be82c63fa fx.collections: update tests 2026-05-25 17:29:56 +01:00
wash 2df76a76c4 fx.collections: remove fx_tree 2026-05-25 17:29:38 +01:00
wash 9d4aab49ac fx.collections: update fx_array to support storing fx_values 2026-05-25 17:29:23 +01:00
wash d84d56681f fx.collections: replace fx_dict with fx_hashtable 2026-05-25 17:28:25 +01:00
wash 6efe473c11 fx: implement updated interfaces in builtin classes 2026-05-25 17:27:14 +01:00
wash 5d0aa577d6 fx.reflection: fix fx_string type marshalling 2026-05-25 17:26:55 +01:00
wash f9b456a99f fx.reflection: implement assembly registration and lookup 2026-05-25 17:26:08 +01:00
wash 673bf07cf6 fx.reflection: implement retrieving functions and properties from fx_type 2026-05-25 17:25:27 +01:00
wash 82bb20c6af fx.reflection: implement fx_property 2026-05-25 17:24:47 +01:00
wash 12b04eea8d fx: namemap: fix nullptr deference in fx_namemap_first 2026-05-25 17:23:50 +01:00
wash 0f1dfc0b07 fx: implement wchar and wstr value types 2026-05-25 17:23:32 +01:00
wash 13332145c2 fx: add functions for hashing wstr and generic buffers 2026-05-25 17:23:19 +01:00
wash 7cdad180da fx: add interface for performing arithmetic operations on values 2026-05-25 17:21:27 +01:00
wash 554a30fa1a fx: add interface for converting a value to a different type 2026-05-25 17:21:08 +01:00
wash 5058bb164a fx: add interface for comparaing two values 2026-05-25 17:20:36 +01:00
wash 79a8041aac fx: add wstr and wchar value types 2026-05-25 17:20:15 +01:00
wash bc902047d3 fx: implement value conversion interface 2026-05-25 17:19:51 +01:00
wash 61182e0153 fx.reflection: function: update invocation to support new value-types 2026-05-17 17:11:02 +01:00
wash f1258489d1 fx: implement a proper value-type mechanism 2026-05-17 17:09:05 +01:00
wash 65a7a025c5 fx: stream: add fx_stream_write_wstr 2026-05-17 17:08:42 +01:00
wash 5ead9118b3 build: add module to handle querying platform details 2026-05-05 21:31:12 +01:00
wash c29465a97d build: re-enable namespace-specific tests 2026-05-05 21:17:21 +01:00
wash 4b20f8ca0e test: dynamic function call tests 2026-05-05 21:16:56 +01:00
wash f3062222cb fx: support for registering callable functions with types 2026-05-05 21:16:31 +01:00
wash 703155affe meta: update clang-format config 2026-05-05 19:03:31 +01:00
wash 90a3905f48 fx: re-arrange type system to allow for reflection 2026-05-05 19:03:22 +01:00
wash 8c12868651 fx: add simple cstr-based hashmap data structure 2026-05-05 12:38:34 +01:00
wash 2966934cd8 fx: add simple resizable array data structure 2026-05-05 12:38:12 +01:00
wash 9937546452 fx.reflection: remove fx_function_context 2026-05-04 22:56:21 +01:00
wash 5d98bc89a1 test: dynamic function argument bind test 2026-05-04 22:54:02 +01:00
wash b8e270b962 fx.reflection: function: implementing pre-binding argument values to functions 2026-05-04 22:53:45 +01:00
wash 3d1600ece6 fx: value: implement copy and unset functions 2026-05-04 22:53:23 +01:00
wash fe3af271e2 meta: update clang-format config 2026-05-04 22:53:03 +01:00
wash dea321acc2 fx.reflection: optimise darwin dynamic function calling 2026-05-04 20:44:02 +01:00
wash abe1603955 meta: update clang-format config 2026-05-04 19:42:37 +01:00
wash bf251dd818 fx.reflection: optimise linux dynamic function calling 2026-05-04 19:37:44 +01:00
wash bebc67537d fx.reflection: implement dynamic function calling on linux-x86_64 2026-05-04 19:23:10 +01:00
wash e954a15d92 fx.reflection: cleanup darwin implementation of dynamic function calling 2026-05-04 19:22:41 +01:00
225 changed files with 11778 additions and 6743 deletions
+6 -64
View File
@@ -1,7 +1,5 @@
BasedOnStyle: WebKit
IndentWidth: 8
---
Language: ObjC
DerivePointerAlignment: false
PointerAlignment: Right
ColumnLimit: 80
@@ -46,68 +44,12 @@ ReflowComments: true
SpacesBeforeTrailingComments: 3
TabWidth: 8
UseTab: AlignWithSpaces
PenaltyReturnTypeOnItsOwnLine: 1000000
PenaltyExcessCharacter: 5
PenaltyBreakOpenParenthesis: 5
PenaltyBreakBeforeFirstCallParameter: 5
PenaltyIndentedWhitespace: 0
AttributeMacros:
- FX_API
ForEachMacros:
- fx_btree_foreach
- fx_queue_foreach
MacroBlockBegin: "FX_(TYPE|ASSEMBLY)_.*_BEGIN"
MacroBlockEnd: "FX_(TYPE|ASSEMBLY)_.*_END"
---
Language: Cpp
DerivePointerAlignment: false
PointerAlignment: Right
ColumnLimit: 80
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveAssignments: None
AlignConsecutiveBitFields: None
AlignConsecutiveDeclarations: None
AlignConsecutiveMacros: AcrossEmptyLinesAndComments
AlignEscapedNewlines: Right
AlignOperands: AlignAfterOperator
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLambdasOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: false
ExperimentalAutoDetectBinPacking: false
BitFieldColonSpacing: Both
BreakBeforeBraces: Linux
BreakBeforeBinaryOperators: All
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
BreakInheritanceList: BeforeComma
BreakStringLiterals: true
ContinuationIndentWidth: 8
Cpp11BracedListStyle: true
IncludeBlocks: Regroup
SortIncludes: true
IndentRequires: true
NamespaceIndentation: Inner
ReflowComments: true
SpacesBeforeTrailingComments: 3
TabWidth: 8
UseTab: AlignWithSpaces
PenaltyReturnTypeOnItsOwnLine: 1000000
PenaltyExcessCharacter: 9999999
PenaltyBreakOpenParenthesis: 5
PenaltyBreakBeforeFirstCallParameter: 5
BreakAfterReturnType: Automatic
PenaltyBreakAssignment: 0
PenaltyReturnTypeOnItsOwnLine: 100000000
PenaltyExcessCharacter: 10000000
PenaltyBreakOpenParenthesis: 0
PenaltyBreakBeforeFirstCallParameter: 0
PenaltyIndentedWhitespace: 0
AttributeMacros:
- FX_API
+19 -8
View File
@@ -1,12 +1,24 @@
cmake_minimum_required(VERSION 3.25)
project(fx C ASM)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include (TestBigEndian)
include(Templates)
include(Platform)
test_big_endian(is_big_endian)
if (is_big_endian)
message(STATUS "Target is Big Endian")
add_compile_definitions(FX_ENDIAN=1)
else ()
message(STATUS "Target is Little Endian")
add_compile_definitions(FX_ENDIAN=0)
endif ()
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(fx_source_root ${CMAKE_CURRENT_SOURCE_DIR})
@@ -33,10 +45,9 @@ endif ()
message(STATUS "Floating point support: ${fx_enable_floating_point}")
set(fx_system_name ${CMAKE_SYSTEM_NAME})
string(TOLOWER ${fx_system_name} fx_system_name)
set(fx_system_arch ${CMAKE_SYSTEM_PROCESSOR})
string(TOLOWER ${fx_system_arch} fx_system_arch)
get_platform_details(
SYSTEM fx_system_name
PROCESSOR fx_system_arch)
message(STATUS "Target system: ${fx_system_name}-${fx_system_arch}")
@@ -44,10 +55,10 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Templates.cmake)
foreach (assembly ${fx_assemblies})
add_subdirectory(assemblies/${assembly})
endforeach (assembly)
add_subdirectory(test)
if (fx_enable_tests)
add_subdirectory(test)
endif ()
+2 -2
View File
@@ -1,7 +1,7 @@
#include <fx/macros.h>
#include <fx/reflection/assembly.h>
FX_ASSEMBLY_BEGIN()
FX_ASSEMBLY_BEGIN(fx_cmdline)
FX_ASSEMBLY_NAME("fx.cmdline");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END()
FX_ASSEMBLY_END(fx_cmdline)
+1 -2
View File
@@ -7,8 +7,7 @@ FX_ASSEMBLY_BEGIN()
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "array", fx_array);
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "bitbuffer", fx_bitbuffer);
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "bitmap", fx_bitmap);
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "dict", fx_dict);
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "hashtable", fx_hashtable);
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "hashmap", fx_hashmap);
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "list", fx_list);
FX_ASSEMBLY_EXPORT_TYPE("fx.collections", "tree", fx_tree);
FX_ASSEMBLY_END()
+2 -2
View File
@@ -1,7 +1,7 @@
#include <fx/macros.h>
#include <fx/reflection/assembly.h>
FX_ASSEMBLY_BEGIN()
FX_ASSEMBLY_BEGIN(fx_compression)
FX_ASSEMBLY_NAME("fx.compression");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END()
FX_ASSEMBLY_END(fx_compression)
+4 -4
View File
@@ -1,7 +1,7 @@
#include <fx/macros.h>
#include <fx/reflection/assembly.h>
FX_ASSEMBLY_BEGIN()
FX_ASSEMBLY_NAME("fx.io");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END()
FX_ASSEMBLY_BEGIN(fx_io)
FX_ASSEMBLY_NAME("fx.io");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END(fx_io)
+2 -2
View File
@@ -1,7 +1,7 @@
#include <fx/macros.h>
#include <fx/reflection/assembly.h>
FX_ASSEMBLY_BEGIN()
FX_ASSEMBLY_BEGIN(fx_runtime)
FX_ASSEMBLY_NAME("fx.runtime");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_EXPORT_TYPE("fx", "stream", fx_stream);
@@ -9,4 +9,4 @@ FX_ASSEMBLY_BEGIN()
FX_ASSEMBLY_EXPORT_TYPE("fx", "stringstream", fx_stringstream);
FX_ASSEMBLY_EXPORT_TYPE("fx", "iterator", fx_iterator);
FX_ASSEMBLY_EXPORT_TYPE("fx.reflection", "assembly", fx_assembly);
FX_ASSEMBLY_END()
FX_ASSEMBLY_END(fx_runtime)
+2 -2
View File
@@ -1,7 +1,7 @@
#include <fx/macros.h>
#include <fx/reflection/assembly.h>
FX_ASSEMBLY_BEGIN()
FX_ASSEMBLY_BEGIN(fx_serial)
FX_ASSEMBLY_NAME("fx.serial");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END()
FX_ASSEMBLY_END(fx_serial)
+2 -2
View File
@@ -1,7 +1,7 @@
#include <fx/macros.h>
#include <fx/reflection/assembly.h>
FX_ASSEMBLY_BEGIN()
FX_ASSEMBLY_BEGIN(fx_term)
FX_ASSEMBLY_NAME("fx.term");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END()
FX_ASSEMBLY_END(fx_term)
+44 -109
View File
@@ -49,43 +49,37 @@ if (FX_STATIC)
set(_lib_suffix "-s")
endif ()
set(supported_components Core Ds Term Cmd Io Serial Compress)
set(components ${FX_FIND_COMPONENTS})
string(REPLACE ";" ", " supported_components_string_list "${supported_components}")
if (NOT components)
set(components ${supported_components})
endif ()
set(assemblies ${FX_FIND_COMPONENTS})
set(required_vars)
foreach (component ${components})
if (NOT "${component}" IN_LIST supported_components)
message(FATAL_ERROR "'${component}' is not a valid FX module.\nSupported modules: ${supported_components_string_list}")
endif ()
if (NOT FX_INCLUDE_DIR)
find_path(FX_INCLUDE_DIR
NAMES fx/misc.h ${FX_FIND_ARGS}
PATH_SUFFIXES include
PATHS ${FX_SEARCH_PATHS})
endif ()
string(TOLOWER ${component} header_name)
set(lib_name ${header_name}${_lib_suffix})
set(required_vars FX_INCLUDE_DIR)
if (NOT FX_${component}_INCLUDE_DIR)
find_path(FX_${component}_INCLUDE_DIR
NAMES fx/${header_name}.h ${FX_FIND_ARGS}
PATH_SUFFIXES include
PATHS ${FX_SEARCH_PATHS})
endif ()
foreach (assembly ${assemblies})
string(TOLOWER ${assembly} header_name)
string(REPLACE "." "_" macro_name ${assembly})
string(TOUPPER ${macro_name} macro_name)
if (NOT FX_${component}_LIBRARY)
find_library(FX_${component}_LIBRARY
NAMES fx-${lib_name} ${FX_FIND_ARGS}
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
PATHS ${FX_SEARCH_PATHS})
else ()
# 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()
list(APPEND required_vars FX_${component}_INCLUDE_DIR FX_${component}_LIBRARY)
endforeach (component)
list(APPEND required_vars ${macro_name}_LIBRARY)
endforeach (assembly)
unset(FX_FIND_ARGS)
@@ -96,94 +90,35 @@ find_package_handle_standard_args(FX
if (FX_FOUND)
set(created_targets)
foreach (component ${components})
string(TOLOWER ${component} header_name)
set(lib_name ${header_name}${_lib_suffix})
foreach (assembly ${assemblies})
set(target_name ${assembly})
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})
add_library(FX::${component} UNKNOWN IMPORTED)
set_target_properties(FX::${component} PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${FX_${component}_INCLUDE_DIR}")
target_compile_definitions(FX::${component} INTERFACE _CRT_SECURE_NO_WARNINGS=1)
string(TOLOWER ${assembly} header_name)
string(REPLACE "." "_" macro_name ${assembly})
string(REPLACE "." "_" macro_name ${assembly})
string(TOUPPER ${macro_name} macro_name)
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)
target_compile_definitions(FX::${component} INTERFACE FX_STATIC=1)
target_compile_definitions(FX::${target_name} INTERFACE FX_STATIC=1)
endif ()
set_target_properties(FX::${component} PROPERTIES
set_target_properties(FX::${target_name} PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${FX_${component}_LIBRARY}")
set(created_targets ${created_targets} ${component})
IMPORTED_LOCATION "${${macro_name}_LIBRARY}")
set(created_targets ${created_targets} ${assembly})
endif ()
endforeach (component)
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)
endforeach (assembly)
endif()
+36
View File
@@ -0,0 +1,36 @@
function(get_platform_details)
set(options)
set(one_value_args SYSTEM PROCESSOR)
set(multi_value_args
NAMESPACES
DEPENDENCIES
LIBS)
cmake_parse_arguments(PARSE_ARGV 0 arg
"${options}"
"${one_value_args}"
"${multi_value_args}")
set(system_name "")
if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
set(system_name "linux")
elseif ("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
set(system_name "apple")
elseif ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
set(system_name "win32")
else ()
string(TOLOWER "${CMAKE_SYSTEM_NAME}" system_name)
endif ()
if ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
set(system_arch "amd64")
elseif ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64")
set(system_arch "aarch64")
else ()
string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" system_arch)
endif ()
set(${arg_SYSTEM} ${system_name} PARENT_SCOPE)
set(${arg_PROCESSOR} ${system_arch} PARENT_SCOPE)
endfunction (get_platform_details)
+17 -10
View File
@@ -18,10 +18,12 @@ function(add_fx_assembly)
set(assembly_include_paths "")
set(assembly_sources ${CMAKE_CURRENT_SOURCE_DIR}/assembly.c)
set(assembly_headers)
foreach (dir ${arg_NAMESPACES})
add_subdirectory(${fx_source_root}/${dir} ${fx_build_root}/namespaces/${dir})
set(assembly_sources ${assembly_sources} ${namespace_sources})
set(assembly_headers ${assembly_headers} ${namespace_headers})
set(assembly_include_paths ${assembly_include_paths} ${namespace_include_paths})
endforeach (dir)
@@ -45,20 +47,23 @@ function(add_fx_assembly)
target_compile_definitions(${assembly_target_name} PRIVATE ${def})
endforeach (def)
if (fx_enable_tests)
foreach (ns ${arg_NAMESPACES})
file(GLOB test_sources ${fx_source_root}/${ns}/test/*.c)
foreach (test_file ${test_sources})
get_filename_component(test_name ${test_file} NAME_WE)
set(test_name ${ns}-${test_name})
string(REPLACE "." "-" test_name ${test_name})
add_executable(${test_name} ${test_file})
target_link_libraries(${test_name} ${assembly_target_name})
endforeach (test_file)
endforeach (ns)
endif ()
set_target_properties(${assembly_target_name} PROPERTIES
FOLDER "${assembly_name}")
TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
if(IS_BIG_ENDIAN)
target_compile_definitions(${assembly_target_name} PRIVATE
BIG_ENDIAN)
else()
target_compile_definitions(${assembly_target_name} PRIVATE
LITTLE_ENDIAN)
endif()
install(TARGETS ${assembly_target_name})
install(FILES ${headers} DESTINATION include/${assembly_path})
endfunction(add_fx_assembly)
macro(export_fx_namespace_details ns_name)
@@ -85,6 +90,7 @@ macro(export_fx_namespace_details ns_name)
${CMAKE_CURRENT_SOURCE_DIR}/sys/${fx_system_name}/${fx_system_arch}/*.h
${CMAKE_CURRENT_SOURCE_DIR}/sys/${fx_system_name}/${fx_system_arch}/*.S)
file(GLOB headers include/${namespace_path}/*.h)
set(namespace_headers ${headers} PARENT_SCOPE)
set(namespace_sources
${namespace_sources}
${base_namespace_sources}
@@ -99,5 +105,6 @@ macro(export_fx_namespace_details ns_name)
PARENT_SCOPE)
set(internal_libs ${internal_libs} PARENT_SCOPE)
set(internal_defines ${internal_defines} PARENT_SCOPE)
install(FILES ${headers} DESTINATION include/${namespace_path})
endmacro(export_fx_namespace_details)
+1 -1
View File
@@ -1,6 +1,6 @@
#include "command.h"
#include <fx/cmd.h>
#include <fx/cmdline/cmd.h>
#include <fx/string.h>
#include <stdlib.h>
#include <string.h>
+1 -1
View File
@@ -1,6 +1,6 @@
#include "command.h"
#include <fx/cmd.h>
#include <fx/cmdline/cmd.h>
#include <fx/misc.h>
#include <fx/string.h>
#include <fx/term/print.h>
+1 -1
View File
@@ -1,7 +1,7 @@
#include "command.h"
#include <fx/bst.h>
#include <fx/cmd.h>
#include <fx/cmdline/cmd.h>
#include <fx/string.h>
#include <fx/term/print.h>
#include <fx/term/tty.h>
+1 -1
View File
@@ -2,7 +2,7 @@
#define _FX_COMMAND_H_
#include <fx/bst.h>
#include <fx/cmd.h>
#include <fx/cmdline/cmd.h>
#include <fx/queue.h>
#include <fx/string.h>
+1 -1
View File
@@ -1,6 +1,6 @@
#include "command.h"
#include <fx/cmd.h>
#include <fx/cmdline/cmd.h>
#include <fx/string.h>
#include <stdlib.h>
#include <string.h>
+13 -5
View File
@@ -1,4 +1,4 @@
#include <fx/cmd.h>
#include <fx/cmdline/cmd.h>
#include <stdio.h>
enum {
@@ -27,16 +27,24 @@ const char *text
"great pleasure.";
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");
fx_arglist_iterator it;
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)) {
printf("opt:%u,arg:%u,i:%zu,value: %s\n", it.opt_id,
it.value->val_id, it.i, it.value->val_str);
printf("opt:%u,arg:%u,i:%zu,value: %s\n",
it.opt_id,
it.value->val_id,
it.i,
it.value->val_str);
fx_arglist_iterator_next(&it);
}
+102 -124
View File
@@ -2,6 +2,7 @@
#include <fx/iterator.h>
#include <fx/stream.h>
#include <fx/string.h>
#include <fx/value.h>
#include <stdlib.h>
#include <string.h>
@@ -12,17 +13,17 @@ struct fx_array_p {
size_t ar_len;
/* maximum number of items that can currently be stored in array */
size_t ar_cap;
fx_object **ar_data;
fx_value *ar_data;
};
struct fx_array_iterator_p {
fx_array *_a;
const fx_array *_a;
struct fx_array_p *_a_p;
/** The index of the current value */
size_t i;
/** The current value */
fx_object *value;
fx_value *value;
};
/*** PRIVATE FUNCTIONS ********************************************************/
@@ -32,7 +33,7 @@ static fx_status resize_array(struct fx_array_p *array, size_t new_capacity)
if (array->ar_cap < new_capacity) {
void *new_data = realloc(
array->ar_data,
new_capacity * sizeof(struct fx_dsref *));
new_capacity * sizeof(fx_value));
if (!new_data) {
return FX_ERR_NO_MEMORY;
}
@@ -40,12 +41,12 @@ static fx_status resize_array(struct fx_array_p *array, size_t new_capacity)
array->ar_data = new_data;
} else {
for (size_t i = new_capacity; i < array->ar_len; i++) {
fx_object_unref(array->ar_data[i]);
fx_value_unset(&array->ar_data[i]);
}
void *new_data = realloc(
array->ar_data,
new_capacity * sizeof(struct fx_dsref *));
new_capacity * sizeof(fx_value));
if (!new_data) {
return FX_ERR_NO_MEMORY;
}
@@ -63,7 +64,7 @@ static fx_status resize_array(struct fx_array_p *array, size_t new_capacity)
static fx_status array_insert(
struct fx_array_p *array,
fx_object *value,
fx_value value,
size_t at)
{
if (at == FX_NPOS) {
@@ -84,13 +85,13 @@ static fx_status array_insert(
}
}
fx_object **src = array->ar_data + at;
fx_object **dest = src + 1;
size_t move_len = (array->ar_len - at) * sizeof(struct fx_dsref *);
fx_value *src = array->ar_data + at;
fx_value *dest = src + 1;
size_t move_len = (array->ar_len - at) * sizeof(fx_value);
memmove(dest, src, move_len);
array->ar_data[at] = fx_object_ref(value);
fx_value_copy(&array->ar_data[at], &value);
array->ar_len++;
return FX_SUCCESS;
@@ -102,11 +103,11 @@ static fx_status array_remove(struct fx_array_p *array, size_t at)
return FX_ERR_OUT_OF_BOUNDS;
}
fx_object **src = array->ar_data + at;
fx_object **dest = src + 1;
size_t move_len = array->ar_len * sizeof(struct fx_dsref *);
fx_value *src = array->ar_data + at;
fx_value *dest = src + 1;
size_t move_len = array->ar_len * sizeof(fx_value);
fx_object_unref(array->ar_data[at]);
fx_value_unset(&array->ar_data[at]);
memmove(dest, src, move_len);
@@ -125,17 +126,17 @@ static fx_status array_remove_back(struct fx_array_p *array)
return array_remove(array, array->ar_len - 1);
}
static fx_object *array_pop(struct fx_array_p *array, size_t at)
static fx_value array_pop(struct fx_array_p *array, size_t at)
{
if (at >= array->ar_len) {
return NULL;
return FX_VALUE_EMPTY;
}
fx_object **src = array->ar_data + at;
fx_object **dest = src + 1;
size_t move_len = array->ar_len * sizeof(struct fx_dsref *);
fx_value *dest = array->ar_data + at;
fx_value *src = dest + 1;
size_t move_len = array->ar_len * sizeof(fx_value);
fx_object *out = array->ar_data[at];
fx_value out = array->ar_data[at];
memmove(dest, src, move_len);
@@ -144,40 +145,40 @@ static fx_object *array_pop(struct fx_array_p *array, size_t at)
return out;
}
static fx_object *array_pop_front(struct fx_array_p *array)
static fx_value array_pop_front(struct fx_array_p *array)
{
return array_pop(array, 0);
}
static fx_object *array_pop_back(struct fx_array_p *array)
static fx_value array_pop_back(struct fx_array_p *array)
{
return array_pop(array, array->ar_len - 1);
}
static fx_object *array_at(const struct fx_array_p *array, size_t at)
static fx_value *array_get_ref(const struct fx_array_p *array, size_t at)
{
if (at >= array->ar_len) {
return NULL;
}
return array->ar_data[at];
return &array->ar_data[at];
}
static fx_object *array_get(struct fx_array_p *array, size_t at)
static fx_value array_get(struct fx_array_p *array, size_t at)
{
if (at >= array->ar_len) {
return NULL;
return FX_VALUE_EMPTY;
}
return fx_object_ref(array->ar_data[at]);
return fx_value_copy_return(array->ar_data[at]);
}
static size_t array_size(const struct fx_array_p *array)
static size_t array_get_size(const struct fx_array_p *array)
{
return array->ar_len;
}
static size_t array_capacity(const struct fx_array_p *array)
static size_t array_get_capacity(const struct fx_array_p *array)
{
return array->ar_cap;
}
@@ -189,18 +190,16 @@ static void array_clear(struct fx_array_p *array)
}
for (size_t i = 0; i < array->ar_len; i++) {
fx_object_unref(array->ar_data[i]);
fx_value_unset(&array->ar_data[i]);
}
memset(array->ar_data, 0x0, array->ar_cap * sizeof(fx_object *));
memset(array->ar_data, 0x0, array->ar_cap * sizeof(fx_value));
array->ar_len = 0;
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_array *fx_array_create_with_values(
fx_object *const *values,
size_t nr_values)
fx_array *fx_array_create_with_values(const fx_value *values, size_t nr_values)
{
fx_array *array = fx_array_create();
if (!array) {
@@ -211,14 +210,14 @@ fx_array *fx_array_create_with_values(
size_t real_nr_values = 0;
for (size_t i = 0; i < nr_values; i++) {
if (values[i]) {
if (fx_value_is_set(&values[i])) {
real_nr_values++;
}
}
p->ar_len = real_nr_values;
p->ar_cap = real_nr_values;
p->ar_data = calloc(real_nr_values, sizeof(struct fx_dsref *));
p->ar_data = calloc(real_nr_values, sizeof(fx_value));
if (!p->ar_data) {
fx_array_unref(array);
return NULL;
@@ -226,13 +225,13 @@ fx_array *fx_array_create_with_values(
size_t index = 0;
for (size_t i = 0; i < nr_values; i++) {
p->ar_data[index++] = fx_object_ref(values[i]);
fx_value_copy(&p->ar_data[index++], &values[i]);
}
return array;
}
fx_status fx_array_insert(fx_array *array, fx_object *value, size_t at)
fx_status fx_array_insert(fx_array *array, fx_value value, size_t at)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_insert, array, value, at);
}
@@ -252,39 +251,39 @@ fx_status fx_array_remove_back(fx_array *array)
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_remove_back, array);
}
fx_object *fx_array_pop(fx_array *array, size_t at)
fx_value fx_array_pop(fx_array *array, size_t at)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_pop, array, at);
}
fx_object *fx_array_pop_front(fx_array *array)
fx_value fx_array_pop_front(fx_array *array)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_pop_front, array);
}
fx_object *fx_array_pop_back(fx_array *array)
fx_value fx_array_pop_back(fx_array *array)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_pop_back, array);
}
fx_object *fx_array_at(const fx_array *array, size_t at)
fx_value *fx_array_get_ref(const fx_array *array, size_t at)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_at, array, at);
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_get_ref, array, at);
}
fx_object *fx_array_get(fx_array *array, size_t at)
fx_value fx_array_get(const fx_array *array, size_t at)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_ARRAY, array_get, array, at);
}
size_t fx_array_size(const fx_array *array)
size_t fx_array_get_size(const fx_array *array)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_size, array);
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_get_size, array);
}
size_t fx_array_capacity(const fx_array *array)
size_t fx_array_get_capacity(const fx_array *array)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_capacity, array);
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ARRAY, array_get_capacity, array);
}
void fx_array_clear(fx_array *array)
@@ -294,12 +293,12 @@ void fx_array_clear(fx_array *array)
/*** PUBLIC ALIAS FUNCTIONS ***************************************************/
fx_status fx_array_append(fx_array *array, fx_object *value)
fx_status fx_array_push_back(fx_array *array, fx_value value)
{
return fx_array_insert(array, value, FX_NPOS);
}
fx_status fx_array_prepend(fx_array *array, fx_object *value)
fx_status fx_array_push_front(fx_array *array, fx_value value)
{
return fx_array_insert(array, value, 0);
}
@@ -317,7 +316,7 @@ static void array_fini(fx_object *obj, void *priv)
if (array->ar_data) {
for (size_t i = 0; i < array->ar_len; i++) {
fx_object_unref(array->ar_data[i]);
fx_value_unset(&array->ar_data[i]);
}
free(array->ar_data);
@@ -325,29 +324,34 @@ static void array_fini(fx_object *obj, void *priv)
}
}
static void array_to_string(const fx_object *obj, fx_stream *out)
static fx_status array_to_string(
const fx_value *obj,
fx_stream *out,
const char *format)
{
struct fx_array_p *array = fx_object_get_private(obj, FX_TYPE_ARRAY);
struct fx_array_p *array = fx_object_get_private(
obj->v_object,
FX_TYPE_ARRAY);
if (!array->ar_len) {
fx_stream_write_cstr(out, "[]", NULL);
return;
return FX_SUCCESS;
}
fx_stream_write_cstr(out, "[\n", NULL);
fx_stream_push_indent(out, 1);
size_t len = array_size(array);
size_t len = array_get_size(array);
for (size_t i = 0; i < array->ar_len; i++) {
fx_object *value = array->ar_data[i];
bool is_string = fx_object_is_type(value, FX_TYPE_STRING);
fx_value *value = &array->ar_data[i];
bool is_string = fx_value_is_type(value, FX_TYPE_STRING);
if (is_string) {
fx_stream_write_char(out, '"');
}
fx_object_to_string(value, out);
fx_value_to_string(value, out, NULL);
if (is_string) {
fx_stream_write_char(out, '"');
@@ -362,40 +366,23 @@ static void array_to_string(const fx_object *obj, fx_stream *out)
fx_stream_pop_indent(out);
fx_stream_write_char(out, ']');
return FX_SUCCESS;
}
/*** 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);
struct fx_array_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_ARRAY_ITERATOR);
struct fx_array_iterator_p *it = fx_object_get_private(
it_obj,
FX_TYPE_ARRAY_ITERATOR);
it->_a = obj;
it->_a_p = fx_object_get_private(obj, FX_TYPE_ARRAY);
it->i = 0;
if (it->_a_p->ar_len > 0) {
it->value = it->_a_p->ar_data[0];
} else {
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
it->value = NULL;
}
return it_obj;
}
static const fx_iterator *iterable_cbegin(const fx_object *obj)
{
fx_array_iterator *it_obj = fx_object_create(FX_TYPE_ARRAY_ITERATOR);
struct fx_array_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_ARRAY_ITERATOR);
it->_a = (fx_array *)obj;
it->_a_p = fx_object_get_private(obj, FX_TYPE_ARRAY);
it->i = 0;
if (it->_a_p->ar_len > 0) {
it->value = it->_a_p->ar_data[0];
it->value = &it->_a_p->ar_data[0];
} else {
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
it->value = NULL;
@@ -406,12 +393,13 @@ static const fx_iterator *iterable_cbegin(const fx_object *obj)
static enum fx_status iterator_move_next(const fx_iterator *obj)
{
struct fx_array_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
struct fx_array_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_ARRAY_ITERATOR);
struct fx_array_p *array = it->_a_p;
if (it->value == NULL || it->i >= array->ar_len) {
return false;
return FX_ERR_NO_DATA;
}
it->i++;
@@ -419,7 +407,7 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
if (it->i >= array->ar_len) {
it->value = NULL;
} else {
it->value = array->ar_data[it->i];
it->value = &array->ar_data[it->i];
}
return (it->value != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA;
@@ -427,22 +415,19 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
static enum fx_status iterator_erase(fx_iterator *obj)
{
struct fx_array_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
struct fx_array_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_ARRAY_ITERATOR);
struct fx_array_p *array = it->_a_p;
if (it->i >= array->ar_len) {
return FX_ERR_OUT_OF_BOUNDS;
}
if (array->ar_data[it->i] != it->value) {
return FX_ERR_BAD_STATE;
}
array_remove(array, it->i);
if (it->i < array->ar_len) {
it->value = array->ar_data[it->i];
it->value = &array->ar_data[it->i];
} else {
it->value = NULL;
}
@@ -450,52 +435,46 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_SUCCESS;
}
static fx_iterator_value iterator_get_value(fx_iterator *obj)
static const fx_value *iterator_get_value(const fx_iterator *obj)
{
struct fx_array_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
struct fx_array_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_ARRAY_ITERATOR);
struct fx_array_p *array = it->_a_p;
return FX_ITERATOR_VALUE_PTR(it->value);
}
if (it->i >= array->ar_len) {
return NULL;
}
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
{
struct fx_array_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
return FX_ITERATOR_VALUE_CPTR(it->value);
return it->value;
}
static enum fx_status iterator_is_valid(const fx_iterator *obj)
{
struct fx_array_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_ARRAY_ITERATOR);
struct fx_array_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_ARRAY_ITERATOR);
struct fx_array_p *array = it->_a_p;
if (it->i >= array->ar_len) {
return false;
}
if (array->ar_data[it->i] != it->value) {
return false;
}
return (it->value != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA;
}
/*** CLASS DEFINITION *********************************************************/
// ---- fx_array DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_array)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_array)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = array_to_string;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = iterable_begin;
FX_INTERFACE_ENTRY(it_cbegin) = iterable_cbegin;
FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_DEFINITION_END(fx_array)
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_array)
FX_TYPE_DEFINITION_BEGIN(fx_array)
FX_TYPE_ID(0xe3c46da1, 0x5f37, 0x4e44, 0xb53b, 0xff5a6200191b);
@@ -507,18 +486,17 @@ FX_TYPE_DEFINITION_BEGIN(fx_array)
FX_TYPE_DEFINITION_END(fx_array)
// ---- fx_array_iterator DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_array_iterator)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_array_iterator)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
FX_TYPE_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_DEFINITION_END(fx_array_iterator)
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_array_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_array_iterator)
FX_TYPE_ID(0xe5e9e8b8, 0x14cb, 0x4192, 0x8138, 0xf45238a2ae73);
+4 -4
View File
@@ -23,11 +23,11 @@ static void bitbuffer_fini(fx_object *obj, void *priv)
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_bitbuffer)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_bitbuffer)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_bitbuffer)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(fx_bitbuffer)
FX_TYPE_DEFINITION_BEGIN(fx_bitbuffer)
FX_TYPE_ID(0x628e33da, 0x3109, 0x4a5d, 0x98d5, 0xb0e4cb3ccb65);
+14 -7
View File
@@ -1,6 +1,7 @@
#include <fx/bitop.h>
#include <fx/collections/bitmap.h>
#include <fx/stream.h>
#include <fx/value.h>
#include <string.h>
#define BITS_PER_WORD (8 * sizeof(bitmap_word_t))
@@ -306,10 +307,14 @@ static void bitmap_fini(fx_object *obj, void *priv)
struct fx_bitmap_p *map = priv;
}
static void bitmap_to_string(const fx_object *obj, fx_stream *out)
static fx_status bitmap_to_string(
const fx_value *obj,
fx_stream *out,
const char *format)
{
const struct fx_bitmap_p *map
= fx_object_get_private(obj, FX_TYPE_BITMAP);
const struct fx_bitmap_p *map = fx_object_get_private(
obj->v_object,
FX_TYPE_BITMAP);
unsigned char *bytes = (unsigned char *)map->map_words;
size_t nr_bytes = map->map_nr_words * sizeof(bitmap_word_t);
@@ -339,15 +344,17 @@ static void bitmap_to_string(const fx_object *obj, fx_stream *out)
fx_stream_write_fmt(out, NULL, "%c", (c & mask) ? '1' : '0');
}
return FX_SUCCESS;
}
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_bitmap)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_bitmap)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = bitmap_to_string;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_bitmap)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(fx_bitmap)
FX_TYPE_DEFINITION_BEGIN(fx_bitmap)
FX_TYPE_ID(0xea115cef, 0x8a63, 0x445f, 0x9474, 0xba9309d5dde8);
+4 -4
View File
@@ -465,11 +465,11 @@ void buffer_fini(fx_object *obj, void *priv)
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_buffer)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_buffer)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_buffer)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(fx_buffer)
FX_TYPE_DEFINITION_BEGIN(fx_buffer)
FX_TYPE_ID(0x323e6858, 0x7a43, 0x4484, 0xa6fb, 0xe3d1e47ae637);
-732
View File
@@ -1,732 +0,0 @@
#include <fx/collections/dict.h>
#include <fx/hash.h>
#include <fx/status.h>
#include <fx/stream.h>
#include <fx/string.h>
#include <stdbool.h>
#include <stdlib.h>
/*** PRIVATE DATA *************************************************************/
struct fx_dict_bucket_item {
fx_queue_entry bi_entry;
fx_string *bi_str;
fx_object *bi_value;
};
struct fx_dict_bucket {
fx_bst_node bk_node;
uint64_t bk_hash;
fx_queue bk_items;
};
struct fx_dict_p {
fx_bst d_buckets;
};
struct fx_dict_iterator_p {
size_t i;
fx_dict_item item;
fx_dict *_d;
struct fx_dict_p *_d_p;
fx_bst_node *_cbn;
fx_queue_entry *_cqe;
};
/*** MISC FUNCTIONS ***********************************************************/
static FX_BST_DEFINE_SIMPLE_GET(
struct fx_dict_bucket,
uint64_t,
bk_node,
bk_hash,
get_bucket);
static FX_BST_DEFINE_SIMPLE_INSERT(
struct fx_dict_bucket,
bk_node,
bk_hash,
put_bucket);
/*** PRIVATE FUNCTIONS ********************************************************/
static struct fx_dict_bucket *create_bucket(void)
{
struct fx_dict_bucket *bucket = malloc(sizeof *bucket);
if (!bucket) {
return NULL;
}
memset(bucket, 0x0, sizeof *bucket);
return bucket;
}
static struct fx_dict_bucket_item *create_bucket_item(void)
{
struct fx_dict_bucket_item *item = malloc(sizeof *item);
if (!item) {
return NULL;
}
memset(item, 0x0, sizeof *item);
return item;
}
static fx_status dict_put(
struct fx_dict_p *dict,
const char *key,
fx_object *value)
{
uint64_t hash = fx_hash_cstr(key);
struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
if (!bucket) {
bucket = create_bucket();
if (!bucket) {
return FX_ERR_NO_MEMORY;
}
bucket->bk_hash = hash;
put_bucket(&dict->d_buckets, bucket);
}
struct fx_dict_bucket_item *item = create_bucket_item();
if (!item) {
return FX_ERR_NO_MEMORY;
}
item->bi_str = fx_string_create_from_cstr(key);
item->bi_value = fx_object_ref(value);
fx_queue_push_back(&bucket->bk_items, &item->bi_entry);
return FX_SUCCESS;
}
static fx_status dict_put_sk(
struct fx_dict_p *dict,
const fx_string *key,
fx_object *value)
{
uint64_t hash = fx_string_hash(key);
struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
if (!bucket) {
bucket = create_bucket();
if (!bucket) {
return FX_ERR_NO_MEMORY;
}
bucket->bk_hash = hash;
put_bucket(&dict->d_buckets, bucket);
}
struct fx_dict_bucket_item *item = create_bucket_item();
if (!item) {
return FX_ERR_NO_MEMORY;
}
item->bi_str = fx_string_duplicate(key);
item->bi_value = fx_object_ref(value);
fx_queue_push_back(&bucket->bk_items, &item->bi_entry);
return FX_SUCCESS;
}
static fx_object *dict_at(const struct fx_dict_p *dict, const char *key)
{
uint64_t hash = fx_hash_cstr(key);
struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
if (!bucket) {
return NULL;
}
struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items);
while (entry) {
struct fx_dict_bucket_item *item
= fx_unbox(struct fx_dict_bucket_item, entry, bi_entry);
if (!strcmp(fx_string_get_cstr(item->bi_str), key)) {
return item->bi_value;
}
entry = fx_queue_next(entry);
}
return NULL;
}
static fx_object *dict_at_sk(const struct fx_dict_p *dict, const fx_string *key)
{
uint64_t hash = fx_string_hash(key);
struct fx_dict_bucket *bucket = get_bucket(&dict->d_buckets, hash);
if (!bucket) {
return NULL;
}
struct fx_queue_entry *entry = fx_queue_first(&bucket->bk_items);
while (entry) {
struct fx_dict_bucket_item *item
= fx_unbox(struct fx_dict_bucket_item, entry, bi_entry);
if (fx_string_compare(item->bi_str, key)) {
return item->bi_value;
}
entry = fx_queue_next(entry);
}
return NULL;
}
static fx_object *dict_get(struct fx_dict_p *dict, const char *key)
{
fx_object *value = dict_at(dict, key);
if (value) {
fx_object_ref(value);
}
return value;
}
static fx_object *dict_get_sk(struct fx_dict_p *dict, const fx_string *key)
{
fx_object *value = dict_at_sk(dict, key);
if (value) {
fx_object_ref(value);
}
return value;
}
static bool dict_has_key(const struct fx_dict_p *dict, const char *key)
{
return dict_at(dict, key) != NULL;
}
static bool dict_has_skey(const struct fx_dict_p *dict, const fx_string *key)
{
return dict_at_sk(dict, key) != NULL;
}
static size_t dict_get_size(const struct fx_dict_p *dict)
{
size_t count = 0;
#if 0
fx_bst_iterator it1;
fx_queue_iterator it2;
fx_bst_foreach (&it1, &dict->d_buckets) {
struct fx_dict_bucket *bucket
= fx_unbox(struct fx_dict_bucket, it1.node, bk_node);
fx_queue_foreach (&it2, &bucket->bk_items) {
count++;
}
}
#endif
return count;
}
static bool dict_is_empty(const struct fx_dict_p *dict)
{
fx_bst_node *first_node = fx_bst_first(&dict->d_buckets);
struct fx_dict_bucket *first_bucket
= fx_unbox(struct fx_dict_bucket, first_node, bk_node);
if (!first_bucket) {
return true;
}
fx_queue_entry *first_entry = fx_queue_first(&first_bucket->bk_items);
struct fx_dict_bucket_item *first_item
= fx_unbox(struct fx_dict_bucket_item, first_entry, bi_entry);
if (!first_item) {
return true;
}
return false;
}
static bool get_next_node(
struct fx_bst_node *cur_node,
struct fx_queue_entry *cur_entry,
struct fx_bst_node **out_next_node,
struct fx_queue_entry **out_next_entry)
{
struct fx_dict_bucket *cur_bucket
= fx_unbox(struct fx_dict_bucket, cur_node, bk_node);
if (!cur_bucket) {
return false;
}
struct fx_dict_bucket_item *cur_item
= fx_unbox(struct fx_dict_bucket_item, cur_entry, bi_entry);
if (!cur_item) {
return false;
}
struct fx_bst_node *next_node = cur_node;
struct fx_queue_entry *next_entry = fx_queue_next(cur_entry);
if (!next_entry) {
next_node = fx_bst_next(cur_node);
if (!next_node) {
return false;
}
struct fx_dict_bucket *next_bucket
= fx_unbox(struct fx_dict_bucket, next_node, bk_node);
if (!next_bucket) {
return false;
}
next_entry = fx_queue_first(&next_bucket->bk_items);
if (!next_entry) {
return false;
}
}
struct fx_dict_bucket_item *next_item
= fx_unbox(struct fx_dict_bucket_item, next_entry, bi_entry);
if (!next_item) {
return false;
}
*out_next_node = next_node;
*out_next_entry = next_entry;
return true;
}
static fx_status delete_item(
struct fx_dict_p *dict,
struct fx_dict_bucket *bucket,
struct fx_dict_bucket_item *item)
{
fx_queue_delete(&bucket->bk_items, &item->bi_entry);
free(item);
if (fx_queue_empty(&bucket->bk_items)) {
fx_bst_delete(&dict->d_buckets, &bucket->bk_node);
free(bucket);
}
return FX_SUCCESS;
}
/*** PUBLIC FUNCTIONS *********************************************************/
#if 0
fx_dict *fx_dict_create_with_items(const fx_dict_item *items)
{
fx_dict *dict = fx_dict_create();
if (!dict) {
return NULL;
}
struct fx_dict_p *p = fx_object_get_private(dict, FX_TYPE_DICT);
for (size_t i = 0; items[i].key; i++) {
dict_put(p, items[i].key, items[i].value);
}
return dict;
}
#endif
fx_status fx_dict_put(fx_dict *dict, const char *key, fx_object *value)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_put, dict, key, value);
}
fx_status fx_dict_put_sk(fx_dict *dict, const fx_string *key, fx_object *value)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_put_sk, dict, key, value);
}
fx_object *fx_dict_at(const fx_dict *dict, const char *key)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_at, dict, key);
}
fx_object *fx_dict_at_sk(const fx_dict *dict, const fx_string *key)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_at_sk, dict, key);
}
fx_object *fx_dict_get(fx_dict *dict, const char *key)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_get, dict, key);
}
fx_object *fx_dict_get_sk(fx_dict *dict, const fx_string *key)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_get_sk, dict, key);
}
bool fx_dict_has_key(const fx_dict *dict, const char *key)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_has_key, dict, key);
}
bool fx_dict_has_skey(const fx_dict *dict, const fx_string *key)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_DICT, dict_has_skey, dict, key);
}
size_t fx_dict_get_size(const fx_dict *dict)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DICT, dict_get_size, dict);
}
bool fx_dict_is_empty(const fx_dict *dict)
{
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_DICT, dict_is_empty, dict);
}
/*** PUBLIC ALIAS FUNCTIONS ***************************************************/
/*** VIRTUAL FUNCTIONS ********************************************************/
static void dict_init(fx_object *obj, void *priv)
{
struct fx_dict_p *dict = priv;
}
static void dict_fini(fx_object *obj, void *priv)
{
struct fx_dict_p *dict = priv;
struct fx_bst_node *node = fx_bst_first(&dict->d_buckets);
while (node) {
struct fx_dict_bucket *bucket
= fx_unbox(struct fx_dict_bucket, node, bk_node);
struct fx_bst_node *next_node = fx_bst_next(node);
fx_bst_delete(&dict->d_buckets, node);
struct fx_queue_entry *entry
= fx_queue_first(&bucket->bk_items);
while (entry) {
struct fx_dict_bucket_item *item = fx_unbox(
struct fx_dict_bucket_item,
entry,
bi_entry);
struct fx_queue_entry *next_entry
= fx_queue_next(entry);
fx_queue_delete(&bucket->bk_items, entry);
free(item->bi_str);
fx_object_unref(item->bi_value);
free(item);
entry = next_entry;
}
free(bucket);
node = next_node;
}
}
static void dict_to_string(const fx_object *obj, fx_stream *out)
{
struct fx_dict_p *dict = fx_object_get_private(obj, FX_TYPE_DICT);
if (dict_is_empty(dict)) {
fx_stream_write_cstr(out, "{}", NULL);
return;
}
fx_stream_write_cstr(out, "{\n", NULL);
fx_stream_push_indent(out, 1);
size_t len = dict_get_size(dict);
size_t i = 0;
struct fx_bst_node *node = fx_bst_first(&dict->d_buckets);
while (node) {
struct fx_dict_bucket *bucket
= fx_unbox(struct fx_dict_bucket, node, bk_node);
struct fx_queue_entry *entry
= fx_queue_first(&bucket->bk_items);
while (entry) {
struct fx_dict_bucket_item *item = fx_unbox(
struct fx_dict_bucket_item,
entry,
bi_entry);
fx_object_to_string(item->bi_str, out);
fx_stream_write_cstr(out, ": ", NULL);
bool is_string = fx_object_is_type(
item->bi_value,
FX_TYPE_STRING);
if (is_string) {
fx_stream_write_char(out, '"');
}
fx_object_to_string(item->bi_value, out);
if (is_string) {
fx_stream_write_char(out, '"');
}
if (i < len - 1) {
fx_stream_write_cstr(out, ",", NULL);
}
fx_stream_write_char(out, '\n');
entry = fx_queue_next(entry);
i++;
}
node = fx_bst_next(node);
}
fx_stream_pop_indent(out);
fx_stream_write_char(out, '}');
}
/*** ITERATOR FUNCTIONS *******************************************************/
static fx_iterator *iterable_begin(fx_dict *dict)
{
fx_iterator *it_obj = fx_object_create(FX_TYPE_DICT_ITERATOR);
if (!it_obj) {
return NULL;
}
struct fx_dict_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_DICT_ITERATOR);
it->i = 0;
it->_d = dict;
it->_d_p = fx_object_get_private(dict, FX_TYPE_DICT);
if (dict_is_empty(it->_d_p)) {
it->item.key = NULL;
it->item.value = NULL;
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
return it_obj;
}
fx_bst_node *first_node = fx_bst_first(&it->_d_p->d_buckets);
struct fx_dict_bucket *first_bucket
= fx_unbox(struct fx_dict_bucket, first_node, bk_node);
if (!first_bucket) {
it->item.key = NULL;
it->item.value = NULL;
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
return it_obj;
}
fx_queue_entry *first_entry = fx_queue_first(&first_bucket->bk_items);
struct fx_dict_bucket_item *first_item
= fx_unbox(struct fx_dict_bucket_item, first_entry, bi_entry);
if (!first_item) {
it->item.key = NULL;
it->item.value = NULL;
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
return it_obj;
}
it->item.key = first_item->bi_str;
it->item.value = first_item->bi_value;
it->_d = dict;
it->_cbn = first_node;
it->_cqe = first_entry;
return it_obj;
}
static const fx_iterator *iterable_cbegin(const fx_dict *dict)
{
fx_iterator *it_obj = fx_object_create(FX_TYPE_DICT_ITERATOR);
if (!it_obj) {
return NULL;
}
struct fx_dict_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_DICT_ITERATOR);
it->i = 0;
it->_d = (fx_dict *)dict;
it->_d_p = fx_object_get_private(dict, FX_TYPE_DICT);
if (dict_is_empty(it->_d_p)) {
it->item.key = NULL;
it->item.value = NULL;
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
return it_obj;
}
fx_bst_node *first_node = fx_bst_first(&it->_d_p->d_buckets);
struct fx_dict_bucket *first_bucket
= fx_unbox(struct fx_dict_bucket, first_node, bk_node);
if (!first_bucket) {
it->item.key = NULL;
it->item.value = NULL;
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
return it_obj;
}
fx_queue_entry *first_entry = fx_queue_first(&first_bucket->bk_items);
struct fx_dict_bucket_item *first_item
= fx_unbox(struct fx_dict_bucket_item, first_entry, bi_entry);
if (!first_item) {
it->item.key = NULL;
it->item.value = NULL;
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
return it_obj;
}
it->item.key = first_item->bi_str;
it->item.value = first_item->bi_value;
it->_cbn = first_node;
it->_cqe = first_entry;
return it_obj;
}
static enum fx_status iterator_move_next(const fx_iterator *obj)
{
struct fx_dict_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR);
struct fx_bst_node *next_node;
struct fx_queue_entry *next_entry;
if (!get_next_node(it->_cbn, it->_cqe, &next_node, &next_entry)) {
it->item.key = NULL;
it->item.value = NULL;
return FX_ERR_NO_DATA;
}
struct fx_dict_bucket_item *next_item
= fx_unbox(struct fx_dict_bucket_item, next_entry, bi_entry);
if (!next_item) {
it->item.key = NULL;
it->item.value = NULL;
return FX_ERR_NO_DATA;
}
it->i++;
it->item.key = next_item->bi_str;
it->item.value = next_item->bi_value;
it->_cbn = next_node;
it->_cqe = next_entry;
return FX_SUCCESS;
}
static enum fx_status iterator_erase(fx_iterator *obj)
{
struct fx_dict_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR);
if ((it->item.key || it->item.value) && !(it->_cbn && it->_cqe)) {
return FX_ERR_BAD_STATE;
}
if (!it->item.key || !it->_cqe) {
return FX_ERR_NO_DATA;
}
struct fx_bst_node *next_node;
struct fx_queue_entry *next_entry;
if (!get_next_node(it->_cbn, it->_cqe, &next_node, &next_entry)) {
it->item.key = NULL;
it->item.value = NULL;
return FX_ERR_NO_DATA;
}
struct fx_dict_bucket *cur_bucket
= fx_unbox(struct fx_dict_bucket, it->_cbn, bk_node);
struct fx_dict_bucket_item *cur_item
= fx_unbox(struct fx_dict_bucket_item, it->_cqe, bi_entry);
struct fx_dict_bucket_item *next_item
= fx_unbox(struct fx_dict_bucket_item, next_entry, bi_entry);
fx_status status = delete_item(it->_d_p, cur_bucket, cur_item);
if (FX_ERR(status)) {
return status;
}
if (next_item) {
it->item.key = next_item->bi_str;
it->item.value = next_item->bi_value;
it->_cbn = next_node;
it->_cqe = next_entry;
} else {
it->item.key = NULL;
it->item.value = NULL;
it->_cbn = NULL;
it->_cqe = NULL;
}
return FX_SUCCESS;
}
static fx_iterator_value iterator_get_value(fx_iterator *obj)
{
struct fx_dict_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR);
return FX_ITERATOR_VALUE_PTR(&it->item);
}
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
{
struct fx_dict_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DICT_ITERATOR);
return FX_ITERATOR_VALUE_CPTR(&it->item);
}
/*** CLASS DEFINITION *********************************************************/
// ---- fx_dict DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_dict)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = dict_to_string;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = iterable_begin;
FX_INTERFACE_ENTRY(it_cbegin) = iterable_cbegin;
FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_DEFINITION_END(fx_dict)
FX_TYPE_DEFINITION_BEGIN(fx_dict)
FX_TYPE_ID(0xd2af61d9, 0xd0be, 0x4960, 0xbe3f, 0x509749814c10);
FX_TYPE_CLASS(fx_dict_class);
FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE);
FX_TYPE_INSTANCE_PRIVATE(struct fx_dict_p);
FX_TYPE_INSTANCE_INIT(dict_init);
FX_TYPE_INSTANCE_FINI(dict_fini);
FX_TYPE_DEFINITION_END(fx_dict)
// ---- fx_dict_iterator DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_dict_iterator)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
FX_TYPE_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_DEFINITION_END(fx_dict_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_dict_iterator)
FX_TYPE_ID(0x9ea96701, 0x1713, 0x4a3e, 0xbf63, 0xdc856b456f3b);
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_dict_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_dict_iterator_p);
FX_TYPE_DEFINITION_END(fx_dict_iterator)
+88 -67
View File
@@ -32,8 +32,9 @@ struct fx_hashmap_p {
struct fx_hashmap_iterator_p {
size_t i;
fx_hashmap_item item;
fx_value item_value;
fx_hashmap *_h;
const fx_hashmap *_h;
struct fx_hashmap_p *_h_p;
fx_bst_node *_cbn;
fx_queue_entry *_cqe;
@@ -112,14 +113,18 @@ static bool get_next_node(
struct fx_bst_node **out_next_node,
struct fx_queue_entry **out_next_entry)
{
struct fx_hashmap_bucket *cur_bucket
= fx_unbox(struct fx_hashmap_bucket, cur_node, bk_node);
struct fx_hashmap_bucket *cur_bucket = fx_unbox(
struct fx_hashmap_bucket,
cur_node,
bk_node);
if (!cur_bucket) {
return false;
}
struct fx_hashmap_bucket_item *cur_item
= fx_unbox(struct fx_hashmap_bucket_item, cur_entry, bi_entry);
struct fx_hashmap_bucket_item *cur_item = fx_unbox(
struct fx_hashmap_bucket_item,
cur_entry,
bi_entry);
if (!cur_item) {
return false;
}
@@ -146,8 +151,10 @@ static bool get_next_node(
}
}
struct fx_hashmap_bucket_item *next_item
= fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry);
struct fx_hashmap_bucket_item *next_item = fx_unbox(
struct fx_hashmap_bucket_item,
next_entry,
bi_entry);
if (!next_item) {
return false;
}
@@ -186,8 +193,9 @@ static fx_status hashmap_put(
const fx_hashmap_value *value)
{
uint64_t hash = hash_key(key);
struct fx_hashmap_bucket *bucket
= get_bucket(&hashmap->h_buckets, hash);
struct fx_hashmap_bucket *bucket = get_bucket(
&hashmap->h_buckets,
hash);
if (!bucket) {
bucket = create_bucket();
@@ -234,8 +242,9 @@ static const struct fx_hashmap_value *hashmap_get(
{
uint64_t hash = hash_key(key);
struct fx_hashmap_bucket *bucket
= get_bucket(&hashmap->h_buckets, hash);
struct fx_hashmap_bucket *bucket = get_bucket(
&hashmap->h_buckets,
hash);
if (!bucket) {
return NULL;
}
@@ -262,8 +271,9 @@ static bool hashmap_has_key(
const fx_hashmap_key *key)
{
uint64_t hash = hash_key(key);
struct fx_hashmap_bucket *bucket
= get_bucket(&hashmap->h_buckets, hash);
struct fx_hashmap_bucket *bucket = get_bucket(
&hashmap->h_buckets,
hash);
if (!bucket) {
return false;
}
@@ -293,8 +303,10 @@ static size_t hashmap_get_size(const struct fx_hashmap_p *hashmap)
static bool hashmap_is_empty(const struct fx_hashmap_p *hashmap)
{
fx_bst_node *first_node = fx_bst_first(&hashmap->h_buckets);
struct fx_hashmap_bucket *first_bucket
= fx_unbox(struct fx_hashmap_bucket, first_node, bk_node);
struct fx_hashmap_bucket *first_bucket = fx_unbox(
struct fx_hashmap_bucket,
first_node,
bk_node);
if (!first_bucket) {
return true;
}
@@ -358,8 +370,9 @@ fx_hashmap *fx_hashmap_create_with_items(const fx_hashmap_item *items)
return NULL;
}
struct fx_hashmap_p *p
= fx_object_get_private(hashmap, FX_TYPE_HASHMAP);
struct fx_hashmap_p *p = fx_object_get_private(
hashmap,
FX_TYPE_HASHMAP);
for (size_t i = 0; items[i].key.key_data && items[i].key.key_size;
i++) {
@@ -408,12 +421,13 @@ bool fx_hashmap_is_empty(const fx_hashmap *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_TYPE_HASHMAP_ITERATOR);
struct fx_hashmap_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_HASHMAP_ITERATOR);
fx_hashmap_iterator *it_obj = fx_object_create(
FX_TYPE_HASHMAP_ITERATOR);
struct fx_hashmap_iterator_p *it = fx_object_get_private(
it_obj,
FX_TYPE_HASHMAP_ITERATOR);
it->_h = hashmap;
it->_h_p = fx_object_get_private(hashmap, FX_TYPE_HASHMAP);
@@ -426,16 +440,18 @@ fx_iterator *fx_hashmap_begin(fx_hashmap *hashmap)
}
struct fx_bst_node *first_node = fx_bst_first(&it->_h_p->h_buckets);
struct fx_hashmap_bucket *first_bucket
= fx_unbox(struct fx_hashmap_bucket, first_node, bk_node);
struct fx_hashmap_bucket *first_bucket = fx_unbox(
struct fx_hashmap_bucket,
first_node,
bk_node);
if (!first_bucket) {
memset(&it->item, 0x0, sizeof it->item);
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
return it_obj;
}
struct fx_queue_entry *first_entry
= fx_queue_first(&first_bucket->bk_items);
struct fx_queue_entry *first_entry = fx_queue_first(
&first_bucket->bk_items);
struct fx_hashmap_bucket_item *first_item = fx_unbox(
struct fx_hashmap_bucket_item,
first_entry,
@@ -473,8 +489,10 @@ static void hashmap_fini(fx_object *obj, void *priv)
struct fx_bst_node *node = fx_bst_first(&map->h_buckets);
while (node) {
struct fx_hashmap_bucket *b
= fx_unbox(struct fx_hashmap_bucket, node, bk_node);
struct fx_hashmap_bucket *b = fx_unbox(
struct fx_hashmap_bucket,
node,
bk_node);
struct fx_bst_node *next_node = fx_bst_next(node);
fx_bst_delete(&map->h_buckets, node);
@@ -484,8 +502,8 @@ static void hashmap_fini(fx_object *obj, void *priv)
struct fx_hashmap_bucket_item,
entry,
bi_entry);
struct fx_queue_entry *next_entry
= fx_queue_next(entry);
struct fx_queue_entry *next_entry = fx_queue_next(
entry);
fx_queue_delete(&b->bk_items, entry);
if (map->h_key_dtor) {
@@ -510,8 +528,9 @@ static void hashmap_fini(fx_object *obj, void *priv)
static enum fx_status iterator_move_next(const fx_iterator *obj)
{
struct fx_hashmap_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
struct fx_hashmap_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_HASHMAP_ITERATOR);
struct fx_bst_node *next_node;
struct fx_queue_entry *next_entry;
@@ -520,8 +539,10 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
return FX_ERR_NO_DATA;
}
struct fx_hashmap_bucket_item *next_item
= fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry);
struct fx_hashmap_bucket_item *next_item = fx_unbox(
struct fx_hashmap_bucket_item,
next_entry,
bi_entry);
if (!next_item) {
memset(&it->item, 0x0, sizeof it->item);
@@ -540,8 +561,9 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
static enum fx_status iterator_erase(fx_iterator *obj)
{
struct fx_hashmap_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
struct fx_hashmap_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_HASHMAP_ITERATOR);
if ((it->item.key.key_data || it->item.value.value_data)
&& !(it->_cbn && it->_cqe)) {
@@ -559,13 +581,19 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_ERR_NO_DATA;
}
struct fx_hashmap_bucket *cur_bucket
= fx_unbox(struct fx_hashmap_bucket, it->_cbn, bk_node);
struct fx_hashmap_bucket_item *cur_item
= fx_unbox(struct fx_hashmap_bucket_item, it->_cqe, bi_entry);
struct fx_hashmap_bucket *cur_bucket = fx_unbox(
struct fx_hashmap_bucket,
it->_cbn,
bk_node);
struct fx_hashmap_bucket_item *cur_item = fx_unbox(
struct fx_hashmap_bucket_item,
it->_cqe,
bi_entry);
struct fx_hashmap_bucket_item *next_item
= fx_unbox(struct fx_hashmap_bucket_item, next_entry, bi_entry);
struct fx_hashmap_bucket_item *next_item = fx_unbox(
struct fx_hashmap_bucket_item,
next_entry,
bi_entry);
fx_status status = delete_item(it->_h_p, cur_bucket, cur_item);
if (FX_ERR(status)) {
@@ -590,33 +618,27 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_SUCCESS;
}
static fx_iterator_value iterator_get_value(fx_iterator *obj)
static const fx_value *iterator_get_value(const fx_iterator *obj)
{
struct fx_hashmap_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
return FX_ITERATOR_VALUE_PTR(&it->item);
}
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
{
const struct fx_hashmap_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_HASHMAP_ITERATOR);
return FX_ITERATOR_VALUE_CPTR(&it->item);
struct fx_hashmap_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_HASHMAP_ITERATOR);
it->item_value = FX_POINTER(&it->item);
return &it->item_value;
}
/*** CLASS DEFINITION *********************************************************/
// ---- fx_hashmap DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_hashmap)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_hashmap)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_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_cbegin) = fx_hashmap_cbegin;
FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_DEFINITION_END(fx_hashmap)
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_hashmap)
FX_TYPE_DEFINITION_BEGIN(fx_hashmap)
FX_TYPE_ID(0x7bf5bcd1, 0x1ff3, 0x4e43, 0xbed8, 0x7c74f28348bf);
@@ -628,18 +650,17 @@ FX_TYPE_DEFINITION_BEGIN(fx_hashmap)
FX_TYPE_DEFINITION_END(fx_hashmap)
// ---- fx_hashmap_iterator DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_hashmap_iterator)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_hashmap_iterator)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
FX_TYPE_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_DEFINITION_END(fx_hashmap_iterator)
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_hashmap_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_hashmap_iterator)
FX_TYPE_ID(0xd9658456, 0xdd80, 0x419a, 0xb23a, 0xb513013e6431);
+695
View File
@@ -0,0 +1,695 @@
#include <assert.h>
#include <fx/collections/hashtable.h>
#include <fx/misc.h>
#include <fx/reflection/property.h>
#include <fx/status.h>
#include <fx/string.h>
#include <fx/vector.h>
#include <stdbool.h>
#include <stdlib.h>
/*** PRIVATE DATA *************************************************************/
/* these primes are used as array capacities to avoid hash collisions. */
static const size_t primes[] = {
11,
23,
47,
97,
197,
397,
797,
1597,
3203,
6421,
12853,
25717,
51437,
};
struct table_item {
fx_hashtable_item *i_item;
};
struct fx_hashtable_item_p {
fx_hashtable_item *i_self;
fx_value i_key, i_value;
};
struct fx_hashtable_p {
struct table_item *t_items;
/* number of items currently stored in t_items */
size_t t_count;
/* index in primes[] that represents the current maximum size of
* t_items */
size_t t_max_index;
};
struct fx_hashtable_iterator_p {
size_t i;
fx_hashtable_item *item;
fx_value item_value;
const fx_hashtable *_h;
struct fx_hashtable_p *_h_p;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static fx_status convert_key_to_index(
const fx_value *v,
struct table_item *table,
size_t table_size,
size_t *out_index)
{
uint64_t hash = 0;
fx_status status = fx_value_hash(v, &hash);
if (!FX_OK(status)) {
return status;
}
size_t index = hash % table_size, i = 1;
bool index_found = false;
while (table[index].i_item && i <= table_size) {
const struct fx_hashtable_item_p *item = fx_object_get_private(
table[index].i_item,
FX_TYPE_HASHTABLE_ITEM);
int cmp = fx_value_compare(&item->i_key, v);
if (cmp == 0) {
index_found = true;
break;
}
index = (index + (i * i)) % table_size;
i++;
}
if (!index_found) {
return FX_ERR_NO_ENTRY;
}
assert(index < table_size);
*out_index = index;
return FX_SUCCESS;
}
static fx_status allocate_index_for_key(
const fx_value *v,
struct table_item *table,
size_t table_size,
size_t *out_index)
{
uint64_t hash = 0;
fx_status status = fx_value_hash(v, &hash);
if (!FX_OK(status)) {
return status;
}
size_t index = hash % table_size;
bool ok = false;
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(
table[index].i_item,
FX_TYPE_HASHTABLE_ITEM);
int cmp = fx_value_compare(&item->i_key, v);
if (cmp == 0) {
ok = true;
break;
}
index = (index + (i * i)) % table_size;
i++;
}
if (!ok) {
return FX_ERR_NO_SPACE;
}
assert(index < table_size);
*out_index = index;
return FX_SUCCESS;
}
static fx_status add_item_to_table(
fx_hashtable_item *item,
struct table_item *table,
size_t table_capacity)
{
size_t index = 0;
struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
fx_status status = allocate_index_for_key(
&item_p->i_key,
table,
table_capacity,
&index);
if (!FX_OK(status)) {
return status;
}
if (index >= table_capacity) {
return FX_ERR_NO_SPACE;
}
struct table_item *bucket = &table[index];
if (bucket->i_item) {
fx_hashtable_item_unref(bucket->i_item);
}
bucket->i_item = item;
return FX_SUCCESS;
}
static fx_status destroy_table(
struct table_item *table,
size_t length,
bool destroy_items)
{
if (!destroy_items) {
free(table);
return FX_SUCCESS;
}
for (size_t i = 0; i < length; i++) {
struct table_item *item = &table[i];
if (!item->i_item) {
continue;
}
fx_hashtable_item_unref(item->i_item);
}
free(table);
return FX_SUCCESS;
}
static fx_status migrate_table(
struct table_item *src_table,
size_t src_length,
struct table_item *dest_table,
size_t dest_capacity)
{
for (size_t i = 0; i < src_length; i++) {
struct table_item *item = &src_table[i];
if (!item->i_item) {
continue;
}
add_item_to_table(item->i_item, dest_table, dest_capacity);
}
return FX_SUCCESS;
}
static fx_status resize_table(
struct fx_hashtable_p *hashtable,
size_t new_capacity_index)
{
size_t capacity = primes[new_capacity_index];
struct table_item *new_table = calloc(capacity, sizeof *new_table);
if (!new_table) {
return FX_ERR_NO_MEMORY;
}
fx_status status = migrate_table(
hashtable->t_items,
primes[hashtable->t_max_index],
new_table,
capacity);
if (!FX_OK(status)) {
free(new_table);
return status;
}
free(hashtable->t_items);
hashtable->t_items = new_table;
hashtable->t_max_index = new_capacity_index;
return FX_SUCCESS;
}
static fx_status hashtable_put(
struct fx_hashtable_p *hashtable,
fx_value *key,
fx_value *value)
{
fx_status status = FX_SUCCESS;
size_t capacity = primes[hashtable->t_max_index];
if (hashtable->t_count + 1 > capacity) {
status = resize_table(hashtable, hashtable->t_max_index + 1);
if (!FX_OK(status)) {
return status;
}
capacity = primes[hashtable->t_max_index];
}
size_t index = 0;
do {
status = allocate_index_for_key(
key,
hashtable->t_items,
capacity,
&index);
if (status == FX_ERR_NO_SPACE) {
status = resize_table(
hashtable,
hashtable->t_max_index + 1);
if (!FX_OK(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);
if (!item) {
return FX_ERR_NO_MEMORY;
}
struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
fx_value_copy(&item_p->i_key, key);
fx_value_copy(&item_p->i_value, value);
hashtable->t_items[index].i_item = item;
hashtable->t_count++;
return FX_SUCCESS;
}
static const fx_value *hashtable_get(
const struct fx_hashtable_p *hashtable,
const fx_value *key)
{
size_t index = 0;
size_t capacity = primes[hashtable->t_max_index];
fx_status status = convert_key_to_index(
key,
hashtable->t_items,
capacity,
&index);
if (!FX_OK(status)) {
return NULL;
}
const fx_hashtable_item *item = hashtable->t_items[index].i_item;
if (!item) {
return NULL;
}
const struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
return &item_p->i_value;
}
static size_t hashtable_get_count(const struct fx_hashtable_p *hashtable)
{
return hashtable->t_count;
}
static bool hashtable_is_empty(const struct fx_hashtable_p *hashtable)
{
return hashtable->t_count == 0;
}
static const fx_value *hashtable_item_get_key(
const struct fx_hashtable_item_p *item)
{
return &item->i_key;
}
static const fx_value *hashtable_item_get_value(
const struct fx_hashtable_item_p *item)
{
return &item->i_value;
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_status fx_hashtable_put(
fx_hashtable *hashtable,
fx_value *key,
fx_value *value)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_HASHTABLE,
hashtable_put,
hashtable,
key,
value);
}
const fx_value *fx_hashtable_get(
const fx_hashtable *hashtable,
const fx_value *key)
{
FX_CLASS_DISPATCH_STATIC(
FX_TYPE_HASHTABLE,
hashtable_get,
hashtable,
key);
}
size_t fx_hashtable_get_count(const fx_hashtable *hashtable)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_HASHTABLE,
hashtable_get_count,
hashtable);
}
bool fx_hashtable_is_empty(const fx_hashtable *hashtable)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_HASHTABLE,
hashtable_is_empty,
hashtable);
}
const fx_value *fx_hashtable_item_get_key(const fx_hashtable_item *item)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_HASHTABLE_ITEM,
hashtable_item_get_key,
item);
}
const fx_value *fx_hashtable_item_get_value(const fx_hashtable_item *item)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_TYPE_HASHTABLE_ITEM,
hashtable_item_get_value,
item);
}
const fx_iterator *fx_hashtable_begin(const fx_hashtable *hashtable)
{
fx_hashtable_iterator *it_obj
= fx_object_create(FX_TYPE_HASHTABLE_ITERATOR);
struct fx_hashtable_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_HASHTABLE_ITERATOR);
it->_h = hashtable;
it->_h_p = fx_object_get_private(hashtable, FX_TYPE_HASHTABLE);
it->i = 0;
if (fx_hashtable_is_empty(hashtable)) {
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
return it_obj;
}
struct table_item *table = it->_h_p->t_items;
size_t capacity = primes[it->_h_p->t_max_index];
while (it->i < capacity && table[it->i].i_item == NULL) {
it->i++;
}
it->item_value = FX_VALUE_OBJECT_REF(table[it->i].i_item);
return it_obj;
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void hashtable_init(fx_object *obj, void *priv)
{
struct fx_hashtable_p *map = priv;
map->t_max_index = 0;
map->t_count = 0;
size_t capacity = primes[map->t_max_index];
map->t_items = calloc(capacity, sizeof *map->t_items);
}
static void hashtable_fini(fx_object *obj, void *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(
const fx_value *obj,
fx_stream *out,
const char *format)
{
struct fx_hashtable_p *hashtable
= fx_object_get_private(obj->v_object, FX_TYPE_HASHTABLE);
if (!hashtable->t_count) {
fx_stream_write_cstr(out, "{}", NULL);
return FX_SUCCESS;
}
fx_stream_write_cstr(out, "{\n", NULL);
fx_stream_push_indent(out, 1);
size_t nr_written = 0;
size_t capacity = primes[hashtable->t_max_index];
for (size_t i = 0; i < capacity; i++) {
fx_hashtable_item *item = hashtable->t_items[i].i_item;
if (!item) {
continue;
}
nr_written++;
struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
const char *cstr = NULL;
fx_value_get_cstr(&item_p->i_key, &cstr);
if (cstr) {
fx_stream_write_char(out, '"');
}
fx_value_to_string(&item_p->i_key, out, NULL);
if (cstr) {
fx_stream_write_char(out, '"');
}
fx_stream_write_cstr(out, " = ", NULL);
fx_value_get_cstr(&item_p->i_value, &cstr);
if (cstr) {
fx_stream_write_char(out, '"');
}
fx_value_to_string(&item_p->i_value, out, NULL);
if (cstr) {
fx_stream_write_char(out, '"');
}
if (nr_written != hashtable->t_count) {
fx_stream_write_char(out, ',');
}
fx_stream_write_char(out, '\n');
}
fx_stream_pop_indent(out);
fx_stream_write_char(out, '}');
return FX_SUCCESS;
}
static fx_status get_key(
const fx_value *item_value,
const fx_property *prop,
fx_value *out)
{
fx_hashtable_item *item = NULL;
fx_value_get_object(item_value, &item);
if (!item) {
return FX_ERR_NOT_SUPPORTED;
}
struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
fx_value_copy(out, &item_p->i_key);
return FX_SUCCESS;
}
static fx_status get_value(
const fx_value *item_value,
const fx_property *prop,
fx_value *out)
{
fx_hashtable_item *item = NULL;
fx_value_get_object(item_value, &item);
if (!item) {
return FX_ERR_NOT_SUPPORTED;
}
struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
fx_value_copy(out, &item_p->i_value);
return FX_SUCCESS;
}
/*** ITERATOR FUNCTIONS *******************************************************/
static enum fx_status iterator_move_next(const fx_iterator *obj)
{
struct fx_hashtable_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_HASHTABLE_ITERATOR);
fx_value_unset(&it->item_value);
struct table_item *table = it->_h_p->t_items;
size_t capacity = primes[it->_h_p->t_max_index];
it->i++;
while (it->i < capacity && table[it->i].i_item == NULL) {
it->i++;
}
if (it->i >= capacity || !table[it->i].i_item) {
return FX_ERR_NO_DATA;
}
it->item_value = FX_VALUE_OBJECT_REF(table[it->i].i_item);
return FX_SUCCESS;
}
static enum fx_status iterator_erase(fx_iterator *obj)
{
struct fx_hashtable_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_HASHTABLE_ITERATOR);
struct table_item *table = it->_h_p->t_items;
size_t capacity = primes[it->_h_p->t_max_index];
if (table[it->i].i_item) {
fx_hashtable_item_unref(table[it->i].i_item);
table[it->i].i_item = NULL;
}
it->i++;
while (it->i < capacity && table[it->i].i_item == NULL) {
it->i++;
}
if (it->i >= capacity || !table[it->i].i_item) {
return FX_ERR_NO_DATA;
}
return FX_SUCCESS;
}
static const fx_value *iterator_get_value(const fx_iterator *obj)
{
struct fx_hashtable_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_HASHTABLE_ITERATOR);
struct table_item *table = it->_h_p->t_items;
size_t capacity = primes[it->_h_p->t_max_index];
if (table[it->i].i_item) {
return &it->item_value;
}
return NULL;
}
/*** CLASS DEFINITION *********************************************************/
// ---- fx_hashtable DEFINITION
FX_TYPE_CLASS_BEGIN(fx_hashtable)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = to_string;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = fx_hashtable_begin;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_hashtable)
FX_TYPE_DEFINITION_BEGIN(fx_hashtable)
FX_TYPE_ID(0xc73b431c, 0x5bd5, 0x45ac, 0x888b, 0x35d352db1fe4);
FX_TYPE_NAME("fx.collections.hashtable");
FX_TYPE_CLASS(fx_hashtable_class);
FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE);
FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_p);
FX_TYPE_INSTANCE_INIT(hashtable_init);
FX_TYPE_INSTANCE_FINI(hashtable_fini);
FX_TYPE_DEFINITION_END(fx_hashtable)
// ---- fx_hashtable_iterator DEFINITION
FX_TYPE_CLASS_BEGIN(fx_hashtable_iterator)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_hashtable_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_hashtable_iterator)
FX_TYPE_ID(0x42c6836d, 0xf801, 0x49f9, 0xba58, 0xf9cd6f1cb22d);
FX_TYPE_NAME("fx.collections.hashtable.iterator");
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_hashtable_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_iterator_p);
FX_TYPE_INSTANCE_FINI(hashtable_iterator_fini);
FX_TYPE_DEFINITION_END(fx_hashtable_iterator)
// ---- fx_hashtable_item DEFINITION
FX_TYPE_CLASS_BEGIN(fx_hashtable_item)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_PROPERTY("key", get_key, NULL);
FX_TYPE_PROPERTY("value", get_value, NULL);
FX_TYPE_CLASS_END(fx_hashtable_item)
FX_TYPE_DEFINITION_BEGIN(fx_hashtable_item)
FX_TYPE_ID(0x748f78f3, 0x53df, 0x406c, 0x8777, 0x2075bd11fb97);
FX_TYPE_NAME("fx.collections.hashtable.item");
FX_TYPE_EXTENDS(FX_TYPE_OBJECT);
FX_TYPE_CLASS(fx_hashtable_item_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_hashtable_item_p);
FX_TYPE_INSTANCE_FINI(hashtable_item_fini);
FX_TYPE_DEFINITION_END(fx_hashtable_item)
+15 -173
View File
@@ -1,13 +1,5 @@
/**
* A heterogeneous array of objects. fx_array only stores references
* to the objects that it contains, not the object data itself.
*
* fx_array stores pointers to objects in a single contiguous array,
* but this is an implementation detail that may change in the future.
* Users of fx_array should not rely on this being the case.
*/
#ifndef FX_DS_ARRAY_H_
#define FX_DS_ARRAY_H_
#ifndef FX_COLLECTIONS_ARRAY_H_
#define FX_COLLECTIONS_ARRAY_H_
#include <fx/iterator.h>
#include <fx/macros.h>
@@ -30,192 +22,42 @@ FX_TYPE_CLASS_DECLARATION_END(fx_array)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_array_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_array_iterator)
FX_API fx_type fx_array_get_type(void);
FX_API fx_type fx_array_iterator_get_type(void);
FX_API fx_type_id fx_array_get_type(void);
FX_API fx_type_id fx_array_iterator_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_array, FX_TYPE_ARRAY);
/**
* Creates an fx_array initialised with the contents of the provided
* fx_object pointer array. The fx_array will take a reference to each
* object specified in `values`, and will increment the reference count.
* The order of objects in the new fx_array will be the same as the order
* of objects in `values`. Any NULL pointers in the `values` array will
* be ignored, and will not result in gaps in the created fx_array.
* However, `nr_values` should be large enough to cover the final
* non-NULL pointer in `values`, including any NULL pointers in-between.
*
* @param values The list of object pointers which should make up the
* contents of the new fx_array.
* @param nr_values The size of the `values` array.
* @return A pointer to the new fx_array, or NULL if an error occurred.
*/
FX_API fx_array *fx_array_create_with_values(
fx_object *const *values,
const fx_value *values,
size_t nr_values);
/**
* Remove all object references from an fx_array, resetting the size of the
* array to zero. The reference counts of all objects in the array will be
* decremented.
*
* @param array The fx_array to clear.
*/
FX_API void fx_array_clear(fx_array *array);
/**
* Inserts an object at the end of an fx_array. The reference count of
* the object will be incremented.
*
* @param array The fx_array to append the object to.
* @param value The object to append.
* @return FX_SUCCESS if the object was appended successfully, or an
* error code if an error occurred.
*/
FX_API fx_status fx_array_append(fx_array *array, fx_object *value);
FX_API fx_status fx_array_push_back(fx_array *array, fx_value value);
/**
* Inserts an object at the beginning of an fx_array. The reference count
* of the object will be incremented. All other objects in the array
* will be moved to make space for the object being pre-pended.
*
* @param array The fx_array to prepend the object to.
* @param value The object to prepend.
* @return FX_SUCCESS if the object was prepended successfully, or an
* error code if an error occurred.
*/
FX_API fx_status fx_array_prepend(fx_array *array, fx_object *value);
FX_API fx_status fx_array_push_front(fx_array *array, fx_value value);
/**
* Inserts an object into an fx_array at a given index. The reference
* count of the object will be incremented. If the specified index is at
* the beginning or mid-way through the array (i.e. not at the end),
* some or all of the objects already in the array will be moved to make
* space for the object being inserted.
*
* @param array The fx_array to insert the object into.
* @param value The object to insert.
* @param at The index to insert the object at. If the index is
* `FX_NPOS`, the object will be inserted at the end of the fx_array.
* @return FX_SUCCESS if the object was inserted, or a status code
* describing any error that occurred.
*/
FX_API fx_status fx_array_insert(fx_array *array, fx_object *value, size_t at);
FX_API fx_status fx_array_insert(fx_array *array, fx_value value, size_t at);
/**
* Removes the object at the specified index from an fx_array. The
* reference count of the removed object will be decremented. If the
* specified index is at the beginning or mid-way through the array
* (i.e. not at the end), the remaining objects will be moved to fill
* the empty space created by the object's removal.
*
* @param array The fx_array to remove the object from.
* @param at The index of the object to be removed.
* @return FX_SUCCESS if the object was removed, or a status code
* describing any error that occurred.
*/
FX_API fx_status fx_array_remove(fx_array *array, size_t at);
/**
* Removes the object at the beginning of an fx_array. The reference count
* of the removed object will be decremented. The remaining objects will be
* moved to fill the empty space created by the object's removal.
*
* @param array The fx_array to remove the object from.
* @return FX_SUCCESS if the object was removed, or a status code describing any
* error that occurred.
*/
FX_API fx_status fx_array_remove_front(fx_array *array);
/**
* Removes the object at the end of an fx_array. The reference count
* of the removed object will be decremented.
*
* @param array The fx_array to remove the object from.
* @return FX_SUCCESS if the object was removed, or a status code describing any
* error that occurred.
*/
FX_API fx_status fx_array_remove_back(fx_array *array);
/**
* Removes the object at the specified index of an fx_array, and returns
* a pointer to it. The reference count of the removed object will NOT
* be decremented. The caller becomes the owner of the array's reference
* to the object. If the specified index is at the beginning or mid-way
* through the array (i.e. not at the end), the remaining objects will
* be moved to fill the empty space created by the object's removal.
*
* @param array The fx_array to remove the object from.
* @param at The index of the object to be removed.
* @return An pointer to the removed object. This pointer is owned by
* the caller. Returns NULL if an error occurred.
*/
FX_API fx_object *fx_array_pop(fx_array *array, size_t at);
FX_API fx_value fx_array_pop(fx_array *array, size_t at);
/**
* Removes the object at the beginning of an fx_array, and returns a
* pointer to it. The reference count of the removed object will NOT be
* decremented. The caller becomes the owner of the array's reference to
* the object. The remaining objects in the fx_array will be moved to
* fill the empty space left by the removed object.
*
* @param array The fx_array to remove the object from.
* @return An pointer to the removed object. This pointer is owned by
* the caller. Returns NULL if an error occurred.
*/
FX_API fx_object *fx_array_pop_front(fx_array *array);
FX_API fx_value fx_array_pop_front(fx_array *array);
/**
* Removes the object at the end of an fx_array, and returns a pointer to it.
* The reference count of the removed object will NOT be decremented. The caller
* becomes the owner of the array's reference to the object.
*
* @param array The fx_array to remove the object from.
* @return An pointer to the removed object. This pointer is owned by the
* caller. Returns NULL if an error occurred.
*/
FX_API fx_object *fx_array_pop_back(fx_array *array);
FX_API fx_value fx_array_pop_back(fx_array *array);
/**
* Returns an unowned pointer to the object at the given index of an fx_array.
* The caller does not own the returned pointer, and MUST NOT release it.
*
* @param array The fx_array.
* @param at The index of the object to return.
* @return A pointer to the object at the given index. This pointer is NOT owned
* by the caller. Returns NULL if an error occurred.
*/
FX_API fx_object *fx_array_at(const fx_array *array, size_t at);
FX_API fx_value *fx_array_get_ref(const fx_array *array, size_t at);
/**
* Returns an owned pointer to the object at the given index of an
* fx_array. The caller owns the returned pointer, and must release it
* when they are finished with it.
*
* @param array The fx_array.
* @param at The index of the object to return.
* @return A pointer to the object at the given index. This pointer is
* owned by the caller. Returns NULL if an error occurred.
*/
FX_API fx_object *fx_array_get(fx_array *array, size_t at);
FX_API fx_value fx_array_get(const fx_array *array, size_t at);
/**
* Returns the number of objects contained in an fx_array.
*
* @param array The fx_array.
* @return The number of objects contained in the fx_array.
*/
FX_API size_t fx_array_size(const fx_array *array);
FX_API size_t fx_array_get_size(const fx_array *array);
/**
* Returns the current maximum capacity of an fx_array. This represents
* the number of objects that can be stored in an fx_array before its
* internal buffer would need to be re-sized.
*
* @param array The fx_array.
* @return The maximum capacity of the fx_array.
*/
FX_API size_t fx_array_capacity(const fx_array *array);
FX_API size_t fx_array_get_capacity(const fx_array *array);
FX_DECLS_END;
@@ -14,7 +14,7 @@ FX_DECLARE_TYPE(fx_bitmap);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_bitmap)
FX_TYPE_CLASS_DECLARATION_END(fx_bitmap)
FX_API fx_type fx_bitmap_get_type(void);
FX_API fx_type_id fx_bitmap_get_type(void);
FX_API fx_bitmap *fx_bitmap_create(size_t nr_bits);
@@ -13,7 +13,7 @@ FX_DECLARE_TYPE(fx_buffer);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_buffer)
FX_TYPE_CLASS_DECLARATION_END(fx_buffer)
FX_API fx_type fx_buffer_get_type(void);
FX_API fx_type_id fx_buffer_get_type(void);
FX_API fx_buffer *fx_buffer_create(size_t item_sz);
FX_API fx_buffer *fx_buffer_create_from_bytes(const void *p, size_t len);
@@ -1,64 +0,0 @@
#ifndef FX_DS_DICT_H_
#define FX_DS_DICT_H_
#include <fx/bst.h>
#include <fx/macros.h>
#include <fx/misc.h>
#include <fx/queue.h>
#include <fx/status.h>
#include <fx/string.h>
FX_DECLS_BEGIN;
#define FX_TYPE_DICT (fx_dict_get_type())
#define FX_TYPE_DICT_ITERATOR (fx_dict_iterator_get_type())
struct fx_dict_p;
FX_DECLARE_TYPE(fx_dict);
FX_DECLARE_TYPE(fx_dict_iterator);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_dict)
FX_TYPE_CLASS_DECLARATION_END(fx_dict)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_dict_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_dict_iterator)
#define FX_DICT_ITEM(k, v) {.key = (k), .value = (v)}
#define FX_DICT_ITEM_END {.key = NULL, .value = NULL}
#define fx_dict_foreach(it, dict) \
for (int z__fx_unique_name() = fx_dict_iterator_begin(dict, it); \
(it)->key != NULL; \
fx_dict_iterator_next(it))
typedef struct fx_dict_item {
const fx_string *key;
fx_object *value;
} fx_dict_item;
FX_API fx_type fx_dict_get_type(void);
FX_API fx_type fx_dict_iterator_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_dict, FX_TYPE_DICT);
#if 0
FX_API fx_dict *fx_dict_create_with_items(const fx_dict_item *items);
#endif
FX_API fx_status fx_dict_put(fx_dict *dict, const char *key, fx_object *value);
FX_API fx_status
fx_dict_put_sk(fx_dict *dict, const fx_string *key, fx_object *value);
FX_API fx_object *fx_dict_at(const fx_dict *dict, const char *key);
FX_API fx_object *fx_dict_at_sk(const fx_dict *dict, const fx_string *key);
FX_API fx_object *fx_dict_get(fx_dict *dict, const char *key);
FX_API fx_object *fx_dict_get_sk(fx_dict *dict, const fx_string *key);
FX_API bool fx_dict_has_key(const fx_dict *dict, const char *key);
FX_API bool fx_dict_has_skey(const fx_dict *dict, const fx_string *key);
FX_API size_t fx_dict_get_size(const fx_dict *dict);
FX_API bool fx_dict_is_empty(const fx_dict *dict);
FX_DECLS_END;
#endif
@@ -2,6 +2,7 @@
#define FX_DS_HASHMAP_H_
#include <fx/bst.h>
#include <fx/iterator.h>
#include <fx/macros.h>
#include <fx/misc.h>
#include <fx/queue.h>
@@ -60,8 +61,8 @@ typedef struct fx_hashmap_item {
fx_hashmap_value value;
} fx_hashmap_item;
FX_API fx_type fx_hashmap_get_type(void);
FX_API fx_type fx_hashmap_iterator_get_type(void);
FX_API fx_type_id fx_hashmap_get_type(void);
FX_API fx_type_id fx_hashmap_iterator_get_type(void);
FX_API fx_hashmap *fx_hashmap_create(
fx_hashmap_key_destructor key_dtor,
@@ -82,7 +83,7 @@ FX_API bool fx_hashmap_has_key(
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 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_DECLS_END;
@@ -0,0 +1,56 @@
#ifndef FX_COLLECTIONS_HASHTABLE_H_
#define FX_COLLECTIONS_HASHTABLE_H_
#include <fx/bst.h>
#include <fx/iterator.h>
#include <fx/macros.h>
#include <fx/misc.h>
#include <fx/queue.h>
#include <fx/status.h>
#include <fx/value.h>
#include <stddef.h>
FX_DECLS_BEGIN;
#define FX_TYPE_HASHTABLE (fx_hashtable_get_type())
#define FX_TYPE_HASHTABLE_ITERATOR (fx_hashtable_iterator_get_type())
#define FX_TYPE_HASHTABLE_ITEM (fx_hashtable_item_get_type())
FX_DECLARE_TYPE(fx_hashtable);
FX_DECLARE_TYPE(fx_hashtable_iterator);
FX_DECLARE_TYPE(fx_hashtable_item);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashtable)
FX_TYPE_CLASS_DECLARATION_END(fx_hashtable)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashtable_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_hashtable_iterator)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_hashtable_item)
FX_TYPE_CLASS_DECLARATION_END(fx_hashtable_item)
FX_API fx_type_id fx_hashtable_get_type(void);
FX_API fx_type_id fx_hashtable_iterator_get_type(void);
FX_API fx_type_id fx_hashtable_item_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_hashtable, FX_TYPE_HASHTABLE);
FX_API fx_hashtable *fx_hashtable_create(void);
FX_API fx_status
fx_hashtable_put(fx_hashtable *hashtable, fx_value *key, fx_value *value);
FX_API const fx_value *fx_hashtable_get(
const fx_hashtable *hashtable,
const fx_value *key);
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 const fx_iterator *fx_hashtable_begin(const fx_hashtable *hashtable);
FX_API const fx_value *fx_hashtable_item_get_key(const fx_hashtable_item *item);
FX_API const fx_value *fx_hashtable_item_get_value(
const fx_hashtable_item *item);
FX_DECLS_END;
#endif
+3 -4
View File
@@ -23,8 +23,8 @@ FX_TYPE_CLASS_DECLARATION_END(fx_list_iterator)
typedef struct fx_list_entry fx_list_entry;
FX_API fx_type fx_list_get_type(void);
FX_API fx_type fx_list_iterator_get_type(void);
FX_API fx_type_id fx_list_get_type(void);
FX_API fx_type_id fx_list_iterator_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_list, FX_TYPE_LIST);
@@ -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 fx_iterator *fx_list_begin(fx_list *q);
FX_API const fx_iterator *fx_list_cbegin(const fx_list *q);
FX_API const fx_iterator *fx_list_begin(const fx_list *q);
FX_DECLS_END;
@@ -1,58 +0,0 @@
#ifndef FX_DS_TREE_H_
#define FX_DS_TREE_H_
#include <fx/macros.h>
#include <fx/misc.h>
#include <fx/queue.h>
#include <fx/string.h>
FX_DECLS_BEGIN;
#define FX_TYPE_TREE (fx_tree_get_type())
#define FX_TYPE_TREE_ITERATOR (fx_tree_iterator_get_type())
FX_DECLARE_TYPE(fx_tree);
FX_DECLARE_TYPE(fx_tree_iterator);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_tree)
FX_TYPE_CLASS_DECLARATION_END(fx_tree)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_tree_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_tree_iterator)
#define FX_TREE_NODE_INIT ((fx_tree_node) {0})
#define FX_TREE_CONTAINER(t, m, v) \
((void *)((v) ? (uintptr_t)(v) - (offsetof(t, m)) : 0))
typedef struct fx_tree_node {
struct fx_tree_node *__p01, *__p02, *__p03;
struct fx_queue_entry __q01;
} fx_tree_node;
FX_API fx_type fx_tree_get_type(void);
FX_API fx_type fx_tree_iterator_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_tree, FX_TYPE_TREE);
FX_API void fx_tree_set_root(fx_tree *tree, struct fx_tree_node *node);
FX_API void fx_tree_node_add_child(fx_tree_node *parent, fx_tree_node *child);
FX_API void fx_tree_node_add_sibling(fx_tree_node *node, fx_tree_node *to_add);
FX_API fx_tree_node *fx_tree_node_get_child(fx_tree_node *node, size_t at);
FX_API fx_tree_node *fx_tree_node_get_parent(fx_tree_node *node);
FX_API fx_iterator *fx_tree_begin(fx_tree *tree);
FX_API const fx_iterator *fx_tree_cbegin(const fx_tree *tree);
FX_API fx_iterator *fx_tree_node_begin(fx_tree_node *node);
FX_API const fx_iterator *fx_tree_node_cbegin(const fx_tree_node *node);
FX_API fx_iterator *fx_tree_node_begin_recursive(fx_tree_node *node);
FX_API const fx_iterator *fx_tree_node_cbegin_recursive(
const fx_tree_node *node);
FX_DECLS_END;
#endif
+61 -68
View File
@@ -23,6 +23,7 @@ struct fx_list_iterator_p {
size_t i;
void *item;
fx_value item_value;
fx_list_entry *entry;
};
@@ -41,8 +42,10 @@ static void *list_first_item(const struct fx_list_p *q)
return NULL;
}
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
struct fx_list_entry *list_entry = fx_unbox(
struct fx_list_entry,
entry,
e_entry);
return list_entry->e_data;
}
@@ -55,8 +58,10 @@ static void *list_last_item(const struct fx_list_p *q)
return NULL;
}
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
struct fx_list_entry *list_entry = fx_unbox(
struct fx_list_entry,
entry,
e_entry);
return list_entry->e_data;
}
@@ -69,8 +74,10 @@ static struct fx_list_entry *list_first_entry(const struct fx_list_p *q)
return NULL;
}
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
struct fx_list_entry *list_entry = fx_unbox(
struct fx_list_entry,
entry,
e_entry);
return list_entry;
}
@@ -83,8 +90,10 @@ static struct fx_list_entry *list_last_entry(const struct fx_list_p *q)
return NULL;
}
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
struct fx_list_entry *list_entry = fx_unbox(
struct fx_list_entry,
entry,
e_entry);
return list_entry;
}
@@ -169,8 +178,10 @@ static void *list_pop_front(struct fx_list_p *q)
return NULL;
}
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
struct fx_list_entry *list_entry = fx_unbox(
struct fx_list_entry,
entry,
e_entry);
void *item = list_entry->e_data;
free(list_entry);
@@ -187,8 +198,10 @@ static void *list_pop_back(struct fx_list_p *q)
return NULL;
}
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
struct fx_list_entry *list_entry = fx_unbox(
struct fx_list_entry,
entry,
e_entry);
void *item = list_entry->e_data;
free(list_entry);
@@ -202,8 +215,10 @@ static struct fx_list_entry *find_item(struct fx_list_p *list, void *item)
{
struct fx_queue_entry *entry = fx_queue_first(&list->l_queue);
while (entry) {
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
struct fx_list_entry *list_entry = fx_unbox(
struct fx_list_entry,
entry,
e_entry);
if (list_entry->e_data == item) {
return list_entry;
@@ -246,8 +261,10 @@ static void list_delete_all(struct fx_list_p *q)
{
struct fx_queue_entry *entry = fx_queue_first(&q->l_queue);
while (entry) {
struct fx_list_entry *list_entry
= fx_unbox(struct fx_list_entry, entry, e_entry);
struct fx_list_entry *list_entry = fx_unbox(
struct fx_list_entry,
entry,
e_entry);
struct fx_queue_entry *next = fx_queue_next(entry);
free(list_entry);
@@ -385,30 +402,12 @@ void *fx_list_entry_value(const struct fx_list_entry *entry)
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);
struct fx_list_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it = fx_object_get_private(
it_obj,
FX_TYPE_LIST_ITERATOR);
it->_q = (fx_list *)q;
it->_q_p = fx_object_get_private(q, FX_TYPE_LIST);
@@ -440,8 +439,9 @@ static void list_fini(fx_object *obj, void *priv)
static enum fx_status iterator_move_next(const fx_iterator *obj)
{
struct fx_list_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_LIST_ITERATOR);
if (!it->_q_entry) {
it->entry = NULL;
@@ -467,8 +467,9 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
static enum fx_status iterator_erase(fx_iterator *obj)
{
struct fx_list_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_LIST_ITERATOR);
if (!it->entry || !it->_q_entry) {
return FX_ERR_OUT_OF_BOUNDS;
@@ -488,35 +489,28 @@ static enum fx_status iterator_erase(fx_iterator *obj)
return FX_SUCCESS;
}
static fx_iterator_value iterator_get_value(fx_iterator *obj)
static const fx_value *iterator_get_value(const fx_iterator *obj)
{
struct fx_list_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
struct fx_list_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_LIST_ITERATOR);
return FX_ITERATOR_VALUE_PTR(it->item);
}
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
{
struct fx_list_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_LIST_ITERATOR);
return FX_ITERATOR_VALUE_CPTR(it->item);
it->item_value = FX_POINTER(it->item);
return &it->item_value;
}
/*** CLASS DEFINITION *********************************************************/
// ---- fx_list DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_list)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_list)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_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_cbegin) = fx_list_cbegin;
FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_DEFINITION_END(fx_list)
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_list)
FX_TYPE_DEFINITION_BEGIN(fx_list)
FX_TYPE_ID(0x8730e66f, 0x0fd9, 0x4773, 0x9bbd, 0x6428f6e495eb);
@@ -528,18 +522,17 @@ FX_TYPE_DEFINITION_BEGIN(fx_list)
FX_TYPE_DEFINITION_END(fx_list)
// ---- fx_list_iterator DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_list_iterator)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_list_iterator)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
FX_TYPE_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_DEFINITION_END(fx_list_iterator)
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_list_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_list_iterator)
FX_TYPE_ID(0xd9658456, 0xdd80, 0x419a, 0xb23a, 0xb513013e6431);
+7 -5
View File
@@ -1,13 +1,14 @@
#include <fx/ds/array.h>
#include <fx/ds/number.h>
#include <fx/collections/array.h>
#include <fx/int.h>
#include <stdio.h>
int main(void)
{
#if 0
fx_array *array = fx_array_create();
fx_array_append(array, FX_RV_INT(32));
fx_array_append(array, FX_RV_INT(64));
fx_array_append(array, FX_RV_INT(128));
fx_array_append(array, fx_int_create(32));
fx_array_append(array, fx_int_create(64));
fx_array_append(array, fx_int_create(128));
fx_iterator *it = fx_iterator_begin(array);
fx_foreach_ptr(fx_object, obj, it)
@@ -17,5 +18,6 @@ int main(void)
fx_iterator_unref(it);
fx_array_unref(array);
#endif
return 0;
}
+49
View File
@@ -0,0 +1,49 @@
#include <fx/collections/hashtable.h>
#include <fx/int.h>
#include <fx/reflection/type.h>
#include <fx/stream.h>
#include <stdio.h>
int main(void)
{
fx_hashtable *ht = fx_hashtable_create();
fx_hashtable_put(ht, &FX_INT(32), &FX_INT(64));
fx_hashtable_put(ht, &FX_CSTR("hello"), &FX_INT(128));
const fx_value *v = fx_hashtable_get(ht, &FX_INT(32));
if (v) {
printf("32= ");
fx_value_to_string(v, fx_stdout, NULL);
printf("\n");
} else {
printf("no value\n");
}
const fx_iterator *it = fx_hashtable_begin(ht);
fx_foreach(val, it)
{
fx_hashtable_item *item = NULL;
fx_value_get_object(val, &item);
printf("item %p\n", item);
const fx_value *key, *value;
key = fx_hashtable_item_get_key(item);
value = fx_hashtable_item_get_value(item);
fx_iterator *prop_it = fx_type_get_properties(
fx_type_get_by_id(FX_TYPE_HASHTABLE_ITEM));
fx_foreach(prop_val, prop_it)
{
fx_property *prop;
fx_value_get_object(prop_val, &prop);
printf("%s = ", fx_property_get_name(prop));
fx_value value = FX_VALUE_EMPTY;
fx_property_get_value(prop, val, &value);
fx_value_to_string(&value, fx_stdout, NULL);
printf("\n");
}
fx_iterator_unref(prop_it);
}
fx_iterator_unref(it);
return 0;
}
+1 -5
View File
@@ -1,11 +1,7 @@
#include <fx/ds/number.h>
#include <fx/float.h>
#include <stdio.h>
int main(void)
{
fx_number *number = fx_number_create_float(6.8);
printf("number=%zd\n", FX_NUMBER_IVAL(number));
fx_number_unref(number);
return 0;
}
+3 -3
View File
@@ -1,10 +1,10 @@
#include <fx/ds/string.h>
#include <fx/string.h>
int main(void)
{
fx_string *string = FX_CSTR("Hello, world!");
fx_string *string = fx_string_create_from_cstr("Hello, world!");
printf("string object = ");
fx_object_to_string(string, fx_stdout);
fx_object_to_string(string, fx_stdout, NULL);
printf("\n");
fx_string_unref(string);
return 0;
+3 -3
View File
@@ -1,6 +1,6 @@
#include <fx/core/stream.h>
#include <fx/core/stringstream.h>
#include <fx/ds/string.h>
#include <fx/stream.h>
#include <fx/string.h>
#include <fx/stringstream.h>
#include <stdio.h>
int main(int argc, const char **argv)
-125
View File
@@ -1,125 +0,0 @@
#include <fx/core/bst.h>
#include <fx/core/iterator.h>
#include <fx/ds/dict.h>
#include <fx/ds/number.h>
#include <fx/ds/tree.h>
#include <stdio.h>
#define NITEMS 16
struct tree_item {
int value;
fx_tree_node node;
};
struct bst_item {
int value;
fx_bst_node node;
};
FX_BST_DEFINE_SIMPLE_GET(struct bst_item, int, node, value, get_node)
FX_BST_DEFINE_SIMPLE_INSERT(struct bst_item, node, value, put_node)
int main(void)
{
fx_dict *dict = fx_dict_create();
fx_dict_put(dict, "hello", FX_RV_INT(32));
fx_dict_put(dict, "world", FX_RV_INT(64));
fx_dict_put(dict, "more", FX_RV_INT(128));
fx_dict_put(dict, "other", FX_RV_INT(256));
fx_iterator *it = fx_iterator_begin(dict);
size_t i = 0;
fx_foreach(fx_dict_item *, item, it)
{
printf("item %zu: %s=%d\n", i++, fx_string_get_cstr(item->key),
fx_number_get_int(item->value));
}
fx_iterator_unref(it);
fx_tree *tree = fx_tree_create();
struct tree_item items2[NITEMS];
for (int i = 0; i < NITEMS; i++) {
items2[i].value = i;
items2[i].node = FX_TREE_NODE_INIT;
}
fx_tree_set_root(tree, &items2[0].node);
fx_tree_node_add_child(&items2[0].node, &items2[1].node);
fx_tree_node_add_child(&items2[0].node, &items2[2].node);
fx_tree_node_add_child(&items2[0].node, &items2[3].node);
fx_tree_node_add_child(&items2[0].node, &items2[7].node);
fx_tree_node_add_child(&items2[1].node, &items2[4].node);
fx_tree_node_add_child(&items2[1].node, &items2[5].node);
fx_tree_node_add_child(&items2[4].node, &items2[6].node);
#if 0
it = fx_iterator_begin(tree);
fx_tree_iterator it2;
fx_tree_foreach(&it2, tree)
{
struct tree_item *item = fx_unbox(struct tree_item, it2.node, node);
for (size_t i = 0; i < it2.depth; i++) {
fputs(" ", stdout);
}
printf("%u\n", item->value);
}
fx_bst bst = {0};
struct bst_item items3[NITEMS] = {0};
for (int i = 0; i < NITEMS; i++) {
items3[i].value = i;
put_node(&bst, &items3[i]);
}
printf("\n\n");
fx_bst_iterator it3;
fx_bst_foreach (&it3, &bst) {
struct bst_item *item
= fx_unbox(struct bst_item, it3.node, node);
for (size_t i = 0; i < it3.depth; i++) {
fputs(" ", stdout);
}
printf("%d\n", item->value);
}
fx_bst_iterator_begin(&bst, &it3);
while (fx_bst_iterator_is_valid(&it3)) {
struct bst_item *item
= fx_unbox(struct bst_item, it3.node, node);
if (item->value == 9) {
fx_bst_iterator_erase(&it3);
} else {
fx_bst_iterator_next(&it3);
}
}
printf("\n\n");
fx_bst_foreach (&it3, &bst) {
struct bst_item *item
= fx_unbox(struct bst_item, it3.node, node);
for (size_t i = 0; i < it3.depth; i++) {
fputs(" ", stdout);
}
printf("%d\n", item->value);
}
fx_tree_unref(tree);
#endif
fx_dict_unref(dict);
return 0;
}
-50
View File
@@ -1,50 +0,0 @@
#include <CuTest.h>
#include <fx/ds/string.h>
static void test_string_create(CuTest *tc)
{
fx_string *str = fx_string_create();
CuAssertPtrNotNull(tc, str);
CuAssertIntEquals(tc, 0, fx_string_get_size(str, FX_STRLEN_NORMAL));
CuAssertStrEquals(tc, "", fx_string_get_cstr(str));
fx_string_unref(str);
str = fx_string_create_from_c('A', 8);
CuAssertPtrNotNull(tc, str);
CuAssertIntEquals(tc, 8, fx_string_get_size(str, FX_STRLEN_NORMAL));
CuAssertStrEquals(tc, "AAAAAAAA", fx_string_get_cstr(str));
fx_string_unref(str);
str = fx_string_create_from_cstr("Hello, world!");
CuAssertPtrNotNull(tc, str);
CuAssertIntEquals(tc, 13, fx_string_get_size(str, FX_STRLEN_NORMAL));
CuAssertStrEquals(tc, "Hello, world!", fx_string_get_cstr(str));
fx_string_unref(str);
}
static void test_string_length(CuTest *tc)
{
const char *cstr = "Hello, \033[91;1mworld!";
fx_string *s = fx_string_create_from_cstr(cstr);
CuAssertIntEquals(tc, 13, fx_string_get_size(s, FX_STRLEN_IGNORE_ESC));
CuAssertIntEquals(tc, 20, fx_string_get_size(s, FX_STRLEN_NORMAL));
fx_string_unref(s);
}
CuSuite *get_all_tests(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_string_create);
SUITE_ADD_TEST(suite, test_string_length);
return suite;
}
-390
View File
@@ -1,390 +0,0 @@
#include <fx/collections/tree.h>
#include <stdlib.h>
#include <string.h>
#define ITERATOR_RECURSIVE 0x01u
#define ITERATOR_IS_RECURSIVE(it) (((it)->_f01 & ITERATOR_RECURSIVE) != 0)
#define ITERATOR_UNSET_RECURSIVE(it) ((it)->_f01 &= ~ITERATOR_RECURSIVE)
#define ITERATOR_SET_RECURSIVE(it) ((it)->_f01 |= ITERATOR_RECURSIVE)
#define NODE_PARENT(n) ((n)->__p01)
#define NODE_FIRST_CHILD(n) ((n)->__p02)
#define NODE_NEXT_SIBLING(n) ((n)->__p03)
/*** PRIVATE DATA *************************************************************/
struct fx_tree_p {
struct fx_tree_node *t_root;
};
struct fx_tree_iterator_p {
size_t i, depth;
fx_tree_node *node;
unsigned char _f01;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static void tree_set_root(struct fx_tree_p *tree, struct fx_tree_node *node)
{
tree->t_root = node;
}
static const struct fx_tree_node *next_node(
const struct fx_tree_node *node,
bool recursive,
int *depth_diff)
{
if (!node) {
return NULL;
}
if (!recursive) {
node = NODE_NEXT_SIBLING(node);
return node;
}
int d = 0;
struct fx_tree_node *next = NODE_FIRST_CHILD(node);
if (next) {
d = 1;
*depth_diff = d;
return next;
}
const struct fx_tree_node *n = node;
next = NODE_NEXT_SIBLING(n);
while (!next) {
n = NODE_PARENT(n);
if (!n) {
break;
}
d--;
next = NODE_NEXT_SIBLING(n);
}
*depth_diff = d;
return next;
}
static void remove_node(struct fx_tree_node *node)
{
struct fx_tree_node *parent = NODE_PARENT(node);
if (!parent) {
return;
}
struct fx_tree_node *n0 = NULL, *n1 = NULL;
n0 = NODE_FIRST_CHILD(parent);
while (n0) {
if (n0 == node) {
break;
}
n1 = n0;
n0 = NODE_NEXT_SIBLING(n0);
}
if (!n0) {
return;
}
if (n1) {
NODE_NEXT_SIBLING(n1) = NODE_NEXT_SIBLING(n0);
} else {
NODE_FIRST_CHILD(parent) = NODE_NEXT_SIBLING(n0);
}
NODE_PARENT(n0) = NODE_NEXT_SIBLING(n0) = NULL;
}
static void reparent_children(
struct fx_tree_node *old_parent,
struct fx_tree_node *new_parent)
{
struct fx_tree_node *last = NODE_FIRST_CHILD(new_parent);
while (last && NODE_NEXT_SIBLING(last)) {
last = NODE_NEXT_SIBLING(last);
}
struct fx_tree_node *cur = NODE_FIRST_CHILD(old_parent);
while (cur) {
struct fx_tree_node *next = NODE_NEXT_SIBLING(cur);
NODE_PARENT(cur) = new_parent;
NODE_NEXT_SIBLING(cur) = NULL;
if (last) {
NODE_NEXT_SIBLING(last) = cur;
} else {
NODE_FIRST_CHILD(new_parent) = cur;
}
last = cur;
cur = next;
}
}
/*** PUBLIC FUNCTIONS *********************************************************/
void fx_tree_set_root(fx_tree *tree, struct fx_tree_node *node)
{
FX_CLASS_DISPATCH_STATIC(FX_TYPE_TREE, tree_set_root, tree, node);
}
void fx_tree_node_add_child(
struct fx_tree_node *parent,
struct fx_tree_node *child)
{
if (NODE_PARENT(child)) {
return;
}
NODE_PARENT(child) = parent;
if (!NODE_FIRST_CHILD(parent)) {
NODE_FIRST_CHILD(parent) = child;
return;
}
struct fx_tree_node *cur = NODE_FIRST_CHILD(parent);
while (NODE_NEXT_SIBLING(cur)) {
cur = NODE_NEXT_SIBLING(cur);
}
NODE_NEXT_SIBLING(cur) = child;
}
void fx_tree_node_add_sibling(
struct fx_tree_node *node,
struct fx_tree_node *to_add)
{
if (NODE_PARENT(to_add) || !NODE_PARENT(node)) {
return;
}
fx_tree_node_add_child(NODE_PARENT(node), to_add);
}
struct fx_tree_node *fx_tree_node_get_child(
struct fx_tree_node *node,
size_t at)
{
size_t i = 0;
struct fx_tree_node *cur = NODE_FIRST_CHILD(node);
while (i < at) {
if (!cur) {
return NULL;
}
cur = NODE_NEXT_SIBLING(cur);
i++;
}
return cur;
}
fx_iterator *fx_tree_begin(fx_tree *tree)
{
struct fx_tree_p *p = fx_object_get_private(tree, FX_TYPE_TREE);
return fx_tree_node_begin(p->t_root);
}
const fx_iterator *fx_tree_cbegin(const fx_tree *tree)
{
struct fx_tree_p *p = fx_object_get_private(tree, FX_TYPE_TREE);
return fx_tree_node_begin(p->t_root);
}
fx_iterator *fx_tree_node_begin(struct fx_tree_node *node)
{
fx_tree_iterator *it_obj = fx_object_create(FX_TYPE_TREE_ITERATOR);
struct fx_tree_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_TREE_ITERATOR);
it->node = NODE_FIRST_CHILD(node);
it->i = 0;
it->depth = 0;
ITERATOR_UNSET_RECURSIVE(it);
if (!it->node) {
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
}
return it_obj;
}
const fx_iterator *fx_tree_node_cbegin(const struct fx_tree_node *node)
{
return fx_tree_node_begin((struct fx_tree_node *)node);
}
fx_iterator *fx_tree_node_begin_recursive(struct fx_tree_node *node)
{
fx_tree_iterator *it_obj = fx_object_create(FX_TYPE_TREE_ITERATOR);
struct fx_tree_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_TREE_ITERATOR);
it->node = node;
it->i = 0;
it->depth = 0;
ITERATOR_SET_RECURSIVE(it);
if (!it->node) {
fx_iterator_set_status(it_obj, FX_ERR_NO_DATA);
}
return it_obj;
}
const fx_iterator *fx_tree_node_cbegin_recursive(
const struct fx_tree_node *node)
{
return fx_tree_node_begin_recursive((struct fx_tree_node *)node);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void tree_init(fx_object *obj, void *priv)
{
struct fx_tree_p *tree = priv;
}
static void tree_fini(fx_object *obj, void *priv)
{
struct fx_tree_p *tree = priv;
}
/*** ITERATOR FUNCTIONS *******************************************************/
static enum fx_status iterator_move_next(const fx_iterator *obj)
{
struct fx_tree_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR);
int depth_diff = 0;
const struct fx_tree_node *next
= next_node(it->node, ITERATOR_IS_RECURSIVE(it), &depth_diff);
if (next) {
it->depth += depth_diff;
it->i++;
} else {
it->depth = 0;
it->i = 0;
}
it->node = (struct fx_tree_node *)next;
return (it->node != NULL) ? FX_SUCCESS : FX_ERR_NO_DATA;
}
static enum fx_status iterator_erase(fx_iterator *obj)
{
struct fx_tree_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR);
if (!it->node) {
return FX_ERR_OUT_OF_BOUNDS;
}
struct fx_tree_node *parent = NODE_PARENT(it->node);
if (!parent) {
return FX_ERR_NOT_SUPPORTED;
}
int d = 0;
struct fx_tree_node *n = it->node;
struct fx_tree_node *next = NODE_NEXT_SIBLING(n);
if (!next) {
next = NODE_FIRST_CHILD(n);
}
while (!next) {
n = NODE_PARENT(n);
if (!n) {
break;
}
d--;
next = NODE_NEXT_SIBLING(n);
}
remove_node(it->node);
reparent_children(it->node, parent);
return FX_SUCCESS;
}
static fx_iterator_value iterator_get_value(fx_iterator *obj)
{
struct fx_tree_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR);
if (!it->node) {
return FX_ITERATOR_VALUE_NULL;
}
return FX_ITERATOR_VALUE_PTR(it->node);
}
static const fx_iterator_value iterator_get_cvalue(const fx_iterator *obj)
{
struct fx_tree_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_TREE_ITERATOR);
if (!it->node) {
return FX_ITERATOR_VALUE_NULL;
}
return FX_ITERATOR_VALUE_CPTR(it->node);
}
/*** CLASS DEFINITION *********************************************************/
// ---- fx_tree DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_tree)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterable, FX_TYPE_ITERABLE)
FX_INTERFACE_ENTRY(it_begin) = fx_tree_begin;
FX_INTERFACE_ENTRY(it_cbegin) = fx_tree_cbegin;
FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_DEFINITION_END(fx_tree)
FX_TYPE_DEFINITION_BEGIN(fx_tree)
FX_TYPE_ID(0x8d8fa36b, 0xc515, 0x4803, 0x8124, 0xfd704f01b8ae);
FX_TYPE_CLASS(fx_tree_class);
FX_TYPE_IMPLEMENTS(FX_TYPE_ITERABLE);
FX_TYPE_INSTANCE_PRIVATE(struct fx_tree_p);
FX_TYPE_INSTANCE_INIT(tree_init);
FX_TYPE_INSTANCE_FINI(tree_fini);
FX_TYPE_DEFINITION_END(fx_tree)
// ---- fx_tree_iterator DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_tree_iterator)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
FX_TYPE_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_DEFINITION_END(fx_tree_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_tree_iterator)
FX_TYPE_ID(0xb896e671, 0x84b2, 0x4892, 0xaf09, 0x407f305f4bf8);
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_tree_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_tree_iterator_p);
FX_TYPE_DEFINITION_END(fx_tree_iterator)
+5 -5
View File
@@ -162,7 +162,7 @@ static bool compressor_eof(const struct compressor_data *p)
/*** PUBLIC FUNCTIONS *********************************************************/
enum fx_status fx_compressor_get_buffer_size(
fx_type type,
fx_type_id type,
fx_compressor_mode mode,
size_t *inbuf_size,
size_t *outbuf_size)
@@ -248,11 +248,11 @@ static void compressor_fini(fx_object *obj, void *priv)
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_compressor)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_compressor)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_compressor)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(fx_compressor)
FX_TYPE_DEFINITION_BEGIN(fx_compressor)
FX_TYPE_ID(0x452ee0f9, 0xfe12, 0x48a1, 0xb596, 0xad5b7a3940e7);
+7 -7
View File
@@ -782,7 +782,7 @@ static enum fx_status cstream_restore_cursor_position(
enum fx_status fx_cstream_open(
fx_stream *endpoint,
fx_type compressor_type,
fx_type_id compressor_type,
fx_compressor_mode mode,
fx_cstream **out)
{
@@ -989,12 +989,12 @@ static void cstream_fini(fx_object *obj, void *priv)
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_cstream)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_cstream)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM)
FX_INTERFACE_ENTRY(s_close) = NULL;
FX_INTERFACE_ENTRY(s_seek) = NULL;
FX_INTERFACE_ENTRY(s_tell) = NULL;
@@ -1002,8 +1002,8 @@ FX_TYPE_CLASS_DEFINITION_BEGIN(fx_cstream)
FX_INTERFACE_ENTRY(s_read) = fx_cstream_read;
FX_INTERFACE_ENTRY(s_write) = fx_cstream_write;
FX_INTERFACE_ENTRY(s_reserve) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_stream, FX_TYPE_STREAM)
FX_TYPE_CLASS_DEFINITION_END(fx_cstream)
FX_TYPE_VTABLE_INTERFACE_END(fx_stream, FX_TYPE_STREAM)
FX_TYPE_CLASS_END(fx_cstream)
FX_TYPE_DEFINITION_BEGIN(fx_cstream)
FX_TYPE_ID(0xe1e899b5, 0x6a3c, 0x4f9c, 0xafd0, 0xaab3f156615c);
+6 -6
View File
@@ -361,12 +361,12 @@ static enum fx_status set_mode(
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_zstd_compressor)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_zstd_compressor)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_compressor, FX_TYPE_COMPRESSOR)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_compressor, FX_TYPE_COMPRESSOR)
FX_INTERFACE_ENTRY(c_buffer_size)
= fx_zstd_compressor_get_buffer_size;
FX_INTERFACE_ENTRY(c_compress) = compress;
@@ -374,8 +374,8 @@ FX_TYPE_CLASS_DEFINITION_BEGIN(fx_zstd_compressor)
FX_INTERFACE_ENTRY(c_decompress) = decompress;
FX_INTERFACE_ENTRY(c_reset) = reset;
FX_INTERFACE_ENTRY(c_set_mode) = set_mode;
FX_TYPE_CLASS_INTERFACE_END(fx_compressor, FX_TYPE_COMPRESSOR)
FX_TYPE_CLASS_DEFINITION_END(fx_zstd_compressor)
FX_TYPE_VTABLE_INTERFACE_END(fx_compressor, FX_TYPE_COMPRESSOR)
FX_TYPE_CLASS_END(fx_zstd_compressor)
FX_TYPE_DEFINITION_BEGIN(fx_zstd_compressor)
FX_TYPE_ID(0x51d437fc, 0xe789, 0x4105, 0xbac7, 0xe6b3f45df198);
@@ -38,7 +38,7 @@ typedef struct fx_compressor_data {
fx_ringbuffer *c_in, *c_out;
} fx_compressor_data;
FX_API fx_type fx_compressor_get_type(void);
FX_API fx_type_id fx_compressor_get_type(void);
#if 0
FX_API fx_status fx_compressor_create(
@@ -48,7 +48,7 @@ FX_API fx_status fx_compressor_create(
#endif
FX_API fx_status fx_compressor_get_buffer_size(
fx_type type,
fx_type_id type,
fx_compressor_mode mode,
size_t *inbuf_size,
size_t *outbuf_size);
@@ -16,11 +16,11 @@ FX_DECLARE_TYPE(fx_cstream);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_cstream)
FX_TYPE_CLASS_DECLARATION_END(fx_cstream)
FX_API fx_type fx_cstream_get_type(void);
FX_API fx_type_id fx_cstream_get_type(void);
FX_API fx_status fx_cstream_open(
fx_stream *endpoint,
fx_type compressor_type,
fx_type_id compressor_type,
enum fx_compressor_mode mode,
fx_cstream **out);
+1 -1
View File
@@ -16,7 +16,7 @@ FX_DECLARE_TYPE(fx_zstd_compressor);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_zstd_compressor)
FX_TYPE_CLASS_DECLARATION_END(fx_compressor)
FX_API fx_type fx_zstd_compressor_get_type(void);
FX_API fx_type_id fx_zstd_compressor_get_type(void);
FX_API fx_status fx_zstd_compressor_get_buffer_size(
fx_compressor_mode mode,
+18 -9
View File
@@ -1,9 +1,9 @@
#include <assert.h>
#include <fx/compress/compressor.h>
#include <fx/compress/cstream.h>
#include <fx/compress/zstd.h>
#include <fx/core/ringbuffer.h>
#include <fx/core/stream.h>
#include <fx/compression/compressor.h>
#include <fx/compression/cstream.h>
#include <fx/compression/zstd.h>
#include <fx/ringbuffer.h>
#include <fx/stream.h>
#include <stdio.h>
#include <string.h>
@@ -48,9 +48,13 @@ int main(int argc, const char **argv)
size_t nr_written = 0;
fx_status status = fx_cstream_write(
cstream, source, source_len, &nr_written);
cstream,
source,
source_len,
&nr_written);
if (!FX_OK(status)) {
fprintf(stderr, "write error: %s\n",
fprintf(stderr,
"write error: %s\n",
fx_status_description(status));
break;
}
@@ -59,14 +63,19 @@ int main(int argc, const char **argv)
if (compressed) {
fx_cstream_end_compressed_section(
cstream, &nr_written_compressed, &nr_written);
cstream,
&nr_written_compressed,
&nr_written);
}
size_t tx_total = 0;
fx_cstream_tx_bytes(cstream, &tx_total);
printf("iteration %d: wrote %zu (compressed) / %zu "
"(uncompressed) / %zu (total) bytes (%s)\n",
i, nr_written_compressed, nr_written, tx_total,
i,
nr_written_compressed,
nr_written,
tx_total,
compressed ? "compressed" : "uncompressed");
compressed = !compressed;
+19 -10
View File
@@ -1,9 +1,9 @@
#include <assert.h>
#include <fx/compress/compressor.h>
#include <fx/compress/cstream.h>
#include <fx/compress/zstd.h>
#include <fx/core/ringbuffer.h>
#include <fx/core/stream.h>
#include <fx/compression/compressor.h>
#include <fx/compression/cstream.h>
#include <fx/compression/zstd.h>
#include <fx/ringbuffer.h>
#include <fx/stream.h>
#include <stdio.h>
#include <string.h>
@@ -37,10 +37,14 @@ int main(int argc, const char **argv)
memset(buf, 0x0, sizeof buf);
size_t nr_read = 0;
fx_status status
= fx_cstream_read(cstream, buf, sizeof buf - 1, &nr_read);
fx_status status = fx_cstream_read(
cstream,
buf,
sizeof buf - 1,
&nr_read);
if (!FX_OK(status)) {
fprintf(stderr, "write error: %s\n",
fprintf(stderr,
"write error: %s\n",
fx_status_description(status));
break;
}
@@ -52,14 +56,19 @@ int main(int argc, const char **argv)
size_t nr_read_compressed = 0;
if (compressed) {
fx_cstream_end_compressed_section(
cstream, &nr_read_compressed, &nr_read);
cstream,
&nr_read_compressed,
&nr_read);
}
size_t tx_total = 0;
fx_cstream_tx_bytes(cstream, &tx_total);
printf(" * iteration %d: read %zu (compressed) / %zu "
"(uncompressed) / %zu (total) bytes (%s)\n",
i, nr_read_compressed, nr_read, tx_total,
i,
nr_read_compressed,
nr_read,
tx_total,
compressed ? "compressed" : "uncompressed");
printf("%s\n", buf);
+19 -10
View File
@@ -1,7 +1,7 @@
#include <assert.h>
#include <fx/compress/compressor.h>
#include <fx/compress/zstd.h>
#include <fx/core/ringbuffer.h>
#include <fx/compression/compressor.h>
#include <fx/compression/zstd.h>
#include <fx/ringbuffer.h>
#include <stdio.h>
#include <string.h>
@@ -12,8 +12,10 @@ int refill_input_buffer(FILE *fp, fx_ringbuffer *dest)
while (1) {
void *buf;
size_t capacity;
fx_status status
= fx_ringbuffer_open_write_buffer(dest, &buf, &capacity);
fx_status status = fx_ringbuffer_open_write_buffer(
dest,
&buf,
&capacity);
if (status == FX_ERR_NO_SPACE) {
break;
}
@@ -43,8 +45,10 @@ int flush_output_buffer(FILE *fp, fx_ringbuffer *src)
while (1) {
const void *buf;
size_t capacity;
fx_status status
= fx_ringbuffer_open_read_buffer(src, &buf, &capacity);
fx_status status = fx_ringbuffer_open_read_buffer(
src,
&buf,
&capacity);
if (status == FX_ERR_NO_DATA) {
break;
}
@@ -68,7 +72,9 @@ int flush_output_buffer(FILE *fp, fx_ringbuffer *src)
int main(int argc, const char **argv)
{
if (argc < 4) {
fprintf(stderr, "usage: %s <C/D> <inpath> <outpath>\n", argv[0]);
fprintf(stderr,
"usage: %s <C/D> <inpath> <outpath>\n",
argv[0]);
return -1;
}
@@ -96,12 +102,15 @@ int main(int argc, const char **argv)
}
fx_status status = FX_SUCCESS;
fx_type compressor_type = FX_TYPE_ZSTD_COMPRESSOR;
fx_type_id compressor_type = FX_TYPE_ZSTD_COMPRESSOR;
fx_compressor *compressor = fx_object_create(compressor_type);
size_t inbuf_size, outbuf_size;
fx_compressor_get_buffer_size(
compressor_type, mode, &inbuf_size, &outbuf_size);
compressor_type,
mode,
&inbuf_size,
&outbuf_size);
fx_ringbuffer *in = fx_ringbuffer_create(inbuf_size);
fx_ringbuffer *out = fx_ringbuffer_create(outbuf_size);
+25 -12
View File
@@ -1,21 +1,24 @@
#include <assert.h>
#include <fx/compress/compressor.h>
#include <fx/compress/cstream.h>
#include <fx/compress/zstd.h>
#include <fx/core/ringbuffer.h>
#include <fx/core/stream.h>
#include <fx/compression/compressor.h>
#include <fx/compression/cstream.h>
#include <fx/compression/zstd.h>
#include <fx/ringbuffer.h>
#include <fx/stream.h>
#include <stdio.h>
#include <string.h>
#define BUF_SIZE 32
static int compress(fx_type compressor_type, FILE *in, FILE *out)
static int compress(fx_type_id compressor_type, FILE *in, FILE *out)
{
fx_stream *out_stream = fx_stream_open_fp(out);
fx_cstream *cstream;
fx_status status = fx_cstream_open(
out_stream, compressor_type, FX_COMPRESSOR_MODE_COMPRESS, &cstream);
out_stream,
compressor_type,
FX_COMPRESSOR_MODE_COMPRESS,
&cstream);
if (!FX_OK(status)) {
fprintf(stderr, "cannot initialise compressor\n");
@@ -48,13 +51,16 @@ static int compress(fx_type compressor_type, FILE *in, FILE *out)
return 0;
}
static int decompress(fx_type compressor_type, FILE *in, FILE *out)
static int decompress(fx_type_id compressor_type, FILE *in, FILE *out)
{
fx_stream *in_stream = fx_stream_open_fp(in);
fx_cstream *cstream;
fx_status status = fx_cstream_open(
in_stream, compressor_type, FX_COMPRESSOR_MODE_DECOMPRESS, &cstream);
in_stream,
compressor_type,
FX_COMPRESSOR_MODE_DECOMPRESS,
&cstream);
if (!FX_OK(status)) {
fprintf(stderr, "cannot initialise compressor\n");
@@ -66,9 +72,14 @@ static int decompress(fx_type compressor_type, FILE *in, FILE *out)
char buf[4096];
while (1) {
size_t r = 0;
fx_status status = fx_cstream_read(cstream, buf, sizeof buf, &r);
fx_status status = fx_cstream_read(
cstream,
buf,
sizeof buf,
&r);
if (!FX_OK(status)) {
fprintf(stderr, "read error: %s\n",
fprintf(stderr,
"read error: %s\n",
fx_status_description(status));
return -1;
}
@@ -94,7 +105,9 @@ static int decompress(fx_type compressor_type, FILE *in, FILE *out)
int main(int argc, const char **argv)
{
if (argc < 4) {
fprintf(stderr, "usage: %s <C/D> <inpath> <outpath>\n", argv[0]);
fprintf(stderr,
"usage: %s <C/D> <inpath> <outpath>\n",
argv[0]);
return -1;
}
+2 -2
View File
@@ -45,8 +45,8 @@ typedef struct fx_directory_entry {
fx_file_info info;
} fx_directory_entry;
FX_API fx_type fx_directory_get_type(void);
FX_API fx_type fx_directory_iterator_get_type(void);
FX_API fx_type_id fx_directory_get_type(void);
FX_API fx_type_id fx_directory_iterator_get_type(void);
FX_API fx_result fx_directory_open(
fx_directory *root,
+1 -1
View File
@@ -51,7 +51,7 @@ typedef struct fx_file_info {
size_t length;
} fx_file_info;
FX_API fx_type fx_file_get_type(void);
FX_API fx_type_id fx_file_get_type(void);
FX_API fx_result fx_file_open(
FX_TYPE_FWDREF(fx_directory) * root,
+1 -1
View File
@@ -20,7 +20,7 @@ FX_TYPE_CLASS_DECLARATION_END(fx_path)
struct fx_file_info;
FX_API fx_type fx_path_get_type(void);
FX_API fx_type_id fx_path_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_path, FX_TYPE_PATH);
@@ -35,6 +35,7 @@ struct fx_directory_iterator_p {
fx_directory *root;
fx_directory_entry entry;
fx_value entry_value;
};
/*** PRIVATE FUNCTIONS ********************************************************/
@@ -64,8 +65,9 @@ static fx_result directory_delete(
{
enum fx_status status = FX_SUCCESS;
fx_iterator *it
= fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_LAST);
fx_iterator *it = fx_directory_begin(
dir,
FX_DIRECTORY_ITERATE_PARENT_LAST);
while (FX_OK(fx_iterator_get_status(it))) {
fx_iterator_erase(it);
}
@@ -291,8 +293,9 @@ static fx_result directory_open(
fx_directory *dir = fx_object_create(FX_TYPE_DIRECTORY);
fx_path *cwd = NULL;
struct fx_directory_p *p
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
struct fx_directory_p *p = fx_object_get_private(
dir,
FX_TYPE_DIRECTORY);
if (!root) {
cwd = fx_path_create_cwd();
@@ -358,8 +361,9 @@ fx_result fx_directory_open_temp(fx_directory **out)
rpath,
FX_DIRECTORY_OPEN_CREATE,
&dir);
struct fx_directory_p *p
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
struct fx_directory_p *p = fx_object_get_private(
dir,
FX_TYPE_DIRECTORY);
if (fx_error_get_status_code(status) == FX_ERR_NAME_EXISTS) {
fx_path_unref(rpath);
@@ -372,8 +376,6 @@ fx_result fx_directory_open_temp(fx_directory **out)
fx_path_unlink(rpath);
fx_path_unref(rpath);
return status;
}
}
@@ -408,8 +410,9 @@ const char *fx_directory_get_rel_path_cstr(const fx_directory *dir)
fx_result fx_directory_delete(fx_directory *dir)
{
struct fx_directory_p *p
= fx_object_get_private(dir, FX_TYPE_DIRECTORY);
struct fx_directory_p *p = fx_object_get_private(
dir,
FX_TYPE_DIRECTORY);
p->d_flags |= DIRECTORY_DELETE_ON_CLOSE;
/* TODO allow object release functions to return a fx_result value */
fx_directory_unref(dir);
@@ -508,6 +511,8 @@ static void update_iterator_data(struct fx_directory_iterator_p *it)
fx_path *path = fx_path_create_from_cstr(
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.filepath = path;
@@ -555,8 +560,9 @@ fx_iterator *fx_directory_begin(
enum fx_directory_iterator_flags flags)
{
fx_iterator *it_obj = fx_object_create(FX_TYPE_DIRECTORY_ITERATOR);
struct fx_directory_iterator_p *it
= fx_object_get_private(it_obj, FX_TYPE_DIRECTORY_ITERATOR);
struct fx_directory_iterator_p *it = fx_object_get_private(
it_obj,
FX_TYPE_DIRECTORY_ITERATOR);
it->flags = flags;
it->root = directory;
@@ -614,12 +620,7 @@ fx_iterator *fx_directory_begin(
return it_obj;
}
static fx_iterator *iterator_begin(fx_object *obj)
{
return fx_directory_begin(obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
}
static const fx_iterator *iterator_cbegin(const fx_object *obj)
static const fx_iterator *iterator_begin(const fx_object *obj)
{
return fx_directory_begin(
(fx_object *)obj,
@@ -628,8 +629,9 @@ static const fx_iterator *iterator_cbegin(const fx_object *obj)
static enum fx_status iterator_move_next(const fx_iterator *obj)
{
struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
struct fx_directory_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_DIRECTORY_ITERATOR);
if (!it || !it->fts) {
return FX_ERR_NO_DATA;
}
@@ -677,10 +679,12 @@ static enum fx_status iterator_move_next(const fx_iterator *obj)
static enum fx_status iterator_erase(fx_iterator *obj)
{
struct fx_directory_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
fx_result result
= fx_directory_path_unlink(it->root, it->entry.filepath);
struct fx_directory_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_DIRECTORY_ITERATOR);
fx_result result = fx_directory_path_unlink(
it->root,
it->entry.filepath);
if (fx_result_is_error(result)) {
enum fx_status status = fx_error_get_status_code(result);
fx_error_discard(result);
@@ -690,35 +694,27 @@ static enum fx_status iterator_erase(fx_iterator *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
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
struct fx_directory_iterator_p *it = fx_object_get_private(
obj,
FX_TYPE_DIRECTORY_ITERATOR);
return FX_ITERATOR_VALUE_PTR(&it->entry);
}
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);
return &it->entry_value;
}
/*** CLASS DEFINITION *********************************************************/
// ---- fx_directory DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_directory)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_directory)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_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_cbegin) = iterator_cbegin;
FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_DEFINITION_END(fx_directory)
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_directory)
FX_TYPE_DEFINITION_BEGIN(fx_directory)
FX_TYPE_ID(0x10d36546, 0x7f96, 0x464b, 0xbc4d, 0xe504b283fa45);
@@ -730,18 +726,17 @@ FX_TYPE_DEFINITION_BEGIN(fx_directory)
FX_TYPE_DEFINITION_END(fx_directory)
// ---- fx_directory_iterator DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_directory_iterator)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_directory_iterator)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
FX_TYPE_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_DEFINITION_END(fx_directory_iterator)
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_directory_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_directory_iterator)
FX_TYPE_ID(0xc707fce6, 0xc895, 0x4925, 0x8700, 0xa60641dee0cc);
@@ -618,20 +618,20 @@ static void file_fini(fx_object *obj, void *priv)
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_file)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_file)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM)
FX_INTERFACE_ENTRY(s_close) = stream_close;
FX_INTERFACE_ENTRY(s_getc) = stream_getc;
FX_INTERFACE_ENTRY(s_read) = stream_read;
FX_INTERFACE_ENTRY(s_write) = stream_write;
FX_INTERFACE_ENTRY(s_seek) = stream_seek;
FX_INTERFACE_ENTRY(s_tell) = stream_tell;
FX_TYPE_CLASS_INTERFACE_END(fx_stream, FX_TYPE_STREAM)
FX_TYPE_CLASS_DEFINITION_END(fx_file)
FX_TYPE_VTABLE_INTERFACE_END(fx_stream, FX_TYPE_STREAM)
FX_TYPE_CLASS_END(fx_file)
FX_TYPE_DEFINITION_BEGIN(fx_file)
FX_TYPE_ID(0x495a73f6, 0xb8c3, 0x4e17, 0xb5f4, 0x6fc321f67c7b);
@@ -6,6 +6,7 @@
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <fx/string.h>
#include <fx/value.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
@@ -134,8 +135,8 @@ static enum fx_status path_get_directory(
size_t dir_path_len = (size_t)(sep - path_cstr);
fx_string *dir_path_s = fx_string_get_substr(path_str, 0, dir_path_len);
fx_path *dir_path
= fx_path_create_from_cstr(fx_string_get_cstr(dir_path_s));
fx_path *dir_path = fx_path_create_from_cstr(
fx_string_get_cstr(dir_path_s));
fx_string_unref(dir_path_s);
*out_dir_path = dir_path;
@@ -284,13 +285,15 @@ fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
return NULL;
}
struct fx_path_p *result_p
= fx_object_get_private(result, FX_TYPE_PATH);
struct fx_path_p *result_p = fx_object_get_private(
result,
FX_TYPE_PATH);
for (size_t i = 0; i < nr_paths; i++) {
if (paths[i]) {
struct fx_path_p *path_p
= fx_object_get_private(paths[i], FX_TYPE_PATH);
struct fx_path_p *path_p = fx_object_get_private(
paths[i],
FX_TYPE_PATH);
append_path(result_p, path_p);
}
}
@@ -385,27 +388,33 @@ static void path_init(fx_object *obj, void *priv)
}
}
void path_fini(fx_object *obj, void *priv)
static void path_fini(fx_object *obj, void *priv)
{
struct fx_path_p *path = priv;
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_SUCCESS;
}
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_path)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_path)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = path_to_string;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_path)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(fx_path)
FX_TYPE_DEFINITION_BEGIN(fx_path)
FX_TYPE_ID(0x56dc32eb, 0xea96, 0x46ed, 0x85d3, 0x760fa4ad61f4);
+18 -30
View File
@@ -37,6 +37,7 @@ struct fx_directory_iterator_p {
fx_directory *root;
fx_directory_entry entry;
fx_value entry_value;
};
/*** 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(
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.filepath = path;
@@ -616,12 +619,7 @@ fx_iterator *fx_directory_begin(
return it_obj;
}
static fx_iterator *iterator_begin(fx_object *obj)
{
return fx_directory_begin(obj, FX_DIRECTORY_ITERATE_PARENT_FIRST);
}
static const fx_iterator *iterator_cbegin(const fx_object *obj)
static const fx_iterator *iterator_begin(const fx_object *obj)
{
return fx_directory_begin(
(fx_object *)obj,
@@ -692,35 +690,26 @@ static enum fx_status iterator_erase(fx_iterator *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
= fx_object_get_private(obj, FX_TYPE_DIRECTORY_ITERATOR);
return FX_ITERATOR_VALUE_PTR(&it->entry);
}
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);
return &it->entry_value;
}
/*** CLASS DEFINITION *********************************************************/
// ---- fx_directory DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_directory)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_directory)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_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_cbegin) = iterator_cbegin;
FX_TYPE_CLASS_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_DEFINITION_END(fx_directory)
FX_TYPE_VTABLE_INTERFACE_END(fx_iterable, FX_TYPE_ITERABLE)
FX_TYPE_CLASS_END(fx_directory)
FX_TYPE_DEFINITION_BEGIN(fx_directory)
FX_TYPE_ID(0x10d36546, 0x7f96, 0x464b, 0xbc4d, 0xe504b283fa45);
@@ -732,18 +721,17 @@ FX_TYPE_DEFINITION_BEGIN(fx_directory)
FX_TYPE_DEFINITION_END(fx_directory)
// ---- fx_directory_iterator DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_directory_iterator)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_directory_iterator)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = iterator_erase;
FX_INTERFACE_ENTRY(it_get_value) = iterator_get_value;
FX_INTERFACE_ENTRY(it_get_cvalue) = iterator_get_cvalue;
FX_TYPE_CLASS_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_DEFINITION_END(fx_directory_iterator)
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_directory_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_directory_iterator)
FX_TYPE_ID(0xc707fce6, 0xc895, 0x4925, 0x8700, 0xa60641dee0cc);
+6 -6
View File
@@ -620,20 +620,20 @@ static void file_fini(fx_object *obj, void *priv)
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_file)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_file)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM)
FX_INTERFACE_ENTRY(s_close) = stream_close;
FX_INTERFACE_ENTRY(s_getc) = stream_getc;
FX_INTERFACE_ENTRY(s_read) = stream_read;
FX_INTERFACE_ENTRY(s_write) = stream_write;
FX_INTERFACE_ENTRY(s_seek) = stream_seek;
FX_INTERFACE_ENTRY(s_tell) = stream_tell;
FX_TYPE_CLASS_INTERFACE_END(fx_stream, FX_TYPE_STREAM)
FX_TYPE_CLASS_DEFINITION_END(fx_file)
FX_TYPE_VTABLE_INTERFACE_END(fx_stream, FX_TYPE_STREAM)
FX_TYPE_CLASS_END(fx_file)
FX_TYPE_DEFINITION_BEGIN(fx_file)
FX_TYPE_ID(0x495a73f6, 0xb8c3, 0x4e17, 0xb5f4, 0x6fc321f67c7b);
+14 -7
View File
@@ -392,20 +392,27 @@ void path_fini(fx_object *obj, void *priv)
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 *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_path)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_path)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = path_to_string;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_path)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(fx_path)
FX_TYPE_DEFINITION_BEGIN(fx_path)
FX_TYPE_ID(0x56dc32eb, 0xea96, 0x46ed, 0x85d3, 0x760fa4ad61f4);
+5 -2
View File
@@ -1,6 +1,6 @@
#include <fx/core/stream.h>
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <fx/stream.h>
#include <stdio.h>
int main(int argc, const char **argv)
@@ -8,7 +8,10 @@ int main(int argc, const char **argv)
fx_file *dest;
fx_path *path = fx_path_create_from_cstr("data.txt");
fx_result result = fx_file_open(
NULL, path, FX_FILE_WRITE_ONLY | FX_FILE_CREATE, &dest);
NULL,
path,
FX_FILE_WRITE_ONLY | FX_FILE_CREATE,
&dest);
if (fx_result_is_error(result)) {
fx_throw(result);
return -1;
+6 -2
View File
@@ -20,9 +20,13 @@ int main(int argc, const char **argv)
return -1;
}
fx_iterator *it = fx_directory_begin(dir, FX_DIRECTORY_ITERATE_PARENT_FIRST);
fx_foreach(fx_directory_entry *, entry, it)
fx_iterator *it = fx_directory_begin(
dir,
FX_DIRECTORY_ITERATE_PARENT_FIRST);
fx_foreach(val, it)
{
fx_directory_entry *entry = NULL;
fx_value_get_pointer(val, (void **)&entry);
printf("%s\n", fx_path_ptr(entry->filepath));
}
-35
View File
@@ -1,35 +0,0 @@
#include <CuTest.h>
#include <fx/core/stringstream.h>
#include <fx/io/path.h>
#include <stdio.h>
void test_path_1(CuTest *tc)
{
fx_path *path = fx_path_create_from_cstr("C:\\hello\\world\\");
char buf[512];
fx_stringstream *str = fx_stringstream_create_with_buffer(buf, sizeof buf);
fx_object_to_string(path, str);
printf("%s\n", buf);
fx_path *path2 = fx_path_create_from_cstr("path1\\path2\\");
fx_path *path3 = fx_path_create_from_cstr("path3\\path4\\");
const fx_path *paths[] = {path, path2, path3};
fx_path *path4 = fx_path_join(paths, sizeof paths / sizeof paths[0]);
fx_stringstream_reset_with_buffer(str, buf, sizeof buf);
fx_object_to_string(path4, str);
printf("%s\n", buf);
}
CuSuite *get_all_tests(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_path_1);
return suite;
}
+370 -32
View File
@@ -1,8 +1,24 @@
#include "fx/namemap.h"
#include <fx/bst.h>
#include <fx/hash.h>
#include <fx/iterator.h>
#include <fx/macros.h>
#include <fx/queue.h>
#include <fx/reflection/assembly.h>
#include <fx/reflection/type.h>
FX_API fx_type_id fx_assembly_iterator_get_type();
FX_API fx_type_id fx_assembly_type_iterator_get_type();
FX_DECLARE_TYPE(fx_assembly_iterator);
FX_DECLARE_TYPE(fx_assembly_type_iterator);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_assembly_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_assembly_iterator)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_assembly_type_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_assembly_type_iterator)
enum map_entry_type {
MAP_ENTRY_NONE = 0,
@@ -12,6 +28,7 @@ enum map_entry_type {
struct map_entry {
enum map_entry_type e_type;
struct map_bucket *e_bucket;
uint64_t e_hash;
union {
fx_queue_entry e_entry;
@@ -35,12 +52,14 @@ struct map {
struct type {
struct map_item e_map_item;
fx_type e_type;
fx_type_id e_type;
};
struct fx_assembly_p {
fx_assembly *a_self;
const char *a_name;
struct map a_types;
fx_namemap_entry a_entry;
struct {
long v_major;
@@ -50,6 +69,16 @@ struct fx_assembly_p {
} a_version;
};
struct fx_assembly_iterator_p {
fx_namemap_entry *it_cur;
fx_value it_value;
};
struct fx_assembly_type_iterator_p {
struct type *it_item;
fx_value it_value;
};
/*** PRIVATE FUNCTIONS ********************************************************/
FX_BST_DEFINE_SIMPLE_GET(
@@ -60,6 +89,8 @@ FX_BST_DEFINE_SIMPLE_GET(
map_get_entry);
FX_BST_DEFINE_SIMPLE_INSERT(struct map_entry, e_node, e_hash, map_put_entry);
static fx_namemap assembly_map = FX_NAMEMAP_INIT;
static struct map_bucket *map_item_convert_to_bucket(
struct map *map,
struct map_item *item)
@@ -78,6 +109,28 @@ static struct map_bucket *map_item_convert_to_bucket(
return bucket;
}
static struct map_item *map_first_item(const struct map *map)
{
fx_bst_node *first = fx_bst_first(&map->m_entries);
if (!first) {
return NULL;
}
struct map_entry *entry = fx_unbox(struct map_entry, first, e_node);
if (entry->e_type == MAP_ENTRY_ITEM) {
return (struct map_item *)entry;
}
struct map_bucket *bucket = (struct map_bucket *)entry;
fx_queue_entry *first_entry = fx_queue_first(&bucket->b_items);
if (!first_entry) {
return NULL;
}
entry = fx_unbox(struct map_entry, first_entry, e_entry);
return (struct map_item *)entry;
}
static void map_put(struct map *map, const char *name, struct map_item *item)
{
uint64_t hash = fx_hash_cstr(name);
@@ -109,34 +162,81 @@ static void map_put(struct map *map, const char *name, struct map_item *item)
}
}
static void assembly_set_name(struct fx_assembly_p *asm, const char *name)
static struct map_item *map_item_next(struct map_item *item)
{
asm->a_name = name;
struct map_bucket *bucket = item->i_entry.e_bucket;
struct map_entry *next_item = NULL;
if (bucket) {
struct fx_queue_entry *q_item
= fx_queue_next(&item->i_entry.e_entry);
if (!q_item) {
struct fx_bst_node *node
= fx_bst_next(&bucket->b_entry.e_node);
next_item = fx_unbox(struct map_entry, node, e_node);
} else {
next_item = fx_unbox(struct map_entry, q_item, e_entry);
}
} else {
struct fx_bst_node *node = fx_bst_next(&item->i_entry.e_node);
next_item = fx_unbox(struct map_entry, node, e_node);
}
if (!next_item) {
return NULL;
}
if (next_item->e_type == MAP_ENTRY_BUCKET) {
bucket = (struct map_bucket *)next_item;
struct fx_queue_entry *q_item
= fx_queue_first(&bucket->b_items);
next_item = fx_unbox(struct map_entry, q_item, e_entry);
}
return (struct map_item *)next_item;
}
static fx_status assembly_register(struct fx_assembly_p *assembly)
{
if (fx_namemap_get(&assembly_map, assembly->a_name)) {
return FX_ERR_NAME_EXISTS;
}
fx_namemap_put(&assembly_map, assembly->a_name, &assembly->a_entry);
return FX_SUCCESS;
}
static void assembly_set_name(struct fx_assembly_p *assembly, const char *name)
{
assembly->a_name = name;
}
static void assembly_set_version(
struct fx_assembly_p *asm,
struct fx_assembly_p *assembly,
long major,
long minor,
long build,
long revision)
{
asm->a_version.v_major = major;
asm->a_version.v_minor = minor;
asm->a_version.v_build = build;
asm->a_version.v_revision = revision;
assembly->a_version.v_major = major;
assembly->a_version.v_minor = minor;
assembly->a_version.v_build = build;
assembly->a_version.v_revision = revision;
}
#include <stdio.h>
static void assembly_add_type(
struct fx_assembly_p *asm,
struct fx_assembly_p *assembly,
const char *full_name,
fx_type type_id)
fx_type_id type_id)
{
struct type *type = malloc(sizeof *type);
memset(type, 0x0, sizeof *type);
map_put(&asm->a_types, full_name, &type->e_map_item);
type->e_type = type_id;
fx_type *ty = (fx_type *)fx_type_get_by_id(type_id);
fx_type_set_assembly(ty, assembly->a_self);
map_put(&assembly->a_types, full_name, &type->e_map_item);
}
static void map_item_dump(struct map_item *item)
@@ -145,17 +245,17 @@ static void map_item_dump(struct map_item *item)
printf(" Type: %s\n", type->e_map_item.i_name);
}
static void assembly_dump(struct fx_assembly_p *asm)
static void assembly_dump(struct fx_assembly_p *assembly)
{
printf("Loaded assembly:\n");
printf(" %s, Version=%ld.%ld.%ld.%ld\n",
asm->a_name,
asm->a_version.v_major,
asm->a_version.v_minor,
asm->a_version.v_build,
asm->a_version.v_revision);
assembly->a_name,
assembly->a_version.v_major,
assembly->a_version.v_minor,
assembly->a_version.v_build,
assembly->a_version.v_revision);
fx_bst_node *cur_node = fx_bst_first(&asm->a_types.m_entries);
fx_bst_node *cur_node = fx_bst_first(&assembly->a_types.m_entries);
while (cur_node) {
struct map_entry *entry
= fx_unbox(struct map_entry, cur_node, e_node);
@@ -187,19 +287,116 @@ static void assembly_dump(struct fx_assembly_p *asm)
}
}
static fx_iterator *assembly_get_types(const struct fx_assembly_p *assembly)
{
fx_assembly_type_iterator *it
= fx_object_create(fx_assembly_type_iterator_get_type());
if (!it) {
return NULL;
}
struct fx_assembly_type_iterator_p *it_p = fx_object_get_private(
it,
fx_assembly_type_iterator_get_type());
struct map_item *item = map_first_item(&assembly->a_types);
if (!item) {
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;
}
static const char *assembly_get_name(const struct fx_assembly_p *assembly)
{
return assembly->a_name;
}
static void assembly_get_version(
const struct fx_assembly_p *assembly,
long *out_major,
long *out_minor,
long *out_build,
long *out_revision)
{
if (out_major) {
*out_major = assembly->a_version.v_major;
}
if (out_minor) {
*out_minor = assembly->a_version.v_minor;
}
if (out_build) {
*out_build = assembly->a_version.v_build;
}
if (out_revision) {
*out_revision = assembly->a_version.v_revision;
}
}
/*** PUBLIC FUNCTIONS *********************************************************/
void fx_assembly_set_name(fx_assembly *asm, const char *name)
fx_iterator *fx_assembly_get_all(void)
{
fx_assembly_iterator *it
= fx_object_create(fx_assembly_iterator_get_type());
if (!it) {
return NULL;
}
struct fx_assembly_iterator_p *it_p
= fx_object_get_private(it, fx_assembly_iterator_get_type());
it_p->it_cur = fx_namemap_first(&assembly_map);
if (!it_p->it_cur) {
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;
}
const fx_assembly *fx_assembly_get_by_name(const char *name)
{
const fx_namemap_entry *entry = fx_namemap_get(&assembly_map, name);
if (!entry) {
return NULL;
}
struct fx_assembly_p *assembly
= fx_unbox(struct fx_assembly_p, entry, a_entry);
return assembly->a_self;
}
fx_status fx_assembly_register(fx_assembly *assembly)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_REFLECTION_TYPE_ASSEMBLY,
assembly_register,
assembly);
}
void fx_assembly_set_name(fx_assembly *assembly, const char *name)
{
FX_CLASS_DISPATCH_STATIC_V(
FX_REFLECTION_TYPE_ASSEMBLY,
assembly_set_name,
asm,
assembly,
name);
}
void fx_assembly_set_version(
fx_assembly *asm,
fx_assembly *assembly,
long major,
long minor,
long build,
@@ -208,7 +405,7 @@ void fx_assembly_set_version(
FX_CLASS_DISPATCH_STATIC_V(
FX_REFLECTION_TYPE_ASSEMBLY,
assembly_set_version,
asm,
assembly,
major,
minor,
build,
@@ -216,49 +413,190 @@ void fx_assembly_set_version(
}
void fx_assembly_add_type(
fx_assembly *asm,
fx_assembly *assembly,
const char *full_name,
fx_type type_id)
fx_type_id type_id)
{
FX_CLASS_DISPATCH_STATIC_V(
FX_REFLECTION_TYPE_ASSEMBLY,
assembly_add_type,
asm,
assembly,
full_name,
type_id);
}
void fx_assembly_dump(const fx_assembly *asm)
fx_iterator *fx_assembly_get_types(const fx_assembly *assembly)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_REFLECTION_TYPE_ASSEMBLY,
assembly_get_types,
assembly);
}
const char *fx_assembly_get_name(const fx_assembly *assembly)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_REFLECTION_TYPE_ASSEMBLY,
assembly_get_name,
assembly);
}
void fx_assembly_get_version(
const fx_assembly *assembly,
long *out_major,
long *out_minor,
long *out_build,
long *out_revision)
{
FX_CLASS_DISPATCH_STATIC_V(
FX_REFLECTION_TYPE_ASSEMBLY,
assembly_get_version,
assembly,
out_major,
out_minor,
out_build,
out_revision);
}
void fx_assembly_dump(const fx_assembly *assembly)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_REFLECTION_TYPE_ASSEMBLY,
assembly_dump,
asm);
assembly);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void assembly_init(fx_object *obj, void *priv)
{
struct fx_assembly_p *assembly = priv;
assembly->a_self = obj;
}
static void assembly_fini(fx_object *obj, void *priv)
{
}
/*** ITERATOR DEFINITION ******************************************************/
static enum fx_status type_iterator_move_next(const fx_iterator *obj)
{
struct fx_assembly_type_iterator_p *it = fx_object_get_private(
obj,
fx_assembly_type_iterator_get_type());
if (!it->it_item) {
return FX_ERR_NO_DATA;
}
fx_value_unset(&it->it_value);
struct map_item *next = map_item_next(&it->it_item->e_map_item);
it->it_item = fx_unbox(struct type, next, e_map_item);
if (!it->it_item) {
return FX_ERR_NO_DATA;
}
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(
obj,
fx_assembly_type_iterator_get_type());
if (!it->it_item) {
return NULL;
}
return &it->it_value;
}
static enum fx_status assembly_iterator_move_next(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 FX_ERR_NO_DATA;
}
fx_value_unset(&it->it_value);
it->it_cur = fx_namemap_next(&assembly_map, it->it_cur);
if (!it->it_cur) {
return FX_ERR_NO_DATA;
}
struct fx_assembly_p *assembly
= fx_unbox(struct fx_assembly_p, it->it_cur, a_entry);
it->it_value = FX_VALUE_OBJECT_REF(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 *********************************************************/
// ---- fx_string DEFINITION
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_assembly)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_assembly)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_assembly)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(fx_assembly)
FX_TYPE_DEFINITION_BEGIN(fx_assembly)
FX_TYPE_ID(0xf6690c30, 0x6642, 0x42f0, 0xb79f, 0xe2baf3684b1b);
FX_TYPE_NAME("fx.reflection.assembly");
FX_TYPE_CLASS(fx_assembly_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_assembly_p);
FX_TYPE_INSTANCE_INIT(assembly_init);
FX_TYPE_INSTANCE_FINI(assembly_fini);
FX_TYPE_DEFINITION_END(fx_assembly)
FX_TYPE_CLASS_BEGIN(fx_assembly_type_iterator)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = type_iterator_move_next;
FX_INTERFACE_ENTRY(it_get_value) = type_iterator_get_value;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_assembly_type_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_assembly_type_iterator)
FX_TYPE_ID(0x74ecb8df, 0x155d, 0x4e45, 0x96a6, 0x0f71e3ea0a1e);
FX_TYPE_NAME("fx.reflection.assembly.type_iterator");
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_assembly_type_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_assembly_type_iterator_p);
FX_TYPE_DEFINITION_END(fx_assembly_type_iterator)
FX_TYPE_CLASS_BEGIN(fx_assembly_iterator)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = assembly_iterator_move_next;
FX_INTERFACE_ENTRY(it_get_value) = assembly_iterator_get_value;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_assembly_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_assembly_iterator)
FX_TYPE_ID(0x391f8d65, 0x9baf, 0x4941, 0xbdbe, 0xe739226f8947);
FX_TYPE_NAME("fx.reflection.assembly.iterator");
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_assembly_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_assembly_iterator_p);
FX_TYPE_DEFINITION_END(fx_assembly_iterator)
-86
View File
@@ -1,86 +0,0 @@
#include <fx/macros.h>
#include <fx/reflection/function.h>
#include <fx/string.h>
#include <fx/value.h>
#include <platform/callvm.h>
struct fx_function_context_p {
struct callvm ctx_vm;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static fx_status function_context_reset(
const struct fx_function_context_p *func)
{
return FX_SUCCESS;
}
static fx_status function_context_push_arg(
const struct fx_function_context_p *func,
const fx_value *arg)
{
return FX_SUCCESS;
}
/*** PUBLIC FUNCTIONS *********************************************************/
FX_API fx_function *fx_function_context_create(void)
{
fx_function_context *ctx
= fx_object_create(FX_REFLECTION_TYPE_FUNCTION_CONTEXT);
if (!ctx) {
return NULL;
}
struct fx_function_context_p *p = fx_object_get_private(
ctx,
FX_REFLECTION_TYPE_FUNCTION_CONTEXT);
return ctx;
}
FX_API void fx_function_context_reset(fx_function_context *ctx)
{
FX_CLASS_DISPATCH_STATIC_V0(
FX_REFLECTION_TYPE_FUNCTION_CONTEXT,
function_context_reset,
ctx);
}
FX_API void fx_function_context_push_arg(
fx_function_context *ctx,
const fx_value *value)
{
FX_CLASS_DISPATCH_STATIC_V(
FX_REFLECTION_TYPE_FUNCTION_CONTEXT,
function_context_push_arg,
ctx,
value);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void function_context_init(fx_object *obj, void *priv)
{
}
static void function_context_fini(fx_object *obj, void *priv)
{
}
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_function_context)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_function_context)
FX_TYPE_DEFINITION_BEGIN(fx_function_context)
FX_TYPE_ID(0x97d98a1a, 0x6312, 0x409e, 0xb47d, 0xc5d80daf9e50);
FX_TYPE_CLASS(fx_function_context_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_function_context_p);
FX_TYPE_INSTANCE_INIT(function_context_init);
FX_TYPE_INSTANCE_FINI(function_context_fini);
FX_TYPE_DEFINITION_END(fx_function_context)
+132 -26
View File
@@ -1,6 +1,7 @@
#include <fx/macros.h>
#include <fx/reflection/function.h>
#include <fx/string.h>
#include <fx/value-type.h>
#include <fx/value.h>
#include <platform/callvm.h>
@@ -8,21 +9,81 @@ struct fx_function_p {
char *func_name;
fx_function_flags func_flags;
fx_function_impl func_impl;
fx_value_type func_return_type;
fx_value_type *func_arg_types;
fx_type_id func_return_type;
fx_type_id *func_arg_types;
/* number of explicit arguments that the function takes.
* if the FX_FUNCTION_F_VARARG flag is set, the function supports a
* variable number of arguments, which may be handled differently by the
* ABI */
size_t func_nr_args;
fx_value *func_bound_args;
size_t func_nr_bound_args, func_max_bound_args;
};
/*** PRIVATE FUNCTIONS ********************************************************/
extern intptr_t fx_function_invoke_i(
fx_function_impl impl,
const fx_value *args,
size_t nr_args);
static const char *function_get_name(const struct fx_function_p *func)
{
return func->func_name;
}
static fx_status function_bind(
struct fx_function_p *func,
fx_value *args,
size_t nr_args)
{
if (func->func_nr_bound_args) {
fx_value_unset_array(
func->func_bound_args,
func->func_nr_bound_args);
}
if (!args || !nr_args) {
func->func_nr_bound_args = 0;
return FX_SUCCESS;
}
if (nr_args > func->func_max_bound_args) {
fx_value *buf = realloc(
func->func_bound_args,
nr_args * sizeof *buf);
if (!buf) {
return FX_ERR_NO_MEMORY;
}
func->func_bound_args = buf;
func->func_max_bound_args = nr_args;
}
fx_value_copy_array(func->func_bound_args, args, nr_args);
func->func_nr_bound_args = nr_args;
return FX_SUCCESS;
}
static void push_arg(
const fx_value *arg,
fx_type_id param_type,
struct callvm *vm)
{
if (!param_type) {
callvm_push(vm, arg);
return;
}
unsigned int param_value_type = __fx_type_get_value_type(param_type);
if (param_value_type == __FX_VALUE_TYPE_CSTR
&& fx_value_is_type(arg, FX_TYPE_STRING)) {
fx_object *obj;
fx_value_get_object(arg, &obj);
fx_value cstr = FX_CSTR(fx_string_get_cstr(obj));
callvm_push(vm, &cstr);
return;
}
callvm_push(vm, arg);
}
static fx_status function_invoke(
const struct fx_function_p *func,
@@ -30,28 +91,50 @@ static fx_status function_invoke(
size_t nr_args,
fx_value *return_value)
{
if (nr_args < func->func_nr_args) {
size_t total_args = nr_args + func->func_nr_bound_args;
if (total_args < func->func_nr_args) {
return FX_ERR_INVALID_ARGUMENT;
}
if (nr_args > func->func_nr_args
if (total_args > func->func_nr_args
&& !(func->func_flags & FX_FUNCTION_F_VARARG)) {
return FX_ERR_INVALID_ARGUMENT;
}
size_t nr_fixed_args = func->func_nr_args;
size_t nr_var_args = nr_args - nr_fixed_args;
size_t nr_var_args = total_args - nr_fixed_args;
struct callvm vm = {0};
callvm_reset(&vm, nr_fixed_args);
for (size_t i = 0; i < nr_args; i++) {
const fx_value *arg = &args[i];
callvm_push(&vm, arg);
size_t param_index = 0;
for (size_t i = 0; i < func->func_nr_bound_args; i++) {
const fx_value *arg = &func->func_bound_args[i];
fx_type_id param_type = NULL;
if (param_index < func->func_nr_args) {
param_type = func->func_arg_types[param_index];
}
push_arg(arg, param_type, &vm);
param_index++;
}
*return_value
= callvm_invoke(&vm, func->func_impl, func->func_return_type);
for (size_t i = 0; i < nr_args; i++) {
const fx_value *arg = &args[i];
fx_type_id param_type = NULL;
if (param_index < func->func_nr_args) {
param_type = func->func_arg_types[param_index];
}
push_arg(arg, param_type, &vm);
param_index++;
}
*return_value = callvm_invoke(
&vm,
func->func_impl,
func->func_return_type);
callvm_reset(&vm, 0);
return FX_SUCCESS;
@@ -59,21 +142,26 @@ static fx_status function_invoke(
/*** PUBLIC FUNCTIONS *********************************************************/
FX_API fx_function *fx_function_create(
fx_function *fx_function_create(
const char *name,
fx_function_flags flags,
fx_function_impl impl,
const fx_value_type *args,
const fx_type_id *args,
size_t nr_args,
fx_value_type return_type)
fx_type_id return_type)
{
fx_function *func = fx_object_create(FX_REFLECTION_TYPE_FUNCTION);
if (!func) {
return NULL;
}
struct fx_function_p *p
= fx_object_get_private(func, FX_REFLECTION_TYPE_FUNCTION);
struct fx_function_p *p = fx_object_get_private(
func,
FX_REFLECTION_TYPE_FUNCTION);
if (nr_args == 1 && args[0] == NULL) {
nr_args = 0;
}
p->func_name = fx_strdup(name);
p->func_flags = flags;
@@ -88,7 +176,25 @@ FX_API fx_function *fx_function_create(
return func;
}
FX_API fx_status fx_function_invoke(
const char *fx_function_get_name(const fx_function *func)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_REFLECTION_TYPE_FUNCTION,
function_get_name,
func);
}
fx_status fx_function_bind(fx_function *func, fx_value *args, size_t nr_args)
{
FX_CLASS_DISPATCH_STATIC(
FX_REFLECTION_TYPE_FUNCTION,
function_bind,
func,
args,
nr_args);
}
fx_status fx_function_invoke(
const fx_function *func,
const fx_value *args,
size_t nr_args,
@@ -122,17 +228,17 @@ static void function_fini(fx_object *obj, void *priv)
}
}
/*** CLASS DEFINITION
* *********************************************************/
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_function)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_function)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_function)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(fx_function)
FX_TYPE_DEFINITION_BEGIN(fx_function)
FX_TYPE_ID(0x09e40174, 0x7443, 0x486e, 0xad21, 0xcc9374762e7e);
FX_TYPE_NAME("fx.reflection.function");
FX_TYPE_CLASS(fx_function_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_function_p);
FX_TYPE_INSTANCE_INIT(function_init);
+21 -8
View File
@@ -1,6 +1,7 @@
#ifndef FX_REFLECTION_ASSEMBLY_H_
#define FX_REFLECTION_ASSEMBLY_H_
#include <fx/iterator.h>
#include <fx/macros.h>
FX_DECLS_BEGIN;
@@ -12,26 +13,38 @@ FX_DECLARE_TYPE(fx_assembly);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_assembly)
FX_TYPE_CLASS_DECLARATION_END(fx_assembly)
FX_API fx_type fx_assembly_get_type();
FX_API fx_type_id fx_assembly_get_type();
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_assembly, FX_REFLECTION_TYPE_ASSEMBLY);
FX_API void fx_assembly_set_name(fx_assembly *asm, const char *name);
FX_API fx_iterator *fx_assembly_get_all(void);
FX_API const fx_assembly *fx_assembly_get_by_name(const char *name);
FX_API fx_status fx_assembly_register(fx_assembly *assembly);
FX_API void fx_assembly_set_name(fx_assembly *assembly, const char *name);
FX_API void fx_assembly_set_version(
fx_assembly *asm,
fx_assembly *assembly,
long major,
long minor,
long build,
long revision);
FX_API void fx_assembly_add_type(
fx_assembly *asm,
fx_assembly *assembly,
const char *full_name,
fx_type type_id);
fx_type_id type_id);
FX_API fx_iterator *fx_assembly_get_types(const fx_assembly *assembly);
FX_API void fx_assembly_dump(const fx_assembly *asm);
FX_API const char *fx_assembly_get_name(const fx_assembly *assembly);
FX_API void fx_assembly_get_version(
const fx_assembly *assembly,
long *out_major,
long *out_minor,
long *out_build,
long *out_revision);
FX_DECLS_END
;
FX_API void fx_assembly_dump(const fx_assembly *assembly);
FX_DECLS_END;
#endif
+7 -42
View File
@@ -7,76 +7,41 @@
FX_DECLS_BEGIN;
#define FX_REFLECTION_TYPE_FUNCTION (fx_function_get_type())
#define FX_REFLECTION_TYPE_FUNCTION_CONTEXT (fx_function_context_get_type())
#define FX_REFLECTION_TYPE_FUNCTION (fx_function_get_type())
typedef enum fx_function_flags {
FX_FUNCTION_F_NONE = 0x00u,
FX_FUNCTION_F_STATIC = 0x01u,
FX_FUNCTION_F_VIRTUAL = 0x02u,
FX_FUNCTION_F_VARARG = 0x04u,
FX_FUNCTION_F_CONSTRUCTOR = 0x08u,
} fx_function_flags;
#if 0
#define FX_FUNCTION_VALUE(type) \
((fx_function_value) { \
.v_type = FX_FUNCTION_V_PRIMITIVE, \
.v_primitive = (type), \
})
typedef enum fx_function_value_type {
FX_FUNCTION_V_NONE = 0,
FX_FUNCTION_V_PRIMITIVE,
FX_FUNCTION_V_OBJECT,
} fx_function_value_type;
typedef struct fx_function_value {
fx_function_value_type v_type;
union {
fx_value_type v_primitive;
fx_type v_object;
};
} fx_function_value;
#endif
typedef void (*fx_function_impl)();
FX_DECLARE_TYPE(fx_function);
FX_DECLARE_TYPE(fx_function_context);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_function)
FX_TYPE_CLASS_DECLARATION_END(fx_function)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_function_context)
FX_TYPE_CLASS_DECLARATION_END(fx_function_context)
FX_API fx_type_id fx_function_get_type();
FX_API fx_type fx_function_get_type();
FX_API fx_type fx_function_context_get_type();
FX_API fx_function_context *fx_function_context_create(void);
FX_API void fx_function_context_reset(fx_function_context *ctx);
FX_API void fx_function_context_push_arg(
fx_function_context *ctx,
const fx_value *value);
FX_API const char *fx_function_get_name(const fx_function *func);
FX_API fx_function *fx_function_create(
const char *name,
fx_function_flags flags,
fx_function_impl impl,
const fx_value_type *args,
const fx_type_id *args,
size_t nr_args,
fx_value_type return_type);
fx_type_id return_type);
FX_API fx_status
fx_function_bind(const fx_function *func, const fx_value *args, size_t nr_args);
fx_function_bind(fx_function *func, fx_value *args, size_t nr_args);
FX_API fx_status fx_function_invoke(
const fx_function *func,
const fx_value *args,
size_t nr_args,
fx_value *return_value);
FX_API fx_status fx_function_invoke_with_context(
const fx_function *func,
const fx_function_context *ctx,
fx_value *return_value);
FX_DECLS_END;
@@ -1,4 +1,38 @@
#ifndef FX_REFLECTION_PROPERTY_H_
#define FX_REFLECTION_PROPERTY_H_
#include <fx/macros.h>
#include <fx/value.h>
FX_DECLS_BEGIN;
#define FX_REFLECTION_TYPE_PROPERTY (fx_property_get_type())
FX_DECLARE_TYPE(fx_property)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_property)
FX_TYPE_CLASS_DECLARATION_END(fx_property)
typedef fx_status (
*fx_property_getter)(const fx_value *, const fx_property *, fx_value *);
typedef fx_status (
*fx_property_setter)(fx_value *, const fx_property *, fx_value *);
FX_API fx_type_id fx_property_get_type(void);
FX_API fx_property *fx_property_create(
const char *name,
fx_property_getter get,
fx_property_setter set);
FX_API const char *fx_property_get_name(const fx_property *prop);
FX_API fx_status fx_property_get_value(
const fx_property *prop,
const fx_value *container,
fx_value *out);
FX_API fx_status fx_property_set_value(
const fx_property *prop,
fx_value *container,
fx_value *new_value);
#endif
@@ -0,0 +1,46 @@
#ifndef FX_REFLECTION_TYPE_H_
#define FX_REFLECTION_TYPE_H_
#include <fx/iterator.h>
#include <fx/macros.h>
#include <fx/reflection/assembly.h>
#include <fx/reflection/function.h>
#include <fx/reflection/property.h>
FX_DECLS_BEGIN;
struct fx_type_info;
#define FX_REFLECTION_TYPE_TYPE (fx_type_get_type())
FX_DECLARE_TYPE(fx_type);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_type)
FX_TYPE_CLASS_DECLARATION_END(fx_type)
FX_API fx_type_id fx_type_get_type();
FX_API const fx_assembly *fx_type_get_assembly(const fx_type *ty);
FX_API fx_status fx_type_set_assembly(fx_type *ty, fx_assembly *assembly);
FX_API const char *fx_type_get_name(const fx_type *ty);
FX_API fx_type_flags fx_type_get_flags(const fx_type *ty);
FX_API const fx_function *fx_type_get_function(
const fx_type *ty,
const char *name);
FX_API fx_iterator *fx_type_get_functions(const fx_type *ty);
FX_API const fx_property *fx_type_get_property(
const fx_type *ty,
const char *name);
FX_API fx_iterator *fx_type_get_properties(const fx_type *ty);
FX_API const fx_type *fx_type_get_by_id(fx_type_id id);
FX_API const fx_type *fx_type_get_by_name(const char *name);
FX_API fx_type_id fx_type_get_id(const fx_type *ty);
FX_API const fx_type *fx_type_get_parent(const fx_type *ty);
FX_API void *fx_type_get_interface(const fx_type *ty, fx_type_id interface_id);
FX_API fx_type *__fx_type_create(struct fx_type_info *opaque);
#endif
+140
View File
@@ -0,0 +1,140 @@
#include <fx/macros.h>
#include <fx/reflection/property.h>
#include <fx/string.h>
#include <fx/value-type.h>
#include <fx/value.h>
#include <platform/callvm.h>
struct fx_property_p {
fx_property *prop_self;
char *prop_name;
fx_property_getter prop_getter;
fx_property_setter prop_setter;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static const char *property_get_name(const struct fx_property_p *prop)
{
return prop->prop_name;
}
static fx_status property_get_value(
const struct fx_property_p *prop,
const fx_value *container,
fx_value *out)
{
if (!prop->prop_getter) {
return FX_ERR_NOT_SUPPORTED;
}
return prop->prop_getter(container, prop->prop_self, out);
}
static fx_status property_set_value(
const struct fx_property_p *prop,
fx_value *container,
fx_value *new_value)
{
if (!prop->prop_setter) {
return FX_ERR_NOT_SUPPORTED;
}
return prop->prop_setter(container, prop->prop_self, new_value);
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_property *fx_property_create(
const char *name,
fx_property_getter getter,
fx_property_setter setter)
{
fx_property *prop = fx_object_create(FX_REFLECTION_TYPE_PROPERTY);
if (!prop) {
return NULL;
}
struct fx_property_p *p = fx_object_get_private(
prop,
FX_REFLECTION_TYPE_PROPERTY);
p->prop_name = fx_strdup(name);
if (!p->prop_name) {
fx_property_unref(prop);
return NULL;
}
p->prop_self = prop;
p->prop_getter = getter;
p->prop_setter = setter;
return prop;
}
const char *fx_property_get_name(const fx_property *prop)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_REFLECTION_TYPE_PROPERTY,
property_get_name,
prop);
}
fx_status fx_property_get_value(
const fx_property *prop,
const fx_value *container,
fx_value *out)
{
FX_CLASS_DISPATCH_STATIC(
FX_REFLECTION_TYPE_PROPERTY,
property_get_value,
prop,
container,
out);
}
fx_status fx_property_set_value(
const fx_property *prop,
fx_value *container,
fx_value *new_value)
{
FX_CLASS_DISPATCH_STATIC(
FX_REFLECTION_TYPE_PROPERTY,
property_set_value,
prop,
container,
new_value);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void property_init(fx_object *obj, void *priv)
{
}
static void property_fini(fx_object *obj, void *priv)
{
struct fx_property_p *p = priv;
if (p->prop_name) {
free(p->prop_name);
p->prop_name = NULL;
}
}
/*** CLASS DEFINITION
* *********************************************************/
FX_TYPE_CLASS_BEGIN(fx_property)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(fx_property)
FX_TYPE_DEFINITION_BEGIN(fx_property)
FX_TYPE_ID(0x7007a5ef, 0x3caf, 0x4215, 0x8c0b, 0xd54bf8195624);
FX_TYPE_NAME("fx.reflection.property");
FX_TYPE_CLASS(fx_property_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_property_p);
FX_TYPE_INSTANCE_INIT(property_init);
FX_TYPE_INSTANCE_FINI(property_fini);
FX_TYPE_DEFINITION_END(fx_property)
+92
View File
@@ -0,0 +1,92 @@
.global _callvm_invoke_i
.align 4
# x0 = (function ptr) impl
# x1 = (struct callvm) context
_callvm_invoke_i:
# save the stack frame and link pointer
stp x29, x30, [sp, #-16]!
mov x29, sp
# preserve callee-saved registers
stp x19, x20, [sp, #-16]!
# store function pointer and callvm context for later
mov x19, x0
mov x20, x1
# First, set up the variable arguments
### calculate the amount of stack space needed for the varargs
# store the original stack pointer, so it can be restored later
mov x0, sp
str x0, [sp, #-16]!
# x8 = excess arg count
ldr x8, [x20, #32]
# x8 = excess arg buffer size (8 bytes per value)
lsl x8, x8, #3
# allocate the stack space
sub sp, sp, x8
# re-align the stack, and take a pointer to the newly-allocated buffer
mov x0, sp
and x0, x0, #0xFFFFFFFFFFFFFFF0
mov sp, x0
ldr x1, [x20, #176]
mov x2, x8
# memcpy(stack vararg buffer, callvm excess buffer, arg size)
bl _memcpy
# Next, set up the fixed integer arguments
ldr x0, [x20, #48]
ldr x1, [x20, #56]
ldr x2, [x20, #64]
ldr x3, [x20, #72]
ldr x4, [x20, #80]
ldr x5, [x20, #88]
ldr x6, [x20, #96]
ldr x7, [x20, #104]
# Finally, set up the fixed double arguments
ldr d0, [x20, #112]
ldr d1, [x20, #120]
ldr d2, [x20, #128]
ldr d3, [x20, #136]
ldr d4, [x20, #144]
ldr d5, [x20, #152]
ldr d6, [x20, #160]
ldr d7, [x20, #168]
# call the function implementation
blr x19
# restore the saved stack pointer, and de-allocate the stack varargs buffer
ldr x9, [x29, #-32]
mov sp, x9
# restore callee-saved registers
ldp x19, x20, [sp], #16
# restore the saved stack frame and link pointer
ldp x29, x30, [sp], #16
ret
.global _callvm_invoke_d
.align 4
# x0 = (function ptr) impl
# x1 = (struct callvm) context
_callvm_invoke_d:
b _callvm_invoke_i
.global _callvm_invoke_v
.align 4
# x0 = (function ptr) impl
# x1 = (struct callvm) context
_callvm_invoke_v:
b _callvm_invoke_i
+240
View File
@@ -0,0 +1,240 @@
#include <fx/type.h>
#include <fx/value-type.h>
#include <fx/value.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;
}
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++;
}
void callvm_push(struct callvm *vm, const fx_value *value)
{
if (vm->vm_arg_count >= vm->vm_arg_fixed) {
push_excess(vm, (uintptr_t)value->v_pointer);
return;
}
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));
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:
return FX_VALUE_EMPTY;
}
}
@@ -0,0 +1,43 @@
#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 CALLVM_INVOKE_PROTOTYPE(suffix) \
fx_value callvm_invoke_##suffix( \
struct callvm *vm, \
fx_function_impl impl)
#define MAX_FIXED_ARGS ((unsigned int)-1)
#define MAX_DOUBLE_ARGS 8
#define MAX_INT_ARGS 8
/* 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;
};
typedef fx_value (*callvm_invoke_impl)(struct callvm *, fx_function_impl);
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
-110
View File
@@ -1,110 +0,0 @@
#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;
}
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++;
}
void callvm_push(struct callvm *vm, const fx_value *value)
{
if (vm->vm_arg_count >= vm->vm_arg_fixed) {
push_excess(vm, (uintptr_t)value->v_pointer);
return;
}
switch (value->v_type.t_primitive) {
case FX_VALUE_TYPE_DOUBLE:
push_double(vm, value->v_double);
break;
default:
push_int(vm, (uintptr_t)value->v_pointer);
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_value_type return_type)
{
switch (return_type) {
case FX_VALUE_TYPE_NONE:
callvm_invoke_v(impl, vm);
break;
case FX_VALUE_TYPE_DOUBLE:
return FX_VALUE_DOUBLE(callvm_invoke_d(impl, vm));
default:
return FX_VALUE_INT(callvm_invoke_i(impl, vm));
}
return FX_VALUE_EMPTY;
}
@@ -1,178 +0,0 @@
.global _callvm_invoke_i
.align 4
# x0 = (function ptr) impl
# x1 = (struct callvm) context
_callvm_invoke_i:
# save the stack frame and link pointer
stp x29, x30, [sp, #-16]!
mov x29, sp
# store function pointer for later
mov x16, x0
mov x15, x1
# First, set up the fixed arguments
# x8 = fixed arg count
ldr x8, [x15, #0]
# arg[0]
cmp x8, #0
ble .iv.idone
ldr x0, [x15, #48]
# arg[1]
cmp x8, #1
ble .iv.idone
ldr x1, [x15, #56]
# arg[2]
cmp x8, #2
ble .iv.idone
ldr x2, [x15, #64]
# arg[3]
cmp x8, #3
ble .iv.idone
ldr x3, [x15, #72]
# arg[4]
cmp x8, #4
ble .iv.idone
ldr x4, [x15, #80]
# arg[5]
cmp x8, #5
ble .iv.idone
ldr x5, [x15, #88]
# arg[6]
cmp x8, #6
ble .iv.idone
ldr x6, [x15, #96]
# arg[7]
cmp x8, #7
ble .iv.idone
ldr x7, [x15, #104]
.iv.idone:
# Next, set up the fixed double arguments
# x8 = fixed double arg count
ldr x8, [x15, #8]
# arg[0]
cmp x8, #0
ble .iv.vdone
ldr d0, [x15, #112]
# arg[1]
cmp x8, #1
ble .iv.vdone
ldr d1, [x15, #120]
# arg[2]
cmp x8, #2
ble .iv.vdone
ldr d2, [x15, #128]
# arg[3]
cmp x8, #3
ble .iv.vdone
ldr d3, [x15, #136]
# arg[4]
cmp x8, #4
ble .iv.vdone
ldr d4, [x15, #144]
# arg[5]
cmp x8, #5
ble .iv.vdone
ldr d5, [x15, #152]
# arg[6]
cmp x8, #6
ble .iv.vdone
ldr d6, [x15, #160]
# arg[7]
cmp x8, #7
ble .iv.vdone
ldr d7, [x15, #168]
.iv.vdone:
# Finally, set up the variable arguments
# x8 = excess arg count
ldr x8, [x15, #32]
# calculate the amount of stack space needed for the varargs
mov x10, #0x10
mul x12, x8, x10
# x19 is caller-saved, and we'll need x12 later
str x19, [sp, #-16]!
mov x19, x12
# allocate the stack space
sub sp, sp, x19
mov x13, sp
# get a pointer to the end of the stack vararg buffer
#mov x14, #8
#mul x14, x14, x11
#add x13, x13, x14
# x8: number of var args (decrements with every iteration)
# x9: arg src buffer (increments with every iteration)
# x13: var arg dest pointer (starts at the end, decrements with every iteration)
ldr x9, [x15, #176]
.iv.loop:
cmp x8, #0
beq .iv.loop.end
# read the arg value from the src pointer
ldr x14, [x9]
# write it to the stack, and increment the dest pointer
str x14, [x13]
add x13, x13, #8
# increment the src pointer, decrement the arg count
sub x8, x8, #1
add x9, x9, #8
b .iv.loop
.iv.loop.end:
# call the function implementation
blr x16
# de-allocate the stack varargs buffer (the size is now stored in x19)
add sp, sp, x19
# restore the saved value of x19,
ldr x19, [sp], #16
# restore the saved stack frame and link pointer
ldp x29,x30, [sp], #16
ret
.global _callvm_invoke_d
.align 4
# x0 = (function ptr) impl
# x1 = (struct callvm) context
_callvm_invoke_d:
b _callvm_invoke_i
.global _callvm_invoke_v
.align 4
# x0 = (function ptr) impl
# x1 = (struct callvm) context
_callvm_invoke_v:
b _callvm_invoke_i
@@ -1,221 +0,0 @@
.global _fx_function_invoke_i
.align 4
# x0 = (function ptr) impl
# x1 = (fx_value[]) args
# x2 = (size_t) nr args
_fx_function_invoke_i:
# save the stack frame and link pointer
stp x29, x30, [sp, #-16]!
# store function pointer for later
str x0, [sp, #-16]!
# move arg array of of x1 and x2, so that they can be initialised for
# the function call
mov x8, x1
mov x9, x2
# arg[0]
cmp x9, #0
ble .i.done
ldr x0, [x8, #8]
# arg[1]
cmp x9, #1
ble .i.done
ldr x1, [x8, #24]
# arg[2]
cmp x9, #2
ble .i.done
ldr x2, [x8, #40]
# arg[3]
cmp x9, #3
ble .i.done
ldr x3, [x8, #56]
# arg[4]
cmp x9, #4
ble .i.done
ldr x4, [x8, #72]
# arg[5]
cmp x9, #5
ble .i.done
ldr x5, [x8, #88]
# arg[6]
cmp x9, #6
ble .i.done
ldr x6, [x8, #104]
# arg[7]
cmp x9, #7
ble .i.done
ldr x7, [x8, #120]
.i.done:
ldr x8, [sp], #16
blr x8
# restore the saved stack frame and link pointer
ldp x29,x30, [sp], #16
ret
.global _fx_function_invoke_d
.align 4
_fx_function_invoke_d:
mov x0, 888
ret
.global _fx_function_invoke_v
.align 4
_fx_function_invoke_v:
ret
.global _fx_function_invoke_iv
.align 4
# x0 = (function ptr) impl
# x1 = (fx_value[]) args
# x2 = (size_t) nr fixed args
# x3 = (size_t) nr var args
_fx_function_invoke_iv:
# save the stack frame and link pointer
stp x29, x30, [sp, #-16]!
mov x29, sp
# store function pointer for later
mov x15, x0
# move arg array of of x1,x2,x3, so that they can be initialised for
# the function call
mov x9, x1
mov x10, x2
mov x11, x3
# First, set up the fixed arguments
# arg[0]
cmp x10, #0
ble .iv.done
ldr x0, [x9, #8]
add x9, x9, #16
# arg[1]
cmp x10, #1
ble .iv.done
ldr x1, [x9, #8]
add x9, x9, #16
# arg[2]
cmp x10, #2
ble .iv.done
ldr x2, [x9, #8]
add x9, x9, #16
# arg[3]
cmp x10, #3
ble .iv.done
ldr x3, [x9, #8]
add x9, x9, #16
# arg[4]
cmp x10, #4
ble .iv.done
ldr x4, [x9, #8]
add x9, x9, #16
# arg[5]
cmp x10, #5
ble .iv.done
ldr x5, [x9, #8]
add x9, x9, #16
# arg[6]
cmp x10, #6
ble .iv.done
ldr x6, [x9, #8]
add x9, x9, #16
# arg[7]
cmp x10, #7
ble .iv.done
ldr x7, [x9, #8]
add x9, x9, #16
.iv.done:
# Next, set up the variable arguments
# calculate the amount of stack space needed for the varargs
mov x10, #0x10
mul x12, x11, x10
#and x12, x12, #0x10
#mov x10, #0x10
#add x12, x12, x10
# x19 is caller-saved, and we'll need x12 later
str x19, [sp, #-16]!
mov x19, x12
# allocate the stack space
sub sp, sp, x19
mov x13, sp
# get a pointer to the end of the stack vararg buffer
#mov x14, #8
#mul x14, x14, x11
#add x13, x13, x14
# x9: arg src buffer (starts at varargs, increments with every iteration)
# x11: number of var args (decrements with every iteration)
# x13: var arg dest pointer (starts at the end, decrements with every iteration)
.iv.loop:
cmp x11, #0
beq .iv.loop.end
# read the arg value from the src pointer
ldr x14, [x9, #8]
# write it to the stack, and increment the dest pointer
str x14, [x13]
add x13, x13, #8
# increment the src pointer, decrement the arg count
sub x11, x11, #1
add x9, x9, #16
b .iv.loop
.iv.loop.end:
# call the function implementation
blr x15
# de-allocate the stack varargs buffer (the size is now stored in x19)
add sp, sp, x19
# restore the saved value of x19,
ldr x19, [sp], #16
# restore the saved stack frame and link pointer
ldp x29,x30, [sp], #16
ret
.global _fx_function_invoke_dv
.align 4
_fx_function_invoke_dv:
ret
.global _fx_function_invoke_vv
.align 4
_fx_function_invoke_vv:
ret
+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
+241
View File
@@ -0,0 +1,241 @@
#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));
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:
return FX_VALUE_EMPTY;
}
return FX_VALUE_EMPTY;
}
@@ -7,7 +7,7 @@
#define MAX_FIXED_ARGS ((unsigned int)-1)
#define MAX_DOUBLE_ARGS 8
#define MAX_INT_ARGS 8
#define MAX_INT_ARGS 6
/* dyn-dispatch.S depends on the layout of this struct */
struct callvm {
@@ -24,6 +24,7 @@ struct callvm {
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);
@@ -31,6 +32,6 @@ extern void callvm_push(struct callvm *vm, const fx_value *value);
extern fx_value callvm_invoke(
struct callvm *vm,
fx_function_impl impl,
fx_value_type return_type);
fx_type_id return_type);
#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
+430
View File
@@ -0,0 +1,430 @@
#include "../fx/type.h"
#include <fx/macros.h>
#include <fx/reflection/type.h>
#include <fx/type.h>
struct fx_type_registration;
#define FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR \
(fx_type_function_iterator_get_type())
#define FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR \
(fx_type_property_iterator_get_type())
FX_DECLARE_TYPE(fx_type_function_iterator);
FX_DECLARE_TYPE(fx_type_property_iterator);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_type_function_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_type_function_iterator)
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_type_property_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_type_property_iterator)
FX_API fx_type_id fx_type_function_iterator_get_type();
FX_API fx_type_id fx_type_property_iterator_get_type();
struct fx_type_p {
struct fx_type_info *ty_info;
fx_assembly *ty_assembly;
};
struct fx_type_function_iterator_p {
struct fx_namemap *it_src;
struct fx_namemap_entry *it_cur;
fx_value it_value;
};
struct fx_type_property_iterator_p {
const fx_type_info *it_cur_type;
struct fx_namemap *it_src;
struct fx_namemap_entry *it_cur;
fx_value it_value;
};
/*** PRIVATE FUNCTIONS ********************************************************/
extern struct fx_type_info *fx_type_info_get_by_id(const union fx_type_id *key);
extern struct fx_type_info *fx_type_info_get_by_name(const char *name);
extern fx_function *fx_type_info_get_function_by_name(
const struct fx_type_info *ty,
const char *name);
static const fx_assembly *type_get_assembly(const struct fx_type_p *ty)
{
return ty->ty_assembly;
}
static fx_status type_set_assembly(struct fx_type_p *ty, fx_assembly *assembly)
{
ty->ty_assembly = assembly;
return FX_SUCCESS;
}
static const char *type_get_name(const struct fx_type_p *ty)
{
return ty->ty_info->ty_name;
}
static fx_type_flags type_get_flags(const struct fx_type_p *ty)
{
return ty->ty_info->ty_flags;
}
const fx_function *type_get_function(
const struct fx_type_p *ty,
const char *name)
{
return fx_type_info_get_function_by_name(ty->ty_info, name);
}
fx_iterator *type_get_functions(const struct fx_type_p *ty)
{
fx_type_function_iterator *it_obj
= fx_object_create(FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
struct fx_type_function_iterator_p *it = fx_object_get_private(
it_obj,
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
it->it_src = &ty->ty_info->ty_functions;
it->it_cur = fx_namemap_first(it->it_src);
return it_obj;
}
const fx_property *type_get_property(
const struct fx_type_p *ty,
const char *name)
{
struct fx_type_info *cur = ty->ty_info;
while (cur) {
const fx_property *prop
= fx_type_info_get_property_by_name(cur, name);
if (prop) {
return prop;
}
if (fx_type_id_compare(&cur->ty_id, FX_TYPE_OBJECT) == 0) {
break;
}
cur = fx_type_info_get_by_id(&cur->ty_parent_id);
}
return NULL;
}
static fx_iterator *type_get_properties(const struct fx_type_p *ty)
{
fx_type_property_iterator *it_obj
= fx_object_create(FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
struct fx_type_property_iterator_p *it = fx_object_get_private(
it_obj,
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
it->it_cur_type = ty->ty_info;
it->it_src = &ty->ty_info->ty_properties;
it->it_cur = fx_namemap_first(it->it_src);
while (!it->it_cur) {
if (fx_type_id_compare(&it->it_cur_type->ty_id, FX_TYPE_OBJECT)
== 0) {
break;
}
const struct fx_type_info *next_type
= fx_type_info_get_by_id(&it->it_cur_type
->ty_parent_id);
if (!next_type) {
break;
}
it->it_cur_type = next_type;
it->it_cur = fx_namemap_first(&it->it_cur_type->ty_properties);
}
if (!it->it_cur) {
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;
}
static fx_type_id type_get_id(const struct fx_type_p *ty)
{
return &ty->ty_info->ty_id;
}
static const fx_type *type_get_parent(const struct fx_type_p *ty)
{
if (fx_type_id_compare(&ty->ty_info->ty_id, FX_TYPE_OBJECT) == 0) {
return NULL;
}
return fx_type_get_by_id(&ty->ty_info->ty_parent_id);
}
static void *type_get_interface(
const struct fx_type_p *ty,
fx_type_id interface_id)
{
return fx_class_get_interface(ty->ty_info->ty_class, interface_id);
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_type *__fx_type_create(struct fx_type_info *type_info)
{
fx_type *out = fx_object_create(FX_REFLECTION_TYPE_TYPE);
if (!out) {
return NULL;
}
struct fx_type_p *p
= fx_object_get_private(out, FX_REFLECTION_TYPE_TYPE);
p->ty_info = type_info;
return out;
}
const fx_assembly *fx_type_get_assembly(const fx_type *ty)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_REFLECTION_TYPE_TYPE,
type_get_assembly,
ty);
}
fx_status fx_type_set_assembly(fx_type *ty, fx_assembly *assembly)
{
FX_CLASS_DISPATCH_STATIC(
FX_REFLECTION_TYPE_TYPE,
type_set_assembly,
ty,
assembly);
}
const char *fx_type_get_name(const fx_type *ty)
{
FX_CLASS_DISPATCH_STATIC_0(FX_REFLECTION_TYPE_TYPE, type_get_name, ty);
}
fx_type_flags fx_type_get_flags(const fx_type *ty)
{
FX_CLASS_DISPATCH_STATIC_0(FX_REFLECTION_TYPE_TYPE, type_get_flags, ty);
}
const fx_function *fx_type_get_function(const fx_type *ty, const char *name)
{
FX_CLASS_DISPATCH_STATIC(
FX_REFLECTION_TYPE_TYPE,
type_get_function,
ty,
name);
}
fx_iterator *fx_type_get_functions(const fx_type *ty)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_REFLECTION_TYPE_TYPE,
type_get_functions,
ty);
}
const fx_property *fx_type_get_property(const fx_type *ty, const char *name)
{
FX_CLASS_DISPATCH_STATIC(
FX_REFLECTION_TYPE_TYPE,
type_get_property,
ty,
name);
}
fx_iterator *fx_type_get_properties(const fx_type *ty)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_REFLECTION_TYPE_TYPE,
type_get_properties,
ty);
}
const fx_type *fx_type_get_by_id(fx_type_id id)
{
struct fx_type_info *ty = fx_type_info_get_by_id(id);
return ty ? ty->ty_metatype : NULL;
}
const fx_type *fx_type_get_by_name(const char *name)
{
struct fx_type_info *ty = fx_type_info_get_by_name(name);
return ty ? ty->ty_metatype : NULL;
}
fx_type_id fx_type_get_id(const fx_type *ty)
{
FX_CLASS_DISPATCH_STATIC_0(FX_REFLECTION_TYPE_TYPE, type_get_id, ty);
}
const fx_type *fx_type_get_parent(const fx_type *ty)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_REFLECTION_TYPE_TYPE,
type_get_parent,
ty);
}
void *fx_type_get_interface(const fx_type *ty, fx_type_id interface_id)
{
FX_CLASS_DISPATCH_STATIC(
FX_REFLECTION_TYPE_TYPE,
type_get_interface,
ty,
interface_id);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void type_init(fx_object *obj, void *priv)
{
}
static void type_fini(fx_object *obj, void *priv)
{
}
/*** ITERATOR FUNCTIONS *******************************************************/
static enum fx_status function_iterator_move_next(const fx_iterator *obj)
{
struct fx_type_function_iterator_p *it = fx_object_get_private(
obj,
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
if (!it->it_cur) {
return FX_ERR_NO_DATA;
}
fx_value_unset(&it->it_value);
it->it_cur = fx_namemap_next(it->it_src, it->it_cur);
if (!it->it_cur) {
return FX_ERR_NO_DATA;
}
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(
obj,
FX_REFLECTION_TYPE_TYPE_FUNCTION_ITERATOR);
if (!it->it_cur) {
return NULL;
}
return &it->it_value;
}
static enum fx_status property_iterator_move_next(const fx_iterator *obj)
{
struct fx_type_property_iterator_p *it = fx_object_get_private(
obj,
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
if (!it->it_cur) {
return FX_ERR_NO_DATA;
}
it->it_cur = fx_namemap_next(it->it_src, it->it_cur);
if (!it->it_cur) {
if (fx_type_id_compare(&it->it_cur_type->ty_id, FX_TYPE_OBJECT)
== 0) {
return FX_ERR_NO_DATA;
}
const struct fx_type_info *next_type
= fx_type_info_get_by_id(&it->it_cur_type
->ty_parent_id);
if (!next_type) {
return FX_ERR_NO_DATA;
}
it->it_cur_type = next_type;
it->it_cur = fx_namemap_first(&it->it_cur_type->ty_properties);
}
if (!it->it_cur) {
return 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 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(
obj,
FX_REFLECTION_TYPE_TYPE_PROPERTY_ITERATOR);
if (!it->it_cur) {
return NULL;
}
struct fx_type_property *prop
= fx_unbox(struct fx_type_property, it->it_cur, p_entry);
return &it->it_value;
}
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_BEGIN(fx_type)
FX_TYPE_CLASS_END(fx_type)
FX_TYPE_DEFINITION_BEGIN(fx_type)
FX_TYPE_ID(0xec8b2679, 0x5c73, 0x4ec9, 0xa04e, 0x3f72881b5b5a);
FX_TYPE_NAME("fx.reflection.type");
FX_TYPE_CLASS(fx_type_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_type_p);
FX_TYPE_INSTANCE_INIT(type_init);
FX_TYPE_INSTANCE_FINI(type_fini);
FX_TYPE_DEFINITION_END(fx_type)
FX_TYPE_CLASS_BEGIN(fx_type_function_iterator)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = function_iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = NULL;
FX_INTERFACE_ENTRY(it_get_value) = function_iterator_get_value;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_type_function_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_type_function_iterator)
FX_TYPE_ID(0xb29012d5, 0x4d6c, 0x4e3c, 0x9fe5, 0x7d1a8a9e1075);
FX_TYPE_NAME("fx.reflection.type.function_iterator");
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_type_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_type_function_iterator_p);
FX_TYPE_DEFINITION_END(fx_type_function_iterator)
FX_TYPE_CLASS_BEGIN(fx_type_property_iterator)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = property_iterator_move_next;
FX_INTERFACE_ENTRY(it_erase) = NULL;
FX_INTERFACE_ENTRY(it_get_value) = property_iterator_get_value;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_type_property_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_type_property_iterator)
FX_TYPE_ID(0x91fcbdd4, 0x7c54, 0x48c2, 0xa5a4, 0x186f76efd84c);
FX_TYPE_NAME("fx.reflection.type.property_iterator");
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_type_property_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_type_property_iterator_p);
FX_TYPE_DEFINITION_END(fx_type_property_iterator)
+8 -8
View File
@@ -5,7 +5,7 @@
static struct fx_error *bitcode_serialise(
fx_serial_ctx *serial,
fx_object *src,
const fx_value *src,
fx_stream *dest,
enum fx_serial_flags flags)
{
@@ -15,7 +15,7 @@ static struct fx_error *bitcode_serialise(
static struct fx_error *bitcode_deserialise(
fx_serial_ctx *serial,
fx_stream *src,
fx_object **dest,
fx_value *dest,
enum fx_serial_flags flags)
{
return FX_RESULT_ERR(NOT_SUPPORTED);
@@ -31,16 +31,16 @@ static void bitcode_serial_ctx_fini(fx_object *obj, void *priv)
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_bitcode_serial_ctx)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_bitcode_serial_ctx)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_serial_ctx, FX_TYPE_SERIAL_CTX)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_serial_ctx, FX_TYPE_SERIAL_CTX)
FX_INTERFACE_ENTRY(s_serialise) = bitcode_serialise;
FX_INTERFACE_ENTRY(s_deserialise) = bitcode_deserialise;
FX_TYPE_CLASS_INTERFACE_END(fx_serial_ctx, FX_TYPE_SERIAL_CTX)
FX_TYPE_CLASS_DEFINITION_END(fx_bitcode_serial_ctx)
FX_TYPE_VTABLE_INTERFACE_END(fx_serial_ctx, FX_TYPE_SERIAL_CTX)
FX_TYPE_CLASS_END(fx_bitcode_serial_ctx)
FX_TYPE_DEFINITION_BEGIN(fx_bitcode_serial_ctx)
FX_TYPE_ID(0xcdc8c462, 0xf2b3, 0x4193, 0x8cae, 0xc1e5ad9afcb8);
+6 -6
View File
@@ -26,11 +26,11 @@ static void serial_ctx_fini(fx_object *obj, void *priv)
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_serial_ctx)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_serial_ctx)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_serial_ctx)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(fx_serial_ctx)
FX_TYPE_DEFINITION_BEGIN(fx_serial_ctx)
FX_TYPE_ID(0xc7c1039a, 0xf397, 0x4fda, 0xb473, 0x4d86fec85384);
@@ -44,7 +44,7 @@ FX_TYPE_DEFINITION_END(fx_serial_ctx)
fx_result fx_serial_ctx_serialise(
fx_serial_ctx *ctx,
fx_object *src,
const fx_value *src,
fx_stream *dest,
enum fx_serial_flags flags)
{
@@ -62,7 +62,7 @@ fx_result fx_serial_ctx_serialise(
fx_result fx_serial_ctx_deserialise(
fx_serial_ctx *ctx,
fx_stream *src,
fx_object **dest,
fx_value *dest,
enum fx_serial_flags flags)
{
FX_CLASS_DISPATCH_VIRTUAL(
+1 -1
View File
@@ -12,7 +12,7 @@ FX_DECLARE_TYPE(fx_bitcode_serial_ctx);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_bitcode_serial_ctx)
FX_TYPE_CLASS_DECLARATION_END(fx_bitcode_serial_ctx)
FX_API fx_type fx_bitcode_serial_ctx_get_type(void);
FX_API fx_type_id fx_bitcode_serial_ctx_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_bitcode_serial_ctx, FX_TYPE_BITCODE_SERIAL_CTX);
+6 -5
View File
@@ -6,6 +6,7 @@
#include <fx/object.h>
#include <fx/status.h>
#include <fx/stream.h>
#include <fx/value.h>
FX_DECLS_BEGIN;
@@ -21,13 +22,13 @@ FX_DECLARE_TYPE(fx_serial_ctx);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_serial_ctx)
fx_result (*s_serialise)(
fx_serial_ctx *,
fx_object *,
const fx_value *,
fx_stream *,
fx_serial_flags);
fx_result (*s_deserialise)(
fx_serial_ctx *,
fx_stream *,
fx_object **,
fx_value *,
fx_serial_flags);
FX_TYPE_CLASS_DECLARATION_END(fx_serial_ctx)
@@ -35,18 +36,18 @@ typedef struct fx_serial_ctx_data {
fx_stream_buffer *ctx_streambuf;
} fx_serial_ctx_data;
FX_API fx_type fx_serial_ctx_get_type(void);
FX_API fx_type_id fx_serial_ctx_get_type(void);
FX_API fx_result fx_serial_ctx_serialise(
fx_serial_ctx *ctx,
fx_object *src,
const fx_value *src,
fx_stream *dest,
fx_serial_flags flags);
FX_API fx_result fx_serial_ctx_deserialise(
fx_serial_ctx *ctx,
fx_stream *src,
fx_object **dest,
fx_value *dest,
fx_serial_flags flags);
FX_DECLS_END;
+1 -1
View File
@@ -12,7 +12,7 @@ FX_DECLARE_TYPE(fx_toml_serial_ctx);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_toml_serial_ctx)
FX_TYPE_CLASS_DECLARATION_END(fx_toml_serial_ctx)
FX_API fx_type fx_toml_serial_ctx_get_type(void);
FX_API fx_type_id fx_toml_serial_ctx_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_toml_serial_ctx, FX_TYPE_TOML_SERIAL_CTX);
+11 -9
View File
@@ -1,21 +1,22 @@
#include <fx/core/stream.h>
#include <fx/ds/array.h>
#include <fx/ds/dict.h>
#include <fx/ds/number.h>
#include <fx/ds/string.h>
#include <fx/serial.h>
#include <fx/collections/array.h>
#include <fx/int.h>
#include <fx/serial/ctx.h>
#include <fx/serial/toml.h>
#include <fx/stream.h>
#include <fx/string.h>
#include <stdio.h>
int main(void)
{
#if 0
fx_serial_ctx *ctx = fx_toml_serial_ctx_create();
fx_dict *dict = fx_dict_create();
fx_array *array = fx_array_create();
fx_array_append(array, FX_RV_INT(32));
fx_array_append(array, FX_RV_INT(64));
fx_array_append(array, FX_RV_INT(128));
fx_array_append(array, fx_int_create(32));
fx_array_append(array, fx_int_create(64));
fx_array_append(array, fx_int_create(128));
fx_dict_put(dict, "numbers", FX_RV(array));
@@ -32,6 +33,7 @@ int main(void)
fx_dict_unref(dict);
fx_serial_ctx_unref(ctx);
#endif
return 0;
}
+64 -86
View File
@@ -1,20 +1,26 @@
#include <fx/ds/array.h>
#include <fx/ds/datetime.h>
#include <fx/ds/dict.h>
#include <fx/ds/number.h>
#include <fx/ds/string.h>
#include <fx/serial.h>
#include <fx/bool.h>
#include <fx/collections/array.h>
#include <fx/collections/hashtable.h>
#include <fx/datetime.h>
#include <fx/float.h>
#include <fx/int.h>
#include <fx/serial/ctx.h>
#include <fx/serial/toml.h>
#include <fx/string.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)
{
fx_stream_write_cstr(fx_stdout, "\"", NULL);
const fx_iterator *it = fx_iterator_cbegin(data);
fx_foreach_c(fx_wchar, c, it)
const fx_iterator *it = fx_iterator_begin(data);
fx_foreach(val, it)
{
fx_wchar c;
fx_value_get_wchar(val, &c);
if (c >= 0x10000) {
c -= 0x10000;
long hi = 0xD800 | ((c >> 10) & 0x3FF);
@@ -50,69 +56,48 @@ void write_tagged_string(fx_string *data)
fx_stream_write_cstr(fx_stdout, " }", NULL);
}
void write_tagged_integer(fx_number *data)
void write_tagged_integer(const fx_value *data)
{
fx_stream_write_cstr(
fx_stdout,
"{ \"type\": \"integer\", \"value\": \"",
NULL);
if (fx_number_is_inf_positive(data)) {
fx_stream_write_cstr(fx_stdout, "inf", NULL);
} else if (fx_number_is_inf_negative(data)) {
fx_stream_write_cstr(fx_stdout, "-inf", NULL);
} else if (fx_number_is_nan_positive(data)) {
fx_stream_write_cstr(fx_stdout, "nan", NULL);
} else if (fx_number_is_nan_negative(data)) {
fx_stream_write_cstr(fx_stdout, "-nan", NULL);
} else {
fx_stream_write_fmt(
fx_stdout,
NULL,
"%lld",
fx_number_get_longlong(data),
NULL);
}
fx_stream_write_fmt(fx_stdout, NULL, "%lld", data->v_i64, NULL);
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
}
void write_tagged_float(fx_number *data)
void write_tagged_float(const fx_value *data)
{
fx_stream_write_cstr(
fx_stdout,
"{ \"type\": \"float\", \"value\": \"",
NULL);
if (fx_number_is_inf_positive(data)) {
double d = data->v_double;
if (d == INFINITY) {
fx_stream_write_cstr(fx_stdout, "inf", NULL);
} else if (fx_number_is_inf_negative(data)) {
} else if (d == -INFINITY) {
fx_stream_write_cstr(fx_stdout, "-inf", NULL);
} else if (fx_number_is_nan_positive(data)) {
} else if (d == NAN) {
fx_stream_write_cstr(fx_stdout, "nan", NULL);
} else if (fx_number_is_nan_negative(data)) {
} else if (d == -NAN) {
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 {
double v = fx_number_get_double(data);
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_fmt(fx_stdout, NULL, "%.9lf", d, NULL);
}
fx_stream_write_cstr(fx_stdout, "\" }", NULL);
}
void write_tagged_bool(fx_number *data)
void write_tagged_bool(const fx_value *data)
{
int v = fx_number_get_int8(data);
fx_stream_write_fmt(
fx_stdout,
NULL,
"{ \"type\": \"bool\", \"value\": \"%s\" }",
(v > 0) ? "true" : "false",
data->v_bool ? "true" : "false",
NULL);
}
@@ -143,31 +128,37 @@ void write_tagged_datetime(fx_datetime *data)
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_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_string_unref(new_data);
fx_stringstream_unref(new_data);
}
void write_tagged_dict(fx_dict *data)
void write_tagged_hashtable(fx_hashtable *data)
{
fx_stream_write_cstr(fx_stdout, "{ ", NULL);
int i = 0;
fx_iterator *it = fx_iterator_begin(data);
fx_foreach(fx_dict_item *, item, it)
const fx_iterator *it = fx_iterator_begin(data);
fx_foreach(v, it)
{
fx_hashtable_item *item;
fx_value_get_object(v, &item);
if (i++ > 0) {
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);
write_tagged_value(item->value);
write_tagged_value(value);
}
fx_iterator_unref(it);
@@ -179,48 +170,36 @@ void write_tagged_array(fx_array *data)
fx_stream_write_cstr(fx_stdout, "[ ", NULL);
int i = 0;
fx_iterator *it = fx_iterator_begin(data);
fx_foreach(fx_object *, obj, it)
const fx_iterator *it = fx_iterator_begin(data);
fx_foreach(val, it)
{
if (i++ > 0) {
fx_stream_write_cstr(fx_stdout, ", ", NULL);
}
write_tagged_value(obj);
write_tagged_value(val);
}
fx_iterator_unref(it);
fx_stream_write_cstr(fx_stdout, " ]", NULL);
}
void write_tagged_value(fx_object *data)
void write_tagged_value(const fx_value *data)
{
if (fx_object_is_type(data, FX_TYPE_DICT)) {
write_tagged_dict(data);
} else if (fx_object_is_type(data, FX_TYPE_ARRAY)) {
write_tagged_array(data);
} else if (fx_object_is_type(data, FX_TYPE_STRING)) {
write_tagged_string(data);
} else if (fx_object_is_type(data, FX_TYPE_DATETIME)) {
write_tagged_datetime(data);
} else if (fx_object_is_type(data, FX_TYPE_NUMBER)) {
switch (fx_number_get_number_type(data)) {
case FX_NUMBER_LONGLONG:
write_tagged_integer(data);
break;
case FX_NUMBER_INT8:
write_tagged_bool(data);
break;
case FX_NUMBER_DOUBLE:
write_tagged_float(data);
break;
default:
break;
}
if (fx_value_is_type(data, FX_TYPE_HASHTABLE)) {
write_tagged_hashtable(data->v_object);
} else if (fx_value_is_type(data, FX_TYPE_ARRAY)) {
write_tagged_array(data->v_object);
} else if (fx_value_is_type(data, FX_TYPE_STRING)) {
write_tagged_string(data->v_object);
} else if (fx_value_is_type(data, FX_TYPE_DATETIME)) {
write_tagged_datetime(data->v_object);
} else if (fx_value_is_type(data, FX_TYPE_BOOL)) {
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_object *data;
fx_value data;
fx_result result = fx_serial_ctx_deserialise(ctx, src, &data, 0);
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_serial_ctx_unref(ctx);
fx_object_unref(data);
fx_value_unset(&data);
return 0;
}
+204 -185
View File
@@ -1,17 +1,17 @@
#include <fx/bool.h>
#include <fx/collections/array.h>
#include <fx/collections/datetime.h>
#include <fx/collections/dict.h>
#include <fx/collections/hashmap.h>
#include <fx/double.h>
#include <fx/collections/hashtable.h>
#include <fx/datetime.h>
#include <fx/error.h>
#include <fx/float.h>
#include <fx/int.h>
#include <fx/serial/ctx.h>
#include <fx/serial/toml.h>
#include <fx/status.h>
#include <fx/string.h>
#include <fx/stringstream.h>
#include <fx/uint.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
@@ -117,9 +117,9 @@ struct ctx {
fx_stream *ctx_src;
fx_string *ctx_wordbuf;
fx_string *ctx_linebuf;
fx_iterator *ctx_linebuf_ptr;
const fx_iterator *ctx_linebuf_ptr;
fx_result ctx_result;
fx_hashmap *ctx_objects_flags;
fx_hashtable *ctx_objects_flags;
fx_queue ctx_tokens;
};
@@ -133,28 +133,18 @@ static void ctx_set_object_flags(
return;
}
fx_hashmap_key key = {
.key_data = obj,
.key_size = sizeof(fx_object *),
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
};
const fx_hashmap_value *old_value
= fx_hashmap_get(ctx->ctx_objects_flags, &key);
fx_value ptr = FX_POINTER(obj);
const fx_value *old_value
= fx_hashtable_get(ctx->ctx_objects_flags, &ptr);
enum object_flags new_flags = 0;
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;
fx_hashmap_value value = {
.value_data = (void *)new_flags,
.value_size = sizeof new_flags,
};
fx_hashmap_put(ctx->ctx_objects_flags, &key, &value);
fx_hashtable_put(ctx->ctx_objects_flags, &ptr, &FX_I32(new_flags));
}
static void ctx_clear_object_flags(
@@ -166,28 +156,18 @@ static void ctx_clear_object_flags(
return;
}
fx_hashmap_key key = {
.key_data = obj,
.key_size = sizeof(fx_object *),
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
};
const fx_hashmap_value *old_value
= fx_hashmap_get(ctx->ctx_objects_flags, &key);
fx_value ptr = FX_POINTER(obj);
const fx_value *old_value
= fx_hashtable_get(ctx->ctx_objects_flags, &ptr);
enum object_flags new_flags = 0;
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;
fx_hashmap_value value = {
.value_data = (void *)new_flags,
.value_size = sizeof new_flags,
};
fx_hashmap_put(ctx->ctx_objects_flags, &key, &value);
fx_hashtable_put(ctx->ctx_objects_flags, &ptr, &FX_I32(new_flags));
}
static enum object_flags ctx_get_object_flags(struct ctx *ctx, fx_object *obj)
@@ -196,16 +176,13 @@ static enum object_flags ctx_get_object_flags(struct ctx *ctx, fx_object *obj)
return 0;
}
fx_hashmap_key key = {
.key_data = obj,
.key_size = sizeof(fx_object *),
.key_flags = FX_HASHMAP_KEY_F_INTVALUE,
};
fx_value ptr = FX_POINTER(obj);
const fx_value *old_value
= fx_hashtable_get(ctx->ctx_objects_flags, &ptr);
const fx_hashmap_value *value
= fx_hashmap_get(ctx->ctx_objects_flags, &key);
if (value) {
return (enum object_flags)(uintptr_t)value->value_data;
enum object_flags new_flags = 0;
if (old_value) {
return (enum object_flags)old_value->v_i32;
}
return 0;
@@ -295,7 +272,7 @@ static fx_wchar advance_char(struct ctx *ctx)
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)) {
ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT);
@@ -328,7 +305,7 @@ static fx_wchar peek_char(struct ctx *ctx)
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)) {
ctx->ctx_result = FX_RESULT_ERR(BAD_FORMAT);
@@ -924,8 +901,11 @@ static void split_word(struct ctx *ctx, fx_string *wordbuf)
FX_STRING_TOK_F_INCLUDE_EMPTY_TOKENS);
size_t i = 0;
fx_foreach_c(const char *, tok, it)
fx_foreach(tokv, it)
{
const char *tok = NULL;
fx_value_get_cstr(tokv, &tok);
if (i > 0) {
enqueue_token(ctx, TOK_DOT);
}
@@ -1027,9 +1007,11 @@ static void read_word(struct ctx *ctx)
return;
}
fx_iterator *it = fx_iterator_begin(wordbuf);
fx_foreach(fx_wchar, c, it)
const fx_iterator *it = fx_iterator_begin(wordbuf);
fx_foreach(cv, it)
{
fx_wchar c = FX_WCHAR_INVALID;
fx_value_get_wchar(cv, &c);
/* only allow ASCII numbers/letters here */
bool ok = isalnum(c) || c == '_' || c == '-' || c == '.';
if (!ok) {
@@ -1473,14 +1455,24 @@ static fx_result advance_token(struct ctx *ctx)
start:
c = peek_char(ctx);
while (isspace(c) && c != '\n' && c != '\r') {
advance_char(ctx);
c = peek_char(ctx);
if (c <= 0) {
if (fx_error_get_status_code(ctx->ctx_result)
== FX_ERR_NO_DATA) {
ctx->ctx_flags |= CTX_EOF;
}
return ctx->ctx_result;
}
if (c == -1) {
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
@@ -1550,7 +1542,7 @@ static void ctx_cleanup(struct ctx *ctx)
}
if (ctx->ctx_objects_flags) {
fx_hashmap_unref(ctx->ctx_objects_flags);
fx_hashtable_unref(ctx->ctx_objects_flags);
ctx->ctx_objects_flags = NULL;
}
}
@@ -1562,14 +1554,14 @@ static fx_result ctx_init(struct ctx *ctx)
ctx->ctx_linebuf = 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;
}
static fx_result toml_serialise(
fx_serial_ctx *serial,
fx_object *src,
const fx_value *src,
fx_stream *dest,
enum fx_serial_flags flags)
{
@@ -1663,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_key_value_pair(struct ctx *ctx, fx_dict *container);
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_timestamp(struct ctx *ctx, fx_object **result)
static fx_result parse_timestamp(struct ctx *ctx, fx_value *result)
{
struct token *tok = peek_token(ctx);
fx_datetime *dt = tok->tok_value.time;
tok->tok_value.time = NULL;
*result = (dt);
*result = FX_VALUE_OBJECT(dt);
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);
fx_string *str = fx_string_duplicate(tok->tok_str);
@@ -1684,88 +1676,61 @@ static fx_result parse_string(struct ctx *ctx, fx_object **result)
return FX_RESULT_ERR(NO_MEMORY);
}
*result = (str);
*result = FX_VALUE_OBJECT(str);
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)
{
struct token *tok = peek_token(ctx);
fx_int *val = fx_int_create(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);
*result = FX_I64(tok->tok_value.i.v);
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)
{
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.v >= 0) {
fx_double_set_value_inf(val);
*result = FX_DOUBLE(INFINITY);
} else {
fx_double_set_value_inf_negative(val);
*result = FX_DOUBLE(-INFINITY);
}
} else if (tok->tok_value.f.nan) {
if (tok->tok_value.f.v >= 0) {
fx_double_set_value_nan(val);
*result = FX_DOUBLE(NAN);
} else {
fx_double_set_value_nan_negative(val);
*result = FX_DOUBLE(-NAN);
}
} else {
*result = FX_DOUBLE(tok->tok_value.f.v);
}
*result = (val);
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)
{
struct token *tok = peek_token(ctx);
fx_bool *val = fx_bool_create(tok->tok_value.b);
if (!val) {
return FX_RESULT_ERR(NO_MEMORY);
}
*result = (val);
*result = FX_BOOL(tok->tok_value.b);
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);
advance_token(ctx);
fx_dict *table = fx_dict_create();
fx_hashtable *table = fx_hashtable_create();
if (!table) {
return FX_RESULT_ERR(NO_MEMORY);
}
struct token *tok = peek_token(ctx);
if (tok && tok->tok_type == TOK_RIGHT_BRACE) {
*result = (table);
*result = FX_VALUE_OBJECT(table);
return FX_RESULT_SUCCESS;
}
@@ -1775,14 +1740,14 @@ static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
fx_object *value;
fx_result status = parse_key_value_pair(ctx, table);
if (!FX_OK(status)) {
fx_dict_unref(table);
fx_hashtable_unref(table);
return status;
}
tok = peek_token(ctx);
if (!tok) {
fx_dict_unref(table);
fx_hashtable_unref(table);
return status;
}
@@ -1794,12 +1759,12 @@ static fx_result parse_table_inline(struct ctx *ctx, fx_object **result)
advance_token(ctx);
break;
default:
fx_dict_unref(table);
fx_hashtable_unref(table);
return FX_RESULT_ERR(BAD_FORMAT);
}
}
*result = (table);
*result = FX_VALUE_OBJECT(table);
return FX_RESULT_SUCCESS;
}
@@ -1813,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;
ENABLE_EXTENDED_LEXING(ctx);
@@ -1850,14 +1815,15 @@ static fx_result parse_array_inline(struct ctx *ctx, fx_object **result)
break;
}
fx_object *value;
fx_value value;
fx_result status = parse_value(ctx, &value);
if (!FX_OK(status)) {
fx_array_unref(array);
return status;
}
fx_array_append(array, FX_RV(value));
fx_array_push_back(array, value);
fx_value_unset(&value);
ENABLE_EXTENDED_LEXING(ctx);
advance_token(ctx);
@@ -1880,11 +1846,11 @@ static fx_result parse_array_inline(struct ctx *ctx, fx_object **result)
}
DISABLE_EXTENDED_LEXING(ctx);
*result = (array);
*result = FX_VALUE_OBJECT(array);
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);
@@ -1912,7 +1878,7 @@ static fx_result parse_value(struct ctx *ctx, fx_object **result)
}
}
static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
static fx_result parse_key_value_pair(struct ctx *ctx, fx_hashtable *container)
{
struct token *tok = peek_token(ctx);
if (!IS_VALID_KEY_COMPONENT(tok)) {
@@ -1931,36 +1897,42 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
}
while (tok && tok->tok_type == TOK_DOT) {
fx_object *sub_dict = fx_dict_at_sk(container, key);
if (!sub_dict) {
sub_dict = (fx_dict_create());
fx_dict_put_sk(container, key, FX_RV(sub_dict));
} else if (
sub_dict
&& !fx_object_is_type(sub_dict, FX_TYPE_DICT)) {
free(key);
const fx_value *child
= fx_hashtable_get(container, &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_value_get_object(child, &subtable);
if (!subtable) {
subtable = (fx_hashtable_create());
fx_hashtable_put(
container,
&FX_VALUE_OBJECT(key),
&FX_VALUE_OBJECT(subtable));
}
#if 1
enum object_flags flags = ctx_get_object_flags(ctx, sub_dict);
enum object_flags flags = ctx_get_object_flags(ctx, subtable);
if (flags
& (OBJECT_KV_END_DEFINED | OBJECT_HEADER_END_DEFINED)) {
free(key);
fx_string_unref(key);
return FX_RESULT_ERR(BAD_FORMAT);
}
#endif
ctx_set_object_flags(ctx, sub_dict, OBJECT_KV_MID_DEFINED);
ctx_set_object_flags(ctx, subtable, OBJECT_KV_MID_DEFINED);
advance_token(ctx);
tok = peek_token(ctx);
if (!IS_VALID_KEY_COMPONENT(tok)) {
free(key);
fx_string_unref(key);
return FX_RESULT_ERR(BAD_FORMAT);
}
container = sub_dict;
container = subtable;
fx_string_unref(key);
key = fx_string_duplicate(tok->tok_str);
if (!key) {
@@ -1971,7 +1943,7 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
tok = peek_token(ctx);
}
if (fx_dict_has_skey(container, key)) {
if (fx_hashtable_get(container, &FX_VALUE_OBJECT(key))) {
return FX_RESULT_ERR(BAD_FORMAT);
}
@@ -1986,7 +1958,7 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
ENABLE_EXTENDED_LEXING(ctx);
advance_token(ctx);
fx_object *value = NULL;
fx_value value = FX_VALUE_EMPTY;
fx_result result = parse_value(ctx, &value);
DISABLE_EXTENDED_LEXING(ctx);
@@ -2001,20 +1973,24 @@ static fx_result parse_key_value_pair(struct ctx *ctx, fx_dict *container)
return fx_result_propagate(result);
}
fx_dict_put_sk(container, key, FX_RV(value));
fx_hashtable_put(container, &FX_VALUE_OBJECT(key), &value);
if (fx_object_is_type(value, FX_TYPE_DICT)
|| fx_object_is_type(value, FX_TYPE_ARRAY)) {
ctx_set_object_flags(ctx, value, OBJECT_KV_END_DEFINED);
if (fx_value_is_type(&value, FX_TYPE_HASHTABLE)
|| fx_value_is_type(&value, FX_TYPE_ARRAY)) {
ctx_set_object_flags(
ctx,
value.v_object,
OBJECT_KV_END_DEFINED);
}
fx_value_unset(&value);
return FX_RESULT_SUCCESS;
}
static fx_result parse_table_header(
struct ctx *ctx,
fx_dict *container,
fx_dict **new_container)
fx_hashtable *container,
fx_hashtable **new_container)
{
advance_token(ctx);
struct token *tok = peek_token(ctx);
@@ -2034,17 +2010,24 @@ static fx_result parse_table_header(
}
while (tok && tok->tok_type == TOK_DOT) {
fx_object *sub_dict = fx_dict_at_sk(container, key);
enum object_flags flags = ctx_get_object_flags(ctx, sub_dict);
if (!sub_dict) {
sub_dict = (fx_dict_create());
fx_dict_put_sk(container, key, FX_RV(sub_dict));
} else if (fx_object_is_type(sub_dict, FX_TYPE_ARRAY)) {
sub_dict = fx_array_at(
sub_dict,
fx_array_size(sub_dict) - 1);
} else if (!fx_object_is_type(sub_dict, FX_TYPE_DICT)) {
const fx_value *value
= fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
fx_object *subtable = NULL;
fx_value_get_object(value, &subtable);
enum object_flags flags = ctx_get_object_flags(ctx, subtable);
if (!value) {
subtable = (fx_hashtable_create());
fx_hashtable_put(
container,
&FX_VALUE_OBJECT(key),
&FX_VALUE_OBJECT(subtable));
fx_object_unref(subtable);
} else if (fx_value_is_type(value, FX_TYPE_ARRAY)) {
value = fx_array_get_ref(
subtable,
fx_array_get_size(subtable) - 1);
fx_value_get_object(value, &subtable);
} else if (!fx_value_is_type(value, FX_TYPE_HASHTABLE)) {
return FX_RESULT_ERR(BAD_FORMAT);
}
@@ -2058,9 +2041,9 @@ static fx_result parse_table_header(
return FX_RESULT_ERR(BAD_FORMAT);
}
ctx_set_object_flags(ctx, sub_dict, OBJECT_HEADER_MID_DEFINED);
ctx_set_object_flags(ctx, subtable, OBJECT_HEADER_MID_DEFINED);
container = sub_dict;
container = subtable;
fx_string_unref(key);
key = fx_string_duplicate(tok->tok_str);
if (!key) {
@@ -2075,20 +2058,31 @@ static fx_result parse_table_header(
return FX_RESULT_ERR(BAD_FORMAT);
}
fx_dict *new_table = fx_dict_at_sk(container, key);
const fx_value *child
= fx_hashtable_get(container, &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_value_get_object(child, &new_table);
if (!new_table) {
new_table = fx_dict_create();
new_table = fx_hashtable_create();
if (!new_table) {
free(key);
fx_string_unref(key);
return FX_RESULT_ERR(NO_MEMORY);
}
fx_dict_put_sk(container, key, FX_RV(new_table));
fx_hashtable_put(
container,
&FX_VALUE_OBJECT(key),
&FX_VALUE_OBJECT(new_table));
fx_hashtable_unref(new_table);
}
if (!fx_object_is_type((new_table), FX_TYPE_DICT)) {
if (!fx_object_is_type((new_table), FX_TYPE_HASHTABLE)) {
return FX_RESULT_ERR(BAD_FORMAT);
}
@@ -2109,8 +2103,8 @@ static fx_result parse_table_header(
static fx_result parse_array_header(
struct ctx *ctx,
fx_dict *container,
fx_dict **new_container)
fx_hashtable *container,
fx_hashtable **new_container)
{
advance_token(ctx);
struct token *tok = peek_token(ctx);
@@ -2130,15 +2124,29 @@ static fx_result parse_array_header(
}
while (tok && tok->tok_type == TOK_DOT) {
fx_object *sub_dict = fx_dict_at_sk(container, key);
if (!sub_dict) {
sub_dict = (fx_dict_create());
fx_dict_put_sk(container, key, FX_RV(sub_dict));
} else if (fx_object_is_type(sub_dict, FX_TYPE_ARRAY)) {
sub_dict = fx_array_at(
const fx_value *child
= fx_hashtable_get(container, &FX_VALUE_OBJECT(key));
fx_object *sub_dict = NULL;
fx_value_get_object(child, &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());
fx_hashtable_put(
container,
&FX_VALUE_OBJECT(key),
&FX_VALUE_OBJECT(sub_dict));
fx_hashtable_unref(sub_dict);
} else if (fx_value_is_type(child, FX_TYPE_ARRAY)) {
child = fx_array_get_ref(
sub_dict,
fx_array_size(sub_dict) - 1);
} else if (!fx_object_is_type(sub_dict, FX_TYPE_DICT)) {
fx_array_get_size(sub_dict) - 1);
sub_dict = child->v_object;
} else if (!fx_value_is_type(child, FX_TYPE_HASHTABLE)) {
return FX_RESULT_ERR(BAD_FORMAT);
}
@@ -2163,37 +2171,48 @@ static fx_result parse_array_header(
return FX_RESULT_ERR(BAD_FORMAT);
}
fx_array *array = fx_dict_get_sk(container, key);
const fx_value *child
= fx_hashtable_get(container, &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_value_get_object(child, &array);
if (!array) {
array = fx_array_create();
fx_dict_put_sk(container, key, FX_RV(array));
fx_hashtable_put(
container,
&FX_VALUE_OBJECT(key),
&FX_VALUE_OBJECT(array));
} else if (!fx_object_is_type(array, FX_TYPE_ARRAY)) {
return FX_RESULT_ERR(BAD_FORMAT);
}
free(key);
fx_string_unref(key);
enum object_flags flags = ctx_get_object_flags(ctx, (array));
if (flags & OBJECT_KV_END_DEFINED) {
return FX_RESULT_ERR(NO_MEMORY);
return FX_RESULT_ERR(BAD_FORMAT);
}
fx_dict *new_table = fx_dict_create();
fx_hashtable *new_table = fx_hashtable_create();
if (!new_table) {
return FX_RESULT_ERR(NO_MEMORY);
}
fx_array_append(array, FX_RV(new_table));
fx_array_push_back(array, FX_VALUE_OBJECT(new_table));
advance_token(ctx);
*new_container = new_table;
return FX_RESULT_SUCCESS;
}
static fx_result parse_root(struct ctx *ctx, fx_dict **out)
static fx_result parse_root(struct ctx *ctx, fx_hashtable **out)
{
fx_result result = FX_RESULT_SUCCESS;
fx_dict *root = fx_dict_create();
fx_dict *current = root;
fx_hashtable *root = fx_hashtable_create();
fx_hashtable *current = root;
while (!(ctx->ctx_flags & CTX_EOF) && fx_result_is_success(result)) {
struct token *tok = peek_token(ctx);
@@ -2253,7 +2272,7 @@ static fx_result parse_root(struct ctx *ctx, fx_dict **out)
}
if (fx_result_is_error(result)) {
fx_dict_unref(root);
fx_hashtable_unref(root);
root = NULL;
}
@@ -2264,7 +2283,7 @@ static fx_result parse_root(struct ctx *ctx, fx_dict **out)
static fx_result toml_deserialise(
fx_serial_ctx *serial,
fx_stream *src,
fx_object **dest,
fx_value *dest,
enum fx_serial_flags flags)
{
struct ctx ctx = {0};
@@ -2285,17 +2304,17 @@ static fx_result toml_deserialise(
}
if (ctx.ctx_flags & CTX_EOF) {
*dest = (fx_dict_create());
*dest = FX_VALUE_OBJECT(fx_hashtable_create());
return FX_RESULT_SUCCESS;
}
fx_dict *data = NULL;
fx_hashtable *data = NULL;
ctx.ctx_result = parse_root(&ctx, &data);
if (fx_result_is_error(ctx.ctx_result)) {
return fx_result_propagate(ctx.ctx_result);
}
*dest = (data);
*dest = FX_VALUE_OBJECT(data);
#if 0
ctx.ctx_flags
= CTX_ENABLE_NUMBERS | CTX_ENABLE_TIMESTAMPS | CTX_ENABLE_BOOLS;
@@ -2322,16 +2341,16 @@ static void toml_serial_ctx_fini(fx_object *obj, void *priv)
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_toml_serial_ctx)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_BEGIN(fx_toml_serial_ctx)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_serial_ctx, FX_TYPE_SERIAL_CTX)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_serial_ctx, FX_TYPE_SERIAL_CTX)
FX_INTERFACE_ENTRY(s_serialise) = toml_serialise;
FX_INTERFACE_ENTRY(s_deserialise) = toml_deserialise;
FX_TYPE_CLASS_INTERFACE_END(fx_serial_ctx, FX_TYPE_SERIAL_CTX)
FX_TYPE_CLASS_DEFINITION_END(fx_toml_serial_ctx)
FX_TYPE_VTABLE_INTERFACE_END(fx_serial_ctx, FX_TYPE_SERIAL_CTX)
FX_TYPE_CLASS_END(fx_toml_serial_ctx)
FX_TYPE_DEFINITION_BEGIN(fx_toml_serial_ctx)
FX_TYPE_ID(0xaec8dca0, 0x131a, 0x4217, 0x916b, 0xaed15756601c);

Some files were not shown because too many files have changed in this diff Show More