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

Asked

Viewed 394 times

5

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

  • 3

    Welcome to Stack Overflow in Portuguese! I think the question may even get interesting Rodrigo. You have to be more specific in what you want. O stackoverflow is a site of direct questions for direct answers, trying not to get too into the opinion. See Help Center How to Ask. Normally I’d vote to close the question, but I’ll let the community decide.

  • 1

    I think it’s too broad. I suggest focusing on a specific problem, for example, this implementation of stdlib.h. You can [Edit] the question to let it conform to the standards of the site detailed in [help].

  • 1

    I already edited the post to try to focus on one thing first. Thanks for the tip.

  • 2

    @Rodrigoapp, I removed the unrelated text, please focus on the technical problem (the [tour] of the site is quite explicit about this, because it is considered as noise). Did you ever try to implement the function? If so, post your code and explain your problem with it.

  • No, I didn’t. I wouldn’t know where to start. Would it have to be implemented in C? Or in low-level language?

  • @brasofilo Rodrigoapp came to ask in a group on Facebook what it would be like to implement a function that showed a pixel on the screen. I said his question would be greatly appreciated if he asked the question here at Sopt. I think he doesn’t want to know how to implement the function in a particular language, but rather understand the concept behind showing a pixel on the screen by manipulating the VRAM. This is it Rodrigoapp?

  • @Avelino, ah, because it’s important to be in the question then.

  • Well, if that’s what he wants, he could ask another question and let that one save.

  • I think it’s best to understand how functions like printf() and scanf() are implemented and then understand how functions like image making are implemented.

Show 4 more comments

1 answer

5

Where to start:

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

Java

In Java, although Oracle’s JDK/JRE is closed, there is Openjdk, in which you can find the implementation of standard functions such as System.out.println()

System.out.println()

Let’s look at the basic way callback is done, or all the calls (on a slightly more abstract level) of the same:

Nível abstrato fluxograma da println

And you can see all the functions called:

inserir a descrição da imagem aqui

As you can see, the function is done as generic as possible and as much as possible, but always ends up needing to reach 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 don’t want to leave this answer too long) I will post what happens in JNI that is defined in the file 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 still goes down a little more, but it is already 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()).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.