Search code examples
stringsmalltalksqueak

squeak(smalltallk) how to 'inject' string into string


I'm writing a class named "MyObject". one of the class methods is:

addTo: aCodeString assertType: aTypeCollection

when the method is called with aCodeString, I want to add (in runtime) a new method to "MyObject" class which aCodeString is it's source code and inject type checking code into the source code. for example, if I call addTo: assertType: like that:

a := MyObject new.
a addTo:  'foo: a boo:b baz: c
    ^(a*b+c)'
assertType: #(SmallInteger SmallInteger SmallInteger).

I expect that I could write later:

answer := (a foo: 2 boo: 5 baz: 10). 

and get 20 in answer. and if I write:

a foo: 'someString' boo: 5 baz: 10.

I get the proper message because 'someString' is not a SmallInteger.

I know how to write the type checking code, and I know that to add the method to the class in runtime I can use 'compile' method from Behavior class.

the problem is that I want to add the type checking code inside the source code. I'm not really familiar with all of squeak classes so I'm not sure if I rather edit the aCodeString as a string inside addTo: assertType: and then use compile: (and I don't know how to do so), or that there is a way to inject code to an existing method in Behavior class or other squeak class.

so basically, what I'm asking is how can I inject string into an existing string or to inject code into an existing method.


Solution

  • There are many ways you could achieve such type checking...

    The one you propose is to modify the source code (a String) so as to insert additional pre-condition type checks.

    The key point with this approach is that you will have to insert the type checking at the right place. That means somehow parsing the original source (or at least the selector and arguments) so as to find its exact span (and the argument names).

    See method initPattern:return: in Parser and its senders. You will find quite low level (not most beautiful) code that feed the block (passed thru return: keyword) with sap an Array of 3 objects: the method selector, the method arguments and the method precedence (a code telling if the method is connected to unary, binary or keyword message). From there, you'll get enough material for achieving source code manipulation (insert a string into another with copyReplace:from:to:with:).

    Do not hesitate to write small snippets of code and execute in the Debugger (select code to debug, then use debug it menu or ALT+Shift+D). Also use the inspectors extensively to gain more insight on how things work!

    Another solution is to parse the whole Abstract Syntax Tree (AST) of the source code, and manipulate that AST to insert the type checks. Normally, the Parser builds the AST, so observe how it works. From the modified AST, you can then generate new CompiledMethod (the bytecode instructions) and install it in methodDictionary - see the source code of compile: and follow the message sent until you discover generateMethodFromNode:trailer:. This is a bit more involved, and has a bad side effect that the source code is now not in phase with generated code, which might become a problem once you want to debug the method (fortunately, Squeak can used decompiled code in place of source code!).

    Last, you can also arrange to have an alternate compiler and parser for some of your classes (see compilerClass and/or parserClass). The alternate TypeHintParser would accept modified syntax with the type hints in source code (once upon a time, it was implemented with type hints following the args inside angle brackets foo: x <Integer> bar: y <Number>). And the alternate TypeHintCompiler would arrange to compile preconditions automatically given those type hints. Since you will then be very advanced in Squeak, you will also create special mapping between source code index and bytecodes so as to have sane debugger and even special Decompiler class that could recognize the precondition type checks and transform them back to type hints just in case.

    My advice would be to start with the first approach that you are proposing.

    EDIT

    I forgot to say, there is yet another way, but it is currently available in Pharo rather than Squeak: Pharo compiler (named OpalCompiler) does reify the bytecode instructions as objects (class names beginning with IR) in the generation phase. So it is also possible to directly manipulate the bytecode instructions by proper hacking at this stage... I'm pretty sure that we can find examples of usage. Probably the most advanced technic.