Skip to content

Commit

Permalink
Merge pull request PerfectlySoft#200 from CoderXpert/refactor_dynamic…
Browse files Browse the repository at this point in the history
…loader

refactor dynamic loader class to look more like Swift
  • Loading branch information
kjessup authored Jun 27, 2016
2 parents 3c44c68 + 0e7aa45 commit 7add841
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions Sources/PerfectLib/DynamicLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ struct DynamicLoader {
let resolvedPath = at.stringByResolvingSymlinksInPath
let moduleName = resolvedPath.lastPathComponent.stringByDeletingPathExtension
let file = File(resolvedPath + "/" + moduleName)
if file.exists {
let realPath = file.realPath
return self.loadRealPath(realPath, moduleName: moduleName)
}
return false
guard file.exists else {
return false
}
let realPath = file.realPath
return self.loadRealPath(realPath, moduleName: moduleName)
}

func loadLibrary(atPath at: String) -> Bool {
Expand All @@ -66,14 +66,13 @@ struct DynamicLoader {
let newModuleName = moduleName.stringByReplacing(string: "-", withString: "_").stringByReplacing(string: " ", withString: "_")
let symbolName = "_TF\(newModuleName.utf8.count)\(newModuleName)\(initFuncName.utf8.count)\(initFuncName)FT_T_"
let sym = dlsym(openRes, symbolName)
if sym != nil {
let f: InitFunction = unsafeBitCast(sym, to: InitFunction.self)
f()
return true
} else {
Log.warning(message: "Error loading \(realPath). Symbol \(symbolName) not found.")
dlclose(openRes)
}
return false
guard sym != nil else {
Log.warning(message: "Error loading \(realPath). Symbol \(symbolName) not found.")
dlclose(openRes)
return false
}
let f: InitFunction = unsafeBitCast(sym, to: InitFunction.self)
f()
return true
}
}

0 comments on commit 7add841

Please sign in to comment.