@@ -460,11 +460,16 @@ class Interpreter {
460
460
struct ImportCacheValue {
461
461
std::string foundHere;
462
462
std::string content;
463
+ /* * Parsed desugared expression.
464
+ *
465
+ * Null if this file was only ever successfully imported with importstr.
466
+ */
467
+ AST *expr;
463
468
};
464
469
465
470
/* * Cache for imported Jsonnet files. */
466
471
std::map<std::pair<std::string, String>,
467
- const ImportCacheValue *> cachedImports;
472
+ ImportCacheValue *> cachedImports;
468
473
469
474
/* * External variables for std.extVar. */
470
475
ExtMap externalVars;
@@ -688,12 +693,16 @@ class Interpreter {
688
693
*/
689
694
AST *import (const LocationRange &loc, const LiteralString *file)
690
695
{
691
- const ImportCacheValue *input = importString (loc, file);
692
- Tokens tokens = jsonnet_lex (input->foundHere , input->content .c_str ());
693
- AST *expr = jsonnet_parse (alloc, tokens);
694
- jsonnet_desugar (alloc, expr, nullptr );
695
- jsonnet_static_analysis (expr);
696
- return expr;
696
+ ImportCacheValue *input = importString (loc, file);
697
+ if (input->expr == nullptr ) {
698
+ Tokens tokens = jsonnet_lex (input->foundHere , input->content .c_str ());
699
+ AST *expr = jsonnet_parse (alloc, tokens);
700
+ jsonnet_desugar (alloc, expr, nullptr );
701
+ jsonnet_static_analysis (expr);
702
+ // If no errors then populate cache.
703
+ input->expr = expr;
704
+ }
705
+ return input->expr ;
697
706
}
698
707
699
708
/* * Import a file as a string.
@@ -705,14 +714,14 @@ class Interpreter {
705
714
* \param file Path to the filename.
706
715
* \param found_here If non-null, used to store the actual path of the file
707
716
*/
708
- const ImportCacheValue *importString (const LocationRange &loc, const LiteralString *file)
717
+ ImportCacheValue *importString (const LocationRange &loc, const LiteralString *file)
709
718
{
710
719
std::string dir = dir_name (loc.file );
711
720
712
721
const String &path = file->value ;
713
722
714
723
std::pair<std::string, String> key (dir, path);
715
- const ImportCacheValue *cached_value = cachedImports[key];
724
+ ImportCacheValue *cached_value = cachedImports[key];
716
725
if (cached_value != nullptr )
717
726
return cached_value;
718
727
@@ -734,6 +743,7 @@ class Interpreter {
734
743
auto *input_ptr = new ImportCacheValue ();
735
744
input_ptr->foundHere = found_here_cptr;
736
745
input_ptr->content = input;
746
+ input_ptr->expr = nullptr ; // May be filled in later by import().
737
747
::free (found_here_cptr);
738
748
cachedImports[key] = input_ptr;
739
749
return input_ptr;
0 commit comments