Objective-C runtime code generation - objective-c

I want to create an Objective-C application which lets you specify a class implementation at runtime.
I want the user to type some code (correctness of the code is out of scope for now).
When the user is done i want to create a class of the typed code and use it in the application.
So i want to dynamically add code in runtime of the application. Is this possible?
If so, how can i achieve this? If not, why not and are there any alternatives to create the same effect which i want to create?
Thanks.

You can dynamically load classes at runtime, but to get there you'd first need to handle distributing a compiler, its compilation dependencies (headers, ...), setup its enviroment, etc.
Usually applications use scripting languages that are painlessly embeddable (Lua, Python, ...) or already available on the platform (JavaScript, AppleScript, ...).

I'd check out F-Script. It's closer to smalltalk than Obj-C, but IMO it's closer to Obj-C than JavaScript or Lua :-)

On iOS devices, this isn't possible. On a Mac you could link against the clang+llvm libraries and use them to generate code into a buffer, then mprotect() the buffer to be executable, I believe.

Related

How to parse Objective-C code within a Cocoa application

I am writing a Mac OS X desktop application in which I want to be able to parse fragments of Objective-C such as variable and method declarations, as well as full Objective-C header and source files.
It looks to me as if I should be making use of Clang to do this, but I could do with some pointers and examples on how to integrate it as a library in my project, and how to invoke it to parse strings and files.
Can anyone provide me with any help on this?
You probably want libclang, code browsable at http://llvm.org/svn/llvm-project/cfe/trunk/tools/libclang/ (though you'll need to checkout the entire Clang repo to build it). There's very little documentation around on it, sadly. There is a presentation at http://llvm.org/devmtg/2010-11/Gregor-libclang.pdf that might help kickstart things, but mostly just some hunting through the code is the way to go.
Clang is actually more modular than libclang provides for (you can import just the components you want). If you've adventurous, there are examples at http://llvm.org/svn/llvm-project/cfe/trunk/examples/.

Runtime library importing in Objective-C

I would like to know how I can import a library into Objective-C at runtime. I'm new to Objective-C and this may be a complex question, but I'm fairly proficient in C/C++,
I'm developing a modular application where specific modules (libraries) are loaded as needed. Basically I have a name in runtime, like "nodeX", and I want to load a library that has it.
I've been searching for a way to do this but have been unsuccessful.
Thanks!
See NSBundle.
Note that you can't safely unload code on Mac OS X, Objective-C or otherwise, unless you are so terribly careful that it just isn't worth the effort to do so.
Note, also, that you can't dynamically load code on iOS.

Compiling Scheme to Java and/or Objective-C

We have Bigloo.NET does anyone know of such a project that offers the same but for the Java and/or Objective-C language?
I am developing a component of a project that will also have a Windows and Apple GUI built around it. Would be nice if I could develop this component in a single language and have it compiled into the native language for the current GUI. Any ideas?
Do you know that Bigloo initially targeted the JVM, and only later the CLR? I'm assuming you do, and that it's insufficient for you. If you weren't aware:
Java code and Bigloo code can be
merged together. Bigloo functions can
call Java functions and vice-versa,
Bigloo code can use and instantiate
Java classes. Bigloo functions and
variables can hold Java values (Java
classes values). Bigloo data
structures can point to Java data
structures and vice-versa.
If that doesn't do it for you, but you still want a Lisp, Clojure is a Lisp, though neither Scheme nor Common Lisp. It shares with Scheme a single namespace for functions and variables, however, and I've found it pretty comfortable in my short acquaintance with it. Clojure is also Java --- anything you do from Clojure can be used from plain Java, and vice versa.
Maybe you could give more detail on why Bigloo doesn't work for you, and that could help us give better answers.
Schemes for the JVM: SISC and JScheme. Both are interpreters with good Java interoperability.

Extract Objective-C class information from library at runtime

I was wondering if there were a way to extract information from an objective-c app, static or dynamic library and/or framework?
Information such as an array of class names without instantiating or running the target.
I've checked google and the apple developer documentation and haven't found anything.
Frank
F-script appears to be able to do what you want, but I'm no expert. Check out www.fscript.org.
If you want to extract classes from an application/dynamic library, there is a handy tool called ClassDump.
It can even generate the header files in order to get an overview of the classes, protocols, etc.
If you want to do it at runtime, then take a look at the source code to learn how to load and parse the different mach-o segments.
This is an excellent starting point for reverse-engineering Cocoa apps:
http://culater.net/wiki/moin.cgi/CocoaReverseEngineering
It mentions F-Script, class-dump, and a few others.

How do I expose erl_interface (Erlang's C library) through a DLL?

I've been working non-stop for the last three days on a completely managed interface to Erlang. At this point, I've decided that there simply must be an easier way. I've got a little over 3000 lines and it's not even in a compilable state yet. To be honest, I'm getting lost in my own code.
So, I then remembered that Erlang has a C library called erl_interface. Unfortunately, it only comes as a .LIB file, which isn't usable via P/Invoke. I'm now investigating ways to expose the static library through a DLL.
I'd like to stay away from Visual C++, mostly because I'm not a C/C++ programmer by nature and I find it really difficult to configure. TinyC is my compiler of choice when working with anything in C.
How can I go about this?
I know I can link erl_interface to a DLL, but how can I expose the functions? Do I have to essentially wrap each and every one of them in my own exports? That probably won't be a problem, since I could write a script to generate the code from the header file. But is there an easier way that I just don't know about?
Also, please don't recommend OTP.NET. It's a nice library, but I'm looking to use this is a large project, so I'd like to keep it in-house.
So, your problem is one of turning a static lib into a dynamic one.
The least-effort solution would be to write a thin shim file in 'C', that just delegates to the files in the .lib e.g.
ReturnType my_method1(args...) {
return real_method1(args...);
}
...
and build a DLL from that and the static lib.
Afterthought -- There is another approach you could take -- which is build the .lib into a C++/CLI assembly and do the transition/wrapping in that. It's what C++/CLi is there for, after all.
If you want some help with interfacing to Erlang with C, have a look at "EPAPI" (Erlang Port API) link text. You can of course browse the source code since it is hosted on Google Code. A DEBIAN repository is also available.