class procedures are not procedures of object ! lazarus / 62-General ---------------------------------------------------------------------- guest58172 · 2015-11-21 01:17 Class procedures work without a context pointer. This is well know, for example you won't be able to access a variable that's itself not a class var. However if you take a pointer to a class procedure, a program will allocate a fat pointer made of the entry point (address in process image) and object address (instance of the object). But the second pointer (instance of the object) is totally superfluous. The problem is that we won't be able to cast a pointer to class procedure as a pointer to a free procedure, even if it's actually one. We should be able to do it, as shown in this example, the object instance is not needed at all: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;{$ASMMODE INTEL}type  TProc = procedure();  TTempProc = procedure() of object;  TFoo = class    class var a: integer;    class procedure foo();  end; var  proc: TProc;  tempproc: TTempProc;  Foo: TFoo; class procedure TFoo.foo();begin  writeln('works');  a := 8;  writeln(a);end; function toProc(a: pointer): TProc; assembler;asm  {$IFDEF CPU64}  mov rax, [a];  {$ELSE}  mov eax, [a];  {$ENDIF}end; begin  Foo := TFoo.Create;   // not allowed because this returns a procedure of object  //proc := toProc(@tempproc);   tempproc := @Foo.foo;  proc := toProc(@tempproc);   proc();  readln;end. So what's the rationale for the pointers to class proc being some pointer to object proc ? It looks like an error, no ? Do you have a counter example where the cast would be unsafe ? Otherwise, I'll file an issue. ---------------------------------------------------------------------- Blaazen · 2015-11-21 02:19 I simply retyped it and it works, without asm: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1;{$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls; type  { TForm1 }  TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  private    { private declarations }  public    { public declarations }  end;   TProc = procedure;   { TFoo }  TFoo = class    class procedure cp;  end; var  Form1: TForm1;  p: TProc; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin  p:=TProc(@Tfoo.cp);  p;  //click button writes out "Done"end; { TFoo } class procedure TFoo.cp;begin  writeln('Done');end; end. ---------------------------------------------------------------------- guest58172 · 2015-11-21 08:00 I haven't noticed that it worked when taking the pointer from the type (you use @TFoo no @Foo). But there is definitively still two bugs: - it should work when taking the pointer from Foo. Even if it's an instance, the class proc doesn't become a proc of object. - it should work without explicit cast. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i proc := TProc(@TFoo.foo); // ok but should work without explicit castproc := TProc(@Foo.foo);  //project1.lpr(40,11) Error: Illegal type conversion: "" to ""  foo is still a class proc. The compiler should detect this, there's a hole somewhere. ---------------------------------------------------------------------- Thaddy · 2015-11-21 09:53 AFAIK Florian fixed something like this last weekend in trunk. Let's have a quick test: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program testme2;{$mode objfpc}{$H+}type  TProc = procedure;  { TFoo }  TFoo = class    class procedure cp; //static;  end; var  p: TProc; procedure testmeproc;begin  p:=TProc(@Tfoo.cp);  p;end; { TFoo } class procedure TFoo.cp;begin  writeln('Done');end; begin  TestmeProc;  readln;end. Ergo: this is fixed in trunk. I tested with r32385. ---------------------------------------------------------------------- Jonas Maebe · 2015-11-21 10:30 Quote from: BBasile on November 21, 2015, 02:17:46 amClass procedures work without a context pointer. All class methods have a hidden "self" pointer, which is a "class of " of the class used to call the class used to invoke it. That is why taking the address of a class method results in a "procedure of object". This self pointer is mainly used when calling virtual class methods. If you have a class method that you do not want to get a self pointer, you have to declare it as "static;". The bug Thaddy is referring to can be found at http://bugs.freepascal.org/view.php?id=27414 (which means that in current non-trunk versions, getting the address of a static class method *in objfpc mode* still returned a procedure of object, which was wrong). ---------------------------------------------------------------------- Thaddy · 2015-11-21 10:47 The case Jonas pointed out is already in my example, but commented out. Just remove the slashes.