Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

fuzion/sys/internal_array.fz


# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation.  If not, see <https://www.gnu.org/licenses/>.


# -----------------------------------------------------------------------
#
#  Tokiwa Software GmbH, Germany
#
#  Source code of Fuzion standard library feature sys.internal_array
#
#  Author: Fridtjof Siebert (siebert@tokiwa.software)
#
# -----------------------------------------------------------------------

# helper to allocate memory for an internal_array.
# returns an internal_array.
#
module internal_array_init(T type, length i32) =>
  internal_array T (fuzion.sys.alloc T length) length


# fuzion.sys.internal_array_init -- one-dimensional low-level array
#
module internal_array(T type, module data Array T, module length i32) is


  # make sure that this internal array will no longer be modified.  This is just for
  # debugging and will be a NOP in case internal checks are disabled or in case the
  # backend does not support this.
  #
  module freeze unit => intrinsic

  # check that this internal array was not frozen, i.e., `freeze` was not called.
  # Cause a runtime error if this `freeze` was called.  Note that this is a NOP if
  # the backend does not support these checks or internal checks are disabled.
  #
  module ensure_not_frozen unit => intrinsic


  # wrap this into an immutable array.
  #
  module fixed as_array =>
    array (fuzion.sys.internal_array T data length) unit unit unit


  module indices => 0..length-1

  module index [ ] (i i32) T
    pre
      safety: 0 ≤ i < length
  =>
    fuzion.sys.getel T data i

  module set [ ] (i i32, o T) unit
    pre
      safety: 0 ≤ i < length
      debug : {ensure_not_frozen; true}
  # post   NYI
    # debug: array.this[i] == o
  =>
    fuzion.sys.setel data i o


# allocate memory for l elements of type X
#
type.alloc(X type, l i32) Array X => intrinsic

# get element at index i from data d
#
type.getel(X type, d Array X, i i32) X => intrinsic

# set element at index i in data d
#
type.setel(X type, d Array X, i i32, o X) unit => intrinsic

last changed: 2025-07-10