ffi/internal/ObjectProjection.protos

Repository
guillermomolina/protos
Commit
2723a55555e7beecee0816bf99d16e3c8c1ae635
Tree
c36054e4e5abd7fc29a039b4e096cac2e8706492
Blob
a69abc15901718e3f930561926c7c3224a628fec
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.

//! Private FFI objectprojection operations. Dependencies remain initializer-owned.

create: (associations, captureGroup, commitPrepared, fail, isOrdinaryObject, projectionLayout, projectionPlan, readGroup, requireArity, requireCanonicalMethod, slots, storage) => {
    snapshotGroup: null
    snapshotValue: null
    readAs: null
    captureSlots: null
    captureSlotValue: null
    writeFrom: null

    methods: {
        snapshotGroup: (parentPrototype, layout, nodes, values) => {
            result: null
            if(storage(layout).layoutKind == "array") {
                elements: slots(nodes.size())
                index: 0
                for(node in nodes) {
                    elements[index] = snapshotValue(node, values[index])
                    index = index + 1
                }
                result = elements
            } else {
                requireCanonicalMethod(parentPrototype, "createSlot")
                answer: parentPrototype {}
                create: answer.createSlot
                for(node in nodes) {
                    create(node.key, snapshotValue(node, values.at(node.key)))
                }
                result = answer
            }
            result
        }

        // A nested struct or union is an ordinary object of Object; leaves keep the
        // value of the existing typed read.
        snapshotValue: (node, value) => {
            result: value
            if(node.children !== null) {
                result = snapshotGroup(Object, node.layout, node.children, value)
            }
            result
        }

        readAs: (prototype: fail(), region: fail(), arms: fail(), ...extra) => {
            requireArity(extra)
            (isOrdinaryObject(prototype) === true).ifFalse(() => fail())
            associations.containsKey(prototype).ifFalse(() => fail())
            layout: projectionLayout(region, false)
            (associations.at(prototype) === layout).ifFalse(() => fail())
            requireCanonicalMethod(prototype, "createSlot")
            nodes: projectionPlan(layout, arms)
            snapshotGroup(prototype, layout, nodes, readGroup(region, layout, nodes))
        }

        // The local slots an ordinary object holds for the planned members, captured
        // through its canonical bound `slotValue` into a private Map (an Array per
        // fixed array) of exactly the shape captureGroup admits. A missing local slot
        // is the ordinary Error; delegated slots are never consulted and slots
        // outside the plan are never read.
        captureSlots: (layout, nodes, input) => {
            result: null
            if(storage(layout).layoutKind == "array") {
                Array.recognizes(input).ifFalse(() => fail())
                ordered: Array(...input)
                (ordered.size() == nodes.size()).ifFalse(() => fail())
                elements: slots(nodes.size())
                index: 0
                for(node in nodes) {
                    elements[index] = captureSlotValue(node, ordered[index])
                    index = index + 1
                }
                result = elements
            } else {
                (isOrdinaryObject(input) === true).ifFalse(() => fail())
                requireCanonicalMethod(input, "slotValue")
                read: input.slotValue
                fields: Map()
                for(node in nodes) {
                    fields[node.key] = captureSlotValue(node, read(node.key))
                }
                result = fields
            }
            result
        }

        captureSlotValue: (node, value) => {
            result: value
            if(node.children !== null) {
                result = captureSlots(node.layout, node.children, value)
            }
            result
        }

        writeFrom: (region: fail(), value: fail(), arms: fail(), ...extra) => {
            requireArity(extra)
            layout: projectionLayout(region, true)
            (isOrdinaryObject(value) === true).ifFalse(() => fail())
            requireCanonicalMethod(value, "parent")
            prototype: value.parent()
            associations.containsKey(prototype).ifFalse(() => fail())
            (associations.at(prototype) === layout).ifFalse(() => fail())
            requireCanonicalMethod(value, "slotValue")
            nodes: projectionPlan(layout, arms)
            fields: captureSlots(layout, nodes, value)
            commitPrepared(region, layout, nodes, captureGroup(layout, nodes, fields))
        }
    }

    // Bind private operations once; factory-created closures use normal completion.
    snapshotGroup = methods.snapshotGroup
    snapshotValue = methods.snapshotValue
    readAs = methods.readAs
    captureSlots = methods.captureSlots
    captureSlotValue = methods.captureSlotValue
    writeFrom = methods.writeFrom

    {
        snapshotGroup: snapshotGroup
        snapshotValue: snapshotValue
        readAs: readAs
        captureSlots: captureSlots
        captureSlotValue: captureSlotValue
        writeFrom: writeFrom
    }
}