- 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
联系方式
- GitHub : dKingbin
- Email : [email protected]
License
Copyright (c) 2019 dKingbin
Licensed under MIT or later
DynamicOC required features are based on or derives from projects below: