Archived
Radiance becomes a multi-model engine: one process serves several models behind one port, loads each when a request names it, parks it in host memory or restores it from its container, and shares the cards between them (docs/MULTIMODEL.md). The configuration is one YAML file; the command line flags are gone (`radiance --config FILE`, hot reload on SIGHUP). Formats. IQ8R (rotated int8) and IQR-V4 replace FP8 and MXFP4 as whole-model formats, with an int8 KV cache (`kv-cache-dtype: i8`) in place of the fp8 one. Containers whose weights are block FP8, MXFP4, M2XFP4, NVFP4 or ParoQuant are refused at startup by the name of the first such weight. Not served until they have int8 containers: GLM-5.3-Flash, Ornith-1.5, Sage-A1.5, Nemotron-3.5-Bolt-74B, Ling-3.0-flash, drama-3 and Anima's FP8 container; Nemotron-3.5-Bolt-E4B, the Gemma 4 31B DFlash containers and MiniCPM5-2B bf16 serve without their drafter. 1.17.0 serves all of them. Speed. Chained launches for Qwen3.8-27B, Gemma 4, Gemma 3 and the small bf16 models, a new Qwen3.8-Flash-Next container (IQR-V4 experts, int8 trunk) with its decode step and prefill reworked, and a reworked adaptive draft window. Deployment. deploy/compose has two fleet files, tp1.yaml and tp2.yaml, each with its configuration beside it, in place of the per-model files. `host-pool-mib` is a model's host pool in all, split among its ranks. The dashboard is rebuilt: an hour of history kept by the server, each accelerator's memory by model, model states over time. Merged since 1.17.0: Nemotron-3.5-Bolt-74B-A9B by keptn42 (#339); GLM-5.3-Flash on four cards by AnimusNull (#397); chained launches for Flash-Next's decode step (#396) and preempted requests keeping their prefix and place (#405) by mici81; --prefill-interleave auto by Code000 (#410); eight more libr3d kernels and the w4a4 reference by RikshaDriver (#403, #407, #408); libr3d large-BAR and E4M3 decode fixes by neuromanMD (#438, #439); int4/int5 decode tiles by Kezi (#426); attention for Nanbeige 4.3 33B by everplays (#417); expert_stats_report.py by baKon (#431). Fixed: #398, #402, #419, #437 (dschmitz's patch); added the metrics of #392, and multi-model serving as asked for in #367 and #109. Plugin ABI 39: out-of-tree plugins need to be rebuilt. Thanks to @keptn42, @AnimusNull, @mici81, @Code000, @RikshaDriver, @neuromanMD, @Kezi, @everplays, @baKon and @dschmitz.
902 lines
35 KiB
C++
902 lines
35 KiB
C++
#pragma once
|
|
|
|
#include "string.h"
|
|
#include "utils.h"
|
|
|
|
#include "util/json.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <set>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <unordered_map>
|
|
|
|
namespace jinja {
|
|
|
|
struct value_t;
|
|
using value = std::shared_ptr<value_t>;
|
|
|
|
|
|
// Helper to check the type of a value
|
|
template<typename T>
|
|
struct extract_pointee {
|
|
using type = T;
|
|
};
|
|
template<typename U>
|
|
struct extract_pointee<std::shared_ptr<U>> {
|
|
using type = U;
|
|
};
|
|
template<typename T>
|
|
bool is_val(const value & ptr) {
|
|
using PointeeType = typename extract_pointee<T>::type;
|
|
return dynamic_cast<const PointeeType*>(ptr.get()) != nullptr;
|
|
}
|
|
template<typename T>
|
|
bool is_val(const value_t * ptr) {
|
|
using PointeeType = typename extract_pointee<T>::type;
|
|
return dynamic_cast<const PointeeType*>(ptr) != nullptr;
|
|
}
|
|
template<typename T, typename... Args>
|
|
std::shared_ptr<typename extract_pointee<T>::type> mk_val(Args&&... args) {
|
|
using PointeeType = typename extract_pointee<T>::type;
|
|
return std::make_shared<PointeeType>(std::forward<Args>(args)...);
|
|
}
|
|
template<typename T>
|
|
const typename extract_pointee<T>::type * cast_val(const value & ptr) {
|
|
using PointeeType = typename extract_pointee<T>::type;
|
|
return dynamic_cast<const PointeeType*>(ptr.get());
|
|
}
|
|
template<typename T>
|
|
typename extract_pointee<T>::type * cast_val(value & ptr) {
|
|
using PointeeType = typename extract_pointee<T>::type;
|
|
return dynamic_cast<PointeeType*>(ptr.get());
|
|
}
|
|
// End Helper
|
|
|
|
|
|
struct context; // forward declaration
|
|
|
|
|
|
// for converting from JSON to jinja values
|
|
// example input JSON:
|
|
// {
|
|
// "messages": [
|
|
// {"role": "user", "content": "Hello!"},
|
|
// {"role": "assistant", "content": "Hi there!"}
|
|
// ],
|
|
// "bos_token": "<s>",
|
|
// "eos_token": "</s>",
|
|
// }
|
|
//
|
|
// to mark strings as user input, wrap them in a special object:
|
|
// {
|
|
// "messages": [
|
|
// {
|
|
// "role": "user",
|
|
// "content": {"__input__": "Hello!"} // this string is user input
|
|
// },
|
|
// ...
|
|
// ],
|
|
// }
|
|
//
|
|
// marking input can be useful for tracking data provenance
|
|
// and preventing template injection attacks
|
|
//
|
|
// Note: T_JSON can be rad::Json
|
|
template<typename T_JSON>
|
|
void global_from_json(context & ctx, const T_JSON & json_obj, bool mark_input);
|
|
|
|
/* One global from a JSON value, converted in place: a caller assembling several globals from
|
|
* JSON values it already holds (a chat render's messages and tools) need not first copy them
|
|
* into an intermediate JSON object for global_from_json to convert back out. */
|
|
void global_set_from_json(context & ctx, const std::string & key,
|
|
const rad::Json & j, bool mark_input);
|
|
|
|
//
|
|
// base value type
|
|
//
|
|
|
|
struct func_args; // function argument values
|
|
|
|
using func_hptr = value(const func_args &);
|
|
using func_handler = std::function<func_hptr>;
|
|
using func_builtins = std::map<std::string, func_handler>;
|
|
|
|
enum value_compare_op { eq, ge, gt, lt, ne };
|
|
bool value_compare(const value & a, const value & b, value_compare_op op);
|
|
|
|
struct value_t {
|
|
int64_t val_int;
|
|
double val_flt;
|
|
string val_str;
|
|
|
|
std::vector<value> val_arr;
|
|
std::vector<std::pair<value, value>> val_obj;
|
|
|
|
func_handler val_func;
|
|
|
|
// only used if ctx.is_get_stats = true
|
|
struct stats_t {
|
|
bool used = false;
|
|
// ops can be builtin calls or operators: "array_access", "object_access"
|
|
std::set<std::string> ops;
|
|
// utility to recursively mark value and its children as used
|
|
static void mark_used(value & val, bool deep = false);
|
|
} stats;
|
|
|
|
value_t() = default;
|
|
value_t(const value_t &) = default;
|
|
virtual ~value_t() = default;
|
|
|
|
// Note: only for debugging and error reporting purposes
|
|
virtual std::string type() const { return ""; }
|
|
|
|
virtual int64_t as_int() const { throw_type_error("is not an int value"); }
|
|
virtual double as_float() const { throw_type_error("is not a float value"); }
|
|
virtual string as_string() const { throw_type_error("is not a string value"); }
|
|
virtual bool as_bool() const { throw_type_error("is not a bool value"); }
|
|
virtual const std::vector<value> & as_array() const { throw_type_error("is not an array value"); }
|
|
virtual const std::vector<std::pair<value, value>> & as_ordered_object() const { throw_type_error("is not an object value"); }
|
|
virtual value invoke(const func_args &) const { throw_type_error("is not a function value"); }
|
|
virtual bool is_none() const { return false; }
|
|
virtual bool is_undefined() const { return false; }
|
|
virtual const func_builtins & get_builtins() const { throw_type_error("has no builtins"); }
|
|
|
|
virtual bool has_key(const value &) { throw_type_error("is not an object value"); }
|
|
virtual void insert(const value & /* key */, const value & /* val */) { throw_type_error("is not an object value"); }
|
|
virtual value & at(const value & /* key */, value & /* default_val */) { throw_type_error("is not an object value"); }
|
|
virtual value & at(const value & /* key */) { throw_type_error("is not an object value"); }
|
|
virtual value & at(const std::string & /* key */, value & /* default_val */) { throw_type_error("is not an object value"); }
|
|
virtual value & at(const std::string & /* key */) { throw_type_error("is not an object value"); }
|
|
virtual value & at(int64_t /* idx */, value & /* default_val */) { throw_type_error("is not an array value"); }
|
|
virtual value & at(int64_t /* idx */) { throw_type_error("is not an array value"); }
|
|
|
|
virtual bool is_numeric() const { return false; }
|
|
virtual bool is_hashable() const { return false; }
|
|
virtual bool is_immutable() const { return true; }
|
|
virtual hasher unique_hash() const noexcept = 0;
|
|
// TODO: C++20 <=> operator
|
|
// NOTE: We are treating == as equivalent (for normal comparisons) and != as strict nonequal (for strict (is) comparisons)
|
|
virtual bool operator==(const value_t & other) const { return equivalent(other); }
|
|
virtual bool operator!=(const value_t & other) const { return nonequal(other); }
|
|
|
|
// Note: only for debugging purposes
|
|
virtual std::string as_repr() const { return as_string().str(); }
|
|
|
|
private:
|
|
[[noreturn]] void throw_type_error(const char* expected) const {
|
|
throw std::runtime_error(type() + " " + expected);
|
|
}
|
|
|
|
protected:
|
|
virtual bool equivalent(const value_t &) const = 0;
|
|
virtual bool nonequal(const value_t & other) const { return !equivalent(other); }
|
|
};
|
|
|
|
//
|
|
// utils
|
|
//
|
|
|
|
const func_builtins & global_builtins();
|
|
|
|
std::string value_to_json(const value & val, int indent = -1, const std::string_view item_sep = ", ", const std::string_view key_sep = ": ");
|
|
|
|
// Note: only used for debugging purposes
|
|
std::string value_to_string_repr(const value & val);
|
|
|
|
struct not_implemented_exception : public std::runtime_error {
|
|
not_implemented_exception(const std::string & msg) : std::runtime_error("NotImplemented: " + msg) {}
|
|
};
|
|
|
|
struct value_hasher {
|
|
size_t operator()(const value & val) const noexcept {
|
|
return val->unique_hash().digest();
|
|
}
|
|
};
|
|
|
|
struct value_equivalence {
|
|
bool operator()(const value & lhs, const value & rhs) const {
|
|
return *lhs == *rhs;
|
|
}
|
|
bool operator()(const std::pair<value, value> & lhs, const std::pair<value, value> & rhs) const {
|
|
return *(lhs.first) == *(rhs.first) && *(lhs.second) == *(rhs.second);
|
|
}
|
|
};
|
|
|
|
struct value_equality {
|
|
bool operator()(const value & lhs, const value & rhs) const {
|
|
return !(*lhs != *rhs);
|
|
}
|
|
};
|
|
|
|
//
|
|
// primitive value types
|
|
//
|
|
|
|
struct value_int_t : public value_t {
|
|
value_int_t(int64_t v) {
|
|
val_int = v;
|
|
val_flt = static_cast<double>(v);
|
|
if (static_cast<int64_t>(val_flt) != v) {
|
|
val_flt = v < 0 ? -INFINITY : INFINITY;
|
|
}
|
|
}
|
|
virtual std::string type() const override { return "Integer"; }
|
|
virtual int64_t as_int() const override { return val_int; }
|
|
virtual double as_float() const override { return val_flt; }
|
|
virtual string as_string() const override { return std::to_string(val_int); }
|
|
virtual bool as_bool() const override {
|
|
return val_int != 0;
|
|
}
|
|
virtual const func_builtins & get_builtins() const override;
|
|
virtual bool is_numeric() const override { return true; }
|
|
virtual bool is_hashable() const override { return true; }
|
|
virtual hasher unique_hash() const noexcept override {
|
|
return hasher(typeid(*this))
|
|
.update(&val_int, sizeof(val_int))
|
|
.update(&val_flt, sizeof(val_flt));
|
|
}
|
|
protected:
|
|
virtual bool equivalent(const value_t & other) const override {
|
|
return other.is_numeric() && val_int == other.val_int && val_flt == other.val_flt;
|
|
}
|
|
virtual bool nonequal(const value_t & other) const override {
|
|
return !(typeid(*this) == typeid(other) && val_int == other.val_int);
|
|
}
|
|
};
|
|
using value_int = std::shared_ptr<value_int_t>;
|
|
|
|
|
|
struct value_float_t : public value_t {
|
|
value val;
|
|
value_float_t(double v) {
|
|
val_flt = v;
|
|
val_int = std::isfinite(v) ? static_cast<int64_t>(v) : 0;
|
|
val = mk_val<value_int>(val_int);
|
|
}
|
|
virtual std::string type() const override { return "Float"; }
|
|
virtual double as_float() const override { return val_flt; }
|
|
virtual int64_t as_int() const override { return val_int; }
|
|
virtual string as_string() const override {
|
|
std::string out = std::to_string(val_flt);
|
|
out.erase(out.find_last_not_of('0') + 1, std::string::npos); // remove trailing zeros
|
|
if (out.back() == '.') out.push_back('0'); // leave one zero if no decimals
|
|
return out;
|
|
}
|
|
virtual bool as_bool() const override {
|
|
return val_flt != 0.0;
|
|
}
|
|
virtual const func_builtins & get_builtins() const override;
|
|
virtual bool is_numeric() const override { return true; }
|
|
virtual bool is_hashable() const override { return true; }
|
|
virtual hasher unique_hash() const noexcept override {
|
|
if (static_cast<double>(val_int) == val_flt) {
|
|
return val->unique_hash();
|
|
} else {
|
|
return hasher(typeid(*this))
|
|
.update(&val_int, sizeof(val_int))
|
|
.update(&val_flt, sizeof(val_flt));
|
|
}
|
|
}
|
|
protected:
|
|
virtual bool equivalent(const value_t & other) const override {
|
|
return other.is_numeric() && val_int == other.val_int && val_flt == other.val_flt;
|
|
}
|
|
virtual bool nonequal(const value_t & other) const override {
|
|
return !(typeid(*this) == typeid(other) && val_flt == other.val_flt);
|
|
}
|
|
};
|
|
using value_float = std::shared_ptr<value_float_t>;
|
|
|
|
|
|
struct value_string_t : public value_t {
|
|
value_string_t() { val_str = string(); }
|
|
value_string_t(const std::string & v) { val_str = string(v); }
|
|
value_string_t(std::string && v) { val_str = string(std::move(v)); }
|
|
value_string_t(const string & v) { val_str = v; }
|
|
value_string_t(string && v) { val_str = std::move(v); }
|
|
virtual std::string type() const override { return "String"; }
|
|
virtual string as_string() const override { return val_str; }
|
|
virtual std::string as_repr() const override {
|
|
std::ostringstream ss;
|
|
for (const auto & part : val_str.parts) {
|
|
ss << (part.is_input ? "INPUT: " : "TMPL: ") << part.val << "\n";
|
|
}
|
|
return ss.str();
|
|
}
|
|
virtual bool as_bool() const override {
|
|
return val_str.length() > 0;
|
|
}
|
|
virtual const func_builtins & get_builtins() const override;
|
|
virtual bool is_hashable() const override { return true; }
|
|
virtual hasher unique_hash() const noexcept override {
|
|
const auto type_hash = typeid(*this).hash_code();
|
|
auto hash = hasher();
|
|
hash.update(&type_hash, sizeof(type_hash));
|
|
val_str.hash_update(hash);
|
|
return hash;
|
|
}
|
|
void mark_input() {
|
|
val_str.mark_input();
|
|
}
|
|
protected:
|
|
virtual bool equivalent(const value_t & other) const override {
|
|
return typeid(*this) == typeid(other) && val_str.equals(other.val_str);
|
|
}
|
|
};
|
|
using value_string = std::shared_ptr<value_string_t>;
|
|
|
|
|
|
struct value_bool_t : public value_t {
|
|
value val;
|
|
value_bool_t(bool v) {
|
|
val_int = static_cast<int64_t>(v);
|
|
val_flt = static_cast<double>(v);
|
|
val = mk_val<value_int>(val_int);
|
|
}
|
|
virtual std::string type() const override { return "Boolean"; }
|
|
virtual int64_t as_int() const override { return val_int; }
|
|
virtual bool as_bool() const override { return val_int; }
|
|
virtual string as_string() const override { return std::string(val_int ? "True" : "False"); }
|
|
virtual const func_builtins & get_builtins() const override;
|
|
virtual bool is_numeric() const override { return true; }
|
|
virtual bool is_hashable() const override { return true; }
|
|
virtual hasher unique_hash() const noexcept override {
|
|
return val->unique_hash();
|
|
}
|
|
protected:
|
|
virtual bool equivalent(const value_t & other) const override {
|
|
return other.is_numeric() && val_int == other.val_int && val_flt == other.val_flt;
|
|
}
|
|
virtual bool nonequal(const value_t & other) const override {
|
|
return !(typeid(*this) == typeid(other) && val_int == other.val_int);
|
|
}
|
|
};
|
|
using value_bool = std::shared_ptr<value_bool_t>;
|
|
|
|
|
|
struct value_array_t : public value_t {
|
|
value_array_t() = default;
|
|
value_array_t(value & v) {
|
|
val_arr = v->val_arr;
|
|
}
|
|
value_array_t(std::vector<value> && arr) {
|
|
val_arr = arr;
|
|
}
|
|
value_array_t(const std::vector<value> & arr) {
|
|
val_arr = arr;
|
|
}
|
|
void reverse() {
|
|
if (is_immutable()) {
|
|
throw std::runtime_error("Attempting to modify immutable type");
|
|
}
|
|
std::reverse(val_arr.begin(), val_arr.end());
|
|
}
|
|
void push_back(const value & val) {
|
|
if (is_immutable()) {
|
|
throw std::runtime_error("Attempting to modify immutable type");
|
|
}
|
|
val_arr.push_back(val);
|
|
}
|
|
void push_back(value && val) {
|
|
if (is_immutable()) {
|
|
throw std::runtime_error("Attempting to modify immutable type");
|
|
}
|
|
val_arr.push_back(std::move(val));
|
|
}
|
|
value pop_at(int64_t index) {
|
|
if (is_immutable()) {
|
|
throw std::runtime_error("Attempting to modify immutable type");
|
|
}
|
|
if (index < 0) {
|
|
index = static_cast<int64_t>(val_arr.size()) + index;
|
|
}
|
|
if (index < 0 || index >= static_cast<int64_t>(val_arr.size())) {
|
|
throw std::runtime_error("Index " + std::to_string(index) + " out of bounds for array of size " + std::to_string(val_arr.size()));
|
|
}
|
|
value val = val_arr.at(static_cast<size_t>(index));
|
|
val_arr.erase(val_arr.begin() + index);
|
|
return val;
|
|
}
|
|
virtual std::string type() const override { return "Array"; }
|
|
virtual bool is_immutable() const override { return false; }
|
|
virtual const std::vector<value> & as_array() const override { return val_arr; }
|
|
virtual string as_string() const override {
|
|
const bool immutable = is_immutable();
|
|
std::ostringstream ss;
|
|
ss << (immutable ? "(" : "[");
|
|
for (size_t i = 0; i < val_arr.size(); i++) {
|
|
if (i > 0) ss << ", ";
|
|
value val = val_arr.at(i);
|
|
ss << value_to_string_repr(val);
|
|
}
|
|
if (immutable && val_arr.size() == 1) {
|
|
ss << ",";
|
|
}
|
|
ss << (immutable ? ")" : "]");
|
|
return ss.str();
|
|
}
|
|
virtual bool as_bool() const override {
|
|
return !val_arr.empty();
|
|
}
|
|
virtual value & at(int64_t index, value & default_val) override {
|
|
if (index < 0) {
|
|
index += val_arr.size();
|
|
}
|
|
if (index < 0 || static_cast<size_t>(index) >= val_arr.size()) {
|
|
return default_val;
|
|
}
|
|
return val_arr[index];
|
|
}
|
|
virtual value & at(int64_t index) override {
|
|
if (index < 0) {
|
|
index += val_arr.size();
|
|
}
|
|
if (index < 0 || static_cast<size_t>(index) >= val_arr.size()) {
|
|
throw std::runtime_error("Index " + std::to_string(index) + " out of bounds for array of size " + std::to_string(val_arr.size()));
|
|
}
|
|
return val_arr[index];
|
|
}
|
|
virtual const func_builtins & get_builtins() const override;
|
|
virtual bool is_hashable() const override {
|
|
if (std::all_of(val_arr.begin(), val_arr.end(), [&](auto & val) -> bool {
|
|
return val->is_immutable() && val->is_hashable();
|
|
})) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
virtual hasher unique_hash() const noexcept override {
|
|
auto hash = hasher(typeid(*this));
|
|
for (const auto & val : val_arr) {
|
|
// must use digest to prevent problems from "concatenation" property of hasher
|
|
// for ex. hash of [ "ab", "c" ] should be different from [ "a", "bc" ]
|
|
const size_t val_hash = val->unique_hash().digest();
|
|
hash.update(&val_hash, sizeof(size_t));
|
|
}
|
|
return hash;
|
|
}
|
|
protected:
|
|
virtual bool equivalent(const value_t & other) const override {
|
|
return typeid(*this) == typeid(other) && is_hashable() && other.is_hashable() && std::equal(val_arr.begin(), val_arr.end(), other.val_arr.begin(), other.val_arr.end(), value_equivalence());
|
|
}
|
|
};
|
|
using value_array = std::shared_ptr<value_array_t>;
|
|
|
|
/* A block statement's results, carried up to the gather that actually needs text. If and for
|
|
* statements used to gather their bodies into a fresh string at every nesting level, copying
|
|
* every shared part's bytes each time; as a carrier, the parts are only flattened once -- by
|
|
* the top-level render, or by exec_statements where a real string value is required (a set
|
|
* block, a macro body). It is an array for storage only: the gathers recurse into it where a
|
|
* plain array value would print its repr, and it never reaches template expressions. */
|
|
struct value_stmt_results_t : public value_array_t {};
|
|
using value_stmt_results = std::shared_ptr<value_stmt_results_t>;
|
|
|
|
|
|
struct value_tuple_t : public value_array_t {
|
|
value_tuple_t(value & v) {
|
|
val_arr = v->val_arr;
|
|
}
|
|
value_tuple_t(std::vector<value> && arr) {
|
|
val_arr = arr;
|
|
}
|
|
value_tuple_t(const std::vector<value> & arr) {
|
|
val_arr = arr;
|
|
}
|
|
value_tuple_t(const std::pair<value, value> & pair) {
|
|
val_arr.push_back(pair.first);
|
|
val_arr.push_back(pair.second);
|
|
}
|
|
virtual std::string type() const override { return "Tuple"; }
|
|
virtual bool is_immutable() const override { return true; }
|
|
};
|
|
using value_tuple = std::shared_ptr<value_tuple_t>;
|
|
|
|
|
|
struct value_object_t : public value_t {
|
|
std::unordered_map<value, value, value_hasher, value_equivalence> unordered;
|
|
/* The hash index over val_obj, built only once an object outgrows a linear scan. The
|
|
* objects a chat render builds by the thousand -- messages, calls, scopes -- hold a
|
|
* handful of keys each, and for those the map's bucket array and per-key nodes cost more
|
|
* than every lookup they would ever serve. val_obj stays the ordered authority either way. */
|
|
bool unordered_built_ = false;
|
|
static constexpr size_t kHashIndexThreshold = 32;
|
|
void build_unordered() {
|
|
if (unordered_built_) {
|
|
return;
|
|
}
|
|
unordered.reserve(val_obj.size() * 2);
|
|
for (const auto & pair : val_obj) {
|
|
unordered.emplace(pair.first, pair.second);
|
|
}
|
|
unordered_built_ = true;
|
|
}
|
|
void maybe_build_unordered() {
|
|
if (val_obj.size() > kHashIndexThreshold) {
|
|
build_unordered();
|
|
}
|
|
}
|
|
value * find_key_linear(const value & key) {
|
|
for (auto & pair : val_obj) {
|
|
if (*(pair.first) == *key) {
|
|
return &pair.second;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
bool has_builtins = true; // context and loop objects do not have builtins
|
|
value_object_t() = default;
|
|
value_object_t(value & v) {
|
|
val_obj = v->val_obj;
|
|
maybe_build_unordered();
|
|
}
|
|
value_object_t(const std::map<value, value> & obj) {
|
|
for (const auto & pair : obj) {
|
|
insert(pair.first, pair.second);
|
|
}
|
|
}
|
|
value_object_t(const std::vector<std::pair<value, value>> & obj) {
|
|
for (const auto & pair : obj) {
|
|
insert(pair.first, pair.second);
|
|
}
|
|
}
|
|
void insert(const std::string & key, const value & val) {
|
|
// The common case is re-binding a name that is already present (a loop variable, a
|
|
// scope copy): find it by name first so no key value is allocated for the lookup.
|
|
if (is_immutable()) {
|
|
throw std::runtime_error("Attempting to modify immutable type");
|
|
}
|
|
for (auto & pair : val_obj) {
|
|
const auto * ks = dynamic_cast<const value_string_t *>(pair.first.get());
|
|
if (ks && ks->val_str.parts.size() == 1 && ks->val_str.parts[0].val == key) {
|
|
pair.second = val;
|
|
if (unordered_built_) {
|
|
unordered[pair.first] = val;
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
insert(mk_val<value_string>(key), val);
|
|
}
|
|
/* Take another object's entries wholesale (a scope inheriting its parent's variables):
|
|
* the ordered list copies in one allocation instead of a key allocation and a scan per
|
|
* variable. The hash index, if the size calls for one, is rebuilt from the copy. */
|
|
void copy_entries_from(const value_object_t & other) {
|
|
val_obj = other.val_obj;
|
|
unordered.clear();
|
|
unordered_built_ = false;
|
|
maybe_build_unordered();
|
|
}
|
|
virtual std::string type() const override { return "Object"; }
|
|
virtual bool is_immutable() const override { return false; }
|
|
virtual const std::vector<std::pair<value, value>> & as_ordered_object() const override { return val_obj; }
|
|
virtual string as_string() const override {
|
|
std::ostringstream ss;
|
|
ss << "{";
|
|
for (size_t i = 0; i < val_obj.size(); i++) {
|
|
if (i > 0) ss << ", ";
|
|
auto & [key, val] = val_obj.at(i);
|
|
ss << value_to_string_repr(key) << ": " << value_to_string_repr(val);
|
|
}
|
|
ss << "}";
|
|
return ss.str();
|
|
}
|
|
virtual bool as_bool() const override {
|
|
return !val_obj.empty();
|
|
}
|
|
virtual bool has_key(const value & key) override {
|
|
if (!key->is_immutable() || !key->is_hashable()) {
|
|
throw std::runtime_error("Object key of unhashable type: " + key->type());
|
|
}
|
|
if (unordered_built_) {
|
|
return unordered.find(key) != unordered.end();
|
|
}
|
|
return find_key_linear(key) != nullptr;
|
|
}
|
|
virtual void insert(const value & key, const value & val) override {
|
|
if (is_immutable()) {
|
|
throw std::runtime_error("Attempting to modify immutable type");
|
|
}
|
|
if (!key->is_immutable() || !key->is_hashable()) {
|
|
throw std::runtime_error("Object key of unhashable type: " + key->type());
|
|
}
|
|
if (unordered_built_) {
|
|
auto it = unordered.find(key);
|
|
if (it != unordered.end()) {
|
|
// if key exists, replace value in ordered list instead of appending
|
|
it->second = val;
|
|
for (auto & pair : val_obj) {
|
|
if (*(pair.first) == *key) {
|
|
pair.second = val;
|
|
break;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
unordered.emplace(key, val);
|
|
val_obj.push_back({key, val});
|
|
return;
|
|
}
|
|
if (value * existing = find_key_linear(key)) {
|
|
*existing = val;
|
|
return;
|
|
}
|
|
val_obj.push_back({key, val});
|
|
maybe_build_unordered();
|
|
}
|
|
/* Insert a key the caller knows is absent (a fresh object's fields): no lookup first. */
|
|
void insert_new(const value & key, const value & val) {
|
|
if (is_immutable()) {
|
|
throw std::runtime_error("Attempting to modify immutable type");
|
|
}
|
|
if (unordered_built_) {
|
|
unordered.emplace(key, val);
|
|
}
|
|
val_obj.push_back({key, val});
|
|
maybe_build_unordered();
|
|
}
|
|
void insert_new(const std::string & key, const value & val) {
|
|
insert_new(mk_val<value_string>(key), val);
|
|
}
|
|
virtual value & at(const value & key, value & default_val) override {
|
|
if (!key->is_immutable() || !key->is_hashable()) {
|
|
throw std::runtime_error("Object key of unhashable type: " + key->type());
|
|
}
|
|
if (unordered_built_) {
|
|
auto it = unordered.find(key);
|
|
return it == unordered.end() ? default_val : it->second;
|
|
}
|
|
if (value * v = find_key_linear(key)) {
|
|
return *v;
|
|
}
|
|
return default_val;
|
|
}
|
|
virtual value & at(const value & key) override {
|
|
if (!key->is_immutable() || !key->is_hashable()) {
|
|
throw std::runtime_error("Object key of unhashable type: " + key->type());
|
|
}
|
|
if (unordered_built_) {
|
|
auto it = unordered.find(key);
|
|
if (it == unordered.end()) {
|
|
throw std::runtime_error("Key '" + key->as_string().str() + "' not found in value of type " + type());
|
|
}
|
|
return it->second;
|
|
}
|
|
if (value * v = find_key_linear(key)) {
|
|
return *v;
|
|
}
|
|
throw std::runtime_error("Key '" + key->as_string().str() + "' not found in value of type " + type());
|
|
}
|
|
/* A string key by value, without allocating a key value and hashing it: objects here are
|
|
* message/argument records of a few keys, so the ordered scan is the cheaper lookup. */
|
|
value * find_string_key(const std::string & key) {
|
|
for (auto & pair : val_obj) {
|
|
const auto * ks = dynamic_cast<const value_string_t *>(pair.first.get());
|
|
if (!ks) {
|
|
continue;
|
|
}
|
|
size_t off = 0;
|
|
bool eq = true;
|
|
for (const auto & part : ks->val_str.parts) {
|
|
if (key.compare(off, part.val.size(), part.val) != 0) {
|
|
eq = false;
|
|
break;
|
|
}
|
|
off += part.val.size();
|
|
}
|
|
if (eq && off == key.size()) {
|
|
return &pair.second;
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
virtual value & at(const std::string & key, value & default_val) override {
|
|
if (value * v = find_string_key(key)) {
|
|
return *v;
|
|
}
|
|
return default_val;
|
|
}
|
|
virtual value & at(const std::string & key) override {
|
|
if (value * v = find_string_key(key)) {
|
|
return *v;
|
|
}
|
|
throw std::runtime_error("Key '" + key + "' not found in value of type " + type());
|
|
}
|
|
virtual const func_builtins & get_builtins() const override;
|
|
virtual bool is_hashable() const override {
|
|
if (std::all_of(val_obj.begin(), val_obj.end(), [&](auto & pair) -> bool {
|
|
const auto & val = pair.second;
|
|
return val->is_immutable() && val->is_hashable();
|
|
})) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
virtual hasher unique_hash() const noexcept override {
|
|
auto hash = hasher(typeid(*this));
|
|
for (const auto & [key, val] : val_obj) {
|
|
// must use digest to prevent problems from "concatenation" property of hasher
|
|
// for ex. hash of key="ab", value="c" should be different from key="a", value="bc"
|
|
const size_t key_hash = key->unique_hash().digest();
|
|
const size_t val_hash = val->unique_hash().digest();
|
|
hash.update(&key_hash, sizeof(key_hash));
|
|
hash.update(&val_hash, sizeof(val_hash));
|
|
}
|
|
return hash;
|
|
}
|
|
protected:
|
|
virtual bool equivalent(const value_t & other) const override {
|
|
return typeid(*this) == typeid(other) && is_hashable() && other.is_hashable() && std::equal(val_obj.begin(), val_obj.end(), other.val_obj.begin(), other.val_obj.end(), value_equivalence());
|
|
}
|
|
};
|
|
using value_object = std::shared_ptr<value_object_t>;
|
|
|
|
//
|
|
// none and undefined types
|
|
//
|
|
|
|
struct value_none_t : public value_t {
|
|
virtual std::string type() const override { return "None"; }
|
|
virtual bool is_none() const override { return true; }
|
|
virtual bool as_bool() const override { return false; }
|
|
virtual string as_string() const override { return string(type()); }
|
|
virtual std::string as_repr() const override { return type(); }
|
|
virtual const func_builtins & get_builtins() const override;
|
|
virtual bool is_hashable() const override { return true; }
|
|
virtual hasher unique_hash() const noexcept override {
|
|
return hasher(typeid(*this));
|
|
}
|
|
protected:
|
|
virtual bool equivalent(const value_t & other) const override {
|
|
return typeid(*this) == typeid(other);
|
|
}
|
|
};
|
|
using value_none = std::shared_ptr<value_none_t>;
|
|
|
|
struct value_undefined_t : public value_t {
|
|
std::string hint; // for debugging, to indicate where undefined came from
|
|
value_undefined_t(const std::string & h = "") : hint(h) {}
|
|
virtual std::string type() const override { return hint.empty() ? "Undefined" : "Undefined (hint: '" + hint + "')"; }
|
|
virtual bool is_undefined() const override { return true; }
|
|
virtual bool as_bool() const override { return false; }
|
|
virtual std::string as_repr() const override { return type(); }
|
|
virtual const func_builtins & get_builtins() const override;
|
|
virtual hasher unique_hash() const noexcept override {
|
|
return hasher(typeid(*this));
|
|
}
|
|
protected:
|
|
virtual bool equivalent(const value_t & other) const override {
|
|
return is_undefined() == other.is_undefined();
|
|
}
|
|
};
|
|
using value_undefined = std::shared_ptr<value_undefined_t>;
|
|
|
|
//
|
|
// function type
|
|
//
|
|
|
|
struct func_args {
|
|
public:
|
|
std::string func_name; // for error messages
|
|
context & ctx;
|
|
func_args(context & ctx) : ctx(ctx) {}
|
|
value get_kwarg(const std::string & key, value default_val) const;
|
|
value get_kwarg_or_pos(const std::string & key, size_t pos) const;
|
|
value get_pos(size_t pos) const;
|
|
value get_pos(size_t pos, value default_val) const;
|
|
const std::vector<value> & get_args() const;
|
|
size_t count() const { return args.size(); }
|
|
void push_back(const value & val);
|
|
void push_front(const value & val);
|
|
void ensure_count(size_t min, size_t max = 999) const {
|
|
size_t n = args.size();
|
|
if (n < min || n > max) {
|
|
throw std::runtime_error("Function '" + func_name + "' expected between " + std::to_string(min) + " and " + std::to_string(max) + " arguments, got " + std::to_string(n));
|
|
}
|
|
}
|
|
template<typename T> void ensure_val(const value & ptr) const {
|
|
if (!is_val<T>(ptr)) {
|
|
throw std::runtime_error("Function '" + func_name + "' expected value of type " + std::string(typeid(T).name()) + ", got " + ptr->type());
|
|
}
|
|
}
|
|
void ensure_count(bool require0, bool require1, bool require2, bool require3) const {
|
|
static auto bool_to_int = [](bool b) { return b ? 1 : 0; };
|
|
size_t required = bool_to_int(require0) + bool_to_int(require1) + bool_to_int(require2) + bool_to_int(require3);
|
|
ensure_count(required);
|
|
}
|
|
template<typename T0> void ensure_vals(bool required0 = true) const {
|
|
ensure_count(required0, false, false, false);
|
|
if (required0 && args.size() > 0) ensure_val<T0>(args[0]);
|
|
}
|
|
template<typename T0, typename T1> void ensure_vals(bool required0 = true, bool required1 = true) const {
|
|
ensure_count(required0, required1, false, false);
|
|
if (required0 && args.size() > 0) ensure_val<T0>(args[0]);
|
|
if (required1 && args.size() > 1) ensure_val<T1>(args[1]);
|
|
}
|
|
template<typename T0, typename T1, typename T2> void ensure_vals(bool required0 = true, bool required1 = true, bool required2 = true) const {
|
|
ensure_count(required0, required1, required2, false);
|
|
if (required0 && args.size() > 0) ensure_val<T0>(args[0]);
|
|
if (required1 && args.size() > 1) ensure_val<T1>(args[1]);
|
|
if (required2 && args.size() > 2) ensure_val<T2>(args[2]);
|
|
}
|
|
template<typename T0, typename T1, typename T2, typename T3> void ensure_vals(bool required0 = true, bool required1 = true, bool required2 = true, bool required3 = true) const {
|
|
ensure_count(required0, required1, required2, required3);
|
|
if (required0 && args.size() > 0) ensure_val<T0>(args[0]);
|
|
if (required1 && args.size() > 1) ensure_val<T1>(args[1]);
|
|
if (required2 && args.size() > 2) ensure_val<T2>(args[2]);
|
|
if (required3 && args.size() > 3) ensure_val<T3>(args[3]);
|
|
}
|
|
private:
|
|
std::vector<value> args;
|
|
};
|
|
|
|
struct value_func_t : public value_t {
|
|
std::string name;
|
|
value arg0; // bound "this" argument, if any
|
|
value_func_t(const std::string & name, const func_handler & func) : name(name) {
|
|
val_func = func;
|
|
}
|
|
value_func_t(const std::string & name, const func_handler & func, const value & arg_this) : name(name), arg0(arg_this) {
|
|
val_func = func;
|
|
}
|
|
virtual value invoke(const func_args & args) const override {
|
|
func_args new_args(args); // copy
|
|
new_args.func_name = name;
|
|
if (arg0) {
|
|
new_args.push_front(arg0);
|
|
}
|
|
return val_func(new_args);
|
|
}
|
|
virtual std::string type() const override { return "Function"; }
|
|
virtual std::string as_repr() const override { return type() + "<" + name + ">(" + (arg0 ? arg0->as_repr() : "") + ")"; }
|
|
virtual bool is_hashable() const override { return false; }
|
|
virtual hasher unique_hash() const noexcept override {
|
|
// Note: this is unused for now, we don't support function as object keys
|
|
// use function pointer as unique identifier
|
|
const auto target = val_func.target<func_hptr>();
|
|
return hasher(typeid(*this)).update(&target, sizeof(target));
|
|
}
|
|
protected:
|
|
virtual bool equivalent(const value_t & other) const override {
|
|
// Note: this is unused for now, we don't support function as object keys
|
|
// compare function pointers
|
|
// (val_func == other.val_func does not work as std::function::operator== is only used for nullptr check)
|
|
const auto target_this = this->val_func.target<func_hptr>();
|
|
const auto target_other = other.val_func.target<func_hptr>();
|
|
return typeid(*this) == typeid(other) && target_this == target_other;
|
|
}
|
|
};
|
|
using value_func = std::shared_ptr<value_func_t>;
|
|
|
|
// special value for kwarg
|
|
struct value_kwarg_t : public value_t {
|
|
std::string key;
|
|
value val;
|
|
value_kwarg_t(const std::string & k, const value & v) : key(k), val(v) {}
|
|
virtual std::string type() const override { return "KwArg"; }
|
|
virtual std::string as_repr() const override { return type(); }
|
|
virtual bool is_hashable() const override { return true; }
|
|
virtual hasher unique_hash() const noexcept override {
|
|
const auto type_hash = typeid(*this).hash_code();
|
|
auto hash = val->unique_hash();
|
|
hash.update(&type_hash, sizeof(type_hash))
|
|
.update(key.data(), key.size());
|
|
return hash;
|
|
}
|
|
protected:
|
|
virtual bool equivalent(const value_t & other) const override {
|
|
const value_kwarg_t & other_val = static_cast<const value_kwarg_t &>(other);
|
|
return typeid(*this) == typeid(other) && key == other_val.key && val == other_val.val;
|
|
}
|
|
};
|
|
using value_kwarg = std::shared_ptr<value_kwarg_t>;
|
|
|
|
|
|
} // namespace jinja
|