messed-up code formatting ...

This commit is contained in:
kares 2013-06-16 16:16:09 +02:00
parent 927e82ad60
commit 1dfdf252a4
1 changed files with 89 additions and 94 deletions

125
README.md
View File

@ -22,94 +22,94 @@ Embed the Mozilla Rhino JavaScript interpreter into Ruby
3. Our shark's in the JavaScript! 3. Our shark's in the JavaScript!
```ruby ```ruby
require 'rhino' require 'rhino'
``` ```
* evaluate some simple JavaScript using `eval_js`: * evaluate some simple JavaScript using `eval_js`:
```ruby ```ruby
eval_js "7 * 6" #=> 42 eval_js "7 * 6" #=> 42
``` ```
* that's quick and dirty, but if you want more control over your environment, * that's quick and dirty, but if you want more control over your environment,
use a `Context`: use a `Context`:
```ruby ```ruby
Rhino::Context.open do |cxt| Rhino::Context.open do |context|
cxt['foo'] = "bar" context['foo'] = "bar"
cxt.eval('foo') # => "bar" context.eval('foo') # => "bar"
end end
``` ```
* evaluate a Ruby function from JavaScript: * evaluate a Ruby function from JavaScript:
```ruby ```ruby
Rhino::Context.open do |context| Rhino::Context.open do |context|
context["say"] = lambda {|word, times| word * times} context["say"] = lambda {|word, times| word * times}
context.eval("say("Hello", 3)") #=> HelloHelloHello context.eval("say("Hello", 3)") #=> HelloHelloHello
end end
``` ```
* embed a Ruby object into your JavaScript environment * embed a Ruby object into your JavaScript environment
```ruby ```ruby
class MyMath class MyMath
def plus(lhs, rhs) def plus(a, b)
lhs + rhs a + b
end
end end
end
Rhino::Context.open do |context| Rhino::Context.open do |context|
context["math"] = MyMath.new context["math"] = MyMath.new
context.eval("math.plus(20, 22)") #=> 42 context.eval("math.plus(20, 22)") #=> 42
end end
``` ```
* make a Ruby object *be* your JavaScript environment * make a Ruby object *be* your JavaScript environment
```ruby ```ruby
math = MyMath.new math = MyMath.new
Rhino::Context.open(:with => math) do |context| Rhino::Context.open(:with => math) do |context|
context.eval("plus(20, 22)") #=> 42 context.eval("plus(20, 22)") #=> 42
end end
# or the equivalent # or the equivalent
math.eval_js("plus(20, 22)") math.eval_js("plus(20, 22)")
``` ```
* configure your embedding setup ### Context Configuration
* make your standard objects (Object, String, etc...) immutable: * make your standard objects (Object, String, etc...) immutable:
```ruby ```ruby
Rhino::Context.open(:sealed => true) do |context| Rhino::Context.open(:sealed => true) do |context|
context.eval("Object.prototype.toString = function() {}") # this is an error! context.eval("Object.prototype.toString = function() {}") # this is an error!
end end
``` ```
* turn on Java integration from JavaScript (probably a bad idea): * turn on Java integration from JavaScript (probably a bad idea):
```ruby ```ruby
Rhino::Context.open(:java => true) do |context| Rhino::Context.open(:java => true) do |context|
context.eval("java.lang.System.exit()") # it's dangerous! context.eval("java.lang.System.exit()") # it's dangerous!
end end
``` ```
* limit the number of instructions that can be executed to prevent rogue code: * limit the number of instructions that can be executed to prevent rogue code:
```ruby ```ruby
Rhino::Context.open(:restrictable => true) do |context| Rhino::Context.open(:restrictable => true) do |context|
context.instruction_limit = 100000 context.instruction_limit = 100000
context.eval("while (true);") # => Rhino::RunawayScriptError context.eval("while (true);") # => Rhino::RunawayScriptError
end end
``` ```
* limit the time a script executes (rogue scripts): * limit the time a script executes (rogue scripts):
```ruby ```ruby
Rhino::Context.open(:restrictable => true, :java => true) do |context| Rhino::Context.open(:restrictable => true, :java => true) do |context|
context.timeout_limit = 1.5 # seconds context.timeout_limit = 1.5 # seconds
context.eval %Q{ context.eval %Q{
for (var i = 0; i < 100; i++) { for (var i = 0; i < 100; i++) {
java.lang.Thread.sleep(100); java.lang.Thread.sleep(100);
} }
} # => Rhino::ScriptTimeoutError } # => Rhino::ScriptTimeoutError
end end
``` ```
### Loading JavaScript source ### Loading JavaScript Source
In addition to just evaluating strings, you can also use streams such as files: In addition to just evaluating strings, you can also use streams such as files:
@ -135,16 +135,16 @@ https://github.com/cowboyd/therubyracer/wiki/Accessing-Ruby-Objects-From-JavaScr
Thus you end-up calling arbitrary no-arg methods as if they were JavaScript properties, Thus you end-up calling arbitrary no-arg methods as if they were JavaScript properties,
since instance accessors (properties) and methods (functions) are indistinguishable: since instance accessors (properties) and methods (functions) are indistinguishable:
```ruby ```ruby
Rhino::Context.open do |context| Rhino::Context.open do |context|
context['Time'] = Time context['Time'] = Time
context.eval('Time.now') context.eval('Time.now')
end end
``` ```
However, you can customize this behavior and there's another access implementation However, you can customize this behavior and there's another access implementation
that attempts to mirror only attributes as properties as close as possible: that attempts to mirror only attributes as properties as close as possible:
```ruby ```ruby
class Foo class Foo
attr_accessor :bar attr_accessor :bar
def initialize def initialize
@ -154,21 +154,21 @@ that attempts to mirror only attributes as properties as close as possible:
def check_bar def check_bar
bar == "bar" bar == "bar"
end end
end end
Rhino::Ruby::Scriptable.access = :attribute Rhino::Ruby::Scriptable.access = :attribute
Rhino::Context.open do |context| Rhino::Context.open do |context|
context['Foo'] = Foo context['Foo'] = Foo
context.eval('var foo = new Foo()') context.eval('var foo = new Foo()')
context.eval('foo.bar') # get property using reader context.eval('foo.bar') # get property using reader
context.eval('foo.bar = null') # set property using writer context.eval('foo.bar = null') # set property using writer
context.eval('foo.check_bar()') # called like a function context.eval('foo.check_bar()') # called like a function
end end
``` ```
If you happen to come up with your own access strategy, just set it directly : If you happen to come up with your own access strategy, just set it directly :
```ruby ```ruby
Rhino::Ruby::Scriptable.access = FooApp::BarAccess.instance Rhino::Ruby::Scriptable.access = FooApp::BarAccess.instance
``` ```
## Safe by default ## Safe by default
@ -181,26 +181,21 @@ functions. Nothing from the Ruby world is accessible.
For Ruby objects that you explicitly embed into JavaScript, only the +public+ For Ruby objects that you explicitly embed into JavaScript, only the +public+
methods "defined in their classes" are exposed by default e.g. methods "defined in their classes" are exposed by default e.g.
```ruby ```ruby
class A class A
def a def a; 'a'; end
"a" end
end
end
class B < A class B < A
def b def b; 'b'; end
"b" end
end
end
Rhino::Context.open do |context|
Rhino::Context.open do |cxt| context['a'] = A.new
cxt['a'] = A.new context['b'] = B.new
cxt['b'] = B.new context.eval("a.a()") # => 'a'
cxt.eval("a.a()") # => 'a' context.eval("b.b()") # => 'b'
cxt.eval("b.b()") # => 'b' context.eval("b.a()") # => 'TypeError: undefined property 'a' is not a function'
cxt.eval("b.a()") # => 'TypeError: undefined property 'a' is not a function' end
end
``` ```
## Context Customizations ## Context Customizations
@ -213,8 +208,8 @@ using system properties e.g. to force interpreted mode :
You might also set these programatically as a default for all created contexts : You might also set these programatically as a default for all created contexts :
```ruby ```ruby
Rhino::Context.default_optimization_level = 1 Rhino::Context.default_optimization_level = 1
Rhino::Context.default_javascript_version = 1.6 Rhino::Context.default_javascript_version = 1.6
``` ```
Or using plain old JAVA_OPTS e.g. when setting JavaScript version : Or using plain old JAVA_OPTS e.g. when setting JavaScript version :
@ -234,11 +229,11 @@ Officially supported versions of Rhino's _js.jar_ are packaged separately as
feel like missing something available with Rhino. For experimenters the jar can feel like missing something available with Rhino. For experimenters the jar can
be overriden by defining a `Rhino::JAR_PATH` before `require 'rhino'` e.g. : be overriden by defining a `Rhino::JAR_PATH` before `require 'rhino'` e.g. :
```ruby ```ruby
module Rhino module Rhino
JAR_PATH = File.expand_path('lib/rhino/build/rhino1_7R5pre/js.jar') JAR_PATH = File.expand_path('lib/rhino/build/rhino1_7R5pre/js.jar')
end end
# ... # ...
require 'rhino' require 'rhino'
``` ```
## LICENSE: ## LICENSE: