base_class.pas

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

PAS
2,252 行
字号
        result := GetInt64Prop(Instance, PropInfo);
        {$ELSE}
        D := GetInt64Prop(Instance, PropInfo);
        result := D;
        {$ENDIF}
      end;
      tkFloat: result := GetFloatProp(Instance, PropInfo);
      tkEnumeration:
{$ifdef fp}
        if TypeData^.BaseType = TypeInfo(Boolean) then
{$else}
        if TypeData^.BaseType^ = TypeInfo(Boolean) then
{$Endif}
          result := Boolean(GetOrdProp(Instance, PropInfo))
        else
        begin
          result := GetOrdProp(Instance, PropInfo);
{
          I := GetOrdProp(Instance, PropInfo);
          result := GetEnumName(PropInfo^.PropType^, I); }
        end;
      tkSet:
      begin
        I := GetOrdProp(Instance, PropInfo);
        ByteSet := [];
        Move(I, ByteSet, SizeOf(ByteSet));
        result := ByteSetToPaxArray(ByteSet, Scripter);
      end;
      tkClass:
      begin
        I := GetOrdProp(Instance, PropInfo);
        if I <> 0 then
        begin
          result := ScriptObjectToVariant(DelphiInstanceToScriptObject(TObject(I), Scripter));
        end;
      end;
      tkChar:
      begin
        S := Chr(GetOrdProp(Instance, PropInfo));
        result := S;
      end;
      tkString, tkLString, tkWString:
      begin
        S := GetStrProp(Instance, PropInfo);
        result := S;
      end;
      tkVariant:
      begin
        result := GetVariantProp(Instance, PropInfo);

        if VarType(result) = varDispatch then
        begin
          X := ActiveXObject.Create(Scripter);
          X.D := result;
          result := ScriptObjectToVariant(DelphiInstanceToScriptObject(X, Scripter));
        end
        else
        if (VarType(result) >= varArray) and (Nparams > 0) then
        begin
          SetLength(Indexes, NParams);
          for I:=1 to NParams do
            Indexes[NParams - I] := TPAXBaseScripter(Scripter).Code.PopVariant;
          V := Variant(ArrayGet(@result, Indexes)^);
          result := V;
        end;
      end;
    end;
  end
  else
  begin
    if Instance.InheritsFrom(TComponent) then
    begin
      C := TComponent(Instance);
      for I:=0 to C.ComponentCount - 1 do
        if StrEql(C.Components[I].Name, PropertyName) then
        begin
          result := ScriptObjectToVariant(DelphiInstanceToScriptObject(C.Components[I], Scripter));
          Exit;
        end;
    end;

    P := Instance.MethodAddress(PropertyName);
    if P <> nil then
    begin
      Def := TPaxMethodDefinition.Create(DefinitionList, '', nil, -1, nil, true);

      try
        with Def do
        begin
          DirectProc := P;
          SetLength(Types, NParams + 1);
          SetLength(ExtraTypes, NParams + 1);
          SetLength(ByRefs, NParams + 1);
          for I:=0 to NParams do
          begin
            Types[I] := typeVOID;
            ByRefs[I] := false;
          end;
        end;

        This := ScriptObjectToVariant(DelphiInstanceToScriptObject(Instance, Scripter));
        TPAXBaseScripter(Scripter).Code.CallHostSub(Def, @This, @Result, NParams);
      finally
        Def.Free;
      end;
      Exit;
    end;

    raise TPAXScriptFailure.Create(Format(errPropertyIsNotFoundInClass, [PropertyName, Instance.ClassName]));
  end;
end;

function TPAXScriptObject.HasProperty(const PropertyName: String): Boolean;
var
  P: TPAXProperty;
begin
  P := PropertyList.FindProperty(CreateNameIndex(PropertyName, Scripter));
  if P = nil then
    result := HasPublishedProperty(PropertyName)
  else
    result := true;
end;

function TPAXScriptObject.HasPublishedProperty(const PropertyName: String): Boolean;
var
  PropInfo: PPropInfo;
  pti: PTypeInfo;
  C: TComponent;
  I: Integer;
begin
  result := false;

  if Instance = nil then
    Exit;

  if HasFordiddenProperties then
    if TPaxBaseScripter(Scripter).HasForbiddenPublishedProperty(Instance.ClassType, PropertyName) then
       Exit;

  if Instance.MethodAddress(PropertyName) <> nil then
  begin
    result := true;
    Exit;
  end;

  pti := PTypeInfo(Instance.ClassType.ClassInfo);
  if pti = nil then
    Exit;
  PropInfo := GetPropInfo(pti, PropertyName);
  if PropInfo <> nil then
  begin
    result := true;
    Exit;
  end;
  if Instance.InheritsFrom(TComponent) then
  begin
    C := TComponent(Instance);
    for I:=0 to C.ComponentCount - 1 do
      if StrEql(C.Components[I].Name, PropertyName) then
      begin
        result := true;
        Exit;
      end;
  end;
end;

function TPAXScriptObject.Get(PropertyNameIndex: Integer): TPAXProperty;
begin
  result := PropertyList.FindProperty(PropertyNameIndex);
  if result = nil then
  begin
    TPaxBaseScripter(Scripter).Dump;
    raise TPAXScriptFailure.Create(Format(errPropertyIsNotFound,
        [_GetName(PropertyNameIndex, Scripter)]));
  end;
end;

function TPAXScriptObject.SafeGet(PropertyNameIndex: Integer): TPAXProperty;
begin
  result := PropertyList.FindProperty(PropertyNameIndex);
end;

function TPAXScriptObject.Put(PropertyNameIndex: Integer;
                           const Value: Variant; NParams: Integer): TPAXProperty;
var
  S: String;
  V: Variant;
  D: TPAXMethodDefinition;
  MemberRec: TPaxMemberRec;
begin
  result := PropertyList.FindProperty(PropertyNameIndex);
  if result <> nil then
  begin
    if result.MemberRec <> nil then
      if result.MemberRec.IsPublished then
      begin
        S := _GetName(PropertyNameIndex, Scripter);
        PutPublishedProperty(S, Value);
        Exit;
      end;

    result.PutValue(NParams, Value);
    Exit;
  end;
  if IsImported then
  begin
    S := _GetName(PropertyNameIndex, Scripter);
    if HasPublishedProperty(S) then
      PutPublishedProperty(S, Value)
    else if ClassDef.PutPropDef <> nil then
    begin
      D := ClassDef.PutPropDef;
      D.Name := S;
      with TPAXBaseScripter(ClassRec.Scripter) do
      begin
//        if ClassRec.AncestorName = 'Function' then
//          Instance := Self;

        V := ScriptObjectToVariant(Self);
        Code.SaveState;
        Code.Stack.Push(Integer(@Value));
        Code.CallHostSub(D, @V, nil, NParams + 1);
        Code.RestoreState;
      end;
      D.Name := '';
    end
    else
      Get(PropertyNameIndex); // exception
  end
  else
  begin
    MemberRec := ClassRec.FindMember(PropertyNameIndex, maAny, true);
    if MemberRec = nil then
      Get(PropertyNameIndex) // exception
    else
    begin
      result := CreateProperty(PropertyNameIndex,
                               TPAXBaseScripter(ClassRec.Scripter).SymbolTable.Address[MemberRec.Id],
                               MemberRec);
      while (NParams > 0) do
      begin
        TPAXBaseScripter(ClassRec.Scripter).Code.PopVariant;
        Dec(NParams);      
      end;
    end;
  end;
end;

function TPAXScriptObject.GetProperty(PropertyNameIndex: Integer;
                                      NParams: Integer): Variant;
var
  P: TPAXProperty;
  S: String;
  D: TPAXMethodDefinition;
  V: Variant;
  MemberRec: TPaxMemberRec;
begin
  P := PropertyList.FindProperty(PropertyNameIndex);
  if P <> nil then
  begin
    if P.MemberRec <> nil then
      if P.MemberRec.IsPublished then
      begin
        S := _GetName(PropertyNameIndex, Scripter);
        result := GetPublishedProperty(S, NParams);
        Exit;
      end;

    result := P.GetValue(NParams);
    Exit;
  end;
  if IsImported then
  begin
    S := _GetName(PropertyNameIndex, Scripter);
    if HasPublishedProperty(S) then
      result := GetPublishedProperty(S, NParams)
    else if ClassDef.GetPropDef <> nil then
    begin
//      if ClassRec.AncestorName = 'Function' then
//        Instance := Self;

      D := ClassDef.GetPropDef;
      D.Name := S;
      with TPAXBaseScripter(ClassRec.Scripter) do
      begin
        V := ScriptObjectToVariant(Self);
        Code.SaveState;
        Code.CallHostSub(D, @V, @result, NParams);
        Code.RestoreState;
      end;
      D.Name := '';
    end
    else
      Get(PropertyNameIndex); // exception
  end
  else
  begin
    if SignLoadOnDemand then
    begin
      MemberRec := ClassRec.GetMember(PropertyNameIndex);
      if MemberRec <> nil then
      begin
        result := TPAXBaseScripter(ClassRec.Scripter).SymbolTable.GetVariant(MemberRec.Id);
        Exit;
      end;
    end;

    Get(PropertyNameIndex); // exception
  end;
end;

procedure TPAXScriptObject.ClearProperty(PropertyNameIndex: Integer);
var
  P: TPAXProperty;
begin
  P := PropertyList.FindProperty(PropertyNameIndex);
  if P <> nil then
    VarClear(P.fValue^);
end;

function TPAXScriptObject.GetAddress(PropertyNameIndex: Integer;
                                     NParams: Integer): PVariant;
var
  P: TPAXProperty;
  S: String;
  D: TPAXMethodDefinition;
  V: Variant;
  MemberRec: TPaxMemberRec;
begin
  result := nil;
  P := PropertyList.FindProperty(PropertyNameIndex);
  if P <> nil then
  begin
    if P.MemberRec <> nil then
      if P.MemberRec.IsPublished then
      begin
        result := nil;
//        raise TPaxScriptFailure.Create(errOperatorNotApplicable);
        Exit;
      end;

    result := P.GetAddress(NParams);
    Exit;
  end;
  if IsImported then
  begin
    S := _GetName(PropertyNameIndex, Scripter);
    if HasPublishedProperty(S) then
      result := nil
    else if ClassDef.GetPropDef <> nil then
    begin
      D := ClassDef.GetPropDef;
      D.Name := S;
      with TPAXBaseScripter(ClassRec.Scripter) do
      begin
        V := ScriptObjectToVariant(Self);
        Code.SaveState;
        Code.CallHostSub(D, @V, @TempVariant, NParams);
        Code.RestoreState;

        result := @TempVariant;
      end;
      D.Name := '';
    end
    else
      Get(PropertyNameIndex); // exception
  end
  else
  begin
    if SignLoadOnDemand then
    begin
      MemberRec := ClassRec.GetMember(PropertyNameIndex);
      if MemberRec <> nil then
      begin
        result := TPAXBaseScripter(ClassRec.Scripter).SymbolTable.Address[MemberRec.Id];
        Exit;
      end;
    end;

    Get(PropertyNameIndex); // exception
  end;
end;

procedure TPAXScriptObject.PutProperty(PropertyNameIndex: Integer;
                                    const Value: Variant; NParams: Integer);
begin
  Put(PropertyNameIndex, Value, NParams);
end;

function TPAXScriptObject.GetDefaultProperty(NParams: Integer): Variant;
var
  P: TPAXProperty;
begin
  P := PropertyList.GetDefaultProperty;
  if P = nil then
  begin
    raise TPAXScriptFailure.Create(errDefaultPropertyIsNotFound);
   end;
  result := P.GetValue(NParams);
end;

procedure TPAXScriptObject.PutDefaultProperty(const Value: Variant; NParams: Integer);
var
  P: TPAXProperty;
begin
  P := PropertyList.GetDefaultProperty;
  if P = nil then
    raise TPAXScriptFailure.Create(errDefaultPropertyIsNotFound);
  P.PutValue(NParams, Value);
end;

function TPAXScriptObject.CreateProperty(PropertyNameIndex: Integer;
                                         PValue: PVariant;
                                         MemberRec: TPAXMemberRec): TPAXProperty;
var
  I: Integer;
begin
  I := PropertyList.IndexOf(PropertyNameIndex);
  if I <> -1 then
  begin
    result := PropertyList.Properties[I];
    Exit;
  end;

  result := TPAXProperty.Create(Self, PValue, MemberRec);
  PropertyList.AddObject(PropertyNameIndex, result);
end;

function TPAXScriptObject.Duplicate: TPAXScriptObject;
var
  I, PropertyNameIndex: Integer;
  SourceProperty, DestProperty: TPAXProperty;
  P: PVariant;
  SO: TPAXScriptObject;
  V: Variant;
begin
  result := TPAXScriptObject.Create(ClassRec);
  result.RefCount := 1;

  if Instance = nil then
    result.Instance := Instance;

  result.PClass := PClass;

  if ExtraPtrSize > 0 then
  begin
    result.ExtraPtrSize := ExtraPtrSize;
    result.ExtraPtr := A

⌨️ 快捷键说明

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