Skip to content

Commit

Permalink
Added Lua and Lua Script node support
Browse files Browse the repository at this point in the history
  • Loading branch information
markusmoenig committed Dec 15, 2021
1 parent fb79f07 commit aee23f9
Show file tree
Hide file tree
Showing 82 changed files with 23,103 additions and 48 deletions.
366 changes: 360 additions & 6 deletions ShaderMania.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions Shared/Bridge.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// Bridge.h
// ShaderMania
//
// Created by Markus Moenig on 15/12/21.
//

#ifndef Bridge_h
#define Bridge_h

#import "Metal.h"

// Lua

@import Foundation;

extern int SDegutisLuaRegistryIndex;

//! Project version number for LuaSource.
FOUNDATION_EXPORT double LuaSourceVersionNumber;

//! Project version string for LuaSource.
FOUNDATION_EXPORT const unsigned char LuaSourceVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <LuaSource/PublicHeader.h>

#import "lapi.h"
#import "lauxlib.h"
#import "lcode.h"
#import "lctype.h"
#import "ldebug.h"
#import "ldo.h"
#import "lfunc.h"
#import "lgc.h"
#import "llex.h"
#import "llimits.h"
#import "lmem.h"
#import "lobject.h"
#import "lopcodes.h"
#import "lparser.h"
#import "lprefix.h"
#import "lstate.h"
#import "lstring.h"
#import "ltable.h"
#import "ltm.h"
#import "lua.h"
#import "luaconf.h"
#import "lualib.h"
#import "lundump.h"
#import "lvm.h"
#import "lzio.h"

#endif /* Bridge_h */
17 changes: 17 additions & 0 deletions Shared/Compiler/LuaCompiler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// LuaCompiler.swift
// ShaderMania
//
// Created by Markus Moenig on 15/12/21.
//

import Foundation

class LuaCompiler
{
let model : Model

init(_ model: Model) {
self.model = model
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ class ShaderCompiler
if errorArr.count > 0 {
errorText = String(errorArr[0])
}
if arr.count >= 4 && Int32(arr[0]) != nil && Int32(arr[1]) != nil {
if arr.count >= 4 && Int32(arr[0]) != nil {
var er = CompileError()
er.node = node
er.line = Int32(arr[0])! - lineNumbers - 1
Expand Down
15 changes: 15 additions & 0 deletions Shared/Lua/Boolean.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

extension Bool: Value {

public func push(_ vm: VirtualMachine) {
lua_pushboolean(vm.vm, self ? 1 : 0)
}

public func kind() -> Kind { return .boolean }

public static func arg(_ vm: VirtualMachine, value: Value) -> String? {
if value.kind() != .boolean { return "boolean" }
return nil
}

}
70 changes: 70 additions & 0 deletions Shared/Lua/ExtraTypes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import CoreGraphics

extension CGPoint: Value {

public func push(_ vm: VirtualMachine) {
let t = vm.createTable()
t["x"] = Double(self.x)
t["y"] = Double(self.y)
t.push(vm)
}

public func kind() -> Kind { return .table }

fileprivate static let typeName: String = "point (table with numeric keys x,y)"
public static func arg(_ vm: VirtualMachine, value: Value) -> String? {
if value.kind() != .table { return typeName }
if let result = Table.arg(vm, value: value) { return result }
let t = value as! Table
if !(t["x"] is Number) || !(t["y"] is Number) { return typeName }
return nil
}

}

extension CGSize: Value {

public func push(_ vm: VirtualMachine) {
let t = vm.createTable()
t["w"] = Double(self.width)
t["h"] = Double(self.height)
t.push(vm)
}

public func kind() -> Kind { return .table }

fileprivate static let typeName: String = "size (table with numeric keys w,h)"
public static func arg(_ vm: VirtualMachine, value: Value) -> String? {
if value.kind() != .table { return typeName }
if let result = Table.arg(vm, value: value) { return result }
let t = value as! Table
if !(t["w"] is Number) || !(t["h"] is Number) { return typeName }
return nil
}

}

extension Table {

public func toPoint() -> CGPoint? {
let x = self["x"] as? Number
let y = self["y"] as? Number
if x == nil || y == nil { return nil }
return CGPoint(x: x!.toDouble(), y: y!.toDouble())
}

public func toSize() -> CGSize? {
let w = self["w"] as? Number
let h = self["h"] as? Number
if w == nil || h == nil { return nil }
return CGSize(width: w!.toDouble(), height: h!.toDouble())
}

}

extension Arguments {

public var point: CGPoint { return (values.remove(at: 0) as! Table).toPoint()! }
public var size: CGSize { return (values.remove(at: 0) as! Table).toSize()! }

}
79 changes: 79 additions & 0 deletions Shared/Lua/Function.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

public enum FunctionResults {
case values([Value])
case error(String)
}

open class Function: StoredValue {

open func call(_ args: [Value]) -> FunctionResults {
let debugTable = vm.globals["debug"] as! Table
let messageHandler = debugTable["traceback"]

let originalStackTop = vm.stackSize()

messageHandler.push(vm)
push(vm)
for arg in args {
arg.push(vm)
}

let result = lua_pcallk(vm.vm, Int32(args.count), LUA_MULTRET, Int32(originalStackTop + 1), 0, nil)
vm.remove(originalStackTop + 1)

if result == LUA_OK {
var values = [Value]()
let numReturnValues = vm.stackSize() - originalStackTop

for _ in 0..<numReturnValues {
let v = vm.popValue(originalStackTop+1)!
values.append(v)
}

return .values(values)
}
else {
let err = vm.popError()
return .error(err)
}
}

override open func kind() -> Kind { return .function }

override open class func arg(_ vm: VirtualMachine, value: Value) -> String? {
if value.kind() != .function { return "function" }
return nil
}

}

public typealias TypeChecker = (VirtualMachine, Value) -> String?

public enum SwiftReturnValue {
case value(Value?)
case values([Value])
case nothing // convenience for Values([])
case error(String)
}

public typealias SwiftFunction = (Arguments) -> SwiftReturnValue

open class Arguments {

internal var values = [Value]()

open var string: String { return values.remove(at: 0) as! String }
open var number: Number { return values.remove(at: 0) as! Number }
open var boolean: Bool { return values.remove(at: 0) as! Bool }
open var function: Function { return values.remove(at: 0) as! Function }
open var table: Table { return values.remove(at: 0) as! Table }
open var userdata: Userdata { return values.remove(at: 0) as! Userdata }
open var lightUserdata: LightUserdata { return values.remove(at: 0) as! LightUserdata }
open var thread: Thread { return values.remove(at: 0) as! Thread }

open var integer: Int64 { return (values.remove(at: 0) as! Number).toInteger() }
open var double: Double { return (values.remove(at: 0) as! Number).toDouble() }

open func customType<T: CustomTypeInstance>() -> T { return (values.remove(at: 0) as! Userdata).toCustomType() }

}
19 changes: 19 additions & 0 deletions Shared/Lua/Nil.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

open class Nil: Value, Equatable {

open func push(_ vm: VirtualMachine) {
lua_pushnil(vm.vm)
}

open func kind() -> Kind { return .nil }

open class func arg(_ vm: VirtualMachine, value: Value) -> String? {
if value.kind() != .nil { return "nil" }
return nil
}

}

public func ==(lhs: Nil, rhs: Nil) -> Bool {
return true
}
98 changes: 98 additions & 0 deletions Shared/Lua/Number.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

open class Number: StoredValue, CustomDebugStringConvertible {

override open func kind() -> Kind { return .number }

open func toDouble() -> Double {
push(vm)
let v = lua_tonumberx(vm.vm, -1, nil)
vm.pop()
return v
}

open func toFloat() -> Float {
push(vm)
let v = lua_tonumberx(vm.vm, -1, nil)
vm.pop()
return Float(v)
}

open func toInteger() -> Int64 {
push(vm)
let v = lua_tointegerx(vm.vm, -1, nil)
vm.pop()
return v
}

open var debugDescription: String {
push(vm)
let isInteger = lua_isinteger(vm.vm, -1) != 0
vm.pop()

if isInteger { return toInteger().description }
else { return toDouble().description }
}

open var isInteger: Bool {
push(vm)
let isInteger = lua_isinteger(vm.vm, -1) != 0
vm.pop()
return isInteger
}

override open class func arg(_ vm: VirtualMachine, value: Value) -> String? {
if value.kind() != .number { return "number" }
return nil
}

}

extension Double: Value {

public func push(_ vm: VirtualMachine) {
lua_pushnumber(vm.vm, self)
}

public func kind() -> Kind { return .number }

public static func arg(_ vm: VirtualMachine, value: Value) -> String? {
value.push(vm)
let isDouble = lua_isinteger(vm.vm, -1) != 0
vm.pop()
if !isDouble { return "double" }
return nil
}

}

extension Int64: Value {

public func push(_ vm: VirtualMachine) {
lua_pushinteger(vm.vm, self)
}

public func kind() -> Kind { return .number }

public static func arg(_ vm: VirtualMachine, value: Value) -> String? {
value.push(vm)
let isDouble = lua_isinteger(vm.vm, -1) != 0
vm.pop()
if !isDouble { return "integer" }
return nil
}

}

extension Int: Value {

public func push(_ vm: VirtualMachine) {
lua_pushinteger(vm.vm, Int64(self))
}

public func kind() -> Kind { return .number }

public static func arg(_ vm: VirtualMachine, value: Value) -> String? {
return Int64.arg(vm, value: value)
}

}
Loading

0 comments on commit aee23f9

Please sign in to comment.