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

[View source]

185
186
187
188
189
190
191
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 185

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


183
184
185
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 183

def buffer
  @buffer
end

Instance Method Details

#append(s) ⇒ Object

[View source]

400
401
402
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 400

def append(s)
  @buffer += s
end

#append_symbol(sym) ⇒ Object

[View source]

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

def append_symbol(sym)
  append(':')
  write_fixnum(sym.length)
  append(sym)
end
[View source]

364
365
366
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 364

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

#versionObject

[View source]

404
405
406
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 404

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

#write(object = @object) ⇒ Object

[View source]

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 193

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

[View source]

283
284
285
286
287
288
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 283

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

#write_bignum(n) ⇒ Object

[View source]

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 252

def write_bignum(n)
  sign = n > 0 ? '+' : '-'
  append(sign)

  num = n > 0 ? n : -n

  arr = []
  while num > 0
    arr << (num & 0xffff)
    num = (num / 0x10000).floor
  end

  write_fixnum(arr.size)

  arr.each do |x|
    append(`String.fromCharCode(x & 0xff)`)
    append(`String.fromCharCode(#{(x/0x100).floor})`)
  end
end

#write_class(klass) ⇒ Object

[View source]

303
304
305
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 303

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

#write_extends(object) ⇒ Object

[View source]

340
341
342
343
344
345
346
347
348
349
350
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 340

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

[View source]

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
246
247
248
249
250
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 218

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

[View source]

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

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

[View source]

290
291
292
293
294
295
296
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 290

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

#write_ivars_prefix(object) ⇒ Object

[View source]

394
395
396
397
398
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 394

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

#write_ivars_suffix(object, force = false) ⇒ Object

[View source]

328
329
330
331
332
333
334
335
336
337
338
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 328

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

[View source]

307
308
309
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 307

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

#write_object(obj) ⇒ Object

[View source]

298
299
300
301
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 298

def write_object(obj)
  append_symbol(obj.class.name)
  write_ivars_suffix(obj, true)
end
[View source]

359
360
361
362
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 359

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

#write_regexp(regexp) ⇒ Object

[View source]

311
312
313
314
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 311

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

#write_string(s) ⇒ Object

[View source]

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

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

#write_user_class(klass, object) ⇒ Object

[View source]

352
353
354
355
356
357
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 352

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

#write_userdef(object) ⇒ Object

[View source]

381
382
383
384
385
386
387
388
389
390
391
392
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 381

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

[View source]

368
369
370
371
372
373
374
375
376
377
378
379
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 368

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