packages feed

zeolite-lang-0.20.0.0: tests/simulate-refs/ref-state.0rx

/* -----------------------------------------------------------------------------
Copyright 2021 Kevin P. Barry

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
----------------------------------------------------------------------------- */

// Author: Kevin P. Barry [ta0kira@gmail.com]

define ReferenceState {
  @value Int strongCount
  @value Int weakCount
  @value Bool locked
  @value Int aliveCleanup
  @value Int dataCleanup
  @value [Append<Formatted>&DefaultOrder<Formatted>] operations

  new () {
    return ReferenceState{ 0, 0, false, 0, 0, Vector:create<Formatted>() }
  }

  addOperation (name) {
    \ operations.append(String.builder()
        .append(name)
        .append("[")
        .append(typename<#x>())
        .append("]: ")
        .append(self)
        .build())
  }

  getOperations () {
    return operations.defaultOrder()
  }

  addStrong () {
    return (strongCount <- strongCount+1)
  }

  remStrong () {
    return (strongCount <- strongCount-1)
  }

  plusStrong (n) {
    return (strongCount <- strongCount+n)
  }

  minusStrong (n) {
    return (strongCount <- strongCount-n)
  }

  hasStrong () {
    return strongCount > 0
  }

  addWeak () {
    return (weakCount <- weakCount+1)
  }

  remWeak () {
    return (weakCount <- weakCount-1)
  }

  getWeak () {
    return weakCount
  }

  hasWeak () {
    return weakCount > 0
  }

  tryLock () {
    if (locked) {
      return false
    } else {
      locked <- true
      return true
    }
  }

  remLock () {
    locked <- false
  }

  hasLock () {
    return locked
  }

  kill () {
    aliveCleanup <- aliveCleanup+1
  }

  isAlive () {
    return aliveCleanup == 0
  }

  freeData () {
    dataCleanup <- dataCleanup+1
  }

  getError () {
    if (aliveCleanup > 1) {
      return "object freed multiple times"
    }
    if (dataCleanup > 1) {
      return "counters freed multiple times"
    }
    if (aliveCleanup > 0 && strongCount > 0) {
      return "object freed while still referenced"
    }
    if (aliveCleanup == 0 && strongCount == 0) {
      return "object not cleaned up after last reference"
    }
    return empty
  }

  formatted () {
    return String.builder()
        .append("L: ")
        .append(locked)
        .append(" S: ")
        .append(strongCount)
        .append(" W: ")
        .append(weakCount)
        .append(" AC: ")
        .append(aliveCleanup)
        .append(" DC: ")
        .append(dataCleanup)
        .build()
  }
}

define Run {
  then (first,second) {
    return RunThen.new(first,second)
  }
}

concrete RunThen {
  @type new (ReferenceMachine,ReferenceMachine) -> (ReferenceMachine)
}

define RunThen {
  refines ReferenceMachine

  @value ReferenceMachine first
  @value ReferenceMachine second

  new (first,second) {
    return RunThen{ first, second }
  }

  runWith (name,state) {
    scoped {
      optional ReferenceMachine next <- first.runWith(name,state)
    } in if (present(next)) {
      return RunThen{ require(next), second }
    } else {
      return second
    }
  }
}