Skip to content

Commit

Permalink
Fixed a few bugs, added jsc.system() command.
Browse files Browse the repository at this point in the history
  • Loading branch information
N-Holzschuch committed Apr 18, 2023
1 parent d0cdb71 commit 6a83eb1
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions shell_cmds_ios/jsc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ public func jsc(argc: Int32, argv: UnsafeMutablePointer<UnsafeMutablePointer<Int
return ""
}
gateway?.setObject(readFile, forKeyedSubscript: "readFile" as NSString)
let readFileBase64: @convention(block) (String) -> String = { string in
do {
return try NSData(contentsOf: URL(fileURLWithPath: fileName)).base64EncodedString()
}
catch {
context.exception = JSValue(newErrorFromMessage: error.localizedDescription, in: context)
}
return ""
}
gateway?.setObject(readFileBase64, forKeyedSubscript: "readFileBase64" as NSString)

let writeFile: @convention(block) (String, String) -> Int = { filePath, content in
do {
try content.write(toFile: filePath, atomically: true, encoding: String.Encoding.utf8)
Expand All @@ -217,6 +228,19 @@ public func jsc(argc: Int32, argv: UnsafeMutablePointer<UnsafeMutablePointer<Int
return -1
}
gateway?.setObject(writeFile, forKeyedSubscript: "writeFile" as NSString)
let writeFileBase64: @convention(block) (String, String) -> Int = { filePath, content in
do {
if let data = Data(base64Encoded: content, options: .ignoreUnknownCharacters) {
try data.write(to: URL(fileURLWithPath: filePath))
return 0
}
}
catch {
context.exception = JSValue(newErrorFromMessage: error.localizedDescription, in: context)
}
return -1
}
gateway?.setObject(writeFileBase64, forKeyedSubscript: "writeFileBase64" as NSString)

let listFiles: @convention(block) (String) -> [String] = { directory in
do {
Expand Down Expand Up @@ -300,6 +324,19 @@ public func jsc(argc: Int32, argv: UnsafeMutablePointer<UnsafeMutablePointer<Int
}
gateway?.setObject(fileSize, forKeyedSubscript: "fileSize" as NSString)

let system: @convention(block) (String) -> Int32 = { command in
let pid = ios_fork()
var result = ios_system(command)
ios_waitpid(pid)
ios_releaseThreadId(pid)
if (result == 0) {
// If there's already been an error (e.g. "command not found") no need to ask for more.
result = ios_getCommandStatus()
}
return result
}
gateway?.setObject(system, forKeyedSubscript: "system" as NSString)

// Load require:
if let requireUrl = Bundle.main.url(forResource: "require_jscore", withExtension: "js") {
if let data = try? Data(contentsOf: requireUrl) {
Expand Down

0 comments on commit 6a83eb1

Please sign in to comment.