Billy Pilgrim
Billy Pilgrim

Reputation: 1852

How do I call a Lua script from an HTML5 script/file/page

I want to create web pages with dynamic content. I have an HTML page, and I want to call a lua script from it

  1. How do I invoke the lua script?

    <script type="text/application" >?
    <script type="text/lua" >?

  2. Retrieve data from it? Can I do something like:

    int xx = 0;
    <script type=text/lua>
       xx = 123;
    </script>
    

    and have any hope that xx will be 123 when the script exits?

  3. Replace the current web page with the content generated by the lua script.

Upvotes: 7

Views: 23555

Answers (6)

Макс Хэ
Макс Хэ

Reputation: 11

Lua won't run natively in the browser; JavaScript is the only scripting library

However, you can use the JavaScript library "Starlight", which implements Lua.

You can use it like this:

<!DOCTYPE html>
<html>
  <body>
    <!-- WARNING! This will only work if you load the starlight library later -->
    <script type="application/lua">
        print('Hello world')
    </script>

    <!-- Here we load the library for Lua -->
    <script src="//cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"></script>
    <script src="http://paulcuth.me.uk/starlight/browser-lib/starlight.js" data-run-script-tags></script>
  </body>
</html>

Upvotes: 0

Janus Troelsen
Janus Troelsen

Reputation: 21270

On the client-side, you can use:

Fengari and Moonshine execute compiled Lua bytecode. They are more compatible than lua.js, and have a lower code-size footprint than e.g. an Emscripten-compiled Lua interpreter. They may be the slowest method of all because they aren't using WASM/asm.js like the stock Lua interpreter compiled with Emscripten would, and they aren't generating JavaScript which could be subsequently JIT'ed.

I'd try using Fengari first as it seems to be most active. It has easier JS interop than something using WASM would have.

Upvotes: 16

Alexander Gladysh
Alexander Gladysh

Reputation: 41383

If you want to run Lua in browser, consider using Lua Alchemy — Lua interpreter for Flash VM.

There are also several JavaScript implementations of Lua, but they were not mature enough last time I looked.

If you want to run Lua on server, consult this answer: What web server to use for Lua web development

Upvotes: 0

Quentin
Quentin

Reputation: 943097

On the WWW scripts can run in two places.

  1. In the web browser
  2. On the web server

If you want it to run in the browser, then you need support for the language built into the browser (or provided by an extension). For all practical purposes, if you are writing webpages for the WWW, then the only language you can use in an HTML <script> is JavaScript.

If you want to run it on the web server, then you need to get your HTTPD to run the script in response to a URL being requested from it. The simplest way to achieve this is via CGI.

With CGI, the HTTPD will run a program (as a separate process) in response to the request being made. It will pass in various information about the request via STDIN and environment variables (as described in the CGI specification). The script then prints an HTTP response (header (at least a Content-Type) and body (e.g. an HTML document)) and sends it to STDOUT where the HTTPD picks it up and sends it back to the browser.

How you configure your server to run programs using CGI depends on the server. Apache has a guide for their server.

There are probably CGI libraries for Lua, but I don't know the language so cannot make any recommendations.

CGI is a slow and inefficient protocol (as it requires a new processes to be spawned for each request). There are alternatives, such as FastCGI and various language specific options. Again, I don't know what is considered optimal in Lua land.

Upvotes: 9

willrabbit
willrabbit

Reputation: 1

Most of the CGI and Lua I have played with involve generating the web page and inserting the dynamic bits, rather than calling a script from a web page. So more like option C from your original question. Any HTML 4 or 5 elements you want to have could easily be added into the generated web page.

Here are some places you can check out for more detailed information:

CGILua has some good information on how to use CGI and Lua together.

This long forum page has some good examples with code and output.

The Beginning Lua Programming book has a whole chapter walking through how to set up and use CGI and Lua. (Chapter 15 - Programming for the Web)

(While some of these places are a bit dated, they still do a good job of showing how to do this sort of thing.)

Remember: If you are using cgi or fastcgi on the server side you will need the first line of your Lua file to have a pointer to wherever the Lua interpreter is, such as:

#!/usr/local/bin/lua

Upvotes: 0

kikito
kikito

Reputation: 52621

If you want to run a script from a browser, consider using javascript instead.

It is very similar to Lua, and unlike Lua it's interpreted by most browsers.

Upvotes: -1

Related Questions