Refactoring methods and creating copies with a different name in Pharo Smalltalk? - reflection

I am experimenting with refactoring and some custom functionality. Is there a way to copy a method to the same class as the original but under a new name? (essentially creating a really shallow copy) You can do this by hand by editing the method source, but can it be done programatically?
For example:
doMethodName: anArgument
^ anObject doThat with: anArgument
becomes:
doNewMethodName: anArgument
^ anObject doThat with: anArgument

You can compile methods by sending a compile: message to the target class.
retrieve the method
e.g. method := Float>>#cos or method := Float methodNamed: #cos
retrieve the source code
method sourceCode will return the method code as a string
method ast (or method parseTree) will return the code as parsed tree representation
Compile code into a class (optionally with protocol)
TargetClass compile: sourceCode, or
TargetClass compile: sourceCode classified: protocol
So if you have
Something>>doMethodName: anArgument
^ anObject doThat with: anArgument
You can do
code := (Something>>#doMethodName:) sourceCode.
"replace all matches"
newCode := code copyReplaceAll: 'doMethodName:' with: 'doNewMethodName:'.
"or just the first"
newCode := code copyWithRegex: '^doMethodName\:' matchesReplacedWith: 'doNewMethodName:'.
Something compile: newCode.
Using AST
sourceCode returns the code as string, which isn't the nicest to manipulate.
If you just want to change the method name, you can rename it in the AST, e.g.
tree := (Something>>#doMethodName:) parseTree.
tree selector: 'doNewerMethodName:'.
Something compile: tree newSource.

Related

How do I get information about function calls from a Lua script?

I have a script written in Lua 5.1 that imports third-party module and calls some functions from it. I would like to get a list of function calls from a module with their arguments (when they are known before execution).
So, I need to write another script which takes the source code of my first script, parses it, and extracts information from its code.
Consider the minimal example.
I have the following module:
local mod = {}
function mod.foo(a, ...)
print(a, ...)
end
return mod
And the following driver code:
local M = require "mod"
M.foo('a', 1)
M.foo('b')
What is the better way to retrieve the data with the "use" occurrences of the M.foo function?
Ideally, I would like to get the information with the name of the function being called and the values of its arguments. From the example code above, it would be enough to get the mapping like this: {'foo': [('a', 1), ('b')]}.
I'm not sure if Lua has functions for reflection to retrieve this information. So probably I'll need to use one of the existing parsers for Lua to get the complete AST and find the function calls I'm interested in.
Any other suggestions?
If you can not modify the files, you can read the files into a strings then parse mod file and find all functions in it, then use that information to parse the target file for all uses of the mod library
functions = {}
for func in modFile:gmatch("function mod%.(%w+)") do
functions[func] = {}
end
for func, call in targetFile:gmatch("M%.(%w+)%(([^%)]+)%)") do
args = {}
for arg in string.gmatch(call, "([^,]+)") do
table.insert(args, arg)
end
table.insert(functions[func], args)
end
Resulting table can then be serialized
['foo'] = {{"'a'", " 1"}, {"'b'"}}
3 possible gotchas:
M is not a very unique name and could vary possibly match unintended function calls to another library.
This example does not handle if there is a function call made inside the arg list. e.g. myfunc(getStuff(), true)
The resulting table does not know the typing of the args so they are all save as strings representations.
If modifying the target file is an option you can create a wrapper around your required module
function log(mod)
local calls = {}
local wrapper = {
__index = function(_, k)
if mod[k] then
return function(...)
calls[k] = calls[k] or {}
table.insert(calls[k], {...})
return mod[k](...)
end
end
end,
}
return setmetatable({},wrapper), calls
end
then you use this function like so.
local M, calls = log(require("mod"))
M.foo('a', 1)
M.foo('b')
If your module is not just functions you would need to handle that in the wrapper, this wrapper assumes all indexes are a function.
after all your calls you can serialize the calls table to get the history of all the calls made. For the example code the table looks like
{
['foo'] = {{'a', 1}, {'b'}}
}

Argument attribute definition for Qore function implemented via QPP

I'd like to define a CPP __attribute__ for (Qore script) functions implemented in .qpp and preprocessed into plain .cpp.
Qore script class methods may be called from C++ side. In this case an empty function is implemented in QPP class definition and preprocessed in .cpp file. C++ compiler will raise warning messages during compilation. Solution is GNU attribute unused. Is somehow possible to define argument attributes in .qpp ?
MyClass::onAction(int arg0, reference arg1) {
}
Expanded in cpp as:
static QoreValue MyClass_onAction_HashBGD(QoreObject* self, MyClassObject *o, const QoreValueList* args, q_rt_flags_t rtflags, ExceptionSink* xsink)
Note: ScopeGuard.h contains #define UNUSED_VARIABLE __attribute__((unused)) but qpp syntax does not support "normal" C++ usage as argument prefix.
As there are also automatically expanded arguments seems function flag is to be implemented.
I believe this is already implemented; look at the implementation for [doc] in qpp.cpp; if you declare a parameter variable in a qpp file as [doc], it means that no C++ glue for that variable will be generated, and the parameter variable is for documentation only.
Example in QC_StreamWriter.qpp:
nothing StreamWriter::printf(string[doc] fmt, ...) {
sw->f_printf(args, xsink);
}
This generates the following C++ code:
// nothing StreamWriter::printf(string fmt, ...){}
static QoreValue StreamWriter_printf_VsVV(QoreObject* self, StreamWriter* sw, const QoreValueList* args, q_rt_flags_t rtflags, ExceptionSink* xsink) {
# 146 "QC_StreamWriter.qpp"
sw->printf(args, xsink);
return QoreValue();
}
I hope this helps!

How to extend a Java object in JNLua

I am experimenting with using JNLua's javavm module to connect with and extend a Java library (JAR). So far I am super impressed with how easy it is to pass Java objects back and forth between Lua and Java- seemlessness.
Now I am interested to extend these Java objects in LUA. In my naive approach I've wrapped the Java object in a Lua class with the intent of extending that objects API i.e. adding methods to it. But I don't want to have to recreate, within the wrapper, all of the Java objects methods. It seems like I should be able to inherit from the Java object so that when a method is missing from my wrapper Lua will look for it in the Java object which is a member of the wrapped class. I've tried adapting the examples shown in Inheritance but this is a slightly trickier thing to set up, given that I'm dealing with a Java object. Thoughts?
I found my answer in the below SO question
Add members dynamically to a class using Lua + SWIG
I needed to realize I was dealing with a UserData object, not a table- no way to add members
I needed some metatable kung-fu
The below code has the effect of allowing me to extend (add methods) a Java object.
function Model:new (model)
o = {}
WrapObject(Model, o, model)
self.__index = self
self.model = model or nil
return o
end
function WrapObject(class, object, userData)
local wrapper_metatable = {}
function wrapper_metatable.__index(self, key)
local ret = rawget(class, key)
if(not ret) then
ret = userData[key]
if(type(ret) == "function") then
return function(self, ...)
return ret(userData, ...)
end
else
return ret
end
else
return ret
end
end
setmetatable(object, wrapper_metatable)
return object
end
function Model:Test ()
name = self:GetFullName()
fileName = self:GetFileName()
ret = name .. fileName
print("It's a test!!")
return ret
end

XQuery: using global var in function

I need to use a counter to remember how many node I have dealed with. So I defined a global var $classCounter. For some unknown reasons, I get an error from zorba:
test.xqy>:15,9: error [zerr:XSST0004]: "local:owlClassNameBuilerHelper": function declared nonsequential but has sequential body
I really don't understand what this error means. How to implement a global counter in XQuery?
The whole xqy file is:
declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare namespace owl="http://www.w3.org/2002/07/owl#";
declare namespace xsd="http://www.w3.org/2001/XMLSchema#";
declare namespace rdfs="http://www.w3.org/2000/01/rdf-schema#";
import module namespace functx="http://www.functx.com";
declare variable $srcDoc:="test_xsd.xml"; (:need to adjust the input XSD file here:)
declare variable $defaultXMLNS:="http://www.test.com#";
declare variable $defaultXMLBase:=$defaultXMLNS;
declare variable $classCounter:=0;
declare function local:owlClassNameBuilerHelper($pnode as node()*)
as xs:string?
{
$classCounter:=classCounter+1;
let $tmp:=""
return
(
"haha"
(:if(functx:if-empty($pnode/#name, "-1")!="-1") (:if the name attr doesn't exist:)
then data($pnode/ancestor::element[1]/#name) (:get the name attr of first ancestor named element:)
else data($pnode/#name):)
)
};
element rdf:RDF
{
namespace {""} {$defaultXMLNS},
namespace {"owl"} {"http://www.w3.org/2002/07/owl#"},
namespace {"xsd"} {"http://www.w3.org/2001/XMLSchema#"},
namespace {"rdfs"} {"http://www.w3.org/2000/01/rdf-schema#"},
attribute xml:base {$defaultXMLBase}
}
command line:
zorba -i -f -q test.xqy
I need to use a counter to remember how many node I have dealed with.
Firstly, XQuery is a functional programming language. That's a completely different processing model: you can't "remember" what you have "dealt with", because there is no memory and no time dimension. Functions are mathematical functions, they can't have side-effects like updating global variables.
Now, the error message suggests to me that the particular XQuery processor you are using (Zorba) has extensions that allow you to depart from the pure functional programming model; but you are using the extensions incorrectly. In particular, if you want a function to have side-effects then you must declare the function as such. You'll have to look in the Zorba documentation for how to do that, because there is no standard.

How to call inputTask from within another inputTask?

In a inputTask I'm programmatically calling another inputTask, e.g. testOnly, with parameter string as follows:
val readParams = inputKey[Unit]("reads version")
readParams := {
... // here some Parser code
val a = "*OnlyThisClassPls*"
testOnly.toTask(a)
}
Unfortunately instead of result I get an exception Illegal dynamic reference. Why?
I think I solved my problem.
I created a method which converts testOnly inputTask to dynamic task (taskDyn) with parameter
def testOnlyWithDynamicParams(params: String) = Def.taskDyn {
(testOnly in Test).toTask(params)
}
I defined an dynamic input task (inputTaskDyn) which uses method to convert and evaluates value at the end
readParams := Def.inputTaskDyn {
... // here some Parser code
val paramsForTestOnly = " *OnlyThisClassPls*"
testOnlyWithDynamicParams(paramsForTestOnly)
}.evaluated
I'm not sure if it is a best way but it works for me. If you know the better solution please correct me.

Resources