-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Lua and Lua Script node support
- Loading branch information
1 parent
fb79f07
commit aee23f9
Showing
82 changed files
with
23,103 additions
and
48 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()! } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
Oops, something went wrong.