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

Watchers

 avatar  avatar  avatar

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.