Class: Johnson::Runtime

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

Overview

An interface to a JavaScript engine.

Direct Known Subclasses

TraceMonkey::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 Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRuntime

Install the Johnson prelude into this runtime environment.



50
51
52
53
54
55
# File 'lib/johnson/runtime.rb', line 50

def initialize # :notnew:
  evaluate PRELUDE, PRELUDE_PATH, 1
  global.Johnson.runtime = self
  global['Ruby'] = Object
  evaluate CORE, CORE_PATH, 1
end

Class Attribute Details

.runtimesObject (readonly)

Returns the value of attribute runtimes.



175
176
177
# File 'lib/johnson/runtime.rb', line 175

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.

options passed to the underlying runtime Use size to specify heap limit

:call-seq:

new(runtime_class=nil, options)


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/johnson/runtime.rb', line 35

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

  delegate = args.first
  if delegate.is_a? Class
    delegate.new
  elsif !delegate.nil? && !delegate.is_a?( Hash )
    delegate
  else
    default.new( *args )
  end
end

Instance Method Details

#[](key) ⇒ Object

Access the key property of the JavaScript global object.



59
60
61
# File 'lib/johnson/runtime.rb', line 59

def [](key)
  global[key]
end

#[]=(key, value) ⇒ Object

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



66
67
68
# File 'lib/johnson/runtime.rb', line 66

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.

Raises:

  • (NotImplementedError)


113
114
115
# File 'lib/johnson/runtime.rb', line 113

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

#current_stackObject



124
125
126
# File 'lib/johnson/runtime.rb', line 124

def current_stack
  global.Johnson.getStack
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.



77
78
79
80
81
# File 'lib/johnson/runtime.rb', line 77

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().

Raises:

  • (NotImplementedError)


120
121
122
# File 'lib/johnson/runtime.rb', line 120

def evaluate_compiled_script(script,scope=nil)
  raise NotImplementedError
end

#globalObject

The JavaScript unique Global Object.

Raises:

  • (NotImplementedError)


85
86
87
# File 'lib/johnson/runtime.rb', line 85

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.



93
94
95
96
97
# File 'lib/johnson/runtime.rb', line 93

def load(*files)
  files.map { |f|
    evaluate(File.read(f).gsub(/\A#!.*$/, ''), f, 1)
  }.last
end

#raise_js_exception(jsex) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/johnson/runtime.rb', line 144

def raise_js_exception(jsex)
  exception_source = caller(2)

  case jsex
  when Exception
    if stack = jsex.send(:remove_instance_variable, :@js_stack)
      jsex.append_to_stack_deck parse_js_stack(stack)
    end
    raise jsex
  when String
    ex = Johnson::Error.new(jsex)
    ex.set_backtrace exception_source
  when Johnson::RubyLandProxy
    ex = Johnson::Error.new(jsex['message'] || jsex.to_s, jsex)
    ex.set_backtrace exception_source

    if stack = jsex['stack']
      ex.append_to_stack_deck parse_js_stack(stack, jsex)
    end
  else
    ex = Johnson::Error.new(jsex.inspect)
    ex.set_backtrace exception_source
  end

  raise ex
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.



104
105
106
107
108
# File 'lib/johnson/runtime.rb', line 104

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