DelphiWebScript

OOP - Object Orientated Programming

Homepage
DWS
OOP
 

Static Objects

DWS implements two different kinds of object orientated programming. Using a normal TdwsFunc component it's possible to create "static objects":

s:= request.name;

DWS transforms this script code into...

s := request ('NAME');

The declaration of the TdwsFunc component has to be: "request (string, ...) : <resulttype>". If you replace the dots ... by additional parameters a call of this static method could look like this:

s := request.name (1, 2, 4.5);

// Transformed into...
s := request ('NAME', 1, 2, 4.5);

The implementation of the function request looks like this:

procedure TForm1.fncRequest (Sender: TObject; args: TArgList; var Result: Variant);
begin
if args[0].Eval = 'NAME' then
...
else if args[0].Eval = 'URL' then
...
end;

It's also possible to use static objects in an assignment:

myobject.value := 2;

This statement is transformed into...

myobject ("value", 2);

Have a look at the demo scripts for more information.

Dynamic Objects

Dynamic Objects use almost the same concept as the objects known from Delphi (obj := TObject.Create). E. g. you could use this code in DWS:

orders := dbOpen ('ORDERS.DB');
customers := dbOpen ('CUSTOMERS.DB');

orders.dbFrist;
customers.dbFirst;

send (orders.dbFieldByName ('NAME'), ', '
customers.dbFieldByName ('PRODNAME'));
.....

The objects "orders" and "customers" are in fact only variables of type integer. In the example the call to dbOpen creates and opens a Delphi TTable object. To make this object accessible from within DWS it's stored in a object derived from TdwsObj.

TdwsDbObj = class (TdwsObj)
tab : TTable;
end;

proedure TdbOpenFunc.Evaluate;
var dwsDbObj : TdwsDbObj;
begin
dwsDbObj := TdwsDbObj.Create;
dwsDbObj.tab := TTable.Create (....);
result := funcScript.NewObj (dwsDbObj);
end;

The function NewObj of the TDelphiWebScript component adds the new object to its internal list an returns an index. This index is stored in the variable orders and customers like a normal integer value.

The method calls work similar to the static objects:

orders.dbFieldByName ('NAME');
// is transformed to...
dbFieldByName (orders, 'NAME');

The function dbFieldByName needs to access the TTable object previously stored with a call to the function NewObj. For this purpose the method GetObj has to be called using the integer value returned by NewObj as argument. A possible implementation of dbFieldByNameFunc.Evaluate:

proedure TdbFieldByNameFunc.Evaluate;
var dwsDbObj : TdwsDbObj;
begin
dwsDbObj := funcScript.GetObj (TExpr (args[0]).Eval);
result := dwsDbObj.tab.FieldByName (TExpr (args[1]).Eval);
end;

Have a look at the demo scripts for more information.