Calling object prototype functions does not work. #25

Open
opened 2020-01-10 06:09:19 +00:00 by JernejL · 3 comments
JernejL commented 2020-01-10 06:09:19 +00:00 (Migrated from github.com)

I'm really determined to use besen in my project, and am still working on my game with it and writing a tutorial for others to do so, but i have a odd issue which i cannot figure out how to do properly, but it's most likely a bug:

I have registered native object:

BesenInst.RegisterNativeObject('Actor', TActorInterface);
The object has a property for OnCreate:

property OnCreate : TBESENObjectFunction read FOnCreate write FOnCreate;
In Javascript i've created an additional function on class:

Actor.prototype.OnCreate = function(OtherPlayer, AIClass) {
	
	console.log('actor_stack pushing - Actor.OnCreate')
	actor_stack.push(self);
	
}

Inside the constructor, i am attempting to call this function:

procedure TActorInterface.ConstructObject(const ThisArgument: TBESENValue; Arguments: PPBESENValues; CountArguments: integer);

...

	if Assigned(FOnCreate) then begin

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

	    try

	        AResult.ValueType := bvtBOOLEAN;
		    OnCreate.Call(BESENObjectValue(OnCreate), @CallParams, 3, AResult);

	    except

	       on e: exception do HandleBesenException(e);

	    end;

		TBESEN(BesenInst).GarbageCollector.Unprotect(OnCreate);

	end; 

This however does not work as planned, it does seem to work if a function is added on an instance of class itself - but how do i call a prototype method added in javascript - Assigned(FOnCreate) check fails and nothing is called.

Omiting assigned check does not work either.

I've also tried to check prototype - Prototype.HasProperty('OnCreate') to no avail.

I'm not totally sure how i should be handling this, any help will be appreciated.

I'm really determined to use besen in my project, and am still working on my game with it and writing a tutorial for others to do so, but i have a odd issue which i cannot figure out how to do properly, but it's most likely a bug: I have registered native object: `BesenInst.RegisterNativeObject('Actor', TActorInterface); ` The object has a property for OnCreate: `property OnCreate : TBESENObjectFunction read FOnCreate write FOnCreate; ` In Javascript i've created an additional function on class: ``` Actor.prototype.OnCreate = function(OtherPlayer, AIClass) { console.log('actor_stack pushing - Actor.OnCreate') actor_stack.push(self); } ``` Inside the constructor, i am attempting to call this function: ``` procedure TActorInterface.ConstructObject(const ThisArgument: TBESENValue; Arguments: PPBESENValues; CountArguments: integer); ... if Assigned(FOnCreate) then begin TBESEN(BesenInst).GarbageCollector.Protect(OnCreate); try AResult.ValueType := bvtBOOLEAN; OnCreate.Call(BESENObjectValue(OnCreate), @CallParams, 3, AResult); except on e: exception do HandleBesenException(e); end; TBESEN(BesenInst).GarbageCollector.Unprotect(OnCreate); end; ``` This however does not work as planned, it does seem to work if a function is added on an instance of class itself - but how do i call a prototype method added in javascript - Assigned(FOnCreate) check fails and nothing is called. Omiting assigned check does not work either. I've also tried to check prototype - Prototype.HasProperty('OnCreate') to no avail. I'm not totally sure how i should be handling this, any help will be appreciated.
Coldzer0 commented 2020-01-10 14:44:02 +00:00 (Migrated from github.com)

Hello @JernejL

In my case i defined
https://github.com/Coldzer0/Cmulator/blob/master/Core/jsplugins_bengine.pas#L20
https://github.com/Coldzer0/Cmulator/blob/master/Core/jsplugins_bengine.pas#L29

then assigned it on JS code then check if it's not nil and finally call it

Hello @JernejL In my case i defined https://github.com/Coldzer0/Cmulator/blob/master/Core/jsplugins_bengine.pas#L20 https://github.com/Coldzer0/Cmulator/blob/master/Core/jsplugins_bengine.pas#L29 then assigned it on JS code then check if it's not nil and finally call it
Coldzer0 commented 2020-01-10 14:48:56 +00:00 (Migrated from github.com)

Plus you can check my project for QJS (is a JS Engine with Almost Complete ES2019 & Alot of ES2020 support)

https://github.com/Coldzer0/QuickJS-Pascal

It's very easy to use, I already updated my Cmulator project to use it.

Plus you can check my project for QJS (is a JS Engine with Almost Complete ES2019 & Alot of ES2020 support) https://github.com/Coldzer0/QuickJS-Pascal It's very easy to use, I already updated my Cmulator project to use it.
JernejL commented 2020-01-10 15:13:10 +00:00 (Migrated from github.com)

@Coldzer0 thanks - i did find your project and am trying to learn from it as much as possible, as it is a rare project using besen.

I also use that approach that you described and it works, the problem that i have is, that this cannot be used to call prototype functions - it works well for individual instances only, and it means copying the function to each newly created object to achieve that.

I added a function to main instance of object - by appending a function to Actor.prototype - and that itself cannot be called or reached from inside besen.

quickjs looks interesting, but so far i'll try to stick with besen - i think it has a lot of potential, it just lacks more complete examples on integration and usage scenatios.

@Coldzer0 thanks - i did find your project and am trying to learn from it as much as possible, as it is a rare project using besen. I also use that approach that you described and it works, the problem that i have is, that this cannot be used to call prototype functions - it works well for individual instances only, and it means copying the function to each newly created object to achieve that. I added a function to main instance of object - by appending a function to Actor.prototype - and that itself cannot be called or reached from inside besen. quickjs looks interesting, but so far i'll try to stick with besen - i think it has a lot of potential, it just lacks more complete examples on integration and usage scenatios.
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#25
No description provided.