ECMAScript icon indicating copy to clipboard operation
ECMAScript copied to clipboard

Implement new function

Open Vercix opened this issue 2 years ago • 0 comments

This pull request would implement the new function. This will create a more convenient API for instantiating JavaScript classes in GDScript.

For example:

var ControllerApp = preload("res://dist/GodotFreApp.jsx")

# Called when the node enters the scene tree for the first time.
func _ready():
	var x = ControllerApp.new(1, null, 'some_other_prop', self)
	add_child(x)

This is related to #107

There is a few things that I am not happy about with this implementation, though.

  • I created a new method for the QuickJS binder class, create_ecma_instance_for_godot_object_args, which is essentially a copy of create_ecma_instance_for_godot except that it accepts args. Which are converted into JSValues and then passed to JS_CallConstructor2. I removed the initialize_properties call in the new function which is used in the original function to set properties registered through godot.register_property. Because they are set to null when instantiating through new(). There probably is a better way of handling this though.
  • There probably should be some error checking to see if the Godot class the JavaScript class inherits from exists in emcascript.cpp.

Looking forward to review and suggestions.

Vercix avatar Jun 21 '22 13:06 Vercix