I've made a quick way to call JavaScript inside Solar2D (formerly Corona SDK). It is open-source, so feel free to make a pull request at https://github.com/joehinkle11/quickjs/
{
plugins = {
["plugin.quickjs"] = {
publisherId = "io.joehinkle",
},
},
}
Import
local QuickJs = require "plugin.solarquickjs"
Returning a value
local returnedMessage = QuickJs.run {
js = [[
return "hello from js!"
]],
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
}
Using a callback and returning a value
local returnedMessage = 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
}
print(returnedMessage) -- "hello from js!" on HTML5 builds and "hello from lua!" on all other builds
Passing data
QuickJs.run {
data = {
hello = "hello"
},
js = [[
console.log(data.hello) // "hello"
]],
lua = function(data)
print(data.hello) -- "hello"
end
}
All together now! 🎉
local returnedMessage = QuickJs.run {
data = {
hello = "hello"
},
js = [[
var message = data.hello + " from js!"
// wait one second
setTimeout(function(){
callback(message)
}, 1000)
return message
]],
lua = function(data, callback)
local message = data.hello.." from lua!"
-- wait one second
timer.performWithDelay(1000, function()
callback(message)
end)
return message
end,
callback = function(callbackMessage)
print(callbackMessage) -- "hello from js!" on HTML5 builds and "hello from lua!" on all other builds
end
}
print(returnedMessage) -- "hello from js!" on HTML5 builds and "hello from lua!" on all other builds
Force JavaScript (non-HTML5 builds still will use Lua, but print a warning)
QuickJs.run {
runJs = true,
js = [[
// js code here
]],
lua = function()
-- lua code here
end
}
Force Lua
QuickJs.run {
runJs = false,
js = [[
// js code here
]],
lua = function()
-- lua code here
end
}
When the callback in a JavaScript function is called, I automatically clean the reference to the callback in Lua. This means you can only call the JavaScript callback once. If you wish to disable this, set neverCleanJsCallback
to true
and you can make the callback as many times as you want.
QuickJs.run {
neverCleanJsCallback = true,
js = [[
callback(1)
callback(2)
callback(3)
]],
lua = function(callback)
callback(1)
callback(2)
callback(3)
end,
callback = function(num)
print(num) -- 1, 2, 3
end
}
Platform | Details |
---|---|
Web/HTML5 | defaults to JavaScript, but you can force Lua |
iOS | always runs Lua |
iOS Simulator | always runs Lua |
Android | always runs Lua |
TVOS | always runs Lua |
TVOS Simulator | always runs Lua |
Corona Simulator (Mac) | always runs Lua |
Corona Simulator (Windows) | always runs Lua |