Commit f18f5043 authored by Drew's avatar Drew

Initial commit

parents
.atllbuild/
bin/
This diff is collapsed.
DynamicRuntime
© 2016 Drew Crawford.
https://code.sealedabstract.com/drewcrawford/DynamicRuntime
\ No newline at end of file
;; Copyright (c) 2016 Anarchy Tools Contributors.
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http:;;www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;; The build file for all of the targets of the Anarchy Tools build tool.
(package
:name "DynamicRuntime"
:version "0.1"
:tasks {
:default {
:tool "atllbuild"
:sources ["src/**.swift"]
:name "DynamicRuntime"
:output-type "static-library"
:publish-product true
}
:check-build {
:tool "atllbuild"
:sources ["tests/main.swift"]
:name "DynamicRuntimeCheck"
:output-type "executable"
:link-with-product ["DynamicRuntime.a"]
:dependencies ["default"]
:publish-product true
}
:check {
:tool "shell"
:script "bin/DynamicRuntimeCheck"
:dependencies ["check-build"]
}
}
)
/*
Copyright 2016 Drew Crawford
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
private enum Selectors: String, Selector {
case legalText = "legalText"
}
internal final class LegalTextWriter: StaticCallable {
static var selectors: [Selector] { return [Selectors.legalText] }
static func _call(_ selector: Selector, input: Any?) -> Any? {
if selector == Selectors.legalText {
return "DynamicRuntime\n© 2016 Drew Crawford.\nhttps://code.sealedabstract.com/drewcrawford/DynamicRuntime"
}
return nil
}
}
private func _setupOnce() {
DynamicRuntime._register(LegalTextWriter.self)
}
let setupOnce: Void = _setupOnce()
extension DynamicRuntime {
public static var legalTexts: [String] {
return typesRespondingTo(staticSelector: Selectors.legalText).map{$0.call(Selectors.legalText, input: nil) as String?}.flatMap{$0}
}
}
\ No newline at end of file
/*
Copyright 2016 Drew Crawford
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
///Namespace type
public enum DynamicRuntime { }
\ No newline at end of file
/*
Copyright 2016 Drew Crawford
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
///Typically, this is an enum with a string raw value
public protocol Selector {
var rawValue: String { get }
}
func == (lhs: Selector, rhs: Selector) -> Bool { return lhs.rawValue == rhs.rawValue }
///conform to this protocol to allow callers to send you messages
public protocol StaticCallable {
static var selectors: [Selector] { get }
static func _call(_ selector: Selector, input: Any?) -> Any?
}
public struct StringSelector: Selector, ExpressibleByStringLiteral {
let string: String
public init(stringLiteral value: StaticString) {
self.string = String(describing: value)
}
public init(unicodeScalarLiteral value: StaticString) {
self.init(stringLiteral: value)
}
public init(extendedGraphemeClusterLiteral value: StaticString) {
self.init(stringLiteral: value)
}
public var rawValue : String { return string }
}
extension StaticCallable {
public static func call<Output>(_ selector: Selector, input: Any?) -> Output? {
return self._call(selector, input: input) as? Output
}
}
\ No newline at end of file
/*
Copyright 2016 Drew Crawford
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import Dispatch
private var _registeredTypes: [String: Any.Type] = [:]
private let queue = DispatchQueue(label: "DynamicRuntime.Lock")
extension DynamicRuntime {
private static func typeName(of: Any.Type) -> String {
return _typeName(of, qualified: true)
}
internal static func _register(_ type: Any.Type) {
queue.sync {
_registeredTypes[typeName(of: type)] = type
print("registering type \(typeName(of: type))")
}
}
public static func register(_ type: Any.Type) {
let _ = setupOnce
_register(type)
}
public static var registeredTypes: [String: Any.Type] {
let _ = setupOnce
var t: [String:Any.Type]! = [:]
queue.sync {
t = _registeredTypes
}
return t
}
public static func typesRespondingTo(staticSelector: Selector) -> [StaticCallable.Type] {
return registeredTypes.values.flatMap{$0 as? StaticCallable.Type}.filter{$0.selectors.filter{$0 == staticSelector}.count > 0}
}
}
\ No newline at end of file
/*
Copyright 2016 Drew Crawford
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
extension String {
/*func contains(_ str: String) -> Bool {
//the world's dumbest implementation
var startIndex = self.utf8.startIndex
while startIndex != self.utf8.endIndex {
let viewA = self.utf8[startIndex..<self.utf8.index(startIndex, offsetBy: str.utf8.count, limitBy: self.utf8.endIndex)]
if viewA == str.utf8 { return true }
}
return false
}*/
}
/*
Copyright 2016 Drew Crawford
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import DynamicRuntime
precondition(DynamicRuntime.legalTexts == ["DynamicRuntime\n© 2016 Drew Crawford.\nhttps://code.sealedabstract.com/drewcrawford/DynamicRuntime"])
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment