Re: OSX platform code

new topic     » goto parent     » topic index » view thread      » older message » newer message
Marco A. Achury P. said...

What platform() code do you plan to use on OSX?

It is different enough that a new code should be returned, but IMHO, a new code should be returned for FreeBSD as well, but it's too late for that. platform() returns 3 for Linux and FreeBSD and returns 4 for OS X. However, as Jim said, I would not use platform() any more. The new ifdef system is much better in almost all ways as it removes any run time check. It is only a parse time check. ifdef has a few predefined words thus far:

  • DOS32
  • WIN32
  • LINUX
  • FREEBSD
  • OSX
  • UNIX
  • EU400

UNIX is true when LINUX, FREEBSD or OSX is true. FREEBSD is only true on a legitimate FreeBSD machine. It is not true on Linux machines.

You can also define your own words at runtime by:

C:\EUPHORIA > exwc -D MYWORD myapp.ex 

or in code:

with define=MYWORD 

Now, you can do things such as:

while get_line() do 
    ifdef DEBUG then  
        printf(1, "Line='%s'\n", {line}) 
    end ifdef 
    -- process code 
end while 

The above is where the real power of the ifdef system comes in, because, let's say that you run the above program with DEBUG defined, i.e.:

$ exu -D DEBUG myprog.ex 

The parser comes across the ifdef word, and makes a decision at parse time. Should this code be included? Oh, yes, it should, DEBUG is defined. So, the parser then only emits IL code for the contents of the ifdef, not the ifdef itself. So, the interpreter when it hit's this portion of code, actually sees:

while get_line() do 
    printf(1, "Line='%s'\n", {line}) 
    -- process code 
end while 

Notice, there is no logic decision in the code. That loop could loop 10,000,000,000,000,000 times w/no performance hit what-so-ever for deciding if it is DEBUG or not. Now, let's say you are done with your app and you run it without define DEBUG. Now the interpreter will see:

while get_line() do 
    -- process code 
end while 

Notice, all DEBUG code is gone, the interpreter sees none of it.

This is the same for any platform check. For instance, when wrapping a library:

ifdef WIN32 then 
    constant lib = open_dll("mylib.dll") 
elsifdef OSX then 
    constant lib = open_dll("mylib.dylib") 
elsifdef UNIX then 
    constant lib = open_dll("mylib.so") 
elsifdef DOS32 then 
    puts(1, "Please upgrade finally!? grin") 
    abort(1) 
else 
    puts(1, "Hm, what platform is this? Please email me!") 
    abort(2) 
end ifdef 

Notice how we can benefit from using a constant here. There is no redefinition of the lib constant because only 1 of those blocks is emitted. Also notice a trick I did with OSX and UNIX. When opening a library, Linux and FreeBSD are enough alike to support the same open_dll call, however, OS X is not, notice the .dylib extension. Thus, I checked for OSX first, then for UNIX (in which is true for FreeBSD, Linux and OSX but the OSX target would have already been tripped if it were OSX).

So, the long story is, platform() returns 4, but you should use ifdef. smile

Jeremy

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu