Class: Marshal::WriteBuffer

Inherits:
Object show all
Defined in:
opal/opal/corelib/marshal/write_buffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ WriteBuffer

Returns a new instance of WriteBuffer



180
181
182
183
184
185
186
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 180

def initialize(object)
  @object = object
  @buffer = BinaryString.new
  @cache = []
  @extends = Hash.new { |h, k| h[k] = [] }
  append(version)
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer



178
179
180
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 178

def buffer
  @buffer
end

Instance Method Details

#append(s) ⇒ Object



375
376
377
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 375

def append(s)
  @buffer += s
end

#append_symbol(sym) ⇒ Object



252
253
254
255
256
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 252

def append_symbol(sym)
  append(':')
  write_fixnum(sym.length)
  append(sym)
end


339
340
341
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 339

def save_link(object)
  @cache << object.object_id
end

#versionObject



379
380
381
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 379

def version
  `String.fromCharCode(#{MAJOR_VERSION}, #{MINOR_VERSION})`
end

#write(object = @object) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 188

def write(object = @object)
  if idx = @cache.index(object.object_id)
    write_object_link(idx)
  elsif object.respond_to?(:marshal_dump)
    write_usr_marshal(object)
  elsif object.respond_to?(:_dump)
    write_userdef(object)
  else
    case object
    when nil, true, false, Proc, Method, MatchData, Range, Struct, Array, Class, Module, Hash, Regexp
      object.__marshal__(self)
    when Integer
      Fixnum.instance_method(:__marshal__).bind(object).call(self)
    when Float
      Float.instance_method(:__marshal__).bind(object).call(self)
    when String
      String.instance_method(:__marshal__).bind(object).call(self)
    else
      BasicObject.instance_method(:__marshal__).bind(object).call(self)
    end
  end

  @buffer
end

#write_array(a) ⇒ Object



258
259
260
261
262
263
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 258

def write_array(a)
  write_fixnum(a.length)
  a.each do |item|
    write(item)
  end
end

#write_class(klass) ⇒ Object



278
279
280
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 278

def write_class(klass)
  write_string(klass.name)
end

#write_extends(object) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 315

def write_extends(object)
  singleton_mods = object.singleton_class.ancestors.reject { |mod| mod.is_a?(Class) }
  class_mods = object.class.ancestors.reject { |mod| mod.is_a?(Class) }
  own_mods = singleton_mods - class_mods
  if own_mods.length > 0
    own_mods.each do |mod|
      append('e')
      append_symbol(mod.name)
    end
  end
end

#write_fixnum(n) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 213

def write_fixnum(n)
  %x{
    var s;

    if (n == 0) {
      s = String.fromCharCode(n);
    } else if (n > 0 && n < 123) {
      s = String.fromCharCode(n + 5);
    } else if (n < 0 && n > -124) {
      s = String.fromCharCode(256 + n - 5);
    } else {
      s = "";
      var cnt = 0;
      for (var i = 0; i < 4; i++) {
        var b = n & 255;
        s += String.fromCharCode(b);
        n >>= 8
        cnt += 1;
        if (n === 0 || n === -1) {
          break;
        }
      }
      var l_byte;
      if (n < 0) {
        l_byte = 256 - cnt;
      } else {
        l_byte = cnt;
      }
      s = String.fromCharCode(l_byte) + s;
    }
    #{append(`s`)}
  }
end

#write_float(f) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 291

def write_float(f)
  if f.equal?(Float::INFINITY)
    write_string('inf')
  elsif f.equal?(-Float::INFINITY)
    write_string('-inf')
  elsif f.equal?(Float::NAN)
    write_string('nan')
  else
    write_string(f.to_s)
  end
end

#write_hash(h) ⇒ Object



265
266
267
268
269
270
271
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 265

def write_hash(h)
  write_fixnum(h.length)
  h.each do |key, value|
    write(key)
    write(value)
  end
end

#write_ivars_prefix(object) ⇒ Object



369
370
371
372
373
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 369

def write_ivars_prefix(object)
  if object.instance_variables.length > 0
    append('I')
  end
end

#write_ivars_suffix(object, force = false) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 303

def write_ivars_suffix(object, force = false)
  if object.instance_variables.length == 0 && !force
    return
  end

  write_fixnum(object.instance_variables.length)
  object.instance_variables.each do |ivar_name|
    append_symbol(ivar_name)
    write(object.instance_variable_get(ivar_name))
  end
end

#write_module(mod) ⇒ Object



282
283
284
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 282

def write_module(mod)
  write_string(mod.name)
end

#write_object(obj) ⇒ Object



273
274
275
276
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 273

def write_object(obj)
  append_symbol(obj.class.name)
  write_ivars_suffix(obj, true)
end


334
335
336
337
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 334

def write_object_link(idx)
  append('@')
  write_fixnum(idx)
end

#write_regexp(regexp) ⇒ Object



286
287
288
289
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 286

def write_regexp(regexp)
  write_string(regexp.to_s)
  append(`String.fromCharCode(#{regexp.options})`)
end

#write_string(s) ⇒ Object



247
248
249
250
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 247

def write_string(s)
  write_fixnum(s.length)
  append(s)
end

#write_user_class(klass, object) ⇒ Object



327
328
329
330
331
332
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 327

def write_user_class(klass, object)
  unless object.class.equal?(klass)
    append('C')
    append_symbol(object.class.name)
  end
end

#write_userdef(object) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 356

def write_userdef(object)
  value = object._dump(0)

  unless value.is_a?(String)
    raise TypeError, "_dump() must return string"
  end

  write_ivars_prefix(value)
  append('u')
  append_symbol(object.class.name)
  write_string(value)
end

#write_usr_marshal(object) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 343

def write_usr_marshal(object)
  value = object.marshal_dump
  klass = object.class
  append('U')
  namespace = `#{klass}.$$base_module`
  if namespace.equal?(Object)
    append_symbol(`#{klass}.$$name`)
  else
    append_symbol(namespace.name + '::' + `#{klass}.$$name`)
  end
  write(value)
end