Expand/Shrink

platform

Definition: integer i = platform()
Description: platform() is a function built-in to the interpreter.
It indicates the platform that the program is being executed on: WINDOWS, LINUX, or JS.
pwa/p2js: Supported. Fairly obviously, the desktop versions never resolve platform() to JS, only pwa/p2js does that.
Comments: When p.exe or pw.exe is running the platform is WINDOWS.
When phix is running the platform is LINUX.
When transpiled to JavaScript via pwa/p2js and running in a web browser the platform is JS.

Phix implicitly defines (via psym.e/syminit) the following constants:

    global constant WINDOWS = 2,
                    LINUX = 3,
                    ARM = 16,
                    JS = 4

The deprecated constant WIN32 is the same as WINDOWS; it does not mean 32-bit. (See machine_bits() and machine_word()).
The wholly unnecessary aliases WEB and JAVASCRIPT were removed in 1.0.2, please use JS instead.
(ARM is not yet officially supported; I will rationalise these to 2/4/8/16 as part of the 2.0.0 release)


Use platform() when you want to execute different code depending on which platform the program is running on.

You may also use (eg) requires(WINDOWS) to trigger a crash should the runtime requirements not be met.

Additional platforms may be added as Phix is ported to new machines and operating environments.

The call to platform() costs nothing. It is optimized at compile-time into the appropriate integer value: 2, 3 or 4. Further, no code whatsoever will be emitted for the comparisons, else, and two of the branches in the following example.
Example:
if platform()=WINDOWS then
    puts(1,"hello Windows")
elsif platform()=LINUX then
    puts(1,"hello Linux")
elsif platform()=JS then
    puts(1,"hello JavaScript")
else
    puts(1,"has someone added another platform I don't know about?")
end if
Implementation: Optimised away by the compiler - search for T_platform in pmain.e and/or JS in p2js.exw for more details.
See Also: machine_bits, machine_word, format directive, version, requires