Function: make-closure

make-closure is a function defined in alloc.c.

Signature

(make-closure PROTOTYPE &rest CLOSURE-VARS)

Documentation

Create a byte-code closure from PROTOTYPE and CLOSURE-VARS.

Return a copy of PROTOTYPE, a byte-code object, with CLOSURE-VARS replacing the elements in the beginning of the constant-vector.

Probably introduced at or before Emacs version 28.1.

Source Code

// Defined in /usr/src/emacs/src/alloc.c
{
  Lisp_Object protofun = args[0];
  CHECK_TYPE (COMPILEDP (protofun), Qbyte_code_function_p, protofun);

  /* Create a copy of the constant vector, filling it with the closure
     variables in the beginning.  (The overwritten part should just
     contain placeholder values.) */
  Lisp_Object proto_constvec = AREF (protofun, COMPILED_CONSTANTS);
  ptrdiff_t constsize = ASIZE (proto_constvec);
  ptrdiff_t nvars = nargs - 1;
  if (nvars > constsize)
    error ("Closure vars do not fit in constvec");
  Lisp_Object constvec = make_uninit_vector (constsize);
  memcpy (XVECTOR (constvec)->contents, args + 1, nvars * word_size);
  memcpy (XVECTOR (constvec)->contents + nvars,
	  XVECTOR (proto_constvec)->contents + nvars,
	  (constsize - nvars) * word_size);

  /* Return a copy of the prototype function with the new constant vector. */
  ptrdiff_t protosize = PVSIZE (protofun);
  struct Lisp_Vector *v = allocate_vectorlike (protosize, false);
  v->header = XVECTOR (protofun)->header;
  memcpy (v->contents, XVECTOR (protofun)->contents, protosize * word_size);
  v->contents[COMPILED_CONSTANTS] = constvec;
  return make_lisp_ptr (v, Lisp_Vectorlike);
}