d782d463cb
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
62 lines
2.3 KiB
C++
62 lines
2.3 KiB
C++
// Lua 5.4 wrapper para formulas de columnas custom del playground tables.
|
|
//
|
|
// Features:
|
|
// - Sandbox medio: io/require/dofile fuera; os reducido a date/time/diff/clock.
|
|
// - Builtins fn.* (~20 funciones).
|
|
// - Sintaxis [col_name] preprocesada a row["col_name"].
|
|
// - Auto-`return` si la formula es expresion suelta sin keyword inicial.
|
|
// - Type-aware push: row.x devuelve number si la col es Int/Float, boolean
|
|
// si Bool, string en el resto (Date/String/Json). Nil si vacia.
|
|
// - UTF-8 ok en nombres de columnas dentro de [].
|
|
// - Comentarios y string literals preservados por el preprocesador.
|
|
// - Llamadas recursivas: un derived col puede referenciar a otro derived col;
|
|
// ciclos cortados con nil.
|
|
#pragma once
|
|
|
|
#include "data_table_logic.h"
|
|
|
|
#include <string>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
// Forward declaration del C struct de Lua (definido en lua.h).
|
|
struct lua_State;
|
|
|
|
namespace lua_engine {
|
|
|
|
struct Engine;
|
|
|
|
Engine* get();
|
|
void shutdown();
|
|
|
|
int compile(Engine* e, const std::string& body, std::string* err_out);
|
|
void release(Engine* e, int id);
|
|
|
|
struct RowCtx {
|
|
const char* const* cells = nullptr;
|
|
int orig_cols = 0;
|
|
int row = 0;
|
|
const std::vector<std::string>* header_names = nullptr;
|
|
const std::unordered_map<std::string,int>* name_to_col = nullptr;
|
|
|
|
// Tipos declarados/auto-detect de las cols originales. nullptr -> heuristica.
|
|
const data_table::ColumnType* types_orig = nullptr;
|
|
int n_types_orig = 0;
|
|
|
|
// Derived cols + lookup por nombre (incluye retipo puro y formulas).
|
|
const std::vector<data_table::DerivedColumn>* derived = nullptr;
|
|
const std::unordered_map<std::string,int>* derived_name_to_idx = nullptr;
|
|
};
|
|
|
|
std::string eval(Engine* e, int id, const RowCtx& ctx, std::string* err_out);
|
|
|
|
// Helper expuesto para tests: preprocesa `[col]` -> `row["col"]` respetando
|
|
// strings y comentarios. Tambien aplica auto-return.
|
|
std::string preprocess(const std::string& body);
|
|
|
|
// Acceso al lua_State subyacente. Uso restringido: tql.cpp parsea chunks
|
|
// (return { ... }) y walks tablas. NO usar para nada que rompa el sandbox.
|
|
::lua_State* raw_state();
|
|
|
|
} // namespace lua_engine
|