addon-sdk/mach_commands.py
author Kyle Huey <khuey@kylehuey.com>
Tue, 11 Aug 2015 06:10:46 -0700
changeset 257246 a13c1f26e351dd6251da641fe7a9eb53790fc2d0
parent 250202 c8cedeb555cd8b3b084a1c3dad3a5e450e52f6e1
child 260987 e240f2db0b466d4bf15fa45654fcf9aa5a7e1db2
permissions -rw-r--r--
Bug 1179909: Refactor stable state handling. r=smaug This is motivated by three separate but related problems: 1. Our concept of recursion depth is broken for things that run from AfterProcessNextEvent observers (e.g. Promises). We decrement the recursionDepth counter before firing observers, so a Promise callback running at the lowest event loop depth has a recursion depth of 0 (whereas a regular nsIRunnable would be 1). This is a problem because it's impossible to distinguish a Promise running after a sync XHR's onreadystatechange handler from a top-level event (since the former runs with depth 2 - 1 = 1, and the latter runs with just 1). 2. The nsIThreadObserver mechanism that is used by a lot of code to run "after" the current event is a poor fit for anything that runs script. First, the order the observers fire in is the order they were added, not anything fixed by spec. Additionally, running script can cause the event loop to spin, which is a big source of pain here (bholley has some nasty bug caused by this). 3. We run Promises from different points in the code for workers and main thread. The latter runs from XPConnect's nsIThreadObserver callbacks, while the former runs from a hardcoded call to run Promises in the worker event loop. What workers do is particularly problematic because it means we can't get the right recursion depth no matter what we do to nsThread. The solve this, this patch does the following: 1. Consolidate some handling of microtasks and all handling of stable state from appshell and WorkerPrivate into CycleCollectedJSRuntime. 2. Make the recursionDepth counter only available to CycleCollectedJSRuntime (and its consumers) and remove it from the nsIThreadInternal and nsIThreadObserver APIs. 3. Adjust the recursionDepth counter so that microtasks run with the recursionDepth of the task they are associated with. 4. Introduce the concept of metastable state to replace appshell's RunBeforeNextEvent. Metastable state is reached after every microtask or task is completed. This provides the semantics that bent and I want for IndexedDB, where transactions autocommit at the end of a microtask and do not "spill" from one microtask into a subsequent microtask. This differs from appshell's RunBeforeNextEvent in two ways: a) It fires between microtasks, which was the motivation for starting this. b) It no longer ensures that we're at the same event loop depth in the native event queue. bent decided we don't care about this. 5. Reorder stable state to happen after microtasks such as Promises, per HTML. Right now we call the regular thread observers, including appshell, before the main thread observer (XPConnect), so stable state tasks happen before microtasks.

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

# Integrates the xpcshell test runner with mach.

from __future__ import absolute_import

import os

import mozpack.path as mozpath

from mozbuild.base import (
    MachCommandBase,
    MozbuildObject,
)

from mach.decorators import (
    CommandArgument,
    CommandProvider,
    Command,
)

class JetpackRunner(MozbuildObject):
    """Run jetpack tests."""
    def run_tests(self, **kwargs):
        self._run_make(target='jetpack-tests')

@CommandProvider
class MachCommands(MachCommandBase):
    @Command('jetpack-test', category='testing',
        description='Run the jetpack test suite (Add-on SDK).')
    def run_jetpack_test(self, **params):
        # We should probably have a utility function to ensure the tree is
        # ready to run tests. Until then, we just create the state dir (in
        # case the tree wasn't built with mach).
        self._ensure_state_subdir_exists('.')

        jetpack = self._spawn(JetpackRunner)

        jetpack.run_tests(**params)

    @Command('generate-addon-sdk-moz-build', category='misc',
        description='Generates the moz.build file for the addon-sdk/ directory.')
    def run_addon_sdk_moz_build(self, **params):
        addon_sdk_dir = mozpath.join(self.topsrcdir, 'addon-sdk')
        js_src_dir = mozpath.join(addon_sdk_dir, 'source/lib')
        dirs_to_files = {}

        for path, dirs, files in os.walk(js_src_dir):
            js_files = [f for f in files if f.endswith(('.js', '.jsm', '.html'))]
            if not js_files:
                continue

            relative = mozpath.relpath(path, js_src_dir)
            dirs_to_files[relative] = js_files

        moz_build = """# AUTOMATICALLY GENERATED FROM mozbuild.template AND mach.  DO NOT EDIT.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

%(moz-build-template)s
if CONFIG['MOZ_WIDGET_TOOLKIT'] != "gonk":
%(non-b2g-modules)s
%(always-on-modules)s"""

        non_b2g_paths = [
            'method/test',
            'sdk/ui',
            'sdk/ui/button',
            'sdk/ui/sidebar',
            'sdk/places',
            'sdk/places/host',
            'sdk/tabs',
            'sdk/panel',
            'sdk/frame',
            'sdk/test',
            'sdk/window',
            'sdk/windows',
            'sdk/deprecated',
        ]

        non_b2g_modules = []
        always_on_modules = []

        for d, files in sorted(dirs_to_files.items()):
            if d in non_b2g_paths:
                non_b2g_modules.append((d, files))
            else:
                always_on_modules.append((d, files))

        def list_to_js_modules(l, indent=''):
            js_modules = []
            for d, files in l:
                if d == '':
                    module_path = ''
                    dir_path = ''
                else:
                    # Ensure that we don't have things like:
                    #   EXTRA_JS_MODULES.commonjs.sdk.private-browsing
                    # which would be a Python syntax error.
                    path = d.split('/')
                    module_path = ''.join('.' + p if p.find('-') == -1 else "['%s']" % p for p in path)
                    dir_path = d + '/'
                filelist = ["'source/lib/%s%s'" % (dir_path, f)
                            for f in sorted(files, key=lambda x: x.lower())]
                js_modules.append("EXTRA_JS_MODULES.commonjs%s += [\n    %s,\n]\n"
                                  % (module_path, ',\n    '.join(filelist)))
            stringified = '\n'.join(js_modules)
            # This isn't the same thing as |js_modules|, since |js_modules| had
            # embedded newlines.
            lines = stringified.split('\n')
            # Indent lines while avoiding trailing whitespace.
            lines = [indent + line if line else line for line in lines]
            return '\n'.join(lines)

        moz_build_output = mozpath.join(addon_sdk_dir, 'moz.build')
        moz_build_template = mozpath.join(addon_sdk_dir, 'mozbuild.template')
        with open(moz_build_output, 'w') as f, open(moz_build_template, 'r') as t:
            substs = { 'moz-build-template': t.read(),
                       'non-b2g-modules': list_to_js_modules(non_b2g_modules,
                                                             indent='    '),
                       'always-on-modules': list_to_js_modules(always_on_modules) }
            f.write(moz_build % substs)