public function renderFormJavascriptPrototypes(array $context, FormView $rootView)
    {
        if (!isset($rootView->vars['prototype'])) {
            throw new \InvalidArgumentException('The given root form view has no prototype');
        }

        $stack = [
            // FormView $prototype, string $prototypeName, string[] $parentPrototypeNames
            [$rootView->vars['prototype'], $rootView->vars['prototype']->vars['name'], []],
        ];

        $output = '[';
        $counter = 0;

        while (list($prototype, $prototypeName, $parentPrototypeNames) = \array_pop($stack)) {
            try {
                // the "unique_block_prefix" must be changed to prevent messing
                // up FormRenderer's internal cache and causing errors later on
                $prototype->vars['unique_block_prefix'] .= \sprintf('_%d', ++$this->prototypeRenderUidSeq);

                if (isset($prototype->vars['prototype'])) {
                    // directly nested collection prototypes lead to recursion in searchAndRenderBlock()
                    throw new \RuntimeException('Directly nested collections with prototypes are not supported. Add an intermediate type or disable prototypes.');
                }
                $code = $this->renderer->searchAndRenderBlock($prototype, 'javascript_prototype', $context);

                if (++$counter > 1) {
                    $output .= ",\n";
                }

                $output .= \sprintf(
                    '{id: %s, prototypeName: %s, parentPrototypeNames: %s, initializer: function ($field) { %s }}',
                    \json_encode($prototype->vars['id']),
                    \json_encode($prototypeName),
                    \json_encode($parentPrototypeNames),
                    $code
                );
            } catch (\LogicException $e) {
                foreach ($prototype->children as $child) {
                    $stack[] = isset($child->vars['prototype'])
                        ? [
                            $child->vars['prototype'],
                            $child->vars['prototype']->vars['name'],
                            \array_merge($parentPrototypeNames, [$prototypeName]),
                        ]
                        : [
                            $child,
                            $prototypeName,
                            $parentPrototypeNames,
                        ];
                }
            }
        }

        $output .= ']';

        return $output;
    }