Skip to content

Latest commit

 

History

History
executable file
·
116 lines (91 loc) · 2.92 KB

README-chs.md

File metadata and controls

executable file
·
116 lines (91 loc) · 2.92 KB

English README | 原理介绍

DynamicOC

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];
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];
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];
ASTVariable* result = [root execute];
NSAssert([result.value doubleValue] == 1024, nil);

已支持语法

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

TODO

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

联系方式

Warnning

纯粹是技术分享,鉴于JSPath的被禁,不建议用于上架Appstore。

License

Copyright (c) 2019 letqingbin
Licensed under MIT or later

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