Search code examples
chaiscript

Error: "Can not find object: use"


I am currently using ChaiScript version 6.0.0 and Visual Studio 2017.

In my C++, I am retrieving a reference to a function on_init() from a script file, and executing it. The ChaiScript object was constructed with the default/empty constructor. The function looks like this:

def on_init() {
  use("scripts/test.chai");
}

The contents of "scripts/test.chai" looks like this:

class A {
  def A() {
    print("Created an instance of A!");
  }
}

My file structure looks like this:

bin
   \
   | my_executable.exe
   | scripts
           \
           | main_menu.chai
           | test.chai

When executing the on_init() function shown above, the console prints the following message:

Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use" Error: "Can not find object: use"

Providing "usepaths" when constructing the ChaiScript object results in the same situation.

I have tried use("test.chai") as well as use("scripts/test.chai"), and both result in the same messages.

I am not providing any chaiscript::Options enum on construction of the ChaiScript object, so it should be using the default (which appears to contain External_Scripts as well as Load_Modules).

I am compiling ChaiScript with thread-safety disabled.

I am not having any issues running any of the other built-in functions in this script's body, including in other functions that I am retrieving (into C++) in the same manner as this one.

If there is any more information that is needed, let me know.

Am I using the "use" function incorrectly?


Solution

  • EDIT: Must've been really really high when I wrote the stuff underneath, but I'll leave it there just because I'm astounded I could even come up with that.

    The true answer is that you must explicitly pass chaiscript::Options::External_Scripts to the ChaiScript constructor in order to enable the file loading functions.

    I do it like so:

    class ChaiPlugin
    {
    public:
        /* stuff */
    private:
        chaiscript::ChaiScript chai = { {}, {}, { chaiscript::Options::External_Scripts } };
    };
    

    use is only used for debugging purposes.

    from within unittests/boxed_cast_test.cpp:

    template<typename T>
    void use(T){}
    

    I believe what you're looking for is usefile("scripts/test.chai").