How to add modules to ChaiScript in version 6?


#1

I used to do the following:

  • Load the chaiscript::Std_Lib::library() once and store it in a chaiscript::ModulePtr
  • generate a second module with basic functions and data types and store it in a second chaiscript::ModulePtr
  • Instantiate a chaiscript::ChaiScript object and pass it the std_lib module ptr
  • call chaiscript::ChaiScript::add to add my second module

This now results in an error in version 6, because the constructor and the signature of the add function have changed. How can I fix my code?


#2

I may be misunderstanding the issue, but it seems you would want to do something like this:

The signature for add should not have changed enough to affect your ability to use a separately instantiated ModulePtr and you should be able to use the create_chaiscript_stdlib and create_chaiscript_parser code to keep the separate compilation you are using.

If this doesn’t make sense, please try to provide a simple complete example with your issue.


#3

Thanks for your response!
ATextureManager is the class that will execute the ChaiScripts to generate textures:

class ATextureManager::TImpl
{
public:
  TImpl();

  ...

private:
    chaiscript::ModulePtr mpLibrary;
    chaiscript::ChaiScript mMyChai;
}

ATextureManager::TImpl::TImpl()
: mpLibrary(new chaiscript::Module())
, mMyChai(NChaiScript::SStdLibrary())    // <-- Error 1
{
  mpLibrary->add(NChaiScript::SBaseModule());     // <-- Error 2
  mpLibrary->add(chaiscript::fun(&ThisClass::HasOption, this), "HasOption");
  ...
  mMyChai.add(mpLibrary);
  LoadTextureScripts();
}

In NChaiScript I store precompiled modules:

chaiscript::ModulePtr NChaiScript::SStdLibrary()
{
  static chaiscript::ModulePtr sStdModulePtr = chaiscript::Std_Lib::library();
  return sStdModulePtr;
}

chaiscript::ModulePtr NChaiScript::SBaseModule()
{
  // build base module with a bunch of types for points, rects, colors, etc:
  static chaiscript::ModulePtr sBaseModulePtr = NChaiScriptLocal::SGenerateBaseModule();
  return sBaseModulePtr;
}

Compiling with ChaiScript v6.0.0 I get:

Src/TextureManager.cpp:369:3: No matching constructor for initialization of 'chaiscript::ChaiScript'
ChaiScript/include/chaiscript/chaiscript.hpp:830:9: Candidate constructor (the implicit copy constructor) not viable: no known conversion from 'chaiscript::ModulePtr' (aka 'shared_ptr<chaiscript::Module>') to 'const chaiscript::ChaiScript' for 1st argument
ChaiScript/include/chaiscript/chaiscript.hpp:833:7: Candidate constructor not viable: no known conversion from 'chaiscript::ModulePtr' (aka 'shared_ptr<chaiscript::Module>') to 'std::vector<std::string>' (aka 'vector<basic_string<char, char_traits<char>, allocator<char> > >') for 1st argument

and:

Src/TextureManager.cpp:371:14: No matching member function for call to 'add'
ChaiScript/include/chaiscript/dispatchkit/dispatchkit.hpp:158:15: Candidate function not viable: no known conversion from 'shared_ptr<chaiscript::Module>' to 'shared_ptr<chaiscript::detail::Type_Conversion_Base>' for 1st argument
ChaiScript/include/chaiscript/dispatchkit/dispatchkit.hpp:152:15: Candidate function not viable: requires 2 arguments, but 1 was provided
ChaiScript/include/chaiscript/dispatchkit/dispatchkit.hpp:164:15: Candidate function not viable: requires 2 arguments, but 1 was provided

#4

Ok, for what you are doing, You’ll need to use the new ChaiScript_Basic interface, which gives you more flexibility.

ChaiScript_Basic chai(
  NChaiScript::SStdLibrary(), 
  std::make_unique<parser::ChaiScript_Parser<eval::Noop_Tracer, optimizer::Optimizer_Default>>(),
);

(this might be helpful: https://github.com/ChaiScript/ChaiScript/blob/develop/include/chaiscript/chaiscript.hpp#L830-L842)

Then for your error #2, Modules no longer reference each other by pointer, that was determined to be unnecessary.

So, you could change your SBaseModule() to return a value, or you could dereference the shared_ptr when you call add:

mpLibrary->add(NChaiScript::SBaseModule());     // <-- Error 2
// becomes
mpLibrary->add(*NChaiScript::SBaseModule()); 

We’ve tried in general to reduce unnecessary shared_ptrs in ChaiScript. This is the one case that affects the user.

-Jason