Function objects

Ecmascript functions

Duktape Function objects add a few properties to standard Ecmascript properties. The table below summarizes properties assigned to newly created function instances (properties can of course be added or removed afterwards):

Property nameCompatibilityDescription
lengthstandardFunction (nominal) argument count (if relevant). Present for all Function objects, including bound functions.
prototypestandardPrototype used for new objects when called as a constructor. Present for most constructable Function objects, not copied to bound functions.
callerstandardAccessor which throws an error. Present for strict functions and bound functions. Not copied to bound functions. (If DUK_USE_NONSTD_FUNC_CALLER_PROPERTY is given, non-strict functions will get a non-standard caller property.)
argumentsstandardAccessor which throws an error. Present for strict functions and bound functions. Not copied to bound functions.
nameDuktapeFunction name, see below. Bound function name is based on this property, with a "bound " prefix (standard ES2015 behavior).
fileNameDuktapeFilename or context where function was declared (same name as in error tracebacks). Copied to bound function from target function.
calleen/aNever assigned by default (listed here to clarify relationship to "caller" property).

The name property is assigned to all functions and is also the name used in tracebacks. It is assigned as follows:

function funcDecl() {
    /* Function declaration: 'name' is declaration name, here 'funcDecl'. */
}

var foo = function namedFunc() {
    /* Named function expression: 'name' is the name used in expression,
     * here 'namedFunc' (not 'foo').
     */
}

var bar = function () {
    /* Anonymous function expression: 'name' is the empty string. */
}
Several Ecmascript built-in functions have properties different from user created Functions.

Duktape/C functions

User-created Duktape/C functions (duk_push_c_function()) have a different set of properties to reduce Function object memory footprint:

Property nameCompatibilityDescription
lengthstandard Function argument count, matches argument to duk_push_c_function(), 0 for varargs. Non-writable and non-configurable.

Note in particular that the standard prototype, caller, and arguments properties are missing by default. This is not strictly compliant but is important to reduce function footprint. User code can of course assign these properties but is not required to do so.

There's also no (non-standard) name property. Setting it manually is useful because it affects how a function appears in tracebacks.

Lightweight Duktape/C functions

Lightweight Duktape/C functions (lightfuncs) are a very memory efficient way of representing a native function in the Ecmascript environment. Lightfuncs don't have a property table so they can't hold properties. However, they inherit from Function.prototype and have the following virtual properties (which are non-configurable and non-writable):

Property nameCompatibilityDescription
lengthstandardFunction (nominal) argument count.
nameDuktapeFunction name: "light_<PTR>_<FLAGS>".

The name property is an automatically generated virtual function name. <PTR> is a platform dependent dump of the Duktape/C function pointer, and <FLAGS> is a raw hex dump of the 16-bit internal control fields (the format is Duktape internal). You shouldn't rely on a specific format. For example:

duk> print(myLightFunc.name);
light_0805b94c_0511

As for ordinary functions, a lightfunc coerces to an implementation dependent string. You shouldn't rely on a specific format. For example:

duk> print(myLightFunc);
function light_0805b94c_0511() {"light"}

For more details, see: