瀏覽代碼

Add SecureCompute Interface

xcbosa mbp16 2 年之前
父節點
當前提交
a7245505e2

+ 12 - 0
XCTreeLang.xcodeproj/project.pbxproj

@@ -66,6 +66,7 @@
 		756916B92A2B86AA005FF14B /* XCTLSwiftInvocation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 756916B82A2B86AA005FF14B /* XCTLSwiftInvocation.swift */; };
 		756916BD2A2B91E7005FF14B /* NSArray+Enumerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 756916BC2A2B91E7005FF14B /* NSArray+Enumerator.swift */; };
 		756916BF2A2B97BF005FF14B /* InitialContent.xct in Resources */ = {isa = PBXBuildFile; fileRef = 756916BE2A2B97BF005FF14B /* InitialContent.xct */; };
+		75978AB02A408452003162DA /* XCTLSecureComputeDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 75978AAF2A408452003162DA /* XCTLSecureComputeDelegate.swift */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -160,6 +161,7 @@
 		756916C42A2BA600005FF14B /* Screen-Shot-Example1.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Screen-Shot-Example1.png"; sourceTree = "<group>"; };
 		756916C72A2BA713005FF14B /* Screen-Shot-Example2.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Screen-Shot-Example2.png"; sourceTree = "<group>"; };
 		75978AA22A3F165D003162DA /* XCFramework.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = XCFramework.sh; sourceTree = "<group>"; };
+		75978AAF2A408452003162DA /* XCTLSecureComputeDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCTLSecureComputeDelegate.swift; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -206,6 +208,7 @@
 		7520C3DC2A283AFC0010E7F8 /* XCTreeLang */ = {
 			isa = PBXGroup;
 			children = (
+				75978AAE2A408444003162DA /* SecureCompute */,
 				7569168F2A28C447005FF14B /* NativeTypes */,
 				7520C3DD2A283AFC0010E7F8 /* XCTreeLang.h */,
 				7520C4172A283B1E0010E7F8 /* InitializerImpl */,
@@ -374,6 +377,14 @@
 			path = GithubImages;
 			sourceTree = "<group>";
 		};
+		75978AAE2A408444003162DA /* SecureCompute */ = {
+			isa = PBXGroup;
+			children = (
+				75978AAF2A408452003162DA /* XCTLSecureComputeDelegate.swift */,
+			);
+			path = SecureCompute;
+			sourceTree = "<group>";
+		};
 /* End PBXGroup section */
 
 /* Begin PBXHeadersBuildPhase section */
@@ -532,6 +543,7 @@
 				756916992A28E1D9005FF14B /* XCTLContinueStatement.swift in Sources */,
 				7520C42D2A283B1E0010E7F8 /* XCTLListStatement.swift in Sources */,
 				7520C4222A283B1E0010E7F8 /* XCTLInitStatement.swift in Sources */,
+				75978AB02A408452003162DA /* XCTLSecureComputeDelegate.swift in Sources */,
 				756916872A2851DF005FF14B /* XCTLStream.swift in Sources */,
 				756916B92A2B86AA005FF14B /* XCTLSwiftInvocation.swift in Sources */,
 				756916A22A28FE20005FF14B /* XCTLRuntimeTypeInstance.m in Sources */,

+ 2 - 2
XCTreeLang.xcodeproj/xcuserdata/xcbosa.xcuserdatad/xcschemes/xcschememanagement.plist

@@ -7,7 +7,7 @@
 		<key>TestApp.xcscheme_^#shared#^_</key>
 		<dict>
 			<key>orderHint</key>
-			<integer>1</integer>
+			<integer>0</integer>
 		</dict>
 		<key>XCFramework.xcscheme_^#shared#^_</key>
 		<dict>
@@ -17,7 +17,7 @@
 		<key>XCTreeLang.xcscheme_^#shared#^_</key>
 		<dict>
 			<key>orderHint</key>
-			<integer>0</integer>
+			<integer>1</integer>
 		</dict>
 	</dict>
 </dict>

+ 15 - 0
XCTreeLang/SecureCompute/XCTLSecureComputeDelegate.swift

@@ -0,0 +1,15 @@
+//
+//  XCTLSecureComputeDelegate.swift
+//  XCTreeLang
+//
+//  Created by 邢铖 on 2023/6/19.
+//
+
+import UIKit
+
+@objc
+public protocol XCTLSecureComputeDelegate: AnyObject {
+    
+    func secureCompute(_ engine: XCTLEngine, requireCodeFileForName name: String) -> String?
+    
+}

+ 23 - 11
XCTreeLang/XCTLEngine.swift

@@ -29,6 +29,8 @@ public class XCTLEngine: NSObject {
     
     public static var shared: XCTLEngine = XCTLEngine()
     
+    public weak var secureComputeDelegate: XCTLSecureComputeDelegate?
+    
     private static var debugMode: Bool = false
     
     private var prototypes = [String : XCTLGenerateProtocol.Type]()
@@ -144,20 +146,30 @@ public class XCTLEngine: NSObject {
 
 public extension UIViewController {
     
+    private func execute(xctFile: String, withContent content: String) {
+        let time = Date()
+        guard let ast = XCTLEngine.shared.compile(code: content) else {
+            fatalError("Unable to compile XCT file \(xctFile)")
+        }
+        do {
+            try XCTLEngine.shared.evaluate(ast: ast, sourceObject: self)
+        } catch let error {
+            fatalError("Runtime error when execute XCT file \(xctFile): \(error)")
+        }
+        print("[XCTLEngine-ScanClass] Finish execute \(xctFile) in \(time.distance(to: Date())) seconds")
+    }
+    
     @objc func swizzledXCTLEngineViewDidLoad() {
         let className = "\(self.classForCoder)"
-        if let xctFile = Bundle.main.path(forResource: className, ofType: "xct") {
+        if let xctCode = XCTLEngine.shared.secureComputeDelegate?.secureCompute(XCTLEngine.shared,
+                                                                                requireCodeFileForName: "\(className).xct") {
+            print("[XCTLEngine-ScanClass] Execute secure network file \(className).xct")
+            self.execute(xctFile: "\(className).xct", withContent: xctCode)
+        }
+        if let xctFile = Bundle.main.path(forResource: className, ofType: "xct"),
+            let xctCode = try? String(contentsOfFile: xctFile) {
             print("[XCTLEngine-ScanClass] Execute disk file \(className).xct")
-            let time = Date()
-            guard let ast = XCTLEngine.shared.compile(fullFilePath: xctFile) else {
-                fatalError("Unable to compile XCT file \(xctFile)")
-            }
-            do {
-                try XCTLEngine.shared.evaluate(ast: ast, sourceObject: self)
-            } catch let error {
-                fatalError("Runtime error when execute XCT file \(xctFile): \(error)")
-            }
-            print("[XCTLEngine-ScanClass] Finish execute \(className).xct in \(time.distance(to: Date())) seconds")
+            self.execute(xctFile: "\(className).xct", withContent: xctCode)
         }
         self.perform(NSSelectorFromString("UIViewController.originalViewDidLoad"))
     }