asm.js - How should function pointers be implemented

Viewed 850

Note: This question is purely about asm.js not about C++ nor any other programming language.

As the title already says:

How should a function pointer be implemented in a efficient way?

I couldn't find anything on the web, so I figured asking it here.

Edit: I would like to implement virtual functions in the compiler I'm working on.

In C++ I would do something like this to generate a vtable:

#include <iostream>

class Base {
  public:
    virtual void doSomething() = 0;
};

class Derived : public Base {
  public:
    void doSomething() {
        std::cout << "I'm doing something..." << std::endl;
    }
};

int main()
{
    Base* instance = new Derived();
    instance->doSomething();
    return 0;
}

To be more precise; how can I generate a vtable in asm.js without the need of plain JavaScript? In any case, I would like the "near native" capabilities of asm.js while using function pointers.

The solution may be suitable for computer generated code only.

3 Answers
Related