Error: "Can not find object: use"

Error: "Can not find object: use"

我目前使用的是 ChaiScript 版本 6.0.0 和 Visual Studio 2017。

在我的 C++ 中,我正在从脚本文件中检索对函数 on_init() 的引用,并执行它。 ChaiScript 对象是使用 default/empty 构造函数构造的。该函数如下所示:

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

"scripts/test.chai" 的内容如下所示:

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

我的文件结构如下所示:

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

执行上面显示的on_init()函数时,控制台打印如下信息:

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"

在构造 ChaiScript 对象时提供 "usepaths" 会导致相同的情况。

我试过 use("test.chai")use("scripts/test.chai"),两者都产生相同的消息。

我没有提供任何关于构造 ChaiScript 对象的 chaiscript::Options 枚举,因此它应该使用默认值(它似乎包含 External_Scripts 以及 Load_Modules)。

我在禁用线程安全的情况下编译 ChaiScript。

我没有遇到任何问题 运行 此脚本正文中的任何其他内置函数,包括我以与此相同的方式检索(进入 C++)的其他函数。

如果需要更多信息,请告诉我。

我是不是用错了"use"功能?

编辑: 当我在下面写下这些东西时一定真的非常高,但我会把它留在那里只是因为我很惊讶我什至可以想出那。

真正的答案是,您必须显式地将 chaiscript::Options::External_Scripts 传递给 ChaiScript 构造函数才能启用文件加载功能。

我是这样做的:

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

use用于 调试目的。

来自 unittests/boxed_cast_test.cpp:

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

我相信你要找的是usefile("scripts/test.chai")