Implement function descriptors. #4
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Now for the magic. We're going to use template metaprogramming to obtain the properties of a function at compile time. Once we have that information, we can use it to do two different things.
First, we can use template metaprogramming to automatically generate all the boilerplate code for each function (checking if there are enough arguments, checking the type and obtaining the value of each argumetn, etc). The result will be a wrapper function with a signature that the interpreter can consume, regardless of the signature of the function being wrapped.
Second, we can use template metaprogramming to automatically generate an object, called a descriptor, that returns the run-time type of a function. More specifically, it uses the
Typeclass we defined earlier to describe the run-time type of the result and parameters of the function. Later on, we will use this information to print a function table that can be consumed by the JavaScript side of the interpreter to automatically generate wrappers on that side, too.Between wrappers and descriptors, descriptors are the easiest to understand, so we'll begin there. I'm using several C++11 features here that you may not be familiar with. If that's the case, it might be beneficial for you to read up on them. In particular, I'm using parameter packs to define template classes with zero or more template arguments:
http://en.cppreference.com/w/cpp/language/parameter_pack