-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathObject.swift
93 lines (75 loc) · 2.3 KB
/
Object.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// Object.swift
// SwiftRuby
//
// Created by John Holdsworth on 26/09/2015.
// Copyright © 2015 John Holdsworth. All rights reserved.
//
// $Id: //depot/SwiftRuby/Object.swift#19 $
//
// Repo: https://github.com/RubyNative/SwiftRuby
//
// See: http://ruby-doc.org/core-2.2.3/Object.html
//
import Foundation
public let ARGV = CommandLine.arguments
public let STDIN = IO(what: "stdin", unixFILE: stdin)
public let STDOUT = IO(what: "stdout", unixFILE: stdout)
public let STDERR = IO(what: "stderr", unixFILE: stderr)
public let ENV = ENVProxy()
open class ENVProxy {
open subscript(key: string_like) -> String? {
get {
let val = getenv(key.to_s)
return val != nil ? String(validatingUTF8: val!) ?? "Value not UTF8" : nil
}
set {
if newValue != nil {
setenv(key.to_s, newValue!, 1)
}
else {
unsetenv(key.to_s)
}
}
}
}
open class RubyObject {
open var hash: fixnum {
return unsafeBitCast(self, to: Int.self)
}
open var instance_variables: [String] {
return instanceVariablesForClass(type(of: self), NSMutableArray()).map {$0}
}
open var methods: [String] {
return methodSymbolsForClass(type(of: self), NSMutableArray()).map { _stdlib_demangleName($0) }
}
}
// not public in Swift3
#if swift(>=3.0)
@_silgen_name("swift_demangle")
public
func _stdlib_demangleImpl(
_ mangledName: UnsafePointer<CChar>?,
mangledNameLength: UInt,
outputBuffer: UnsafeMutablePointer<UInt8>?,
outputBufferSize: UnsafeMutablePointer<UInt>?,
flags: UInt32
) -> UnsafeMutablePointer<CChar>?
public func _stdlib_demangleName(_ mangledName: String) -> String {
return mangledName.utf8CString.withUnsafeBufferPointer {
(mangledNameUTF8) in
let demangledNamePtr = _stdlib_demangleImpl(
mangledNameUTF8.baseAddress,
mangledNameLength: UInt(mangledNameUTF8.count - 1),
outputBuffer: nil,
outputBufferSize: nil,
flags: 0)
if let demangledNamePtr = demangledNamePtr {
let demangledName = String(cString: demangledNamePtr)
free(demangledNamePtr)
return demangledName
}
return mangledName
}
}
#endif