dukglue icon indicating copy to clipboard operation
dukglue copied to clipboard

Added call to register 'intercepted' method

Open desertkun opened this issue 8 years ago • 0 comments

Often you already have some code base you would like to introduce into Javascript world. But the API you have is just not ready to be used in dukglue_register_method:

  • It's not recommended to edit the existing code base
  • Some methods return 'by value'
  • Magic enums are used javascript have no idea about
  • Arguments are const references

You can try to wrap your object into a wrapper that would add that dukglue-ready methods, but sometimes even that is just not possible.

Say you have this class:

class A {
public:
    B process(const C& c, Vector2 v);
};

Then using simple wrapper that does not touch the original class:

namespace AWrapper {
    B* process(A* instance, duk_context* context, C* c, Vector2* v) {
        static B b;
        b = instance->process(*c, *v);
        return &b;
    }
};

You can bind it:

dukglue_register_intercepted_method<A>(ctx, "process", &AWrapper::process);

The bound should have two additional arguments added before the args that javascript may pass:

  1. A pointer to current class instance
  2. A pointer to current context

Under the hood it behaves like dukglue_register_method and dukglue_register_function combined, since both obj_ptr and func_ptr are used (honestly have no idea how it works there are way to many template magic up there).

I would appreciate a comment on how to make this PR possible.

desertkun avatar Oct 04 '17 22:10 desertkun