summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYusuke Endoh <mame@ruby-lang.org>2021-06-21 21:15:25 +0900
committerYusuke Endoh <mame@ruby-lang.org>2021-06-21 21:15:25 +0900
commited8e265d4be53e15bffea3b1db649b1cea4ea864 (patch)
treecd3faaa65dd3ebf0fd5b3f9dae16666fa9f17196
parent089a26b0a619a649b1958061df8f21a6cdc6cacd (diff)
downloadruby-ed8e265d4be53e15bffea3b1db649b1cea4ea864.tar.gz
Experimentally expose RubyVM::AST::Node#node_id
Now ISeq#to_a includes the node_id list for each bytecode instruction. I want a way to retrieve the AST::Node instance corresponding to an instruction for a research purpose including TypeProf-based LSP server.
-rw-r--r--ast.c7
-rw-r--r--ast.rb11
2 files changed, 12 insertions, 6 deletions
diff --git a/ast.c b/ast.c
index 3945a32af7..32d8ab9eb6 100644
--- a/ast.c
+++ b/ast.c
@@ -254,16 +254,14 @@ ast_node_type(rb_execution_context_t *ec, VALUE self)
return rb_sym_intern_ascii_cstr(node_type_to_str(data->node));
}
-#ifdef DEBUG_ISEQ_NODE_ID
static VALUE
-ast_node_node_id(VALUE self)
+ast_node_node_id(rb_execution_context_t *ec, VALUE self)
{
struct ASTNodeData *data;
TypedData_Get_Struct(self, struct ASTNodeData, &rb_node_type, data);
return INT2FIX(nd_node_id(data->node));
}
-#endif
#define NEW_CHILD(ast, node) node ? ast_new_internal(ast, node) : Qnil
@@ -725,7 +723,4 @@ Init_ast(void)
rb_mAST = rb_define_module_under(rb_cRubyVM, "AbstractSyntaxTree");
rb_cNode = rb_define_class_under(rb_mAST, "Node", rb_cObject);
rb_undef_alloc_func(rb_cNode);
-#ifdef DEBUG_ISEQ_NODE_ID
- rb_define_method(rb_cNode, "node_id", ast_node_node_id, 0);
-#endif
}
diff --git a/ast.rb b/ast.rb
index ce99f53c45..34dd3f1d49 100644
--- a/ast.rb
+++ b/ast.rb
@@ -141,6 +141,17 @@ module RubyVM::AbstractSyntaxTree
end
# call-seq:
+ # node.node_id -> integer
+ #
+ # Returns an internal node_id number.
+ # Note that this is an API for ruby internal use, debugging,
+ # and research. Do not use this for any other purpose.
+ # The compatibility is not guaranteed.
+ def node_id
+ Primitive.ast_node_node_id
+ end
+
+ # call-seq:
# node.script_lines -> array
#
# Returns the original source code as an array of lines.