wren-lang / wren

The Wren Programming Language. Wren is a small, fast, class-based concurrent scripting language.
http://wren.io
MIT License
6.89k stars 552 forks source link

How do I pass a foreign object to a function call? #1179

Closed DCubix closed 1 year ago

DCubix commented 1 year ago

I currently have this script:

class Test {
    construct new() {}

    static on_create(object) {
        System.print(object.name)
        System.print("Hello from on_create")
    }

    static on_update(object, dt) {

    }

    static on_destroy(object) {

    }
}

But I have no idea how can I call the methods from the C API and pass a foreign object to them.

I call them like this from the engine:

void de_script_component_create_func(de_scene_t* scene, de_object_t* object, void* comp) {
    de_script_t* script = (de_script_t*)comp;
    if (script->on_create) {
        wrenEnsureSlots(script->vm, 3);
        wrenSetSlotHandle(script->vm, 0, script->receiver);
        wrenSetSlotHandle(script->vm, 1, script->object_class);

        de_object_t** obj_ptr = (de_object_t**)wrenSetSlotNewForeign(script->vm, 2, 1, sizeof(de_object_t*));
        *obj_ptr = object;

        wrenCall(script->vm, script->on_create);
    }
}

However, it's passing 3 parameters to it, so "object" is the class... So the call becomes on_create(object_class, object).

Is there a workaround for this? And also, wouldn't it make more sense for wrenSetSlotNewForeign to pop that class type from the stack?

CrazyInfin8 commented 1 year ago

Currently your script has on_create defined as static on_create(object). This means that script->receiver must be the Test class itself and the signature passed to wrenMakeCallHandle (which returns the value used for script->on_create) should be "on_create(_)" (single underscore for single parameter).

Your engine ensures 3 slots, extra slots should just be ignored when calling wrenCall so slot 0 is the receiver and slot 1 is the value for object (slot 2 is extra so it would just get ignored).

If you want to pass just the foreign object to the function parameter, you could put the object class in slot 2 and create the foreign function in slot 1 instead using wrenSetSlotNewForeign (might be able to get away with putting the object class in slot 1 then replacing it with the foreign object to the same slot but you should test that first)

if you want both the object class as well as the foreign object passed to on_create, you'll need to create a method with signature: "on_create(_,_)" (2 underscores for 2 parameters, also you'd use this signature for wrenMakeCallHandle instead). This allows the value in slot 2 to be used as the second parameter.

I'm not completely certain I fully understand the situation so feel free to clarify if I misunderstood the issue.

DCubix commented 1 year ago

If you want to pass just the foreign object to the function parameter, you could put the object class in slot 2 and create the foreign function in slot 1 instead using wrenSetSlotNewForeign (might be able to get away with putting the object class in slot 1 then replacing it with the foreign object to the same slot but you should test that first)

@CrazyInfin8 oooh that worked just fine! Thank you!

What if I want to remove the static from the functions and do something like Lua's metatables for the foreign object where I can implement new functionality in runtime?

So instead of doing like I did with the static methods, I have something like this:

class Test {
    construct new() {}

    on_create() {
        System.print(this.name) // this = the foreign object
        System.print("Hello from on_create")
    }

    on_update(dt) {

    }

    on_destroy() {

    }
}
CrazyInfin8 commented 1 year ago

to remove static from the functions, you'd need an instance to call them first. You can create an instance using one of the following methods

After you've done one of these, you can proceed to use that instance as the new receiver (instead of using the class itself) when calling on_create, on_update, on_destroy.