Class: Marshal.self::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.
195 196 197 198 199 200 201 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 195 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.
185 186 187 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 185 def buffer @buffer end |
Instance Method Details
#append(s) ⇒ Object
411 412 413 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 411 def append(s) `#{@buffer} += #{s}` end |
#append_symbol(sym) ⇒ Object
288 289 290 291 292 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 288 def append_symbol(sym) append(':') write_fixnum(sym.length) append(sym) end |
#save_link(object) ⇒ Object
375 376 377 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 375 def save_link(object) @cache << object.object_id end |
#version ⇒ Object
415 416 417 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 415 def version `String.fromCharCode(#{MAJOR_VERSION}, #{MINOR_VERSION})` end |
#write(object = @object) ⇒ Object
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 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 203 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
294 295 296 297 298 299 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 294 def write_array(a) write_fixnum(a.length) a.each do |item| write(item) end end |
#write_bignum(n) ⇒ Object
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 263 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
314 315 316 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 314 def write_class(klass) write_string(klass.name) end |
#write_extends(object) ⇒ Object
351 352 353 354 355 356 357 358 359 360 361 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 351 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
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 260 261 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 229 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
327 328 329 330 331 332 333 334 335 336 337 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 327 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
301 302 303 304 305 306 307 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 301 def write_hash(h) write_fixnum(h.length) h.each do |key, value| write(key) write(value) end end |
#write_ivars_prefix(object) ⇒ Object
405 406 407 408 409 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 405 def write_ivars_prefix(object) unless object.instance_variables.empty? append('I') end end |
#write_ivars_suffix(object, force = false) ⇒ Object
339 340 341 342 343 344 345 346 347 348 349 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 339 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
318 319 320 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 318 def write_module(mod) write_string(mod.name) end |
#write_object(obj) ⇒ Object
309 310 311 312 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 309 def write_object(obj) append_symbol(obj.class.name) write_ivars_suffix(obj, true) end |
#write_object_link(idx) ⇒ Object
370 371 372 373 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 370 def write_object_link(idx) append('@') write_fixnum(idx) end |
#write_regexp(regexp) ⇒ Object
322 323 324 325 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 322 def write_regexp(regexp) write_string(regexp.to_s) append(`String.fromCharCode(#{regexp.})`) end |
#write_string(s) ⇒ Object
283 284 285 286 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 283 def write_string(s) write_fixnum(s.length) append(s) end |
#write_user_class(klass, object) ⇒ Object
363 364 365 366 367 368 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 363 def write_user_class(klass, object) unless object.class.equal?(klass) append('C') append_symbol(object.class.name) end end |
#write_userdef(object) ⇒ Object
392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 392 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
379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 379 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 |