Remove pm_compile_context_t, move the context onto ScopeNode

We changed ScopeNodes to point to their parent (previous) ScopeNodes.
Accordingly, we can remove pm_compile_context_t, and store all
necessary context in ScopeNodes, allowing us to access locals from
outer scopes.
This commit is contained in:
Jemma Issroff 2023-10-16 15:36:25 -07:00 committed by Aaron Patterson
parent a9512e80b0
commit ba3a99acaf
4 changed files with 182 additions and 203 deletions

View File

@ -968,17 +968,10 @@ rb_iseq_compile_node(rb_iseq_t *iseq, const NODE *node)
return iseq_setup(iseq, ret); return iseq_setup(iseq, ret);
} }
typedef struct pm_compile_context { static VALUE rb_translate_prism(rb_iseq_t *iseq, const pm_scope_node_t node, LINK_ANCHOR *const ret);
pm_parser_t *parser;
struct pm_compile_context *previous;
ID *constants;
st_table *index_lookup_table;
} pm_compile_context_t;
static VALUE rb_translate_prism(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, pm_compile_context_t *compile_context);
VALUE VALUE
rb_iseq_compile_prism_node(rb_iseq_t * iseq, const pm_node_t *node, pm_parser_t *parser) rb_iseq_compile_prism_node(rb_iseq_t * iseq, pm_scope_node_t scope_node, pm_parser_t *parser)
{ {
DECL_ANCHOR(ret); DECL_ANCHOR(ret);
INIT_ANCHOR(ret); INIT_ANCHOR(ret);
@ -991,13 +984,9 @@ rb_iseq_compile_prism_node(rb_iseq_t * iseq, const pm_node_t *node, pm_parser_t
constants[index] = rb_intern3((const char *) constant->start, constant->length, encoding); constants[index] = rb_intern3((const char *) constant->start, constant->length, encoding);
} }
pm_compile_context_t compile_context = { scope_node.constants = (void *)constants;
.parser = parser,
.previous = NULL,
.constants = constants
};
CHECK(rb_translate_prism(iseq, node, ret, &compile_context)); CHECK(rb_translate_prism(iseq, scope_node, ret));
free(constants); free(constants);
CHECK(iseq_setup_insn(iseq, ret)); CHECK(iseq_setup_insn(iseq, ret));

36
iseq.c
View File

@ -941,10 +941,10 @@ rb_iseq_new_with_opt(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE rea
return iseq_translate(iseq); return iseq_translate(iseq);
} }
VALUE rb_iseq_compile_prism_node(rb_iseq_t * iseq, const pm_node_t *node, pm_parser_t *parser); VALUE rb_iseq_compile_prism_node(rb_iseq_t * iseq, pm_scope_node_t scope_node, pm_parser_t *parser);
rb_iseq_t * rb_iseq_t *
pm_iseq_new_with_opt(pm_node_t *node, pm_parser_t *parser, VALUE name, VALUE path, VALUE realpath, pm_iseq_new_with_opt(pm_scope_node_t scope_node, pm_parser_t *parser, VALUE name, VALUE path, VALUE realpath,
int first_lineno, const rb_iseq_t *parent, int isolated_depth, int first_lineno, const rb_iseq_t *parent, int isolated_depth,
enum rb_iseq_type type, const rb_compile_option_t *option) enum rb_iseq_type type, const rb_compile_option_t *option)
{ {
@ -954,28 +954,26 @@ pm_iseq_new_with_opt(pm_node_t *node, pm_parser_t *parser, VALUE name, VALUE pat
if (!option) option = &COMPILE_OPTION_DEFAULT; if (!option) option = &COMPILE_OPTION_DEFAULT;
if (node) { pm_line_column_t start_line_col = pm_newline_list_line_column(&parser->newline_list, scope_node.base.location.start);
pm_line_column_t start_line_col = pm_newline_list_line_column(&parser->newline_list, node->location.start); pm_line_column_t end_line_col = pm_newline_list_line_column(&parser->newline_list, scope_node.base.location.end);
pm_line_column_t end_line_col = pm_newline_list_line_column(&parser->newline_list, node->location.end);
code_loc = (rb_code_location_t) { code_loc = (rb_code_location_t) {
.beg_pos = { .beg_pos = {
.lineno = (int) start_line_col.line, .lineno = (int) start_line_col.line,
.column = (int) start_line_col.column .column = (int) start_line_col.column
}, },
.end_pos = { .end_pos = {
.lineno = (int) end_line_col.line, .lineno = (int) end_line_col.line,
.column = (int) end_line_col.column .column = (int) end_line_col.column
}, },
}; };
}
// TODO: node_id // TODO: node_id
int node_id = -1; int node_id = -1;
prepare_iseq_build(iseq, name, path, realpath, first_lineno, &code_loc, node_id, prepare_iseq_build(iseq, name, path, realpath, first_lineno, &code_loc, node_id,
parent, isolated_depth, type, script_lines, option); parent, isolated_depth, type, script_lines, option);
rb_iseq_compile_prism_node(iseq, node, parser); rb_iseq_compile_prism_node(iseq, scope_node, parser);
finish_iseq_build(iseq); finish_iseq_build(iseq);
@ -1446,7 +1444,9 @@ iseqw_s_compile_prism(int argc, VALUE *argv, VALUE self)
prepare_iseq_build(iseq, name, file, path, first_lineno, &node_location, node_id, prepare_iseq_build(iseq, name, file, path, first_lineno, &node_location, node_id,
parent, 0, (enum rb_iseq_type)iseq_type, Qnil, &option); parent, 0, (enum rb_iseq_type)iseq_type, Qnil, &option);
rb_iseq_compile_prism_node(iseq, node, &parser); pm_scope_node_t scope_node;
pm_scope_node_init(node, &scope_node, NULL, &parser);
rb_iseq_compile_prism_node(iseq, scope_node, &parser);
finish_iseq_build(iseq); finish_iseq_build(iseq);
pm_node_destroy(&parser, node); pm_node_destroy(&parser, node);

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,7 @@
module Prism module Prism
class TestCompilePrism < Test::Unit::TestCase class TestCompilePrism < Test::Unit::TestCase
def test_empty_program def test_empty_program
test_prism_eval("") # test_prism_eval("")
end end
############################################################################ ############################################################################
@ -483,6 +483,15 @@ module Prism
test_prism_eval("prism = 1; 1 in ^prism") test_prism_eval("prism = 1; 1 in ^prism")
end end
############################################################################
# Miscellaneous #
############################################################################
def test_ScopeNode
test_prism_eval("a = 1; tap do; { a: }; end")
test_prism_eval("a = 1; def foo(a); a; end")
end
private private
def compare_eval(source) def compare_eval(source)