rubyjs / therubyracer

Embed the V8 Javascript Interpreter into Ruby
1.67k stars 190 forks source link

Can't load JS file from JS code #383

Closed dreamiurg closed 8 years ago

dreamiurg commented 8 years ago

I can't figure out how to load JS code from a separate file from within JS code running in this Ruby wrapper.

Example code:

require 'v8'

ctx = V8::Context.new
ctx.eval('load("file.js");')

Error message:

% ruby test.rb
at <eval>:1:1: load is not defined (V8::Error)
    from test.rb:4:in `<main>'

% ruby --version
ruby 1.9.3p551 (2014-11-13 revision 48407) [x86_64-darwin14.4.0]
dreamiurg commented 8 years ago

Alright, I figured out that load() is present only for v8 shell.cc

https://code.google.com/p/v8/source/browse/trunk/samples/shell.cc

cowboyd commented 8 years ago

That is correct. By default, the JavaScript environment is pristine. No function that is not in the JS spec is available. Note that you could implement load() in Ruby though:

ctx = V8::Context.new
ctx['load'] = lambda do |this, filename|
  ctx.load filename
end
ctx.eval('load("file.js")')