base_class.pas

来自「Delphi脚本控件」· PAS 代码 · 共 2,252 行 · 第 1/5 页

PAS
2,252 行
字号
      V := ScriptObjectToVariant(SO);
      SymbolTable.SaveState;
      SubID := SymbolTable.AppReference(V, SymbolTable.NameIndex[WriteID], maAny);
      Code.EvalSetProcedure(SubID, NParams, AValue);
      SymbolTable.RestoreState;
      Exit;
    end;

    P := SO.PropertyList.FindProperty(SymbolTable.NameIndex[WriteID]);
    if P = nil then
      raise TPAXScriptFailure.Create(Format(errPropertyIsNotFound, [SymbolTable.Name[WriteID]]));

    P.fValue^ := AValue;
  end;
end;

function TPAXProperty.IsImported: Boolean;
begin
  result := Definition <> nil;
end;

function TPAXProperty.IsStatic: Boolean;
begin
  result := false;
  if MemberRec <> nil then
    result := modStatic in MemberRec.ml;
end;

function TPAXProperty.Definition: TPAXDefinition;
begin
  if MemberRec = nil then
  begin
    result := nil;
    Exit;
  end;

  result := MemberRec.Definition;

  if ExtraDef <> nil then
    result := ExtraDef;
end;

function TPAXProperty.GetReadID: Integer;
begin
  if MemberRec = nil then
  begin
    result := 0;
    Exit;
  end;

  result := MemberRec.ReadID;
end;

function TPAXProperty.GetWriteID: Integer;
begin
  if MemberRec = nil then
  begin
    result := 0;
    Exit;
  end;

  result := MemberRec.WriteID;
end;

constructor TPAXPropertyList.Create(Scripter: Pointer);
begin
  Self.Scripter := Scripter;
  inherited Create;
end;

function TPAXPropertyList.GetProperty(I: Integer): TPAXProperty;
begin
  result := TPAXProperty(Objects[I]);
end;

function TPAXPropertyList.GetName(I: Integer): String;
var
  NameIndex: Integer;
begin
  NameIndex := NameID[I];
  result := _GetName(NameIndex, Scripter);
end;

function TPAXPropertyList.FindProperty(PropertyNameIndex: Integer): TPAXProperty;
var
  I, UpCaseIndex: Integer;
  P: TPAXProperty;
begin
  result := TPAXProperty(GetObject(PropertyNameIndex));

  if result = nil then
  begin
    UpCaseIndex := NameIndexToUpperCaseIndex(PropertyNameIndex, Scripter);
    for I:=0 to Count - 1 do
    begin
      P := Properties[I];
      if P.UpCaseIndex = UpCaseIndex then
      begin
        result := P;
        Exit;
      end;
    end;
  end;
end;

function TPAXPropertyList.GetDefaultProperty: TPAXProperty;
var
  I: Integer;
begin
  result := nil;
  for I:=0 to Count - 1 do
    with TPAXProperty(Objects[I]) do
      if MemberRec <> nil then
        if modDEFAULT in MemberRec.ml then
        begin
          result := Properties[I];
          Exit;
        end;
end;

constructor TPAXScriptObject.Create(ClassRec: TPAXClassRec);
begin
  inherited Create;

  Inc(TPAXBaseScripter(ClassRec.Scripter)._ObjCount);
  _ObjCount := TPAXBaseScripter(ClassRec.Scripter)._ObjCount;

  Self.ClassRec := ClassRec;

  PropertyList := TPAXPropertyList.Create(ClassRec.Scripter);

  Instance := nil;
  PClass := nil;
  RefCount := 0;
  IsClass := false;
  ExtraPtr := nil;
  ExtraPtrSize := 0;
  ExternalExtraPtr := false;
  Intf := nil;
  PIntf := nil;

{$IFDEF WIN32}
  ThreadID := GetCurrentThreadID();
{$ELSE}
  ThreadID := 0;
{$ENDIF}

  TPAXBaseScripter(ClassRec.Scripter).ScriptObjectList.Add(Self);
  HasFordiddenProperties := (TPaxBaseScripter(Scripter).ForbiddenPublishedProperties.Count > 0) or
                            (TPaxBaseScripter(Scripter).ForbiddenPublishedPropertiesEx.Count > 0); 
end;

procedure TPAXScriptObject.CallAutoDestructor;
begin
  Pointer(Intf) := nil;

  if ClassRec = nil then
    Exit;
  if ClassRec.AutoDestructorID = 0 then
    Exit;

//  TPAXBaseScripter(Scripter).CallMethod(ClassRec.AutoDestructorID,
//    ScriptObjectToVariant(Self), []);
end;

function TPAXScriptObject.ExtraInstance: TObject;
begin
  result := Instance;
end;

function TPAXScriptObject.GetPropertyName(Index: Integer): String;
var
  NameIndex: Integer;
begin
  with PropertyList do
    if (Index >= 0) and (Index < Count) then
    begin
      NameIndex := NameID[Index];
      result := _GetName(NameIndex, Scripter)
    end
    else
      result := '';
end;

procedure TPAXScriptObject.SetDefaultValue(const V: Variant);
begin
end;

function TPAXScriptObject.DefaultValue: Variant;
begin
  result := Undefined;
end;

procedure TPAXScriptObject.FreeExtraPtr;
var
  D: TPaxClassDefinition;
  P: Pointer;
begin
  if ExtraPtr = nil then
    Exit;
  if ExternalExtraPtr then
    Exit;

  if ExtraPtrSize > 0 then
  begin
    if ClassRec.ck = ckDynamicArray then
      ExtraPtr := ShiftPointer(ExtraPtr, - SizeOf(Pointer) - SizeOf(Pointer));
    FreeMem(ExtraPtr, ExtraPtrSize);
    Exit;
  end
  else if ExtraPtrSize < 0 then
  begin
    P := ShiftPointer(ExtraPtr, ExtraPtrSize);
    FreeMem(P, - ExtraPtrSize);
  end;

  D := GetClassDef;
  if D = nil then
    Exit;

  if D.ClassKind <> ckDynamicArray then
     Exit;
end;

destructor TPAXScriptObject.Destroy;
begin
  PropertyList.Free;

  if (Assigned(Instance) and (ClassRec.ck = ckArray)) then
  begin
    Instance.Free;
    Instance := nil;
  end;

  FreeExtraPtr;

  inherited;
end;

function TPAXScriptObject.Scripter: Pointer;
begin
  result := ClassRec.Scripter;
end;

function TPAXScriptObject.IsImported: Boolean;
begin
  if ClassRec = nil then
    result := false
  else
    result := ClassRec.IsImported;
end;

function TPAXScriptObject.GetClassDef: TPAXClassDefinition;
begin
  if not IsImported then
  begin
    result := nil;
    Exit;
  end;
  result := ClassRec.GetClassDef;
end;


procedure TPAXScriptObject.PutPublishedProperty(const PropertyName: String; const Value: Variant);
var
  PropInfo: PPropInfo;
  I: Integer;
  M: TMethod;
  pti, pti1: PTypeInfo;
  ptd: PTypeData;
  EnumName, S: String;
  SO: TPAXScriptObject;
  EventHandler: TPAXEventHandler;
  SubID: Integer;
  This: Variant;
  ByteSet: TByteSet;
begin
  if HasFordiddenProperties then
    if TPaxBaseScripter(Scripter).HasForbiddenPublishedProperty(Instance.ClassType, PropertyName) then
      raise TPAXScriptFailure.Create(Format(errPropertyNotFoundInClass, [PropertyName, Instance.ClassName]));

  pti := PTypeInfo(Instance.ClassType.ClassInfo);

  if pti = nil then
    PropInfo := nil
  else
    PropInfo := GetPropInfo(pti, PropertyName);

  if PropInfo <> nil then
  begin
{$ifdef fp}
    pti1 := PropInfo.PropType;
{$else}
    pti1 := PropInfo.PropType^;
{$endif}
    case pti1^.Kind of
      tkInteger, tkChar, tkWChar:
        SetOrdProp(Instance, PropInfo, TVarData(Value).VInteger);
      tkClass:
        SetOrdProp(Instance, PropInfo, Integer(VariantToScriptObject(Value).Instance));
      tkEnumeration:
      begin
        case VarType(Value) of
          varString:
          begin
{$ifdef fp}
            pti := PropInfo.PropType;
{$else}
            pti := PropInfo.PropType^;
{$endif}
            ptd := GetTypeData(pti);
            S := ToStr(Scripter, Value);
            with ptd^ do
              for I:= MinValue to MaxValue do
              begin
                EnumName := GetEnumName(pti, I);
                if StrEql(S, EnumName) then
                begin
                  SetOrdProp(Instance, PropInfo, I);
                  Break;
                end;
              end;
          end;
          varBoolean:
          begin
            if TVarData(Value).VInteger <> 0 then
              SetOrdProp(Instance, PropInfo, Ord(True))
            else
              SetOrdProp(Instance, PropInfo, 0);
          end;
        else
          SetOrdProp(Instance, PropInfo, TVarData(Value).VInteger);
        end;
      end;
      tkFloat:
        SetFloatProp(Instance, PropInfo, Value);
      tkString, tkLString, tkWString:
        case TVarData(Value).VType of
          varInteger: SetStrProp(Instance, PropInfo, Chr(TVarData(Value).VInteger));
          varString: SetStrProp(Instance, PropInfo, String(TVarData(Value).VString));
        else
          SetStrProp(Instance, PropInfo, ToStr(Scripter, Value));
        end;
      tkVariant:
        SetVariantProp(Instance, PropInfo, GetTerminal(@Value)^);
      tkSet:
      begin
        ByteSet := PaxArrayToByteSet(Value);
        I := 0;
        Move(ByteSet, I, SizeOf(ByteSet));
        SetOrdProp(Instance, PropInfo, I);
      end;
      tkMethod:
      begin
        M := GetMethodProp(Instance, PropInfo);

        if IsUndefined(Value) then
        begin
          M.Code := nil;
          M.Data := nil;
          SetMethodProp(Instance, PropInfo, M);
          Exit;
        end;

        SO := VariantToScriptObject(Value);

        if SO.ClassRec.AncestorName = 'Function' then
          SubID := SO.ClassRec.GetConstructorID
        else
          SubID := TPAXDelegate(SO.Instance).SubID;
        This := ScriptObjectToVariant(Self);
{$ifdef fp}
        EventHandler := TPAXEventHandler.Create(Scripter, @PropInfo.PropType, SubID, This);
{$else}
        EventHandler := TPAXEventHandler.Create(Scripter, PropInfo.PropType^, SubID, This);
{$endif}
        EventHandler.HostHandler := M;
        EventHandler.OverrideHandlerMode := TPaxBaseScripter(Scripter).OverrideHandlerMode;

        M.Code := @TPAXEventHandler.HandleEvent;
        M.Data := Pointer(EventHandler);
        TPAXBaseScripter(Scripter).EventHandlerList.Add(EventHandler);
        SetMethodProp(Instance, PropInfo, M);

        EventHandler.DelphiInstance := Instance;
        EventHandler.PropInfo := PropInfo;

        EventHandler.This := TPAXBaseScripter(Scripter).Code._This;

        if IsUndefined(EventHandler.This) then
        begin
          SubID := TPAXBaseScripter(Scripter).Code.LevelStack.Top;
          with TPAXBaseScripter(Scripter).SymbolTable do
            EventHandler.This := GetVariant(GetThisID(SubID));
        end;
      end;
    end
  end
  else
  begin
    raise TPAXScriptFailure.Create(Format(errPropertyIsNotFoundInClass, [PropertyName, Instance.ClassName]));
  end;
end;

function TPAXScriptObject.GetPublishedProperty(const PropertyName: String; NParams: Integer): Variant;
var
  PropInfo: PPropInfo;
  S: String;
  I: Integer;
  TypeData: PTypeData;
  pti, pti1: PTypeInfo;
  C: TComponent;
  X: ActiveXObject;
  Indexes: array of Integer;
  V: Variant;
  P: Pointer;
  This: Variant;
  Def: TPAXMethodDefinition;
  ByteSet: TByteSet;
{$IFNDEF VARIANTS}
  D: Double;
{$ENDIF}
begin
  if HasFordiddenProperties then
    if TPaxBaseScripter(Scripter).HasForbiddenPublishedProperty(Instance.ClassType, PropertyName) then
      raise TPAXScriptFailure.Create(Format(errPropertyIsNotFoundInClass, [PropertyName, Instance.ClassName]));

  pti := PTypeInfo(Instance.ClassType.ClassInfo);
  if pti = nil then
    PropInfo := nil
  else
    PropInfo := GetPropInfo(pti, PropertyName);

  if PropInfo <> nil then
  begin
{$ifdef fp}

    TypeData := GetTypeData(PropInfo^.PropType);
    pti1 := PropInfo.PropType;
{$else}
    TypeData := GetTypeData(PropInfo^.PropType^);
    pti1 := PropInfo.PropType^;
{$endif}

    // return the right type

    case pti1^.Kind of
      tkInteger: result := GetOrdProp(Instance, PropInfo);
      tkInt64:
      begin
        {$IFDEF VARIANTS}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?