//
// BlockBasement__BlockObjectViewController.swift
// iOS底层
//
// Created by yanqunchao on 2019/5/19.
// Copyright © 2019 yanqunchao. All rights reserved.
//
import UIKit
class BlockBasement__BlockObjectViewController: UIViewController {
typealias FuncPt = ((MainBlockImpl0)->())
typealias CopyFuncPt = ((MainBlockImpl0,MainBlockImpl0)->())
typealias DisposeFuncPt = ((MainBlockImpl0)->())
typealias BlockByrefObjectCopy = ((__BlockByrefPerson0,__BlockByrefPerson0)->())
typealias BlockByrefObjectCopyDispose = ((__BlockByrefPerson0)->())
/*
* __block修饰的变量的包装类
**/
class __BlockByrefPerson0: NSObject {
var isa:UnsafeRawPointer?
var __forwarding:__BlockByrefPerson0?
var flags:Int?
var size:Int?
var byrefCopy:BlockByrefObjectCopy?
var byrefDispose:BlockByrefObjectCopyDispose?
var __strongPerson:MJPerson? // 强引用
// var __weakPerson:MJPerson? // 弱引用
override init() {
}
init(isa:UnsafeRawPointer?,flags:Int,size:Int,byrefCopy:@escaping BlockByrefObjectCopy,byrefDispose:@escaping BlockByrefObjectCopyDispose, person:MJPerson) {
super.init()
self.isa = isa
self.__forwarding = self
self.flags = flags
self.size = size
self.byrefCopy = byrefCopy
self.byrefDispose = byrefDispose
self.__strongPerson = person
// self.weakPerson = person
}
}
/*
* 函数实现类
**/
class BlockImpl: NSObject {
var isa:UnsafeRawPointer?
var Flags:Int?
var Reserved:Int?
var FuncPtr:FuncPt? //函数地址,
}
/*
* 访问对象类型局部变量的Desc类
**/
class MainBlockDesc0: NSObject {
var reserved:Int?
var BlockSize:Int?
var copy:CopyFuncPt?
var dispose:DisposeFuncPt?
init(reserved:Int,BlockSize:Int,copy:@escaping CopyFuncPt,dipose:@escaping DisposeFuncPt) {
self.reserved = reserved
self.BlockSize = BlockSize
self.copy = copy
self.dispose = dipose
}
override init() {
}
}
/*
* 访问auto局部变量(对象类型)的block类
**/
class MainBlockImpl0: NSObject {
var imp: BlockImpl?
var Desc: MainBlockDesc0?
var person: __BlockByrefPerson0?
init(fp:@escaping FuncPt,Desc:MainBlockDesc0,flag:Int, byrefPerson:__BlockByrefPerson0) {
imp = BlockImpl()
// imp!.isa = &_NSConcreateStackBlock
imp?.Flags = 0
imp?.FuncPtr = fp
self.Desc = Desc
self.person = byrefPerson
}
override init() {
}
}
/*
*block调用的函数地址
**/
func mainBlockFunc0(block:MainBlockImpl0){
let person = MJPerson()
person.age = 40
block.person?.__forwarding?.__strongPerson = person
}
/*
*当block调用copy操作的时候,也会对__BlockByrefPerson0对象进行copy操作,就会调用此函数
*若果是强引用,就对__block修饰的对象进行retain操作,若过是__weak修饰的__block类型,就进行弱引用
**/
func blockByrefObjectCopy(dest:__BlockByrefPerson0,src:__BlockByrefPerson0){
blockObjectAssign(destObjc: dest.__strongPerson!, srcObjc: src.__strongPerson!, type: 131)
}
/*
*当__BlockByrefPerson0销毁时,会对__strongPerson进行release操作
**/
func blockByrefObjectDispose(src:__BlockByrefPerson0){
blockObjectDispose(srcObjc: src.__strongPerson!, type: 131)
}
/*
*当block调用copy操作的时候,就会调用此函数
**/
func mainBlockCopy0(destBlock:MainBlockImpl0,srcBlock:MainBlockImpl0){
blockObjectAssign(destObjc: destBlock.person!, srcObjc: srcBlock.person!, type: 8) //8表示 BLOCK_FIELD_IS_BYREF
}
/*
*当block从堆中移除,就会调用此函数
**/
func mainBlockDispose0(srcBlock:MainBlockImpl0){
blockObjectDispose(srcObjc: srcBlock.person!, type: 3)
}
/*
* 依据传入的srcObjc的类型,决定是否对其强引用.
* 如果是strong,就会对其进行强引用(类似于retainCount + 1 的操作),如果是weak,就会对其弱引用。
**/
func blockObjectAssign(destObjc:AnyObject,srcObjc:AnyObject,type:Int){
// if srcObjc 为强引用 {
//
// }else{
//
// }
}
/*
*会自动释放引用的auto变量,类似于 retainCount - 1 的操作
**/
func blockObjectDispose(srcObjc:AnyObject,type:Int){
}
class MJPerson: NSObject {
var age: Int?
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
blockWith__BlockLocalVar()
}
func blockWith__BlockLocalVar(){ // // 修改局部变量的block
let person = MJPerson()
person.age = 10
let byrefPerson = __BlockByrefPerson0(isa: UnsafeRawPointer(bitPattern: 0), flags: 33554432, size: MemoryLayout.size(ofValue: MainBlockImpl0.self), byrefCopy: blockByrefObjectCopy, byrefDispose: blockByrefObjectDispose, person: person)
let mainBlockDesc0Data = MainBlockDesc0(reserved: 0, BlockSize: MemoryLayout.size(ofValue: MainBlockImpl0.self), copy: mainBlockCopy0,dipose: mainBlockDispose0)
let block = MainBlockImpl0(fp: mainBlockFunc0, Desc: mainBlockDesc0Data,flag: 0, byrefPerson: byrefPerson)
block.imp?.FuncPtr!(block)
print(byrefPerson.__strongPerson!.age!)
}
}
网友评论