=== Top of the Swiki === Attachments ===

Squeak As Cpp Class

From the SqueakList, October 28, 1997:

Another thing that must be done on the Squeak->Newton port that might get you closer to the guts of Squeak than the other items I mentioned before: mouse event issue, getting image on Newton, sound & networking primitives.

There can be no global variables (other than unchanging data) in a C++ Newton program. So, I have made the VM a C++ class, and hold onto one instance of it. Right now I convert the C VM to a C++ class by hand, and put all global & static variables into it. I looked at the C code generator and subclassed it and messed with it a bit quite a while ago (to make a record based VM, though I abandoned that approach), and it should be possible to make it generate C++ code fairly easily.

While I have the VM in C++ now, it would be nice to automatically take advantage of new VM code without having to redo any work by hand. It takes me an hour or two to do it, mostly by search and replace, and then compiling looking for errors.

No matter what you do, there will likely still be a bit of hand editing at the end. The objective is to minimize this, not neccesarily entirely replace it. Like at the end, one still likely must move globals from support routines into the VM class definition.

Basically:

Have the C++ code generator put a class definition for a SqueakVM class around the global variable list and function declaration list. Then have it append SqueakVM:: before every function definition, and after the type of course.

Seven other things you could do with the C++ code generator if you're ambitious; I only did the first four in my by hand version:
* Take any calls to ioFunctions or any other primitive function in a support file and have it pass the VM as a pointer (this) as the first arguement to any of those function. I modify the support functions for the Newton to expect this as a SqueakVM* vm.
* Have it use something other than 'class' for some variable names.
* Move any static variables into the class.
* Somehow deal with the initialization of these global variables, like in a class constructor or elsewhere.
* Improve the generator so it doesn't put int on functions without return values, put void instead.
* (hard!) Have it automatically detect whether a function requires any of the C++ class variables, and if not, declare it 'static' for optimization as a 'class side' function not needing a vm pointer.
* Don't use the inline option when generating the VM. Instead use the 'inline' C++ flag in code for those functions. Note: I don't think you can have loops in inline C++ code.