JavaScriptCore_JSPatch小笔记

2016-07-06  本文已影响21人  QihuaZhou

结合使用JSPatch学习一下JSCore,里面也有很多Runtime的知识。

//
//  ViewController.m
//  JSPatch_Test
//
//  Created by nali on 16/6/30.
//  Copyright © 2016年 Kimi. All rights reserved.
//

#import "ViewController.h"
#import "JPEngine.h"

@interface ViewController ()
{
    NSString *globalScript;
    JSContext *globalContext;
    JSVirtualMachine *globalMachine;
}

- (void)showAlert:(NSString *)name;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self initView];
    [self initScript];
    [self draw];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initView
{
    UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(150, 30, 150, 20)];
    [button1 setTitle:@"updateLabel" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(coverDraw) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(150, 60, 150, 20)];
    [button2 setTitle:@"displayAlert" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(displayAlert) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
    UIButton *button3 = [[UIButton alloc] initWithFrame:CGRectMake(150, 90, 150, 20)];
    [button3 setTitle:@"OC_JS" forState:UIControlStateNormal];
    [button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(OC_JS) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];
    
    UIButton *button4 = [[UIButton alloc] initWithFrame:CGRectMake(150, 120, 150, 20)];
    [button4 setTitle:@"JS_OC:block" forState:UIControlStateNormal];
    [button4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button4 addTarget:self action:@selector(JS_OC) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button4];
    
    UIButton *resetJSContext = [[UIButton alloc] initWithFrame:CGRectMake(150, 150, 150, 20)];
    [resetJSContext setTitle:@"resetJSContext" forState:UIControlStateNormal];
    [resetJSContext setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [resetJSContext addTarget:self action:@selector(initScript) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:resetJSContext];
}

- (void)initScript
{
    NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"js"];
    NSString *src = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil];
    self->globalScript = src;
    
//    self->globalMachine = [[JSVirtualMachine alloc] init];
//    self->globalContext = [[JSContext alloc] initWithVirtualMachine:self->globalMachine];
    
    self->globalContext = [[JSContext alloc] init];
    [self->globalContext evaluateScript:self->globalScript];
}

- (void)draw
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    label.center = self.view.center;
    label.text = @"red";
    label.textColor = [UIColor redColor];
    label.backgroundColor = [UIColor lightGrayColor];
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
}

- (void)coverDraw
{
    //[JPEngine startEngine];
    [JPEngine startEngineWithSharedContext:self->globalContext];
    //[JPEngine startEngineWithVirtualMachine:self->globalMachine];
    [JPEngine evaluateScript:self->globalScript];
    
    [self draw];
}

- (void)displayAlert
{
    //[JPEngine startEngine];
    [JPEngine startEngineWithSharedContext:self->globalContext];
    //[JPEngine startEngineWithVirtualMachine:self->globalMachine];
    [JPEngine evaluateScript:self->globalScript];
    
    NSString *cmd = NSStringFromSelector(_cmd);
    [self showAlert:[NSString stringWithFormat:@"%@: add new methods", cmd]];
}

- (void)OC_JS
{
    //JSContext *context= [[JSContext alloc] init];
    
    [self->globalContext evaluateScript:self->globalScript];
    JSValue *value = [self->globalContext[@"multiply"] callWithArguments:@[@"2", @"3"]];
    NSLog(@"%@",[value toString] );//multiply: 6
}

- (void)JS_OC
{
    //JSContext *context = [[JSContext alloc] init];
    
    self->globalContext[@"printSthBlock"] = ^(NSString *sth)
    {
        NSLog(@"2.JS_OC [printSthBlock]: %@", sth);
    };
    
    [self->globalContext evaluateScript:self->globalScript];
}

@end


var multiply = function(a, b) {
    return '1.OC_JS [multiply]: ' + a*b;
};

printSthBlock("hello world");

defineClass('ViewController',{
            draw: function()
            {
            require('UILabel,UIColor');
            var label = UILabel.new();
            label.setFrame({x:100, y:100, width:200, height:30});
            label.setText("greengreengreen");
            label.setTextColor(UIColor.greenColor());
            label.setBackgroundColor(UIColor.lightGrayColor());
            label.setTextAlignment(1);
            self.view().addSubview(label);
                
            var selfView = self.view();
            var center = selfView.center();
            //label.setCenter(center);
            label.setCenter(self.view().center());
            
            //var centerX = self.view().center().x();
            },
            
            showAlert : function(name)
            {
            var alertView = require('UIAlertView').alloc().initWithTitle_message_delegate_cancelButtonTitle_otherButtonTitles("Alert",name, self, "OK",  undefined);
            alertView.show()
            },
    
})

上一篇下一篇

猜你喜欢

热点阅读