Discussion:
Question on Forth & C++/ObjC objects
(too old to reply)
Erik
2005-01-02 11:01:50 UTC
Permalink
Well, according to Google groups, this will be the first post of the
year, so: First Post! Woo-Hoo!

Anyway,

I was reading some docs on Objective-C, which I have yet to really
start learning, and I found myself wondering on how Mops (or any OO
Forth for that matter) would handle instantiating or calling objects in
Objective C or C++.

For example, if you have an ObjC or C++ shared library. Being that
they both are object oriented languages, it would make sense that there
would be classes and objects involved. If calling that library meant
accessing those objects, then I'm sure there would be some
cross-language quirks to say the least.

Then again, ObjC (to my understanding) uses the C conventions for
shared library calling, so perhaps using ObjC objects wouldn't be that
difficult. C++ would be a different story, though...

Anyway, I just wanted to throw that out there and see what everybody
thought. Thanks for your time!

Happy New Year!
Erik
Mike Hore
2005-01-02 11:33:01 UTC
Permalink
Post by Erik
Well, according to Google groups, this will be the first post of the
year, so: First Post! Woo-Hoo!
Not here -- it's the 2nd here by now. Maybe in your time zone...
Post by Erik
Anyway,
I was reading some docs on Objective-C, which I have yet to really
start learning, and I found myself wondering on how Mops (or any OO
Forth for that matter) would handle instantiating or calling objects in
Objective C or C++.
The forthcoming Mops 5.6 will have direct support for calling ObjC
functions in Cocoa. I won't give all the details here -- we're in
beta but it shouldn't be long.
Post by Erik
For example, if you have an ObjC or C++ shared library. Being that
they both are object oriented languages, it would make sense that there
would be classes and objects involved. If calling that library meant
accessing those objects, then I'm sure there would be some
cross-language quirks to say the least.
Yes, definitely.
Post by Erik
Then again, ObjC (to my understanding) uses the C conventions for
shared library calling, so perhaps using ObjC objects wouldn't be that
difficult. C++ would be a different story, though...
It's probably "just" a matter of working out what actual functions
to call. Certainly the ObjC sequence is constructed of a series
of low-level calls. C++ would probably be similar though I haven't
really looked into it. But once you know the low-level calls, you
just call them. The ABI for simple calls is pretty standard across
PowerPC languages (though I suspect there may be minor differences
with floating-point parameters).
Post by Erik
Anyway, I just wanted to throw that out there and see what everybody
thought. Thanks for your time!
Happy New Year!
Yes, and to you too!

Cheers, Mike.


----------------------------------------------------------------
Mike Hore ***@OVE.invalid.icasolution.com.au
----------------------------------------------------------------
Erik
2005-01-04 23:50:23 UTC
Permalink
Post by Mike Hore
Post by Erik
Well, according to Google groups, this will be the first post of the
year, so: First Post! Woo-Hoo!
Not here -- it's the 2nd here by now. Maybe in your time zone...
Damn! That's right, you're 24 hours ahead of us, aren't you? Ah
well...
Post by Mike Hore
It's probably "just" a matter of working out what actual functions
to call. Certainly the ObjC sequence is constructed of a series
of low-level calls. C++ would probably be similar though I haven't
really looked into it. But once you know the low-level calls, you
just call them. The ABI for simple calls is pretty standard across
PowerPC languages (though I suspect there may be minor differences
with floating-point parameters).
You know, I recall some time back checking out Apple's docs on
ObjC (sorry, can't remember the exact webpage or general
section; it was on Objective C and shared library generation, I
think). On this particular page, Apple actually warned against using
C++ for shared libraries. Something about how each C++ compiler
sort of does its own thing, so there's no standard way of calling
functions or objects unless you interject a layer of C code. Don't
know if this relates to the Cocoa framework in any way or it's
just a general C++ issue, though.

I'll try something with XCode sometime this weekend and see
what happens. I've been meaning to play with it any way. (It would
be interesting if XCode did one thing, and Codewarrior did another...)
Erik
Roelf Toxopeus
2005-01-02 13:25:10 UTC
Permalink
In article <***@z14g2000cwz.googlegroups.com>,
"Erik" <***@email.com> wrote:

[...]
Post by Erik
Anyway,
I was reading some docs on Objective-C, which I have yet to really
start learning, and I found myself wondering on how Mops (or any OO
Forth for that matter) would handle instantiating or calling objects in
Objective C or C++.
If it's about calling Cocoa/Objective C from where ever:

<QUOTE>
Re: Calling Cocoa APIs from C?
comp.sys.mac.programmer.help
2000-29-09
Post by Erik
Is there a way to do this? Or to make Obj-C calls from C? I'm trying
to get SmallEiffel to work under OSX and Eiffel allows C calls. The
other option would be to use carbon. :/
Yes.
You need to call the C function objc_msgSend( receiver, selector, args... ).
The function sel_getUid() will get you a selector (from a C-String), and
objc_getClass() to get a class (also from a C-String).
----------------------
</QUOTE>

from developer docs
file:///Developer/Documentation/Cocoa/Conceptual/ObjectiveC/9objc_runtime
_reference/index.html

Translated to a non OO Forth:


\ call obj C from MacForth.
\ very simple and trivial usage, no creating instances etc.

\ see carboncocoa.pdf

anew --cocoa--

default-order
decimal


framework cocoa.framework
cocoa.framework

1 machofunc _objc_getClass ( 0string:class -- id )
1 machofunc _sel_getUid ( 0string:method -- SEL )

(*
I don't know in advance how many arguments I'm sending,
so I use an arbitrary default of 7. The first 2 for receiver
and selector and the other 5 for possible arguments.
*)
7 machofunc _objc_msgSend
( id:receiver SEL:selector arg1 ... arg5 -- ret )

\ provide stubs, the actual method will pick what it needs.
: args ( n -- 5-n_stubs )
dup 5 > abort" too many arguments!"
5 swap - 0 ?do 0 loop ;

\ these should be used before and after using the obj-c stuff!
: allocPool ( -- id )
0" NSAutoreleasePool" _objc_getClass
0" alloc" _sel_getUid
0 args _objc_msgSend
0" init" _sel_getUid
0 args _objc_msgSend ;

: releasePool ( id -- ret )
0" release" _sel_getUid
0 args _objc_msgSend ;

prior.stream

\ example:
: pp
allocpool >r \ prolog
0" NSProcessInfo" _objc_getClass \ create processInfo
0" processInfo" _sel_getUid
0 args _objc_msgSend
0" processName" _sel_getUid \ retrieve processName
0 args _objc_msgSend
0" getCString:" _sel_getUid \ copy to pad
pad dup 20 erase 1 args _objc_msgSend drop
r> releasepool drop \ epilog
;

pp

pad dup 0$len type ( expect: My Extended Kernel )
Erik
2005-01-04 23:56:09 UTC
Permalink
Hi!
Post by Erik
The function sel_getUid() will get you a selector (from a
C-String), and
Post by Erik
objc_getClass() to get a class (also from a C-String).
So in other words, Apple (or Brad Cox, ObjC's creator) created a way to
access ObjC functions from non-ObjC languages to maintain some sort of
compatibility. Would that be accurate? That's interesting; it never
occured to me that something like that could be done.

Thanks,
Erik
n***@yahoo.co.jp
2005-01-06 11:46:46 UTC
Permalink
Hi Erik,
Post by Erik
Hi!
Post by Erik
The function sel_getUid() will get you a selector (from a
C-String), and
Post by Erik
objc_getClass() to get a class (also from a C-String).
So in other words, Apple (or Brad Cox, ObjC's creator) created a way to
access ObjC functions from non-ObjC languages to maintain some sort of
compatibility. Would that be accurate? That's interesting; it never
occured to me that something like that could be done.
Thanks,
Erik
A bit supplement.
These functions including "objc_msgSend()" are there also for
Objective-C.

[quote-begin]
Discussion
When it encounters a method call, the compiler generates a call to one
of the functions objc_msgSend, objc_msgSend_stret, objc_msgSendSuper,
or objc_msgSendSuper_stret.
[quote-end]
from: (excuse me for a long line)
<http://developer.apple.com/documentation/Cocoa/Reference/ObjCRuntimeRef/ObjCRuntimeRef/function_group_2.html#//apple_ref/doc/uid/20001426/objc_msgSend>

As Mike and Roelf implied, mechanisms to link to shared library will be
easier to understand when you think it belongs to lower level than
features of programing languages.

Sincerely,
Nao Sacrada

Loading...