Chaiscript doesn't throw if a method is not found


#21

mmm How can I do it from C++? can I add a method_missing for a specific class type from c++?

module->add(chaiscript::fun([](const vegeta &object, const std::string &funName, const std::vector< chaiscript::Boxed_Value> &params ){ std::cout<<“Function not found: “<< funName <<”\n”; }), “method_missing”);

something like this will work? (I’m at work now I’ll try this when I can).


#22

You’d need to do something like (untested):

module->add(chaiscript::fun(
  [](const chaiscript::dispatch::Dynamic_Object &object, const std::string &funName, const std::vector< chaiscript::Boxed_Value> &params ){ 
    if (object.get_type_name() != "vegeta") throw exception::guard_error();
    
    std::cout<<"Function not found: "<< funName <<"\n"; 
  }), "method_missing");
 

Hopefully it’s obvious at this point that you’re going outside of what is normally supported.

The real question needs to be: what exactly do you want to happen? Is there some extension to ChaiScript that could make your code a lot more simple?

Is the main point to detect if a method has been defined?

-Jason


#23

What I want is to print an error if a variable or function that doesn’t exists is called.

I removed my method_missing function and added what you suggested :slight_smile:

        std::string creator_statement = (R"(fun() 
                   { 
                     var new_instance = )") + className + (R"(();
                     new_instance.set_explicit(true);
                     return new_instance; 
                   })");

and now it works as expected it throws an eval_error exceptions when a function or variable doesnt exists too. I missed that eval_error exception :frowning: now i catch this to print the error.

Sorry and thanks.