Class: Marshal::WriteBuffer
- Defined in:
- opal/opal/corelib/marshal/write_buffer.rb
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
Instance Method Summary collapse
- #append(s) ⇒ Object
- #append_symbol(sym) ⇒ Object
-
#initialize(object) ⇒ WriteBuffer
constructor
A new instance of WriteBuffer.
- #save_link(object) ⇒ Object
- #version ⇒ Object
- #write(object = @object) ⇒ Object
- #write_array(a) ⇒ Object
- #write_bignum(n) ⇒ Object
- #write_class(klass) ⇒ Object
- #write_extends(object) ⇒ Object
- #write_fixnum(n) ⇒ Object
- #write_float(f) ⇒ Object
- #write_hash(h) ⇒ Object
- #write_ivars_prefix(object) ⇒ Object
- #write_ivars_suffix(object, force = false) ⇒ Object
- #write_module(mod) ⇒ Object
- #write_object(obj) ⇒ Object
- #write_object_link(idx) ⇒ Object
- #write_regexp(regexp) ⇒ Object
- #write_string(s) ⇒ Object
- #write_user_class(klass, object) ⇒ Object
- #write_userdef(object) ⇒ Object
- #write_usr_marshal(object) ⇒ Object
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
#buffer ⇒ Object (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
408 409 410 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 408 def append(s) `#{@buffer} += #{s}` end |
#append_symbol(sym) ⇒ Object
285 286 287 288 289 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 285 def append_symbol(sym) append(':') write_fixnum(sym.length) append(sym) end |
#save_link(object) ⇒ Object
372 373 374 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 372 def save_link(object) @cache << object.object_id end |
#version ⇒ Object
412 413 414 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 412 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 |
# 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
291 292 293 294 295 296 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 291 def write_array(a) write_fixnum(a.length) a.each do |item| write(item) end end |
#write_bignum(n) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 260 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
311 312 313 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 311 def write_class(klass) write_string(klass.name) end |
#write_extends(object) ⇒ Object
348 349 350 351 352 353 354 355 356 357 358 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 348 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
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 251 252 253 254 255 256 257 258 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 226 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
324 325 326 327 328 329 330 331 332 333 334 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 324 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
298 299 300 301 302 303 304 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 298 def write_hash(h) write_fixnum(h.length) h.each do |key, value| write(key) write(value) end end |
#write_ivars_prefix(object) ⇒ Object
402 403 404 405 406 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 402 def write_ivars_prefix(object) unless object.instance_variables.empty? append('I') end end |
#write_ivars_suffix(object, force = false) ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 336 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
315 316 317 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 315 def write_module(mod) write_string(mod.name) end |
#write_object(obj) ⇒ Object
306 307 308 309 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 306 def write_object(obj) append_symbol(obj.class.name) write_ivars_suffix(obj, true) end |
#write_object_link(idx) ⇒ Object
367 368 369 370 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 367 def write_object_link(idx) append('@') write_fixnum(idx) end |
#write_regexp(regexp) ⇒ Object
319 320 321 322 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 319 def write_regexp(regexp) write_string(regexp.to_s) append(`String.fromCharCode(#{regexp.})`) end |
#write_string(s) ⇒ Object
280 281 282 283 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 280 def write_string(s) write_fixnum(s.length) append(s) end |
#write_user_class(klass, object) ⇒ Object
360 361 362 363 364 365 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 360 def write_user_class(klass, object) unless object.class.equal?(klass) append('C') append_symbol(object.class.name) end end |
#write_userdef(object) ⇒ Object
389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 389 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
376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 376 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 |