Skip to content

Instantly share code, notes, and snippets.

@foxicode
foxicode / jsPluginCode.js
Last active April 18, 2020 07:34
Sample JS plugin for iOS app
const circle1 = 1;
const circle2 = 2;
const circle3 = 3;
let time = 0.0;
let step = 0;
function tick(delta) {
time += delta;
if (time >= 1000) {
@foxicode
foxicode / luaPluginCode.lua
Created May 12, 2020 20:35
Sample Lua script
local circle1 = 1
local circle2 = 2
local circle3 = 3
local time = 0.0
local step = 0
function tick(delta)
time = time + delta
if (time >= 1000) then
@foxicode
foxicode / addWidget.swift
Created May 12, 2020 21:11
addWidget function to call from Lua script
vm.globals["addWidget"] = vm.createFunction([
String.arg, // widget name
Int64.arg, // tag
Number.arg, // x coordinate
Number.arg, // y coordinate
Number.arg, // radius
String.arg // color hex string
]) { (args) -> SwiftReturnValue in
let (widgetName, widgetTag, xCoordinate, yCoordinate, radius, hexColor) = (
args.string, args.integer, args.number, args.number, args.number, args.string
@foxicode
foxicode / updateProperty.swift
Created May 12, 2020 21:11
updateProperty function to call from Lua script
vm.globals["updateProperty"] = vm.createFunction([
Int64.arg, // tag
String.arg, // attribute name
String.arg // attribute value
]) { (args) -> SwiftReturnValue in
let (widgetTag, attributeName, attributeValue) = (
args.integer, args.string, args.string
)
guard let w = self.vPluginContainer.viewWithTag(Int(widgetTag)) else { return .nothing }
@foxicode
foxicode / readLuaFile.swift
Created May 12, 2020 21:20
Reading Lua file
guard let luaSourcePath = Bundle.main.path(forResource: "luaPluginCode", ofType: "lua") else {
print("LUA file not found")
return
}
do {
let luaCode = try String(contentsOfFile: luaSourcePath)
let result = vm.createFunction(luaCode)
switch result {
case .value(let f):
@foxicode
foxicode / JSViewController.swift
Last active May 12, 2020 21:26
ViewController using JavaScript
import UIKit
import JavaScriptCore
class JSViewController: UIViewController {
@IBOutlet weak var vPluginContainer: UIView!
let jsContext = JSContext()!
var timer: Timer? = nil
@foxicode
foxicode / LuaViewController.swift
Created May 12, 2020 21:26
ViewController using Lua
import UIKit
import Lua
class LuaViewController: UIViewController {
@IBOutlet weak var vPluginContainer: UIView!
let vm = Lua.VirtualMachine()
var startFunc: Function?
var tickFunc: Function?
var timer: Timer?
@foxicode
foxicode / Lights.swift
Created May 25, 2020 16:46
Dynamic iOS module
import Foundation
let circle1 = 1
let circle2 = 2
let circle3 = 3
var time = 0.0
var step = 0
public var updateProperty: (_ tag: Int, _ property: String, _ value: Any) -> Void = { (_, _, _) in }
@foxicode
foxicode / FrameworkViewController.swift
Created May 25, 2020 16:49
FrameworkViewController
import UIKit
import DynamicLights
class FrameworkViewController: UIViewController {
@IBOutlet weak var vPluginContainer: UIView!
var timer: Timer?
@IBAction func runPlugin() {
DynamicLights.updateProperty = { [weak self] (tag, property, value) in
@foxicode
foxicode / Lights.swift
Created May 25, 2020 18:39
Version of Lights.swift for dynamic linking
import Foundation
let circle1 = 1
let circle2 = 2
let circle3 = 3
var time = 0.0
var step = 0
var updateProperty: (_ tag: Int, _ property: String, _ value: Any) -> Void