Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Way to call method on Chaiscript object from C++ #603

Open
mallardtheduck opened this issue Oct 7, 2022 · 1 comment
Open

Way to call method on Chaiscript object from C++ #603

mallardtheduck opened this issue Oct 7, 2022 · 1 comment

Comments

@mallardtheduck
Copy link

Hi, I'm looking for some way to call a method on a Chaiscript object from C++, but I can't see anything obvious.

For example:
(Chaiscript)

class Foo{
   def Foo() {}
   def Bar(x) { return x + 5; }
};

(C++)

auto foo = chai.eval("Foo()");
//somehow retrieve 'Foo::Bar' as std::function<int(int)> or similar
std::cout << bar(3) << std::endl; // prints "8"

I can get the object as a chaiscript::dispatch::Dynamic_Object, but there seems to be no way to see methods on the object. get_attrs just gives me member variables, not the methods.

@mallardtheduck
Copy link
Author

As a workaround, I've created Chaiscript wrapper functions outside of an object that can be called from C++.

i.e. for the example above:

(Chaiscript)

def Call_Bar(o, x){
   return o.Bar(x);
}

(C++)

auto foo = chai.eval<chaiscript::dispatch::Dynamic_Object>("Foo()");
auto caller = chai.eval<std::function<int(chaiscript::dispatch::Dynamic_Object, int)>>("Call_Bar");
std::cout << caller(foo, 3) << std::endl;

It would still be much nicer if there were a proper way to do this...

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

No branches or pull requests

1 participant