Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC for #196 - Relax computed importstr #805

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/desugarer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct BuiltinDecl {
std::vector<UString> params;
};

static unsigned long max_builtin = 37;
static unsigned long max_builtin = 38;
BuiltinDecl jsonnet_builtin_decl(unsigned long builtin)
{
switch (builtin) {
Expand Down Expand Up @@ -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();
Expand Down
23 changes: 18 additions & 5 deletions core/vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<std::string, UString> key(dir, path);
ImportCacheValue *cached_value = cachedImports[key];
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -1191,6 +1195,15 @@ class Interpreter {
return nullptr;
}

const AST *builtinImportstrDyn(const LocationRange &loc, const std::vector<Value> &args)
{
validateBuiltinArgs(loc, "importstrDyn", args, {Value::STRING});
const auto *str = static_cast<const HeapString *>(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<Value> &args)
{
validateBuiltinArgs(loc, "exp", args, {Value::NUMBER});
Expand Down Expand Up @@ -1921,7 +1934,7 @@ class Interpreter {

case AST_IMPORTSTR: {
const auto &ast = *static_cast<const Importstr *>(ast_);
const ImportCacheValue *value = importString(ast.location, ast.file);
const ImportCacheValue *value = importLiteralString(ast.location, ast.file);
scratch = makeString(decode_utf8(value->content));
} break;

Expand Down