New Plugin: Run JavaScript without Leaving Lua

I just finished my new plugin QuickJs. You can try it out here https://github.com/joehinkle11/QuickJs

It’s now live on the public Solar2D directory. Add it like this.

{
    plugins = {
        ["plugin.quickjs"] = {
            publisherId = "io.joehinkle",
        },
    },
}

It lets you run JavaScript without leaving your Lua script. Here’s some examples.

Returning a value

local QuickJs = require "plugin_quickjs"
local returnedMessage = QuickJs.run {
    js = [[
        var message = "hello from js!"
        return message
    ]],
    lua = function()
        return "hello from lua!"
    end
}
print(returnedMessage) -- "hello from js!" on HTML5 builds and "hello from lua!" on all other builds

Using a callback

QuickJs.run {
    js = [[
        // wait one second
        setTimeout(function(){
            callback("hello from js!")
        }, 1000)
    ]],
    lua = function(callback)
        -- wait one second
        timer.performWithDelay(1000, function()
            callback("hello from lua!")
        end)
    end,
    callback = function(callbackMessage)
        print(callbackMessage) -- "hello from js!" on HTML5 builds and "hello from lua!" on all other builds
    end
}

Passing data

QuickJs.run {
    data = {
        hello = "hello"
    },
    js = [[
        console.log(data.hello) // "hello"
    ]],
    lua = function(data)
        print(data.hello) -- "hello"
    end
}

That’s just the basics. There’s more settings you can find in the readme on the GitHub! Hope you guys enjoy this! I made this for myself mostly because I’m going to be making more HTML5 games in Solar2D.

9 Likes

A great idea! Thanks for sharing!!!

It works great! I’m going to use it for my HTML5 games when I get the chance to do game dev again :slight_smile:

1 Like