4

If I define a function in my vimrc (or in a plugin), Vim will load it and I can use it. But what's happening under the hood after my vimscript is parsed?

Is it transcoded down to C and compiled?

Is it put on the heap at runtime and referenced through a function pointer?

  • 4
    Vimscript is nothing more than series of ex commands so your functions are interpreted just like any ex commands. No compilation or transcoding necessary. – romainl Feb 6 '16 at 20:56
7

Is it transcoded down to C and compiled?

Vimscript is interpreted, and in fact you can do basically the same things in vimscript as you could at the : command (and the other way around), plus or minus some bar and escape characters to make things sane.

The evaluation code is mainly located in eval.c, which you can browse if you are brave.

Is it put on the heap at runtime and referenced through a function pointer?

While a built-in function is ultimately a function pointer to the C implementation, a user function is represented by a ufunc_T structure (trimmed of some stuff for brevity):

struct ufunc
{
    int      uf_varargs;
    int      uf_flags;
    int      uf_calls;
    garray_T uf_args;
    garray_T uf_lines;
    scid_T   uf_script_ID;
    int      uf_refcount;
    char_u   uf_name[1];
};

The function call_user_func is used to actually make the call to a user function. After doing a ton of setup to initialize arguments, variables, profiling information and state management, call_user_func does

do_cmdline(NULL, get_func_line, (void *)fc, DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);

to execute each line of the function as a : command line (the implementation of do_cmdline in in ex_docmd.c).

  • Sated my curiosity. Great answer! – ivan Feb 10 '16 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.