1

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?

  • It is behaving as though `use` is completely missing from the executable. That or, `test.chai` is some doing something that is causing a recursion. Both odd. But I'd need a more complete example to diagnose. `use` is used for *all* unit tests, so it is known that it does work. – lefticus May 30 '17 at 13:26

1 Answers1

2

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").