class Test

  def initialize

  end

  def crash
    print x
  end

end

Test.new.crash

显然,此代码段将在第8行崩溃。如果使用Opal进行解析,则会得到以下已编译的代码:
/* Generated by Opal 0.8.0.beta1 */
(function(Opal) {
  Opal.dynamic_require_severity = "error";
  var self = Opal.top, $scope = Opal, nil = Opal.nil, $breaker = Opal.breaker, $slice = Opal.slice, $klass = Opal.klass;

  Opal.add_stubs(['$print', '$x', '$crash', '$new']);
  (function($base, $super) {
    function $Test(){};
    var self = $Test = $klass($base, $super, 'Test', $Test);

    var def = self.$$proto, $scope = self.$$scope;

    def.$initialize = function() {
      var self = this;

      return nil;
    };

    return (def.$crash = function() {
      var self = this;

      return self.$print(self.$x());
    }, nil) && 'crash';
  })(self, null);
  return $scope.get('Test').$new().$crash();
})(Opal);

当然,它将引发相同的错误。

但是,是否有办法确定此错误来自的 Ruby 行?

我可以看到这个问题:Is there a way to show the Ruby line numbers in javascript generated by Opal,但我不明白答案:它导致我进入https://github.com/opal/opal/tree/0-6-stable/examples/rack,我不确定应该在看什么或在做什么。

当我运行JavaScript时,我有一个index.html文件,该文件会加载opal.min.jsopal-parser.min.js,然后最终我将自己的已编译Ruby-Javascript代码存储在<script>标记中。

最佳答案

Opal具有source map support,以促进这种源代码级别的调试。我不会详细介绍源 map ,但是HTML5Rocksgreat article涵盖了深入的主题。

这是使用Opal进行设置的最小样板:

index.rb作为我们的源文件:

class Test

  def initialize

  end

  def crash
    print x
  end

end

Test.new.crash

由于您宁愿不使用很多多余的实用程序,因此让我们直接使用Opal API。创建一个文件builder.rb,它将编译上面的文件:
require 'opal'
Opal::Processor.source_map_enabled = true
Opal.append_path "."

builder = Opal::Builder.new.build('index')

# Write the output file containing a referece to sourcemap
# which we generate below : this will help the browser locate the
# sourcemap. Note that we are generating sourcemap for only code and not
# the entire Opal corelib.
#
File.binwrite "build.js", "#{builder.to_s}\n//# sourceMappingURL=build.js.map"
File.binwrite "build.js.map", builder.source_map.to_s

File.binwrite "opal_lib.js", Opal::Builder.build('opal_lib')

还创建一个仅包含以下内容的opal_lib.rb文件:
require 'opal'

最后,创建一个index.html,它将使我们能够在浏览器中运行脚本。
<!DOCTYPE html>
<html>
  <head>
    <script src="opal_lib.js"></script>
    <script src="build.js"></script>
  </head>
  <body>
  </body>
</html>

现在,要实际编译文件,请运行:
ruby builder.rb

这将生成已编译的javascript文件opal_lib.jsbuild.js,这些文件将由我们的index.html文件引用。现在,只需在浏览器中打开index.html。您将获得完整的调用堆栈和源 View :

javascript - 在Ruby Opal代码中获取错误行-LMLPHP

源文件的行号可用:

javascript - 在Ruby Opal代码中获取错误行-LMLPHP

除了使用浏览器以外,您还可以出于相同目的使用Node.js。这需要您安装Node.jsnpm。您还需要安装npm模块source-map-support
npm install source-map-support

现在,您可以打开节点repl并输入以下内容:
require('source-map-support').install();
require('./opal_lib');
require('./build');

您将获得带有正确源代码行号的堆栈跟踪:
/home/gaurav/Workspace/opal-playground/opal_lib.js:4436
        Error.captureStackTrace(err);
              ^
NoMethodError: undefined method `x' for #<Test:0x102>
    at OpalClass.$new (/home/gaurav/Workspace/opal-playground/opal_lib.js:4436:15)
    at OpalClass.$exception (/home/gaurav/Workspace/opal-playground/opal_lib.js:4454:31)
    at $Test.$raise (/home/gaurav/Workspace/opal-playground/opal_lib.js:4204:31)
    at $Test.Opal.defn.TMP_1 (/home/gaurav/Workspace/opal-playground/opal_lib.js:3032:19)
    at $Test.method_missing_stub [as $x] (/home/gaurav/Workspace/opal-playground/opal_lib.js:886:35)
    at $Test.$crash (/home/gaurav/Workspace/opal-playground/index.rb:8:11)
    at /home/gaurav/Workspace/opal-playground/index.rb:13:10
    at Object.<anonymous> (/home/gaurav/Workspace/opal-playground/index.rb:13:10)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)

我建议您使用bundler进行 gem 管理。这是用于获取蛋白石母版的Gemfile:
source 'http://production.cf.rubygems.org'

gem 'opal', github: 'opal/opal'

要进行编译,您必须运行:
bundle install
bundle exec ruby builder.rb

其他人提到的链轮集成/机架集成在下面使用相同的API,从而抽象化了管道。

更新:

由于我们在堆栈中有正确的行号,因此可以通过编程方式解析堆栈并将此行号提取到一个变量中:
require('./opal_lib');
require('source-map-support').install();
var $e = null;
try {
  require('./build');
} catch (e) {
  $e = e;
}
var lines = e.split('\n').map(function(line){ return line.match(/^.*\((\S+):(\d+):(\d+)\)/) })

var first_source_line;

for (var i = 0; i < lines.length ; i++) {
  var match = lines[i];
  if (match == null) continue;
  if (match[1].match(/index.rb$/) {
    first_source_line = match;
    break;
  }
}

var line_number;
if (first_source_line) line_number = first_source_line[2] // ==> 8

当然,您也可以使用ruby来做到这一点(但是,如果您在浏览器中运行它,则还必须在此处包括source-map-support):
class Test

  def initialize

  end

  def crash
    print x
  end

end

line_num = nil
begin
  Test.new.crash
rescue => e
  if line = e.backtrace.map{|line| line.match(/^.*\((\S+):(\d+):(\d+)\)/) }.compact.find{|match| match[1] =~ /index.rb$/ }
    line_num = line[2]
  end
end

puts "line_num => #{line_num}" # ==> 8

10-08 19:20