V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
dKingbin
V2EX  ›  iOS

基于 flex/yacc 的 iOS 热更新框架

  •  
  •   dKingbin · Jul 30, 2019 · 3362 views
    This topic created in 2475 days ago, the information mentioned may be changed or developed.

    English README

    • DynamicOC 是一个功能上与JSPath类似,但是仅需要编写原生 OC 语法就能实现热更新(hotfix)的功能。

    功能特点

    • 动态执行 OC 代码
    • 动态执行 C 函数和 block 异步调用
    • 动态添加属性
    • 动态替换方法
    • 动态添加方法
    • 有完善的单元测试
    • flex/yacc 实现强大的 OC 语法解析器
    • 支持 CGRect/CGSize/CGPoint/NSRange/UIEdgeInsets/CGAffineTransform 常用结构体 ...

    基本用法

    动态执行 block

    NSString* text = @" \
    	__block int result = 0;\
    	UIView* view = [[UIView alloc]init];\
    	void(^blk)(int value) = ^(int value){\
    		view.tag = value;\
    	};\
    blk(1024);\
    return view.tag;";
    
    ASTNode* root = [ASTUtil parseString:text];
    [ASTUtil linkContextToRoot:root];
    ASTVariable* result = [root execute];
    NSAssert([result.value doubleValue] == 1024, nil);
    

    动态执行 C 函数

    int echo(int value) {
    	return value;
    }
    
    NSString* text = @" \
    [OCCfuntionHelper defineCFunction:@\"echo\" types:@\"int, int\"]; \
    return echo(1024);";
    
    ASTNode* root = [ASTUtil parseString:text];
    [ASTUtil linkContextToRoot:root];
    ASTVariable* result = [root execute];
    NSAssert([result.value doubleValue] == 1024, nil);
    

    动态添加 Property

    NSString* text = @" \
    [OCCfuntionHelper defineCFunction:@\"objc_setAssociatedObject\" types:@\"void,id,void *,id,unsigned int\"];\
    [OCCfuntionHelper defineCFunction:@\"objc_getAssociatedObject\" types:@\"id,id,void *\"];\
    NSString* key = @\"key\"; \
    objc_setAssociatedObject(self, key, @(1024), 1);\
    return objc_getAssociatedObject(self, key);";
    
    ASTNode* root = [ASTUtil parseString:text];
    [ASTUtil linkContextToRoot:root];
    ASTVariable* result = [root execute];
    NSAssert([result.value doubleValue] == 1024, nil);
    

    已支持语法

    • [x] if/else while do/while for
    • [x] return break continue
    • [x] i++ i-- ++i --i
    • [x] +i -i !i
    • [x] + - * / %等四则运算
    • [x] >> << & | ^ 等位运算
    • [x] && || >= <= != > < 等比较运算
    • [x] ?:
    • [x] __block
    • [x] array[i] dict[@""]
    • [x] @666 @() @[] @{}
    • [x] self super
    • [x] self.property
    • [x] self->_property
    • [x] most of objective-c keyword

    TODO

    • [ ] @avaiable()
    • [ ] [NSString stringWithFormat:"%d",value] : use [NSString stringWithFormat:"%@",@(value)] instead。
    • [ ] dispatch_async / dispatch_after ...
    • [ ] *stop =YES, in block
    • [ ] fix bugs

    联系方式

    License

    Copyright (c) 2019 dKingbin
    Licensed under MIT or later
    

    DynamicOC required features are based on or derives from projects below:

    Supplement 1  ·  Aug 1, 2019

    Warnning

    纯粹是技术分享,不要用于上架操作!
    
    mrrobot97
        1
    mrrobot97  
       Jul 30, 2019
    你觉得 App Store 会容忍这么干吗
    wobuhuicode
        2
    wobuhuicode  
       Jul 31, 2019
    可以列出已经上线了的 APP 吗,想去举报
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2943 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 89ms · UTC 10:08 · PVG 18:08 · LAX 03:08 · JFK 06:08
    ♥ Do have faith in what you're doing.