Commit c7415220 authored by gsamain's avatar gsamain

Messages take their own ref of cyobjects

parent be8cd9ed
...@@ -1163,6 +1163,32 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1163,6 +1163,32 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
target_object_code = target_object_type.declaration_code(target_object_cname) target_object_code = target_object_type.declaration_code(target_object_cname)
target_object_argument_code = target_object_type.declaration_code(target_object_name) target_object_argument_code = target_object_type.declaration_code(target_object_name)
def put_refcount_op_on_narg_optarg(op, func_type, opt_arg_name, code):
opt_arg_count = func_type.optional_arg_count
narg_count = len(func_type.args) - opt_arg_count
for narg in func_type.args[:narg_count]:
if narg.type.is_cyp_class:
code.putln("%s(this->%s);" % (op, narg.cname))
code.putln("if (this->%s != NULL) {" % opt_arg_name)
num_if = 0
for opt_idx, optarg in enumerate(func_type.args[narg_count:]):
if optarg.type.is_cyp_class:
code.putln("if (this->%s->%sn > %s) {" %
(opt_arg_name,
Naming.pyrex_prefix,
opt_idx
))
code.putln("%s(this->%s->%s);" %
(op,
opt_arg_name,
func_type.opt_arg_cname(optarg.name)
))
num_if += 1
for _ in range(num_if):
code.putln("}")
code.putln("}")
for reifying_class_entry in entry.type.scope.reifying_entries: for reifying_class_entry in entry.type.scope.reifying_entries:
reified_function_entry = reifying_class_entry.reified_entry reified_function_entry = reifying_class_entry.reified_entry
reifying_class_full_name = reifying_class_entry.type.empty_declaration_code() reifying_class_full_name = reifying_class_entry.type.empty_declaration_code()
...@@ -1184,11 +1210,11 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1184,11 +1210,11 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
initialized_arg_names.append(arg.name) initialized_arg_names.append(arg.name)
initialized_arg_cnames.append(arg.cname) initialized_arg_cnames.append(arg.cname)
message_opt_arg_attr_name = "opt_args"
if opt_arg_count: if opt_arg_count:
# We cannot initialize the struct before allocating memory, so # We cannot initialize the struct before allocating memory, so
# it must be handled in constructor body, not initializer list # it must be handled in constructor body, not initializer list
opt_decl_code = reified_function_entry.type.op_arg_struct.declaration_code(Naming.optional_args_cname) opt_decl_code = reified_function_entry.type.op_arg_struct.declaration_code(Naming.optional_args_cname)
message_opt_arg_attr_name = "opt_args"
message_opt_arg_attr_decl = reified_function_entry.type.op_arg_struct.declaration_code(message_opt_arg_attr_name) message_opt_arg_attr_decl = reified_function_entry.type.op_arg_struct.declaration_code(message_opt_arg_attr_name)
code.putln("%s;" % message_opt_arg_attr_decl) code.putln("%s;" % message_opt_arg_attr_decl)
constructor_decl_list.append(opt_decl_code) constructor_decl_list.append(opt_decl_code)
...@@ -1218,6 +1244,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1218,6 +1244,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code.putln("} else {") code.putln("} else {")
code.putln("this->%s = NULL;" % message_opt_arg_attr_name) code.putln("this->%s = NULL;" % message_opt_arg_attr_name)
code.putln("}") code.putln("}")
# Acquire a ref on CyObject, as we don't know when the message will be processed
put_refcount_op_on_narg_optarg("Cy_INCREF", reified_function_entry.type, message_opt_arg_attr_name, code)
code.putln("}") code.putln("}")
code.putln("int activate() {") code.putln("int activate() {")
code.putln("/* Activate only if its sync object agrees to do so */") code.putln("/* Activate only if its sync object agrees to do so */")
...@@ -1250,6 +1278,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1250,6 +1278,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
# Destructor # Destructor
code.putln("virtual ~%s() {" % class_name) code.putln("virtual ~%s() {" % class_name)
put_refcount_op_on_narg_optarg("Cy_DECREF", reified_function_entry.type, message_opt_arg_attr_name, code)
if opt_arg_count: if opt_arg_count:
code.putln("free(this->%s);" % message_opt_arg_attr_name) code.putln("free(this->%s);" % message_opt_arg_attr_name)
code.putln("}") code.putln("}")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment