I have to bind an Objective-C object to a variable of a Lua script. I don't have write access to this Lua script, I just load and run it from the Objective-C code. I know it uses a variable, called luaVar, that uses methods defined in the object. Here is the Lua code:
function run()
print("Running Lua script")
print(luaVar)
print("Ending Lua script")
end
run()
This script should print the luaVar variable, and not nil. Now, here is the Objective-C code I use. The function validate gets the Lua code in the script variable and the object to pass to Lua as f in the theObject variable:
#import "ScriptObject.h"
static const char* SCRIPT_OBJECT_LUA_KEY = "f";
ScriptObject* scriptObject = nil;
- (void)validate:(NSString*)script withObject:(ScriptObject*)theObject {
L = luaL_newstate(); // create a new state structure for the interpreter
luaL_openlibs(L); // load all the standard libraries into the interpreter
lua_settop(L, 0);
newScriptObject(L);
// load the script
int err = luaL_loadstring(L, [script cStringUsingEncoding:NSASCIIStringEncoding]);
if (LUA_OK != err) {
NSLog(#"Error while loading Lua script!");
lua_pop(L, 1);
return NO;
}
// call the script
err = lua_pcall(L, 0, LUA_MULTRET, 0);
if (LUA_OK != err) {
NSLog(#"Error while running Lua script: %s", lua_tostring(L, -1));
lua_pop(L, 1);
return NO;
}
lua_close(L);
}
static int newScriptObject(lua_State *L) {
ScriptObject * __strong *lgo = (ScriptObject * __strong *)lua_newuserdata(L, sizeof(ScriptObject *));
*lgo = scriptObject;
luaL_getmetatable(L, SCRIPT_OBJECT_LUA_KEY);
lua_setmetatable(L, -2);
lua_newtable(L);
lua_setuservalue(L, -2);
NSLog(#"New ScriptObject created");
return 1;
}
I tried the approach from this SO answer, but that didn't handle Objective-C objects. I looked at this question, but it is not clear how it does the binding.
Does anyone know how to do that?
Found it myself. It was just a missing line at the end of the newScriptObject function which sets the defined variable as a Lua global:
lua_setglobal(L, SCRIPT_OBJECT_LUA_KEY);
Related
I would like to use some C code that uses a file descriptor.
Background is that I would like to read some data from cgraph library.
public extension UnsafeMutablePointer where Pointee == Agraph_t {
func saveTo(fileName: String) {
let f = fopen(cString(fileName), cString("w"))
agwrite(self,f)
fsync(fileno(f))
fclose(f)
}
}
I would like to have the file output, but without writing to a temp file. Hence, I would like to do something like this:
public extension UnsafeMutablePointer where Pointee == Agraph_t {
var asString: String {
let pipe = Pipe()
let fileDescriptor = UnsafeMutablePointer<Int32>.allocate(capacity: 1)
fileDescriptor.pointee = pipe.fileHandleForWriting.fileDescriptor
agwrite(self, fileDescriptor)
let data = pipe.fileHandleForReading.readDataToEndOfFile()
if let output = String(data: data, encoding: .utf8) {
return output
}
return ""
}
}
But it doesn't work, resulting in a EXC_BAD_ACCESS within agwrite(,). What do I need to do instead?
Many thanks in advance!
File descriptors and file pointers are not the same thing. It's confusing, and made even more frustrating by the fact that FILE * is really hard to Google because of the symbol.
You need to fdopen the file descriptor (pipe.fileHandleForWriting.fileDescriptor), to receive a FILE * (UnsafeMutablePointer<FILE> in Swift). This is what you then pass to agwrite.
It's important to fclose the file pointer when you're done writing to it, otherwise .readDataToEndOfFile() will never terminate. I made a helper function to ensure the fclose can't be forgetten. It's possible that agwrite closes the file pointer itself, internally. If that's the case, you should delete this code and just give it the result of fdopen, plain and simple.
import Foundation
public typealias Agraph_t = Int // Dummy value
public struct AGWriteWrongEncoding: Error { }
func agwrite(_: UnsafeMutablePointer<Agraph_t>, _ filePointer: UnsafeMutablePointer<FILE>) {
let message = "This is a stub."
_ = message.withCString { cString in
fputs(cString, stderr)
}
}
#discardableResult
func use<R>(
fileDescriptor: Int32,
mode: UnsafePointer<Int8>!,
closure: (UnsafeMutablePointer<FILE>) throws -> R
) rethrows -> R {
// Should prob remove this `!`, but IDK what a sensible recovery mechanism would be.
let filePointer = fdopen(fileDescriptor, mode)!
defer { fclose(filePointer) }
return try closure(filePointer)
}
public extension UnsafeMutablePointer where Pointee == Agraph_t {
func asString() throws -> String {
let pipe = Pipe()
use(fileDescriptor: pipe.fileHandleForWriting.fileDescriptor, mode: "w") { filePointer in
agwrite(self, filePointer)
}
let data = pipe.fileHandleForReading.readDataToEndOfFile()
guard let output = String(data: data, encoding: .utf8) else {
throw AGWriteWrongEncoding()
}
return output
}
}
let ptr = UnsafeMutablePointer<Agraph_t>.allocate(capacity: 1) // Dummy value
print(try ptr.asString())
Several other things:
Throwing an error is probably a better choice than returning "". Empty strings aren't a good error handling mechanism. Returning an optional would also work, but it's likely to always be force unwrapped, anyway.
readDataToEndOfFile is a blocking call, which can lead to a bad use experience. It's probably best that this code be run on a background thread, or use a FileHandle.readabilityHandler to asynchronously consume the data as it comes in.
I have a code like this:
print("Migration Execution: Successfully uninstalled MCAfee")
migrationInfoPicture.image = NSImage(named: "Unroll")
migrationInfoText.stringValue = NSLocalizedString("Unrolling from old server... Please wait!", comment: "Unrolling")
while(!readFile(path:logfilePath)!.contains("result: 2 OK")) {
searchLogForError(scriptPath: scriptOnePath)
}
print("Migration Execution: Successfully unrolled from old server")
migrationInfoText.stringValue = NSLocalizedString("Setting up MDM profile... Please wait!", comment: "Setting up MDM")
while(!readFile(path:logfilePath)!.contains("result: 3 OK")) {
searchLogForError(scriptPath: scriptOnePath)
}
It actually works in the background, reading from the file works and logging works but since the GUI will be hanging executing a while loop with a quickly completed task, the image and the text changes will not be visible.
Code for searchForLogError is:
func searchLogForError(scriptPath:String) {
if((readFile(path:logfilePath)!.filter { $0.contains("ERROR") }).contains("ERROR")) {
print("Migration abborted")
migrationInfoPicture.image = NSImage(named: "FatalError")
migrationInfoText.stringValue = NSLocalizedString("An error occured: \n", comment: "Error occurence") + readFile(path:logfilePath)!.filter { $0.contains("ERROR") }[0]
migrationWarningText.stringValue = NSLocalizedString("In order to get further help, please contact: mac.workplace#swisscom.com", comment: "Error support information")
self.view.window?.level = .normal
btnExitApplicationOutlet.isHidden = false
getScriptProcess(path:scriptPath).terminate()
return
}
}
How can I achieve a visible change of NSImage and NSLocalizedString while constantly looking for log file change without a hanging GUI (or even with a hanging GUI, but with enough time to change the visible elements between the while-loops)?
Polling file system resources is a horrible practice. Don't do that. There are dedicated APIs to observe file system resources for example DispatchSourceFileSystemObject
Create a property
var fileSystemObject : DispatchSourceFileSystemObject?
and two methods to start and stop the observer. In the closure of setEventHandler insert the code to read the file
func startObserver(at url: URL)
{
if fileSystemObject != nil { return }
let fileDescriptor : CInt = open(url.path, O_EVTONLY);
if fileDescriptor < 0 {
print("Could not open file descriptor"))
return
}
fileSystemObject = DispatchSource.makeFileSystemObjectSource(fileDescriptor: fileDescriptor, eventMask: [.write, .rename], queue: .global())
if fileSystemObject == nil {
close(fileDescriptor)
print"Could not create Dispatch Source"))
return
}
fileSystemObject!.setEventHandler {
if self.fileSystemObject!.mask.contains(.write) {
// the file has been modified, do something
}
}
fileSystemObject!.setCancelHandler {
close(fileDescriptor)
}
fileSystemObject!.resume()
}
func stopObserver()
{
fileSystemObject?.cancel()
fileSystemObject = nil
}
POSIX getaddrinfo allocates memory that must later be freed using freeaddrinfo.
See http://manpages.ubuntu.com/manpages/xenial/en/man3/getaddrinfo.3.html
To simplify the API, I've created this function:
import Foundation
enum SystemError: Swift.Error {
case getaddrinfo(Int32, Int32?)
}
public func getaddrinfo(node: String?, service: String?, hints: addrinfo?) throws -> [addrinfo] {
var err: Int32
var res: UnsafeMutablePointer<addrinfo>?
if var hints = hints {
err = getaddrinfo(node, service, &hints, &res)
} else {
err = getaddrinfo(node, service, nil, &res)
}
if err == EAI_SYSTEM {
throw SystemError.getaddrinfo(err, errno)
}
if err != 0 {
throw SystemError.getaddrinfo(err, nil)
}
defer {
freeaddrinfo(res)
}
var result = [addrinfo]()
var ai = res?.pointee
while ai != nil {
result.append(ai!)
ai = ai!.ai_next?.pointee
}
return result
}
I don't feel that the function is correct, though.
How can the Swift memory model know that getaddrinfo allocates memory, and that Swift should not overwrite that memory with own stuff?
How can Swift know that freeaddrinfo deletes the whole list, and that it should copy out ai information that has been assigned to the result array?
What's the correct way to interface with getaddrinfo?
Memory allocated by getaddrinfo (e.g. by malloc) will not be given to any other
dynamic memory allocation function in the same running process
until released by freeaddrinfo (e.g. by free).
Therefore the Swift runtime will not trample on that memory (if we
assume that it has no programming errors such as wrong pointer calculations).
Also struct addrinfo is a value type, so
result.append(ai!)
will append a copy of the pointed-to structure to the array.
But there is still a problem. Some members of struct addrinfo
are pointers
public var ai_canonname: UnsafeMutablePointer<Int8>! /* canonical name for hostname */
public var ai_addr: UnsafeMutablePointer<sockaddr>! /* binary address */
which may point into the memory allocated by getaddrinfo and therefore
invalid after freeaddrinfo, and dereferencing them after your
function returns causes undefined behaviour.
Therefore you must either postpone the freeaddrinfo until the
address list is not needed anymore, or copy the information.
This is a bit cumbersome because ai_addr may point to a
IPv4 or IPv6 socket address structure which have different length.
The following code demonstrates how the address list can be copied
to an array of sockaddr_storage structures (which are large enough
to hold any IP address). This code has not been thoroughly tested,
so use it with care.
public func getaddrinfo(node: String, service: String, hints: addrinfo?) throws -> [sockaddr_storage] {
var err: Int32
var res: UnsafeMutablePointer<addrinfo>?
if var hints = hints {
err = getaddrinfo(node, service, &hints, &res)
} else {
err = getaddrinfo(node, service, nil, &res)
}
if err == EAI_SYSTEM {
throw SystemError.getaddrinfo(err, errno)
}
if err != 0 {
throw SystemError.getaddrinfo(err, nil)
}
defer {
freeaddrinfo(res)
}
guard let firstAddr = res else {
return []
}
var result = [sockaddr_storage]()
for addr in sequence(first: firstAddr, next: { $0.pointee.ai_next }) {
var sockAddr = sockaddr_storage()
memcpy(&sockAddr, addr.pointee.ai_addr, Int(addr.pointee.ai_addrlen))
result.append(sockAddr)
}
return result
}
Remarks:
If you are only interested in a single address family then
you can replace sockaddr_storage by sockaddr_in or
sockaddr_in6.
I have made the node and service parameters non-optional.
The reason is that Swift currently has a Bug when passing more
than one optional String to a C function, see
Why does Swift return an unexpected pointer when converting an optional String into an UnsafePointer?.
sequence() is used here to traverse the linked list instead of
a while loop.
Xcode 8 beta 2 / Swift 3:
According to Apple's CoreMIDI API documentation, a MIDI thru connection can be established as persistent (stays in place forever, even after your app quits and your system reboots) or non-persistent/transitory (owned by your application and automatically destroys it on app quit).
The trouble I'm running into is that I can't seem to create a non-persistent connection, even though I am following Apple's guidelines.
It comes down to this API:
func MIDIThruConnectionCreate(_ inPersistentOwnerID: CFString?,
_ inConnectionParams: CFData,
_ outConnection: UnsafeMutablePointer<MIDIThruConnectionRef>) -> OSStatus
If you pass null (nil) to inPersistentOwnerID which is a Swift optional, the connection should be created as transitory. However, regardless of whether I pass nil or a String, connections are always created as persistent. (I can verify this by checking CoreMIDI's persistent thru connections.)
A summation of my code:
public class OTMIDIConnectedThru {
var connectionRef = MIDIThruConnectionRef()
init?(sourceEndpoints: [MIDIEndpointRef], destinationEndpoints: [MIDIEndpointRef], persistentOwnerID: String? = nil) {
var params = MIDIThruConnectionParams()
MIDIThruConnectionParamsInitialize(¶ms) // fill with defaults
// (... snip: code to prepare parameters here ...)
let paramsData = withUnsafePointer(¶ms) { p in
NSData(bytes: p, length: MIDIThruConnectionParamsSize(¶ms))
}
result = MIDIThruConnectionCreate(persistentOwnerID, paramsData, &connectionRef)
guard result == noErr else { return nil }
}
}
Any idea what I'm doing wrong? This couldn't possibly be a bug in the API?
I had the same issue, and yes I think it always does create persistent connections. Probably an ID of NULL is the same as an empty string, because MIDIThruConnectionFind with an empty string returns all those persistent connections. So, a bug in the API or the docs!
I would recommend using a real persistentID, and remove all existing/stale connections when you initialize your MIDI stuff:
CFDataRef data;
MIDIThruConnectionFind(CFSTR("com.yourcompany.yourapp"), &data);
unsigned long n = CFDataGetLength(data) / sizeof(MIDIThruConnectionRef);
MIDIThruConnectionRef * con = (MIDIThruConnectionRef*)CFDataGetBytePtr(data);
for(int i=0;i<n;i++) {
MIDIThruConnectionDispose(*con);
con++;
}
I'm writing a Swift command line tool that uses NSTask to interact with git. In the simplest scenario I want to run three commands: init, add ., and commit -m Initial Commit. I intend to use a separate NSTask for each command, and want to house each command in its own function - returning true if the task succeeded or false if it didn't. This set-up would allow my main function to look like this:
func main() {
if runInit() {
if runStage() {
if runCommit() {
NSLog("success!")
}
}
}
}
To accomplish this each of the three functions must do the following before returning (i) launch the task (ii) wait for it to complete, (iii) obtain whatever is in stdout, and (iv) set the return value (true or false). Here's what I've got for the commit stage:
func runCommit() -> Bool {
var retval = false
var commitTask = NSTask()
commitTask.standardOutput = NSPipe()
commitTask.launchPath = gitPath
commitTask.arguments = ["commit", "-m", "Initial Commit"]
commitTask.currentDirectoryPath = demoProjectURL.path!
commitTask.standardOutput.fileHandleForReading.readToEndOfFileInBackgroundAndNotify()
nc.addObserverForName(NSFileHandleReadToEndOfFileCompletionNotification,
object: commitTask.standardOutput.fileHandleForReading,
queue: nil) { (note) -> Void in
// get the output, log it, then...
if commitTask.terminationStatus == EXIT_SUCCESS {
retval = true
}
}
commitTask.launch()
commitTask.waitUntilExit()
return retval
}
My question is essentially about how waitUntilExit works, particularly in conjunction with the notification I sign up for to enable me to get the output. Apple's docs say:
This method first checks to see if the receiver is still running using isRunning. Then it polls the current run loop using NSDefaultRunLoopMode until the task completes.
I'm a bit out of my depth when it comes to run loop mechanics, and was wondering what this means in this context - can I safely assume that my notification block will always be executed before the enclosing function returns?
waitUntilExit returns when the SIGCHILD signal has been received
to indicate that the child process has terminated. The notification
block is executed when EOF is read from the pipe to the child process.
It is not specified which of these events occurs first.
Therefore you have to wait for both. There are several possible solutions,
here is one using a "signalling semaphore", you could also use
a "dispatch group".
Another error in your code is that the observer is never removed.
func runCommit() -> Bool {
let commitTask = NSTask()
commitTask.standardOutput = NSPipe()
commitTask.launchPath = gitPath
commitTask.arguments = ["commit", "-m", "Initial Commit"]
commitTask.currentDirectoryPath = demoProjectURL.path!
commitTask.standardOutput!.fileHandleForReading.readToEndOfFileInBackgroundAndNotify()
let sema = dispatch_semaphore_create(0)
var obs : NSObjectProtocol!
obs = nc.addObserverForName(NSFileHandleReadToEndOfFileCompletionNotification,
object: commitTask.standardOutput!.fileHandleForReading, queue: nil) {
(note) -> Void in
// Get data and log it.
if let data = note.userInfo?[NSFileHandleNotificationDataItem] as? NSData,
let string = String(data: data, encoding: NSUTF8StringEncoding) {
print(string)
}
// Signal semaphore.
dispatch_semaphore_signal(sema)
nc.removeObserver(obs)
}
commitTask.launch()
// Wait for process to terminate.
commitTask.waitUntilExit()
// Wait for semaphore to be signalled.
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)
let retval = commitTask.terminationStatus == EXIT_SUCCESS
return retval
}