Skip to content

Commit

Permalink
PerlSvConvertible -> PerlScalarConvertible
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksey-mashanov committed Jun 1, 2017
1 parent 87fab0d commit 1bd1a97
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 53 deletions.
10 changes: 5 additions & 5 deletions Sources/Perl/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public final class PerlArray : PerlValue {

/// Initializes Perl array with elements of collection `c`.
public convenience init<C : Collection>(_ c: C, perl: PerlInterpreter = .current)
where C.Iterator.Element : PerlSvConvertible {
where C.Iterator.Element : PerlScalarConvertible {
self.init(perl: perl)
reserveCapacity(numericCast(c.count))
for (i, v) in c.enumerated() {
Expand Down Expand Up @@ -121,7 +121,7 @@ extension PerlArray {
/// - Returns: `nil` if the element not exists or is undefined.
///
/// - Complexity: O(1).
public func fetch<T : PerlSvConvertible>(_ index: Int) throws -> T? {
public func fetch<T : PerlScalarConvertible>(_ index: Int) throws -> T? {
return try withUnsafeAvContext { c in
try c.fetch(index).flatMap {
try T?(_fromUnsafeSvContextInc: $0)
Expand All @@ -135,7 +135,7 @@ extension PerlArray {
/// - Parameter value: The value to store in the array.
///
/// - Complexity: O(1).
public func store<T : PerlSvConvertible>(_ index: Int, value: T) {
public func store<T : PerlScalarConvertible>(_ index: Int, value: T) {
withUnsafeAvContext {
$0.store(index, value: value._toUnsafeSvPointer(perl: $0.perl))
}
Expand All @@ -145,7 +145,7 @@ extension PerlArray {
///
/// - Parameter index: The position of the element to fetch.
/// - Returns: Deleted element or `nil` if the element not exists or is undefined.
public func delete<T : PerlSvConvertible>(_ index: Int) throws -> T? {
public func delete<T : PerlScalarConvertible>(_ index: Int) throws -> T? {
return try withUnsafeAvContext { c in
try c.delete(index).flatMap {
try T?(_fromUnsafeSvContextInc: $0)
Expand Down Expand Up @@ -296,7 +296,7 @@ extension PerlArray: ExpressibleByArrayLiteral {
}
}

extension Array where Element : PerlSvConvertible {
extension Array where Element : PerlScalarConvertible {
/// Creates an array from the Perl array.
///
/// - Parameter av: The Perl array with the elements compatible with `Element`.
Expand Down
6 changes: 3 additions & 3 deletions Sources/Perl/Call.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ extension PerlInterpreter {
return "G_SCALAR"

def generic(r):
return ", ".join(map(lambda r: r + " : PerlSvConvertible", re.findall("\\bR\\d*\\b", r)))
return ", ".join(map(lambda r: r + " : PerlScalarConvertible", re.findall("\\bR\\d*\\b", r)))

def fqGeneric(r):
g = generic(r)
Expand All @@ -83,7 +83,7 @@ extension PerlInterpreter {

dispatchVariants = ["sub", "method"]

argsVariants = ["args: [PerlSvConvertible?]", "_ args: PerlSvConvertible?..."]
argsVariants = ["args: [PerlScalarConvertible?]", "_ args: PerlScalarConvertible?..."]

def returnVariants(context):
if context == "void":
Expand All @@ -109,7 +109,7 @@ extension PerlInterpreter {

extension PerlInterpreter {
% for ret in allReturnVariants:
func call${fqGeneric(ret)}(sv: UnsafeSvPointer, args: [PerlSvConvertible?], flags: Int32 = ${contextFlags(ret)}) throws -> ${ret} {
func call${fqGeneric(ret)}(sv: UnsafeSvPointer, args: [PerlScalarConvertible?], flags: Int32 = ${contextFlags(ret)}) throws -> ${ret} {
let svArgs: [UnsafeSvPointer] = args.map { $0?._toUnsafeSvPointer(perl: self) ?? pointee.newSV(0) }
% if ret == "Void":
_ = try unsafeCall(sv: sv, args: svArgs, flags: flags)
Expand Down
18 changes: 9 additions & 9 deletions Sources/Perl/Hash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public final class PerlHash : PerlValue {
/// ```
///
/// - Parameter dict: a dictionary with `String` keys and values
/// convertible to Perl scalars (conforming to `PerlSvConvertible`).
public convenience init<T : PerlSvConvertible>(_ dict: [String: T]) {
/// convertible to Perl scalars (conforming to `PerlScalarConvertible`).
public convenience init<T : PerlScalarConvertible>(_ dict: [String: T]) {
self.init()
for (k, v) in dict {
self[k] = v as? PerlScalar ?? PerlScalar(v)
Expand Down Expand Up @@ -136,7 +136,7 @@ extension PerlHash {
/// - Parameter key: The key to find in the hash.
/// - Returns: The value associated with `key` if `key` is in the hash;
/// otherwise, `nil`.
public func fetch<T : PerlSvConvertible>(_ key: String) throws -> T? {
public func fetch<T : PerlScalarConvertible>(_ key: String) throws -> T? {
return try withUnsafeHvContext { c in
try c.fetch(key).flatMap { try T?(_fromUnsafeSvContextInc: $0) }
}
Expand All @@ -146,7 +146,7 @@ extension PerlHash {
///
/// - Parameter key: The key to associate with `value`.
/// - Parameter value: The value to store in the hash.
public func store<T : PerlSvConvertible>(key: String, value: T) {
public func store<T : PerlScalarConvertible>(key: String, value: T) {
withUnsafeHvContext { c in
c.store(key, value: value._toUnsafeSvPointer(perl: c.perl))
}
Expand All @@ -156,7 +156,7 @@ extension PerlHash {
///
/// - Parameter key: The key to remove along with its associated value.
/// - Returns: The value that was removed, or `nil` if the key was not found in the hash.
public func delete<T : PerlSvConvertible>(_ key: String) throws -> T? {
public func delete<T : PerlScalarConvertible>(_ key: String) throws -> T? {
return try withUnsafeHvContext { c in
try c.delete(key).flatMap { try T?(_fromUnsafeSvContextInc: $0) }
}
Expand All @@ -177,7 +177,7 @@ extension PerlHash {
/// - Parameter key: The key to find in the hash.
/// - Returns: The value associated with `key` if `key` is in the hash;
/// otherwise, `nil`.
public func fetch<T : PerlSvConvertible>(_ key: PerlScalar) throws -> T? {
public func fetch<T : PerlScalarConvertible>(_ key: PerlScalar) throws -> T? {
return try withUnsafeHvContext { c in
try key.withUnsafeSvContext {
try c.fetch($0.sv).flatMap { try T?(_fromUnsafeSvContextInc: $0) }
Expand All @@ -189,7 +189,7 @@ extension PerlHash {
///
/// - Parameter key: The key to associate with `value`.
/// - Parameter value: The value to store in the hash.
public func store<T : PerlSvConvertible>(key: PerlScalar, value: T) {
public func store<T : PerlScalarConvertible>(key: PerlScalar, value: T) {
withUnsafeHvContext { c in
key.withUnsafeSvContext {
c.store($0.sv, value: value._toUnsafeSvPointer(perl: c.perl))
Expand All @@ -201,7 +201,7 @@ extension PerlHash {
///
/// - Parameter key: The key to remove along with its associated value.
/// - Returns: The value that was removed, or `nil` if the key was not found in the hash.
public func delete<T : PerlSvConvertible>(_ key: PerlScalar) throws -> T? {
public func delete<T : PerlScalarConvertible>(_ key: PerlScalar) throws -> T? {
return try withUnsafeHvContext { c in
try key.withUnsafeSvContext {
try c.delete($0.sv).flatMap { try T?(_fromUnsafeSvContextInc: $0) }
Expand Down Expand Up @@ -377,7 +377,7 @@ extension PerlHash : ExpressibleByDictionaryLiteral {
}

// where Key == String, but it is unsupported
extension Dictionary where Value : PerlSvConvertible {
extension Dictionary where Value : PerlScalarConvertible {
/// Creates a dictionary from the Perl hash.
///
/// - Parameter hv: The Perl hash with the values compatible with `Value`.
Expand Down
10 changes: 5 additions & 5 deletions Sources/Perl/Object.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import CPerl
/// var secure: Bool { return try! call(method: "secure") }
/// }
/// ```
open class PerlObject : PerlValue, PerlSvConvertible {
open class PerlObject : PerlValue, PerlScalarConvertible {
convenience init(noinc svc: UnsafeSvContext) throws {
guard svc.isObject else {
throw PerlError.notObject(fromUnsafeSvContext(noinc: svc))
Expand Down Expand Up @@ -95,14 +95,14 @@ open class PerlObject : PerlValue, PerlSvConvertible {
/// - Parameter method: A name of the constuctor. Usually it is *new*.
/// - Parameter args: Arguments to pass to the constructor.
/// - Parameter perl: The Perl interpreter.
public convenience init(method: String, args: [PerlSvConvertible?], perl: PerlInterpreter = .current) throws {
public convenience init(method: String, args: [PerlScalarConvertible?], perl: PerlInterpreter = .current) throws {
guard let named = type(of: self) as? PerlNamedClass.Type else {
fatalError("PerlObject.init(method:args:perl) is only supported for subclasses conforming to PerlNamedClass")
}
perl.enterScope()
defer { perl.leaveScope() }
let classname = named.perlClassName
let args = [classname as PerlSvConvertible?] + args
let args = [classname as PerlScalarConvertible?] + args
let svArgs: [UnsafeSvPointer] = args.map { $0?._toUnsafeSvPointer(perl: perl) ?? perl.pointee.newSV(0) }
let sv = try perl.unsafeCall(sv: perl.newSV(method, mortal: true), args: svArgs, flags: G_METHOD|G_SCALAR)[0]
let svc = UnsafeSvContext(sv: sv, perl: perl)
Expand Down Expand Up @@ -200,7 +200,7 @@ open class PerlObject : PerlValue, PerlSvConvertible {
}

// Dirty hack to initialize instance of another class (subclass).
extension PerlSvConvertible where Self : PerlObject {
extension PerlScalarConvertible where Self : PerlObject {
init(as derivedClass: Self.Type, noinc svc: UnsafeSvContext) {
self = derivedClass.init(noincUnchecked: svc)
}
Expand All @@ -212,7 +212,7 @@ extension PerlSvConvertible where Self : PerlObject {
/// Declare a `static var perlClassName` that contains a name of the Perl class
/// your Swift class should be bridged to. Use `addPerlMethod` method on
/// startup to provide ability to access your methods and attributes from Perl.
public protocol PerlBridgedObject : AnyPerl, PerlNamedClass, PerlSvConvertible {}
public protocol PerlBridgedObject : AnyPerl, PerlNamedClass, PerlScalarConvertible {}

/// A class having Perl representation.
public protocol PerlNamedClass : class {
Expand Down
8 changes: 4 additions & 4 deletions Sources/Perl/Scalar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public final class PerlScalar : PerlValue {
}

/// Creates a `SV` containig a `v`.
public convenience init<T : PerlSvConvertible>(_ v: T, perl: PerlInterpreter = .current) {
public convenience init<T : PerlScalarConvertible>(_ v: T, perl: PerlInterpreter = .current) {
self.init(noincUnchecked: UnsafeSvContext(sv: v._toUnsafeSvPointer(perl: perl), perl: perl))
}

Expand Down Expand Up @@ -104,17 +104,17 @@ public final class PerlScalar : PerlValue {
}

/// Creates a `RV` pointing to a `AV` which contains `SV`s with elements of an `array`.
public convenience init<T : PerlSvConvertible>(_ array: [T], perl: PerlInterpreter = .current) {
public convenience init<T : PerlScalarConvertible>(_ array: [T], perl: PerlInterpreter = .current) {
self.init(noincUnchecked: UnsafeSvContext(sv: array._toUnsafeSvPointer(perl: perl), perl: perl))
}

/// Creates a `RV` pointing to a `HV` which contains `SV`s with elements of a `dict`.
public convenience init<T : PerlSvConvertible>(_ dict: [String: T], perl: PerlInterpreter = .current) {
public convenience init<T : PerlScalarConvertible>(_ dict: [String: T], perl: PerlInterpreter = .current) {
self.init(noincUnchecked: UnsafeSvContext(sv: dict._toUnsafeSvPointer(perl: perl), perl: perl))
}

/// Creates a `SV` containig an unwrapped value of a `v` if `v != nil` or an `undef` in other case.
public convenience init<T : PerlSvConvertible>(_ v: T?, perl: PerlInterpreter = .current) {
public convenience init<T : PerlScalarConvertible>(_ v: T?, perl: PerlInterpreter = .current) {
if let v = v {
self.init(v, perl: perl)
} else {
Expand Down
8 changes: 4 additions & 4 deletions Sources/Perl/Stack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct UnsafeXSubStack : UnsafeStack {
return args[i]
}

func fetch<T : PerlSvConvertible>(at index: Int) throws -> T {
func fetch<T : PerlScalarConvertible>(at index: Int) throws -> T {
guard index < args.count else {
if T.self == PerlScalar.self {
return PerlScalar() as! T
Expand All @@ -60,13 +60,13 @@ struct UnsafeXSubStack : UnsafeStack {
return try T(_fromUnsafeSvContextCopy: UnsafeSvContext(sv: args[index], perl: perl))
}

func fetch<T : PerlSvConvertible>(at index: Int) throws -> T? {
func fetch<T : PerlScalarConvertible>(at index: Int) throws -> T? {
guard index < args.count else { return nil }
return try Optional<T>(_fromUnsafeSvContextCopy: UnsafeSvContext(sv: args[index], perl: perl))
}

@_specialize(Bool) @_specialize(Int) @_specialize(UInt) @_specialize(Double) @_specialize(String) @_specialize(PerlScalar)
func fetchTail<T : PerlSvConvertible>(startingAt index: Int) throws -> [T] {
func fetchTail<T : PerlScalarConvertible>(startingAt index: Int) throws -> [T] {
guard index < args.count else { return [] }
var tail: [T] = []
tail.reserveCapacity(args.count - index)
Expand All @@ -77,7 +77,7 @@ struct UnsafeXSubStack : UnsafeStack {
}

@_specialize(Bool) @_specialize(Int) @_specialize(UInt) @_specialize(Double) @_specialize(String) @_specialize(PerlScalar)
func fetchTail<T : PerlSvConvertible>(startingAt index: Int) throws -> [String: T] {
func fetchTail<T : PerlScalarConvertible>(startingAt index: Int) throws -> [String: T] {
guard index < args.count else { return [:] }
var tail: [String: T] = [:]
var i = args[index..<args.count].makeIterator()
Expand Down
14 changes: 7 additions & 7 deletions Sources/Perl/Subroutine.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public final class PerlSub : PerlValue {
/// convertible to the desired type.
///
/// - Complexity: O(1).
public func get<T : PerlSvConvertible>(_ index: Int) throws -> T {
public func get<T : PerlScalarConvertible>(_ index: Int) throws -> T {
guard index >= startIndex && index < endIndex else {
throw PerlError.noArgumentOnStack(at: index)
}
Expand All @@ -162,7 +162,7 @@ public final class PerlSub : PerlValue {
/// - Throws: If the argument is not convertible to the desired type.
///
/// - Complexity: O(1).
public func get<T : PerlSvConvertible>(_ index: Int) throws -> T? {
public func get<T : PerlScalarConvertible>(_ index: Int) throws -> T? {
guard index >= startIndex && index < endIndex else { return nil }
return try T?(_fromUnsafeSvContext${rc.title()}: UnsafeSvContext(sv: unsafeArgs[index], perl: perl))
}
Expand Down Expand Up @@ -193,9 +193,9 @@ public final class PerlSub : PerlValue {

%{
def generic(count, tail):
list = map(lambda n: "P" + str(n) + ": PerlSvConvertible", range(0, count))
list = map(lambda n: "P" + str(n) + ": PerlScalarConvertible", range(0, count))
if tail != "fixed":
list.append("T: PerlSvConvertible")
list.append("T: PerlScalarConvertible")
g = ", ".join(list)
return "" if g == "" else "<" + g + ">"

Expand All @@ -219,7 +219,7 @@ public final class PerlSub : PerlValue {
return vars

def result(count):
list = map(lambda n: "PerlSvConvertible?", range(0, count))
list = map(lambda n: "PerlScalarConvertible?", range(0, count))
return "(" + ", ".join(list) + ")"

def bodyArgs(count, tail):
Expand Down Expand Up @@ -283,7 +283,7 @@ extension PerlSub {
/// - Parameter file: A name of a source file subroutine was declared in. Used for debug purposes only.
/// - Parameter body: The body of the XSUB.
@discardableResult
public convenience init(name: String? = nil, file: StaticString = #file, body: @escaping (Args) throws -> [PerlSvConvertible?]) {
public convenience init(name: String? = nil, file: StaticString = #file, body: @escaping (Args) throws -> [PerlScalarConvertible?]) {
self.init(name: name, file: file) {
(stack: UnsafeXSubStack) in
let result = try body(Args(stack.args, perl: stack.perl))
Expand Down Expand Up @@ -330,7 +330,7 @@ extension PerlNamedClass {
/// - Parameter file: A name of a source file subroutine was declared in. Used for debug purposes only.
/// - Parameter body: The body of the XSUB.
@discardableResult
public static func createPerlMethod(_ method: String, file: StaticString = #file, body: @escaping (PerlSub.Args) throws -> [PerlSvConvertible?]) -> PerlSub {
public static func createPerlMethod(_ method: String, file: StaticString = #file, body: @escaping (PerlSub.Args) throws -> [PerlScalarConvertible?]) -> PerlSub {
return PerlSub(name: perlClassName + "::" + method, file: file, body: body)
}
}
Loading

0 comments on commit 1bd1a97

Please sign in to comment.