Class: Johnson::Runtime
- Inherits:
-
Object
- Object
- Johnson::Runtime
- Defined in:
- lib/johnson/runtime.rb
Overview
An interface to a JavaScript engine.
Direct Known Subclasses
Constant Summary collapse
- PRELUDE_PATH =
File. File.dirname(__FILE__) + "/js/prelude.js"
- PRELUDE =
:nodoc:
IO.read PRELUDE_PATH
Class Attribute Summary collapse
-
.runtimes ⇒ Object
readonly
Returns the value of attribute runtimes.
Class Method Summary collapse
-
.new(*args) ⇒ Object
Create a new Runtime instance, using the default JavaScript engine.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access the
keyproperty of the JavaScriptglobalobject. -
#[]=(key, value) ⇒ Object
Set the
keyproperty of the JavaScriptglobalobject tovalue. -
#compile(script, filename = nil, linenum = nil, global = nil) ⇒ Object
Compile the JavaScript source in
script. -
#delegate ⇒ Object
Deprecated: Previously, returned the underlying JavaScript engine instance.
-
#evaluate(script, filename = nil, linenum = nil) ⇒ Object
Execute the JavaScript source in
script. -
#evaluate_compiled_script(script, scope = nil) ⇒ Object
Evaluates the given JS script, that should have been returned by a previous call to #compile().
-
#global ⇒ Object
The JavaScript unique Global Object.
-
#initialize ⇒ Runtime
constructor
Install the Johnson prelude into this runtime environment.
-
#load(*files) ⇒ Object
Load and execute the named JavaScript
files. -
#require(*files) ⇒ Object
Search the Ruby load path for each of the named
files, and evaluate them if they have not yet been loaded.
Constructor Details
#initialize ⇒ Runtime
Install the Johnson prelude into this runtime environment.
52 53 54 55 56 |
# File 'lib/johnson/runtime.rb', line 52 def initialize # :notnew: evaluate PRELUDE, PRELUDE_PATH, 1 global.Johnson.runtime = self global['Ruby'] = Object end |
Class Attribute Details
.runtimes ⇒ Object (readonly)
Returns the value of attribute runtimes.
129 130 131 |
# File 'lib/johnson/runtime.rb', line 129 def runtimes @runtimes end |
Class Method Details
.new(*args) ⇒ Object
Create a new Runtime instance, using the default JavaScript engine.
Optionally takes a parameter specifying which engine to use, but this is deprecated; instead, just create an instance of that engine's runtime directly.
:call-seq:
new(runtime_class=nil,={})
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/johnson/runtime.rb', line 29 def self.new(*args) return super if self < Johnson::Runtime runtime = args.shift = {} if runtime if runtime.is_a? Hash = runtime runtime = nil else = args.shift end end if runtime.is_a? Class runtime = runtime.new( ) elsif runtime runtime else default.new( ) end end |
Instance Method Details
#[](key) ⇒ Object
Access the key property of the JavaScript global object.
60 61 62 |
# File 'lib/johnson/runtime.rb', line 60 def [](key) global[key] end |
#[]=(key, value) ⇒ Object
Set the key property of the JavaScript global object to
value.
67 68 69 |
# File 'lib/johnson/runtime.rb', line 67 def []=(key, value) global[key] = value end |
#compile(script, filename = nil, linenum = nil, global = nil) ⇒ Object
Compile the JavaScript source in script. If supplied, the script
is marked as starting on line linenum of filename.
114 115 116 |
# File 'lib/johnson/runtime.rb', line 114 def compile(script, filename=nil, linenum=nil, global=nil) raise NotImplementedError end |
#delegate ⇒ Object
Deprecated: Previously, returned the underlying JavaScript engine instance. Now returns self.
14 15 16 |
# File 'lib/johnson/runtime.rb', line 14 def delegate self end |
#evaluate(script, filename = nil, linenum = nil) ⇒ Object
Execute the JavaScript source in script. If supplied, the script
is marked as starting on line linenum of filename.
Equivalent to calling RubyLandScript#execute on the result of Runtime#compile.
78 79 80 81 82 |
# File 'lib/johnson/runtime.rb', line 78 def evaluate(script, filename = nil, linenum = nil) return nil if script.nil? compiled_script = compile(script, filename, linenum) evaluate_compiled_script(compiled_script) end |
#evaluate_compiled_script(script, scope = nil) ⇒ Object
Evaluates the given JS script, that should have been returned by a previous call to #compile().
121 122 123 |
# File 'lib/johnson/runtime.rb', line 121 def evaluate_compiled_script(script,scope=nil) raise NotImplementedError end |
#global ⇒ Object
The JavaScript unique Global Object.
86 87 88 |
# File 'lib/johnson/runtime.rb', line 86 def global raise NotImplementedError end |
#load(*files) ⇒ Object
Load and execute the named JavaScript files.
Checks for (and skips) a shebang line at the top of any of them.
94 95 96 97 98 |
# File 'lib/johnson/runtime.rb', line 94 def load(*files) files.map { |f| evaluate(File.read(f).gsub(/\A#!.*$/, ''), f, 1) }.last end |
#require(*files) ⇒ Object
Search the Ruby load path for each of the named files, and
evaluate them if they have not yet been loaded.
Calls Johnson.require() in JavaScript on each filename in turn.
105 106 107 108 109 |
# File 'lib/johnson/runtime.rb', line 105 def require(*files) files.each do |file| evaluate("Johnson.require('#{file}');") end end |