io/commands/internal/Descriptions.protos

Repository
guillermomolina/protos
Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75
Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6
Blob
6a5d53d342829938690d2304977d31e908203850
Source
View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.

//! COMMANDS.md §§3-4: the single validation and snapshot path for command
//! descriptions, shared by `command` and by run admission. Only portable
//! domain checks happen here; native representability, NUL, environment-name
//! equivalence and directory admission stay with `execution.start`.

// An empty matcher Map recognizes exactly normal standard Maps (not
// IdentityMap) without invoking any behavior of the candidate.
mapMatcher: %{}
isMap: (value) => { mapMatcher.match(value) === true }

fail: () => { Error().signal() }

// Scalar families are rejected before any message reaches the candidate.
scalar: (value) => {
    (value === null) || (value === true) || (value === false) ||
        Integer.recognizes(value) || Float.recognizes(value) ||
        String.recognizes(value)
}

descriptionSlots: Array("executable", "arguments", "environment", "workingDirectory")
descriptionSlots.freeze()

// A fresh frozen copy of a String Array; the caller's Array is never frozen.
copyArguments: (arguments) => {
    Array.recognizes(arguments).ifFalse(() => { fail() })
    copy: Array(...arguments)
    for(argument in copy) {
        String.recognizes(argument).ifFalse(() => { fail() })
    }
    copy.freeze()
}

// A fresh frozen normal Map holding every String entry; nothing is inherited.
copyEnvironment: (environment) => {
    isMap(environment).ifFalse(() => { fail() })
    copy: Map()
    environment.each((name, value) => {
        (String.recognizes(name) && String.recognizes(value)).ifFalse(() => { fail() })
        copy[name] = value
    })
    copy.freeze()
}

/// Answers a fresh frozen description of exactly four data slots, or signals
/// an ordinary Error for data outside the portable domains.
snapshot: (executable, arguments, environment, workingDirectory) => {
    String.recognizes(executable).ifFalse(() => { fail() })
    argv: copyArguments(arguments)
    childEnvironment: copyEnvironment(environment)
    ((workingDirectory === null) || String.recognizes(workingDirectory)).ifFalse(() => { fail() })
    description: {
        executable: executable
        arguments: argv
        environment: childEnvironment
        workingDirectory: workingDirectory
    }
    description.freeze()
}

/// Snapshots an admitted run element: any ordinary record whose own slots are
/// exactly the four description slots. No ancestry, registration or identity
/// is consulted.
admit: (value) => {
    scalar(value).ifTrue(() => { fail() })
    (Array.recognizes(value) || isMap(value)).ifTrue(() => { fail() })
    (value.slotNames().size() == descriptionSlots.size()).ifFalse(() => { fail() })
    for(name in descriptionSlots) {
        value.hasSlot(name).ifFalse(() => { fail() })
    }
    snapshot(
        value.slotValue("executable"),
        value.slotValue("arguments"),
        value.slotValue("environment"),
        value.slotValue("workingDirectory")
    )
}