Skip to content

Commit

Permalink
Fix concurrency errors
Browse files Browse the repository at this point in the history
  • Loading branch information
STREGA committed Jan 19, 2025
1 parent 5293f88 commit 3fe6360
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 32 deletions.
3 changes: 2 additions & 1 deletion Sources/GateEngine/Scripting/Gravity/Gravity+Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import Gravity

@MainActor
@MainActor
@preconcurrency
internal func errorCallback(
vm: OpaquePointer?,
errorType: error_type_t,
Expand Down
4 changes: 3 additions & 1 deletion Sources/GateEngine/Scripting/Gravity/Gravity+Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import Foundation
import Gravity

@MainActor
@MainActor
@preconcurrency
internal func filenameCallback(
fileID: UInt32,
xData: UnsafeMutableRawPointer?
Expand All @@ -21,6 +22,7 @@ internal func filenameCallback(
}

@MainActor
@preconcurrency
internal func loadFileCallback(
file: UnsafePointer<CChar>!,
size: UnsafeMutablePointer<Int>!,
Expand Down
1 change: 1 addition & 0 deletions Sources/GateEngine/Scripting/Gravity/Gravity+Testing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import Gravity

@MainActor
@preconcurrency
internal func unittestCallback(
vm: OpaquePointer!,
errorType: error_type_t,
Expand Down
54 changes: 24 additions & 30 deletions Sources/GateEngine/Scripting/Gravity/Gravity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* http://stregasgate.com
*/

import Gravity
@preconcurrency import Gravity
import struct Foundation.URL

extension Gravity {
Expand Down Expand Up @@ -374,36 +374,9 @@ public class Gravity {
}
}

// This is only called for global closures. Instance methods use the seperate bridge delegate callback.
@MainActor
internal func gravityCFuncInternal(
vm: OpaquePointer!,
args: UnsafeMutablePointer<gravity_value_t>!,
nargs: UInt16,
rindex: UInt32
) -> Bool {
let functionName: String = {
let gClosure = unsafeBitCast(
args!.pointee.p,
to: UnsafeMutablePointer<gravity_closure_t>.self
)
let cName = gClosure.pointee.f.pointee.identifier!
return String(cString: cName)
}()

let vmID = Int(bitPattern: vm)

guard let function = Gravity.storage[vmID]?.cInternalFunctionMap[functionName] else {
fatalError()
}
var args = UnsafeBufferPointer(start: args, count: Int(nargs)).map({ GravityValue(gValue: $0) })
args.removeFirst() // The first is always the closure being called
let result = function(Gravity(vm: vm), args)
return _gravityHandleCFuncReturn(vm: vm, returnValue: result, returnSlot: rindex)
}

@MainActor
@inline(__always)
@preconcurrency
internal func _gravityHandleCFuncReturn(
vm: OpaquePointer,
returnValue: GravityValue,
Expand Down Expand Up @@ -472,7 +445,28 @@ extension Gravity {
@MainActor
public func setFunc(_ key: String, to function: @escaping GravitySwiftFunctionReturns) {
key.withCString { cKey in
let gFunc = gravity_function_new_internal(vm, cKey, gravityCFuncInternal, 0)
let exec: gravity_c_internal = { vm, args, nargs, rindex -> Bool in
let functionName: String = {
let gClosure = unsafeBitCast(
args!.pointee.p,
to: UnsafeMutablePointer<gravity_closure_t>.self
)
let cName = gClosure.pointee.f.pointee.identifier!
return String(cString: cName)
}()

let vmID = Int(bitPattern: vm)

guard let function = Gravity.storage[vmID]?.cInternalFunctionMap[functionName] else {
fatalError()
}
var args = UnsafeBufferPointer(start: args, count: Int(nargs)).map({ GravityValue(gValue: $0) })
args.removeFirst() // The first is always the closure being called
let result = function(Gravity(vm: vm!), args)
return _gravityHandleCFuncReturn(vm: vm!, returnValue: result, returnSlot: rindex)
}

let gFunc = gravity_function_new_internal(vm, cKey, exec, 0)
let gClosure = gravity_closure_new(vm, gFunc)

var gValue = gravity_value_t()
Expand Down
1 change: 1 addition & 0 deletions Sources/GateEngine/Scripting/Gravity/GravityClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ internal class GravityCFuncBridgedUserData {
}

@MainActor
@preconcurrency
internal func gravityCFuncBridged(
vm: OpaquePointer!,
xdata: UnsafeMutableRawPointer?,
Expand Down

0 comments on commit 3fe6360

Please sign in to comment.