1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
#pragma once
#include <iostream>
#include <cassert>
#include "string.hpp"
namespace Spasm
{
enum class ValueType
{
Number,
Null,
Undefined,
Boolean,
String,
Array,
Object,
Function,
};
static_assert(int(ValueType::Function) < 8, "Too many types");
struct Value
{
Value() { m_value.as_double = 0.0; }
explicit Value(double v) { m_value.as_double = v; }
explicit Value(bool v) : Value(ValueType::Boolean, (uint64_t)v) {}
Value(ValueType tag, uint64_t payload)
{
assert(tag != ValueType::Number && "Zero tag is plain double");
assert(!(payload >> 48) && "Use only pointers to the heap.");
m_value.as_pointer.pointer = payload;
m_value.as_pointer.tag = uint64_t(tag);
m_value.as_pointer.nan = 0x1fff;
}
Value(ValueType tag, void* pointer) : Value(tag, (uint64_t)pointer) {}
struct NanPointer
{
uint64_t pointer : 48;
uint64_t tag : 3;
uint64_t nan : 13;
};
struct CheckType
{
uint64_t payload : 48;
uint64_t check : 16;
};
union {
double as_double;
NanPointer as_pointer;
CheckType to_check;
uint64_t as_int64;
} m_value;
static_assert(sizeof(double) == 8, "unsupported arch");
bool is_double() const { return m_value.to_check.check <= 0xFFF8; }
double get_double() const
{
assert(is_double());
return m_value.as_double;
}
ValueType get_type() const
{
return is_double() ? ValueType::Number
: ValueType(m_value.as_pointer.tag);
}
void* get_pointer() const
{
return (void*)(uint64_t)m_value.as_pointer.pointer;
}
bool get_boolean() const
{
assert(get_type() == ValueType::Boolean);
return m_value.as_pointer.pointer != 0;
}
explicit operator bool() const
{
// NaN will be true. Assume that is ok.
return (get_type() != ValueType::Boolean && m_value.as_int64 != 0) ||
get_boolean();
}
};
inline std::istream& operator>>(std::istream& input, Value& value)
{
return input >> value.m_value.as_double;
}
inline std::ostream& operator<<(std::ostream& output, const Value& value)
{
switch (value.get_type())
{
case ValueType::Number:
return output << value.m_value.as_double;
case ValueType::Boolean:
return output << bool(value);
case ValueType::String:
{
const auto s = static_cast<const SpasmImpl::SPStringValue*>(
value.get_pointer());
return output << s->GetValue();
}
default:
break;
}
return output;
}
inline Value operator+(const Value& lhs, const Value& rhs)
{
return Value(lhs.get_double() + rhs.get_double());
}
inline Value operator-(const Value& lhs, const Value& rhs)
{
return Value(lhs.get_double() - rhs.get_double());
}
inline Value operator*(const Value& lhs, const Value& rhs)
{
return Value(lhs.get_double() * rhs.get_double());
}
inline Value operator/(const Value& lhs, const Value& rhs)
{
return Value(lhs.get_double() / rhs.get_double());
}
inline Value operator%(const Value& lhs, const Value& rhs)
{
return Value(double(int64_t(lhs.get_double()) % int64_t(rhs.get_double())));
}
inline Value operator<(const Value& lhs, const Value& rhs)
{
return Value(lhs.get_double() < rhs.get_double());
}
inline Value operator>(const Value& lhs, const Value& rhs)
{
return Value(lhs.get_double() > rhs.get_double());
}
inline Value operator<=(const Value& lhs, const Value& rhs)
{
return Value(lhs.get_double() <= rhs.get_double());
}
inline Value operator>=(const Value& lhs, const Value& rhs)
{
return Value(lhs.get_double() >= rhs.get_double());
}
inline Value operator==(const Value& lhs, const Value& rhs)
{
return Value(lhs.get_double() == rhs.get_double());
}
inline Value operator!=(const Value& lhs, const Value& rhs)
{
return Value(lhs.get_double() != rhs.get_double());
}
} // namespace Spasm
|