Class: Marshal.self::WriteBuffer

Inherits:
Object
  • 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.



193
194
195
196
197
198
199
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 193

def initialize(object)
  @object = object
  @buffer = ''
  @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



409
410
411
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 409

def append(s)
  `#{@buffer} += #{s}`
end

#append_symbol(sym) ⇒ Object



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

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


373
374
375
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 373

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

#versionObject



413
414
415
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 413

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

#write(object = @object) ⇒ Object



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
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 201

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
      ::Integer.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

  `binaryString(#{@buffer})`
end

#write_array(a) ⇒ Object



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

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

#write_bignum(n) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 261

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



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

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

#write_extends(object) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 349

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
  unless own_mods.empty?
    own_mods.each do |mod|
      append('e')
      append_symbol(mod.name)
    end
  end
end

#write_fixnum(n) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 227

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



325
326
327
328
329
330
331
332
333
334
335
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 325

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



299
300
301
302
303
304
305
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 299

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

#write_ivars_prefix(object) ⇒ Object



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

def write_ivars_prefix(object)
  unless object.instance_variables.empty?
    append('I')
  end
end

#write_ivars_suffix(object, force = false) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 337

def write_ivars_suffix(object, force = false)
  if object.instance_variables.empty? && !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



316
317
318
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 316

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

#write_object(obj) ⇒ Object



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

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


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

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

#write_regexp(regexp) ⇒ Object



320
321
322
323
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 320

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

#write_string(s) ⇒ Object



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

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

#write_user_class(klass, object) ⇒ Object



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

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

#write_userdef(object) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 390

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

  unless value.is_a?(::String)
    ::Kernel.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



377
378
379
380
381
382
383
384
385
386
387
388
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 377

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