Article View: comp.lang.misc
Article #6261Re: Call by string (was: B&D)
From: David Phillip Oster
Date: Tue, 31 Jan 1989 16:43
Date: Tue, 31 Jan 1989 16:43
41 lines
2263 bytes
2263 bytes
Under the macintosh operatiing system, call by string is simply:
typedef long (*LFuncPtr)(); /* LFuncPtr is a pointer to a func
returning a long */
LFuncPtr *byString;
if(NULL != (byString = (LFuncPtr *)
GetNamedResource('XCMD',"\pYour Name Here"))){
HLock((Handle) byString);
(**byString)(arg1, arg2, arg3); /* your args here */
HUnlock((Handle byString);
}
explanation:
On the mac, every program has a built-in archive of data called "the
resource fork". The system call GetResource() fetches data by 4-char type
and integer id number. The system call GetNamedResource() fetches data by
f-char type and string name. The "\p" generates a length byte, which is
the kind of string GetNamedResource takes.
It doesn't return a ptr to the data, but a ptr to ptr. The extra level of
indirection lets the system move the data, if it needs to change its size,
so long as I always double indirect when I access through the ptr to ptr.
HLock() tells the system not to move this piece of data for a while.
(Can't have the code moving out from under the program counter while it is
executing!
You could repackage all of the above (except the HUnlock()) as a function
called "CallByString", that takes one argument, a string. It would lock
anmd return a pointer to the code object, if it found it. Otherwise, it
would return a pointer to an error routine.
Then you could say:
(*CallByString(stringVariable))(arg1, arg2, arg3)
You can't get much cleaner than that. C hardly seems to be a B&D language
here: as I have proved by demonstrating, with the right library support,
call by string isn't difficult at all in C.
No, if you want B&D, try to call C printf from Pascal. Try to write printf
in pascal.
Technical Details
Message-ID:
<27844@ucbvax.BERKELEY.EDU>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!pasteur!ucbvax!dewey.soe.berkeley.edu!osterReferences:
<4279@enea.se> <1260@iesd.uucp>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!pasteur!ucbvax!dewey.soe.berkeley.edu!oster From: oster@dewey.soe.berkeley.edu (David Phillip Oster) Newsgroups: comp.lang.misc Subject: Re: Call by string (was: B&D) Message-ID: <27844@ucbvax.BERKELEY.EDU> Date: 31 Jan 89 16:43:50 GMT References: <4279@enea.se> <1260@iesd.uucp> Sender: usenet@ucbvax.BERKELEY.EDU Reply-To: oster@dewey.soe.berkeley.edu.UUCP (David Phillip Oster) Organization: School of Education, UC-Berkeley Lines: 41