Skip to content

Commit

Permalink
Cleanup and docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
sean-parent committed Jan 8, 2025
1 parent 2a95f2b commit 7c0b776
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
16 changes: 14 additions & 2 deletions adobe/virtual_machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ namespace adobe {

/**************************************************************************************************/

// todo: temporary hack to get around std::function not being comparable
/// Any function of the form `any_regular_t(const any_regular_t&)`. All function_t objects compare
/// equal so they can be placed in an `any_regular_t` object.
class function_t : public std::function<any_regular_t(const any_regular_t&)> {
public:
using base_t = std::function<any_regular_t(const any_regular_t&)>;
Expand All @@ -58,10 +59,12 @@ class function_t : public std::function<any_regular_t(const any_regular_t&)> {

/**************************************************************************************************/

/// Returns a simple name for the core types used by the virtual machine.
const char* type_name(const std::type_info& type);

/**************************************************************************************************/

/// Does a runtime cast on an any_regular_t with better error reporting.
template <class T>
auto cast(adobe::any_regular_t& x) -> decltype(x.cast<T>()) {
if constexpr (!std::is_same_v<T, adobe::any_regular_t>) {
Expand All @@ -72,6 +75,7 @@ auto cast(adobe::any_regular_t& x) -> decltype(x.cast<T>()) {
return x.cast<T>();
}

/// Does a runtime cast on an any_regular_t with better error reporting.
template <class T>
auto cast(const adobe::any_regular_t& x) -> decltype(x.cast<T>()) {
if constexpr (!std::is_same_v<T, adobe::any_regular_t>) {
Expand All @@ -84,6 +88,12 @@ auto cast(const adobe::any_regular_t& x) -> decltype(x.cast<T>()) {

/**************************************************************************************************/

namespace detail {

/// Provide an invoke function for an invocable F with a signature Sig, that will extract the
/// arguments from an array_t. An exception will be thrown if the number of items in the array
/// does not match the number of arguments in the signature, or if any of the argument types do
/// not match the corresponding array_t item type.
template <class Sig>
struct function_packager;

Expand All @@ -103,13 +113,15 @@ struct function_packager<R(Args...)> {
}
};

} // namespace detail

/**************************************************************************************************/

/// Create a function_t from an invocable object.
template <class Sig, class F>
any_regular_t make_function(F&& f) {
return function_t{[f = std::forward<F>(f)](const any_regular_t& args) {
return function_packager<Sig>::invoke(f, args.cast<array_t>());
return detail::function_packager<Sig>::invoke(f, args.cast<array_t>());
}};
}

Expand Down
25 changes: 0 additions & 25 deletions source/virtual_machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,20 +399,12 @@ class virtual_machine_t::implementation_t {

static operator_table_t* operator_table_g;
static variable_table_t* variable_table_g;
#if 0
static array_function_table_t* array_function_table_g;
static dictionary_function_table_t* dictionary_function_table_g;
#endif
};

/**************************************************************************************************/

operator_table_t* virtual_machine_t::implementation_t::operator_table_g;
variable_table_t* virtual_machine_t::implementation_t::variable_table_g;
#if 0
array_function_table_t* virtual_machine_t::implementation_t::array_function_table_g;
dictionary_function_table_t* virtual_machine_t::implementation_t::dictionary_function_table_g;
#endif

/**************************************************************************************************/

Expand Down Expand Up @@ -832,23 +824,6 @@ void virtual_machine_t::push_scope(variable_scope_t&& scope) {

/**************************************************************************************************/

#if 0
void virtual_machine_t::set_array_function_lookup(const array_function_lookup_t& function) {
object_m->array_function_lookup_m = function;
}
#endif

/**************************************************************************************************/

#if 0
void virtual_machine_t::set_dictionary_function_lookup(
const dictionary_function_lookup_t& function) {
object_m->dictionary_function_lookup_m = function;
}
#endif

/**************************************************************************************************/

void virtual_machine_t::set_named_index_lookup(const named_index_lookup_t& function) {
object_m->named_index_lookup_m = function;
}
Expand Down

0 comments on commit 7c0b776

Please sign in to comment.