• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 the V8 project 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.
4 
5 #include "src/ast/compile-time-value.h"
6 
7 #include "src/ast/ast.h"
8 #include "src/factory.h"
9 #include "src/handles-inl.h"
10 #include "src/isolate.h"
11 #include "src/objects-inl.h"
12 
13 namespace v8 {
14 namespace internal {
15 
IsCompileTimeValue(Expression * expression)16 bool CompileTimeValue::IsCompileTimeValue(Expression* expression) {
17   if (expression->IsLiteral()) return true;
18   MaterializedLiteral* lit = expression->AsMaterializedLiteral();
19   return lit != NULL && lit->is_simple();
20 }
21 
GetValue(Isolate * isolate,Expression * expression)22 Handle<FixedArray> CompileTimeValue::GetValue(Isolate* isolate,
23                                               Expression* expression) {
24   Factory* factory = isolate->factory();
25   DCHECK(IsCompileTimeValue(expression));
26   Handle<FixedArray> result = factory->NewFixedArray(2, TENURED);
27   ObjectLiteral* object_literal = expression->AsObjectLiteral();
28   if (object_literal != NULL) {
29     DCHECK(object_literal->is_simple());
30     if (object_literal->fast_elements()) {
31       result->set(kLiteralTypeSlot, Smi::FromInt(OBJECT_LITERAL_FAST_ELEMENTS));
32     } else {
33       result->set(kLiteralTypeSlot, Smi::FromInt(OBJECT_LITERAL_SLOW_ELEMENTS));
34     }
35     result->set(kElementsSlot, *object_literal->constant_properties());
36   } else {
37     ArrayLiteral* array_literal = expression->AsArrayLiteral();
38     DCHECK(array_literal != NULL && array_literal->is_simple());
39     result->set(kLiteralTypeSlot, Smi::FromInt(ARRAY_LITERAL));
40     result->set(kElementsSlot, *array_literal->constant_elements());
41   }
42   return result;
43 }
44 
GetLiteralType(Handle<FixedArray> value)45 CompileTimeValue::LiteralType CompileTimeValue::GetLiteralType(
46     Handle<FixedArray> value) {
47   Smi* literal_type = Smi::cast(value->get(kLiteralTypeSlot));
48   return static_cast<LiteralType>(literal_type->value());
49 }
50 
GetElements(Handle<FixedArray> value)51 Handle<HeapObject> CompileTimeValue::GetElements(Handle<FixedArray> value) {
52   return Handle<HeapObject>(HeapObject::cast(value->get(kElementsSlot)));
53 }
54 
55 }  // namespace internal
56 }  // namespace v8
57