move source-code-related stuff out of utils.js, into source-code.js
[conkeror.git] / modules / source-code.js
blob42129b39e907d217d0ba285315f5d3c3977bb03f
1 /**
2  * (C) Copyright 2004-2007 Shawn Betts
3  * (C) Copyright 2007-2009 John J. Foerch
4  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
5  *
6  * Use, modification, and distribution are subject to the terms specified in the
7  * COPYING file.
8 **/
10 require_later("external-editor.js");
12 var conkeror_source_code_path = null;
14 function source_code_reference (uri, line_number) {
15     this.uri = uri;
16     this.line_number = line_number;
18 source_code_reference.prototype = {
19     get module_name () {
20         if (this.uri.indexOf(module_uri_prefix) == 0)
21             return this.uri.substring(module_uri_prefix.length);
22         return null;
23     },
25     get file_name () {
26         var file_uri_prefix = "file://";
27         if (this.uri.indexOf(file_uri_prefix) == 0)
28             return this.uri.substring(file_uri_prefix.length);
29         return null;
30     },
32     get best_uri () {
33         if (conkeror_source_code_path != null) {
34             var module_name = this.module_name;
35             if (module_name != null)
36                 return "file://" + conkeror_source_code_path + "/modules/" + module_name;
37         }
38         return this.uri;
39     },
41     open_in_editor : function() {
42         yield open_with_external_editor(load_spec(this.best_uri),
43                                         $line = this.line_number);
44     }
47 var get_caller_source_code_reference_ignored_functions = {};
49 function get_caller_source_code_reference (extra_frames_back) {
50     /* Skip at least this function itself and whoever called it (and
51      * more if the caller wants to be skipped). */
52     var frames_to_skip = 2;
53     if (extra_frames_back != null)
54         frames_to_skip += extra_frames_back;
56     for (let f = Components.stack; f != null; f = f.caller) {
57         if (frames_to_skip > 0) {
58             --frames_to_skip;
59             continue;
60         }
61         if (get_caller_source_code_reference_ignored_functions[f.name])
62             continue;
63         return new source_code_reference(f.filename, f.lineNumber);
64     }
66     return null;
69 function ignore_function_for_get_caller_source_code_reference (func_name) {
70     get_caller_source_code_reference_ignored_functions[func_name] = 1;