BeRo1985 / besen

Complete ECMAScript Fifth Edition Implemention in Object Pascal
GNU Lesser General Public License v2.1
214 stars 48 forks source link

Creating besen objects from application side + passing to functions #19

Open JernejL opened 5 years ago

JernejL commented 5 years ago

type..

TActorInterface = class(TBESENNativeObject)

class is registered properly so it can be prototyped from:

BesenInst.RegisterNativeObject('Actor', TActorInterface);

So i try to create a object on my side, and pass it as parameter to javascript

    JsPlayer: TBESENValue;

    AResult: TBESENValue;
    ta: Tactor;
    JsCreatedPlayer: TActorInterface;

begin

                TBESEN(BesenInst).GarbageCollector.Protect(OnCreatePlayer);

                try
                   AResult.ValueType := bvtBOOLEAN;

                   ta:= Tactor.GetFromIndex(PlayerID);

                   JsCreatedPlayer:= TActorInterface.Create(BesenInst, TBESEN(BesenInst).ObjectPrototype, false);
                   JsCreatedPlayer.SetHandle(ta.GetHandle());

                   TBESEN(BesenInst).GarbageCollector.Add(JsCreatedPlayer);
                   TBESEN(BesenInst).GarbageCollector.Protect(TBESENObject(JsCreatedPlayer));

                   a[0]:= @JsPlayer;
                   JsPlayer := BESENObjectValue(TBESENObject(JsCreatedPlayer));
                   // todo: add parameter player ID

                   OnCreatePlayer.Call(BESENObjectValue(OnCreatePlayer), @a, 2, AResult);

                except
                   on e: EBESENError do
                   begin
                     addtochat(Format('%s ( Line %d ): %s', [e.Name, TBESEN(BesenInst).LineNumber, e.Message]));
                   end;

                   on e: exception do
                   begin
                     addtochat(Format('%s ( Line %d ): %s', ['Exception', TBESEN(Instance).LineNumber, e.Message]));
                   end;
                end;

If i print the object it's: [object TActorInterface$Constructor] {}

Looks like some sort of empty object, it doesn't function properly in javascript (cannot change properties, etc.. ).

I've based my code on what i saw in https://github.com/Coldzer0/Cmulator/ project, i'm guessing i did something very wrong.

I'd just like to create an instance of native object, and pass it to function, so that properties and all can be correctly used as if it was created within javascript.

If you have a better method of contacting i'll take it, as i'm abusing tickets for help / support :( i'm in #besen channel but you are usually not reachable there.

BeRo1985 commented 5 years ago

Can you upload a complete test project with your issue?

JernejL commented 5 years ago

I will try to make an isolated example.

As for object creation logic, does that seem correct way to do it at all?

JernejL commented 5 years ago

Ok, here is a whole self-contained lazarus project that does what i experimented with, binary and all code included:

https://gtamp.com/tdc/besenexperiment.7z

I'm sure i'm doing a lot of things wrong or things could be done better, so i will take any kind of feedback on how to integrate besen better / properly.

I'm still learning on how besen works, but maybe out of this i can build a few more detailed examples and some nice documentation.

JernejL commented 5 years ago

Writing again, just as notification.. i made the example and linked it here.