Git Product home page Git Product logo

oceval's Introduction

OCEval

Travis support

A tiny JIT Interpreter of Objective-C, dynamically run your code like eval().

中文介绍

Features

  • Run Objective-C code dynamically.
  • Support iOS & OS X.
  • Written by Objective-C.
  • Driven By Unit Tests.
  • Support part of Low-level APIs like block and C funtion.

Usage

Dynamically call Objective-C method

//Example 1
NSString *inputStr = @"return 1 + 3 <= 4 && [NSString string] != nil;";
NSNumber *result = [OCEval eval:inputStr];
NSAssert([result boolValue] == YES, nil);
//Example 2
NSString *inputStr = @"{NSArray *content = @[@6,@7,@8,@9,@1,@2,@3,@4];\
NSComparisonResult (^comparison)(id obj1, id obj2) = ^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {\
return [obj1 doubleValue] > [obj2 doubleValue];\
};\
content = [content sortedArrayUsingComparator:comparison];\
return content;\
}";
NSArray *result = [OCEval eval:inputStr];
NSAssert([result[6] intValue] == 8, nil);

Replace method implementation dynamically

//new implementation code,just call originalInvocation
NSString *viewDidLoad2 = @"{\
[originalInvocation invoke];\
";

[OCEval hookClass:@"ViewController"
         selector:@"viewDidLoad"
         argNames:@[]
          isClass:NO
   implementation:viewDidLoad2];

Even make a dynamical app

Theoretically we could make a whole application written by Objective-C and deliver it through the network. You could read the iOS demo for details.

Installation

Cocoapods

pod 'OCEval'

TO-DO List

Already supported syntax

  • if..else..,do..while..
  • for..in,for
  • @() @[] @{}
  • array[0] or dic[@""]
  • block
  • call C external function
  • mac application
  • i++,++i,i+=1
  • if(a){} : means if(a == nil){}
  • C struct: including CGRect,CGPoint,CGSize,NSRange etc.
  • [super doSth]

Not support yet

  • call C inline function
  • macro like #define xx or typedef: use original value instead.
  • ((YES)&&(NO)) : use (YES && NO) instead
  • [stringformat:@"%d",aInt] : use [stringformat:@"%@",[NSNumber numberWithInt:aInt]] instead
  • ?: : use if else instead
  • _propertyName : use self.propertyName instead
  • if (!a): use if(a == nil) instead

Warning

An app submission to Appstore including this framework will likely be rejected.

Dependency

  • libffi
  • Aspect

oceval's People

Contributors

lilidan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

oceval's Issues

上下文有获取全局变量的方法吗

如题,当原有函数的上下文使用了全局变量类如 NSMutableDictionary *someXX = @{}.mutableCopy 这种,替换后的函数文本应该怎么设置才能获取到这个 someXX

How to eval string?

just like

    NSString *input = @"NSLog(@\"say hello\");";
    [OCEval eval:input];

or

    NSString *input = @"NSLog(@""say hello"");";
    [OCEval eval:input];

both got error. so how to eval string?

Demo运行起来 崩溃Thread 1: "+[NSInvocation _invocationWithMethodSignature:frame:]: method signature argument cannot be nil"

2022-04-24 10:27:57.356288+0800 OCEvalDemo[22215:14541831] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSInvocation _invocationWithMethodSignature:frame:]: method signature argument cannot be nil'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff203fbbb4 __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007fff2019ebe7 objc_exception_throw + 48
2 CoreFoundation 0x00007fff203fe941 +[NSInvocation invocationWithMethodSignature:] + 0
3 OCEval 0x0000000103b1fc8f +[OCMethodNode(invoke) invokeWithCaller:selectorName:argments:] + 175
4 OCEval 0x0000000103b32c1c -[OCPropertyNode excuteWithCtx:] + 604
5 OCEval 0x0000000103b34ad1 -[OCReturnNode excuteWithCtx:] + 1345
6 OCEval 0x0000000103b1f47e -[OCLineNode excuteWithCtx:] + 398
7 OCEval 0x0000000103b3523a -[OCScopeNode excuteWithCtx:] + 362
8 OCEval 0x0000000103b169b7 -[OCControlNode excuteWithCtx:] + 599
9 OCEval 0x0000000103b3523a -[OCScopeNode excuteWithCtx:] + 362
10 OCEval 0x0000000103b35407 -[OCRootNode excuteWithCtx:] + 87
11 OCEval 0x0000000103b17815 __61+[OCEval hookClass:selector:argNames:isClass:implementation:]block_invoke + 725
12 CoreFoundation 0x00007fff2040247c invoking
+ 140
13 CoreFoundation 0x00007fff203ff872 -[NSInvocation invoke] + 305
14 CoreFoundation 0x00007fff203ffb05 -[NSInvocation invokeWithTarget:] + 70
15 OCEval 0x0000000103b0a490 -[AspectIdentifier invokeWithInfo:] + 752
16 OCEval 0x0000000103b0e699 ASPECTS_ARE_BEING_CALLED + 2505
17 CoreFoundation 0x00007fff203ffe57 forwarding + 820
18 CoreFoundation 0x00007fff204021e8 _CF_forwarding_prep_0 + 120
19 UIKitCore 0x00007fff25271009 -[UITableView numberOfRowsInSection:] + 62
20 UIKitCore 0x00007fff2528726e -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 2068
21 UIKitCore 0x00007fff2528ac18 -[UITableViewRowData numberOfRows] + 64
22 UIKitCore 0x00007fff2523e982 -[UITableView noteNumberOfRowsChanged] + 112
23 UIKitCore 0x00007fff2523d4e6 -[UITableView reloadData] + 1021
24 CoreFoundation 0x00007fff2040247c invoking
+ 140
25 CoreFoundation 0x00007fff203ff872 -[NSInvocation invoke] + 305
26 OCEval 0x0000000103b2abb9 +[OCMethodNode(invoke) invokeWithInvocation:argments:] + 2025
27 OCEval 0x0000000103b1fd6f +[OCMethodNode(invoke) invokeWithCaller:selectorName:argments:] + 399
28 OCEval 0x0000000103b2c3ea -[OCMethodNode excuteWithCtx:] + 522
29 OCEval 0x0000000103b1f3e8 -[OCLineNode excuteWithCtx:] + 248
30 OCEval 0x0000000103b3523a -[OCScopeNode excuteWithCtx:] + 362
31 OCEval 0x0000000103b16a36 -[OCControlNode excuteWithCtx:] + 726
32 OCEval 0x0000000103b3523a -[OCScopeNode excuteWithCtx:] + 362
33 OCEval 0x0000000103b12bb8 JPBlockInterpreter + 1592
34 OCEval 0x0000000103b374b9 ffi_closure_unix64_inner + 487
35 OCEval 0x0000000103b3a16a ffi_closure_unix64 + 72
36 Foundation 0x00007fff207df16b NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK + 7
37 Foundation 0x00007fff207df063 -[NSBlockOperation main] + 98
38 Foundation 0x00007fff207e2073 NSOPERATION_IS_INVOKING_MAIN + 17
39 Foundation 0x00007fff207de2b0 -[NSOperation start] + 785
40 Foundation 0x00007fff207e29c7 NSOPERATIONQUEUE_IS_STARTING_AN_OPERATION + 17
41 Foundation 0x00007fff207e250c __NSOQSchedule_f + 182
42 libdispatch.dylib 0x00000001039d6bf7 _dispatch_block_async_invoke2 + 83
43 libdispatch.dylib 0x00000001039c7c0c _dispatch_client_callout + 8
44 libdispatch.dylib 0x00000001039d6376 _dispatch_main_queue_callback_4CF + 1195
45 CoreFoundation 0x00007fff2036985d CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 9
46 CoreFoundation 0x00007fff203640ba __CFRunLoopRun + 2772
47 CoreFoundation 0x00007fff20363103 CFRunLoopRunSpecific + 567
48 GraphicsServices 0x00007fff2c851cd3 GSEventRunModal + 139
49 UIKitCore 0x00007fff24ffbe63 -[UIApplication _run] + 928
50 UIKitCore 0x00007fff25000a53 UIApplicationMain + 101
51 OCEvalDemo 0x000000010382ebb8 main + 104
52 dyld 0x0000000103844e1e start_sim + 10
53 ??? 0x0000000000000001 0x0 + 1
)
libc++abi: terminating with uncaught exception of type NSException
dyld4 config: DYLD_ROOT_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot DYLD_LIBRARY_PATH=/Users/caoliang/Library/Developer/Xcode/DerivedData/OCEvalDemo-cbbuxvksytvzbdhkdtwawbifbzoz/Build/Products/Debug-iphonesimulator:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libBacktraceRecording.dylib:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libMainThreadChecker.dylib:/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib DYLD_FRAMEWORK_PATH=/Users/caoliang/Library/Developer/Xcode/DerivedData/OCEvalDemo-cbbuxvksytvzbdhkdtwawbifbzoz/Build/Products/Debug-iphonesimulator
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSInvocation _invocationWithMethodSignature:frame:]: method signature argument cannot be nil'
terminating with uncaught exception of type NSException
CoreSimulator 776.4 - Device: iPhone 13 Pro Max (B2851D2D-EA0B-4ABB-A2C0-53CA3C7E0DBB) - Runtime: iOS 15.0 (19A339) - DeviceType: iPhone 13 Pro Max
辛苦有时间看下

我觉得可以通过审核

源码还没有细抠,逐步断点去理解。我大致的理解是解析oc语法,nsinvoke实现消息转发和aspects实现method swizling,对吗?
nsinvoke实现消息转发和aspects实现method swizling在上线审核上都没问题。
应该可以使用在线上吧?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.