Skip to content

Introduce JS_DefineProperties, DRY error handling #1174

@bnoordhuis

Description

@bnoordhuis

We should consider introducing a JS_DefineProperties or something like it that sets properties in bulk, to make it easier to handle errors, and port code in quickjs.c over to it.

Case in point: js_regexp_exec dedicates 100 lines to setting properties on the result object. Here's an excerpt:

        t = groups, groups = JS_UNDEFINED;
        if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_groups,
                                   t, prop_flags) < 0) {
            goto fail;
        }

        t = js_int32((capture[0] - str_buf) >> shift);
        if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_index, t, prop_flags) < 0)
            goto fail;

        t = str_val, str_val = JS_UNDEFINED;
        if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_input, t, prop_flags) < 0)
            goto fail;

        if (!JS_IsUndefined(indices)) {
            t = indices_groups, indices_groups = JS_UNDEFINED;
            if (JS_DefinePropertyValue(ctx, indices, JS_ATOM_groups,
                                       t, prop_flags) < 0) {
                goto fail;
            }
            t = indices, indices = JS_UNDEFINED;
            if (JS_DefinePropertyValue(ctx, obj, JS_ATOM_indices,
                                       t, prop_flags) < 0) {
                goto fail;
            }
        }

For the record: JS_SetPropertyFunctionList and JS_NewObjectFrom exist and sort of cover this use case, although not exactly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions