Can I pass information from my C++ program to Lua Server Pages?

54 views
Skip to first unread message

Jurie Horneman

unread,
Mar 6, 2014, 9:11:56 AM3/6/14
to mongoos...@googlegroups.com
I was wondering if there is a way to pass arbitrary information from C++ through Mongoose so it's accessible in Lua? I want to pass some variables to Javascript and just passing them as literal Javascript in a script tag is a technique I find often works well (as opposed to asking for the data using a JSON call).

(Example: My C++ code has a variable magic_string which contains "Mongoose rocks". And I want to pass that to my Javascript code, so I want to generate this using Lua:

<script>
   var magic_string = "Mongoose rocks";
</script> )

I looked at the manual and the code but I can't figure out if there is a way. Short of using environment variables I can't think of a way of doing it from Lua either.

A way of hooking into prepare_lua_environment (e.g. passing in a list of strings) might do the trick. (No idea if that's the Mongoose way, sorry.)

Thanks in advance for any help.

Jurie

Sergey Lyubka

unread,
Mar 6, 2014, 3:16:19 PM3/6/14
to mongoose-users
Hi Jurie,
There is no mechanism currently that makes the modification of Lua state easy. You have to patch prepare_lua_environment().

Your suggestion to make a hook is a way to go - a new MG_LUA event could be introduced, and event handler can add whatever variables to the new Lua state.



--
You received this message because you are subscribed to the Google Groups "mongoose-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-user...@googlegroups.com.
To post to this group, send email to mongoos...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongoose-users.
For more options, visit https://groups.google.com/groups/opt_out.

Jurie Horneman

unread,
Mar 6, 2014, 4:50:17 PM3/6/14
to mongoos...@googlegroups.com
Hi Sergey,

> Your suggestion to make a hook is a way to go - a new MG_LUA event could be introduced, and event handler can add whatever variables to the new Lua state.

I looked into it a bit, and here is how I think one could do it:

- Add a MG_LUA_SETUP event type.
- Add a pointer to the Lua state to mg_connection, because otherwise the event handler can't access it.
- Add a call_user() call to prepare_lua_environment. The return value can be ignored.
- Expose the Lua support functions (reg_string etc.) in mongoose.h. (I haven't looked into how embedding in Lua works, I'm just saying this based on what I see in prepare_lua_environment.)

Does that make sense?

Jurie


> On Thu, Mar 6, 2014 at 2:11 PM, Jurie Horneman <jurie.h...@gmail.com> wrote:
> I was wondering if there is a way to pass arbitrary information from C++ through Mongoose so it's accessible in Lua? I want to pass some variables to Javascript and just passing them as literal Javascript in a script tag is a technique I find often works well (as opposed to asking for the data using a JSON call).
>
> (Example: My C++ code has a variable magic_string which contains "Mongoose rocks". And I want to pass that to my Javascript code, so I want to generate this using Lua:
>
> <script>
> var magic_string = "Mongoose rocks";
> </script> )
>
> I looked at the manual and the code but I can't figure out if there is a way. Short of using environment variables I can't think of a way of doing it from Lua either.
>
> A way of hooking into prepare_lua_environment (e.g. passing in a list of strings) might do the trick. (No idea if that's the Mongoose way, sorry.)
>
> Thanks in advance for any help.
>
> Jurie
>
> --
> You received this message because you are subscribed to the Google Groups "mongoose-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-user...@googlegroups.com.
> To post to this group, send email to mongoos...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mongoose-users.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "mongoose-users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongoose-users/tz-MjbEw-pY/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to mongoose-user...@googlegroups.com.

Sergey Lyubka

unread,
Mar 6, 2014, 5:56:50 PM3/6/14
to mongoose-users
On Thu, Mar 6, 2014 at 9:50 PM, Jurie Horneman <ju...@jurie.org> wrote:
Hi Sergey,

> Your suggestion to make a hook is a way to go - a new MG_LUA event could be introduced, and event handler can add whatever variables to the new Lua state.

I looked into it a bit, and here is how I think one could do it:

- Add a MG_LUA_SETUP event type.
- Add a pointer to the Lua state to mg_connection, because otherwise the event handler can't access it.
- Add a call_user() call to prepare_lua_environment. The return value can be ignored.
- Expose the Lua support functions (reg_string etc.) in mongoose.h. (I haven't looked into how embedding in Lua works, I'm just saying this based on what I see in prepare_lua_environment.)

Does that make sense?

Yes it does make sense, it's exactly how it should be done, thank you!

Jurie Horneman

unread,
Mar 7, 2014, 2:21:33 AM3/7/14
to mongoos...@googlegroups.com
Cool :) Do you want me to send a pull request? Or open an issue? I forked Mongoose but I haven't tried compiling it or writing a test program for this case.

Sergey Lyubka

unread,
Mar 7, 2014, 8:36:43 AM3/7/14
to mongoose-users
Pull request would be appreciated. If not, just open an issue please.


For more options, visit https://groups.google.com/d/optout.

Sergey Lyubka

unread,
Mar 9, 2014, 3:32:06 PM3/9/14
to mongoose-users

Jurie Horneman

unread,
Mar 9, 2014, 3:34:44 PM3/9/14
to mongoos...@googlegroups.com
Awesome! Sorry for not sending a pull request yet.

On 09 Mar 2014, at 20:32 , Sergey Lyubka <val...@gmail.com> wrote:

> Pushed https://github.com/cesanta/mongoose/commit/976b5f37c1b8592903a3a1d3e977078727bfa975
> Convenience functions are not exposed though.
>
> --
> You received this message because you are subscribed to a topic in the Google Groups "mongoose-users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongoose-users/tz-MjbEw-pY/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to mongoose-user...@googlegroups.com.
> To post to this group, send email to mongoos...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mongoose-users.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages