/*
 * Copyright 2026 WebAssembly Community Group participants
 *
 * 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.
 */

#ifndef wasm_ir_runtime_table_h
#define wasm_ir_runtime_table_h

#include <stddef.h>
#include <vector>

#include "literal.h"
#include "wasm.h"

namespace wasm {

// Runtime representation of a table for interpreting use cases e.g.
// wasm-interpreter.h. Effectively a vector of Literal. Throws TrapException on
// out-of-bounds access.
class RuntimeTable {
public:
  RuntimeTable(Table table) : tableDefinition(table) {}
  virtual ~RuntimeTable() = default;

  virtual void set(Address i, Literal l) = 0;

  virtual Literal get(Address i) const = 0;

  // Returns nullopt if the table grew beyond the max possible size.
  [[nodiscard]] virtual std::optional<Address> grow(Address delta,
                                                    Literal fill) = 0;

  virtual Address size() const = 0;

  // True iff this is a subtype of the definition `other`. i.e. This table can
  // be imported with the definition of `other`
  virtual bool isSubType(const Table& other) {
    return tableDefinition.addressType == other.addressType &&
           Type::isSubType(tableDefinition.type, other.type) &&
           size() >= other.initial && tableDefinition.max <= other.max;
  }

  const Table* getDefinition() const { return &tableDefinition; }

protected:
  const Table tableDefinition;
};

class RealRuntimeTable : public RuntimeTable {
public:
  RealRuntimeTable(Literal initial, Table table_) : RuntimeTable(table_) {
    table.resize(tableDefinition.initial, initial);
  }

  RealRuntimeTable(const RealRuntimeTable&) = delete;
  RealRuntimeTable& operator=(const RealRuntimeTable&) = delete;

  void set(Address i, Literal l) override;

  Literal get(Address i) const override;

  std::optional<Address> grow(Address delta, Literal fill) override;

  Address size() const override { return table.size(); }

private:
  std::vector<Literal> table;
};

} // namespace wasm

#endif // wasm_ir_runtime_table_h
