lhmouse/asteria

Simplify `Simple_Binding_Wrapper`

Closed this issue · 2 comments

At the moment we have this in Simple_Binding_Wrapper:

using Prototype = Reference (const Value& opaque,
                             const Global_Context& global,
                             Reference&& self,
                             cow_vector<Reference>&& args);

The parameter list is horrible and not Simple at all:

  1. Seldom do any bindings need the opaque parameter. In reality, no standard library function have ever used it so far.
  2. The global parameter is used only by bindings that wish to call user-provided functions.
  3. The self reference is used only by member functions.
  4. There are a few bindings that take no argument.

Basing on this fact, the parameter list should be simplified in such a way that those less likely to be useful come more behind.

I propose a prototype as follows:

using Prototype = Reference (cow_vector<Reference>&& args,
                             Reference&& self,
                             const Global_Context& global,
                             const Value& opaque);

As all parameters are references, it is possible to cast a user-defined function from Reference (*)(cow_vector<Reference>&& args) to Prototype* then call it. According to the C++ standard this is undefined behavior, but a lot of POSIX functions have been doing this (calling functions taking fewer parameters than arguments) for decades, and Itanium ABI already states that references are passed as if they were pointers, so I think it is safe.

Stop doing things stupid by assuming "safe".

You are being brilliant for making things hard to use aren't you?