1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00

update the README to reflect the 0.7 API changes.

This commit is contained in:
Charles Lowell 2010-05-31 00:12:36 +03:00
parent ea6bd6f942
commit 789f2a92fb

View file

@ -17,7 +17,8 @@ Embed the V8 Javascript interpreter into Ruby.
* Currently ALPHA software. * Currently ALPHA software.
== SYNOPSIS: == SYNOPSIS:
gem install therubyracer gem install therubyracer (stable)
gem install therubyracer --pre (bleeding edge)
# then in your ruby code # then in your ruby code
@ -25,23 +26,20 @@ Embed the V8 Javascript interpreter into Ruby.
# evaluate some simple javascript # evaluate some simple javascript
V8.eval("7 * 6") #=> 42 cxt = V8::Context.new
cxt.eval('7 * 6') #=> 42
# embed values into the scope of your context # embed values into the scope of your context
V8::Context.open do |cxt| cxt['foo'] = "bar"
cxt['foo'] = "bar" cxt.eval('foo') # => "bar"
cxt.eval('foo') # => "bar"
end
# evaluate a ruby function from javascript # embed ruby code into your scope and call it from javascript
V8::Context.open do |context| cxt["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
# embed a ruby object into your javascript environment # embed a ruby object into your scope and access its properties/methods from javascript
class MyMath class MyMath
def plus(lhs, rhs) def plus(lhs, rhs)
@ -49,50 +47,31 @@ Embed the V8 Javascript interpreter into Ruby.
end end
end end
V8::Context.open do |context| cxt['math'] = MyMath.new
context["math"] = MyMath.new cxt.eval("math.plus(20,22)") #=> 42
context.eval("math.plus(20,22)") #=> 42
end
# make a ruby object *be* your javascript environment # make a ruby object *be* your global javascript scope.
math = MyMath.new math = MyMath.new
V8::Context.open(:with => math) do |context| V8::Context.new(:with => math) do |cxt|
context.eval("plus(20,22)") #=> 42 cxt.eval("plus(20,22)") #=> 42
end end
#or the equivalent #you can do the same thing with Object#eval_js
math.eval_js("plus(20,22)") math.eval_js("plus(20,22)")
# Configure your embedding setup
#COMING SOON!
# Make your standard objects (Object, String, etc...) immutable
V8::Context.open(:sealed => true) do |context|
context.eval("Object.prototype.toString = function() {}") # this is an error!
end
#COMING SOON!
#limit the number of instructions that can be executed in order to prevent
#rogue scripts
V8::Context.open do |context|
context.instruction_limit = 100000
context.eval("while (true);") # => Error!
end
==== Different ways of loading javascript source ==== Different ways of 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.
# evaluate bytes read from any File/IO object: # evaluate bytes read from any File/IO object:
File.open("mysource.js") do |file| File.open("mysource.js") do |file|
eval_js file, "mysource.js" cxt.eval(file, "mysource.js")
end end
# or load it by filename # or load it by filename
V8::Context.open do |context|
context.load("mysource.js") cxt.load("mysource.js")
end
=== Safe by default === Safe by default
@ -116,12 +95,12 @@ exposed by default. E.g.
end end
V8::Context.open do |cxt| V8::Context.new do |cxt|
cxt['a'] = A.new cxt['a'] = A.new
cxt['b'] = B.new cxt['b'] = B.new
cxt.eval("a.a()") # => 'a' cxt.eval("a.a") # => 'a'
cxt.eval("b.b()") # => 'b' cxt.eval("b.b") # => 'b'
cxt.eval("b.a()") # => 'TypeError: undefined property 'a' is not a function' cxt.eval("b.a") # => undef
end end
== REQUIREMENTS: == REQUIREMENTS: