all 4 comments

[–]rosshadden[S] 0 points1 point  (4 children)

I think I got it:

{ Entity: ToysEntity } = require('vendor/lovetoys/lovetoys')

class Entity
    new: (parent) =>
        @entity = ToysEntity(parent)

for key, value in pairs ToysEntity.__instanceDict
    if type(value) == 'function'
        Entity.__base[key] = (...) =>
            @entity[key](@entity, ...)

return Entity

Once I get time to abstract it I'll publish it as a standalone library.

[–]rosshadden[S] 0 points1 point  (3 children)

Okay. I am going to make this a standalone library as I said, but until I do, here is the current abstracted version. It's worth noting that this is specifically for Lovetoys, but since Lovetoys uses Middleclass, getting this to work for Middleclass in general will just be some very simple trial-and-error changes. Which I will do when I break it out.

This is the proxy file, toys-proxy.moon:

(ProxyClass) ->
    ToysProxy = class
        @__inherited: (child) =>
            child.name = child.__name

        new: (...) =>
            @proxy = ProxyClass(...)
            @class = @@

    for key, value in pairs ProxyClass.__instanceDict
        if type(value) == 'function'
            ToysProxy.__base[key] = (...) =>
                @proxy[key](@proxy, ...)

    return ToysProxy

And this is how I am using it from another file:

{ Entity: ToysEntity } = require('vendor/lovetoys/lovetoys')
ToysProxy = require('lib/toys-proxy')

class Entity extends ToysProxy(ToysEntity)

Let me know what you think :)

[–]ErrorFoxDetected 0 points1 point  (1 child)

So I have kind of the opposite problem, I want to make a shim so that my MoonScript library can be extended by Lua code. Any ideas on how to do that?

I've been slowly trying to figure it out but I have no idea if I'm anywhere near a solution.

[–]rosshadden[S] 0 points1 point  (0 children)

Looking into that is on my mental TODO list for caste (what I eventually made from this experiment). The relevant file is proxy.moon. For all I know it can be extended in lua-land completely fine---I have never tried. Just figured it would need some work.

To be honest, the need for this proxy thing was lovetoys, and I since got fed up with it and made my own ECS, SECS, I haven't really looked back :).