mirror of
https://github.com/rubyjs/therubyracer
synced 2023-03-27 23:21:42 -04:00
Add utility methods to do quick and dirty eval
This commit is contained in:
parent
6adef0bfb6
commit
fd6f42879c
2 changed files with 16 additions and 2 deletions
|
@ -20,6 +20,10 @@ Embed the V8 Javascript interpreter into Ruby.
|
||||||
|
|
||||||
# evaluate some simple javascript
|
# evaluate some simple javascript
|
||||||
|
|
||||||
|
V8.eval("7 * 6") #=> 42
|
||||||
|
|
||||||
|
# embed values into the scope of your context
|
||||||
|
|
||||||
V8::Context.open do |cxt|
|
V8::Context.open do |cxt|
|
||||||
cxt['foo'] = "bar"
|
cxt['foo'] = "bar"
|
||||||
cxt.eval('foo') # => "bar"
|
cxt.eval('foo') # => "bar"
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
module V8
|
require 'stringio'
|
||||||
|
|
||||||
|
module V8
|
||||||
class Context
|
class Context
|
||||||
def initialize(opts = {})
|
def initialize(opts = {})
|
||||||
@native = C::Context.new(opts[:with])
|
@native = C::Context.new(opts[:with])
|
||||||
|
@ -46,7 +48,15 @@ module V8
|
||||||
|
|
||||||
def self.open(opts = {}, &block)
|
def self.open(opts = {}, &block)
|
||||||
new(opts).open(&block)
|
new(opts).open(&block)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.eval(source)
|
||||||
|
new.eval(source)
|
||||||
|
end
|
||||||
|
|
||||||
|
def V8.eval(*args)
|
||||||
|
V8::Context.eval(*args)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class ContextError < StandardError
|
class ContextError < StandardError
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue