Sha256: b17a405af858d491539aaf90c6fb2557b6543a6aaa524c7f5328882d505606e5

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

require 'tempfile'
require 'securerandom'
require 'greenletters'

module BooJS
  def self.verify str
    phantom = Phantomjs.path

    #Create tmp file
    tmp = Tempfile.new(SecureRandom.hex)
    tmp.puts %{
      phantom.onError = function(msg, trace) {
        console.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        console.log("PhantomJS Error");
        console.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        console.log(msg);
        trace.forEach(function(t) {
          console.log(t.file + ': line ' + t.line );
        })
        console.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        phantom.exit(1);
      }
    }
    tmp.puts str
    tmp.puts "phantom.exit(0)"
    tmp.close

    system("phantomjs #{tmp.path} 2>&1") or raise "Verifying failed"
  end

  #Optionally, accept code to inject
  def self.pipe(str=nil)
    js = %{
      var system = require('system');
      function __spec_ping(str) {
        system.stdout.writeLine("pong"+str)
      }

      phantom.onError = function(msg, trace) {
        system.stderr.writeLine("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        system.stderr.writeLine("PhantomJS Error");
        system.stderr.writeLine("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        system.stderr.writeLine(msg);
        trace.forEach(function(t) {
          system.stderr.writeLine(t.file + ': line ' + t.line );
        })
        system.stderr.writeLine("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        phantom.exit(1);
      }
    }
    
    #Any code the user wanted
    js += "\n#{str}" if str

    #Repl
    js += "\nwhile (true) { var line = system.stdin.readLine(); eval(line); }"

    phantom = Phantomjs.path
    tmp = Tempfile.new(SecureRandom.hex)
    tmp.puts js
    tmp.close
    exit system("#{phantom} #{tmp.path}")
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
boojs-0.0.11 lib/boojs.rb
boojs-0.0.10 lib/boojs.rb
boojs-0.0.9 lib/boojs.rb