Skip to content

Added call to register 'intercepted' method#11

Open
desertkun wants to merge 1 commit intoAloshi:masterfrom
Salleodore:dukglue_register_intercepted_method
Open

Added call to register 'intercepted' method#11
desertkun wants to merge 1 commit intoAloshi:masterfrom
Salleodore:dukglue_register_intercepted_method

Conversation

@desertkun
Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant