Method: LEON.type_check

Defined in:
lib/io.rb

.type_check(v) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/io.rb', line 164

def self.type_check(v)
  if v === nil
    return Constants::NULL
  elsif v === true
    return Constants::BOOLEAN
  elsif v === false
    return Constants::BOOLEAN + 1
  elsif v.kind_of? LEON::NaN
    return Constants::NAN
  elsif v.kind_of? LEON::Undefined
    return Constants::UNDEFINED
  elsif v.kind_of? Date or v.kind_of? Time
    return Constants::DATE
  elsif v.kind_of? LEON::StringBuffer
    return Constants::BUFFER
  elsif v.kind_of? LEON::RegExp
    return Constants::REGEXP
  elsif v === Float::INFINITY
    return Constants::INFINITY
  elsif v === -1/0.0
    return Constants::MINUS_INFINITY
  elsif v.kind_of?(Array)
    return Constants::ARRAY
  elsif v.kind_of?(Hash)
    return Constants::OBJECT
  elsif v.is_a? String
    return Constants::STRING
  elsif v.kind_of? Symbol
    return Constants::STRING
  elsif v.is_a? Fixnum
    if v < 0
      v = v.abs
      if v <= (1 << 7)
        return Constants::CHAR
      elsif v <= (1 << 15)
        return Constants::SHORT
      elsif v <= (1 << 31)
        return Constants::INT
      end
      return Constants::DOUBLE
    end
    if v < (1 << 8)
      return Constants::UNSIGNED_CHAR
    end
    if v < (1 << 16)
      return Constants::UNSIGNED_SHORT
    end
    if v < (1 << 32)
      return Constants::UNSIGNED_INT
    end
    return Constants::DOUBLE
  elsif v.is_a? Float
    v = v.abs
    log = Math::log(v)/Math::log(2)
    if log < 0
      log = log.ceil
    else
      log = log.floor
    end
    exp = 103 + log
    if exp < 0 or exp > 255
      return Constants::DOUBLE
    end
    v *= 2**(-log + 24)
    if v.floor != v
      return Constants::DOUBLE
    end
    return Constants::FLOAT
  elsif v.kind_of? Object
    return Constants::NATIVE_OBJECT
  end
end