Class: RKelly::JS::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rkelly/js/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



5
6
7
8
9
10
11
12
13
# File 'lib/rkelly/js/base.rb', line 5

def initialize
  @properties = Hash.new { |h,k|
    h[k] = Property.new(k, :undefined, self)
  }
  @return     = nil
  @returned   = false
  @value      = self
  self['Class'] = self.class.to_s.split('::').last
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



4
5
6
# File 'lib/rkelly/js/base.rb', line 4

def properties
  @properties
end

#returnObject

Returns the value of attribute return.



4
5
6
# File 'lib/rkelly/js/base.rb', line 4

def return
  @return
end

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'lib/rkelly/js/base.rb', line 4

def value
  @value
end

Instance Method Details

#[](name) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/rkelly/js/base.rb', line 15

def [](name)
  return self.properties[name] if has_property?(name)
  if self.properties['prototype'].value != :undefined
    self.properties['prototype'].value[name]
  else
    RKelly::Runtime::UNDEFINED
  end
end

#[]=(name, value) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/rkelly/js/base.rb', line 24

def []=(name, value)
  return unless can_put?(name)
  if has_property?(name)
    self.properties[name].value = value
  else
    self.properties[name] = Property.new(name, value, self)
  end
end

#can_put?(name) ⇒ Boolean

Returns:



33
34
35
36
37
38
39
40
41
# File 'lib/rkelly/js/base.rb', line 33

def can_put?(name)
  if !has_property?(name)
    return true if self.properties['prototype'].nil?
    return true if self.properties['prototype'].value.nil?
    return true if self.properties['prototype'].value == :undefined
    return self.properties['prototype'].value.can_put?(name)
  end
  !self.properties[name].read_only?
end

#default_value(hint) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rkelly/js/base.rb', line 58

def default_value(hint)
  case hint
  when 'Number'
    value_of = self['valueOf']
    if value_of.function || value_of.value.is_a?(RKelly::JS::Function)
      return value_of
    end
    to_string = self['toString']
    if to_string.function || to_string.value.is_a?(RKelly::JS::Function)
      return to_string
    end
  end
end

#delete(name) ⇒ Object



51
52
53
54
55
56
# File 'lib/rkelly/js/base.rb', line 51

def delete(name)
  return true unless has_property?(name)
  return false if self.properties[name].dont_delete?
  self.properties.delete(name)
  true
end

#has_property?(name) ⇒ Boolean

Returns:



43
44
45
46
47
48
49
# File 'lib/rkelly/js/base.rb', line 43

def has_property?(name)
  return true if self.properties.has_key?(name)
  return false if self.properties['prototype'].nil?
  return false if self.properties['prototype'].value.nil?
  return false if self.properties['prototype'].value == :undefined
  self.properties['prototype'].value.has_property?(name)
end

#returned?Boolean

Returns:



77
# File 'lib/rkelly/js/base.rb', line 77

def returned?; @returned; end