Vol*_*ort 9 javascript ruby opalrb
class Test
def initialize
end
def crash
print x
end
end
Test.new.crash
Run Code Online (Sandbox Code Playgroud)
很明显,这个代码片段会在第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);
Run Code Online (Sandbox Code Playgroud)
当然它会抛出同样的错误.
但是,有没有办法确定出现此错误的Ruby行?
我可以看到这个问题:有没有办法在Opal生成的javascript中显示Ruby行号,但我不明白答案:它引导我到https://github.com/opal/opal/tree/0 -6-stable/examples/rack我不确定我应该看什么或做什么.
当我跑我的JavaScript,我有一个index.html加载的文件opal.min.js和opal-parser.min.js,最后我有我的编译Ruby的Javascript代码的<script>标签.
Opal有源映射支持,以方便这种源级别的调试。我不会详细介绍源地图,但HTML5Rocks有一篇很棒的文章,深入介绍了该主题。
这是使用 Opal 进行设置的最小样板:
让index.rb我们的源文件:
class Test
def initialize
end
def crash
print x
end
end
Test.new.crash
Run Code Online (Sandbox Code Playgroud)
由于您不想使用很多无关的实用程序,让我们直接使用 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')
Run Code Online (Sandbox Code Playgroud)
还要创建一个opal_lib.rb仅包含以下内容的文件:
require 'opal'
Run Code Online (Sandbox Code Playgroud)
最后创建一个index.html允许我们在浏览器中运行脚本的。
<!DOCTYPE html>
<html>
<head>
<script src="opal_lib.js"></script>
<script src="build.js"></script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
现在要实际编译您的文件,请运行:
ruby builder.rb
Run Code Online (Sandbox Code Playgroud)
这将生成已编译的 javascript 文件opal_lib.js,build.js并由我们的index.html文件引用。现在只需index.html在浏览器中打开即可。您将获得完整的调用堆栈和源代码视图:
源文件的行号可用:
作为使用浏览器的替代方法,您也可以将 Node.js 用于相同目的。这需要你有Node.js并npm安装。您还需要安装 npm 模块source-map-support
npm install source-map-support
Run Code Online (Sandbox Code Playgroud)
现在您可以打开节点 repl 并输入以下内容:
require('source-map-support').install();
require('./opal_lib');
require('./build');
Run Code Online (Sandbox Code Playgroud)
您将获得具有正确源代码行号的堆栈跟踪:
/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)
Run Code Online (Sandbox Code Playgroud)
我建议您使用bundler进行 gem 管理。这是Gemfile获取 Opal 大师的方法:
source 'http://production.cf.rubygems.org'
gem 'opal', github: 'opal/opal'
Run Code Online (Sandbox Code Playgroud)
要编译,您必须运行:
bundle install
bundle exec ruby builder.rb
Run Code Online (Sandbox Code Playgroud)
其他人提到的链轮集成/机架集成在下面使用相同的 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
Run Code Online (Sandbox Code Playgroud)
当然,你也可以用 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
387 次 |
| 最近记录: |