+ # Does the same as an usual send, except it will modify the call chain on the first call when injecting code at Runtime using the debugger.
+ # Instead of creating a pristine Frame, it will copy the actual values of the frame, and re-inject them after execution in the current context.
+ fun rt_send(mproperty: MMethod, args: Array[Instance]): nullable Instance
+ do
+ var recv = args.first
+ var mtype = recv.mtype
+ var ret = send_commons(mproperty, args, mtype)
+ if ret != null then return ret
+ var propdef = mproperty.lookup_first_definition(self.mainmodule, mtype)
+ return self.rt_call(propdef, args)
+ end
+
+ # Same as a regular call but for a runtime injected module
+ #
+ fun rt_call(mpropdef: MMethodDef, args: Array[Instance]): nullable Instance
+ do
+ args = call_commons(mpropdef, args)
+ return rt_call_without_varargs(mpropdef, args)
+ end
+
+ # Common code to call and this function
+ #
+ # Call only executes the variadic part, this avoids
+ # double encapsulation of variadic parameters into an Array
+ fun rt_call_without_varargs(mpropdef: MMethodDef, args: Array[Instance]): nullable Instance
+ do
+ if self.modelbuilder.toolcontext.opt_discover_call_trace.value and not self.discover_call_trace.has(mpropdef) then
+ self.discover_call_trace.add mpropdef
+ self.debug("Discovered {mpropdef}")
+ end
+ assert args.length == mpropdef.msignature.arity + 1 else debug("Invalid arity for {mpropdef}. {args.length} arguments given.")
+
+ # Look for the AST node that implements the property
+ var mproperty = mpropdef.mproperty
+ if self.modelbuilder.mpropdef2npropdef.has_key(mpropdef) then
+ var npropdef = self.modelbuilder.mpropdef2npropdef[mpropdef]
+ self.parameter_check(npropdef, mpropdef, args)
+ if npropdef isa AMethPropdef then
+ return npropdef.rt_call(self, mpropdef, args)
+ else
+ print "Error, invalid propdef to call at runtime !"
+ return null
+ end
+ else if mproperty.name == "init" then
+ var nclassdef = self.modelbuilder.mclassdef2nclassdef[mpropdef.mclassdef]
+ self.parameter_check(nclassdef, mpropdef, args)
+ return nclassdef.call(self, mpropdef, args)
+ else
+ fatal("Fatal Error: method {mpropdef} not found in the AST")
+ abort
+ end
+ end
+
+ # Evaluates dynamically a snippet of Nit code
+ # `nit_code` : Nit code to be executed
+ fun eval(nit_code: String)
+ do
+ var local_toolctx = modelbuilder.toolcontext
+ local_toolctx.dbg = self
+ var e = local_toolctx.parse_something(nit_code)
+ if e isa ABlockExpr then
+ nit_code = "module rt_module\n" + nit_code
+ e = local_toolctx.parse_something(nit_code)
+ end
+ if e isa AExpr then
+ nit_code = "module rt_module\nprint " + nit_code
+ e = local_toolctx.parse_something(nit_code)
+ end
+ if e isa AModule then
+ local_toolctx.had_error = false
+ modelbuilder.load_rt_module(self.mainmodule, e, "rt_module")
+ local_toolctx.run_phases([e])
+ if local_toolctx.had_error then
+ modelbuilder.model.try_remove_module(e.mmodule.as(not null))
+ local_toolctx.dbg = null
+ return
+ end
+ var mmod = e.mmodule
+ if mmod != null then
+ self.mainmodule = mmod
+ var local_classdefs = mmod.mclassdefs
+ var sys_type = mmod.sys_type
+ if sys_type == null then
+ print "Fatal error, cannot find Class Sys !\nAborting"
+ abort
+ end
+ var mobj = new MutableInstance(sys_type)
+ init_instance(mobj)
+ var initprop = mmod.try_get_primitive_method("init", sys_type.mclass)
+ if initprop != null then
+ self.send(initprop, [mobj])
+ end
+ var mainprop = mmod.try_get_primitive_method("main", sys_type.mclass)
+ if mainprop != null then
+ self.rt_send(mainprop, [mobj])
+ end
+ else
+ print "Error while loading_rt_module"
+ end
+ else
+ print "Error when parsing, e = {e.class_name}"
+ end
+ local_toolctx.dbg = null
+ end
+