Module: RubyRunJs::Helper
- Includes:
- ConversionHelper
- Included in:
- BuiltInContext, JsArrayMethods, JsBaseObject, JsBooleanMethods, JsDateMethods, JsErrorMethods, JsFunctionMethods, JsGlobalMethods, JsJsonMethods, JsMathMethods, JsNumberMethods, JsObjectMethods, JsRegExpMethods, JsStringMethods, OPCODES, OPCODES::OP_CODE, Operation
- Defined in:
- lib/ruby_run_js/helper.rb
Instance Method Summary
collapse
#convert_to_js_type, #to_boolean, #to_int32, #to_integer, #to_number, #to_object, #to_primitive, #to_string, #to_uint16, #to_uint32
Instance Method Details
#check_object(obj) ⇒ Object
50
51
52
53
54
|
# File 'lib/ruby_run_js/helper.rb', line 50
def check_object(obj)
if obj.js_type == :Undefined || obj.js_type == :Null
raise make_error('TypeError', 'undefined or null can\'t be converted to object')
end
end
|
#get_member(obj, prop, builtin) ⇒ Object
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
|
# File 'lib/ruby_run_js/helper.rb', line 56
def get_member(obj, prop, builtin)
type = obj.js_type
if is_primitive(obj)
case type
when :String
if prop.js_type == :Number && prop.finite?
index = prop.to_i
if index == prop && index >= 0 && index < obj.length
return obj[index]
end
end
s_prop = to_string(prop)
if s_prop == 'length'
obj.length.to_f
elsif s_prop =~ /^\d+$/
index = s_prop.to_i
if index >= 0 && index < obj.length
return obj[index]
end
end
return builtin.string_prototype.get(s_prop)
when :Number
return builtin.number_prototype.get(to_string(prop))
when :Boolean
return builtin.boolean_prototype.get(to_string(prop))
when :Null
raise make_error('TypeError', "Cannot read property '#{prop}' of null")
when :Undefined
raise make_error('TypeError', "Cannot read property '#{prop}' of undefined")
end
end
obj.get(to_string(prop))
end
|
#get_member_dot(obj, prop, builtin) ⇒ Object
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
|
# File 'lib/ruby_run_js/helper.rb', line 92
def get_member_dot(obj, prop, builtin)
if is_primitive(obj)
case obj.js_type
when :String
if prop == 'length'
obj.length.to_f
elsif prop =~ /^\d+$/
index = prop.to_i
if index >= 0 && index < obj.length
return obj[index]
end
end
return builtin.string_prototype.get(prop)
when :Number
return builtin.number_prototype.get(to_string(prop))
when :Boolean
return builtin.boolean_prototype.get(to_string(prop))
when :Null
raise make_error('TypeError', "Cannot read property '#{prop}' of null")
when :Undefined
raise make_error('TypeError', "Cannot read property '#{prop}' of undefined")
end
end
obj.get(prop)
end
|
#is_accessor_descriptor(desc) ⇒ Object
19
20
21
|
# File 'lib/ruby_run_js/helper.rb', line 19
def is_accessor_descriptor(desc)
desc && desc != undefined && (desc.key?('get') || desc.key?('set'))
end
|
#is_callable(func) ⇒ Object
27
28
29
|
# File 'lib/ruby_run_js/helper.rb', line 27
def is_callable(func)
func.respond_to? :call
end
|
#is_data_descriptor(desc) ⇒ Object
15
16
17
|
# File 'lib/ruby_run_js/helper.rb', line 15
def is_data_descriptor(desc)
desc && desc != undefined && (desc.key?('value') || desc.key?('writable'))
end
|
#is_generic_descriptor(desc) ⇒ Object
23
24
25
|
# File 'lib/ruby_run_js/helper.rb', line 23
def is_generic_descriptor(desc)
desc && desc != undefined && !(is_data_descriptor(desc) || is_accessor_descriptor(desc))
end
|
#is_primitive(value) ⇒ Object
31
32
33
|
# File 'lib/ruby_run_js/helper.rb', line 31
def is_primitive(value)
[:Undefined, :Null, :Boolean, :Number, :String].include?(value.js_type)
end
|
#make_error(error_type, message = 'no info', throw_value = nil) ⇒ Object
11
12
13
|
# File 'lib/ruby_run_js/helper.rb', line 11
def make_error(error_type, message = 'no info', throw_value = nil)
JsException.new(error_type, message, throw_value)
end
|
#strict_equality(a, b) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/ruby_run_js/helper.rb', line 35
def strict_equality(a, b)
type = a.js_type
if type != b.js_type
return false
end
case type
when :Undefined, :Null
return true
when :Boolean, :String, :Number
return a == b
else
return a.equal?(b)
end
end
|