Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: fb0fb7af3b44
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: c2ab935081ad
Choose a head ref
  • 3 commits
  • 6 files changed
  • 1 contributor

Commits on Jun 18, 2014

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    1a89bee View commit details
  2. Copy the full SHA
    3c916fd View commit details
  3. Copy the full SHA
    c2ab935 View commit details
Showing with 61 additions and 54 deletions.
  1. +1 −1 lib/opal/cli_runners/nodejs.rb
  2. +3 −42 stdlib/nodejs.rb
  3. +16 −0 stdlib/nodejs/io.rb
  4. +16 −0 stdlib/nodejs/process.rb
  5. +16 −11 stdlib/nodejs/require.rb
  6. +9 −0 stdlib/nodejs/{runtime.js → runtime.rb}
2 changes: 1 addition & 1 deletion lib/opal/cli_runners/nodejs.rb
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ def run(code, argv)
# Let's support fake IO objects like StringIO
def system_with_output(env, *cmd)
io_output = IO.try_convert(output)
system(env,*cmd) if io_output
return system(env,*cmd) if io_output

require 'open3'
captured_output, status = Open3.capture2(env,*cmd)
45 changes: 3 additions & 42 deletions stdlib/nodejs.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,5 @@
require 'nodejs/runtime.js'
require 'nodejs/runtime'
require 'nodejs/file'
require 'nodejs/dir'

class LoadError < ScriptError; end

module IO::Writable
def puts(*args)
write args.map { |arg| String(arg) }.join($/)+$/
end
end

STDERR = $stderr = IO.new
STDIN = $stdin = IO.new
STDOUT = $stdout = IO.new

$stdout.write_proc = -> (string) {`process.stdout.write(#{string})`}
$stderr.write_proc = -> (string) {`process.stderr.write(string)`}

$stdout.extend(IO::Writable)
$stderr.extend(IO::Writable)

module Kernel
def exit
`process.exit()`
end
end

module NodeJS
def self.require name
`OpalNode.node_require(#{name})`
end
end

ARGV = `process.argv.slice(2)`

ENV = Object.new
def ENV.[]= name, value
`process.env[#{name.to_s}] = #{value.to_s}`
end

def ENV.[] name
`process.env[#{name}] || nil`
end
require 'nodejs/io'
require 'nodejs/process'
16 changes: 16 additions & 0 deletions stdlib/nodejs/io.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module IO::Writable
def puts(*args)
write args.map { |arg| String(arg) }.join($/)+$/
end
end

STDERR = $stderr = IO.new
STDIN = $stdin = IO.new
STDOUT = $stdout = IO.new

$stdout.write_proc = -> (string) {`process.stdout.write(#{string})`}
$stderr.write_proc = -> (string) {`process.stderr.write(string)`}

$stdout.extend(IO::Writable)
$stderr.extend(IO::Writable)

16 changes: 16 additions & 0 deletions stdlib/nodejs/process.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Kernel
def exit
`process.exit()`
end
end

ARGV = `process.argv.slice(2)`

ENV = Object.new
def ENV.[]= name, value
`process.env[#{name.to_s}] = #{value.to_s}`
end

def ENV.[] name
`process.env[#{name}] || nil`
end
27 changes: 16 additions & 11 deletions stdlib/nodejs/require.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
require 'opal-parser'

module Kernel
def require path
def __prepare_require__(path)
name = `$opal.normalize_loadable_path(#{path})`
full_path = name.end_with?('.rb') ? name : name+'.rb'

if `!$opal.modules[#{name}]`
ruby = File.read(path)
js = Opal.compile(ruby, requirable: true, file: name)
ruby = File.read(full_path)
compiler = Opal::Compiler.new(ruby, requirable: true, file: name)
js = compiler.compile
compiler.requires.each do |sub_path|
__prepare_require__(sub_path)
end
`eval(#{js})`
end

name
rescue => e
raise [path, name, full_path].inspect+e.message
end

def require path
name = __prepare_require__(path)
`$opal.require(#{name})`
end

def load path
name = `$opal.normalize_loadable_path(#{path})`

if `!$opal.modules[#{name}]`
ruby = File.read(path)
js = Opal.compile(ruby, requirable: true, file: name)
`eval(js)`
end

name = __prepare_require__(path)
`$opal.load(#{name})`
end
end
9 changes: 9 additions & 0 deletions stdlib/nodejs/runtime.js → stdlib/nodejs/runtime.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
%x{
// Generated by CoffeeScript 1.6.3
(function() {
var OpalNode, extensions, fs, parser, parserFile, source, sourceFile, vm, __path__;
@@ -14,3 +15,11 @@
global.OpalNode = OpalNode;
}).call(this);
}

module NodeJS
def self.require name
`OpalNode.node_require(#{name})`
end
end