1

我目前正在使用 ChaiScript 6.0.0 版和 Visual Studio 2017。

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

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 对象时提供“使用路径”会导致相同的情况。

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

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

我正在编译禁用线程安全的 ChaiScript。

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

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

我是否错误地使用了“使用”功能?

4

1 回答 1

2

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

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

我这样做:

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

于 2017-06-28T13:33:32.667 回答