4

Suppose I have this function saved in a certain script:

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

and I have another script which is the following:

var
    a,b,c:integer;

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

  println(inttoStr(c));

how can i compile both scripts and call the 1st script from the second using dwscript in Delphi?

Zeina
  • 1,573
  • 2
  • 24
  • 34

2 Answers2

4

assuming that one file is called "file1.extension" and it's content is:

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

and another file called "main.extension" with content:

var
    a,b,c:integer;

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

  println(inttoStr(c));

you need to add the following line at the beginning of "main.extension" file:

// 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'}

I suggest using {$include_once ...} rather than {$include ...}.

  • I tried it, but it didn't work. A compiler error is shown saying: **"Couldn't find file on input paths"**, any suggestions?? – Zeina Mar 06 '12 at 07:49
  • Use the debugger to trace through the code and see what list of input paths it's using. Then, either alter that list, or move your file. – Rob Kennedy Mar 06 '12 at 08:05
  • 1
    @Zeina You also have to add to the script's paths "TDelphiWebScript.Config.ScriptPaths" –  Mar 06 '12 at 08:48
  • Thanks, everything went fine after this notice!! – Zeina Mar 06 '12 at 09:11
  • @Zeina awesome, now you may want to get the latest source from SVN, to get all the latest goodies! (: but remember that some are experimental –  Mar 06 '12 at 09:16
3

In addition to using the $include / $include_once as listed in Dorin's answer, you can also use more traditional units with the "uses" statement if you're in the SVN (2.3) version.

Units can be either "classic" units with an interface/implementation, or mixed units that follow the extended scripting syntax (just omit the "interface" keyword and start declaring/implementing away).

The simplest way to pass the sources to the compiler is to use the events (OnInclude for includes source, OnNeedUnit for units source), but you can also pass them by specifying a CompileFileSystem.

Eric Grange
  • 5,931
  • 1
  • 39
  • 61
  • What is the second parameter **'UnitSource'** used with the OnInclude or OnNeedUnit functions? (what should be sent?) – Zeina Mar 06 '12 at 08:11
  • 1
    It's for you to place the source.In the case of OnNeedUnit, you can either specified a script source to be compiled that way, or use the return value to pass an IdwsUnit (when the unit being referred isn't script, but something implemented Delphi side) – Eric Grange Mar 06 '12 at 08:42
  • No I don't want to provide source code inside my project, I would rather prefer to use the $Include mentioned by Dorin, but it is not working. "Couldn't find file on input paths" this is the resulting compiler error. Am I missing something? Thanks in advance. – Zeina Mar 06 '12 at 08:49