Module: Twostroke::Runtime::Types
- Defined in:
- lib/twostroke/runtime/types.rb,
lib/twostroke/runtime/types/null.rb,
lib/twostroke/runtime/types/array.rb,
lib/twostroke/runtime/types/value.rb,
lib/twostroke/runtime/types/number.rb,
lib/twostroke/runtime/types/object.rb,
lib/twostroke/runtime/types/regexp.rb,
lib/twostroke/runtime/types/string.rb,
lib/twostroke/runtime/types/boolean.rb,
lib/twostroke/runtime/types/function.rb,
lib/twostroke/runtime/types/undefined.rb,
lib/twostroke/runtime/types/number_object.rb,
lib/twostroke/runtime/types/string_object.rb,
lib/twostroke/runtime/types/boolean_object.rb
Defined Under Namespace
Classes: Array, Boolean, BooleanObject, Function, Null, Number, NumberObject, Object, Primitive, RegExp, String, StringObject, Undefined, Value
Class Method Summary
collapse
Class Method Details
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/twostroke/runtime/types.rb', line 99
def self.eq(a, b)
if a.class == b.class
a === b
elsif a.is_a?(Null) && b.is_a?(Undefined)
true
elsif a.is_a?(Undefined) && b.is_a?(Null)
true
elsif a.is_a?(Number) && b.is_a?(String)
eq(a, to_number(b))
elsif a.is_a?(String) && b.is_a?(Number)
eq(to_number(a), b)
elsif a.is_a?(Boolean)
eq(to_number(a), b)
elsif b.is_a?(Boolean)
eq(a, to_number(b))
elsif (a.is_a?(String) || b.is_a?(Number)) && b.is_a?(Object)
eq(a, to_primitive(b))
elsif a.is_a?(Object) && (b.is_a?(String) || b.is_a?(Number))
eq(to_primitive(a), b)
else
false
end
end
|
.is_falsy(object) ⇒ Object
91
92
93
|
# File 'lib/twostroke/runtime/types.rb', line 91
def self.is_falsy(object)
!is_truthy(object)
end
|
.is_truthy(object) ⇒ Object
95
96
97
|
# File 'lib/twostroke/runtime/types.rb', line 95
def self.is_truthy(object)
to_boolean(object).boolean
end
|
.seq(a, b) ⇒ Object
123
124
125
126
127
128
129
|
# File 'lib/twostroke/runtime/types.rb', line 123
def self.seq(a, b)
if a.class == b.class
a === b
else
false
end
end
|
.to_boolean(object) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/twostroke/runtime/types.rb', line 10
def self.to_boolean(object)
b = if object.is_a?(Boolean)
object.boolean
elsif object.is_a?(Undefined) || object.is_a?(Null)
false
elsif object.is_a?(Number)
!object.zero? && !object.nan?
elsif object.is_a?(String)
object.string != ""
else
true
end
Boolean.new b
end
|
.to_int32(object) ⇒ Object
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/twostroke/runtime/types.rb', line 41
def self.to_int32(object)
num = to_number object
if num.nan? || num.infinite?
0
else
int32 = num.number.to_i & 0xffff_ffff
int32 -= 2 ** 32 if int32 >= 2 ** 31
int32
end
end
|
.to_number(object) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/twostroke/runtime/types.rb', line 25
def self.to_number(object)
if object.is_a?(Undefined)
Number.new Float::NAN
elsif object.is_a?(Null)
Number.new 0
elsif object.is_a?(Boolean)
Number.new(object.boolean ? 1 : 0)
elsif object.is_a?(Number)
object
elsif object.is_a?(String)
Number.new(Float(object.string)) rescue Number.new(Float::NAN)
else to_number to_primitive(object)
end
end
|
.to_primitive(object, preferred_type = nil) ⇒ Object
2
3
4
5
6
7
8
|
# File 'lib/twostroke/runtime/types.rb', line 2
def self.to_primitive(object, preferred_type = nil)
if object.is_a? Primitive
object
else
object.default_value preferred_type
end
end
|
.to_string(object) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/twostroke/runtime/types.rb', line 61
def self.to_string(object)
if object.is_a?(Undefined)
String.new "undefined"
elsif object.is_a?(Null)
String.new "null"
elsif object.is_a?(Boolean)
String.new object.boolean.to_s
elsif object.is_a?(Number)
String.new object.number.to_s
elsif object.is_a?(String)
object
else
to_string to_primitive(object)
end
end
|
.to_uint32(object) ⇒ Object
52
53
54
55
56
57
58
59
|
# File 'lib/twostroke/runtime/types.rb', line 52
def self.to_uint32(object)
num = to_number object
if num.nan? || num.infinite?
0
else
num.number.to_i & 0xffff_ffff
end
end
|