From 6e719ec2ea8cb03937680219cbf06e2ee4f4aae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Barbero?= Date: Fri, 8 May 2020 14:31:00 +0200 Subject: [PATCH] POC for #196 - Relax computed importstr Adds a new builtin function (importstrDyn) that accept a computed string. I did not modified the importstr logic itself as I don't understand well enough how the parser is working. --- core/desugarer.cpp | 3 ++- core/vm.cpp | 23 ++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/core/desugarer.cpp b/core/desugarer.cpp index 70b40888b..64ac1cafb 100644 --- a/core/desugarer.cpp +++ b/core/desugarer.cpp @@ -34,7 +34,7 @@ struct BuiltinDecl { std::vector params; }; -static unsigned long max_builtin = 37; +static unsigned long max_builtin = 38; BuiltinDecl jsonnet_builtin_decl(unsigned long builtin) { switch (builtin) { @@ -76,6 +76,7 @@ BuiltinDecl jsonnet_builtin_decl(unsigned long builtin) case 35: return {U"parseJson", {U"str"}}; case 36: return {U"encodeUTF8", {U"str"}}; case 37: return {U"decodeUTF8", {U"arr"}}; + case 38: return {U"importstrDyn", {U"str"}}; default: std::cerr << "INTERNAL ERROR: Unrecognized builtin function: " << builtin << std::endl; std::abort(); diff --git a/core/vm.cpp b/core/vm.cpp index 539d4e4ac..f4f34375e 100644 --- a/core/vm.cpp +++ b/core/vm.cpp @@ -734,7 +734,7 @@ class Interpreter { */ HeapThunk *import(const LocationRange &loc, const LiteralString *file) { - ImportCacheValue *input = importString(loc, file); + ImportCacheValue *input = importLiteralString(loc, file); if (input->thunk == nullptr) { Tokens tokens = jsonnet_lex(input->foundHere, input->content.c_str()); AST *expr = jsonnet_parse(alloc, tokens); @@ -756,11 +756,14 @@ class Interpreter { * \param file Path to the filename. * \param found_here If non-null, used to store the actual path of the file */ - ImportCacheValue *importString(const LocationRange &loc, const LiteralString *file) + ImportCacheValue *importLiteralString(const LocationRange &loc, const LiteralString *file) { - std::string dir = dir_name(loc.file); + return importString(loc, file->value); + } - const UString &path = file->value; + ImportCacheValue *importString(const LocationRange &loc, const UString &path) + { + std::string dir = dir_name(loc.file); std::pair key(dir, path); ImportCacheValue *cached_value = cachedImports[key]; @@ -881,6 +884,7 @@ class Interpreter { builtins["parseJson"] = &Interpreter::builtinParseJson; builtins["encodeUTF8"] = &Interpreter::builtinEncodeUTF8; builtins["decodeUTF8"] = &Interpreter::builtinDecodeUTF8; + builtins["importstrDyn"] = &Interpreter::builtinImportstrDyn; } /** Clean up the heap, stack, stash, and builtin function ASTs. */ @@ -1191,6 +1195,15 @@ class Interpreter { return nullptr; } + const AST *builtinImportstrDyn(const LocationRange &loc, const std::vector &args) + { + validateBuiltinArgs(loc, "importstrDyn", args, {Value::STRING}); + const auto *str = static_cast(args[0].v.h); + const ImportCacheValue *importedValue = importString(loc, str->value); + scratch = makeString(decode_utf8(importedValue->content)); + return nullptr; + } + const AST *builtinExp(const LocationRange &loc, const std::vector &args) { validateBuiltinArgs(loc, "exp", args, {Value::NUMBER}); @@ -1921,7 +1934,7 @@ class Interpreter { case AST_IMPORTSTR: { const auto &ast = *static_cast(ast_); - const ImportCacheValue *value = importString(ast.location, ast.file); + const ImportCacheValue *value = importLiteralString(ast.location, ast.file); scratch = makeString(decode_utf8(value->content)); } break;