令人着迷的Rhino

Steve Yegge在google I/O大会上发表了演讲: Rhino on Rails -- Server-side JavaScript on the Java Virtual Machine。

使用 Rhino能够用JavaScript完成Java在server端完成的所有工作!



什么是Rhino

1。用JavaScript实现Java JavaScript implemented in Java!

2。替代Java的解决方案 Drop-in alternative to Java

3。不需要web浏览器环境,没有DOM,没有CSS Not a web-browser environment: no DOM, no CSS

4。只有一种言语的开发,没有浏览器的不一致 Just one language: no browser inconsistencies!

Rhino和Java关系一览 Rhino and Java: a high-level overview



1。Java源代码编译为Java字节码

2。 Rhino JavaScript源代码也能编译为Java字节码

3。网络效果: Rhino能够做到所有Java能做到的


谁在开发Rhino? 谁在使用它?


很多人在使用 Rhino,值得注意的是

1.Googler Norris Boyd,是 Rhino的原始作者,现在的维护者

2.BEA加入了E4X(支持XML的JavaScript)实现.

3. Rhino捆绑进入了Java 6!

4.官方的Java开发工具

5.JavaScript终于变成了写Java的Script :)

为什么要在服务器端使用JavaScript呢?

1。快速原型,并迅速开发:不需要编译,写出代码,马上看到效果,不需要重启;描述功能,然后用Java重写关键地方。

2。功能:
1)支持JavaScript 1.5, 1.6, 1.7 and soon 1.8
2)E4X: Embedded XML
3)函数式编程:通过抽象和组件实现安全性
4)方便:运行派遣,申明式编程

3。Unit Testing
1)最简单的方式创建mock对象
2)不再需要对测试重构

4。代码大小:同样的系统,只需要五分之一至十分之一的代码

工具

调试:内置的命令行调试,提供API
console控制:强大的shell命令
Profiler:通过ContextFactory提供支持
Editors/IDEs支持:
1)IntelliJ:强大的插件支持
2)Aptana: another strong IDE
3)Emacs: js2-mode.el
编译:能够被编译成.class文件
目前Java-JavaScript-Ajax调试暂时还不支持

什么时候你需要使用Rhino

1。当速度和性能比其他东西更加重要: Rhino提供强大的计算能力,没有I/O限制,比如没有数据库

2。为其他java使用者提供一个库:能够更简单使用java, Rhino适应Scripting和应用逻辑

3。你希望使用其他JVM语言


调用Java代码:

1。能够导入任何java class到JavaScript namespace
2。Java静态函数象JavaScript函数一样使用
3。能调用任何java class,通过new操作
Js代码 复制代码
  1. <SPAN class=hilite2>js</SPAN>> new java.util.Date();   
  2. Tue May 13 16:13:43 PDT 2008  


Js代码 复制代码
  1. importClass(Packages.junit.framework.Assert);   
  2. var assertTrue = Assert.assertTrue;   
  3. var assertFalse = Assert.assertFalse;   
  4. var assertEquals = Assert.assertEquals;   
  5. function assertIntEquals(msg, x, y) {   
  6.   assertEquals(msg, new java.lang.Integer(x), new java.lang.Integer(y));   
  7. }   
  8.   
  9. var MyTests = {   
  10.    testBasic: function() {   
  11.      assertEquals(“foobar”, “foo” + “bar”, “concatenation broken!”);   
  12.      assertIntEquals(4, 2 + 2, “addition broken!”);   
  13.    },   
  14. };  


使用Java classes和Interfaces

Js代码 复制代码
  1. <SPAN class=hilite2>js</SPAN>> var t = new Thread(new Runnable() {   
  2.       run: function() { print('hello!') }});   
  3. <SPAN class=hilite2>js</SPAN>> t.start()hello!   
  4.   
  5. <SPAN class=hilite2>js</SPAN>> obj = {run: function() { print('hi') }}   
  6. [object Object]   
  7. <SPAN class=hilite2>js</SPAN>> new Thread(new Runnable(obj)).start()hi  


JavaAdapter 能够创建 “subclass” of a Java class


Js代码 复制代码
  1. <SPAN class=hilite2>js</SPAN>> var obj = {speak: function() { print("<SPAN class=hilite1>Rhino</SPAN> rocks!") }}   
  2. <SPAN class=hilite2>js</SPAN>> obj.run = function() { this.speak(); }   
  3. <SPAN class=hilite2>js</SPAN>> var foo = new JavaAdapter(java.lang.Thread, obj);   
  4. <SPAN class=hilite2>js</SPAN>> foo.start()<SPAN class=hilite1>Rhino</SPAN> rocks!  


Calling Rhino/JavaScript from Java

Js代码 复制代码
  1. import org.mozilla.javascript.Context;   
  2. import org.mozilla.javascript.Scriptable;   
  3.   
  4. public class <SPAN class=hilite1>Rhino</SPAN>Demo {   
  5.    public static void main(String[] args) {   
  6.     Context cx = Context.enter();   
  7.     Scriptable scope = cx.initStandardObjects();   
  8.     // var foo = {a: "hello"}   
  9.     Scriptable foo = cx.newObject(scope);   
  10.     foo.put("a", foo, "hello");   
  11.     scope.put("foo", scope, foo);   
  12.     // fetch and print foo: prints ({a:"hello"})   
  13.     Object result = cx.evaluateString( scope, // our global scope    
  14.       "foo.toSource()",  // <SPAN class=hilite2>js</SPAN> code to evaluate   
  15.              "", 1, null);      // script name, lineno, security   
  16.     System.out.println(cx.<SPAN class=hilite2>js</SPAN>ToJava(result, String.class));   
  17.     Context.exit();   
  18.   }   
  19. }  

使用Rhino的Java层来扩展JavaScript

Js代码 复制代码
  1. function defineBuiltin(target, name, value) {   
  2.   var ScriptableObject =   
  3.      Packages.org.mozilla.javascript.ScriptableObject;   
  4.   ScriptableObject.defineProperty(target, name, value,   
  5.      ScriptableObject.DONTENUM);   
  6. }   
  7.   
  8. defineBuiltin(Function.prototype, 'bind'function(obj) {   
  9.   var _method = this, _args = slice(arguments, 1); // curry args   
  10.     return function() {   
  11.     // this 'arguments' is different from the one above   
  12.     return _method.apply(obj, _args.concat(Array.from(arguments)));   
  13.   };   
  14. });  




Rhino有两个代码路径,interpreter and compiler

Script 运行实现JavaScript方式:
    * Built-in objects
    * Ecma algorithms
    * Java/JavaScript bridge
    * E4X engine
    * RegExp engine
    * Security engine
    * Debugger interface
    * Programmer APIs

Rhino官网看看: http://www.mozilla.org/rhino/
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值