How are the functions of the standard libraries of different programming languages implemented?

5

How are standard libraries of different programming languages implemented? For example, stdlib.h , or Java.Swing , how the System.out.prinln() function is implemented for example.

    
asked by anonymous 08.07.2014 / 09:30

1 answer

5

Where to start:

Always look for open implementations of languages that you want to find out how something works. In general for each platform things are implemented differently and in case of C, yes you will need to work at a certain lower level, which can be from ASM or even a subset of the C language.

Java

In Java, although the Oracle JDK / JRE is closed, there is OpenJDK, where you can find the implementation of standard functions such as System.out.println()

System.out.println ()

Let's look at how callback is done, or all calls (on a slightly more abstract level) of the callback:

Andyoucanseeallfunctionscalled:

As you can see, the function is done as generically as possible and as much as possible, but it always ends up needing to get to the lowest level JNI which is where Java communicates with C and makes system calls .

And just so you have a sense of what the lowest level is like (I do not want to leave this answer too long) I will post what occurs in the JNI that is defined in the io_util.c

void
writeBytes(JNIEnv *env, jobject this, jbyteArray bytes,
           jint off, jint len, jfieldID fid)
{
    jint n;
    char stackBuf[BUF_SIZE];
    char *buf = NULL;
    FD fd;

    if (IS_NULL(bytes)) {
        JNU_ThrowNullPointerException(env, NULL);
        return;
    }

    if (outOfBounds(env, off, len, bytes)) {
        JNU_ThrowByName(env, "java/lang/IndexOutOfBoundsException", NULL);
        return;
    }

    if (len == 0) {
        return;
    } else if (len > BUF_SIZE) {
        buf = malloc(len);
        if (buf == NULL) {
            JNU_ThrowOutOfMemoryError(env, NULL);
            return;
        }
    } else {
        buf = stackBuf;
    }

    (*env)->GetByteArrayRegion(env, bytes, off, len, (jbyte *)buf);

    if (!(*env)->ExceptionOccurred(env)) {
        off = 0;
        while (len > 0) {
            fd = GET_FD(this, fid);
            if (fd == -1) {
                JNU_ThrowIOException(env, "Stream Closed");
                break;
            }
            n = IO_Write(fd, buf+off, len);
            if (n == JVM_IO_ERR) {
                JNU_ThrowIOExceptionWithLastError(env, "Write error");
                break;
            } else if (n == JVM_IO_INTR) {
                JNU_ThrowByName(env, "java/io/InterruptedIOException", NULL);
                break;
            }
            off += n;
            len -= n;
        }
    }
    if (buf != stackBuf) {
        free(buf);
    }
}

Of course, it's still going down a bit, but it's depending on the Operating System, since Linux, Windows, and BSDs need different implementations. But this is the lowest and most common point of the function you asked for as an example ( System.out.println() ).

    
08.07.2014 / 15:04