Giter Site home page Giter Site logo

Comments (5)

markmeeus avatar markmeeus commented on September 2, 2024

I'm quite sure encoding the funtion is not what I need to do here. It's not a erl function.
It looks like call_chunk is the function I need to call, but when I do that:

{:lua_error, {:undefined_function, #Function<6.103755144/1 in :luerl.decode/3>},

I inspected the luerl_state and indeed, that function is not in there.

from luerl.

markmeeus avatar markmeeus commented on September 2, 2024

I think I may have pinpointed the problem
In luerl.erl line 387 there is this function def:

decode(#funref{}=Fun, State, _) ->
    F = fun(Args) ->
		{Args1, State1} = encode_list(Args, State),
		{Ret, State2} = luerl_emul:functioncall(Fun, Args1, State1),
		decode_list(Ret, State2)
	end,
    F;	

If I read this correctly, an internal function is decoded to an external function by wrapping it in a function that encodes the args and decodes the result.
But it only returns the result, discarding the new state.

so instead of
decode_list(Ret, State2) it should return {decode_list(Ret, State2), State2}

However, that would be a breaking change....
I will try to take the unencoded route and implement this wrapper myself for now

from luerl.

markmeeus avatar markmeeus commented on September 2, 2024

I think I found a way to have it working in a backwards compatible way

In the luerl.erl file:

decode(#funref{}=Fun, State, _) ->
    F = fun([#luerl{}=NewSt|Args]) ->
            {Args1, State1} = encode_list(Args, NewSt),
            {Ret, State2} = luerl_emul:functioncall(Fun, Args1, State1),
            {decode_list(Ret, State2), State2};
        (Args) ->
            {Args1, State1} = encode_list(Args, State),
            {Ret, State2} = luerl_emul:functioncall(Fun, Args1, State1),
            decode_list(Ret, State2)
	end,
    F;						%Just a bare fun

This way, the function can be called from within erlang with a more recent state than when the function was passed from lua to erl.
Also, when passing a new luerl state, a new luerl state will be returned as the second tuple value.

Would this change be acceptable in a PR? If so, I'll be happy to create one.

from luerl.

markmeeus avatar markmeeus commented on September 2, 2024

After some thought and investigation I think there are a few major concerns with this approach.

First, constructing the function like this, the old state will have to kept around in Erlang memory because it is used by the returned function.

If the Lua script would call schedule many times, it would mean a separate copy of luerl state is kept in memory for as long as the functions are alive in Erlang land. This could start to add up quite quickly.

This got me thinking about GC, also in Lua land, because if Lua doesn't know Erlang is referencing this function, and Lua isn't, it would probably cause some problems.
And indeed, calling gc on luerl causes an error when I call the function with the gc'ed state... (The script above is fine, but passing an anonymous function to schedule isn't.

I'll have to do some more research to see if there is a way around this.

(I have tried implementing the schedule function entirely in Lua, keeping the function in a global table, and passing the key to erlang, but for some reason, I seem to be getting errors after GC in this scenario as well, strange ...)

from luerl.

markmeeus avatar markmeeus commented on September 2, 2024

Closing this ticket because it all comes down to not using the encoded path.

from luerl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.