【问题标题】:Calling a script from another one从另一个调用脚本
【发布时间】:2012-03-06 06:43:49
【问题描述】:

假设我将这个函数保存在某个脚本中:

function Add(a,b:integer):integer;  
  begin
    result:=a+b;
  end;

我还有另一个脚本如下:

var
    a,b,c:integer;

  a:=1; 
  b:=2;
  c:=Add(a,b);

  println(inttoStr(c));

如何在 Delphi 中使用 dwscript 编译这两个脚本并从第二个脚本调用第一个脚本?

【问题讨论】:

    标签: delphi dwscript


    【解决方案1】:

    假设一个文件名为“file1.extension”,其内容为:

    function Add(a,b:integer):integer;  
      begin
        result:=a+b;
      end;
    

    还有另一个名为“main.extension”的文件,内容为:

    var
        a,b,c:integer;
    
      a:=1; 
      b:=2;
      c:=Add(a,b);
    
      println(inttoStr(c));
    

    您需要在“main.extension”文件的开头添加以下行:

    // note that file name is case sensitive
    // file1.extension <> FILE1.EXTENSION
    // include_once is to solve cycle-includes
    // i.e. file1.extension includes main.extension and vice-versa
    {$include_once 'file1.extension'} 
    // or include if file1.extension does not require functions/objects/variables/etc.
    // from main.extension
    {$include 'file1.extension'}
    

    我建议使用 {$include_once ...} 而不是 {$include ...}。

    【讨论】:

    • 我试过了,但是没有用。显示编译器错误:“在输入路径上找不到文件”,有什么建议吗??
    • 使用调试器跟踪代码并查看它正在使用的输入路径列表。然后,要么更改该列表,要么移动您的文件。
    • @Zeina 您还必须添加到脚本路径“TDelphiWebScript.Config.ScriptPaths”
    • 谢谢,收到通知后一切正常!!
    • @Zeina 太棒了,现在你可能想从 SVN 获取最新的源代码,以获取所有最新的好东西! (: 但请记住,有些是实验性的
    【解决方案2】:

    除了使用 Dorin 的答案中列出的 $include / $include_once 之外,如果您使用的是 SVN (2.3) 版本,您还可以使用带有“uses”语句的更传统的单位。

    单元可以是具有接口/实现的“经典”单元,也可以是遵循扩展脚本语法的混合单元(只需省略“接口”关键字并开始声明/实现)。

    将源传递给编译器的最简单方法是使用事件(OnInclude 用于包含源,OnNeedUnit 用于单元源),但您也可以通过指定 CompileFileSystem 来传递它们。

    【讨论】:

    • 与 OnInclude 或 OnNeedUnit 函数一起使用的第二个参数 'UnitSource' 是什么? (应该发送什么?)
    • 由你来放置源。在 OnNeedUnit 的情况下,你可以指定一个脚本源以这种方式编译,或者使用返回值传递一个 IdwsUnit(当被引用的单元不是脚本,而是在 Delphi 方面实现的东西)
    • 不,我不想在我的项目中提供源代码,我宁愿使用 Dorin 提到的 $Include,但它不起作用。 “在输入路径上找不到文件”这是由此产生的编译器错误。我错过了什么吗?提前致谢。
    猜你喜欢
    • 2011-06-01
    • 2022-08-02
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多