NativeObject string properties #9

Open
opened 2016-06-21 15:56:08 +00:00 by lazuser · 3 comments
lazuser commented 2016-06-21 15:56:08 +00:00 (Migrated from github.com)

Hi Benjamin!
BESEN does not see native object's string properties.
Or maybe I did something wrong?

program BESENTest;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  BESEN,
  BESENValue,
  BESENObject,
  BESENConstants,
  BESENNativeObject;

type
  TScriptSys = class
  public
    procedure print(const ThisArgument: TBESENValue; Arguments: PPBESENValues; CountArguments: integer; var AResult:TBESENValue);
  end;

  TTestClass = class(TBESENNativeObject)
  private
    fTestKey: String;
  protected
    procedure ConstructObject(const ThisArgument: TBESENValue; Arguments: PPBESENValues; CountArguments: integer); Override;
  public
    constructor Create(AInstance: TObject; APrototype: TBESENObject=nil; AHasPrototypeProperty: longbool=false); Overload; Override;
  published
    property testKey: String read fTestKey;
  end;

var
  BesenInst: TBesen;
  ScriptSys: TScriptSys;

procedure TScriptSys.print(const ThisArgument: TBESENValue; Arguments: PPBESENValues; CountArguments: Integer; var AResult: TBESENValue);
begin
  writeLn(TBESEN(BesenInst).ToStr(Arguments^[0]^));
end;

constructor TTestClass.Create(AInstance: TObject; APrototype: TBESENObject=nil; AHasPrototypeProperty: longbool=false);
begin
  inherited Create(AInstance, APrototype, AHasPrototypeProperty);
  fTestKey := 'test string value';
end;

procedure TTestClass.ConstructObject(const ThisArgument: TBESENValue; Arguments: PPBESENValues; CountArguments: integer);
begin
  inherited ConstructObject(ThisArgument, Arguments, CountArguments);
end;

begin
  BesenInst := TBesen.Create(COMPAT_JS);
  ScriptSys := TScriptSys.Create;
  BesenInst.ObjectGlobal.RegisterNativeFunction('print', ScriptSys.Print, 1, []);
  BesenInst.RegisterNativeObject('Test', TTestClass);
  BesenInst.Execute(
    'var obj = new Test();' +
    'print(obj.testKey);' +
    'print(JSON.stringify(obj));'
  );
  readLn;
end.
Hi Benjamin! BESEN does not see native object's string properties. Or maybe I did something wrong? ``` program BESENTest; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, BESEN, BESENValue, BESENObject, BESENConstants, BESENNativeObject; type TScriptSys = class public procedure print(const ThisArgument: TBESENValue; Arguments: PPBESENValues; CountArguments: integer; var AResult:TBESENValue); end; TTestClass = class(TBESENNativeObject) private fTestKey: String; protected procedure ConstructObject(const ThisArgument: TBESENValue; Arguments: PPBESENValues; CountArguments: integer); Override; public constructor Create(AInstance: TObject; APrototype: TBESENObject=nil; AHasPrototypeProperty: longbool=false); Overload; Override; published property testKey: String read fTestKey; end; var BesenInst: TBesen; ScriptSys: TScriptSys; procedure TScriptSys.print(const ThisArgument: TBESENValue; Arguments: PPBESENValues; CountArguments: Integer; var AResult: TBESENValue); begin writeLn(TBESEN(BesenInst).ToStr(Arguments^[0]^)); end; constructor TTestClass.Create(AInstance: TObject; APrototype: TBESENObject=nil; AHasPrototypeProperty: longbool=false); begin inherited Create(AInstance, APrototype, AHasPrototypeProperty); fTestKey := 'test string value'; end; procedure TTestClass.ConstructObject(const ThisArgument: TBESENValue; Arguments: PPBESENValues; CountArguments: integer); begin inherited ConstructObject(ThisArgument, Arguments, CountArguments); end; begin BesenInst := TBesen.Create(COMPAT_JS); ScriptSys := TScriptSys.Create; BesenInst.ObjectGlobal.RegisterNativeFunction('print', ScriptSys.Print, 1, []); BesenInst.RegisterNativeObject('Test', TTestClass); BesenInst.Execute( 'var obj = new Test();' + 'print(obj.testKey);' + 'print(JSON.stringify(obj));' ); readLn; end. ```
JernejL commented 2018-02-13 07:14:42 +00:00 (Migrated from github.com)

It works for me, i tried a string and integer property, it seems to work well, but property has to be published, which seems you did correct, maybe you need to re-test as this could be something that bero fixed?

Also, which compiler did you use? i used this in delphi 7 and it worked all ok.

It works for me, i tried a string and integer property, it seems to work well, but property has to be published, which seems you did correct, maybe you need to re-test as this could be something that bero fixed? Also, which compiler did you use? i used this in delphi 7 and it worked all ok.
3FLLC commented 2020-09-28 14:09:15 +00:00 (Migrated from github.com)

@lazuser - Make sure {$M+} is enabled in your code... which turns on RTTI so the code can crawl the published properties. Otherwise, they are optimized out... ( I know this is 4 yr old question, but n00bs may hit this).

@lazuser - Make sure {$M+} is enabled in your code... which turns on RTTI so the code can crawl the published properties. Otherwise, they are optimized out... ( I know this is 4 yr old question, but n00bs may hit this).
JernejL commented 2020-10-02 07:30:02 +00:00 (Migrated from github.com)

I suppose {$typeInfo on} is same.

Bero could fix this by extending TBESENBaseObject from Tpersistent instead from TObject ( https://www.freepascal.org/docs-html/rtl/classes/tpersistent.html ) which is always in {$M+}

I suppose {$typeInfo on} is same. Bero could fix this by extending TBESENBaseObject from Tpersistent instead from TObject ( https://www.freepascal.org/docs-html/rtl/classes/tpersistent.html ) which is always in {$M+}
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
BeRo1985/besen#9
No description provided.