Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / profile_resetter / jtl_interpreter.cc
blob7460cee55782a76ca933663ffc483decf23ec249
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/profile_resetter/jtl_interpreter.h"
7 #include <numeric>
9 #include "base/memory/scoped_vector.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "chrome/browser/profile_resetter/jtl_foundation.h"
13 #include "crypto/hmac.h"
14 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
15 #include "url/gurl.h"
17 namespace {
19 class ExecutionContext;
21 // An operation in an interpreted program.
22 class Operation {
23 public:
24 virtual ~Operation() {}
25 // Executes the operation on the specified context and instructs the context
26 // to continue execution with the next instruction if appropriate.
27 // Returns true if we should continue with any potential backtracking that
28 // needs to be done.
29 virtual bool Execute(ExecutionContext* context) = 0;
32 // An execution context of operations.
33 class ExecutionContext {
34 public:
35 // |input| is the root of a dictionary that stores the information the
36 // sentence is evaluated on.
37 ExecutionContext(const jtl_foundation::Hasher* hasher,
38 const std::vector<Operation*>& sentence,
39 const base::DictionaryValue* input,
40 base::DictionaryValue* working_memory)
41 : hasher_(hasher),
42 sentence_(sentence),
43 next_instruction_index_(0u),
44 working_memory_(working_memory),
45 error_(false) {
46 stack_.push_back(input);
48 ~ExecutionContext() {}
50 // Returns true in case of success.
51 bool ContinueExecution() {
52 if (error_ || stack_.empty()) {
53 error_ = true;
54 return false;
56 if (next_instruction_index_ >= sentence_.size())
57 return true;
59 Operation* op = sentence_[next_instruction_index_];
60 next_instruction_index_++;
61 bool continue_traversal = op->Execute(this);
62 next_instruction_index_--;
63 return continue_traversal;
66 std::string GetHash(const std::string& input) {
67 return hasher_->GetHash(input);
70 // Calculates the |hash| of a string, integer or double |value|, and returns
71 // true. Returns false otherwise.
72 bool GetValueHash(const base::Value& value, std::string* hash) {
73 DCHECK(hash);
74 std::string value_as_string;
75 int tmp_int = 0;
76 double tmp_double = 0.0;
77 if (value.GetAsInteger(&tmp_int))
78 value_as_string = base::IntToString(tmp_int);
79 else if (value.GetAsDouble(&tmp_double))
80 value_as_string = base::DoubleToString(tmp_double);
81 else if (!value.GetAsString(&value_as_string))
82 return false;
83 *hash = GetHash(value_as_string);
84 return true;
87 const base::Value* current_node() const { return stack_.back(); }
88 std::vector<const base::Value*>* stack() { return &stack_; }
89 base::DictionaryValue* working_memory() { return working_memory_; }
90 bool error() const { return error_; }
92 private:
93 // A hasher used to hash node names in a dictionary.
94 const jtl_foundation::Hasher* hasher_;
95 // The sentence to be executed.
96 const std::vector<Operation*> sentence_;
97 // Position in |sentence_|.
98 size_t next_instruction_index_;
99 // A stack of Values, indicating a navigation path from the root node of
100 // |input| (see constructor) to the current node on which the
101 // sentence_[next_instruction_index_] is evaluated.
102 std::vector<const base::Value*> stack_;
103 // Memory into which values can be stored by the program.
104 base::DictionaryValue* working_memory_;
105 // Whether a runtime error occurred.
106 bool error_;
107 DISALLOW_COPY_AND_ASSIGN(ExecutionContext);
110 class NavigateOperation : public Operation {
111 public:
112 explicit NavigateOperation(const std::string& hashed_key)
113 : hashed_key_(hashed_key) {}
114 virtual ~NavigateOperation() {}
115 virtual bool Execute(ExecutionContext* context) OVERRIDE {
116 const base::DictionaryValue* dict = NULL;
117 if (!context->current_node()->GetAsDictionary(&dict)) {
118 // Just ignore this node gracefully as this navigation is a dead end.
119 // If this NavigateOperation occurred after a NavigateAny operation, those
120 // may still be fulfillable, so we allow continuing the execution of the
121 // sentence on other nodes.
122 return true;
124 for (base::DictionaryValue::Iterator i(*dict); !i.IsAtEnd(); i.Advance()) {
125 if (context->GetHash(i.key()) != hashed_key_)
126 continue;
127 context->stack()->push_back(&i.value());
128 bool continue_traversal = context->ContinueExecution();
129 context->stack()->pop_back();
130 if (!continue_traversal)
131 return false;
133 return true;
136 private:
137 std::string hashed_key_;
138 DISALLOW_COPY_AND_ASSIGN(NavigateOperation);
141 class NavigateAnyOperation : public Operation {
142 public:
143 NavigateAnyOperation() {}
144 virtual ~NavigateAnyOperation() {}
145 virtual bool Execute(ExecutionContext* context) OVERRIDE {
146 const base::DictionaryValue* dict = NULL;
147 const base::ListValue* list = NULL;
148 if (context->current_node()->GetAsDictionary(&dict)) {
149 for (base::DictionaryValue::Iterator i(*dict);
150 !i.IsAtEnd(); i.Advance()) {
151 context->stack()->push_back(&i.value());
152 bool continue_traversal = context->ContinueExecution();
153 context->stack()->pop_back();
154 if (!continue_traversal)
155 return false;
157 } else if (context->current_node()->GetAsList(&list)) {
158 for (base::ListValue::const_iterator i = list->begin();
159 i != list->end(); ++i) {
160 context->stack()->push_back(*i);
161 bool continue_traversal = context->ContinueExecution();
162 context->stack()->pop_back();
163 if (!continue_traversal)
164 return false;
166 } else {
167 // Do nothing, just ignore this node.
169 return true;
172 private:
173 DISALLOW_COPY_AND_ASSIGN(NavigateAnyOperation);
176 class NavigateBackOperation : public Operation {
177 public:
178 NavigateBackOperation() {}
179 virtual ~NavigateBackOperation() {}
180 virtual bool Execute(ExecutionContext* context) OVERRIDE {
181 const base::Value* current_node = context->current_node();
182 context->stack()->pop_back();
183 bool continue_traversal = context->ContinueExecution();
184 context->stack()->push_back(current_node);
185 return continue_traversal;
188 private:
189 DISALLOW_COPY_AND_ASSIGN(NavigateBackOperation);
192 class StoreValue : public Operation {
193 public:
194 StoreValue(const std::string& hashed_name, scoped_ptr<base::Value> value)
195 : hashed_name_(hashed_name),
196 value_(value.Pass()) {
197 DCHECK(IsStringUTF8(hashed_name));
198 DCHECK(value_);
200 virtual ~StoreValue() {}
201 virtual bool Execute(ExecutionContext* context) OVERRIDE {
202 context->working_memory()->Set(hashed_name_, value_->DeepCopy());
203 return context->ContinueExecution();
206 private:
207 std::string hashed_name_;
208 scoped_ptr<base::Value> value_;
209 DISALLOW_COPY_AND_ASSIGN(StoreValue);
212 class CompareStoredValue : public Operation {
213 public:
214 CompareStoredValue(const std::string& hashed_name,
215 scoped_ptr<base::Value> value,
216 scoped_ptr<base::Value> default_value)
217 : hashed_name_(hashed_name),
218 value_(value.Pass()),
219 default_value_(default_value.Pass()) {
220 DCHECK(IsStringUTF8(hashed_name));
221 DCHECK(value_);
222 DCHECK(default_value_);
224 virtual ~CompareStoredValue() {}
225 virtual bool Execute(ExecutionContext* context) OVERRIDE {
226 const base::Value* actual_value = NULL;
227 if (!context->working_memory()->Get(hashed_name_, &actual_value))
228 actual_value = default_value_.get();
229 if (!value_->Equals(actual_value))
230 return true;
231 return context->ContinueExecution();
234 private:
235 std::string hashed_name_;
236 scoped_ptr<base::Value> value_;
237 scoped_ptr<base::Value> default_value_;
238 DISALLOW_COPY_AND_ASSIGN(CompareStoredValue);
241 template<bool ExpectedTypeIsBooleanNotHashable>
242 class StoreNodeValue : public Operation {
243 public:
244 explicit StoreNodeValue(const std::string& hashed_name)
245 : hashed_name_(hashed_name) {
246 DCHECK(IsStringUTF8(hashed_name));
248 virtual ~StoreNodeValue() {}
249 virtual bool Execute(ExecutionContext* context) OVERRIDE {
250 scoped_ptr<base::Value> value;
251 if (ExpectedTypeIsBooleanNotHashable) {
252 if (!context->current_node()->IsType(base::Value::TYPE_BOOLEAN))
253 return true;
254 value.reset(context->current_node()->DeepCopy());
255 } else {
256 std::string hash;
257 if (!context->GetValueHash(*context->current_node(), &hash))
258 return true;
259 value.reset(new base::StringValue(hash));
261 context->working_memory()->Set(hashed_name_, value.release());
262 return context->ContinueExecution();
265 private:
266 std::string hashed_name_;
267 DISALLOW_COPY_AND_ASSIGN(StoreNodeValue);
270 // Stores the effective SLD (second-level domain) of the URL represented by the
271 // current node into working memory.
272 class StoreNodeEffectiveSLD : public Operation {
273 public:
274 explicit StoreNodeEffectiveSLD(const std::string& hashed_name)
275 : hashed_name_(hashed_name) {
276 DCHECK(IsStringUTF8(hashed_name));
278 virtual ~StoreNodeEffectiveSLD() {}
279 virtual bool Execute(ExecutionContext* context) OVERRIDE {
280 std::string possibly_invalid_url;
281 std::string effective_sld;
282 if (!context->current_node()->GetAsString(&possibly_invalid_url) ||
283 !GetEffectiveSLD(possibly_invalid_url, &effective_sld))
284 return true;
285 context->working_memory()->Set(
286 hashed_name_, new base::StringValue(context->GetHash(effective_sld)));
287 return context->ContinueExecution();
290 private:
291 // If |possibly_invalid_url| is a valid URL that has an effective second-level
292 // domain part, outputs that in |effective_sld| and returns true.
293 // Returns false otherwise.
294 static bool GetEffectiveSLD(const std::string& possibly_invalid_url,
295 std::string* effective_sld) {
296 namespace domains = net::registry_controlled_domains;
297 DCHECK(effective_sld);
298 GURL url(possibly_invalid_url);
299 if (!url.is_valid())
300 return false;
301 std::string sld_and_registry = domains::GetDomainAndRegistry(
302 url.host(), domains::EXCLUDE_PRIVATE_REGISTRIES);
303 size_t registry_length = domains::GetRegistryLength(
304 url.host(),
305 domains::EXCLUDE_UNKNOWN_REGISTRIES,
306 domains::EXCLUDE_PRIVATE_REGISTRIES);
307 // Fail unless (1.) the URL has a host part; and (2.) that host part is a
308 // well-formed domain name that ends in, but is not in itself, as a whole,
309 // a recognized registry identifier that is acknowledged by ICANN.
310 if (registry_length == std::string::npos || registry_length == 0)
311 return false;
312 DCHECK_LT(registry_length, sld_and_registry.size());
313 // Subtract one to cut off the dot separating the SLD and the registry.
314 effective_sld->assign(
315 sld_and_registry, 0, sld_and_registry.size() - registry_length - 1);
316 return true;
319 std::string hashed_name_;
320 DISALLOW_COPY_AND_ASSIGN(StoreNodeEffectiveSLD);
323 class CompareNodeBool : public Operation {
324 public:
325 explicit CompareNodeBool(bool value) : value_(value) {}
326 virtual ~CompareNodeBool() {}
327 virtual bool Execute(ExecutionContext* context) OVERRIDE {
328 bool actual_value = false;
329 if (!context->current_node()->GetAsBoolean(&actual_value))
330 return true;
331 if (actual_value != value_)
332 return true;
333 return context->ContinueExecution();
336 private:
337 bool value_;
338 DISALLOW_COPY_AND_ASSIGN(CompareNodeBool);
341 class CompareNodeHash : public Operation {
342 public:
343 explicit CompareNodeHash(const std::string& hashed_value)
344 : hashed_value_(hashed_value) {}
345 virtual ~CompareNodeHash() {}
346 virtual bool Execute(ExecutionContext* context) OVERRIDE {
347 std::string actual_hash;
348 if (!context->GetValueHash(*context->current_node(), &actual_hash) ||
349 actual_hash != hashed_value_)
350 return true;
351 return context->ContinueExecution();
354 private:
355 std::string hashed_value_;
356 DISALLOW_COPY_AND_ASSIGN(CompareNodeHash);
359 class CompareNodeHashNot : public Operation {
360 public:
361 explicit CompareNodeHashNot(const std::string& hashed_value)
362 : hashed_value_(hashed_value) {}
363 virtual ~CompareNodeHashNot() {}
364 virtual bool Execute(ExecutionContext* context) OVERRIDE {
365 std::string actual_hash;
366 if (context->GetValueHash(*context->current_node(), &actual_hash) &&
367 actual_hash == hashed_value_)
368 return true;
369 return context->ContinueExecution();
372 private:
373 std::string hashed_value_;
374 DISALLOW_COPY_AND_ASSIGN(CompareNodeHashNot);
377 template<bool ExpectedTypeIsBooleanNotHashable>
378 class CompareNodeToStored : public Operation {
379 public:
380 explicit CompareNodeToStored(const std::string& hashed_name)
381 : hashed_name_(hashed_name) {}
382 virtual ~CompareNodeToStored() {}
383 virtual bool Execute(ExecutionContext* context) OVERRIDE {
384 const base::Value* stored_value = NULL;
385 if (!context->working_memory()->Get(hashed_name_, &stored_value))
386 return true;
387 if (ExpectedTypeIsBooleanNotHashable) {
388 if (!context->current_node()->IsType(base::Value::TYPE_BOOLEAN) ||
389 !context->current_node()->Equals(stored_value))
390 return true;
391 } else {
392 std::string actual_hash;
393 std::string stored_hash;
394 if (!context->GetValueHash(*context->current_node(), &actual_hash) ||
395 !stored_value->GetAsString(&stored_hash) ||
396 actual_hash != stored_hash)
397 return true;
399 return context->ContinueExecution();
402 private:
403 std::string hashed_name_;
404 DISALLOW_COPY_AND_ASSIGN(CompareNodeToStored);
407 class CompareNodeSubstring : public Operation {
408 public:
409 explicit CompareNodeSubstring(const std::string& hashed_pattern,
410 size_t pattern_length,
411 uint32 pattern_sum)
412 : hashed_pattern_(hashed_pattern),
413 pattern_length_(pattern_length),
414 pattern_sum_(pattern_sum) {
415 DCHECK(pattern_length_);
417 virtual ~CompareNodeSubstring() {}
418 virtual bool Execute(ExecutionContext* context) OVERRIDE {
419 std::string value_as_string;
420 if (!context->current_node()->GetAsString(&value_as_string) ||
421 !pattern_length_ || value_as_string.size() < pattern_length_)
422 return true;
423 // Go over the string with a sliding window. Meanwhile, maintain the sum in
424 // an incremental fashion, and only calculate the SHA-256 hash when the sum
425 // checks out so as to improve performance.
426 std::string::const_iterator window_begin = value_as_string.begin();
427 std::string::const_iterator window_end = window_begin + pattern_length_ - 1;
428 uint32 window_sum =
429 std::accumulate(window_begin, window_end, static_cast<uint32>(0u));
430 while (window_end != value_as_string.end()) {
431 window_sum += *window_end++;
432 if (window_sum == pattern_sum_ && context->GetHash(std::string(
433 window_begin, window_end)) == hashed_pattern_)
434 return context->ContinueExecution();
435 window_sum -= *window_begin++;
437 return true;
440 private:
441 std::string hashed_pattern_;
442 size_t pattern_length_;
443 uint32 pattern_sum_;
444 DISALLOW_COPY_AND_ASSIGN(CompareNodeSubstring);
447 class StopExecutingSentenceOperation : public Operation {
448 public:
449 StopExecutingSentenceOperation() {}
450 virtual ~StopExecutingSentenceOperation() {}
451 virtual bool Execute(ExecutionContext* context) OVERRIDE {
452 return false;
455 private:
456 DISALLOW_COPY_AND_ASSIGN(StopExecutingSentenceOperation);
459 class Parser {
460 public:
461 explicit Parser(const std::string& program)
462 : program_(program),
463 next_instruction_index_(0u) {}
464 ~Parser() {}
465 bool ParseNextSentence(ScopedVector<Operation>* output) {
466 ScopedVector<Operation> operators;
467 bool sentence_ended = false;
468 while (next_instruction_index_ < program_.size() && !sentence_ended) {
469 uint8 op_code = 0;
470 if (!ReadOpCode(&op_code))
471 return false;
472 switch (static_cast<jtl_foundation::OpCodes>(op_code)) {
473 case jtl_foundation::NAVIGATE: {
474 std::string hashed_key;
475 if (!ReadHash(&hashed_key))
476 return false;
477 operators.push_back(new NavigateOperation(hashed_key));
478 break;
480 case jtl_foundation::NAVIGATE_ANY:
481 operators.push_back(new NavigateAnyOperation);
482 break;
483 case jtl_foundation::NAVIGATE_BACK:
484 operators.push_back(new NavigateBackOperation);
485 break;
486 case jtl_foundation::STORE_BOOL: {
487 std::string hashed_name;
488 if (!ReadHash(&hashed_name) || !IsStringUTF8(hashed_name))
489 return false;
490 bool value = false;
491 if (!ReadBool(&value))
492 return false;
493 operators.push_back(new StoreValue(
494 hashed_name,
495 scoped_ptr<base::Value>(new base::FundamentalValue(value))));
496 break;
498 case jtl_foundation::COMPARE_STORED_BOOL: {
499 std::string hashed_name;
500 if (!ReadHash(&hashed_name) || !IsStringUTF8(hashed_name))
501 return false;
502 bool value = false;
503 if (!ReadBool(&value))
504 return false;
505 bool default_value = false;
506 if (!ReadBool(&default_value))
507 return false;
508 operators.push_back(new CompareStoredValue(
509 hashed_name,
510 scoped_ptr<base::Value>(new base::FundamentalValue(value)),
511 scoped_ptr<base::Value>(
512 new base::FundamentalValue(default_value))));
513 break;
515 case jtl_foundation::STORE_HASH: {
516 std::string hashed_name;
517 if (!ReadHash(&hashed_name) || !IsStringUTF8(hashed_name))
518 return false;
519 std::string hashed_value;
520 if (!ReadHash(&hashed_value))
521 return false;
522 operators.push_back(new StoreValue(
523 hashed_name,
524 scoped_ptr<base::Value>(new base::StringValue(hashed_value))));
525 break;
527 case jtl_foundation::COMPARE_STORED_HASH: {
528 std::string hashed_name;
529 if (!ReadHash(&hashed_name) || !IsStringUTF8(hashed_name))
530 return false;
531 std::string hashed_value;
532 if (!ReadHash(&hashed_value))
533 return false;
534 std::string hashed_default_value;
535 if (!ReadHash(&hashed_default_value))
536 return false;
537 operators.push_back(new CompareStoredValue(
538 hashed_name,
539 scoped_ptr<base::Value>(new base::StringValue(hashed_value)),
540 scoped_ptr<base::Value>(
541 new base::StringValue(hashed_default_value))));
542 break;
544 case jtl_foundation::STORE_NODE_BOOL: {
545 std::string hashed_name;
546 if (!ReadHash(&hashed_name) || !IsStringUTF8(hashed_name))
547 return false;
548 operators.push_back(new StoreNodeValue<true>(hashed_name));
549 break;
551 case jtl_foundation::STORE_NODE_HASH: {
552 std::string hashed_name;
553 if (!ReadHash(&hashed_name) || !IsStringUTF8(hashed_name))
554 return false;
555 operators.push_back(new StoreNodeValue<false>(hashed_name));
556 break;
558 case jtl_foundation::STORE_NODE_EFFECTIVE_SLD_HASH: {
559 std::string hashed_name;
560 if (!ReadHash(&hashed_name) || !IsStringUTF8(hashed_name))
561 return false;
562 operators.push_back(new StoreNodeEffectiveSLD(hashed_name));
563 break;
565 case jtl_foundation::COMPARE_NODE_BOOL: {
566 bool value = false;
567 if (!ReadBool(&value))
568 return false;
569 operators.push_back(new CompareNodeBool(value));
570 break;
572 case jtl_foundation::COMPARE_NODE_HASH: {
573 std::string hashed_value;
574 if (!ReadHash(&hashed_value))
575 return false;
576 operators.push_back(new CompareNodeHash(hashed_value));
577 break;
579 case jtl_foundation::COMPARE_NODE_HASH_NOT: {
580 std::string hashed_value;
581 if (!ReadHash(&hashed_value))
582 return false;
583 operators.push_back(new CompareNodeHashNot(hashed_value));
584 break;
586 case jtl_foundation::COMPARE_NODE_TO_STORED_BOOL: {
587 std::string hashed_name;
588 if (!ReadHash(&hashed_name) || !IsStringUTF8(hashed_name))
589 return false;
590 operators.push_back(new CompareNodeToStored<true>(hashed_name));
591 break;
593 case jtl_foundation::COMPARE_NODE_TO_STORED_HASH: {
594 std::string hashed_name;
595 if (!ReadHash(&hashed_name) || !IsStringUTF8(hashed_name))
596 return false;
597 operators.push_back(new CompareNodeToStored<false>(hashed_name));
598 break;
600 case jtl_foundation::COMPARE_NODE_SUBSTRING: {
601 std::string hashed_pattern;
602 uint32 pattern_length = 0, pattern_sum = 0;
603 if (!ReadHash(&hashed_pattern))
604 return false;
605 if (!ReadUint32(&pattern_length) || pattern_length == 0)
606 return false;
607 if (!ReadUint32(&pattern_sum))
608 return false;
609 operators.push_back(new CompareNodeSubstring(
610 hashed_pattern, pattern_length, pattern_sum));
611 break;
613 case jtl_foundation::STOP_EXECUTING_SENTENCE:
614 operators.push_back(new StopExecutingSentenceOperation);
615 break;
616 case jtl_foundation::END_OF_SENTENCE:
617 sentence_ended = true;
618 break;
619 default:
620 return false;
623 output->swap(operators);
624 return true;
627 bool HasNextSentence() const {
628 return next_instruction_index_ < program_.size();
631 private:
632 // Reads an uint8 and returns whether this operation was successful.
633 bool ReadUint8(uint8* out) {
634 DCHECK(out);
635 if (next_instruction_index_ + 1u > program_.size())
636 return false;
637 *out = static_cast<uint8>(program_[next_instruction_index_]);
638 ++next_instruction_index_;
639 return true;
642 // Reads an uint32 and returns whether this operation was successful.
643 bool ReadUint32(uint32* out) {
644 DCHECK(out);
645 if (next_instruction_index_ + 4u > program_.size())
646 return false;
647 *out = 0u;
648 for (int i = 0; i < 4; ++i) {
649 *out >>= 8;
650 *out |= static_cast<uint8>(program_[next_instruction_index_]) << 24;
651 ++next_instruction_index_;
653 return true;
656 // Reads an operator code and returns whether this operation was successful.
657 bool ReadOpCode(uint8* out) { return ReadUint8(out); }
659 bool ReadHash(std::string* out) {
660 DCHECK(out);
661 if (next_instruction_index_ + jtl_foundation::kHashSizeInBytes >
662 program_.size())
663 return false;
664 *out = program_.substr(next_instruction_index_,
665 jtl_foundation::kHashSizeInBytes);
666 next_instruction_index_ += jtl_foundation::kHashSizeInBytes;
667 DCHECK(jtl_foundation::Hasher::IsHash(*out));
668 return true;
671 bool ReadBool(bool* out) {
672 DCHECK(out);
673 uint8 value = 0;
674 if (!ReadUint8(&value))
675 return false;
676 if (value == 0)
677 *out = false;
678 else if (value == 1)
679 *out = true;
680 else
681 return false;
682 return true;
685 std::string program_;
686 size_t next_instruction_index_;
687 DISALLOW_COPY_AND_ASSIGN(Parser);
690 } // namespace
692 JtlInterpreter::JtlInterpreter(
693 const std::string& hasher_seed,
694 const std::string& program,
695 const base::DictionaryValue* input)
696 : hasher_seed_(hasher_seed),
697 program_(program),
698 input_(input),
699 working_memory_(new base::DictionaryValue),
700 result_(OK) {
701 DCHECK(input->IsType(base::Value::TYPE_DICTIONARY));
704 JtlInterpreter::~JtlInterpreter() {}
706 void JtlInterpreter::Execute() {
707 jtl_foundation::Hasher hasher(hasher_seed_);
708 Parser parser(program_);
709 while (parser.HasNextSentence()) {
710 ScopedVector<Operation> sentence;
711 if (!parser.ParseNextSentence(&sentence)) {
712 result_ = PARSE_ERROR;
713 return;
715 ExecutionContext context(
716 &hasher, sentence.get(), input_, working_memory_.get());
717 context.ContinueExecution();
718 if (context.error()) {
719 result_ = RUNTIME_ERROR;
720 return;
725 bool JtlInterpreter::GetOutputBoolean(const std::string& unhashed_key,
726 bool* output) const {
727 std::string hashed_key =
728 jtl_foundation::Hasher(hasher_seed_).GetHash(unhashed_key);
729 return working_memory_->GetBoolean(hashed_key, output);
732 bool JtlInterpreter::GetOutputString(const std::string& unhashed_key,
733 std::string* output) const {
734 std::string hashed_key =
735 jtl_foundation::Hasher(hasher_seed_).GetHash(unhashed_key);
736 return working_memory_->GetString(hashed_key, output);