Class: Johnson::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/johnson/runtime.rb

Overview

An interface to a JavaScript engine.

Direct Known Subclasses

SpiderMonkey::Runtime

Constant Summary collapse

PRELUDE_PATH =
File.expand_path File.dirname(__FILE__) +
"/js/prelude.js"
CORE_PATH =

:nodoc:

File.expand_path File.dirname(__FILE__) +
"/js/core.js"
PRELUDE =

:nodoc:

IO.read PRELUDE_PATH
CORE =

:nodoc:

IO.read CORE_PATH

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRuntime

Install the Johnson prelude into this runtime environment.



47
48
49
50
51
52
# File 'lib/johnson/runtime.rb', line 47

def initialize # :notnew:
  evaluate PRELUDE, PRELUDE_PATH, 1
  global.Johnson.runtime = self
  global['Ruby'] = Object
  evaluate CORE, CORE_PATH, 1
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)


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/johnson/runtime.rb', line 32

def self.new(*args)
  return super if self < Johnson::Runtime

  delegate = args.first
  if delegate.is_a? Class
    delegate.new
  elsif delegate
    delegate
  else
    Johnson::SpiderMonkey::Runtime.new
  end
end

Instance Method Details

#[](key) ⇒ Object

Access the key property of the JavaScript global object.



56
57
58
# File 'lib/johnson/runtime.rb', line 56

def [](key)
  global[key]
end

#[]=(key, value) ⇒ Object

Set the key property of the JavaScript global object to value.



63
64
65
# File 'lib/johnson/runtime.rb', line 63

def []=(key, value)
  global[key] = value
end

#compile(script, filename = nil, linenum = nil) ⇒ Object

Compile the JavaScript source in script. If supplied, the script is marked as starting on line linenum of filename.

Raises:

  • (NotImplementedError)


111
112
113
# File 'lib/johnson/runtime.rb', line 111

def compile(script, filename=nil, linenum=nil)
  raise NotImplementedError
end

#delegateObject

Deprecated: Previously, returned the underlying JavaScript engine instance. Now returns self.



17
18
19
# File 'lib/johnson/runtime.rb', line 17

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.



74
75
76
77
78
# File 'lib/johnson/runtime.rb', line 74

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) ⇒ Object

Evaluates the given JS script, that should have been returned by a previous call to #compile().

Raises:

  • (NotImplementedError)


118
119
120
# File 'lib/johnson/runtime.rb', line 118

def evaluate_compiled_script(script)
  raise NotImplementedError
end

#globalObject

The JavaScript unique Global Object.

Raises:

  • (NotImplementedError)


83
84
85
# File 'lib/johnson/runtime.rb', line 83

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.



91
92
93
94
95
# File 'lib/johnson/runtime.rb', line 91

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.



102
103
104
105
106
# File 'lib/johnson/runtime.rb', line 102

def require(*files)
  files.each do |file|
    evaluate("Johnson.require('#{file}');")
  end
end