base_code.pas

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

PAS
2,451 行
字号
  Inc(N);
end;

procedure TPAXCode.OperPush;
var
  SO: TPaxScriptObject;
  P: Pointer;
  V: Variant;
begin
  with SymbolTable, Prog[N] do
  begin
    case Kind[Arg1] of
      KindRef:
      begin
        SO := VariantToScriptObject(GetVariant(Arg1));
        P := SO.GetAddress(NameIndex[Arg1], 0);
        if P <> nil then
          Stack.Push(P)
        else
        begin
          V := VariantValue[Arg1];
          PutVariant(Arg1, V);
          Kind[Arg1] := KindVAR;
          Stack.Push(Address[Arg1]);
        end;
      end;
      KindHostVar:
        Stack.Push(GetAddressEx(Arg1));
      KindHostConst:
        Stack.Push(GetAddressEx(Arg1));
      KindHostObject:
        Stack.Push(GetAddressEx(Arg1));
      KindHostInterfaceVar:
        Stack.Push(GetAddressEx(Arg1));
      else
      begin
         if (Arg1 > 0) and SignFOP and (not IsAlias(Address[Arg1])) then
          begin
            PP_Arg1 := @A[Arg1].Address;
            OP := FOP_PUSH;
          end;
          Stack.Push(Address[Arg1]);
      end;
    end;
  end;

  Inc(N);
end;

procedure TPAXCode.CallSub(SubID, ParamCount: Integer; PThis: PVariant; ResultID: Integer;
                           MakeFOP: Boolean = false);
var
  I, K, ParamID, NP, BoundID: Integer;
  P: Pointer;
  SO: TPAXScriptObject;
  FInstance: TPAXDelegate;
  Entry, T: Integer;
begin
  K := 0;

  _ParamCount := ParamCount;

  if (Prog[N]._Entry = 0) or (not SignFOP) then
  begin
    if SubID < 0 then
    begin
        if IsObject(PThis^) then
          CallHostSub(DefinitionList[-SubID],
                    PThis,
                    SymbolTable.Address[ResultID], ParamCount)
        else
          CallHostSub(DefinitionList[-SubID],
                    SymbolTable.Address[SymbolTable.RootNamespaceID],
                    SymbolTable.Address[ResultID], ParamCount);
      Exit;
    end;

    SO := VariantToScriptObject(SymbolTable.VariantValue[SubID]);

    if SO.Instance = nil then
      raise TPAXScriptFailure.Create(Format(errFunctionNotFound, [SymbolTable.Name[SubID]]));

    FInstance := TPAXDelegate(SO.Instance);
    SubID := FInstance.SubID;
    Entry := FInstance.N;

    if FInstance.D <> nil then
    begin
      CallHostSub(FInstance.D, PThis, SymbolTable.A[ResultID].Address, ParamCount);
      Exit;
    end;

    if SubID = 0 then
      raise TPAXScriptFailure.Create(Format(errFunctionNotFound, [SymbolTable.Name[SubID]]));

    Prog[N]._Entry := Entry;
    Prog[N]._SubID := SubID;

    if MakeFOP and SignFOP then
      Prog[N].Op := FOP_CALL;
  end
  else
  with Prog[N] do
  begin
    Entry := _Entry;
    SubID := _SubID;
  end;

  with SymbolTable do
  begin
    NP := A[SubID].Count;

    if NP > ParamCount then
    begin
      BoundID := GetParamID(SubID, ParamCount);
      I := TPaxBaseScripter(Scripter).DefaultParameterList.FindFirst(SubID);
      while I <> -1 do
      begin
        if TPaxBaseScripter(Scripter).DefaultParameterList[I].ID > BoundID then
        begin
          Inc(ParamCount);
          Stack.Push(@ TPaxBaseScripter(Scripter).DefaultParameterList[I].Value);
        end;
        I := TPaxBaseScripter(Scripter).DefaultParameterList.FindNext(I, SubID);
      end;
      if NP > ParamCount then
      begin
        if DeclareON then raise TPAXScriptFailure.Create(errNotEnoughParameters);
      end
      else if NP < ParamCount then
      begin
        if DeclareON then raise TPAXScriptFailure.Create(errTooManyParameters);
      end
    end
    else if NP < ParamCount then
    begin
      if DeclareON then raise TPAXScriptFailure.Create(errTooManyParameters);
    end;

    AllocateSub(SubID);
    ParamID := GetParamID(SubID, ParamCount);
    for I:=ParamCount downto 1 do
    begin
      P := Stack.PopPointer;
      T := PType[ParamID];

      if ByRef[ParamID] > 0 then
      begin
        Inc(K);
        RefStack.Push(A[ParamID].Address);
        RefStack.Push(ParamID);

        A[ParamID].Address := P;
        if ByRef[ParamID] = 1 then
        if not TestBit(Prog[N].Vars, I) then
          raise TPAXScriptFailure.Create(errIncorrectUsageOfRefParameter + ' ' + Name[ParamID] +
                ' of method ' + Name[SubID]);

        if T = typeDOUBLE then
          if Rank[ParamID] = 0 then
             Variant(A[ParamID].Address^) := Double(Variant(P^));
      end
      else
      begin
        if (T = typeDOUBLE) and (Rank[ParamID] = 0) then
        begin
           if IsObject(Variant(P^)) then
             Variant(A[ParamID].Address^) := Variant(P^)
           else
             Variant(A[ParamID].Address^) := Double(Variant(P^));
        end
        else
          Variant(A[ParamID].Address^) := Variant(P^);
      end;

      if DebugState then
        DebugInfo.Push(P);
      Dec(ParamID);
    end;

    if DebugState then
    with DebugInfo do
    begin
      Push(ParamCount);
      Push(N);
      Push(SubID);
    end;

//    Address[GetThisID(SubID)] := PThis;

    I := GetThisID(SubID);
    if A[I].Next <> 0 then
      PutVariant(I, PThis^)
    else
      Address[I] := PThis;

    with Stack do
    begin
      Push(N);
      Push(SubID);
      Push(ResultID);
    end;

    N := Entry;
  end;

  LevelStack.Push(SubID);

  RefStack.Push(K);
end;

procedure TPAXCode.CallHostSub(D: TPAXMethodDefinition; PThis, PResult: PVariant; ParamCount: Integer);

function ToCardinal(const V: Variant): Cardinal;
var
  D: Double;
begin
  case VarType(V) of
    varInteger: result := V;
    varDouble:
    begin
      D := V;
      result := Round(D);
    end;
    else
      raise TPAXScriptFailure.Create(errIncompatibleTypes);
  end;
end;

{$IFNDEF VARIANTS}
function ToInt64(const V: Variant): Int64;
var
  D: Double;
begin
  case VarType(V) of
    varInteger, varDouble:
    begin
      D := V;
      result := Round(D);
    end;
    else
      raise TPAXScriptFailure.Create(errIncompatibleTypes);
  end;
end;
{$ENDIF}

function GetMethodAddress(II: Pointer; MethodNum: Integer): Pointer;
begin
  Result:= Pointer(II);
  Result:= PPointer(Result)^;
  Integer(Result):= Integer(Result) + (MethodNum-1) * 4;
end;

procedure CallHostSubDirect;
var
  P, Params: array of Pointer;
  I, J, T: Integer;
  V: Variant;
  S: String;
  WS: WideString;
  Dbl: Double;

  Variants: array[0..MaxParams] of Variant;
  Integers: array[0..MaxParams] of Integer;
  Int64s: array[0..MaxParams] of Int64;
  Doubles: array[0..MaxParams] of Double;
  Singles: array[0..MaxParams] of Single;

  Reals: array[0..MaxParams] of Real;

  Currences: array[0..MaxParams] of Currency;
  Comps: array[0..MaxParams] of Comp;
  Extendeds: array[0..MaxParams] of Extended;
  Strings: array[0..MaxParams] of String;
  ShortStrings: array[0..MaxParams] of ShortString;
  WideStrings: array[0..MaxParams] of WideString;
  Pointers: array[0..MaxParams] of Pointer;
  Objects: array[0..MaxParams] of TObject;

  SO, tempSO: TPAXScriptObject;
  Instance: TObject;
  AClass: TClass;
  IntVal: Integer;
  W: TPAXScriptObjectList;
  IsLock: Boolean;
  ByteSet: TByteSet;
  ACardinal: TCardinal;
  AInt64: TInt64;
  ScriptObject: TPaxScriptObject;
  ClassRec: TPAXClassRec;
  PP: Pointer;
  Address: Pointer;
  TempClassDef: TPaxClassDefinition;
  Types: array of Integer;
  StrTypes: array of String;
  hr: HRESULT;
  PaxArray: TPaxArray;
  VT: Integer;
const
  E_NOINTERFACE = HRESULT($80004002);
begin
  SetLength(Params, ParamCount + 1);
  SetLength(P, ParamCount + 1);
  SetLength(Types, ParamCount + 1);
  SetLength(StrTypes, ParamCount + 1);

  if IsUndefined(PThis^) then
  begin
    SO := VariantToScriptObject(SymbolTable.VariantValue[SymbolTable.RootNamespaceID]);
  end
  else
    SO := VariantToScriptObject(PThis^);

  // read addresses of script-defined parameters and result
  for I:=0 to ParamCount - 1 do
    P[ParamCount - 1 - I] := GetTerminal(Stack.PopPointer);
  P[ParamCount] := PResult;

  for I:=0 to ParamCount do
  begin
    T := D.Types[I];
    Types[I] := T;

    if D.ExtraTypes[I] = typeDYNAMICARRAY then
    begin
      if I < ParamCount then
        V := Variant(P[I]^);
      Pointers[I] := PaxArrayToDynamicArray(V, D.Types[I]);
      Params[I] := Pointers[I];
    end
    else case T of
      typeVOID:
        begin
          if I < ParamCount then
          begin
            Variants[I] := Variant(P[I]^);

            if IsPaxArray(Variants[I]) then
            begin
              PaxArray := TPaxArray(VariantToScriptObject(Variants[I]).Instance);
              Pointers[I] := PaxArray.Buffer;
              Types[I] := typePOINTER;
              Params[I] := Pointers[I];
              D.ExtraTypes[I] := typeARRAY;
            end
            else if IsHostObject(Variants[I]) then
            begin
              Objects[I] := VariantToScriptObject(Variants[I]).Instance;
              Params[I] := @Objects[I];
            end
            else if VarType(Variants[I]) = varInteger then
            begin
              Types[I] := typeINTEGER;
              Integers[I] := Variants[I];
              Params[I] := @Integers[I];
            end
            else if VarType(Variants[I]) = varString then
            begin
              Types[I] := typeSTRING;
              Strings[I] := Variants[I];
              Params[I] := @Strings[I];
            end
            else if VarType(Variants[I]) = varDouble then
            begin
              Types[I] := typeDOUBLE;
              Doubles[I] := Variants[I];
              Params[I] := @Doubles[I];
            end
            else
              Params[I] := @Variants[I];
          end
          else
            Params[I] := @Variants[I];
        end;

      typeVARIANT:
      begin
        Variants[I] := Variant(P[I]^);
        Params[I] := @Variants[I];
        if I = ParamCount then
          FillChar(Params[I]^, _SizeVariant, 0);
      end;
      typeOLEVARIANT: Params[I] := P[I];
      typeINTEGER,
      typeBYTE,
      typeENUM,
      typeWORD:
      begin
        if TVarData(PVariant(P[I])^).VType = varDouble then
        begin
          PVariant(P[I])^ := Round(PVariant(P[I])^);
        end;

        Params[I] := ShiftPointer(P[I], 8);
      end;
      typeSMALLINT:
      begin
        Params[I] := ShiftPointer(P[I], 8);
        if I < ParamCount then
          SmallInt(Params[I]^) := Variant(P[I]^);
      end;
      typeSHORTINT:
      begin
        Params[I] := ShiftPointer(P[I], 8);
        if I < ParamCount then
          ShortInt(Params[I]^) := Variant(P[I]^);
      end;
      typeSTRING:
      begin
        Params[I] := @Strings[I];
        if I < ParamCount then
          Strings[I] := Variant(P[I]^)
        else
          Strings[I] := '';
      end;
      typeCARDINAL:
      begin
        Params[I] := ShiftPointer(P[I], 8);
        if I < ParamCount then
          TCardinal(Params[I]^) := ToCardinal(Variant(P[I]^));
      end;
      typeINT64:
      begin
        Params[I] := @Int64s[I];
        if I < ParamCount then
          Int64s[I] := ToInt64(Variant(P[I]^));
      end;
      typeDOUBLE:
      begin
        Params[I] := @Doubles[I];
        if I < ParamCount then
          Doubles[I] := Variant(P[I]^);
      end;
      typeSINGLE:
      begin
        Params[I] := @Singles[I];
        if I < ParamCount then
          Singles[I] := Variant(P[I]^);
      end;
      typeCURRENCY:
      begin
        Params[I] := @Currences[I];
        if I < ParamCount then
          Currences[I] := Variant(P[I]^);
      end;
      typeCOMP:
      begin
        Params[I] := @Comps[I];
        if I < ParamCount then
          Comps[I] := Variant(P[I]^);
      end;
      typeREAL48:
      begin
        Params[I] := @Reals[I];
        if I < ParamCount then
          Reals[I] := Variant(P[I]^);
      end;
      typeEXTENDED:
      begin
        Params[I] := @Extendeds[I];
        if I < ParamCount then
          Extendeds[I] := Variant(P[I]^);
      end;
      typeBOOLEAN:
      begin
        Params[I] := ShiftPointer(P[I], 8);
        if Byte(Params[I]^) <> 0 then
          Byte(Params[I]^) := 1;
      end;
      typeWORDBOOL:
      begin
        Params[I] := ShiftPointer(P[I], 8);
        if I < ParamCount then
          TWordBool(Params[I]^) := Variant(P[I]^);
      end;
      typeLONGBOOL:
      begin
        Params[I] := ShiftPointer(P[I], 8);
        if I < ParamCount then
          TLongBool(Params[I]^) := Variant(P[I]^);
      end;
      typeCHAR:
      begin
        Params[I] := @Integers[I];
        if I < ParamCount then
        begin
          S := ToString(Variant(P[I]^));
          TChar(Params[I]^) := S[1];
        end;
      end;
      typeWIDECHAR:

⌨️ 快捷键说明

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