Class: Jscall::Exported

Inherits:
Object
  • Object
show all
Defined in:
lib/jscall.rb

Overview

inbound referneces from Node to Ruby

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExported

Returns a new instance of Exported.



42
43
44
45
46
47
48
# File 'lib/jscall.rb', line 42

def initialize
    ary = Array.new(Jscall::TableSize) {|i| i + 1}
    ary[-1] = -1
    @objects = HiddenRef.new(ary)
    @free_element = 0
    @hashtable = HiddenRef.new({})
end

Instance Attribute Details

#objectsObject (readonly)

Returns the value of attribute objects.



40
41
42
# File 'lib/jscall.rb', line 40

def objects
  @objects
end

Instance Method Details

#export(obj) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jscall.rb', line 50

def export(obj)
    hash = @hashtable.__getobj__
    if hash.include?(obj)
        hash[obj]
    else
        objs = @objects.__getobj__
        idx = @free_element
        if idx < 0
            idx = objs.size
        else
            @free_element = objs[idx]
        end
        objs[idx] = obj
        hash[obj] = idx   # return idx
    end
end

#find(idx) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/jscall.rb', line 67

def find(idx)
    obj = @objects.__getobj__[idx]
    if obj.is_a?(Numeric)
        raise JavaScriptError.new("unknown index is given to find(): #{idx}")
    else
        obj
    end
end

#remove(idx) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/jscall.rb', line 76

def remove(idx)
    objects = @objects.__getobj__
    obj = objects[idx]
    if obj.is_a?(Numeric)
        raise JavaScriptError.new("unknown index is given to remove(): #{idx}")
    else
        objects[idx] = @free_element
        @free_element = idx
        @hashtable.__getobj__.delete(obj)
    end
end