分享

ERP SYSTEM 开发指南(二)添加脚本支持

 昵称10504424 2013-12-18

在二次开发中实现脚本对算定义类的调用

如何为一个类添加脚本支持,FastScript是解释执行的语言,通过对语义的分析来执行

FastScript已经对Delphi常用的类做好了解释,比如fs_iformsrtti解释了脚本对窗体的调用,fs_iinirtti解释了脚本对TIniFiles类的使用

一个类的published属性,在RTTI的支持下可以自动解释,关键是函数的解释

模拟下面源代码的编写,就可以在脚本中直接使用该类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
unit fs_iMyClassRTTI; //按FastScript的习惯命名, fs_类名RTTI.pas
interface
uses
Windows, Messages, SysUtils, StrUtils, Variants, Classes, Graphics, Controls, Forms;
type
  TMyClass=class(TObject)
  private
    FNewProp:string;
    FNewProp2:string;
  public
    constructor Create;
    function Func1(int i):int;
    function Func2(str:TStrings):boolean;
  public
    property NewProp2:string read FNewProp2 write FNewProp2;
  published
    property NewProp:string read FNewProp write FNewProp;
  end;
implementation
type
  TFunctions = class(TfsRTTIModule)
  private
  private
    //函数调用
    function CallMethod(Instance: TObject; ClassType: TClass; const MethodName: String; Caller: TfsMethodHelper): Variant;
    //属性获取
    function GetProp(Instance: TObject; ClassType: TClass; const PropName: String): Variant;
    //属性设置
    procedure SetProp(Instance: TObject; ClassType: TClass; const PropName: String; Value: Variant);
public
    //在构照器进行类型,函数说明
    constructor Create(AScript: TfsScript); override;
end;
{ TMyClass }
function TMyClass.Func1(int i):int;
begin
  result:=0;
end;
function TMyClass.Func2(str:TStrings):boolean;
begin
  result:=True;
end;
{ TFunctions }
function TFunctions.CallMethod(Instance: TObject; ClassType: TClass; const MethodName: String; Caller: TfsMethodHelper): Variant;
begin
  Result := 0;
  //根据函数名调用实际执行的函数,关键当参数为类对象时的转换,看函数Func2的调用写法
  if ClassType = TMyClass then
  begin
    if MethodName = 'CREATE' then
      Result := Integer(TMyClass.Create)
    else if MethodName = 'FUNC1' then
      Result:= TMyClass(Instance).Func1(Caller.Params[0]))
    else if MethodName = 'FUNC2' then
      Result := TMyClass(Instance).Func2(TStrings(Integer(Caller.Params[0])))
    end;
end;
constructor TFunctions.Create(AScript: TfsScript);
begin
  inherited;
  with AScript do
  begin
    //添加类说明,第一个参数为被说明的类,第二个参数为该类型的基类
    with AddClass(TMyClass, 'TObject') do
    begin
      //添加函数定义说明,第二个参数是该函数由哪个函数调用
      AddMethod('function Func1(int i):int;',CallMethod);
      AddMethod('function Func2(str:TStrings):boolean;',CallMethod);
    end;
  end;
end;
function TFunctions.GetProp(Instance: TObject; ClassType: TClass; const PropName: String): Variant;
begin
  //添加属性说明,published属性不必添加
  if ClassType = TMyClass then
  begin
    if PropName = 'NewProp2' then
      Result := TMyClass(Instance).NewProp2;
  end
end;
procedure TFunctions.SetProp(Instance: TObject; ClassType: TClass; const PropName: String; Value: Variant);
begin
  if ClassType = TMyClass then
  begin
    if PropName = 'NewProp2' then
      TMyClass(Instance).NewProp2:=Value;
  end
end;
initialization
  //文件加载时,将解析类TFunctions添加到脚本引擎RTTI库  
  fsRTTIModules.Add(TFunctions);
finalization
fsRTTIModules.Remove(TFunctions);

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多